Tool/software:
Hello,
I initialize the serial port as below:
void init_serial(void) {
MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
MAP_UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC); // UART_CLOCK_PIOSC or UART_CLOCK_SYSTEM
MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTStdioConfig(0, 230400, 16000000);
}
I want to process nmea data coming from a gps.while doing other things. Is there an example on doing serial handling as the data comes in? Equivalent to:
while(Serial.available()) {
c=Serial.read();
// do stuff with c
}
Best Regards,
C.
Hi Can,
Below is a snippet of code to read from UART and echo it out on the same UART.
// // Enter a loop to read characters from the UART, and write them back // (echo). When a line end is received, the loop terminates. // do { // // Read a character using the blocking read function. This function // will not return until a character is available. // cThisChar = UARTCharGet(UART0_BASE); // // Write the same character using the blocking write function. This // function will not return until there was space in the FIFO and // the character is written. // UARTCharPut(UART0_BASE, cThisChar); }
You can find UART examples in:
C:\ti\TivaWare_C_Series-2.2.0.295\examples\peripherals\uart
C:\ti\TivaWare_C_Series-2.2.0.295\examples\boards\ek-tm4c123gxl\uart_echo
Hi Charles,
I was using usb-cdc, with ControlHandler, TxHandler, and RxHandler. Is there anyway to use the regular uart in this fashion, I actually just need rx handler, so I guess the example at uart_echo can be modified to suit my needs.
Another thing I am curious about is how to use the circular ring after
cThisChar = UARTCharGet(UART0_BASE);
Best regards,
C.
I was using usb-cdc, with ControlHandler, TxHandler, and RxHandler. Is there anyway to use the regular uart in this fashion, I actually just need rx handler, so I guess the example at uart_echo can be modified to suit my needs.
Hi Can,
I'm not quite following you here. Below is the RXHandler for USB-CDC. What do you mean you want to the regular uart in this fashion? The type of message event (e.g. USB_EVENT_RX_AVAILABLE, USB_EVENT_DATA_REMAINING) that is passed to the RXHandler is specific to the USB CDC class. There is no parameter to pass to a UART ISR. You can use UARTSpaceAvail() to mimic USB_EVENT_DATA_REMAINING and UARTCharGet() to mimic USB_EVENT_RX_AVAILABLE.
uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData) { uint32_t ui32Count; // // Which event are we being sent? // switch(ui32Event) { // // A new packet has been received. // case USB_EVENT_RX_AVAILABLE: { // // Feed some characters into the UART TX FIFO and enable the // interrupt so we are told when there is more space. // USBUARTPrimeTransmit(USB_UART_BASE); MAP_UARTIntEnable(USB_UART_BASE, UART_INT_TX); break; } // // We are being asked how much unprocessed data we have still to // process. We return 0 if the UART is currently idle or 1 if it is // in the process of transmitting something. The actual number of // bytes in the UART FIFO is not important here, merely whether or // not everything previously sent to us has been transmitted. // case USB_EVENT_DATA_REMAINING: { // // Get the number of bytes in the buffer and add 1 if some data // still has to clear the transmitter. // ui32Count = MAP_UARTBusy(USB_UART_BASE) ? 1 : 0; return(ui32Count); } // // We are being asked to provide a buffer into which the next packet // can be read. We do not support this mode of receiving data so let // the driver know by returning 0. The CDC driver should not be sending // this message but this is included just for illustration and // completeness. // case USB_EVENT_REQUEST_BUFFER: { return(0); } // // We don't expect to receive any other events. Ignore any that show // up in a release build or hang in a debug build. // default: #ifdef DEBUG while(1); #else break; #endif } return(0); }
Hello Charles,
What I meant was: The usb-cdc example, along with many other datastructures, uses an interrupt handler so when a packet is received RxHandler gets called. The non-cdc uart examples, do work by polling in a loop to detect incoming data. Am I wrong? Or behind the code usb-cdc examples too work by polling?
Best Regards,
Can
Hi Can,
For UART, you can also use the interrupt mode. Check out the C:\ti\TivaWare_C_Series-2.2.0.295\examples\boards\ek-tm4c123gxl\uart_echo example where interrupt is used to receive UART stream and process the data inside the ISR. For USB, the callback is used (e.g. RxHandler), no polling is used.