lpcware

Error in AN11238: LPC11Cxx CANopen network demo

Discussion created by lpcware Employee on Jun 15, 2016
Content originally posted in LPCWare by R2D2 on Sat Feb 28 15:21:23 MST 2015
AN11238: LPC11Cxx CANopen network demo

http://www.lpcware.com/content/nxpfile/an11238-lpc11cxx-canopen-network-demo-0

is including a frame length calculation error in void CAN_RX() of canopen_driver.c  :((

/* if end of buffer has been reached, then this is the last frame */
if(CANopen_SDOC_InBuff == CANopen_SDOC_Seg_BuffSize)
{
CANopen_Msg_Obj.data[0] |= ([color=#f00](7-(CANopen_SDOC_Seg_BuffSize%7)[/color])<<1) | 0x01; /* save length */
CANopen_SDOC_State = CANopen_SDOC_Succes;/* set state to success */
}

This part should calculate n: indicates the number of bytes that do not contain data (bytes 8-n to 7 do not contain data):

n = 7-(CANopen_SDOC_Seg_BuffSize%7)

That's working correct for (CANopen_SDOC_Seg_BuffSize%7)  > 0.

[color=#30f]Sample #1:[/color] CANopen_SDOC_Seg_BuffSize = 72

For segmented transfer there are 11 frames necessary, the last frame is containing 2 data bytes:

n = 7-(CANopen_SDOC_Seg_BuffSize%7) = 7-2 = 5

So the last 5 data bytes are not containing data, that's correct.
[color=#30f]
Sample #2:
[/color] CANopen_SDOC_Seg_BuffSize = 70

For segmented transfer there are 10 frames necessary, the last frame is containing 7 data bytes:

n = 7-(CANopen_SDOC_Seg_BuffSize%7) = 7-0 = 7

So the last 7 data bytes are not containing data, that's wrong, they are all including data  :O

[color=#0c0]Of course the simple solution is change n from 7 to 0   :)
[/color]
/* if end of buffer has been reached, then this is the last frame */
if(CANopen_SDOC_InBuff == CANopen_SDOC_Seg_BuffSize)
{
i=  7-(CANopen_SDOC_Seg_BuffSize%7);//bytes in last frame
[color=#f00] if(i==7)i=0;[/color]//set all data bytes
CANopen_Msg_Obj.data[0] |= (i<<1) | 0x01;/* save length */
CANopen_SDOC_State = CANopen_SDOC_Succes;/* set state to success */
}


Note: With this error data of the last frame (if it's a full frame / data = 7) are ignored in segmented transfer and SDO buffer isn't filled completely...

Outcomes