LPC2138 another try on freertos

Started by Giuseppe Marullo December 2, 2009
Hi all,
I am going to try again to port the freertos for this combination:

- LPC2138 with just one uart (uart1) readily available (there is a fpga
between uart1 and the serial port configured to forward data to the pc).
Uart 0 pins are available and can be connected to the fpga as well but
not both at the same time.
- 24MHz fixed clock (slave from a FX2 chip), I would like to reach 48MHz
with peripheral clock ad full speed
- Eclipse/Yagarto/OpenOCD on Windows
- JTAG emulated over USB (fx2 based)
- Two leds on P0_31 and P0_30
- no other peripherals connected
- pins reachable from the FPGA are:
P0_2 to P0_19, P0_23

I started with this:
http://www.freertos.org/portlpc2368_Eclipse.html

I did some modifications about the PLL and such, I removed/commented
most of the stuff I don't need and I was able to compile it. I have just
the led routines setup for the LEDs. I got some problems due to missing
the FAST I/O and CCLKDIV that I don't think are present in the LPC2138,
I hope to have done the needed modification.

Now, if I try to link with a lpc2138.ld file that I have, it starts
complaining about .startup sections and such. Some labels are just named
differently (I tried to change/insert accordingly) but
I hope they are just about "standard" files that somewhere I could grab
and use without modification for my combination (boot.s .ld and
makefile) that are freertos friendly.

I don't see any suitable example in the demos how could I fix this?
TIA,

Giuseppe Marullo

In freeRTOSConfig.h
- put the correct include
- I have the clock frequency:
#define configCPU_CLOCK_HZ ( ( unsigned long ) 48000000 )

In main.c
- I have disabled any reference to SCS, I don't find it in LPC2138:
//SCS |= mainOSC_ENABLE;
//while( !( SCS & mainOSC_STAT ) );
//CLKSRCSEL = mainOSC_SELECT;

I have changed the PLL settings, to get M=2 and P=2. This is to achieve
the maximum speed with the 24MHz clock (M=2) and to have Fcco within limits.

#define mainPLL_MUL ( ( unsigned portLONG ) ( 1 ) ) // M of
2 equal to MSEL of 00001 [4:0] 0x0001
#define mainPLL_DIV ( ( unsigned portLONG ) 0x0002 ) // P of
2 equal to PSEL of 10 [6:5] 0x0020

I think CCLKCFG is not used in LPC2138, I will use ABPDIV istead:
VPBDIV = 0x0001;

An Engineer's Guide to the LPC2100 Series

I assume you've seen JC Wren's port to the 2148 for FreeRTOS?
http://jcwren.com/arm/
You'll have to pull out the USB stuff and re-map your IO for your board but it should be a *better* start.

--- In l..., Giuseppe Marullo wrote:
>
> Hi all,
> I am going to try again to port the freertos for this combination:
>
> - LPC2138 with just one uart (uart1) readily available (there is a fpga
> between uart1 and the serial port configured to forward data to the pc).
> Uart 0 pins are available and can be connected to the fpga as well but
> not both at the same time.
> - 24MHz fixed clock (slave from a FX2 chip), I would like to reach 48MHz
> with peripheral clock ad full speed
> - Eclipse/Yagarto/OpenOCD on Windows
> - JTAG emulated over USB (fx2 based)
> - Two leds on P0_31 and P0_30
> - no other peripherals connected
> - pins reachable from the FPGA are:
> P0_2 to P0_19, P0_23
>
> I started with this:
> http://www.freertos.org/portlpc2368_Eclipse.html
>
> I did some modifications about the PLL and such, I removed/commented
> most of the stuff I don't need and I was able to compile it. I have just
> the led routines setup for the LEDs. I got some problems due to missing
> the FAST I/O and CCLKDIV that I don't think are present in the LPC2138,
> I hope to have done the needed modification.
>
> Now, if I try to link with a lpc2138.ld file that I have, it starts
> complaining about .startup sections and such. Some labels are just named
> differently (I tried to change/insert accordingly) but
> I hope they are just about "standard" files that somewhere I could grab
> and use without modification for my combination (boot.s .ld and
> makefile) that are freertos friendly.
>
> I don't see any suitable example in the demos how could I fix this?
> TIA,
>
> Giuseppe Marullo
>
> In freeRTOSConfig.h
> - put the correct include
> - I have the clock frequency:
> #define configCPU_CLOCK_HZ ( ( unsigned long ) 48000000 )
>
> In main.c
> - I have disabled any reference to SCS, I don't find it in LPC2138:
> //SCS |= mainOSC_ENABLE;
> //while( !( SCS & mainOSC_STAT ) );
> //CLKSRCSEL = mainOSC_SELECT;
>
> I have changed the PLL settings, to get M=2 and P=2. This is to achieve
> the maximum speed with the 24MHz clock (M=2) and to have Fcco within limits.
>
> #define mainPLL_MUL ( ( unsigned portLONG ) ( 1 ) ) // M of
> 2 equal to MSEL of 00001 [4:0] 0x0001
> #define mainPLL_DIV ( ( unsigned portLONG ) 0x0002 ) // P of
> 2 equal to PSEL of 10 [6:5] 0x0020
>
> I think CCLKCFG is not used in LPC2138, I will use ABPDIV istead:
> VPBDIV = 0x0001;
>

