/** * \brief Set the baudrate using precalculated BAUDCTRL values from PROGMEM * * \note This function only works for cpu_hz 2Mhz or 32Mhz and baudrate values * 1200, 2400, 4800, 9600, 19200, 38400 and 57600. * * \param usart The USART module. * \param baud The baudrate. * \param cpu_hz The CPU frequency. * */ void usart_set_baudrate_precalculated(USART_t *usart, uint32_t baud, uint32_t cpu_hz) { uint8_t baud_offset; uint16_t baudctrl = 0; baud_offset = usart_get_baud_offset(baud); if (cpu_hz == 2000000UL) { baudctrl = PROGMEM_READ_WORD(baudctrl_2mhz + baud_offset); } else if (cpu_hz == 32000000UL) { baudctrl = PROGMEM_READ_WORD(baudctrl_32mhz + baud_offset); } else { /* Error, system clock speed or USART baud rate is not supported by the look-up table */ Assert(false); } if (baud_offset != USART_BAUD_UNDEFINED) { (usart)->BAUDCTRLB = (uint8_t)((uint16_t)baudctrl); (usart)->BAUDCTRLA = (uint8_t)((uint16_t)baudctrl >> 8); }
/** * \brief Set the baudrate using pre calculated BAUDCTRL values stored in program * memory * * \note This function only works for cpu_hz 1Mhz, 8Mhz or 16Mhz and baudrate values * 1200, 2400, 4800, 9600, 19200, 38400 and 57600. * * \param usart The USART module. * \param baud The baudrate. * \param cpu_hz The CPU frequency. * */ void usart_set_baudrate_precalculated(USART_t *usart, uint32_t baud, uint32_t cpu_hz) { uint8_t baud_offset; uint16_t baudctrl = 0; baud_offset = usart_get_baud_offset(baud); if (cpu_hz == 1000000UL) { baudctrl = PROGMEM_READ_WORD(baudctrl_1mhz + baud_offset); } else if (cpu_hz == 8000000UL) { baudctrl = PROGMEM_READ_WORD(baudctrl_8mhz + baud_offset); } else if (cpu_hz == 16000000UL) { baudctrl = PROGMEM_READ_WORD(baudctrl_16mhz + baud_offset); } else { /* Error, system clock speed or USART baud rate is not supported * by the look-up table */ Assert(false); } if (baud_offset != USART_BAUD_UNDEFINED) { usart->UBRR = baudctrl; } }