NXP LPC17xx driver library

18 Aug 2010

Hello,

Any one tried NXP's driver library ?  I havn't seen much posts about this library I wonder why no one is using it ? it looks really good and heavily documentd with examples too, anyway, I downloaded the library and I managed to finally run the LedBlinky example after minor modifications, I compiled with codesourcery toolchain, the binary runs fine and the led actually blinks :) however, the Delay function blocks for ever, I tried to set the led in the handler but that didn't work so it seems that the SysTick_Handler is not being called, any help please ? this is my code

 

 

#include "lpc17xx_gpio.h"
#include "lpc17xx_libcfg.h"

volatile unsigned long SysTickCnt;

void SysTick_Handler (void) {
  SysTickCnt++;
}

void Delay (unsigned long tick) {
    unsigned long systickcnt;
    
    systickcnt = SysTickCnt;
    while ((SysTickCnt - systickcnt) < tick);
}

int main(void)
{
  uint8_t port = 1;
  uint32_t led = (1<<18);

  SysTick_Config(SystemCoreClock / 1000); /* Generate interrupt each 1 ms   */

  GPIO_SetDir(port, led, 1);
  GPIO_ClearValue(port, led);

  while (1) {
      GPIO_SetValue(port, led);
      Delay(500);
      GPIO_ClearValue(port, led);
      Delay(500);
  }
  return 0;
}

 

 

18 Aug 2010

If you're compiling in C++ mode, make it

extern "C" void SysTick_Handler (void) {..}

Otherwise the linker will not pick up your handler as it will have a mangled name.

18 Aug 2010

user avatar Igor Skochinsky wrote:

If you're compiling in C++ mode, make it

extern "C" void SysTick_Handler (void) {..}

Otherwise the linker will not pick up your handler as it will have a mangled name.

I'm compiling with gcc arm-none-eabi-gcc-4.4.1

18 Aug 2010

 

user avatar Ibrahim Abd Elkader wrote:

I'm compiling with gcc arm-none-eabi-gcc-4.4.1

But does your file have .c or .cpp extension?

 

18 Aug 2010

user avatar Igor Skochinsky wrote:

 

user avatar Ibrahim Abd Elkader wrote:

I'm compiling with gcc arm-none-eabi-gcc-4.4.1

But does your file have .c or .cpp extension?

 

 

No I'm using the example LedBlinky.c I just simplified it by removing code for specific boards MCB and IAR whatever those are :) and changed the bin to (1<<18) the led is on but doesn't blink,  maybe I need to change something is the initialization code ? I'm really just starting to wrap my mind around all this, so thanks for your help

18 Aug 2010

Okay, then that is not the problem.

I see that the original is calling SystemInit(); Why did you remove it, and does putting it back help?

18 Aug 2010 . Edited: 18 Aug 2010

I have the original LedBlinky.c it doesn't call SystemInit(), different version ? but anyway I added SystemInit() doesn't help, there's something that I have changed, the linker script that comes with the library didn't work, so I use cmsis example linker script, only it didn't define __bss_start__ and __bss_end__ so I defined them like the original linker script and it worked:

 

 

146   .bss :
147   {
148     . = ALIGN (8);
149     __bss_start__ = .;
150     *(.shbss)
151     *(.bss .bss.* .gnu.linkonce.b.*)
152     *(COMMON)
153     __bss_end__ = .;
154     *(.ram.b)
155     . = ALIGN (8);
156     _end = .;
157     __end = .;
158   } >ram AT>rom

 

 

Any ideas :) ?

 

Edit:

Would you be so kind to recommend books or any material about this subject ? I would like to write my own initialization and startup code one day, I have started with OReilly programming embedded systems with c and gnu

I could understand what everything does but not how. I also have all the documentation and manuals for both the cortex and the lpc, and I will start reading the definitive guide to the arm cortex next, I appreciate the mbed libraries and all, but I sort of bought it to learn more about arm for college.

 

Thanks Igor

18 Aug 2010

I copied system_LPC17xx.c and startup_LPC17xx.s from the cmsis_v1p30, I thought maybe something's off with the startup and/or system code, and it works :) ! the clock seems to be correct too.

I'm still wondering why this library is not used more ?

 

Anyway, thanks for all your help. I will wait for your books recommandations if you have any.

18 Aug 2010

I haven't read it myself but I've heard good things about The Definitive Guide to the ARM Cortex-M3 2nd Ed.

18 Aug 2010

I just started reading it and it looks great, I really wish to understand all the details, maybe this is a good start.

I'm also trying to build the same example, LedBlinky, with my own toolchain, if you're interested I will tell you how it goes.

 

