/** * \internal * \brief Helper function to read a byte from an arbitrary interface * * This function is used to hide what interface is used by the component * driver, e.g. the component driver does not need to know if USART in SPI * mode is used or the native SPI module. * * \retval uint8_t Byte of data read from the display controller */ __always_inline static uint8_t ili9341_read_byte(void) { uint8_t data; #if defined(CONF_ILI9341_USART_SPI) # if XMEGA /* Workaround for clearing the RXCIF for XMEGA */ usart_rx_enable(CONF_ILI9341_USART_SPI); usart_put(CONF_ILI9341_USART_SPI, 0xFF); while (!usart_rx_is_complete(CONF_ILI9341_USART_SPI)) { /* Do nothing */ } data = usart_get(CONF_ILI9341_USART_SPI); /* Workaround for clearing the RXCIF for XMEGA */ usart_rx_disable(CONF_ILI9341_USART_SPI); # else usart_spi_read_single(CONF_ILI9341_USART_SPI, &data); # endif #elif defined(CONF_ILI9341_SPI) spi_write_single(CONF_ILI9341_SPI, 0xFF); ili9341_wait_for_send_done(); /* Wait for RX to complete */ while (!spi_is_rx_full(CONF_ILI9341_SPI)) { /* Do nothing */ } spi_read_single(CONF_ILI9341_SPI, &data); #endif return data; }
void ntx2b_uart_init(void) { static usart_rs232_options_t NTX2B_USART_SERIAL_OPTIONS = { .baudrate = NTX2B_USART_SERIAL_BAUDRATE, .charlength = NTX2B_USART_SERIAL_CHAR_LENGTH, .paritytype = NTX2B_USART_SERIAL_PARITY, .stopbits = NTX2B_USART_SERIAL_STOP_BIT }; sysclk_enable_module(SYSCLK_PORT_D, PR_USART0_bm); usart_init_rs232(NTX2B_USART_SERIAL, &NTX2B_USART_SERIAL_OPTIONS); usart_rx_disable(NTX2B_USART_SERIAL); // we don't use the receiver ioport_set_pin_mode(GPIO_NTX2B_EN, IOPORT_MODE_TOTEM | IOPORT_MODE_INVERT_PIN ); // set enable low }
/** * \brief Initialize USART in SPI master mode. * * This function initializes the USART module in SPI master mode using the * usart_spi_options_t configuration structure and CPU frequency. * * \param usart The USART module. * \param opt The RS232 configuration option. */ void usart_init_spi(USART_t *usart, const usart_spi_options_t *opt) { ioport_pin_t sck_pin; bool invert_sck; sysclk_enable_peripheral_clock(usart); usart_rx_disable(usart); /* configure Clock polarity using INVEN bit of the correct SCK I/O port **/ invert_sck = (opt->spimode == 2) || (opt->spimode == 3); UNUSED(invert_sck); #ifdef USARTC0 if ((uint16_t)usart == (uint16_t)&USARTC0) { sck_pin = IOPORT_CREATE_PIN(PORTC, 1); } #endif #ifdef USARTC1 if ((uint16_t)usart == (uint16_t)&USARTC1) { sck_pin = IOPORT_CREATE_PIN(PORTC, 5); } #endif #ifdef USARTD0 if ((uint16_t)usart == (uint16_t)&USARTD0) { sck_pin = IOPORT_CREATE_PIN(PORTD, 1); } #endif #ifdef USARTD1 if ((uint16_t)usart == (uint16_t)&USARTD1) { sck_pin = IOPORT_CREATE_PIN(PORTD, 5); } #endif #ifdef USARTE0 if ((uint16_t)usart == (uint16_t)&USARTE0) { sck_pin = IOPORT_CREATE_PIN(PORTE, 1); } #endif #ifdef USARTE1 if ((uint16_t)usart == (uint16_t)&USARTE1) { sck_pin = IOPORT_CREATE_PIN(PORTE, 5); } #endif #ifdef USARTF0 if ((uint16_t)usart == (uint16_t)&USARTF0) { sck_pin = IOPORT_CREATE_PIN(PORTF, 1); } #endif #ifdef USARTF1 if ((uint16_t)usart == (uint16_t)&USARTF1) { sck_pin = IOPORT_CREATE_PIN(PORTF, 5); } #endif /* Configure the USART output pin */ ioport_set_pin_dir(sck_pin, IOPORT_DIR_OUTPUT); ioport_set_pin_mode(sck_pin, IOPORT_MODE_TOTEM | (invert_sck? IOPORT_MODE_INVERT_PIN : 0)); ioport_set_pin_level(sck_pin, IOPORT_PIN_LEVEL_HIGH); usart_set_mode(usart, USART_CMODE_MSPI_gc); if (opt->spimode == 1 || opt->spimode == 3) { usart->CTRLC |= USART_UCPHA_bm; } else { usart->CTRLC &= ~USART_UCPHA_bm; } if (opt->data_order) { (usart)->CTRLC |= USART_DORD_bm; } else { (usart)->CTRLC &= ~USART_DORD_bm; } usart_spi_set_baudrate(usart, opt->baudrate, sysclk_get_per_hz()); usart_tx_enable(usart); usart_rx_enable(usart); }
/** * \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."); }