/***************************************************************************** ** Function name: UARTInit ** ** Descriptions: Initialize UART0 port, setup pin select, ** clock, parity, stop bits, FIFO, etc. ** ** parameters: UART baudrate ** Returned value: None ** *****************************************************************************/ void UARTInit(uint32_t baudrate) { uint32_t Fdiv,Dval,Mval; uint32_t regVal; //UARTTxEmpty = 1; UARTCount = 0; NVIC_DisableIRQ(UART_IRQn); LPC_IOCON->PIO1_6 &= ~0x07; /* UART I/O config */ LPC_IOCON->PIO1_6 |= 0x01; /* UART RXD */ LPC_IOCON->PIO1_7 &= ~0x07; LPC_IOCON->PIO1_7 |= 0x01; /* UART TXD */ /* Enable UART clock */ LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); LPC_SYSCON->UARTCLKDIV = 0x1; /* divided by 1 */ LPC_UART->LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */ regVal = LPC_SYSCON->UARTCLKDIV; //Fdiv = ((SystemAHBFrequency/regVal)/16)/baudrate ; /*baud rate */ BaudRateCalculator(SystemCoreClock/regVal,baudrate,&Fdiv,&Dval,&Mval); LPC_UART->DLM = Fdiv >> 8; LPC_UART->DLL = Fdiv & 0xff; LPC_UART->FDR = Mval << 4 | Dval; LPC_UART->LCR = 0x03; /* DLAB = 0 */ LPC_UART->FCR = 0x07; /* Enable and reset TX and RX FIFO. */ /* Read to clear the line status. */ regVal = LPC_UART->LSR; /* Ensure a clean start, no data in either TX or RX FIFO. */ while (( LPC_UART->LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) ); while ( LPC_UART->LSR & LSR_RDR ) { regVal = LPC_UART->RBR; /* Dump data from RX FIFO */ } /* Enable the UART Interrupt */ NVIC_EnableIRQ(UART_IRQn); #if TX_INTERRUPT LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS; /* Enable UART interrupt */ #else LPC_UART->IER = IER_RBR | IER_RLS; /* Enable UART interrupt */ #endif return; }
/*---------------------------------------------------------------------------------------------------------*/ int32_t DrvUART_Open(E_UART_PORT u32Port, STR_UART_T *sParam) { UART_T * tUART; /*-----------------------------------------------------------------------------------------------------*/ /* Check UART port */ /*-----------------------------------------------------------------------------------------------------*/ if ((u32Port != UART_PORT0) && (u32Port != UART_PORT1) && (u32Port != UART_PORT2)) { return E_DRVUART_ERR_PORT_INVALID; } /*-----------------------------------------------------------------------------------------------------*/ /* Check the supplied parity */ /*-----------------------------------------------------------------------------------------------------*/ if ((sParam->u8cParity != DRVUART_PARITY_NONE) && (sParam->u8cParity != DRVUART_PARITY_EVEN) && (sParam->u8cParity != DRVUART_PARITY_ODD) && (sParam->u8cParity != DRVUART_PARITY_MARK) && (sParam->u8cParity != DRVUART_PARITY_SPACE)) { return E_DRVUART_ERR_PARITY_INVALID; } /*-----------------------------------------------------------------------------------------------------*/ /* Check the supplied number of data bits */ /*-----------------------------------------------------------------------------------------------------*/ else if ((sParam->u8cDataBits != DRVUART_DATABITS_5) && (sParam->u8cDataBits != DRVUART_DATABITS_6) && (sParam->u8cDataBits != DRVUART_DATABITS_7) && (sParam->u8cDataBits != DRVUART_DATABITS_8)) { return E_DRVUART_ERR_DATA_BITS_INVALID; } /*-----------------------------------------------------------------------------------------------------*/ /* Check the supplied number of stop bits */ /*-----------------------------------------------------------------------------------------------------*/ else if ((sParam->u8cStopBits != DRVUART_STOPBITS_1) && (sParam->u8cStopBits != DRVUART_STOPBITS_2) && (sParam->u8cStopBits != DRVUART_STOPBITS_1_5) ) { return E_DRVUART_ERR_STOP_BITS_INVALID; } /*-----------------------------------------------------------------------------------------------------*/ /* Check the supplied nember of trigger level bytes */ /*-----------------------------------------------------------------------------------------------------*/ else if ((sParam->u8cRxTriggerLevel != DRVUART_FIFO_1BYTES) && (sParam->u8cRxTriggerLevel != DRVUART_FIFO_4BYTES) && (sParam->u8cRxTriggerLevel != DRVUART_FIFO_8BYTES) && (sParam->u8cRxTriggerLevel != DRVUART_FIFO_14BYTES)&& (sParam->u8cRxTriggerLevel != DRVUART_FIFO_30BYTES)&& (sParam->u8cRxTriggerLevel != DRVUART_FIFO_46BYTES)&& (sParam->u8cRxTriggerLevel != DRVUART_FIFO_62BYTES)) { return E_DRVUART_ERR_TRIGGERLEVEL_INVALID; } if(u32Port == UART_PORT0) { /* Reset IP */ SYS->IPRSTC2.UART0_RST = 1; SYS->IPRSTC2.UART0_RST = 0; /* Enable UART clock */ SYSCLK->APBCLK.UART0_EN = 1; } else if(u32Port == UART_PORT1) { /* Reset IP */ SYS->IPRSTC2.UART1_RST = 1; SYS->IPRSTC2.UART1_RST = 0; /* Enable UART clock */ SYSCLK->APBCLK.UART1_EN = 1; } else { /* Reset IP */ SYS->IPRSTC2.UART2_RST = 1; SYS->IPRSTC2.UART2_RST = 0; /* Enable UART clock */ SYSCLK->APBCLK.UART2_EN = 1; } tUART = (UART_T *)((uint32_t)UART0 + u32Port); /* Tx FIFO Reset & Rx FIFO Reset & FIFO Mode Enable */ tUART->FCR.TFR =1; tUART->FCR.RFR =1; /* Set Rx Trigger Level */ tUART->FCR.RFITL = sParam->u8cRxTriggerLevel; /* Set Parity & Data bits & Stop bits */ tUART->LCR.SPE =((sParam->u8cParity)&0x4)?1:0; tUART->LCR.EPE =((sParam->u8cParity)&0x2)?1:0; tUART->LCR.PBE =((sParam->u8cParity)&0x1)?1:0; tUART->LCR.WLS =sParam->u8cDataBits; tUART->LCR.NSB =sParam->u8cStopBits; /* Set Time-Out */ tUART->TOR.TOIC =sParam->u8TimeOut; /* Set BaudRate */ BaudRateCalculator(GetUartClk(), sParam->u32BaudRate, &tUART->BAUD); return E_SUCCESS; }