Thanks again Igor.

18 Aug 2010

Ibrahim,

I'd be very interested to hear how your experiments with your own toolchain go. Like you I am learning.

Regards, Romilly

19 Aug 2010 . Edited: 19 Aug 2010

Romilly,

After compiling LedBlinky, I rolled up all the drivers plus the startup and system code in one library, copied the library, headers and linker script to /usr/local/lpc1768,  so basically the library has this:

 

debug_frmwrk.o
lpc17xx_dac.o
lpc17xx_gpio.o
lpc17xx_mcpwm.o
lpc17xx_qei.o
lpc17xx_ssp.o
lpc17xx_wdt.o
lpc17xx_adc.o
lpc17xx_emac.o
lpc17xx_i2c.o
lpc17xx_nvic.o
lpc17xx_rit.o
lpc17xx_systick.o
lpc17xx_can.o
lpc17xx_exti.o
lpc17xx_i2s.o
lpc17xx_pinsel.o
lpc17xx_rtc.o
lpc17xx_timer.o
lpc17xx_clkpwr.o
lpc17xx_gpdma.o
lpc17xx_pwm.o
lpc17xx_spi.o
lpc17xx_uart.o
lpc17xx_libcfg_default.o
system_LPC17xx.o
startup_LPC17xx.o

 

Then I copied LedBlinky.c to a new dir and used this makefile:

PATH:=$(PATH):/usr/local/arm-cortex-codesourcery/bin
BIN=blink
OBJECTS=blink.o
LIBS=-lgcc -lc -lcs3 -lcs3unhosted -lcs3arm -llpc1768
GCFLAGS = -mcpu=cortex-m3 -mthumb -msoft-float -O0 -Wall -I. -I./include/ -I/usr/local/lpc1768/include
LDFLAGS = -mcpu=cortex-m3 -mthumb -msoft-float -O0 -Wl,-Map=$(BIN).map -T /usr/local/lpc1768/lpc1768.ld -L/usr/local/lpc1768/ $(LIBS)

GCC = arm-none-eabi-gcc
AS = arm-none-eabi-as
LD = arm-none-eabi-ld
STRIP = arm-none-eabi-strip -s
OBJCOPY = arm-none-eabi-objcopy
REMOVE = rm -f
INSTALL= cp $(BIN).bin /media/MBED/ && sync
SIZE = arm-none-eabi-size

all:: $(BIN).hex $(BIN).bin

$(BIN).bin: $(BIN).elf
	$(STRIP) $(BIN).elf
	$(OBJCOPY) -O binary -j .text -j .data $(BIN).elf $(BIN).bin

$(BIN).hex: $(BIN).elf
	$(OBJCOPY) -R .stack -O ihex $(BIN).elf $(BIN).hex

$(BIN).elf: $(OBJECTS)
	$(GCC) $(OBJECTS) $(LDFLAGS) -o $(BIN).elf

stats: $(BIN).elf
	$(SIZE) $(BIN).elf

clean:
	$(REMOVE) $(OBJECTS)
	$(REMOVE) $(BIN).hex
	$(REMOVE) $(BIN).elf
	$(REMOVE) $(BIN).map
	$(REMOVE) $(BIN).bin

install:
	$(INSTALL)

.c.o :
	$(GCC) $(GCFLAGS) -c $<

 

Now I just type make && make install and that's it :D also the blink.bin is just 4.3k compared to the fat HelloWorld.bin (22.9k !!) this is great :)

 

Next, I tried compiling my own toolchain, the end result didn't go very well, but here are the details anyway maybe you could see something I'm missing, I found a script floating around and I modified it after reading the elua tutorial but I also made some mods on my own don't know if maybe I missed up something:

 

#!/bin/sh
# Written by Uwe Hermann , released as public domain.

TARGET=arm-none-eabi          # Or: TARGET=arm-none-eabi
PREFIX=/usr/local/arm-cortex  # Install location of your final toolchain
PARALLEL="-j 2"               # Or: PARALLEL=""

BINUTILS=binutils-2.20.1
GCC=gcc-4.4.1
NEWLIB=newlib-1.17.0


export PATH="$PATH:$PREFIX/bin"

mkdir build

