Simplified CANopen library for a CANopen slave device

Dependents:   SDC21XX_Motor SDC21XX_Motor

Committer:
kkoichy
Date:
Mon May 30 11:28:35 2016 +0000
Revision:
0:0f99b1d63054
Child:
1:a5e0bd1f1c93
V1.0; First Upload; Class CANopen created, allows to send command through CAN bus using CANopen protocol; 2 instructions created : Initiate SDO Down- & Upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kkoichy 0:0f99b1d63054 1 #include "CANopen.h"
kkoichy 0:0f99b1d63054 2
kkoichy 0:0f99b1d63054 3
kkoichy 0:0f99b1d63054 4 namespace mbed {
kkoichy 0:0f99b1d63054 5
kkoichy 0:0f99b1d63054 6 CANopen::CANopen(short _id, CAN * _can)
kkoichy 0:0f99b1d63054 7 {
kkoichy 0:0f99b1d63054 8 can = _can;
kkoichy 0:0f99b1d63054 9 node_id = _id;
kkoichy 0:0f99b1d63054 10
kkoichy 0:0f99b1d63054 11 }
kkoichy 0:0f99b1d63054 12 CANopen::CANopen(short _id, CAN * _can, int _baud)
kkoichy 0:0f99b1d63054 13 {
kkoichy 0:0f99b1d63054 14 can = _can;
kkoichy 0:0f99b1d63054 15 node_id = _id;
kkoichy 0:0f99b1d63054 16 can->frequency(_baud);
kkoichy 0:0f99b1d63054 17
kkoichy 0:0f99b1d63054 18 }
kkoichy 0:0f99b1d63054 19 int CANopen::Send_Initiate_SDO_Download(short index, short subindex, long data)
kkoichy 0:0f99b1d63054 20 {
kkoichy 0:0f99b1d63054 21 char _data[8];
kkoichy 0:0f99b1d63054 22 _data[0] = 0x22;
kkoichy 0:0f99b1d63054 23 _data[1] = index;
kkoichy 0:0f99b1d63054 24 _data[2] = index >> 8;
kkoichy 0:0f99b1d63054 25 _data[3] = subindex;
kkoichy 0:0f99b1d63054 26 _data[4] = data >> 0;
kkoichy 0:0f99b1d63054 27 _data[5] = data >> 8;
kkoichy 0:0f99b1d63054 28 _data[6] = data >> 16;
kkoichy 0:0f99b1d63054 29 _data[7] = data >> 24;
kkoichy 0:0f99b1d63054 30 return can->write(CANMessage(0x600 + node_id, _data, 8));
kkoichy 0:0f99b1d63054 31
kkoichy 0:0f99b1d63054 32 }
kkoichy 0:0f99b1d63054 33 int32_t CANopen::Send_Initiate_SDO_Upload(short index, short subindex)
kkoichy 0:0f99b1d63054 34 {
kkoichy 0:0f99b1d63054 35 CANMessage _message;
kkoichy 0:0f99b1d63054 36 char _data[8];
kkoichy 0:0f99b1d63054 37 _data[0] = 0x60;
kkoichy 0:0f99b1d63054 38 _data[1] = index;
kkoichy 0:0f99b1d63054 39 _data[2] = index >> 8;
kkoichy 0:0f99b1d63054 40 _data[3] = subindex;
kkoichy 0:0f99b1d63054 41 _data[4] = 0;
kkoichy 0:0f99b1d63054 42 _data[5] = 0;
kkoichy 0:0f99b1d63054 43 _data[6] = 0;
kkoichy 0:0f99b1d63054 44 _data[7] = 0;
kkoichy 0:0f99b1d63054 45 &_message.data = _data;
kkoichy 0:0f99b1d63054 46 message.len = 8;
kkoichy 0:0f99b1d63054 47 message.id = 0x600 + node_id;
kkoichy 0:0f99b1d63054 48 can->write(_message);
kkoichy 0:0f99b1d63054 49 can->read(&_message);
kkoichy 0:0f99b1d63054 50 return (_message.data[4] | _message.data[5] << 8 | _message.data[6] << 16 | _message.data[7] << 24);
kkoichy 0:0f99b1d63054 51
kkoichy 0:0f99b1d63054 52 }
kkoichy 0:0f99b1d63054 53 short CANopen::GetNodeID(void)
kkoichy 0:0f99b1d63054 54 {
kkoichy 0:0f99b1d63054 55 return node_id;
kkoichy 0:0f99b1d63054 56
kkoichy 0:0f99b1d63054 57 }
kkoichy 0:0f99b1d63054 58 void CANopen::SetNodeID(short _id)
kkoichy 0:0f99b1d63054 59 {
kkoichy 0:0f99b1d63054 60 node_id = _id;
kkoichy 0:0f99b1d63054 61
kkoichy 0:0f99b1d63054 62 }
kkoichy 0:0f99b1d63054 63
kkoichy 0:0f99b1d63054 64
kkoichy 0:0f99b1d63054 65
kkoichy 0:0f99b1d63054 66
kkoichy 0:0f99b1d63054 67 }//end namespace