Atmel website | ARM Community | AVR freaks | Technical Support
Banner
 FAQ •  Search •  Register •  Login 

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: CAN questions
PostPosted: Fri Mar 14, 2008 10:15 am 
Offline

Joined: Fri Mar 14, 2008 10:00 am
Posts: 7
Hi, I am trying to get a grasp on this CAN thing (that is so hard to websearch since "can" must be one of the most common words on te web...)

So, I have some questions:

1. Using SAM7A3 is it a lot of work getting CAN to work? Are the ATMEL libs doing anything real here, or would I probably need to buy a lib if I am unexperienced with CAN and dont want to spend many days/weeks? I have managed to get I2C going fairly quick but will be buying a USB HOST stack and FAT file system for example...

2. Is CAN any good by itself, or will I most likely need CANopen or simillar higher lever stuff? I will use CAN to connect between several control boxes of our own, and possibly third party measuring devices....

3. If I need such a higher level, is there any clear winner? Preferrably free and available for FreeRTOS (and IAR) or easy to port to FreeRTOS.

4. Will the SAM7A3 need any external stuff to actually drive a CAN bus at the right levels, to protect it etc? Any suggestions for such components?

5. Will CAN etc steal a lot of ROM/RAM and processor power?

6. Is CAN a good choice even for fast communications?

Rgds
PM


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 12:09 pm 
Offline

Joined: Fri Jan 25, 2008 11:25 am
Posts: 10
6. Fast communication? hmmm the maximal Baudrate is 1MBaud and a Frame has a 44 Bit "Overhead"
You can send 8 Bytes Data in one Frame so you get a 108 Bit Frame.

I think that is ok, fast enough for the most things.

CAN is on the "lowlevel". With the SAM7A3 u configure a Messagebox and when no data is on the bus it will be send. Or you configure a receive messagebox and if a other busdevice send a telegramm this messagebox received it.

i think the greate advantage on CAN is, that every telegram has a Identifer. With this Identifier u differ the busdevices.

With the AtmelLibs u can create the Messageobjekts to receive or send Data and only have to handle the interrupt.

if you only want to send some data frames u need no highlayerprotocol such CANopen or something.

the can controller try to send the data if the bus is empty. the telegram with the lowest identifier have the highest priority.

hmmm this is a good link for canopen and can basics (but it is in german) http://frenzel-berg.de/download/datashe ... uide_d.pdf

To Implement only CAN its easy:
You have to configure the baudrate (or automatic detection) and then u can create message boxes to send or receive data.

u can configure messageboxes to receive only 1 Identifier or a Mask of Identifiers.
So u can configure your device only to hear on the Telegrams with his Identifier and they have no work with other Telegrams.

if you want to connect to third party measuring devices u need to know how it communicate. which identifier they use or if they use a higherlayer protocol.

i haven't seen a free canopen stack or something but i didn't search a lot.

the basic CAN use a minimal of ressources. you have to activet the can controller, then u have to create messageboxes and then you have to handle the data from this boxes. if you use can with 1 MBaud you can get every 44µs an interrupt from a telegram in the worst case.

Here you have a presentation for the can basics: http://www.at91.com/repFichier/Document ... %20CAN.ppt

This is good to get an CAN overview. You can read there that it have a good Errordetection and all the other informations.

I use CANopen in the Industrial Communication. On http://www.can-cia.org/ you can also see information about CAN and CANopen and other Highlayer CAN Protocolls.

Here you can download the CANopen Specification: http://www.can-cia.org/index.php?id=440

CiA 301 DS V4.0.2: CANopen application layer and communication profile
IGCO_301_v04000201.pdf

describes the CANopen Communication and there u can read which CANIdentifier used for what by the CANopen Protocoll.

But if u use only your own Devices u can "develop" your own Protocol.
In the Automobilebranch the use there own protocols. BMW use KWP and Merceds and others use CCP (CAN Calibration Protocol).

if u work with CANopen you have a good Errordetection if some devices doesn't answer or the busmaster get offline.

http://www.vector-worldwide.com/ sales canopen Stacks.

