LPC17XX CAN Bus Sample Code

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC17XX CAN Bus Sample Code

13,574 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rtos on Sun Feb 21 15:35:20 MST 2010
Is there an application note or sample code that show how to use the CAN bus on the LPC17XX chips and LPCXpresso IDE?  Something similar to the CAN_FULL or CAN_ALL demo program for the LPC2129?

Thanks.

Original Attachment has been moved to: 1100134_Console%20Output.txt.zip

Original Attachment has been moved to: 1100134_Console%20Output.txt.zip

Original Attachment has been moved to: 1100134_Console%20Output.txt.zip

Original Attachment has been moved to: 1100134_Console%20Output.txt.zip

Original Attachment has been moved to: 1100134_Console%20Output.txt.zip

Original Attachment has been moved to: 1100134_Console%20Output.txt.zip

0 Kudos
26 Replies

7,673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Jul 24 03:10:11 MST 2012
#1 Since this project is RTOS I would strongly suggest to test this before without RTOS :rolleyes:

#2 I still don't know which hardware you are using :confused:

#3 I would strongly recommend to write to CMR register in Reset mode (as done in CAN_Init)

#4 You are not sending with SRR (Self Reception Request) bit, so you can't expect local self test :mad:


Status CAN_SendMsg (LPC_CAN_TypeDef *CANx, CAN_MSG_Type *CAN_Msg)
....
CANx->CMR = 0x21;
...
CANx->CMR = 0x41;
...
CANx->CMR = 0x81;
So change that to:
Status CAN_SendMsg (LPC_CAN_TypeDef *CANx, CAN_MSG_Type *CAN_Msg)
....
CANx->CMR = 0x30;
...
CANx->CMR = 0x50;
...
CANx->CMR = 0x90;
#5 I'm missing pin function settings :confused:
// CAN1
// P0.0~1, function 0x01, Pin46 RxD1, Pin47 TxD1
 LPC_PINCON->PINSEL0 &=~0x0F;        //clear P0.0~1
 LPC_PINCON->PINSEL0 |= 0x05;        //set b0101

// CAN2:
// P0.4~5, function 0x01, Pin81 RxD2, Pin80 TxD2
 LPC_PINCON->PINSEL0 &=~(0x0F<<8);    //clear P0.4~5
 LPC_PINCON->PINSEL0 |= (0x0A<<8);    //set b1010
0 Kudos

7,673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mustafa on Tue Jul 24 02:02:02 MST 2012
thank you for your reply.
this is my project. I used can self test example and lpc1768 microcontroller. I only deleted debug codes. except that every thing is the same. I measured CAN1 port P0.0(RX) and P0.1(TX)
0 Kudos

6,721 Views
Mithunv
Contributor I

Please http://http//www.2shared.com/file/jnNEk7xY/canTest.html upload this canTest file , i am not able to access , whenever i try to send a can message in lpc1768 my controller is going in RESET mode.

i.e LPC_CAN1->MOD is getting set to 1.

0 Kudos

7,673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Sat Jul 21 01:27:03 MST 2012
Without details it's a guessing game :confused:

#1 Post your project :)

#2 Describe your hardware (own hardware (schematic) or dev. board) ;)

#3 Describe which pins you've measured :rolleyes:
0 Kudos

7,673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by mustafa on Sat Jul 21 00:02:12 MST 2012
hello,

I downloaded this examples and tried CAN bus self test example. It doen't work.
I can't see transmit wave  at the transmit pin and the CAN controller Tx pin. What can i do to fix the problem.

thank you.
0 Kudos

7,673 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Fri Feb 04 08:23:26 MST 2011
When I created that CAN project, I only look for issues related to build the CAN code. Thus it is possible, for example, that you need to set some different defines to those used for the CAN project for the timer and systick code.

However, there is a systick example for the LPCXpresso1768/69 board in the supplied example LPCXpresso1768.zip. More info at:

http://support.code-red-tech.com/CodeRedWiki/SuppliedExamples

And there is a timer example provided in the RDB1768cmsis examples which could be used as the basis of code to run on the LPCXpresso1768/69 board - the RDB1768 board has 4 LEDS rather than the 1 that LPCXpresso board has.

The above systick and timer code may be a better basis for testing out these peripherals than the driver library.

Regards,
CodeRedSupport
0 Kudos

7,672 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fpg on Fri Feb 04 04:18:24 MST 2011

Quote: CodeRedSupport