tar xfvj $BINUTILS.tar.bz2 
cd build
../$BINUTILS/configure --target=$TARGET --prefix=$PREFIX --with-gnu-as --with-gnu-ld --disable-nls
make $PARALLEL
make install
cd ..
rm -rf build/* $BINUTILS

tar xfvj $GCC.tar.bz2 
cd build
../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-languages="c" --with-newlib --without-headers --disable-shared --with-gnu-ld --with-gnu-as --disable-nls --with-cpu=cortex-m3 --with-mode=thumb --disable-multilib
make $PARALLEL all-gcc
make install-gcc
cd ..
rm -rf build/*

tar xfvz $NEWLIB.tar.gz
cd build
../$NEWLIB/configure --target=$TARGET --prefix=$PREFIX --disable-nls --with-gnu-ld --with-gnu-as --disable-newlib-supplied-syscall --disable-shared
make $PARALLEL CFLAGS_FOR_TARGET="-mcpu=cortex-m3 -mthumb -D__thumb2__" CCASFLAGS="-mcpu=cortex-m3 -mthumb -D__thumb2__"
make install
cd ..
rm -rf build/* $NEWLIB

# Yes, you need to build gcc again!
cd build
../$GCC/configure --target=$TARGET --prefix=$PREFIX --enable-languages="c" --with-newlib --disable-shared --with-gnu-ld --with-gnu-as --disable-nls --with-cpu=cortex-m3 --with-mode=thumb --disable-multilib
make $PARALLEL
make install
cd ..
rm -rf build/* $GCC

 

 

I used it to build the toolchain successfully, but I kept getting this warning when building the nxp library, note that march=<NULL>

 

warning: switch -mcpu=cortex-m3 conflicts with -march= switch

 

Anyway, moving on, when it tried to link I got an undefined reference to 'end' in _sbrk, after some research it seems that this symbol should point to the start of the first free word in ram after all the global variables so that sbrk in newlib can use it to allocate dynamic memory,  so I added this symbol to the linker script, the _end and __end sort of gave me a hint to where I needed to add end :D

 

145   .bss :
146   {
147     . = ALIGN (8);
148     __bss_start__ = .;
149     *(.shbss)
150     *(.bss .bss.* .gnu.linkonce.b.*)
151     *(COMMON)
152     __bss_end__ = .;
153     *(.ram.b)
154     . = ALIGN (8);
155     end = .;
156     _end = .;
157     __end = .;
158   } >ram AT>rom

 

 

Finally it compiled and I copied the binary but it doesn't blink :( ...

Well at least I have the CodeSourcery toolchain, I think something's wrong with newlib, it should call main eventually maybe it doesn't ? do I need to implement sbrk or change something ? anyway, I will keep the toolchain around, maybe I will give it another shot later when I know better.

 

P.S. If you want the library, headers and example just let me know how to share them.

19 Aug 2010

Thanks, Ibrahim. And thanks to you and Igor for the book suggestions. I need to do some reading.

02 Dec 2010

Hello

I am trying to do basically the same thing, use an offline compiler for the mbed.

I have a question or two:

when the online compiler is used, what linker script is use to build the final executable.

does the code that implements the mass storage run on the LPC1768 and therefore we need to link our executable in a special way, or that chip on the back impements the mass storage and loading the binary to flash.

can somebody shed some light on this.

I would like to use GCC with mbed,

Thanks

Ali

 

02 Dec 2010

The link script is here. The mass storage drive is managed by the interface chip ("that chip on the back"), so you just need to produce a plain binary suitable for flashing into LPC1768.

02 Dec 2010

Thanks Igor for the reply.

And this encourages me to ask for more help.

I think that format of linker you provided the URL for is for ARMCC toolchain, right?

can you provide some explanation of what it means so I will try to modify a linker script for GCC.

Thanks

02 Dec 2010

Ali,

The NXP library comes with a linker script for gcc, if you want to write your own this may help,  there are also some info about LPC1768 and CM3 in my blog

02 Dec 2010

Thank you Ibrahim

so, did you manage to run any program based on the NXP library built with GCC on the mbed?

02 Dec 2010

Yes, I use all the time, I can't say I've tested everything but so far so good, however, I still use the online compiler sometimes when trying something new...

03 Dec 2010 . Edited: 03 Dec 2010

I had two examples from the NXP code running on mbed. After fixing some mixed forward/backward slashes for paths and case sensitivity for included header files.

Feels good not being dependent on the online compiler.

Thank you for your help

Ali

04 Dec 2010 . Edited: 04 Dec 2010

I've also been down this route -- and have it all working reliably (it took quite a bit of researching of link scripts, etc.).  My code and scripts (heavily commented!) are now available at http://speleotrove.com/tollos/ -- the link scripts, in particular, cover all the 17xx devices, which might be helpful.

[By the way, anyone know the 'right' way to get live links into posts?  The 'link' icon is greyed out for me, and it seems fairly random whether a link turns blue or not, after various edits.]

Mike