Let’s have some more fun with that crazy minimalistic LPC810 chip!
Serial output capability is really useful during development. Without it, we’d have to use an SWD “hardware debugging” tool, which – while powerful – is also quite a bit more complex and would require some extra hardware.
The code to implement serial output is fairly simple. Some hardware initialisation code, and a simple polled I/O loop to send individual characters out.
Here is the simplest possible hookup to support serial output via an FTDI interface:
As in the Getting Started series, the LED is used to drop the voltage from 5V to roughly 3.3V and power the chip, and there’s a small 0.1 µF decoupling capacitor on the back:
The 8-DIP chip socket makes it easy to replace the LPC810 µC. As example, let’s upload this demo code into it, using the techniques described in the Getting Started series:
#include <stdio.h>
#include "serial.h"
int main () {
LPC_SWM->PINASSIGN0 = 0xFFFFFF04UL; // only connect TXD
serial.init(LPC_USART0, 115200);
printf("DEV_ID = %08x\n", LPC_SYSCON->DEVICE_ID);
printf("GPREG0 = %08x\n", LPC_PMU->GPREG0);
printf("GPREG1 = %08x\n", LPC_PMU->GPREG1);
printf("GPREG2 = %08x\n", LPC_PMU->GPREG2);
printf("GPREG3 = %08x\n", LPC_PMU->GPREG3);
return 0;
}
The “lpc21isp” utility can then be used to connect and listen to the serial port as follows:
$ lpc21isp -termonly -control x /dev/ttyUSB* 115200 0
[...]
Terminal started (press Escape to abort)
DEV_ID = 00008100
GPREG0 = 00000000
GPREG1 = 00000000
GPREG2 = 00000000
GPREG3 = 00000000
If you’re not familiar with it, “printf” is a very widely-used C function to print and format a string for textual output. See Wikipedia for more information.
This example prints the hex device ID of the chip (proving that it is indeed an LPC810), as well as some register values – these will all be zero when the chip is first powered up.
Not all printf implementations are the same. This one comes from Georges Menie and is extremely compact for what it does. You can format strings, integers, unsigned ints, hex values, and you can even specify field witdhs and optional left- / right- / zero-padding.
This version does not support floating point, but there is a larger one which does.
So as you can see, serial output with quite extensive formatting capabilities is very easy to add to an LPC810 project. You just need to do four things:
- include the “serial.h” header file in your code
- assign the pin you’re going to use for the serial output
- call
serial.init
in your main code, specifying the device and baud rate - include the “printf-stdarg.c” source file in your project build
Serial + printf support will increase the generated code size by about 1000 bytes.
[Back to article index]