And here ( http://www.can-cia.org/pg/can/ ) you can see products for can (canopen and so on).
If u work with canyo u need a CANalyser to test your Device and send Data or Receive Data from the Bus. I use it from PEAK its cheap and good. (http://www.peak-system.com/?gclid=CP-ow ... uwodBicD-Q) a USB CAN Adapter and the CAN Explorer to see whats going on on my CANBus.




have a lot of fun with CAN :)

Rgds
Shutty


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 6:00 pm 
Offline

Joined: Mon May 02, 2005 12:17 pm
Posts: 137
Location: Germany
Hi PM,

just my opinion to your questions:

1. If you managed to get TWI working, CAN shouldn't be a problem. Of course you have to understand some CAN basics, but using the AT91SAM CAN controller isn't too complicated. USB or FAT are more difficult to implement. The code below is a simple X256 sample, which shows that CAN isn't too difficult if you understand the registers.

2. CAN is good by itself (if you use it for things, it was developed to do). To transmit data from one device to another it's a good choice. Car manufacturer use CAN to transmit a lot of data via CAN. So if it's good for them, why shouldn't it be good for us? If you don't have to use special protocol, you can just transmit/receive data via a simple timer (that's the way I started with X256).

3. Don't know. Always used my own stuff.

4. Of course you need a CAN transceiver. I like the TJA1050 for high speed CAN and TJA1054 for low speed fault tolerant CAN. This Philips (now NXP) parts are used in cars and therefore not very vulnerable. There are a lot of other transceivers available which are also able to work with a supply voltage of 3.3V. I use a SN65HVD231D (3.3V SOIC8) without any problems. Connect it to your SAM, add a termination resistor and that's all.

5. No. With 'Full CAN' you are able to filter/mask messages. So the CAN controller hardware let you receive just the messages you want to receive.

6. Don't know what's fast for you. To transmit data from a few devices 10-50 times per second it should be fast enough.

Code:
//simple CAN with X256

#define CAN_BR_500K      0x00053255
#define CAN_BR_250K      0x000B3255
#define CAN_BR_200K      0x000E3255
#define CAN_BR_150K      0x00133255
#define CAN_BR_125K      0x00173255
#define CAN_BR_100K      0x001D3255
#define CAN_BR_83K       0x00233255
#define CAN_BR_75K       0x00273255
#define CAN_BR_50K       0x003B3255
#define CAN_BR_25K       0x00773255

typedef struct s_can_message         //can message structure
{
unsigned int    RID;               //received ID
unsigned char  RDL;               //received data length
unsigned long   RML,RMH;            //received data
} can_message;


can_message mess[4];
unsigned char   f_new1;

void can_init(void)
{
//enable Pins
AT91C_BASE_PIOA->PIO_ASR = AT91C_PA19_CANRX|AT91C_PA20_CANTX;
AT91C_BASE_PIOA->PIO_PDR = AT91C_PA19_CANRX|AT91C_PA20_CANTX;
//enable Clock
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_CAN;
//init AIC-CAN
AT91C_BASE_AIC->AIC_IDCR = 1 << AT91C_ID_CAN;
//disable Reception Mailbox 0 interrupt
AT91C_BASE_CAN->CAN_IDR = AT91C_CAN_MB0;
AT91C_BASE_AIC->AIC_SMR[AT91C_ID_CAN] = 0x7;      //set prior
AT91C_BASE_AIC->AIC_SVR[AT91C_ID_CAN] = (unsigned int) CANIrqHandler;
AT91C_BASE_AIC->AIC_IECR = 1 << AT91C_ID_CAN;      //disable
AT91C_BASE_AIC->AIC_ICCR = 1 << AT91C_ID_CAN;      //enable
//set baudrate
//BRP = ((1 / (CAN_baudrate *16bit))* 48MHz) -1
//sample: 500kbaud @48MHz ((1/(500000*16))*48MHz)-1 =5  =>CAN_BR = 0x053255;
//sample: 100kbaud @48MHz ((1/(100000*16))*48MHz)-1 =29 =>CAN_BR = 0x1D3255;
AT91C_BASE_CAN->CAN_BR = CAN_BR_100K; //set baudrate
//enable Error Ints
AT91C_BASE_CAN->CAN_IER = AT91C_CAN_AERR | AT91C_CAN_CERR;//
// Configure Mailbox 0, RX
AT91C_BASE_CAN_MB0->CAN_MB_MMR    = AT91C_CAN_MOT_RXOVERWRITE;
AT91C_BASE_CAN_MB0->CAN_MB_MAM    = 0x000 << 18;   //reset acceptance
AT91C_BASE_CAN_MB0->CAN_MB_MID    = 0x000 << 18;   //reset filter
//mailbox 1 transmission low byte first
AT91C_BASE_CAN_MB1->CAN_MB_MID=0x55C<<18;   //11 bit Part A ID
AT91C_BASE_CAN_MB1->CAN_MB_MAM=0x00000000;   //accept all   
AT91C_BASE_CAN_MB1->CAN_MB_MDH=0x87654321;   //data high
AT91C_BASE_CAN_MB1->CAN_MB_MDL=0x00000000;   //data low
AT91C_BASE_CAN_MB1->CAN_MB_MMR=AT91C_CAN_MOT_TX;
// Enable Reception Mailbox 0 interrupt
AT91C_BASE_CAN->CAN_IER = AT91C_CAN_MB0;
// Clear and trigger Transfer
AT91C_BASE_CAN->CAN_TCR = AT91C_CAN_MB0;
AT91C_BASE_CAN->CAN_MB0.CAN_MB_MCR = AT91C_CAN_MTCR;
// Enable CAN
AT91C_BASE_CAN->CAN_MR = AT91C_CAN_CANEN; //| AT91C_CAN_ABM
}


//CAN_TX-----------------------------------------------------------------CAN_TX
void can_tx(unsigned char DLC)
{

if(AT91C_BASE_CAN->CAN_SR & AT91C_CAN_MB1)       //mailbox ready
{
  //AT91C_BASE_CAN->CAN_IER = AT91C_CAN_MB1;         //enable int
  AT91C_BASE_CAN->CAN_MB1.CAN_MB_MCR =(DLC<<16)|AT91C_CAN_MTCR;   //send 8bytes
  //can_status();
  while(!(AT91C_BASE_CAN_MB1->CAN_MB_MSR & AT91C_CAN_MRDY)){} //low while trx
}                     //end mailbox ready
}


//CAN_INT---------------------------------------------------------------CAN_INT
void CANIrqHandler (void)         //CAN INT
{
status = AT91C_BASE_CAN->CAN_SR;   //reset int
status &= AT91C_BASE_CAN->CAN_IMR;   //check for enabled ints
//MB0 INT
if(status &  AT91C_CAN_MB0)      //read message in mess[0]
{
  AT91C_BASE_CAN->CAN_TCR = AT91C_CAN_MB0; 
  //get received ID
  mess[0].RID =(unsigned int)((AT91C_BASE_CAN->CAN_MB0.CAN_MB_MID >> 18))& 0x07FF; 
  mess[0].RDL =(unsigned char)((AT91C_BASE_CAN->CAN_MB0.CAN_MB_MSR >>16) & 0x0F);
  mess[0].RML =AT91C_BASE_CAN->CAN_MB0.CAN_MB_MDL;
  mess[0].RMH =AT91C_BASE_CAN->CAN_MB0.CAN_MB_MDH;
  f_new1 =1;                                    //set flag
  if  ((AT91C_BASE_PIOA->PIO_ODSR & LED1) == LED1)
   AT91C_BASE_PIOA->PIO_CODR = LED1;                     // turn LED2 on   
  else
   AT91C_BASE_PIOA->PIO_SODR = LED1;                     // turn LED2 off 
}
}

//set and send message
  AT91C_BASE_CAN_MB1->CAN_MB_MID=0x55C<<18;   //11 bit Part A ID
  AT91C_BASE_CAN_MB1->CAN_MB_MDH=0x87654321;   //data high
  AT91C_BASE_CAN_MB1->CAN_MB_MDL=0x00000004;   //data low
  can_tx(8);                           //and go....

HTH,
Gerd


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 9:32 pm 
Offline

Joined: Fri Mar 14, 2008 10:00 am
Posts: 7
Great answers! Thanks! (it really IS a problem to serach for CAN stuff, even here it is not opssible to search for CAN in the forum!)

If I understand Gerd right, I should have CAN running in just a few hours?

Will have to get back and check what we need to connect to more than our own boxes, and what we need to transmit then....CANopen etc still seems an open question....

I also welcome more answers and opinions!

Rgds
PM


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 14, 2008 10:01 pm 
Offline

Joined: Mon May 02, 2005 12:17 pm
Posts: 137
Location: Germany
Hi again,

I spent one weekend to get CAN running. But I worked with CAN before (AT90CAN128) and therefore the CAN stuff was no surprise to me. So 'a few hours' is perhaps a little bit optimistic, a few days more realistic.

HTH,
Gerd


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 10:01 am 
Offline

Joined: Fri Mar 14, 2008 10:00 am
Posts: 7
More hardware:

Are there any different standards for the CAN-tranciever, ie signal levels etc, or is that all 100% clear standard?

Rgds
PM


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 10:23 am 
Offline

Joined: Mon May 02, 2005 12:17 pm
Posts: 137
Location: Germany
Hi,

ISO 11898 is automotive standard for high speed (see TJA1050 datasheet) or low speed (see TJA1054 datasheet). TJA1050 is a full differential bus transceiver , TJA1054 is a fault tolerant bus transceiver (with failure detector). If you don't need failure detection just use TJA1050 or other high speed transceiver.

HTH,
Gerd


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 11:00 am 
Offline

Joined: Fri Mar 14, 2008 10:00 am
Posts: 7
Thanks Gerd!

And are there only those two standards to choose from when designing the physical CAN bus?

(we are designing a black box that will interface to yet unknown customers boxes, we are prepared that we have to implement different high-level protocols like CAN-open etc, but it would be really nice if we could choose a CAN-tranceiver that can handle "everything", or at least select the most widely used standard, or at the very least be aware that we might have to redsign the CAN-tranciever PCB for different customers...)

Are you aware of different levels of physical ruggedness in the CAN-driver chips on the market? Are they sensitive to short-circuit, high-voltage, static, EMC etc etc, and are there any specially sturdy ones out there?

Rgds
PM


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 12:10 pm 
Offline

Joined: Fri Mar 14, 2008 10:00 am
Posts: 7
Did some more research on my own, and found a good site:

http://www.kvaser.com/can/protocol/index.htm

So I guess that answers a lot, your opinions are still welcome though, since its hard to know which of all standards are really in use etc....

And actual chip recommendations for the Transceiver.... is NXP the clear choice, and why?

/PM


Top
 Profile  
 
 Post subject: What maximal cable length /less than 25 kbit/s
PostPosted: Mon Mar 17, 2008 6:07 pm 
Offline

Joined: Mon Mar 17, 2008 5:48 pm
Posts: 9
As far as I can see, the lowest Bitrate is 25 kbit/s !?? what means less than 3000 m cable length.

If I have to do connections to stations that are 10000 m away; is there a possibility to decrease baudrate (as BRP only has 7 bit:( ) by changing SJW, PROPAG, PHASE1 or PHASE2 or by using any other clock rate divider to solve the long distance problem?


Top
 Profile  
 
 Post subject: Re: CAN questions
PostPosted: Tue Mar 29, 2011 5:59 am 
Offline

Joined: Tue Mar 29, 2011 5:42 am
Posts: 1
I need to get the nIRQ (ARM CORE) and AIC initialization and CAN Init to work on a new board but I have uMon and MicroCross 4.2 that I am writing in because I like that for development a lot.

So far, compiling the EK examples for it are weird so I have been hand-porting sections of code from Atmel to the uMon setup.

uMon allows me to dynamically add the nIRQ handler, but I am bit fuzzy on the flow and what the complete nIRQ handler should look like (for example, should it write to the AIC_EOICR register (and should it be 1 or 8 times) when booting up?

If anyone is familiar with this lower level stuff that is done way before the CAN_init() I could use some help. I can pay reasonable for some help if someone like Gerd has some time to help me out.

Thanks,
Steve Shireman
Kansas City, MO USA
816-668-2886


Top
 Profile  
 
 Post subject: Re: CAN questions
PostPosted: Wed Mar 30, 2011 8:51 pm 
Offline

Joined: Mon Mar 28, 2011 7:10 pm
Posts: 6
Hello

I have some interesting CAN tutorials - I will post them on at91.com when I get them updated. Now part is here: http://www.keil.com/download/docs/385.asp

However, if you want this material now - email me at bob.boys@arm.com and i will send them to you. I have a new PPT that explains some things that I can send.

I intend to create these docs using ATMEL processors.

Bob
California


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: Bing [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: