void USART_L_init(void) { usart_set_mode(&Wireless_L_USART,USART_CMODE_ASYNCHRONOUS_gc); usart_format_set(&Wireless_L_USART,USART_CHSIZE_8BIT_gc,USART_PMODE_DISABLED_gc,false); usart_set_rx_interrupt_level(&Wireless_L_USART,USART_INT_LVL_MED); //usart_set_tx_interrupt_level(&Wireless_L_USART,USART_INT_LVL_MED); usart_set_baudrate(&Wireless_L_USART,USART_BUADRATE,F_CPU); usart_tx_enable(&Wireless_L_USART); usart_rx_enable(&Wireless_L_USART); }
/** * \brief Initialize USART in RS232 mode. * * This function initializes the USART module in RS232 mode using the * usart_rs232_options_t configuration structure and CPU frequency. * * \param usart The USART module. * \param opt The RS232 configuration option. */ void usart_init_rs232(USART_t *usart, const usart_rs232_options_t *opt) { usart_enable_module_clock(usart); usart_set_mode(usart, USART_CMODE_ASYNCHRONOUS_gc); usart_format_set(usart, opt->charlength, opt->paritytype, opt->stopbits); usart_set_baudrate(usart, opt->baudrate, sysclk_get_per_hz()); usart_tx_enable(usart); usart_rx_enable(usart); }
/** * \brief Initialize USART in RS232 mode. * * This function initializes the USART module in RS232 mode using the * usart_rs232_options_t configuration structure and CPU frequency. * * \param usart The USART module. * \param opt The RS232 configuration option. * * \retval true if the initialization was successfull * \retval false if the initialization failed (error in baud rate calculation) */ bool usart_init_rs232(USART_t *usart, const usart_rs232_options_t *opt) { bool result; sysclk_enable_peripheral_clock(usart); usart_set_mode(usart, USART_CMODE_ASYNCHRONOUS_gc); usart_format_set(usart, opt->charlength, opt->paritytype, opt->stopbits); result = usart_set_baudrate(usart, opt->baudrate, sysclk_get_per_hz()); usart_tx_enable(usart); usart_rx_enable(usart); return result; }
/** * \brief Test setting different parameters of the USART module * * This function calls the different set functions, and verifies that the * correct values are being set. * * \param test Current test case. */ static void run_set_functions_test(const struct test_case *test) { bool success; /* Set USART mode and verify that it has been correctly set. */ usart_set_mode(&CONF_UNIT_USART, USART_CMODE_MSPI_gc); success = (CONF_UNIT_USART.UCSRnC & USART_UMSEL01_gm) == USART_CMODE_MSPI_gc; test_assert_true(test, success, "Trying to set USART mode to master SPI failed."); /* Set USART sync mode and verify that it has been correctly set. */ usart_set_mode(&CONF_UNIT_USART, USART_CMODE_SYNCHRONOUS_gc); success = (CONF_UNIT_USART.UCSRnC & USART_UMSEL01_gm) == USART_CMODE_SYNCHRONOUS_gc; test_assert_true(test, success, "Trying to set USART mode to sync mode failed."); /* Test enabling and disabling USART double baud*/ usart_double_baud_enable(&CONF_UNIT_USART); success = (CONF_UNIT_USART.UCSRnA & USART_U2X_bm); test_assert_true(test, success, "Trying to enable USART double baud failed."); usart_double_baud_disable(&CONF_UNIT_USART); success = !(CONF_UNIT_USART.UCSRnA & USART_U2X_bm); test_assert_true(test, success, "Trying to disable USART double baud failed."); /* Test enabling and disabling USART RX */ usart_rx_enable(&CONF_UNIT_USART); success = (CONF_UNIT_USART.UCSRnB & USART_RXEN_bm); test_assert_true(test, success, "Trying to enable USART RX failed."); usart_rx_disable(&CONF_UNIT_USART); success = !(CONF_UNIT_USART.UCSRnB & USART_RXEN_bm); test_assert_true(test, success, "Trying to disable USART RX failed."); /* Test enabling and disabling USART TX */ usart_tx_enable(&CONF_UNIT_USART); success = (CONF_UNIT_USART.UCSRnB & USART_TXEN_bm); test_assert_true(test, success, "Trying to enable USART TX failed."); usart_tx_disable(&CONF_UNIT_USART); success = !(CONF_UNIT_USART.UCSRnB & USART_TXEN_bm); test_assert_true(test, success, "Trying to disable USART TX failed."); /* Test enabling and disabling USART TX complete interrupt*/ usart_tx_complete_interrupt_enable(&CONF_UNIT_USART); success = (CONF_UNIT_USART.UCSRnB & USART_TXC_bm); test_assert_true(test, success, "Trying to enable USART TX Complete interrupt failed."); usart_tx_complete_interrupt_disable(&CONF_UNIT_USART); success = !(CONF_UNIT_USART.UCSRnB & USART_TXC_bm); test_assert_true(test, success, "Trying to disable USART TX Complete interrupt failed."); /* Test enabling and disabling USART RX complete interrupt*/ usart_rx_complete_interrupt_enable(&CONF_UNIT_USART); success = (CONF_UNIT_USART.UCSRnB & USART_RXC_bm); test_assert_true(test, success, "Trying to enable USART RX Complete interrupt failed."); usart_rx_complete_interrupt_disable(&CONF_UNIT_USART); success = !(CONF_UNIT_USART.UCSRnB & USART_RXC_bm); test_assert_true(test, success, "Trying to disable USART RX Complete interrupt failed."); /* Test enabling and disabling USART data register empty interrupt*/ usart_data_empty_interrupt_enable(&CONF_UNIT_USART); success = (CONF_UNIT_USART.UCSRnB & USART_DRIE_bm); test_assert_true(test, success, "Trying to enable USART data register empty interrupt failed."); usart_data_empty_interrupt_disable(&CONF_UNIT_USART); success = !(CONF_UNIT_USART.UCSRnB & USART_DRIE_bm); test_assert_true(test, success, "Trying to disable USART data register empty interrupt failed."); /* Try to set format. */ usart_format_set(&CONF_UNIT_USART, USART_CHSIZE_8BIT_gc, USART_PMODE_EVEN_gc, true); success = !(CONF_UNIT_USART.UCSRnA & USART_FE_bm); test_assert_true(test, success, "Trying to set the Frame Format failed."); }