Programming LPC1114FN28 with standard C using Newlib

Some of my old posts show how to program the ARM Cortex M0 LPC chip in a bare metal manner, although this approach provides a simple setup that requires no additional libraries, it is only for the studying purpose. It is not a good options for production since your code will be highly platform dependent and you need to handle so many low level stuffs (e.g. registers configuration). The Newlib offers a more productive way by providing a standard C interface to abstract the development on such embedded system. In using Newlib, the code is a lot simpler and more portable.

Newlib does not work out of the box on all platforms since it provides only a "skeleton" library, developers need to "port" it on their platform by defining the system calls which actually implement all features supported by the platform (GPIO, UART, SPI, network, ADC, etc.). Basically, to use Newlib on LPC1114FN28, one need to provide the implementation of a set of system calls specific to this chip, this can be done in a bare metal manner (as we did in previous posts :) ) or via a standard interface such as CMSIS. So, when put it all together, the development using Newlib will be some thing like this:

Porting the Newlib on LPC1114FN28 is not the objective of this post since this is done by some well-know development platform/libraries such as the MbedOS (which supports a wide range of hardware targets ). In my work, i used a Newlib port for the lpc111x family implemented by Philip Munts available at https://github.com/chettrick/arm-mcu/tree/master/lpc11xx, however i have some modifications on the original code to make it work as i need. The modified code is available on my github repository , which contain several examples on GPIO, Serial, PWM, etc. For example, this shows how to control a GPIO pin and serial port using the library

#ifndef CONIO_STDIO
#error This program requires the lightweight console I/O library
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cpu.h>
#include "delay.h"

int main(void)
{
    cpu_init(48000000);
    gpio_configure(PIO1_3, GPIO_MODE_OUTPUT);
    gpio_write(PIO1_3, 0x01);
    serial_stdio(CONSOLE_PORT);
    int dv;
    gpio_write(PIO1_3, 0x00);
    char * str = "this is a test string \n";
    for (;;)
    {
        printf(str);
        delay_ms(500);
    }
}

This snippet of code is pretty neat and clear, we init the CPU at a specific frequency, configure a GPIO (PIO1_3) as output, light it on then init the serial line, when the serial line is on, turn the led off and write some thing to the serial port (which is actual the UART interface on the LPC1114FN28 chip) each 500 ms, using the standard printf function. The code is simple and highly portable on other platform.

Related posts

Powered by antd server, (c) 2017 - 2018 Xuan Sang LE