UART:universal asynchronous receiver and transmitter通用异步收发器,有两个:UART0(URXD0, PA9A; UTXD0, PA10A),UART1(URXD1, PB2A; UTXD1, PB3A)
USART:universal synchronous asynchronous receiver and transmitter通用同步异步收发器,有一个:USART0(RXD0, PA5A; TXD0, PA6A).
由于引脚资源有限,这些串口都作为了异步串口。本来我的计划是用UART0和UART1,这样两个驱动一样,只需要改下引脚就行了。结果硬件设计的人员把USART0给我接到了设备上,没办法,就用这两个串口吧。
这两个收发器的驱动基本一样,使用asf的话,只是寄存器值一个是UART,另一个是USART。
USART0:
- #define CONSOLE_USART USART0
- #define CONSOLE_USART_ID ID_USART0
- #define CONFSOLE_USART_IRQn USART0_IRQn
- /**
- * @brief Initializes the USART0.
- * @param None
- * @retval None
- */
- void USART0_Init(uint32_t Baudrate)
- {
- static uint32_t ul_sysclk;
- const sam_usart_opt_t usart_console_settings = {
- Baudrate,
- US_MR_CHRL_8_BIT,
- US_MR_PAR_NO,
- US_MR_NBSTOP_1_BIT,
- US_MR_CHMODE_NORMAL,
- /* This field is only used in IrDA mode. */
- 0
- };
- ul_sysclk = sysclk_get_cpu_hz();
- /* Enable peripheral clock. */
- sysclk_enable_peripheral_clock(CONSOLE_USART_ID);
- /* Configure USART. */
- usart_init_hw_handshaking(CONSOLE_USART, &usart_console_settings, ul_sysclk);
- /* Disable all the interrupts. */
- usart_enable_interrupt(CONSOLE_USART, US_IER_RXRDY);
- /* Enable Tx & Rx function. */
- usart_enable_tx(CONSOLE_USART);
- usart_enable_rx(CONSOLE_USART);
- NVIC_DisableIRQ(CONFSOLE_USART_IRQn);
- NVIC_ClearPendingIRQ(CONFSOLE_USART_IRQn);
- NVIC_SetPriority(CONFSOLE_USART_IRQn, 1);
- NVIC_EnableIRQ(CONFSOLE_USART_IRQn);
- }
UART1:
- #define CONSOLE_UART UART1
- #define CONSOLE_UART_ID ID_UART1
- #define CONFSOLE_UART_IRQn UART1_IRQn
- /**
- * @brief Initializes the USART1.
- * @param None
- * @retval None
- */
- void UART1_Init(uint32_t Baudrate)
- {
- /* USARTx configured as follow:
- – BaudRate = Baudrate
- – Word Length = 8 Bits
- – One Stop Bit
- – No parity
- – Hardware flow control disabled (RTS and CTS signals)
- – Receive and transmit enabled
- */
- const usart_serial_options_t uart_serial_options = {
- .baudrate = Baudrate,
- .charlength = UART_SERIAL_CHAR_LENGTH,
- .paritytype = CONF_UART_PARITY,
- .stopbits = UART_SERIAL_STOP_BIT
- };
- /* Configure console UART. */
- sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
- stdio_serial_init(CONSOLE_UART, &uart_serial_options);
- uart_enable_interrupt(CONSOLE_UART,US_IER_RXRDY);
- uart_enable(CONSOLE_UART);
- NVIC_DisableIRQ(CONFSOLE_UART_IRQn);
- NVIC_ClearPendingIRQ(CONFSOLE_UART_IRQn);
- NVIC_SetPriority(CONFSOLE_UART_IRQn, 0);
- NVIC_EnableIRQ(CONFSOLE_UART_IRQn);
- }
中断函数:
- /************************************************************************/
- /* @brief Interrupt handler for UART1.
- /************************************************************************/
- void UART1_Handler(void)
- {
- uint32_t ul_status;
- uint32_t gs_ul_read_buffer;
- UART_DATA *ptrComData = NULL;
- ptrComData = &Uart1_Data;
- // read UART status
- ul_status = uart_get_status(CONF_UART);
- if(ul_status & UART_SR_RXRDY)
- {
- usart_getchar(CONF_UART, (uint32_t *)&gs_ul_read_buffer);
- }
- }
- /************************************************************************/
- /* @brief Interrupt handler for USART0.
- /************************************************************************/
- void USART0_Handler(void)
- {
- uint32_t ul_status;
- uint32_t gs_ul_read_buffer;
- UART_DATA *ptrComData = NULL;
- ptrComData = &Uart1_Data;
- // read USART status
- ul_status = usart_get_status(USART0);
- if(ul_status & US_CSR_RXRDY)
- {
- usart_getchar(USART0, (uint32_t *)&gs_ul_read_buffer);
- }
- }
注意中断函数的名字不能错了,asf中都有对应的地址的。