Fix usart not working

pull/25/head
Alex Voinea 2021-06-17 08:40:15 -07:00
parent 58fe013471
commit 4181fbfa23
3 changed files with 11 additions and 9 deletions

View File

@ -4,7 +4,7 @@
namespace hal { namespace hal {
namespace usart { namespace usart {
USART usart1; USART usart1(USART1);
uint8_t USART::Read() { uint8_t USART::Read() {
uint8_t c = 0; uint8_t c = 0;

View File

@ -10,6 +10,10 @@
namespace hal { namespace hal {
namespace usart { namespace usart {
constexpr uint16_t UART_BAUD_SELECT(uint32_t baudRate, uint32_t xtalCpu) {
return (((double)(xtalCpu))/(((double)(baudRate))*8.0)-1.0+0.5);
}
class USART { class USART {
public: public:
struct USART_TypeDef { struct USART_TypeDef {
@ -53,10 +57,10 @@ public:
__attribute__((always_inline)) inline void Init(USART_InitTypeDef *const conf) { __attribute__((always_inline)) inline void Init(USART_InitTypeDef *const conf) {
gpio::Init(conf->rx_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Level::low)); gpio::Init(conf->rx_pin, gpio::GPIO_InitTypeDef(gpio::Mode::input, gpio::Level::low));
gpio::Init(conf->tx_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low)); gpio::Init(conf->tx_pin, gpio::GPIO_InitTypeDef(gpio::Mode::output, gpio::Level::low));
husart->UBRRx = (((double)(F_CPU)) / (((double)(conf->baudrate)) * 8.0) - 1.0 + 0.5); husart->UBRRx = UART_BAUD_SELECT(conf->baudrate, F_CPU);
husart->UCSRxA = (1 << 1); // Set double baudrate setting. Clear all other status bits/flags husart->UCSRxA |= (1 << 1); // Set double baudrate setting. Clear all other status bits/flags
// husart->UCSRxC |= (1 << 3); // 2 stop bits. Preserve data size setting // husart->UCSRxC |= (1 << 3); // 2 stop bits. Preserve data size setting
husart->UCSRxD = 0; //disable hardware flow control. This register is reserved on all AVR devides with USART. husart->UCSRxD = 0; // disable hardware flow control. Few avr MCUs have this feature, but this register is reserved on all AVR devices with USART, so we can disable it without consequences.
husart->UCSRxB = (1 << 3) | (1 << 4) | (1 << 7); // Turn on the transmission and reception circuitry and enable the RX interrupt husart->UCSRxB = (1 << 3) | (1 << 4) | (1 << 7); // Turn on the transmission and reception circuitry and enable the RX interrupt
} }
@ -83,10 +87,7 @@ public:
husart->UCSRxB &= ~(1 << 5); // disable UDRE interrupt husart->UCSRxB &= ~(1 << 5); // disable UDRE interrupt
} }
USART() = default; USART(hal::usart::USART::USART_TypeDef *husart) : husart(husart) {};
void Init(USART_TypeDef *conf) {
husart = conf;
}
private: private:
// IO base address // IO base address
@ -103,4 +104,4 @@ extern USART usart1;
} // namespace usart } // namespace usart
} // namespace hal } // namespace hal
#define USART1 ((hal::USART::USART_TypeDef *)&UCSR1A) #define USART1 ((hal::usart::USART::USART_TypeDef *)&UCSR1A)

View File

@ -278,6 +278,7 @@ void loop() {
int main() { int main() {
setup(); setup();
sei(); ///enable interrupts
for (;;) { for (;;) {
loop(); loop();
} }