Help with UART?


(Dave) #1

I’ve been working with the UART device recently, attempting to create a few higher-level interfacing functions for common tasks like changing baud settings and performing reads, etc (cribbed heavily from the UARTClass.* files in the Arduino package).

I have a few things to finish up but am far enough along that I should be able to write string output at selected baud rates. My working branch on GitHub: https://github.com/dgrubb/freedom-e-sdk/tree/uart_development/software/demo_uart

The problem I’m having is that, when I attempt to write out a string, anything after the 10th byte turns into Authentic Frontier Gibberish. To verify I’ve attached a second USB-TTL serial adapter directly to the header I/O pins and tried a few different baud rates but get the same result. In either case, I can interspace write(STDOUT_FILENO, …) and those strings are successfully printed, so I suspect I’m omitting something really obvious when I’m putting data into the TX FIFO.

Any ideas?


(Albert Ou) #2

It’s easy to overlook, but the UART0_REG macro dereferences the pointer already.
Try this patch:

--- a/software/demo_uart/UART_driver.c
+++ b/software/demo_uart/UART_driver.c
@@ -80,7 +80,7 @@ void UART_set_stop_bits(int stop_bits)
  */
 int UART_put_char(char ch, int blocking)
 {
-  volatile uint32_t *val = UART0_REG(UART_REG_TXFIFO);
+  volatile uint32_t *val = &UART0_REG(UART_REG_TXFIFO);
   uint32_t busy = (*val) & 0x80000000;
   if (blocking) {
     while (*val & 0x80000000);

(Dave) #3

Thanks for the quick answer, Albert. That resolved it. :slight_smile:


(Dave) #4

Incidentally, I really liked the attention to detail in this part:

UARTClass.h:

enum {
  SIO_RXBUFSIZE = (1 << 3),
  SIO_RXBUFMASK = (SIO_RXBUFSIZE - 1)
};

UARTClass.cpp:

c = sio_rxbuf[sio_rxbuf_tail++];
sio_rxbuf_tail &= SIO_RXBUFMASK;

Many people would have just done:

#define SIO_RXBUFSIZE 8

if (sio_rxbuf_tail >= SIO_RXBUFSIZE) {
   sio_rxbuf_tail = 0;
}

Instead, the test and conditional reset are combined into a single efficient bitwise operation. Nice.


(Donnie Agema) #5

Many people would have just done:

#define SIO_RXBUFSIZE 8

if (sio_rxbuf_tail >= SIO_RXBUFSIZE) {
sio_rxbuf_tail = 0;
}

Instead, the test and conditional reset are combined into a single efficient bitwise operation. Nice.

There is of course a tradeoff in readability. I wonder just how much difference in efficiency is gained.


(Dave) #6

This is true, and I would hope that a compiler would optimise out the conditional branch anyway, but it made me smile.


(kumar) #7

Hi All,
I have U540 unleased board and need urgent help:
if we short tx and rx of UART0 for loopbacktest. is it possible that this can damage USB line(micro usb) or can create any issues?