First of all,
many thanks for your answer. I probably tried to peek into most of the
demo around, but for a reason or another I was never able to complete
the porting. I am not into the C thing, more oriented to Delphi so
makefiles and assembler parts are really alien to me. Not to mention
Eclipse and its quirks, really cyclopic development tool.

I have a board with a Spartan 3E on board plus a LPC2138, so the sum of
the two could make it as a very effective robotic board. I have a very
peculiar setup I guess, since the fpga and the ARM are managed by USB
for a lot of functions, power, fpga configuration, high speed data
exchange, jtag programming and so on.

Having said that, I did what you said, started with LPC2418 demo and did
what I think is the correct modifications, still no luck, it does not work.

This is the places where I touched things:

On the left side, the modified stuff, on the right the original.

http://www.xar.it/download/porting001.jpg

This is what I changed in cpu.c (clock is fixed@24MHz, I wanted to reach
48MHz):

http://www.xar.it/download/porting002.jpg

This is what I changed in leds.c:

http://www.xar.it/download/porting003.jpg

I don't have fast I/O on LPC2138 (I think), so I tried to adapt it,
based on a demo I got with the Xylo-lm board. BTW, I had to change the
definition for
IODIR0, IOCLRO and IOSET0.
The ones I had from the Xylo demo gave me syntax error, so I changed
them (leds.c:

http://www.xar.it/download/porting004.jpg

I have just changed the include specific to my mcu in FreeRTOSConfig.h:

http://www.xar.it/download/porting005.jpg

This is how I changed makefile.winarm:

http://www.xar.it/download/porting006.jpg

and

http://www.xar.it/download/porting007.jpg

This is how I stripped down main.c to do just the LED thing...

http://www.xar.it/download/porting008.jpg

I got a nice .bin, followed instructions to program the flash (as I do
from telnet on OpenOCD):

halt
mww 0xE01FC040 1
flash erase_check 0
flash info 0
flash erase 0 0 26
arm7_9 dcc_downloads enable
flash write 0 c:\temp\lpc2148\lpc2148_demo\lpc2148.bin 0
power cycle

...

well, it does not lit. So now, what I have to do? A bounty?

Could some good soul tell me what I have done wrong? Simply too much
variables for me.
TIA,

Giuseppe Marullo
--- In l..., Giuseppe Marullo wrote:
>
> First of all,
> many thanks for your answer. I probably tried to peek into most of the
> demo around, but for a reason or another I was never able to complete
> the porting. I am not into the C thing, more oriented to Delphi so
> makefiles and assembler parts are really alien to me. Not to mention
> Eclipse and its quirks, really cyclopic development tool.
>
> I have a board with a Spartan 3E on board plus a LPC2138, so the sum of
> the two could make it as a very effective robotic board. I have a very
> peculiar setup I guess, since the fpga and the ARM are managed by USB
> for a lot of functions, power, fpga configuration, high speed data
> exchange, jtag programming and so on.
>
> Having said that, I did what you said, started with LPC2418 demo and did
> what I think is the correct modifications, still no luck, it does not work.
>
Marullo
I think you might be better off to modify the LPC2106-GCC sample that comes with FreeRTOS. The thing about JC's code is that it controls EVERYTHING. It is terrific sample code but it is too extensive for a first project - especially if you have to modify it.

The 2106 demo just blinks an LED and tests a few of the internal FreeRTOS functions. From a hardware point of view, it only requires one serial port and one LED.

The 2106 has a different memory map than the 2138 but that should be easy to change in the linker script.

I took the equates from rom_arm.bat and added them to the top of Makefile like:

USE_THUMB=NO
DEBUGOPTIM=-O3
RUN_MODE=RUN_FROM_ROM
LDSCRIPT=lpc2106_rom.ld

Under Windows, these would be added to the environment by running the .bat file.

In any evet, it built immediately, I used lpc21isp to download it and minicom (115200 baud) to test it. It works fine.

Richard

Richard,
I have tried this one too in the past. Today I was able to compile it
and got something working. What and why is working is still not clear to
me..

1) I am using UART1 now, I changed the way the FPGA connects to the ARM.
I just have the tx pin for now.

2) I changed the usual bunch of stuff, and now I have one led flashing
(0x40000000). The UART0 is spitting a sequence of chars A..X (not Z)
@115200 probably there is some error in the stack definition or memory
map...I don't know

The clock is 24MHz, and I set M=2 P=1. I don't know if this is the right
setting, but it works for 115200. What puzzles me is that Fcco < 156MHz,
but as long I get 115200 I will solve this later.

In freertos.h I changed
#define configCPU_CLOCK_HZ ( ( unsigned long ) 48000000 )
/* $MHz osc (fx2) multiplied by 2 using the PLL. */

In main.c I changed:

SCB_PLLCFG = 0x20; // P=2 [01] and M=1 [00000]

I was expecting to use 0x21 (M=2,P02) , but apparently it goes 230k.

Do you know what I could have set wrong for the serial port having this
behavior?

Thanks for you time,

Giuseppe Marullo
Serial output:
...
MNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLM
NOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTU
...

rtstofer wrote:
> --- In l..., Giuseppe Marullo wrote:
>
>> First of all,
>> many thanks for your answer. I probably tried to peek into most of the
>> demo around, but for a reason or another I was never able to complete
>> the porting. I am not into the C thing, more oriented to Delphi so
>> makefiles and assembler parts are really alien to me. Not to mention
>> Eclipse and its quirks, really cyclopic development tool.
>>
>> I have a board with a Spartan 3E on board plus a LPC2138, so the sum of
>> the two could make it as a very effective robotic board. I have a very
>> peculiar setup I guess, since the fpga and the ARM are managed by USB
>> for a lot of functions, power, fpga configuration, high speed data
>> exchange, jtag programming and so on.
>>
>> Having said that, I did what you said, started with LPC2418 demo and did
>> what I think is the correct modifications, still no luck, it does not work.
>>
>>
> Marullo
> I think you might be better off to modify the LPC2106-GCC sample that comes with FreeRTOS. The thing about JC's code is that it controls EVERYTHING. It is terrific sample code but it is too extensive for a first project - especially if you have to modify it.
>
> The 2106 demo just blinks an LED and tests a few of the internal FreeRTOS functions. From a hardware point of view, it only requires one serial port and one LED.
>
> The 2106 has a different memory map than the 2138 but that should be easy to change in the linker script.
>
> I took the equates from rom_arm.bat and added them to the top of Makefile like:
>
> USE_THUMB=NO
> DEBUG> OPTIM=-O3
> RUN_MODE=RUN_FROM_ROM
> LDSCRIPT=lpc2106_rom.ld
>
> Under Windows, these would be added to the environment by running the .bat file.
>
> In any evet, it built immediately, I used lpc21isp to download it and minicom (115200 baud) to test it. It works fine.
>
> Richard
>
>
--- In l..., Giuseppe Marullo wrote:
>
> Richard,
> I have tried this one too in the past. Today I was able to compile it
> and got something working. What and why is working is still not clear to
> me..
>
> 1) I am using UART1 now, I changed the way the FPGA connects to the ARM.
> I just have the tx pin for now.
>
> 2) I changed the usual bunch of stuff, and now I have one led flashing
> (0x40000000). The UART0 is spitting a sequence of chars A..X (not Z)
> @115200 probably there is some error in the stack definition or memory
> map...I don't know
>
> The clock is 24MHz, and I set M=2 P=1. I don't know if this is the right
> setting, but it works for 115200. What puzzles me is that Fcco < 156MHz,
> but as long I get 115200 I will solve this later.
>
> In freertos.h I changed
> #define configCPU_CLOCK_HZ ( ( unsigned long ) 48000000 )
> /* $MHz osc (fx2) multiplied by 2 using the PLL. */
>
> In main.c I changed:
>
> SCB_PLLCFG = 0x20; // P=2 [01] and M=1 [00000]
>
> I was expecting to use 0x21 (M=2,P02) , but apparently it goes 230k.
>
> Do you know what I could have set wrong for the serial port having this
> behavior?
>
> Thanks for you time,
>
> Giuseppe Marullo
> Serial output:
> ...
> MNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLM
> NOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTU
The only part I can get into right now is the serial output. If you look at comtest.c (in the common folder), you will see that it only sends A..X