Assuming it does execute correctly, you should be able to create projects for the other CMSIS driver library CAN examples from this project. To do this, create a new project by copying this one (as per http://support.code-red-tech.com/CodeRedWiki/CopyingExampleProjects), then copy the example code into the 'src' to replace the existing code. You will need to keep the main.c, and comment out the main() function from the example (which just call centry() ).



It works just as described with certain examples, but not with all of them. I succesfully built 2 CAN related projects, but with timer and systick timer examples I get the same errors as with my own code before:

Description    Resource    Path    Location    Type
make: *** [pertest2.axf] Error 1    pertest2        line 0    C/C++ Problem
undefined reference to `TIM_ClearIntPending'    timer_poll_match.c    /pertest2/src    line 117    C/C++ Problem
undefined reference to `TIM_Cmd'    timer_poll_match.c    /pertest2/src    line 112    C/C++ Problem
undefined reference to `TIM_ConfigMatch'    timer_poll_match.c    /pertest2/src    line 111    C/C++ Problem
undefined reference to `TIM_GetIntStatus'    timer_poll_match.c    /pertest2/src    line 116    C/C++ Problem
undefined reference to `TIM_Init'    timer_poll_match.c    /pertest2/src    line 110    C/C++ Problem
0 Kudos

7,677 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Feb 03 08:45:54 MST 2011
I cannot comment as to why there is no formal version of the CMSIS driver library for LPCXpresso users - this is one for NXP.

However I have just produced a very quick project containing the CAN LedControl example from the CMSIS driver library. You can download this from:

http://support.code-red-tech.com/CodeRedWiki/NXPDriverLibraries

This builds, but as I have no suitable hardware I cannot tell you if it correctly executes or not.

Assuming it does execute correctly, you should be able to create projects for the other CMSIS driver library CAN examples from this project. To do this, create a new project by copying this one (as per http://support.code-red-tech.com/CodeRedWiki/CopyingExampleProjects), then copy the example code into the 'src' to replace the existing code. You will need to keep the main.c, and comment out the main() function from the example (which just call centry() ).

Regards,
CodeRedSupport
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by fpg on Thu Feb 03 07:35:55 MST 2011

Quote: CodeRedSupport
I have created a very simple 'port' of the library into an LPCXpresso/Red Suite [B]Makefile[/B] project at:
http://support.code-red-tech.com/CodeRedWiki/NXPDriverLibraries

A Makefile project is a project where the makefile is supplied by the developer and not automatically generated by LPCXpresso/Red Suite.



I can not import that as a library or as a project.
I can build it as a (cmsis) project with the .c and .h files imported, but as soon as I try to _use_ one of the functions I get errors.
If I create a static library and import the .c and .h files I can not even build it.

Could you please write some [U]proper[/U] instructions explaining how to use CMSIS Peripherial Driver Library? It had a great role that I chose to purchase lpc1768 and LPCXpresso IDE and now I cannot use it...

Or if it is [I]that[/I] simple why don't you just upload it as a static library like CMSIS?
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Dec 09 01:12:32 MST 2010
[U]1. CAN Baud Rate[/U]

Quote:
I hope the clock speed in the Keil example matches the clock speed on the LPCXpresso 1768 board. This way the baud rates for CAN will be corect.


If you use Project Wizard to create a new LPCXpresso CMSIS C-Project, your PCLK_CAN1 & PCLK_CAN2 in PCLKSEL0 should be 0, so CAN is running with CCLK/4 = 25MHz.
So you can calculate your BRP (for 100kHz baudrate) with (25E6 / 10 / baudrate ) -1 = (25E6 /10 /100E3) -1 = 24

Together with SJW / TESEG / SAM setting (0x0025C000) a valid BTR is

LPC_CAN1->BTR = 0x0025C000 + 24;

Of course there a are a lot of other settings possible.

If you need more information, read C_CAN User's Manual from BOSCH:

http://www.datasheetarchive.com/C_CAN-datasheet.html

[U]2. Local Self Test[/U]

To avoid problems with wrong baud rates or other hardware problems, it's a good idea to use 'Local Self Test' (UM10360 Chapter16).
You just need a working CAN transceiver but no other active node (ACK isn't expected in Local Self Test Mode).

If you also transmit messages in 'Self Reception Request', transmitted messages are received again and the debugger can be used to show transmitted and received data.

To use Local Self Test change CAN_Init:
LPC_CAN1->MOD = LPC_CAN2->MOD = 0x00; //CAN in normal operation mode

to
LPC_CAN1->MOD = LPC_CAN2->MOD = (1<<2); //local self test

To use 'Self Reception Request' change CAN1_SendMessage:
LPC_CAN1->CMR = 0x21;
...
LPC_CAN1->CMR = 0x41;
...
LPC_CAN1->CMR = 0x81;

to
LPC_CAN1->CMR = 0x30;
...
LPC_CAN1->CMR = 0x50;
...
LPC_CAN1->CMR = 0x90;
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rtos on Wed Dec 08 18:09:23 MST 2010
I have an Mbed and really do like how fast and easy you can get a project up and running.  I really think the Mbed is a great idea and great for certain projects including quick CAN projects.   I've done several quick CAN projects with the Mbed and highly highly recommend the Mbed to anyone. 

The guys that support the Mbed forum seem to be top notch guys that answer a lot of questions.

I'd rather use the Mbed as a fast prototype device and then spend my  time really understanding the LPC1768 and release the final project with  LPCXPresso.  I think this is one of the intents of the Mbed project.

Having said this, the Mbed has some drawback in my opinion...

1) Presently the only compiler is an "on-line" compiler that resides on their servers/website.  This means that you must put your source code on their website and be connected to the Internet in order to compile your programs.  While this is amazingly convenient in many ways the big drawback for me is that if your source code is proprietary then you may not be able to put your code on their servers.

2) The low level drivers (CAN and other peripherals) are closed source. I really have no idea what their underlying code is doing.  I do not like to be reliant on closed source drivers.  Just my personal opinion.

3) I need access to some of the LP1768 interrupts.  It is difficult to use these interrupts on Mbed and you don't always get what you expect due to interactions with the underlying Mbed libraries and peripheral drivers.  I really don't understand what is going on at the low level.
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by igorsk on Wed Dec 08 07:04:08 MST 2010
mbed has its own class which implements CAN and it's closed-source.
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by elm on Wed Dec 08 03:24:56 MST 2010
If you heard of the Mbed board. They are also providing some code about CAN.
On this page.
http://mbed.org/handbook/CAN
But I didn't look deeper in it and I am not sure if you can use it the way as they are doing. Just give it a look.

regards
Florian
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rtos on Tue Dec 07 21:02:25 MST 2010
Downloading Keil now.  Will give it a try.  I hope the clock speed in the Keil example matches the clock speed on the LPCXpresso 1768 board.  This way the baud rates for CAN will be corect.
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Dec 07 20:51:35 MST 2010
It's part of

https://www.keil.com/demo/eval/arm.htm

See Keil\ARM\Boards\Keil\MCB1700\CAN

or download

http://ics.nxp.com/support/documents/microcontrollers/zip/code.bundle.lpc17xx.keil.zip
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rtos on Tue Dec 07 20:11:26 MST 2010

Quote: Zero
AFAIR there's a good Keil LPC1768 sample. Adding it's 2 CAN files (can.c & can.h) should work.



Where is the example?
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Dec 07 19:43:35 MST 2010
AFAIR there's a good Keil LPC1768 sample. Adding it's 2 CAN files (can.c & can.h) should work.
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rtos on Tue Dec 07 19:04:01 MST 2010

Quote: CodeRedSupport
I have created a very simple 'port' of the library into an LPCXpresso/Red Suite [B]Makefile[/B] project at:
http://support.code-red-tech.com/CodeRedWiki/NXPDriverLibraries

A Makefile project is a project where the makefile is supplied by the developer and not automatically generated by LPCXpresso/Red Suite.



I realize this post if from a while ago but I never did get this working and am now giving it another shot.  I imported your makefile port to LPCXpresso and when I try to build the CAN project it tries to build all of the projects and I get the following errors in the makefile in the main root folder which looks like it is associated with the CMSIS library?  Any ideas what is wrong here?

**** Build of configuration Debug for project lpc17xx.cmsis.driver.library ****

make libs 
makefile:45: C:/Users/Dewrek/Documents/lpcxpresso_3.4/workspace/lpc17xx.cmsis.driver.library\makesection\makerule\LPC17xx\make.LPC17xx.gnu: No such file or directory
make: *** No rule to make target `C:/Users/Dewrek/Documents/lpcxpresso_3.4/workspace/lpc17xx.cmsis.driver.library\makesection\makerule\LPC17xx\make.LPC17xx.gnu'.  Stop.
This is the line in the makefile that gives the error:
include $(PROJ_ROOT)\makesection\makerule\$(DEVICE)\make.$(DEVICE).$(TOOL)
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Polux rsv on Tue Aug 10 02:52:28 MST 2010

Quote: Mark_O
Angelo wrote:
> [I]The major problem with the CAN controllers, also present in previous chips as LPC2368, is when you want to use both CAN1 and CAN2 controllers. The user manual states CAN1 and CAN2 should use the same CCLK frequency.[/I] <

I must have missed that requirement. Could you point out a reference for me? All I noted was that "[1]PCLK_CAN1 and PCLK_CAN2 must have the same PCLK divide value when the CAN function is used." in Chapter 4, Section 7.3, Table 40.

Thanks.

- Mark



That's what I was talking about. But the manual didn't tell the PCLK_ACF [U][B]MUST[/B][/U] be the same than PCLK_CAN1 and PCLK_CAN2. Otherwise you have random behaviour of can controllers, not doing what software tells them to do. I lost several hours to find that. :mad:
0 Kudos

7,678 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Mark_O on Tue Aug 10 02:01:22 MST 2010
Angelo wrote:
> [I]The major problem with the CAN controllers, also present in previous  chips as LPC2368, is when you want to use both CAN1 and CAN2  controllers. The user manual states CAN1 and CAN2 should use the same  CCLK frequency.[/I] <

I must have missed that requirement.  Could you point out a reference for me?  All I noted was that "[1]PCLK_CAN1 and PCLK_CAN2 must have the same PCLK divide value when the CAN function is used." in Chapter 4, Section 7.3, Table 40.

Thanks.

- Mark
0 Kudos