11/* The MIT License (MIT)
22 *
3- * Copyright (c) 2014 - 2020 , Andreas Merkle
3+ * Copyright (c) 2014 - 2024 , Andreas Merkle
44 * http://www.blue-andi.de
55 * vscp@blue-andi.de
66 *
2626
2727#include < VSCP.h> // VSCP framework
2828#include < SPI.h> // SPI used for CAN controller communication
29- #include < MCP2515 .h> // CAN controller driver
29+ #include < mcp2515 .h> // CAN controller driver
3030
3131// Create an instance of the VSCP framework
3232VSCP vscp;
3333
34+ // Create an instance of the CAN controller driver
35+ MCP2515 mcp2515 (
36+ 9 // Set CS (chip select) pin, note if you use a CAN BUS shield prior to V1.1 use pin 10!
37+ );
38+
3439// Node is in active state or not
3540static bool isActive = false ;
3641
3742// Read a message from the transport layer, e.g. the CAN bus
3843// If no message is received return false, otherwise true.
3944bool transportRead (vscp_RxMessage * const rxMsg) {
4045
41- bool status = false ;
42- CANMSG canMsg;
46+ bool status = false ;
47+ struct can_frame canMsg;
4348
4449 // Any CAN frame received?
45- if (true == MCP2515::receiveCANMessage (&canMsg, 10 )) {
50+ if (MCP2515::ERROR_OK == mcp2515. readMessage (&canMsg)) {
4651
4752 // Is it a extended CAN frame?
48- if (true == canMsg.isExtendedAdrs ) {
53+ if (0 != (( canMsg.can_id >> 31 ) & 0x01 ) ) {
4954
5055 unsigned char index = 0 ;
5156
52- rxMsg->vscpClass = (uint16_t )((canMsg.adrsValue >> 16 ) & 0x01ff );
53- rxMsg->vscpType = (uint8_t )((canMsg.adrsValue >> 8 ) & 0x00ff );
54- rxMsg->oAddr = (uint8_t )((canMsg.adrsValue >> 0 ) & 0x00ff );
55- rxMsg->hardCoded = (uint8_t )((canMsg.adrsValue >> 25 ) & 0x0001 );
56- rxMsg->priority = (VSCP_PRIORITY)((canMsg.adrsValue >> 26 ) & 0x0007 );
57- rxMsg->dataSize = canMsg.dataLength ;
57+ rxMsg->vscpClass = (uint16_t )((canMsg.can_id >> 16 ) & 0x01ff );
58+ rxMsg->vscpType = (uint8_t )((canMsg.can_id >> 8 ) & 0x00ff );
59+ rxMsg->oAddr = (uint8_t )((canMsg.can_id >> 0 ) & 0x00ff );
60+ rxMsg->hardCoded = (uint8_t )((canMsg.can_id >> 25 ) & 0x0001 );
61+ rxMsg->priority = (VSCP_PRIORITY)((canMsg.can_id >> 26 ) & 0x0007 );
62+ rxMsg->dataSize = canMsg.can_dlc ;
5863
5964 // Protect against a buffer out of bounce access
6065 if (VSCP_L1_DATA_SIZE < rxMsg->dataSize ) {
@@ -79,24 +84,23 @@ bool transportRead(vscp_RxMessage * const rxMsg) {
7984// If it fails to send the message return false, otherwise true.
8085bool transportWrite (vscp_TxMessage const * const txMsg) {
8186
82- bool status = false ;
83- CANMSG canMsg;
84- unsigned char index = 0 ;
85- unsigned char retryCnt = 0 ;
86-
87- canMsg.isExtendedAdrs = true ;
88-
89- canMsg.adrsValue = (((uint32_t )txMsg->priority ) << 26 ) |
90- (((uint32_t )txMsg->hardCoded ) << 25 ) |
91- (((uint32_t )txMsg->vscpClass ) << 16 ) |
92- (((uint32_t )txMsg->vscpType ) << 8 ) |
93- txMsg->oAddr ;
94-
95- canMsg.rtr = 0 ;
96-
97- canMsg.dataLength = txMsg->dataSize ;
87+ bool status = false ;
88+ struct can_frame canMsg;
89+ unsigned char index = 0 ;
90+ unsigned char retryCnt = 0 ;
91+
92+ canMsg.can_id = (1 << 31 ) | // Extended 29 bit
93+ (0 << 30 ) | // No RTR
94+ (0 << 29 ) | // No error message frame
95+ (((uint32_t )txMsg->priority ) << 26 ) |
96+ (((uint32_t )txMsg->hardCoded ) << 25 ) |
97+ (((uint32_t )txMsg->vscpClass ) << 16 ) |
98+ (((uint32_t )txMsg->vscpType ) << 8 ) |
99+ txMsg->oAddr ;
100+
101+ canMsg.can_dlc = txMsg->dataSize ;
98102
99- for (index = 0 ; index < canMsg.dataLength ; ++index) {
103+ for (index = 0 ; index < canMsg.can_dlc ; ++index) {
100104
101105 canMsg.data [index] = txMsg->data [index];
102106 }
@@ -110,7 +114,7 @@ bool transportWrite(vscp_TxMessage const * const txMsg) {
110114 }
111115
112116 // Send CAN message
113- if (false == MCP2515::transmitCANMessage ( canMsg, 10 )) {
117+ if (MCP2515::ERROR_OK == mcp2515. sendMessage (& canMsg)) {
114118
115119 // CAN message couldn't be sent, try again.
116120 ++retryCnt;
@@ -121,7 +125,7 @@ bool transportWrite(vscp_TxMessage const * const txMsg) {
121125 }
122126
123127 } while ((false == status) && (0 < retryCnt));
124-
128+
125129 return status;
126130}
127131
@@ -152,37 +156,16 @@ void setup() {
152156 Serial.begin (115200 );
153157 Serial.println (" VSCP node starts up ..." );
154158
155- do {
156- // Initialize CAN controller with 125 kbit/s (VSCP default bitrate)
157- if (false == MCP2515::initCAN (CAN_BAUD_125K)) {
158-
159- // Try again
160- delay (100 );
161- --retry;
162-
163- if (0 == retry) {
164- isError = true ;
165- }
166-
167- } else {
168-
169- // Successful initialized
170- retry = 0 ;
171- }
172-
173- } while (0 < retry);
174-
175- if (true == isError) {
176-
177- Serial.println (" Failed to initialize CAN controller!" );
178-
159+ if (MCP2515::ERROR_OK != mcp2515.reset ()) {
160+ isError = true ;
179161 }
180- // Set to normal mode non single shot
181- else if (false == MCP2515::setCANNormalMode (LOW)) {
182-
183- Serial.println (" Failed to set CAN controller to normal mode!" );
184-
185- } else {
162+ else if (MCP2515::ERROR_OK != mcp2515.setBitrate (CAN_125KBPS)) {
163+ isError = true ;
164+ }
165+ else if (MCP2515::ERROR_OK != mcp2515.setNormalMode ()) {
166+ isError = true ;
167+ }
168+ else {
186169
187170 Serial.println (" CAN controller initialized successful." );
188171
0 commit comments