/** * @brief Set UART line configuration * * @param[in] uart The pointer of the specified UART module. * @param[in] u32baudrate The register value of baudrate of UART module. * If u32baudrate = 0, UART baudrate will not change. * @param[in] u32data_width The data length of UART module. * - UART_WORD_LEN_5 * - UART_WORD_LEN_6 * - UART_WORD_LEN_7 * - UART_WORD_LEN_8 * @param[in] u32parity The parity setting (none/odd/even/mark/space) of UART module. * - UART_PARITY_NONE * - UART_PARITY_ODD * - UART_PARITY_EVEN * - UART_PARITY_MARK * - UART_PARITY_SPACE * @param[in] u32stop_bits The stop bit length (1/1.5/2 bit) of UART module. * - UART_STOP_BIT_1 * - UART_STOP_BIT_1_5 * - UART_STOP_BIT_2 * * @return None * * @details This function use to config UART line setting. */ void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits) { uint8_t u8UartClkSrcSel, u8UartClkDivNum; uint32_t u32ClkTbl[4] = {__HXT, 0, 0, __HIRC}; uint32_t u32Baud_Div = 0; /* Get UART clock source selection */ u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos; /* Get UART clock divider number */ u8UartClkDivNum = (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos; /* Get PLL clock frequency if UART clock source selection is PLL */ if(u8UartClkSrcSel == 1) u32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq(); /* Set UART baud rate */ if(u32baudrate != 0) { u32Baud_Div = UART_BAUD_MODE2_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate); if(u32Baud_Div > 0xFFFF) uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate)); else uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); } /* Set UART line configuration */ uart->LCR = u32data_width | u32parity | u32stop_bits; }
/*---------------------------------------------------------------------------------------------------------*/ void UART1_Init(void) { /* Set PC.10 and PC.11 multi-function pins for UART1_RXD, UART1_TXD */ SYS->PC_H_MFP |= (SYS_PC_H_MFP_PC11_MFP_UART1_TX | SYS_PC_H_MFP_PC10_MFP_UART1_RX); /* Enable UART1 IP clock */ CLK->APBCLK |= CLK_APBCLK_UART1_EN_Msk; /* Set UART1 IP clock source */ CLK->CLKSEL1 |= CLK_CLKSEL1_UART_S_HXT; /* Configure UART1 and set UART1 Baudrate */ UART1->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(FREQ_12MHZ, 115200); UART1->TLCTL = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1; }
/** * @brief Select and configure IrDA function * * @param[in] uart The pointer of the specified UART module. * @param[in] u32Buadrate The baudrate of UART module. * @param[in] u32Direction The direction of UART module in IrDA mode: * - \ref UART_IRDA_TXEN * - \ref UART_IRDA_RXEN * * @return None * * @details The function is used to configure IrDA relative settings. It consists of TX or RX mode and baudrate. */ void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction) { uint8_t u8UartClkSrcSel, u8UartClkDivNum; uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC}; uint32_t u32Baud_Div; /* Select IrDA function mode */ uart->FUNCSEL = UART_FUNCSEL_IrDA; /* Get UART clock source selection */ u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos; /* Get UART clock divider number */ u8UartClkDivNum = (CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos; /* Get PLL clock frequency if UART clock source selection is PLL */ if(u8UartClkSrcSel == 1) u32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq(); /* Set UART IrDA baud rate in mode 0 */ if(u32Buadrate != 0) { u32Baud_Div = UART_BAUD_MODE0_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32Buadrate); if(u32Baud_Div < 0xFFFF) uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div); } /* Configure IrDA relative settings */ if(u32Direction == UART_IRDA_RXEN) { uart->IRDA |= UART_IRDA_RXINV_Msk; //Rx signal is inverse uart->IRDA &= ~UART_IRDA_TXEN_Msk; } else { uart->IRDA &= ~UART_IRDA_TXINV_Msk; //Tx signal is not inverse uart->IRDA |= UART_IRDA_TXEN_Msk; } }
/** * @brief Open and set UART function * * @param[in] uart The pointer of the specified UART module. * @param[in] u32baudrate The baudrate of UART module. * * @return None * * @details This function use to enable UART function and set baud-rate. */ void UART_Open(UART_T* uart, uint32_t u32baudrate) { uint8_t u8UartClkSrcSel, u8UartClkDivNum; uint32_t u32ClkTbl[4] = {__HXT, 0, 0, __HIRC}; uint32_t u32Baud_Div = 0; /* Get UART clock source selection */ u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos; /* Get UART clock divider number */ u8UartClkDivNum = (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos; /* Select UART function */ uart->FUN_SEL = UART_FUNC_SEL_UART; /* Set UART line configuration */ uart->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1; /* Set UART Rx and RTS trigger level */ uart->FCR &= ~(UART_FCR_RFITL_Msk | UART_FCR_RTS_TRI_LEV_Msk); /* Get PLL clock frequency if UART clock source selection is PLL */ if(u8UartClkSrcSel == 1) u32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq(); /* Set UART baud rate */ if(u32baudrate != 0) { u32Baud_Div = UART_BAUD_MODE2_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate); if(u32Baud_Div > 0xFFFF) uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate)); else uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); } }
void VCOM_LineCoding(uint8_t port) { uint32_t u32Reg, u32Baud_Div; if (port == 0) { NVIC_DisableIRQ(UART0_IRQn); // Reset software fifo comRbytes = 0; comRhead = 0; comRtail = 0; comTbytes = 0; comThead = 0; comTtail = 0; // Reset hardware fifo UART0->CTL = 0x3; // Set baudrate u32Baud_Div = UART_BAUD_MODE0_DIVIDER(__HXT, gLineCoding.u32DTERate); if(u32Baud_Div > 0xFFFF) UART0->BAUD = (UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(__HXT, gLineCoding.u32DTERate)); else UART0->BAUD = (UART_BAUD_MODE0 | u32Baud_Div); // Set parity if(gLineCoding.u8ParityType == 0) u32Reg = 0; // none parity else if(gLineCoding.u8ParityType == 1) u32Reg = 0x08; // odd parity else if(gLineCoding.u8ParityType == 2) u32Reg = 0x18; // even parity else u32Reg = 0; // bit width switch(gLineCoding.u8DataBits) { case 5: u32Reg |= 0; break; case 6: u32Reg |= 1; break; case 7: u32Reg |= 2; break; case 8: u32Reg |= 3; break; default: break; } // stop bit if(gLineCoding.u8CharFormat > 0) u32Reg |= 0x4; // 2 or 1.5 bits UART0->TLCTL = u32Reg; // Re-enable UART interrupt NVIC_EnableIRQ(UART0_IRQn); } }
/*---------------------------------------------------------------------------------------------------------*/ void IrDA_FunctionTxTest() { uint8_t u8OutChar; printf("\n"); printf("+-----------------------------------------------------------+\n"); printf("| Pin Configure |\n"); printf("+-----------------------------------------------------------+\n"); printf("| ______ _______ |\n"); printf("| | | | | |\n"); printf("| |Master|--UART1_TXD(PB.3) <==> UART1_RXD(PB.2)--|Slave | |\n"); printf("| | | | | |\n"); printf("| |______| |_______| |\n"); printf("| |\n"); printf("+-----------------------------------------------------------+\n"); printf("\n"); printf("+-----------------------------------------------------------+\n"); printf("| IrDA Function Test |\n"); printf("+-----------------------------------------------------------+\n"); printf("| Description : |\n"); printf("| The sample code needs two boards. One is Master and |\n"); printf("| the other is slave. Master will send data based on |\n"); printf("| terminal input and Slave will printf received data on |\n"); printf("| terminal screen. |\n"); printf("+-----------------------------------------------------------+\n"); /* UART0 is set to debug port and connect with PC firstly. The IrDA sample code needs two module board to execute. Set the master board is IrDA TX Mode and the other is IrDA Rx mode. Inputting char on terminal will be sent to the UART0 of master. After the master receiving, the inputting char will send to UART0 again. At the same time, it also sends to UART1 TX pin by IrDA mode. Slave will print received char after UART1 send out. Note that IrDA mode is ONLY used when baud rate equation is selected mode 0. */ printf("\n"); printf("+-----------------------------------------------------------+\n"); printf("| IrDA Function Mode Test (Master) |\n"); printf("+-----------------------------------------------------------+\n"); printf("| 1). Input char by UART0 terminal. |\n"); printf("| 2). UART1 will send a char according to step 1. |\n"); printf("| 3). Return step 1. (Press '0' to exit) |\n"); printf("+-----------------------------------------------------------+\n"); printf("\nIrDA Sample Code Start. \n"); /* In IrDA Mode, Baud Rate configuration must be used MODE0 */ UART1->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(__HXT, 57600); /* IrDA Function Enable */ UART1->FUNCSEL = UART_FUNCSEL_IrDA; /* Set IrDA Tx mode */ UART1->IRDA = UART_IRDA_TXEN_Msk; UART1->IRDA &= ~UART_IRDA_TXINV_Msk; //Tx signal is not inverse /* Wait Terminal input to send data to UART1 TX pin */ do { u8OutChar = GetChar(); printf(" Input: %c , Send %c out\n", u8OutChar, u8OutChar); UART1->DAT = u8OutChar; } while(u8OutChar != '0'); printf("\nIrDA Sample Code End.\n"); }