/** * @brief Handle interrupt from UART * @return Nothing */ void UART1_IRQHandler(void) { uint8_t ch; uint32_t IntStatus = Chip_UART_GetIntStatus(LPC_USART1); if (IntStatus & RXRDY_INT) { if(receiveCompleted == false) { Chip_UART_ReceiveByte(LPC_USART1, &RxBuf1[RxBufCnt1++]); if (RxBufCnt1 == BUFFER_SIZE) { Chip_UART_IntEnable(LPC_USART1, RXRDY_INT, DISABLE); receiveCompleted = true; RxBufCnt1 = 0; } } else { Chip_UART_ReceiveByte(LPC_USART1, &ch); } } if (IntStatus & TXRDY_INT) { if (sendCompleted == false) { Chip_UART_SendByte(LPC_USART1, RxBuf1[TxBufCnt1++]); if (TxBufCnt1 == BUFFER_SIZE) { Chip_UART_IntEnable(LPC_USART1, TXRDY_INT, DISABLE); sendCompleted = true; TxBufCnt1 = 0; } } else { return; } } }
void ciaaUARTInit(void) { /* UART0 (RS485/Profibus) */ Chip_UART_Init(LPC_USART0); Chip_UART_SetBaud(LPC_USART0, 115200); Chip_UART_TXEnable(LPC_USART0); Chip_SCU_PinMux(9, 5, MD_PDN, FUNC7); /* P9_5: UART0_TXD */ Chip_SCU_PinMux(9, 6, MD_PLN|MD_EZI|MD_ZI, FUNC7); /* P9_6: UART0_RXD */ Chip_UART_SetRS485Flags(LPC_USART0, UART_RS485CTRL_DCTRL_EN | UART_RS485CTRL_OINV_1); Chip_SCU_PinMux(6, 2, MD_PDN, FUNC2); /* P6_2: UART0_DIR */ /* Restart FIFOS: set Enable, Reset content, set trigger level */ Chip_UART_SetupFIFOS(LPC_USART0, UART_FCR_FIFO_EN | UART_FCR_TX_RS | UART_FCR_RX_RS | UART_FCR_TRG_LEV0); /* dummy read */ Chip_UART_ReadByte(LPC_USART0); /* enable rx interrupt */ Chip_UART_IntEnable(LPC_USART0, UART_IER_RBRINT); NVIC_EnableIRQ(USART0_IRQn); RingBuffer_Init(uarts[0].rrb, rxbuf[0], 1, UART_BUF_SIZE); RingBuffer_Init(uarts[0].trb, txbuf[0], 1, UART_BUF_SIZE); /* UART2 (USB-UART) */ Chip_UART_Init(LPC_USART2); Chip_UART_SetBaud(LPC_USART2, 115200); Chip_UART_TXEnable(LPC_USART2); Chip_SCU_PinMux(7, 1, MD_PDN, FUNC6); /* P7_1: UART2_TXD */ Chip_SCU_PinMux(7, 2, MD_PLN|MD_EZI|MD_ZI, FUNC6); /* P7_2: UART2_RXD */ Chip_UART_IntEnable(LPC_USART2, UART_IER_RBRINT); NVIC_EnableIRQ(USART2_IRQn); RingBuffer_Init(uarts[1].rrb, rxbuf[1], 1, UART_BUF_SIZE); RingBuffer_Init(uarts[1].trb, txbuf[1], 1, UART_BUF_SIZE); /* UART3 (RS232) */ Chip_UART_Init(LPC_USART3); Chip_UART_SetBaud(LPC_USART3, 115200); Chip_UART_TXEnable(LPC_USART3); Chip_SCU_PinMux(2, 3, MD_PDN, FUNC2); /* P2_3: UART3_TXD */ Chip_SCU_PinMux(2, 4, MD_PLN|MD_EZI|MD_ZI, FUNC2); /* P2_4: UART3_RXD */ Chip_UART_IntEnable(LPC_USART3, UART_IER_RBRINT); NVIC_EnableIRQ(USART3_IRQn); RingBuffer_Init(uarts[2].rrb, rxbuf[2], 1, UART_BUF_SIZE); RingBuffer_Init(uarts[2].trb, txbuf[2], 1, UART_BUF_SIZE); }
extern int32_t ciaaDriverUart_ioctl(ciaaDevices_deviceType const * const device, int32_t const request, void * param) { int32_t ret = -1; if((device == ciaaDriverUartConst.devices[0]) || (device == ciaaDriverUartConst.devices[1]) || (device == ciaaDriverUartConst.devices[2]) ) { switch(request) { case ciaaPOSIX_IOCTL_STARTTX: /* disable THRE irq (TX) */ Chip_UART_IntDisable((LPC_USART_T *)device->loLayer, UART_IER_THREINT); /* this one calls write */ ciaaDriverUart_txConfirmation(device); /* enable THRE irq (TX) */ Chip_UART_IntEnable((LPC_USART_T *)device->loLayer, UART_IER_THREINT); ret = 0; break; case ciaaPOSIX_IOCTL_SET_BAUDRATE: ret = Chip_UART_SetBaud((LPC_USART_T *)device->loLayer, (int32_t)param); break; case ciaaPOSIX_IOCTL_SET_FIFO_TRIGGER_LEVEL: Chip_UART_SetupFIFOS((LPC_USART_T *)device->loLayer, UART_FCR_FIFO_EN | UART_FCR_TX_RS | UART_FCR_RX_RS | (int32_t)param); break; case ciaaPOSIX_IOCTL_SET_ENABLE_TX_INTERRUPT: if((bool)(intptr_t)param == false) { /* disable THRE irq (TX) */ Chip_UART_IntDisable((LPC_USART_T *)device->loLayer, UART_IER_THREINT); } else { /* enable THRE irq (TX) */ Chip_UART_IntEnable((LPC_USART_T *)device->loLayer, UART_IER_THREINT); } break; case ciaaPOSIX_IOCTL_SET_ENABLE_RX_INTERRUPT: if((bool)(intptr_t)param == false) { /* disable RBR irq (RX) */ Chip_UART_IntDisable((LPC_USART_T *)device->loLayer, UART_IER_RBRINT); } else { /* enable RBR irq (RX) */ Chip_UART_IntEnable((LPC_USART_T *)device->loLayer, UART_IER_RBRINT); } break; } } return ret; }
/* Init seriové linky a pinů určených k přenosu dat * Nastavení UARTu: 115.2K8N1. * FIFO lvl3 - 14 znaků. * */ void setup_uarts(){ /* UART0 set up */ Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 2, (IOCON_FUNC1 | IOCON_MODE_INACT)); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 3, (IOCON_FUNC1 | IOCON_MODE_INACT)); /* Setup UART0 for 115.2K8N1 */ Chip_UART_Init(LPC_UART0); Chip_UART_SetBaud(LPC_UART0, 115200); Chip_UART_ConfigData(LPC_UART0, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT)); Chip_UART_SetupFIFOS(LPC_UART0, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2)); Chip_UART_TXEnable(LPC_UART0); Chip_UART_IntEnable(UART0, (UART_IER_RBRINT)); Chip_UART_SetupFIFOS(LPC_UART0, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_TRG_LEV3)); NVIC_EnableIRQ(UART0_IRQn); /* UART2 set up */ Chip_IOCON_PinMuxSet(LPC_IOCON, 2, 8, (IOCON_FUNC2 | IOCON_MODE_INACT)); Chip_IOCON_PinMuxSet(LPC_IOCON, 2, 9, (IOCON_FUNC2 | IOCON_MODE_INACT)); /* Setup UART4 for 115.2K8N1 */ Chip_UART_Init(UART2); Chip_UART_SetBaudFDR(UART2, 115200); Chip_UART_ConfigData(UART2, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS)); Chip_UART_TXEnable(UART2); Chip_UART_IntEnable(UART2, (UART_IER_RBRINT)); Chip_UART_SetupFIFOS(UART2, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_TRG_LEV3)); NVIC_EnableIRQ(UART2_IRQn); /* UART4 set up */ Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 22, (IOCON_FUNC3 | IOCON_MODE_INACT)); Chip_IOCON_PinMuxSet(LPC_IOCON, 5, 3, (IOCON_FUNC4 | IOCON_MODE_INACT)); /* Setup UART4 for 115.2K8N1 */ Chip_UART_Init(LPC_UART4); Chip_UART_SetBaudFDR(LPC_UART4, 115200); Chip_UART_ConfigData(LPC_UART4, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS)); Chip_UART_TXEnable(LPC_UART4); Chip_UART_IntEnable(LPC_UART4, (UART_IER_RBRINT)); Chip_UART_SetupFIFOS(LPC_UART4, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_TRG_LEV3)); //Chip_UART_SetAutoBaudReg(LPC_UART4,UART_ACR_START | UART_ACR_MODE); NVIC_EnableIRQ(UART4_IRQn); }
/* UART port init routine */ void UCOM_UartInit(void) { /* Board specific muxing */ Init_UART_PinMux(); /* Before setting up the UART, the global UART clock for USARTS 1-4 must first be setup. This requires setting the UART divider and the UART base clock rate to 16x the maximum UART rate for all UARTs. */ #if defined(USE_INTEGER_CLOCK) /* Use main clock rate as base for UART baud rate divider */ Chip_Clock_SetUARTBaseClockRate(Chip_Clock_GetMainClockRate(), false); #else /* Use 128x expected UART baud rate for fractional baud mode. */ Chip_Clock_SetUARTBaseClockRate((115200 * 128), true); #endif /* Setup UART */ Chip_UART_Init(LPC_USART); Chip_UART_ConfigData(LPC_USART, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1); Chip_UART_SetBaud(LPC_USART, UART_TEST_DEFAULT_BAUDRATE); /* Optional for low clock rates only: Chip_UART_SetBaudWithRTC32K(LPC_USART, 300); */ Chip_UART_Enable(LPC_USART); Chip_UART_TXEnable(LPC_USART); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_USART, UART_INTEN_RXRDY); /* Enable Interrupt for UART channel */ /* Priority = 1 */ NVIC_SetPriority(LPC_IRQNUM, 1); /* Enable Interrupt for UART channel */ NVIC_EnableIRQ(LPC_IRQNUM); }
/** * @brief Main UART program body * @return Always returns 1 */ int main(void) { uint8_t key; int bytes; SystemCoreClockUpdate(); Board_Init(); Board_UART_Init(LPC_UARTX); Board_LED_Set(0, false); /* Setup UART for 115.2K8N1 */ Chip_UART_Init(LPC_UARTX); Chip_UART_SetBaud(LPC_UARTX, 115200); Chip_UART_ConfigData(LPC_UARTX, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT)); Chip_UART_SetupFIFOS(LPC_UARTX, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2)); Chip_UART_TXEnable(LPC_UARTX); /* Before using the ring buffers, initialize them using the ring buffer init function */ RingBuffer_Init(&rxring, rxbuff, 1, UART_RRB_SIZE); RingBuffer_Init(&txring, txbuff, 1, UART_SRB_SIZE); /* Reset and enable FIFOs, FIFO trigger level 3 (14 chars) */ Chip_UART_SetupFIFOS(LPC_UARTX, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_TRG_LEV3)); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_UARTX, (UART_IER_RBRINT | UART_IER_RLSINT)); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UARTx_IRQn, 1); NVIC_EnableIRQ(UARTx_IRQn); /* Send initial messages */ Chip_UART_SendRB(LPC_UARTX, &txring, inst1, sizeof(inst1) - 1); Chip_UART_SendRB(LPC_UARTX, &txring, inst2, sizeof(inst2) - 1); /* Poll the receive ring buffer for the ESC (ASCII 27) key */ key = 0; while (key != 27) { bytes = Chip_UART_ReadRB(LPC_UARTX, &rxring, &key, 1); if (bytes > 0) { /* Wrap value back around */ if (Chip_UART_SendRB(LPC_UARTX, &txring, (const uint8_t *) &key, 1) != 1) { Board_LED_Toggle(0);/* Toggle LED if the TX FIFO is full */ } } } /* DeInitialize UART0 peripheral */ NVIC_DisableIRQ(UARTx_IRQn); Chip_UART_DeInit(LPC_UARTX); return 1; }
//================================================[UART Management]========================================================== void Board_UART_Init(LPC_USART_T *pUART) { if(pUART==LPC_USART0) { /* UART0 (RS485/Profibus) */ Chip_UART_Init(LPC_USART0); Chip_UART_SetBaud(LPC_USART0, 115200); Chip_UART_SetupFIFOS(LPC_USART0, UART_FCR_FIFO_EN | UART_FCR_TRG_LEV0); Chip_UART_TXEnable(LPC_USART0); Chip_SCU_PinMux(9, 5, MD_PDN, FUNC7); /* P9_5: UART0_TXD */ Chip_SCU_PinMux(9, 6, MD_PLN|MD_EZI|MD_ZI, FUNC7); /* P9_6: UART0_RXD */ Chip_UART_SetRS485Flags(LPC_USART0, UART_RS485CTRL_DCTRL_EN | UART_RS485CTRL_OINV_1); Chip_SCU_PinMux(6, 2, MD_PDN, FUNC2); /* P6_2: UART0_DIR */ uart0RxBufferData.buffer = NULL; uart0RxBufferData.size=0; uart0RxBufferData.index=0; uart0RxBufferData.flagNewPacket=0; uart0RxBufferData.timeoutCounter=0; //NVIC_SetPriority(USART0_IRQn, 1); //NVIC_EnableIRQ(USART0_IRQn); } else if(pUART==LPC_USART3) { /* UART3 (RS232) */ Chip_UART_Init(LPC_USART3); Chip_UART_SetBaud(LPC_USART3, 115200); Chip_UART_SetupFIFOS(LPC_USART3, UART_FCR_FIFO_EN | UART_FCR_TRG_LEV0); Chip_UART_TXEnable(LPC_USART3); Chip_SCU_PinMux(2, 3, MD_PDN, FUNC2); /* P2_3: UART3_TXD */ Chip_SCU_PinMux(2, 4, MD_PLN|MD_EZI|MD_ZI, FUNC2); /* P2_4: UART3_RXD */ uart3RxBufferData.buffer = NULL; uart3RxBufferData.size=0; uart3RxBufferData.index=0; uart3RxBufferData.flagNewPacket=0; uart3RxBufferData.timeoutCounter=0; //NVIC_SetPriority(USART3_IRQn, 1); //NVIC_EnableIRQ(USART3_IRQn); } /* Restart FIFOS: set Enable, Reset content, set trigger level */ Chip_UART_SetupFIFOS(pUART, UART_FCR_FIFO_EN | UART_FCR_TX_RS | UART_FCR_RX_RS | UART_FCR_TRG_LEV0); /* dummy read */ Chip_UART_ReadByte(pUART); /* enable rx interrupt */ Chip_UART_IntEnable(pUART, UART_IER_RBRINT); }
/* UART port init routine */ static void UCOM_UartInit(int i) { /* Pin-muxing done in board init. */ //Init_UART_PinMux(); if(i==1) { Chip_UART_Init(LPC_UART0); Chip_UART_SetBaud(LPC_UART0, 115200); Chip_UART_ConfigData(LPC_UART0, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT)); Chip_UART_SetupFIFOS(LPC_UART0, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2)); Chip_UART_TXEnable(LPC_UART0); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_UART0, (UART_IER_RBRINT | UART_IER_RLSINT)); /* Enable Interrupt for UART channel */ /* Priority = 1 */ NVIC_SetPriority(UART0_IRQn, 1); /* Enable Interrupt for UART channel */ } if(i==2) { Chip_UART_Init(LPC_UART2); Chip_UART_SetBaud(LPC_UART2, 115200); Chip_UART_ConfigData(LPC_UART2, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT)); Chip_UART_SetupFIFOS(LPC_UART2, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2)); Chip_UART_TXEnable(LPC_UART2); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_UART2, (UART_IER_RBRINT | UART_IER_RLSINT)); /* Enable Interrupt for UART channel */ /* Priority = 1 */ NVIC_SetPriority(UART2_IRQn, 1); /* Enable Interrupt for UART channel */ } }
/*==================[external functions definition]==========================*/ extern ciaaDevices_deviceType * ciaaDriverUart_open(char const * path, ciaaDevices_deviceType * device, uint8_t const oflag) { /* Restart FIFOS: set Enable, Reset content, set trigger level */ Chip_UART_SetupFIFOS((LPC_USART_T *)device->loLayer, UART_FCR_FIFO_EN | UART_FCR_TX_RS | UART_FCR_RX_RS | UART_FCR_TRG_LEV0); /* dummy read */ Chip_UART_ReadByte((LPC_USART_T *)device->loLayer); /* enable rx interrupt */ Chip_UART_IntEnable((LPC_USART_T *)device->loLayer, UART_IER_RBRINT); return device; }
int skynetbase_gps_init(void) { DBG("begin init GPS...\n"); NVIC_DisableIRQ(GPS_IRQn); Chip_UART_IntDisable(GPS_UART, (UART_IER_RBRINT | UART_IER_RLSINT)); Chip_UART_Init(GPS_UART); Chip_UART_SetBaud(GPS_UART, 9600); Chip_UART_ConfigData(GPS_UART, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT)); Chip_UART_SetupFIFOS(GPS_UART, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2)); Chip_UART_TXEnable(GPS_UART); // Before using the ring buffers, initialize them using the ring buffer init function RingBuffer_Init(&rxring, rxbuff, 1, UART_RRB_SIZE); RingBuffer_Init(&txring, txbuff, 1, UART_SRB_SIZE); // Reset and enable FIFOs, FIFO trigger level 3 (14 chars) Chip_UART_SetupFIFOS(GPS_UART, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_TRG_LEV3)); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(GPS_UART, (UART_IER_RBRINT | UART_IER_RLSINT)); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(GPS_IRQn, 1); NVIC_EnableIRQ(GPS_IRQn); DBG("GPS up.\n"); skynetbase_gps_config("$PUBX,40,GLL,0,0,0,0*5C\r\n"); msDelayActive(250); skynetbase_gps_config("$PUBX,40,ZDA,0,0,0,0*44\r\n"); msDelayActive(250); skynetbase_gps_config("$PUBX,40,VTG,0,0,0,0*5E\r\n"); msDelayActive(250); skynetbase_gps_config("$PUBX,40,GSV,0,0,0,0*59\r\n"); msDelayActive(250); skynetbase_gps_config("$PUBX,40,RMC,0,0,0,0*47\r\n"); msDelayActive(250); skynetbase_gps_config("$PUBX,40,GSA,0,0,0,0*4E\r\n"); msDelayActive(250); skynetbase_gps_config("$PUBX,40,GGA,0,0,0,0*5A\r\n"); msDelayActive(250); DBG("GPS configured.\n"); // TODO return 1; }
/* UCOM bulk EP_IN and EP_OUT endpoints handler */ static ErrorCode_t UCOM_bulk_hdlr(USBD_HANDLE_T hUsb, void *data, uint32_t event) { UCOM_DATA_T *pUcom = (UCOM_DATA_T *) data; uint32_t count = 0; switch (event) { /* A transfer from us to the USB host that we queued has completed. */ case USB_EVT_IN: /* check if UART had more data to send */ if (pUcom->rxBuf_uartIndex < pUcom->rxBuf_usbIndex) { count = UCOM_BUF_SZ - pUcom->rxBuf_usbIndex; } else { count = pUcom->rxBuf_uartIndex - pUcom->rxBuf_usbIndex; } if (count) { pUcom->usbTxBusy = 1; count = USBD_API->hw->WriteEP(pUcom->hUsb, USB_CDC_IN_EP, &pUcom->rxBuf[g_uCOM.rxBuf_usbIndex], count); g_uCOM.rxBuf_usbIndex = (g_uCOM.rxBuf_usbIndex + count) & (UCOM_BUF_SZ - 1); } else { pUcom->usbTxBusy = 0; } break; /* We received a transfer from the USB host . */ case USB_EVT_OUT: if ((Chip_UART_GetIntsEnabled(LPC_USART) & UART_IER_THREINT) == 0) { pUcom->txBuf_count = USBD_API->hw->ReadEP(hUsb, USB_CDC_OUT_EP, pUcom->txBuf); pUcom->txBuf_uartIndex = 0; /* kick start UART tranmission */ pUcom->txBuf_uartIndex = Chip_UART_Send(LPC_USART, &pUcom->txBuf[g_uCOM.txBuf_uartIndex], pUcom->txBuf_count); pUcom->txBuf_count -= pUcom->txBuf_uartIndex; /* Enable UART transmit interrupt */ Chip_UART_IntEnable(LPC_USART, UART_IER_THREINT); } else { pUcom->usbRxPending++; } break; default: break; } return LPC_OK; }
/* Populate a transmit ring buffer and start UART transmit */ uint32_t Chip_UART_SendRB(LPC_USART_T *pUART, RINGBUFF_T *pRB, const void *data, int bytes) { uint32_t ret; uint8_t *p8 = (uint8_t *) data; /* Don't let UART transmit ring buffer change in the UART IRQ handler */ Chip_UART_IntDisable(pUART, UART_IER_THREINT); /* Move as much data as possible into transmit ring buffer */ ret = RingBuffer_InsertMult(pRB, p8, bytes); Chip_UART_TXIntHandlerRB(pUART, pRB); /* Add additional data to transmit ring buffer if possible */ ret += RingBuffer_InsertMult(pRB, (p8 + ret), (bytes - ret)); /* Enable UART transmit interrupt */ Chip_UART_IntEnable(pUART, UART_IER_THREINT); return ret; }
/* UART port init routine */ static void UCOM_UartInit(void) { /* Board specific muxing */ Init_UART_PinMux(); Chip_UART_Init(LPC_USART); Chip_UART_SetBaud(LPC_USART, 115200); Chip_UART_ConfigData(LPC_USART, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT)); Chip_UART_SetupFIFOS(LPC_USART, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2)); Chip_UART_TXEnable(LPC_USART); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_USART, (UART_IER_RBRINT | UART_IER_RLSINT)); /* Enable Interrupt for UART channel */ /* Priority = 1 */ NVIC_SetPriority(UART0_IRQn, 1); /* Enable Interrupt for UART channel */ NVIC_EnableIRQ(UART0_IRQn); }
/* Initialize Interrupt for UART */ static void App_Interrupt_Init(void) { /* Enable UART Rx & line status interrupts */ /* * Do not enable transmit interrupt here, since it is handled by * UART_Send() function, just to reset Tx Interrupt state for the * first time */ Chip_UART_IntEnable(LPC_UART, (UART_IER_RBRINT | UART_IER_RLSINT)); /* Before using the ring buffers, initialize them using the ring buffer init function */ RingBuffer_Init(&rxring, rxbuff, 1, UART_RB_SIZE); RingBuffer_Init(&txring, txbuff, 1, UART_RB_SIZE); /* Enable Interrupt for UART channel */ /* Priority = 1 */ NVIC_SetPriority(UARTx_IRQn, 1); /* Enable Interrupt for UART channel */ NVIC_EnableIRQ(UARTx_IRQn); }
void uart_init(void) { /* Board specific muxing */ uart_pinmux(); Chip_UART_Init(LPC_USART); Chip_UART_SetBaudFDR(LPC_USART, 115200); Chip_UART_ConfigData(LPC_USART, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT)); Chip_UART_SetupFIFOS(LPC_USART, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2)); Chip_UART_TXEnable(LPC_USART); RingBuffer_Init(&uart_rxrb, uart_rxdata, 1, UART_RX_BUF_SZ); RingBuffer_Init(&uart_txrb, uart_txdata, 1, UART_TX_BUF_SZ); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_USART, (UART_IER_RBRINT | UART_IER_RLSINT)); /* Enable Interrupt for UART channel */ /* Priority = 1 */ NVIC_SetPriority(UART0_IRQn, 1); /* Enable Interrupt for UART channel */ NVIC_EnableIRQ(UART0_IRQn); }
void UART_Init(void) { struct __serial_info *serial = (struct __serial_info *)(get_S2E_Packet_pointer()->serial_info); /* Setup UART for 115.2K8N1 */ Chip_UART_Init(LPC_USART); serial_info_init(serial); Chip_UART_SetupFIFOS(LPC_USART, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV3)); Chip_UART_TXEnable(LPC_USART); /* Before using the ring buffers, initialize them using the ring buffer init function */ RingBuffer_Init(&rxring, rxbuff, 1, UART_RRB_SIZE); RingBuffer_Init(&txring, txbuff, 1, UART_SRB_SIZE); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_USART, (UART_IER_RBRINT | UART_IER_RLSINT)); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UART0_IRQn, 1); NVIC_EnableIRQ(UART0_IRQn); }
static ErrorCode_t UCOM_bulk_hdlrOutGeneric(USBD_HANDLE_T hUsb, void *data, uint32_t event) { UCOM_DATA_T *pUcom = (UCOM_DATA_T *) data; switch (event) { case USB_EVT_OUT: if(pUcom->selected!=0){ if ((Chip_UART_GetIntsEnabled(pUcom->selected) & UART_IER_THREINT) == 0) { pUcom->txBuf_count = USBD_API->hw->ReadEP(hUsb, pUcom->outEndpoint, pUcom->txBuf); pUcom->txBuf_uartIndex = 0; /* kick start UART tranmission */ pUcom->txBuf_uartIndex = Chip_UART_Send(pUcom->selected, &pUcom->txBuf[pUcom->txBuf_uartIndex], pUcom->txBuf_count); pUcom->txBuf_count -= pUcom->txBuf_uartIndex; /* Enable UART transmit interrupt */ Chip_UART_IntEnable(pUcom->selected, UART_IER_THREINT); } else { pUcom->usbRxPending++; } return LPC_OK; } break; break; default: break; } return ERR_USBD_UNHANDLED; }
/** * @brief Application main function * @return Does not return * @note This function will not return */ int main(void) { uint32_t i; uint8_t repeat_num = UART_TEST_REPEAT_NUMBER; volatile int j = 1; uint8_t ch = 0; uint32_t IntStatus; /* Generic Initialization */ Board_Init(); Board_LED_Set(0, false); /* Disable UART1 IRQ */ NVIC_DisableIRQ(UART1_IRQn); /* Initialize the UARTs */ App_UART_Init(LPC_USART0); App_UART_Init(LPC_USART1); /* Custom Initialization */ Chip_UART_IntEnable(LPC_USART1, RXRDY_INT, DISABLE); Chip_UART_IntEnable(LPC_USART1, TXRDY_INT, DISABLE); NVIC_EnableIRQ(UART1_IRQn); /* Data transfer loop */ while (repeat_num--) { bufferInit(repeat_num); /* Sending from UART0 (polling mode) to UART1 (interrupt mode) */ receiveCompleted = false; Chip_UART_IntEnable(LPC_USART1, RXRDY_INT, ENABLE); for (i = 0; i < BUFFER_SIZE; i++) { while (Chip_UART_SendByte(LPC_USART0, TxBuf0[i]) != SUCCESS) {} } while (!receiveCompleted) {} /* Clear Rx FIFO */ Chip_UART_ReceiveByte(LPC_USART0, &ch); /* Sending from UART1 (interrupt mode) to UART0 (polling mode) */ sendCompleted = false; Chip_UART_IntEnable(LPC_USART1, TXRDY_INT, ENABLE); for (i = 0; i < BUFFER_SIZE; i++) { while (Chip_UART_ReceiveByte(LPC_USART0, &RxBuf0[i]) != SUCCESS) {} } while (!sendCompleted) {} bufferCheck(); } NVIC_DisableIRQ(UART1_IRQn); /* Test OK - Turn on Red LED */ Board_LED_Set(0, true); /* Should not return */ while (j) {} return 0; }
/** * @brief Main UART program body * @return Always returns -1 */ int main(void) { FlagStatus exitflag; uint8_t buffer[10]; int ret = 0; int len; SystemCoreClockUpdate(); Board_Init(); Board_UART_Init(LPC_UART); #if !((defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357) && UARTNum==3) || ((!(defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357))) && UARTNum==0)) Chip_UART_Init(LPC_UART); Chip_UART_SetBaud(LPC_UART, 115200); Chip_UART_ConfigData(LPC_UART, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT); /* Default 8-N-1 */ /* Enable UART Transmit */ Chip_UART_TXEnable(LPC_UART); #endif /* Reset FIFOs, Enable FIFOs and DMA mode in UART */ Chip_UART_SetupFIFOS(LPC_UART, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_DMAMODE_SEL | UART_FCR_TRG_LEV0)); /* Enable UART End of Auto baudrate & Auto baudrate timeout interrupts */ Chip_UART_IntEnable(LPC_UART, (UART_IER_ABEOINT | UART_IER_ABTOINT)); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UARTx_IRQn, 1); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(UARTx_IRQn); /* Send UART Autobaud completed message */ Chip_UART_SendBlocking(LPC_UART, uartABStart, sizeof(uartABStart)); /* ---------------------- Auto baud rate section ----------------------- */ /* Start auto baudrate mode */ Chip_UART_ABCmd(LPC_UART, UART_ACR_MODE0, true, ENABLE); /* Loop until auto baudrate mode complete */ while (Chip_UART_GetABEOStatus(LPC_UART) == RESET) {} /* Send UART Autobaud completed message */ Chip_UART_SendBlocking(LPC_UART, uartABComplete, sizeof(uartABComplete)); /* Disable UART Interrupt */ NVIC_DisableIRQ(UARTx_IRQn); /* Print welcome screen */ Print_Menu_Polling(); exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = Chip_UART_Read(LPC_UART, buffer, 1); } if (buffer[0] == 27) { /* ESC key, set exit flag */ Chip_UART_SendBlocking(LPC_UART, uartPolling_menu3, sizeof(uartPolling_menu3)); ret = -1; exitflag = SET; } else if (buffer[0] == 'c') { Chip_UART_SendBlocking(LPC_UART, uartPolling_menu4, sizeof(uartPolling_menu4)); len = 0; while (len == 0) { len = Chip_UART_Read(LPC_UART, buffer, sizeof(buffer)); if ((buffer[0] != '1') && (buffer[0] != '2') && (buffer[0] != '3')) { len = 0; } } switch (buffer[0]) { case '1': /* Polling Mode */ Chip_UART_SendBlocking(LPC_UART, uartPolling_menu5, sizeof(uartPolling_menu5)); break; case '2': /* Interrupt Mode */ ret = 2; /* Exitflag = SET; */ App_Interrupt_Test(); Print_Menu_Polling(); break; case '3': /* DMA mode */ ret = 3; App_DMA_Test(); Print_Menu_Polling(); break; } } } /* Wait for current transmission complete - THR must be empty */ while (Chip_UART_CheckBusy(LPC_UART) == SET) {} /* DeInitialize UART0 peripheral */ Chip_UART_DeInit(LPC_UART); return ret; }
/** * @brief Main UART program body * @return Always returns 1 */ int main(void) { uint8_t key; int bytes; SystemCoreClockUpdate(); Board_Init(); Init_UART_PinMux(); Board_LED_Set(0, false); /* Before setting up the UART, the global UART clock for USARTS 1-4 must first be setup. This requires setting the UART divider and the UART base clock rate to 16x the maximum UART rate for all UARTs. */ #if defined(USE_INTEGER_CLOCK) /* Use main clock rate as base for UART baud rate divider */ Chip_Clock_SetUARTBaseClockRate(Chip_Clock_GetMainClockRate(), false); #else /* Use 128x expected UART baud rate for fractional baud mode. */ Chip_Clock_SetUARTBaseClockRate((115200 * 128), true); #endif /* Setup UART */ Chip_UART_Init(LPC_USART); Chip_UART_ConfigData(LPC_USART, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1); Chip_UART_SetBaud(LPC_USART, UART_TEST_DEFAULT_BAUDRATE); /* Optional for low clock rates only: Chip_UART_SetBaudWithRTC32K(LPC_USART, 300); */ Chip_UART_Enable(LPC_USART); Chip_UART_TXEnable(LPC_USART); /* Before using the ring buffers, initialize them using the ring buffer init function */ RingBuffer_Init(&rxring, rxbuff, 1, UART_RB_SIZE); RingBuffer_Init(&txring, txbuff, 1, UART_RB_SIZE); /* Enable receive data and line status interrupt */ Chip_UART_IntEnable(LPC_USART, UART_INTEN_RXRDY); Chip_UART_IntDisable(LPC_USART, UART_INTEN_TXRDY); /* May not be needed */ /* Enable UART interrupt */ NVIC_EnableIRQ(LPC_IRQNUM); /* Initial message sent using blocking method to prevent ring buffer overflow */ Chip_UART_SendBlocking(LPC_USART, inst1, sizeof(inst1) - 1); Chip_UART_SendRB(LPC_USART, &txring, inst2, sizeof(inst2) - 1); /* Poll the receive ring buffer for the ESC (ASCII 27) key */ key = 0; while (key != 27) { bytes = Chip_UART_ReadRB(LPC_USART, &rxring, &key, 1); if (bytes > 0) { /* Wrap value back around */ if (Chip_UART_SendRB(LPC_USART, &txring, (const uint8_t *) &key, 1) != 1) { Board_LED_Toggle(0);/* Toggle LED if the TX FIFO is full */ } } } /* DeInitialize UART peripheral */ NVIC_DisableIRQ(LPC_IRQNUM); Chip_UART_DeInit(LPC_USART); return 1; }