I'm not sure which demo you started with. The LPC2106-GCC or the LPC2148-ROWLEY?

Remember, you calculate M but you write M-1 to the register.

Richard

Hi,

The LPC PLL block is an analog block that needs to be operated at certain frequency to guarantee its frequency stability. For LPC2138, it needs to be in 156Mhz-320Mhz. Otherwise, even though it's running, its frequency may be jittered (shifted up or down during the system operation).

For your case:
CCLK = your CPU core speed = Fosc x M -> Suggested to put it higher to achieve better performance, or lower to conserve power
Fcco = the PLL clock freq = CCLK x 2 x P -> Must be in the range of 156Mhz - 320Mhz
UART clock = PCLK = CCLK / APBDIV -> Read the LPC2138 user manual

UART baudrate = UART clock / (16 x DLAB-in-16-bits-value)

Since the last eq may not satisfy the minimum UART error rate, you can set the fractional divider. Read the LPC2138 User Manual for the details.

Regards,
-daniel

From: l... [mailto:l...] On Behalf Of Giuseppe Marullo
Sent: Tuesday, December 15, 2009 9:22 AM
To: l...
Subject: Re: [lpc2000] Re: LPC2138 another try on freertos


Richard,
I have tried this one too in the past. Today I was able to compile it
and got something working. What and why is working is still not clear to
me..

1) I am using UART1 now, I changed the way the FPGA connects to the ARM.
I just have the tx pin for now.

2) I changed the usual bunch of stuff, and now I have one led flashing
(0x40000000). The UART0 is spitting a sequence of chars A..X (not Z)
@115200 probably there is some error in the stack definition or memory
map...I don't know

The clock is 24MHz, and I set M=2 P=1. I don't know if this is the right
setting, but it works for 115200. What puzzles me is that Fcco < 156MHz,
but as long I get 115200 I will solve this later.

In freertos.h I changed
#define configCPU_CLOCK_HZ ( ( unsigned long ) 48000000 )
/* =24MHz osc (fx2) multiplied by 2 using the PLL. */

In main.c I changed:

SCB_PLLCFG = 0x20; // P=2 [01] and M=1 [00000]

I was expecting to use 0x21 (M=2,P02) , but apparently it goes 230k.

Do you know what I could have set wrong for the serial port having this
behavior?

Thanks for you time,

Giuseppe Marullo

Serial output:
...
MNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLM
NOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTU
...

To take a 12 MHz crystal up to 60 MHz (full speed) for the 2148:

static void feed(void);

/**********************************************************
Initialize
**********************************************************/

#define PLOCK (1 << 10)

void PLL_init(void) {

// Setting Multiplier and Divider values
PLL0CFG = 0x24; // P=2, M=5
feed();

// Enabling the PLL */
PLL0CON = 0x1;
feed();

// Wait for the PLL to lock to set frequency
while(!(PLL0STAT & PLOCK))
;

// Connect the PLL as the clock source
PLL0CON = 0x3;
feed();

// Enabling MAM and setting number of clocks used for Flash memory
fetch (3 cclks in this case)
MAMTIM = 0x03;
MAMCR = 0x02;
//MEMMAP = 0x01;

// Setting peripheral Clock (pclk) to 1/4 System Clock (cclk)
VPBDIV = 0x00;
}
void feed(void)
{
PLL0FEED=0xAA;
PLL0FEED=0x55;
}
To set the UART0 for 115200,n,8,1:

// we are assuming a PCLK of CCLK (60 MHz) / 4 or 15 MHz

// set UART0 for 115200,n,8,1

void UART0_init(void)
{
PINSEL0 |= 0x05; // enable UART0 pins
U0LCR = 0x83; // 8,n,1 and turn on DLAB
U0DLM = 0x00;
U0DLL = 0x05; // divide PCLK by 5
U0FDR = (8 << 4) + 5; // multiply by 8, divide by 8 + 5
U0LCR &= ~(0x80); // turn off DLAB
U0FCR = 0x07; // reset and enable FIFO, RX Trigger level 0 - 1 char
}

Since the peripheral clock is 15 MHz, you need to see how the tick timer is set up. I don't have the FreeRTOS code on this machine. And I'm not sure whether you started with the 2106 version or the 2148.

Richard

>The only part I can get into right now is the serial output. If you look at comtest.c (in the common folder), you will >see that it only sends A..X
So does the output make sense? Great. I was expecting something more "meaningful".

