Tuesday, 25 October 2016

Programming the Launchpad UART

Having immersed ourselves in the complexities of CAN recently, communicating by UART is like coming up for air!

UART is dead simple: 
       One wire (plus ground)
       Agree a data (baud) rate data
      ...and start sending 0's and 1's!

(Actually, there are a few other things you can add such as check-bits etc. But we'll ignore these)


The Texas Instruments TM4C123 Launchpad is a low cost ARM micro-controller I introduced here

TI have free software (an API) for UART communication, but I decided to write my own (partly for fun, partly 'cos I wasn't 100% clear about my licence-freedom to show TI's code, and partly 'cos I wanted to use the ARM CMSIS code standard).

My code is here


The functions I've defined do the obvious...

startUART(UART0, UART0_CFG, baud115200) starts a UART.

fprintstringUART(UART0, "blah, blah...") prints a string.

fprintcharUART(UART0, myChar) prints a character ('myChar')

fprintUint32UART(UART0, 32, DEC) prints '32'

fprintUint32UART(UART0, 32, HEX) prints '0x20'

Using a UART monitor such as the awesome PuTTY, UART_example.c results in



The code has limitations:

At present it's limited to baud9600 or baud115200 (see the enum's in uart_helper.h). Also, when starting a UART its necessary to pass in configuration data. You do this by passing the fstartUART() one of the predefined configurations "UARTn_CFG" (my next post, and the comments in uart_helper.h explain how this works). So far I've only gotten around to defining UART0_CFG.


Finally fprintUint32UART() assumes you're doing the right thing passing it an unsigned integer - there's no size checking etc.
The code-flow basically follows the Lauchpad datasheet (DS-TM4C123GH6PM-15842.2741). So, for example, the datasheet tells you on p.902, that if you want to use the UART, the first thing to do is "Enable the UART module using the RCGCUART register"

Hence the line
     SYSCTL->RCGCUART |= RCGCUART_R0;


in my fstartUART() function. Etc.

For those just getting started I gave an intro. to the concept of registers here

For those unfamiliar with "->" notation, this comes from the so-called "ARM CMSIS". This is a standard way to talk to ARM chips. More about this another time.

Definitions of all the Launchpad's registers (RCGCUART etc. etc. ) are held in a board file (here TM4C123GH6PM.h) you can download from TI. I couldn't find a file listing the individual register-bit-fields however (anyone?) so I started my own tm4c_register_fields.h.

That's all for today. If you spot bugs (there are bound to be some!) or just have a comment generally, drop me a line.

No comments:

Post a Comment