/* 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/DMA program body * @return Does not exit */ int main(void) { int bytes = 0, idx; uint8_t buff[UARTRXBUFFSIZE]; SystemCoreClockUpdate(); Board_Init(); Init_UART_PinMux(); Board_LED_Set(0, false); #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_USART0); Chip_UART_ConfigData(LPC_USART0, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1); Chip_UART_SetBaud(LPC_USART0, 115200); /* Optional for low clock rates only: Chip_UART_SetBaudWithRTC32K(LPC_USART, 300); */ Chip_UART_Enable(LPC_USART0); Chip_UART_TXEnable(LPC_USART0); /* DMA initialization - enable DMA clocking and reset DMA if needed */ Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_DMA); Chip_SYSCTL_PeriphReset(RESET_DMA); /* Initialize ROM API pointer */ pDMAApi = LPC_DMAD_API; size_in_bytes = pDMAApi->dma_get_mem_size(); if (size_in_bytes > RAMBLOCK) { /* Adjust RAMBLOCK size in this case */ return 1; } /* Setup DMA ROM driver, provided RAM blocks for DMA Handle */ dma_handle = pDMAApi->dma_setup(LPC_DMA_BASE, dma_ram_block); /* Init UART 0 TX descriptor */ countTXDescUsed = 0; /* Enable the DMA IRQ */ NVIC_EnableIRQ(DMA_IRQn); /* Enqueue a bunch of strings in DMA transmit descriptors and start transmit. In this use of DMA, the descriptors aren't chained, so the DMA restarts the next queued descriptor in the DMA interrupt handler. */ for (idx = 0; idx < DMASENDSTRCNT; idx++) { sprintf(dmaSendStr[idx], "DMA send string (unlinked) #%d\r\n", idx); dmaTXSend((uint8_t *) dmaSendStr[idx], strlen(dmaSendStr[idx])); } /* Wait for UART TX DMA channel to go inactive */ while (1) { __WFI(); if (countTXDescUsed == 0) { break; } } /* Receive buffers are queued. The DMA interrupt will only trigger on a full DMA buffer receive, so if the UART is idle, but the DMA is only partially complete, the DMA interrupt won't fire. For UART data receive where data is not continuous, a timeout method will be required to flush the DMA when the DMA has pending data and no data has been received on the UART in a specified timeout */ dmaRXQueue(); /* Get RX data via DMA and send it out on TX via DMA */ while (1) { /* Sleep until something happens */ __WFI(); /* Did any data come in? */ bytes = checkRxData(buff); if (bytes > 0) { /* RX data received, send it via TX DMA */ dmaTXSend(buff, bytes); } } return 1; }
/** * @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; }