LPC11Cxx with CANopen?

ChibiOS public support forum for topics related to the NXP LPC family of Cortex-M micro-controllers.
User avatar
jcw
Posts: 55
Joined: Thu May 23, 2013 12:59 am
Location: Houten, NL

LPC11Cxx with CANopen?

Postby jcw » Sun Jun 02, 2013 12:23 am

I'm looking into using the LPC11C24's built-in CANopen ROMs with Chibios.

The LPC1114 demo from ChibiOS works. So does the CAN bus slave example for LPC11C24 from NPX in the LPCxpresso IDE environment.

My next thought was to try and get the code from App Note AN11238 to work under ChibiOS. Integration so far was fairly straightforward - copying a few source files into the LPC1114 demo and adding them to the compile/link steps in the Makefile. Here's is the main code I'm trying out:

Code: Select all

int main(void) {
  halInit();
  chSysInit();
  CANopenInit();

  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  ...


The CANopenInit() call is standard boilerplate, which I think ties into the ROM code. A quick browse indicates that it sets up the CAN device interrupt.

(I'm not using the ChibiOS CAN driver, because there is none for LPC a.t.m.)

Stepping through the first few lines, I can see that chThdCreateStatic() gets called but it never returns. The non-RTOS App Note code is as follows:

Code: Select all

int main(void)
{
   SystemInit();
   CANopenInit();

   /* Timer16B0 */
   LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);      /* Enable clock for 16-bit counter/timer 0 */
   LPC_TMR16B0->MCR = 3;                  /* Interrupt and Reset on MR0 */
   LPC_TMR16B0->MR0 = SystemCoreClock/1000;   /* Interrupt and Reset on MR0 */
    NVIC_EnableIRQ(TIMER_16_0_IRQn);         /* Enable the Timer0 Interrupt */
    LPC_TMR16B0->TCR = 1;                  /* Enable Timer0 */

    /*LEDArray */
    LPC_GPIO2->DIR |= 0x000000FF;            /* LEDs as output */

    LEDArray = 0x00;

   while(1)
   {
      __WFI();   /* Go to Sleep */
   }
   return 0 ;
}


SystemInit() seems to be standard LPC-speak for setting up the clocks. Then the same CANopenInit() call, then a timer which gets called to keep the CAN protocol / state machine going, and that's it.

One detail which might be messing things up is RAM memory use - from UM10398, p.313:

On-chip RAM from address 0x1000 0050 to 0x1000 00B8 is used by the CAN API. This address range should not be used by the application. For applications using the on-chip CAN API, the linker control file should be modified appropriately to prevent usage of this area for application’s variable storage.


I don't know whether this is relevant, and haven't yet figured out where to make such a change in the ChibiOS source tree...

Any suggestions as what could be going on, or how to narrow this down?

-jcw

UPDATE: moving the RAM usage up in LPC1114.ld (ram : org = 0x10000400, len = 7k) doesn't seem to make a difference, although the map indicates the RAM mappings have indeed shifted up.

theShed
Posts: 50
Joined: Tue Feb 26, 2013 3:43 pm
Location: The flatlands of East Anglia

Re: LPC11Cxx with CANopen

Postby theShed » Sun Jun 02, 2013 12:56 am

CAN support for the LPC is likely to happen in the near future (new job allowing). But to get you going...

The ram usage for an application is defined in the linker script file. You will find something like this:

# Define linker script file here
LDSCRIPT= $(PORTLD)/LPC1114.ld

- in your application makefile. Change the file that is referenced. I suggest someting like 'LPC11C24_CAN.ld' but its up to you...

copy this file:
os/ports/GCC/ARMCMx/LPC11xx/ld/LPC1114.ld

to whatever you have called your new linker script e.g
os/ports/GCC/ARMCMx/LPC11xx/ld/LPC11C24_CAN.ld

Now edit the new file and find the memory definition:

MEMORY
{
flash : org = 0x00000000, len = 32k
ram : org = 0x10000000, len = 8k
}

Unless you are short of RAM I'd play safe and block out all of the first 1k. Change the above to:

MEMORY
{
flash : org = 0x00000000, len = 32k
ram : org = 0x10000400, len = 7k
}

that should do it...

--
mike

User avatar
jcw
Posts: 55
Joined: Thu May 23, 2013 12:59 am
Location: Houten, NL

Re: LPC11Cxx with CANopen?

Postby jcw » Sun Jun 02, 2013 1:23 am

Thanks - yes, this confirms what I've been doing, I think. I did all that, and verified that the stack did indeed move up by 1 K:

Code: Select all

.stacks         0x0000000010000400      0x400


Doesn't seem to make a difference, alas.

I did make some progress in another direction: in CANopenInit(), the only line which causes things to fail is this one:

Code: Select all

NVIC_EnableIRQ(CAN_IRQn);


Commenting it out, things works as expected (nothing will happen on CAN, evidently). Threads are running fine. The CAN interrupt is defined as follows, i.e. it reflects the call back to ROM code:

Code: Select all

void CAN_IRQHandler (void)
{
  (*rom)->pCAND->isr();
}


When I comment the rom call out, things still fail - I suppose I need to clear the interrupt (... sooo many things I'm still learning on this embedded stuff, though I do know about systems programming and kernels on larger machines).

-jcw

marcinj7
Posts: 13
Joined: Wed Apr 17, 2013 10:08 am
Location: Poland

Re: LPC11Cxx with CANopen?

Postby marcinj7 » Sun Jun 02, 2013 10:31 am

Hi,

I think than the irq handler

Code: Select all

void CAN_IRQHandler (void)
{
  (*rom)->pCAND->isr();
}

is not connected with vector table.
Check file os/ports/GCC/ARMCMx/LPC11xx/vectors.c
Try this:

Code: Select all

CH_IRQ_HANDLER(Vector74)  {
 
  CH_IRQ_PROLOGUE();
  (*rom)->pCAND->isr();
  CH_IRQ_EPILOGUE();
}

User avatar
jcw
Posts: 55
Joined: Thu May 23, 2013 12:59 am
Location: Houten, NL

Re: LPC11Cxx with CANopen?

Postby jcw » Sun Jun 02, 2013 10:42 am

More progress - inside CANopenInit() (canopen_driver.c, line 90), if I switch from the default interrupt-service ...

(*rom)->pCAND->init_can(&ClkInitTable[0], 1);

... to polled service (see UM10398, p.314) ...

(*rom)->pCAND->init_can(&ClkInitTable[0], 0);

... then ChibiOS threads work.

I'll see if I can get actual CANopen communication to work in polled mode, but somewhere, somehow, the LPC11C24's built-in CANopen ROM code interrupt assumptions seem to be clashing with ChibiOS right now. I still know far too little about ARM and ChibiOS startup to be able to say whether it's a matter of interrupt vectoring setup or something else.

-jcw

User avatar
jcw
Posts: 55
Joined: Thu May 23, 2013 12:59 am
Location: Houten, NL

Re: LPC11Cxx with CANopen?

Postby jcw » Sun Jun 02, 2013 10:57 am

Hello marcinj7,

Aha... that does explain a lot.

Woohoo, it seems to work!
Onwards, and thank you!

-jcw

User avatar
jcw
Posts: 55
Joined: Thu May 23, 2013 12:59 am
Location: Houten, NL

Re: LPC11Cxx with CANopen?

Postby jcw » Sun Jun 02, 2013 5:51 pm

Yes! It works, I'm seeing CANopen NMT boot records being sent out, interrupt based and with a ChibiOS blink task running in parallel.

Thanks for all the help. Code will be on github later, but for now the conclusion is that this code needs to be inserted in main.c:

Code: Select all

extern ROM **rom; // ROM entrypoint for on-chip CAN drivers

CH_IRQ_HANDLER(Vector74)  {
  CH_IRQ_PROLOGUE();
  (*rom)->pCAND->isr();
  CH_IRQ_EPILOGUE();
}


That and a CANopenInit() call is enough to get the ball rolling, with LPC11C24's ROM code taking care of all the hard work.

-jcw

mr_michael_s
Posts: 13
Joined: Sat Jul 07, 2012 1:08 pm

Re: LPC11Cxx with CANopen?

Postby mr_michael_s » Wed Dec 25, 2013 10:11 pm

Has anybody improved the Integration of lpc11c24 meanwhile ?
I am currently starting to use Olimex LPC-P11c24 board with Chibios and want to utilize CAN Interface.

Thanks

Michael

sparkytwd
Posts: 1
Joined: Mon Jun 16, 2014 7:13 pm

Re: LPC11Cxx with CANopen?

Postby sparkytwd » Mon Jun 16, 2014 7:18 pm

I'm also interested if someone had sample code for the 11c24 using CAN.

User avatar
jcw
Posts: 55
Joined: Thu May 23, 2013 12:59 am
Location: Houten, NL

Re: LPC11Cxx with CANopen?

Postby jcw » Thu Aug 28, 2014 7:17 am



Return to “LPC Support”

Who is online

Users browsing this forum: No registered users and 7 guests