int app_com_at_baudrate_change_handler(ke_msg_id_t const msgid, void const *param, ke_task_id_t const dest_id, ke_task_id_t const src_id) { uint32_t reg; // QPRINTF("app_com_at_baudrate_change_handler:%d\r\n",baudrate); reg = (uart_divider[baudrate].integer_h << (UART_POS_DIVIDER_INT + 8)) | (uart_divider[baudrate].integer_l << UART_POS_DIVIDER_INT) | uart_divider[baudrate].fractional; uart_uart_SetBaudDivider(QN_COM_UART, reg); return (KE_MSG_CONSUMED); }
/** **************************************************************************************** * @brief Configurate UART baud rate. * @param[in] UART QN_UART0 or QN_UART1 * @param[in] uartclk USARTx_CLK(div) * @param[in] oversample UART_OVS16 or UART_OVS8 * @param[in] buadrate 115200 * @description * an example of how to configurate baud rate: * If the required baud rate is 230400, UARTCLK = 8MHz, and overs = 16, then: * Baud Rate Divisor = (8¡Á1000000)/(16¡Á230400) = 2.170 * This means BRDI = 2 and BRDF = 0.170. * Therefore, fractional part, m = integer((0.170¡Á64)+0.5) = 11 ***************************************************************************************** */ static void uart_baudrate_set(QN_UART_TypeDef *UART, uint32_t uartclk, enum UART_OVERSAMPLE_TYPE oversample, uint32_t baudrate) { uint32_t inter_div; uint32_t frac_div; uint32_t overs; uint32_t tmp; overs = (oversample == UART_OVS16) ? 16 : 8; tmp = (overs * baudrate); inter_div = uartclk / tmp; frac_div = uartclk - inter_div * tmp; frac_div = (frac_div*64 + tmp / 2 ) / tmp; // Set UART baudrate tmp = (inter_div << UART_POS_DIVIDER_INT) + frac_div; uart_uart_SetBaudDivider(UART, tmp); }
/** **************************************************************************************** * @brief Initialize the UART to default values. * @param[in] UART QN_UART0 or QN_UART1 * @param[in] uartclk USARTx_CLK(div) * @param[in] baudrate baud rate * @description * This function is used to initialize UART, it consists of baud-rate, parity, data-bits, stop-bits, * over sample rate and bit order. The function is also used to enable specified UART interrupt, and * enable NVIC UART IRQ. ***************************************************************************************** */ void uart_init(QN_UART_TypeDef *UART, uint32_t uartclk, enum UART_BAUDRATE baudrate) { // UART0 and UART1 are arranged in cross over configuration uint32_t reg; struct uart_env_tag *uart_env = &uart0_env; uart_clock_on(UART); // Set UART baudrate #if UART_BAUDRATE_TABLE_EN==TRUE reg = (uart_divider[baudrate].integer_h << (UART_POS_DIVIDER_INT + 8)) | (uart_divider[baudrate].integer_l << UART_POS_DIVIDER_INT) | uart_divider[baudrate].fractional; uart_uart_SetBaudDivider(UART, reg); #else uart_baudrate_set(UART, uartclk, UART_OVS16, baudrate); #endif #if CONFIG_ENABLE_DRIVER_UART0==TRUE if (UART == QN_UART0) { /* * Set UART config: * * - oversample rate is 16 * - HW flow control disable * - 1 stop bit * - parity type unused * - no parity * - bitorder = LSB */ reg = UART_MASK_UART_EN // uart enable | UART_MASK_UART_IE // uart int enable //| UART_MASK_CTS_EN //| UART_MASK_RTS_EN | UART_OVS16 | UART_MASK_LEVEL_INV | UART_MASK_BIT_ORDER; uart_env = &uart0_env; #if CONFIG_UART0_TX_ENABLE_INTERRUPT==TRUE && UART_TX_DMA_EN==FALSE // Enable the UART0 TX Interrupt NVIC_EnableIRQ(UART0_TX_IRQn); #endif #if CONFIG_UART0_RX_ENABLE_INTERRUPT==TRUE && UART_RX_DMA_EN==FALSE // Enable the UART0 RX Interrupt NVIC_EnableIRQ(UART0_RX_IRQn); #endif } #endif #if CONFIG_ENABLE_DRIVER_UART1==TRUE if (UART == QN_UART1) { /* * Set UART config: * * - oversample rate is 16 * - HW flow control disable * - 1 stop bit * - parity type unused * - no parity * - bitorder = LSB */ reg = UART_MASK_UART_EN // uart enable | UART_MASK_UART_IE // uart int enable | UART_OVS16 | UART_MASK_LEVEL_INV | UART_MASK_BIT_ORDER; uart_env = &uart1_env; #if CONFIG_UART1_TX_ENABLE_INTERRUPT==TRUE && UART_TX_DMA_EN==FALSE // Enable the UART1 TX Interrupt NVIC_EnableIRQ(UART1_TX_IRQn); #endif #if CONFIG_UART1_RX_ENABLE_INTERRUPT==TRUE && UART_RX_DMA_EN==FALSE // Enable the UART1 RX Interrupt NVIC_EnableIRQ(UART1_RX_IRQn); #endif } #endif // Set UART config uart_uart_SetCR(UART, reg); #if UART_DMA_EN==TRUE dma_init(); #endif //Configure UART environment uart_env->rx.size = 0; uart_env->tx.size = 0; uart_env->rx.bufptr = NULL; uart_env->tx.bufptr = NULL; #if UART_CALLBACK_EN==TRUE uart_env->rx.callback = NULL; uart_env->tx.callback = NULL; #endif }