Skip to content

Commit b07cc6b

Browse files
committed
Update examples.
1 parent 130c26e commit b07cc6b

File tree

2 files changed

+49
-63
lines changed

2 files changed

+49
-63
lines changed

examples/Seeed-Studio-CAN_BUS_Shield/Seeed-Studio-CAN_BUS_Shield.ino

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ static bool isActive = false;
4444
// If no message is received return false, otherwise true.
4545
bool transportRead(vscp_RxMessage * const rxMsg) {
4646

47-
bool status = false;
47+
bool status = false;
4848

4949
if (CAN_MSGAVAIL == canCom.checkReceive())
5050
{
51-
unsigned long canMsgId = 0;
51+
unsigned long canMsgId = 0;
5252

53-
if (CAN_OK == canCom.readMsgBufID(&canMsgId, &rxMsg->dataSize, rxMsg->data)) {
53+
if (CAN_OK == canCom.readMsgBuf(&canMsgId, &rxMsg->dataSize, rxMsg->data)) {
5454

5555
rxMsg->vscpClass = (uint16_t)((canMsgId >> 16) & 0x01ff);
5656
rxMsg->vscpType = (uint8_t)((canMsgId >> 8) & 0x00ff);
@@ -88,7 +88,7 @@ bool transportWrite(vscp_TxMessage const * const txMsg) {
8888
}
8989

9090
// Send CAN message
91-
if (CAN_OK != canCom.sendMsgBuf(canMsgId, 1, 0, txMsg->dataSize, (unsigned char*)txMsg->data)) {
91+
if (CAN_OK != canCom.sendMsgBuf(canMsgId, 1, txMsg->dataSize, (unsigned char*)txMsg->data)) {
9292

9393
// CAN message couldn't be sent, try again.
9494
++retryCnt;
@@ -158,6 +158,9 @@ void setup() {
158158
} else {
159159

160160
Serial.println("CAN controller initialized successful.");
161+
162+
// Change to normal mode to allow messages to be transmitted
163+
canCom.setMode(MCP_NORMAL);
161164

162165
// Only CAN frames with 29-bit identifier shall be received
163166
canCom.init_Mask(0, 1, 0x1fffffff);

examples/Sparkfun_CAN-BUS_Shield/Sparkfun_CAN-BUS_Shield.ino

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,40 @@
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
3232
VSCP 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
3540
static 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.
3944
bool 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?
4853
if (true == canMsg.isExtendedAdrs) {
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.
8085
bool 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, 10)) {
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

Comments
 (0)