>I'm not sure which demo you started with. The LPC2106-GCC or the LPC2148-ROWLEY?
This is the LPC2106 GCC demo.

>Remember, you calculate M but you write M-1 to the register.
More on this later, I am in hurry.

Thanks for now,

Giuseppe

Hi Daniel and Martin and others,
thanks for all your help.

I have 24MHz of clock (Fosc), AFAIK the maximum supported by LPC2138 is
60MHz. So 48MHz (desired CCLK) should be the maximum frequency, right?

CCLK = M x Fosc or
CCLK = Fcco / (2 x P)

So M = 2

PO /(CCLKx2)

P can vary between 156e6/(CCLKx2) = 1.625 and 320e6/CCLKx2)= 3.333, so
the only selectable P must be 2.

Value for PCON [6:5/4:0]
[10 0001] = 0x21

APBDIV is set for Same clock, so:

SCB_VPBDIV = mainBUS_CLK_FULL;

Now, the question:

why this:

#define mainCOM_TEST_BAUD_RATE ( ( unsigned long ) 115200 )

Gets me with 230k baud instead of 115k? Surely it is my fault, simply I
don't understand why.

Other questions:

- I would like to use the UART1, is there a way to compile the demo
and/or tell the console is UART1 instead of UART0? I remember there was
a option in the makefile on the LPC2148 demo CFG_CONSOLE_UART1 but is it
just for it or it is a "feature" of freertos?

- is it possible to use printf instead of sprintf and have its output on
serial? Or, any printf substitute that could output using the printf
formatting ?

- Is it possible to debug using gdb/insight a freertos program? I tried
but probably due to ISR or something it does not start at main,c I
succesfully debug Xylo-LM examples both in ram and flash from Eclipse
and Insight

Thanks,

Giuseppe Marullo
>Since the last eq may not satisfy the minimum UART error rate, you can set the fractional divider. Read the LPC2138 User Manual for the details.
This does not apply here, sorry. Apart for error mine is a LPC2138 that does not have fractional divider (as LPC2138/01), or there is something I don't know.

Daniel Widyanto wrote:
> Hi,
>
> The LPC PLL block is an analog block that needs to be operated at certain frequency to guarantee its frequency stability. For LPC2138, it needs to be in 156Mhz-320Mhz. Otherwise, even though it's running, its frequency may be jittered (shifted up or down during the system operation).
>
> For your case:
> CCLK = your CPU core speed = Fosc x M -> Suggested to put it higher to achieve better performance, or lower to conserve power
> Fcco = the PLL clock freq = CCLK x 2 x P -> Must be in the range of 156Mhz - 320Mhz
> UART clock = PCLK = CCLK / APBDIV -> Read the LPC2138 user manual
>
> UART baudrate = UART clock / (16 x DLAB-in-16-bits-value)
>
> Since the last eq may not satisfy the minimum UART error rate, you can set the fractional divider. Read the LPC2138 User Manual for the details.
>
> Regards,
> -daniel
>
> From: l... [mailto:l...] On Behalf Of Giuseppe Marullo
> Sent: Tuesday, December 15, 2009 9:22 AM
> To: l...
> Subject: Re: [lpc2000] Re: LPC2138 another try on freertos
>
>
> Richard,
> I have tried this one too in the past. Today I was able to compile it
> and got something working. What and why is working is still not clear to
> me..
>
> 1) I am using UART1 now, I changed the way the FPGA connects to the ARM.
> I just have the tx pin for now.
>
> 2) I changed the usual bunch of stuff, and now I have one led flashing
> (0x40000000). The UART0 is spitting a sequence of chars A..X (not Z)
> @115200 probably there is some error in the stack definition or memory
> map...I don't know
>
> The clock is 24MHz, and I set M=2 P=1. I don't know if this is the right
> setting, but it works for 115200. What puzzles me is that Fcco < 156MHz,
> but as long I get 115200 I will solve this later.
>
> In freertos.h I changed
> #define configCPU_CLOCK_HZ ( ( unsigned long ) 48000000 )
> /* $MHz osc (fx2) multiplied by 2 using the PLL. */
>
> In main.c I changed:
>
> SCB_PLLCFG = 0x20; // P=2 [01] and M=1 [00000]
>
> I was expecting to use 0x21 (M=2,P02) , but apparently it goes 230k.
>
> Do you know what I could have set wrong for the serial port having this
> behavior?
>
> Thanks for you time,
>
> Giuseppe Marullo
>
> Serial output:
> ...
> MNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLM
> NOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTUVWXABCDEFGHIJKLMNOPQRSTU
> ...
>