/** * \brief Application entry point for pdc_uart example. * * \return Unused (ANSI-C compatibility). */ int main(void) { /* Initialize the SAM system */ sysclk_init(); board_init(); /* Initialize the UART console */ configure_console(); /* Output example information */ puts(STRING_HEADER); /* Get pointer to UART PDC register base */ g_p_uart_pdc = uart_get_pdc_base(CONSOLE_UART); /* Initialize PDC data packet for transfer */ g_pdc_uart_packet.ul_addr = (uint32_t) g_uc_pdc_buffer; g_pdc_uart_packet.ul_size = BUFFER_SIZE; /* Configure PDC for data receive */ pdc_rx_init(g_p_uart_pdc, &g_pdc_uart_packet, NULL); /* Enable PDC transfers */ pdc_enable_transfer(g_p_uart_pdc, PERIPH_PTCR_RXTEN | PERIPH_PTCR_TXTEN); /* Enable UART IRQ */ uart_enable_interrupt(CONSOLE_UART, UART_IER_RXBUFF); /* Enable UART interrupt */ NVIC_EnableIRQ(CONSOLE_UART_IRQn); while (1) { } }
/** * \brief This function opens an UART * * \note Opening of the specified UART implies initializing local variables and * opening required hardware with the following configuration: * - bauds as specified * - 8 bits, no parity, 1 stop bit * - enable interrupts * * \param chn Communication channel [0, 1] * \param bauds Communication speed in bauds * * \retval true on success. * \retval false on failure. */ int8_t buart_if_open(uint8_t chn, uint32_t bauds) { #if defined(CONF_BOARD_UART0) || defined(CONF_BOARD_UART1) sam_uart_opt_t uart_console_settings; /* MCK for UART */ uart_console_settings.ul_mck = sysclk_get_cpu_hz(); /* Expected baud rate. */ uart_console_settings.ul_baudrate = bauds; /* Initialize value for UART mode register */ uart_console_settings.ul_mode = UART_MR_PAR_NO; #else UNUSED(bauds); #endif /* check uart and it is close */ if (chn >= 2) { return false; } if (buart_chn_open[chn]) { return false; } switch (chn) { #ifdef CONF_BOARD_UART0 case 0: { /* Configure PMC. */ pmc_enable_periph_clk(ID_UART0); /* Configure UART. */ uart_init(UART0, &uart_console_settings); /* Assign buffers to pointers */ buart_comm_data_0.puc_tq_buf = ptr_tx_uart_buf0; buart_comm_data_0.puc_rq_buf = ptr_rx_uart_buf0; buart_comm_data_0.us_rq_count = 0; buart_comm_data_0.us_rq_idx = 0; buart_comm_data_0.us_wq_idx = 0; /* Get board UART0 PDC base address and enable receiver and * transmitter. */ g_p_uart_pdc0 = uart_get_pdc_base(UART0); pdc_enable_transfer(g_p_uart_pdc0, PERIPH_PTCR_RXTEN | PERIPH_PTCR_TXTEN); /* Start receiving data and start timer. */ g_st_uart_rx_packet0.ul_addr = (uint32_t)gs_puc_uart_buf0; g_st_uart_rx_packet0.ul_size = UART_BUFFER_SIZE; pdc_rx_init(g_p_uart_pdc0, &g_st_uart_rx_packet0, NULL); /* Stop transmitting data */ g_st_uart_tx_packet0.ul_addr = (uint32_t)buart_comm_data_0.puc_tq_buf; g_st_uart_tx_packet0.ul_size = 0; pdc_tx_init(g_p_uart_pdc0, &g_st_uart_tx_packet0, NULL); gs_ul_size_uart_buf0 = UART_BUFFER_SIZE; /* Transfer to PDC communication mode, disable RXRDY interrupt * and enable RXBUFF interrupt. */ uart_disable_interrupt(UART0, UART_IDR_RXRDY); uart_enable_interrupt(UART0, UART_IER_RXBUFF); /* Enable the receiver and transmitter. */ uart_enable_tx(UART0); uart_enable_rx(UART0); /* Configure and enable interrupt of USART. */ NVIC_SetPriority((IRQn_Type)UART0_IRQn, UART0_PRIO); NVIC_EnableIRQ(UART0_IRQn); buart_chn_open[chn] = true; num_bytes_rx_uart0 = 0; /* Configure TC uart */ _configure_TC_uart(); tc_start(TC_UART, TC_UART_CHN); return true; } break; #endif #ifdef CONF_BOARD_UART1 case 1: { /* Configure PMC. */ pmc_enable_periph_clk(ID_UART1); /* Configure UART. */ uart_init(UART1, &uart_console_settings); /* Assign buffers to pointers */ buart_comm_data_1.puc_tq_buf = ptr_tx_uart_buf1; buart_comm_data_1.puc_rq_buf = ptr_rx_uart_buf1; buart_comm_data_1.us_rq_count = 0; buart_comm_data_1.us_rq_idx = 0; buart_comm_data_1.us_wq_idx = 0; /* Get board UART1 PDC base address and enable receiver and * transmitter. */ g_p_uart_pdc1 = uart_get_pdc_base(UART1); pdc_enable_transfer(g_p_uart_pdc1, PERIPH_PTCR_RXTEN | PERIPH_PTCR_TXTEN); /* Start receiving data and start timer. */ g_st_uart_rx_packet1.ul_addr = (uint32_t)gs_puc_uart_buf1; g_st_uart_rx_packet1.ul_size = UART_BUFFER_SIZE; pdc_rx_init(g_p_uart_pdc1, &g_st_uart_rx_packet1, NULL); /* Stop transmitting data */ g_st_uart_tx_packet1.ul_addr = (uint32_t)buart_comm_data_1.puc_tq_buf; g_st_uart_tx_packet1.ul_size = 0; pdc_tx_init(g_p_uart_pdc1, &g_st_uart_tx_packet1, NULL); gs_ul_size_uart_buf1 = UART_BUFFER_SIZE; /* Transfer to PDC communication mode, disable RXRDY interrupt * and enable RXBUFF interrupt. */ uart_disable_interrupt(UART1, UART_IDR_RXRDY); uart_enable_interrupt(UART1, UART_IER_RXBUFF); /* Enable the receiver and transmitter. */ uart_enable_tx(UART1); uart_enable_rx(UART1); /* Configure and enable interrupt of USART. */ NVIC_SetPriority((IRQn_Type)UART1_IRQn, UART1_PRIO); NVIC_EnableIRQ(UART1_IRQn); buart_chn_open[chn] = true; num_bytes_rx_uart1 = 0; /* Configure TC uart */ _configure_TC_uart(); tc_start(TC_UART, TC_UART_CHN); return true; } break; #endif default: return false; } }