Seed can implementation
Fork of SEEED_CAN by
seeed_can_api.cpp@1:ad71faa09868, 2013-11-06 (annotated)
- Committer:
- Just4pLeisure
- Date:
- Wed Nov 06 20:16:11 2013 +0000
- Revision:
- 1:ad71faa09868
- Parent:
- 0:f5d099885d3d
- Child:
- 2:fd026fcfde94
Beta release of a CAN-BUS library for Seeed Studios' CAN BUS Shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Just4pLeisure | 0:f5d099885d3d | 1 | /* seeed_can_api.cpp |
Just4pLeisure | 0:f5d099885d3d | 2 | * Copyright (c) 2013 Sophie Dexter |
Just4pLeisure | 0:f5d099885d3d | 3 | * |
Just4pLeisure | 0:f5d099885d3d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Just4pLeisure | 0:f5d099885d3d | 5 | * you may not use this file except in compliance with the License. |
Just4pLeisure | 0:f5d099885d3d | 6 | * You may obtain a copy of the License at |
Just4pLeisure | 0:f5d099885d3d | 7 | * |
Just4pLeisure | 0:f5d099885d3d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Just4pLeisure | 0:f5d099885d3d | 9 | * |
Just4pLeisure | 0:f5d099885d3d | 10 | * Unless required by applicable law or agreed to in writing, software |
Just4pLeisure | 0:f5d099885d3d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Just4pLeisure | 0:f5d099885d3d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Just4pLeisure | 0:f5d099885d3d | 13 | * See the License for the specific language governing permissions and |
Just4pLeisure | 0:f5d099885d3d | 14 | * limitations under the License. |
Just4pLeisure | 0:f5d099885d3d | 15 | */ |
Just4pLeisure | 0:f5d099885d3d | 16 | |
Just4pLeisure | 0:f5d099885d3d | 17 | #include "seeed_can_api.h" |
Just4pLeisure | 0:f5d099885d3d | 18 | |
Just4pLeisure | 0:f5d099885d3d | 19 | /** Initialise the MCP2515 and set the bit rate |
Just4pLeisure | 0:f5d099885d3d | 20 | */ |
Just4pLeisure | 0:f5d099885d3d | 21 | uint8_t mcpInit(can_t *obj, const uint32_t bitRate) |
Just4pLeisure | 0:f5d099885d3d | 22 | { |
Just4pLeisure | 0:f5d099885d3d | 23 | union { // Access CANMsg as: |
Just4pLeisure | 0:f5d099885d3d | 24 | CANMsg x; // the organised struct |
Just4pLeisure | 0:f5d099885d3d | 25 | uint8_t y[]; // or contiguous memory array |
Just4pLeisure | 0:f5d099885d3d | 26 | }; |
Just4pLeisure | 0:f5d099885d3d | 27 | uint8_t maskFilt[8] = { MCP_RXM0SIDH, MCP_RXM1SIDH, MCP_RXF0SIDH, MCP_RXF1SIDH, MCP_RXF2SIDH, MCP_RXF3SIDH, MCP_RXF4SIDH, MCP_RXF5SIDH }; |
Just4pLeisure | 0:f5d099885d3d | 28 | uint8_t canBufCtrl[5] = { MCP_TXB0CTRL, MCP_TXB1CTRL, MCP_TXB2CTRL, MCP_RXB0CTRL, MCP_RXB1CTRL }; |
Just4pLeisure | 0:f5d099885d3d | 29 | uint8_t canBuffer[3] = { MCP_TXB0CTRL+1, MCP_TXB1CTRL+1, MCP_TXB2CTRL+1 }; |
Just4pLeisure | 0:f5d099885d3d | 30 | |
Just4pLeisure | 0:f5d099885d3d | 31 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 32 | printf("Reseting MCP2515\r\n"); |
Just4pLeisure | 0:f5d099885d3d | 33 | #endif |
Just4pLeisure | 0:f5d099885d3d | 34 | mcpReset(obj); |
Just4pLeisure | 0:f5d099885d3d | 35 | for (uint32_t i = 0; i < 8; i++) { // Clear all CAN id masks and filters |
Just4pLeisure | 0:f5d099885d3d | 36 | mcpWriteId(obj, maskFilt[i], NULL, NULL); |
Just4pLeisure | 0:f5d099885d3d | 37 | } |
Just4pLeisure | 0:f5d099885d3d | 38 | for (uint32_t i = 0; i < 5; i++) { // Clear all CAN buffer control registers |
Just4pLeisure | 0:f5d099885d3d | 39 | mcpWrite(obj, canBufCtrl[i], NULL); |
Just4pLeisure | 0:f5d099885d3d | 40 | } |
Just4pLeisure | 0:f5d099885d3d | 41 | for (uint32_t i = 0; i < sizeof(x); i++) y[i] = NULL; // Initialise empty CAN message buffer |
Just4pLeisure | 0:f5d099885d3d | 42 | for (uint32_t i = 0; i < 3; i++) { // Clear all CAN TX buffers |
Just4pLeisure | 0:f5d099885d3d | 43 | mcpWriteMultiple(obj, canBuffer[i], y, sizeof(x) ); // using empty CAN message (as an array) |
Just4pLeisure | 0:f5d099885d3d | 44 | } |
Just4pLeisure | 1:ad71faa09868 | 45 | // enable both receive-buffers, using filters to receive messages with std. and ext. identifiers that meet the filter criteria and enable rollover from RXB0 to RXB1 if RXB0 is full |
Just4pLeisure | 0:f5d099885d3d | 46 | mcpBitModify(obj, MCP_RXB0CTRL, MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK, MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK ); |
Just4pLeisure | 0:f5d099885d3d | 47 | mcpBitModify(obj, MCP_RXB1CTRL, MCP_RXB_RX_MASK, MCP_RXB_RX_STDEXT); |
Just4pLeisure | 0:f5d099885d3d | 48 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 49 | printf("Setting bit rate\r\n"); |
Just4pLeisure | 0:f5d099885d3d | 50 | #endif |
Just4pLeisure | 1:ad71faa09868 | 51 | if (!mcpSetBitRate(obj, bitRate)) { // set baudrate |
Just4pLeisure | 1:ad71faa09868 | 52 | return 0; |
Just4pLeisure | 1:ad71faa09868 | 53 | } |
Just4pLeisure | 1:ad71faa09868 | 54 | return mcpSetMode(obj, MODE_NORMAL) ? 1 : 0; // set Normal mode and return |
Just4pLeisure | 0:f5d099885d3d | 55 | } |
Just4pLeisure | 0:f5d099885d3d | 56 | |
Just4pLeisure | 0:f5d099885d3d | 57 | /** set MCP2515 operation mode |
Just4pLeisure | 0:f5d099885d3d | 58 | * |
Just4pLeisure | 0:f5d099885d3d | 59 | * Configuration, Normal, Sleep, Listen-only or Loopback |
Just4pLeisure | 0:f5d099885d3d | 60 | */ |
Just4pLeisure | 0:f5d099885d3d | 61 | uint8_t mcpSetMode(can_t *obj, const uint8_t newmode) |
Just4pLeisure | 0:f5d099885d3d | 62 | { |
Just4pLeisure | 0:f5d099885d3d | 63 | mcpBitModify(obj, MCP_CANCTRL, MODE_MASK, newmode); |
Just4pLeisure | 0:f5d099885d3d | 64 | for (uint32_t i = 0; i<10; i++) { |
Just4pLeisure | 0:f5d099885d3d | 65 | if ((mcpRead(obj, MCP_CANSTAT) & MODE_MASK) == newmode) { |
Just4pLeisure | 0:f5d099885d3d | 66 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 67 | printf("Successfully entered mode: %02x time: %dms\r\n", newmode, i); |
Just4pLeisure | 0:f5d099885d3d | 68 | printf("CANCTRL:%02x CANSTAT:%02x TXB0:%02x TXB1:%02x TXB2:%02x\r\n", mcpRead(obj, MCP_CANCTRL), mcpRead(obj, MCP_CANSTAT), mcpRead(obj, MCP_TXB0CTRL), mcpRead(obj, MCP_TXB1CTRL), mcpRead(obj, MCP_TXB2CTRL)); |
Just4pLeisure | 0:f5d099885d3d | 69 | #endif |
Just4pLeisure | 0:f5d099885d3d | 70 | return 1; |
Just4pLeisure | 0:f5d099885d3d | 71 | } |
Just4pLeisure | 0:f5d099885d3d | 72 | wait_ms(1); |
Just4pLeisure | 0:f5d099885d3d | 73 | } |
Just4pLeisure | 0:f5d099885d3d | 74 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 75 | printf("Failed to enter mode: %02x\r\n", newmode); |
Just4pLeisure | 0:f5d099885d3d | 76 | printf("CANCTRL:%02x CANSTAT:%02x TXB0:%02x TXB1:%02x TXB2:%02x\r\n", mcpRead(obj, MCP_CANCTRL), mcpRead(obj, MCP_CANSTAT), mcpRead(obj, MCP_TXB0CTRL), mcpRead(obj, MCP_TXB1CTRL), mcpRead(obj, MCP_TXB2CTRL)); |
Just4pLeisure | 0:f5d099885d3d | 77 | #endif |
Just4pLeisure | 0:f5d099885d3d | 78 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 79 | } |
Just4pLeisure | 0:f5d099885d3d | 80 | |
Just4pLeisure | 0:f5d099885d3d | 81 | /** set the CAN bus bitrate |
Just4pLeisure | 0:f5d099885d3d | 82 | * |
Just4pLeisure | 0:f5d099885d3d | 83 | * Calculate suitable BTR register values. |
Just4pLeisure | 0:f5d099885d3d | 84 | * The Bit Rate Pre-scaler (BRP) can be in the range of 1-64. |
Just4pLeisure | 0:f5d099885d3d | 85 | * According to CANopen, Bit Time can be be between 25 and 8 Time Quanta (TQU). |
Just4pLeisure | 0:f5d099885d3d | 86 | * Bit Time = SyncSeg(1 TQU) + PropSeg(1-8 TQU) + PhaseSeg1(1-8 TQU) + PhaseSeg2(2-8 TQU). |
Just4pLeisure | 0:f5d099885d3d | 87 | * SyncSeg is always 1TQU, PhaseSeg2 must be at least 2TQU to be longer than the processing time. |
Just4pLeisure | 0:f5d099885d3d | 88 | * Opinions vary on when to take a sample but a point roughly 2/3 of Bit Time seems OK. |
Just4pLeisure | 0:f5d099885d3d | 89 | * Synchronisation Jump width can be 1-4 TQU, a value of 1 seems to be normal. |
Just4pLeisure | 0:f5d099885d3d | 90 | * |
Just4pLeisure | 0:f5d099885d3d | 91 | * All register values are -1, e.g. PropSeg can range from 1-8 TQU, so values are 0-7 (0-63 for BRP). |
Just4pLeisure | 0:f5d099885d3d | 92 | * |
Just4pLeisure | 0:f5d099885d3d | 93 | * This table has the sampling points as close to 2/3 (66.7%) as possible. |
Just4pLeisure | 0:f5d099885d3d | 94 | * The first value is PropSeg, 2nd PhaseSeg1. |
Just4pLeisure | 0:f5d099885d3d | 95 | * PhaseSeg2 will be the same as PhaseSeg1 when btlmode bit is initialised to 0. |
Just4pLeisure | 0:f5d099885d3d | 96 | */ |
Just4pLeisure | 0:f5d099885d3d | 97 | static const uint8_t timing_pts[18][2] = { |
Just4pLeisure | 0:f5d099885d3d | 98 | {0x0, 0x2}, // 8, 62.5% |
Just4pLeisure | 0:f5d099885d3d | 99 | {0x1, 0x2}, // 9, 66.7% |
Just4pLeisure | 0:f5d099885d3d | 100 | {0x2, 0x2}, // 10, 70.0% |
Just4pLeisure | 0:f5d099885d3d | 101 | {0x1, 0x3}, // 11, 63.6% |
Just4pLeisure | 0:f5d099885d3d | 102 | {0x2, 0x3}, // 12, 66.7% |
Just4pLeisure | 0:f5d099885d3d | 103 | {0x3, 0x3}, // 13, 69.2% |
Just4pLeisure | 0:f5d099885d3d | 104 | {0x2, 0x4}, // 14, 64.3% |
Just4pLeisure | 0:f5d099885d3d | 105 | {0x3, 0x4}, // 15, 66.7% |
Just4pLeisure | 0:f5d099885d3d | 106 | {0x4, 0x4}, // 16, 68.75% |
Just4pLeisure | 0:f5d099885d3d | 107 | {0x3, 0x5}, // 17, 64.7% |
Just4pLeisure | 0:f5d099885d3d | 108 | {0x4, 0x5}, // 18, 66.7% |
Just4pLeisure | 0:f5d099885d3d | 109 | {0x5, 0x5}, // 19, 63.2% |
Just4pLeisure | 0:f5d099885d3d | 110 | {0x4, 0x6}, // 20, 65.0% |
Just4pLeisure | 0:f5d099885d3d | 111 | {0x5, 0x6}, // 21, 66.7% |
Just4pLeisure | 0:f5d099885d3d | 112 | {0x6, 0x6}, // 22, 68.2% |
Just4pLeisure | 0:f5d099885d3d | 113 | {0x5, 0x7}, // 23, 65.2 |
Just4pLeisure | 0:f5d099885d3d | 114 | {0x6, 0x7}, // 24, 66.7% |
Just4pLeisure | 0:f5d099885d3d | 115 | {0x7, 0x7}, // 25, 68.0% |
Just4pLeisure | 0:f5d099885d3d | 116 | }; |
Just4pLeisure | 0:f5d099885d3d | 117 | |
Just4pLeisure | 0:f5d099885d3d | 118 | uint8_t mcpSetBitRate(can_t *obj, const uint32_t bitRate) |
Just4pLeisure | 0:f5d099885d3d | 119 | { |
Just4pLeisure | 0:f5d099885d3d | 120 | union { // Access CANtiming as: |
Just4pLeisure | 0:f5d099885d3d | 121 | CANtiming x; // the organised struct |
Just4pLeisure | 0:f5d099885d3d | 122 | uint8_t y[]; // or contiguous memory array |
Just4pLeisure | 0:f5d099885d3d | 123 | }; |
Just4pLeisure | 0:f5d099885d3d | 124 | uint32_t bestBRP = 0; |
Just4pLeisure | 0:f5d099885d3d | 125 | uint32_t bestTQU = 0; |
Just4pLeisure | 0:f5d099885d3d | 126 | uint32_t bestCanRate = 0; |
Just4pLeisure | 0:f5d099885d3d | 127 | uint32_t minBRP = (MCP_CLOCK_FREQ / (2 * MCP_MAX_TIME_QUANTA * bitRate)); |
Just4pLeisure | 0:f5d099885d3d | 128 | uint32_t maxBRP = (MCP_CLOCK_FREQ / (2 * MCP_MIN_TIME_QUANTA * bitRate)); |
Just4pLeisure | 1:ad71faa09868 | 129 | |
Just4pLeisure | 1:ad71faa09868 | 130 | #ifdef DEBUG |
Just4pLeisure | 1:ad71faa09868 | 131 | printf("Setting configuration mode\r\n"); |
Just4pLeisure | 1:ad71faa09868 | 132 | #endif |
Just4pLeisure | 1:ad71faa09868 | 133 | uint8_t initialMode = mcpRead(obj, MCP_CANCTRL) & MODE_MASK; // Store the current operation mode |
Just4pLeisure | 1:ad71faa09868 | 134 | if(!mcpSetMode(obj, MODE_CONFIG)) { // Go into configuration mode |
Just4pLeisure | 1:ad71faa09868 | 135 | return 0; |
Just4pLeisure | 1:ad71faa09868 | 136 | } |
Just4pLeisure | 1:ad71faa09868 | 137 | |
Just4pLeisure | 0:f5d099885d3d | 138 | for (uint32_t i = 0; i < sizeof(x); i++) y[i] = NULL; // Initialise CANtiming (btlmode, sjw and sam all = 0) |
Just4pLeisure | 0:f5d099885d3d | 139 | if ((bitRate < CAN_MIN_RATE) || (bitRate > CAN_MAX_RATE)) { |
Just4pLeisure | 0:f5d099885d3d | 140 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 141 | printf("FAILED!! The requested Bit Rate is too high or too low: %d\r\n", bitRate); |
Just4pLeisure | 0:f5d099885d3d | 142 | #endif |
Just4pLeisure | 0:f5d099885d3d | 143 | return 0; // Cannot set the requested bit rate! |
Just4pLeisure | 0:f5d099885d3d | 144 | } |
Just4pLeisure | 0:f5d099885d3d | 145 | minBRP = (minBRP == 0) ? MCP_MIN_PRESCALER : minBRP; |
Just4pLeisure | 0:f5d099885d3d | 146 | maxBRP = (maxBRP > MCP_MAX_PRESCALER) ? MCP_MAX_PRESCALER : maxBRP; |
Just4pLeisure | 0:f5d099885d3d | 147 | for (uint32_t BRP = minBRP; BRP < (maxBRP + 1); BRP++) { |
Just4pLeisure | 0:f5d099885d3d | 148 | uint32_t timeQuanta = (MCP_CLOCK_FREQ / (2 * BRP * bitRate)); |
Just4pLeisure | 0:f5d099885d3d | 149 | if ((timeQuanta >= MCP_MIN_TIME_QUANTA) && (timeQuanta <= MCP_MAX_TIME_QUANTA)) { |
Just4pLeisure | 0:f5d099885d3d | 150 | for (uint32_t TQU = timeQuanta; TQU <= MCP_MAX_TIME_QUANTA; TQU++) { |
Just4pLeisure | 0:f5d099885d3d | 151 | uint32_t thisCanRate = MCP_CLOCK_FREQ / (2 * BRP * TQU); |
Just4pLeisure | 0:f5d099885d3d | 152 | if ( abs((int)bitRate - (int)thisCanRate) < abs((int)bitRate - (int)bestCanRate)) { |
Just4pLeisure | 0:f5d099885d3d | 153 | bestCanRate = thisCanRate; |
Just4pLeisure | 0:f5d099885d3d | 154 | bestBRP= BRP; |
Just4pLeisure | 0:f5d099885d3d | 155 | bestTQU= TQU; |
Just4pLeisure | 0:f5d099885d3d | 156 | } |
Just4pLeisure | 0:f5d099885d3d | 157 | } |
Just4pLeisure | 0:f5d099885d3d | 158 | } |
Just4pLeisure | 0:f5d099885d3d | 159 | } |
Just4pLeisure | 0:f5d099885d3d | 160 | x.brp = (bestBRP - 1); |
Just4pLeisure | 0:f5d099885d3d | 161 | x.prseg = (timing_pts[bestTQU - 8][0]); |
Just4pLeisure | 0:f5d099885d3d | 162 | x.phseg1 = (timing_pts[bestTQU - 8][1]); |
Just4pLeisure | 0:f5d099885d3d | 163 | mcpWriteMultiple(obj, MCP_CNF3, y, sizeof(x) ); // Copy CANtiming to the MCP2515 (as an array) |
Just4pLeisure | 0:f5d099885d3d | 164 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 165 | printf("minBRP %d maxBRP %d\r\n", minBRP, maxBRP); |
Just4pLeisure | 0:f5d099885d3d | 166 | printf("Bitrate: %d\tactualBitRate: %d\t Error: %1.2f percent.\r\n", bitRate, bestCanRate, (100-(100*(float)bitRate/(float)bestCanRate))); |
Just4pLeisure | 0:f5d099885d3d | 167 | printf("TimeQuanta: %d\tbitRatePrescaler: %d\tSamplePoint: %2.2f percent\r\n", bestTQU, bestBRP, 100*(float)(3 + x.prseg + x.phseg1)/(float)bestTQU ) ; |
Just4pLeisure | 0:f5d099885d3d | 168 | printf("Syncseg: 1\tPropSeg: %d\tPhaseSeg1: %d\tPhaseSeg2: %d\r\n", (x.prseg+1), (x.phseg1+1), (x.phseg1+1)); |
Just4pLeisure | 0:f5d099885d3d | 169 | printf("Setting normal mode\r\n"); |
Just4pLeisure | 0:f5d099885d3d | 170 | #endif |
Just4pLeisure | 1:ad71faa09868 | 171 | return (mcpSetMode(obj, initialMode)) ? 1 : 0; // desired bit rate set enter normal mode and return |
Just4pLeisure | 0:f5d099885d3d | 172 | } |
Just4pLeisure | 0:f5d099885d3d | 173 | |
Just4pLeisure | 0:f5d099885d3d | 174 | /** write a CAN id to a mask, filter or transmit buffer |
Just4pLeisure | 0:f5d099885d3d | 175 | */ |
Just4pLeisure | 0:f5d099885d3d | 176 | void mcpWriteId(can_t *obj, const uint8_t mcp_addr, const uint8_t ext, const uint32_t id ) |
Just4pLeisure | 0:f5d099885d3d | 177 | { |
Just4pLeisure | 0:f5d099885d3d | 178 | union { // Access CANid as: |
Just4pLeisure | 0:f5d099885d3d | 179 | CANid x; // the organised struct |
Just4pLeisure | 0:f5d099885d3d | 180 | uint8_t y[]; // or contiguous memory array |
Just4pLeisure | 0:f5d099885d3d | 181 | }; |
Just4pLeisure | 0:f5d099885d3d | 182 | |
Just4pLeisure | 0:f5d099885d3d | 183 | for (uint32_t i = 0; i < sizeof(x); i++) y[i] = NULL; // Initialise CANid structure |
Just4pLeisure | 0:f5d099885d3d | 184 | x.ide = ext; // Extended Identifier Flag |
Just4pLeisure | 0:f5d099885d3d | 185 | if (x.ide == CANExtended) { |
Just4pLeisure | 0:f5d099885d3d | 186 | x.sid10_3 = (uint8_t) (id >> 21); // SID10..3 |
Just4pLeisure | 0:f5d099885d3d | 187 | x.sid2_0 = (uint8_t) (id >> 18) & 0x07; // SID2..0 |
Just4pLeisure | 0:f5d099885d3d | 188 | x.eid17_16 = (uint8_t) (id >> 16) & 0x03; // EID17..16 |
Just4pLeisure | 0:f5d099885d3d | 189 | x.eid15_8 = (uint8_t) (id >> 8); // EID15..8 |
Just4pLeisure | 0:f5d099885d3d | 190 | x.eid7_0 = (uint8_t) id; // EID7..0 |
Just4pLeisure | 0:f5d099885d3d | 191 | } else { |
Just4pLeisure | 0:f5d099885d3d | 192 | x.sid10_3 = (uint8_t) (id >> 3); // SID10..3 |
Just4pLeisure | 0:f5d099885d3d | 193 | x.sid2_0 = (uint8_t) (id & 0x07); // SID2..0 |
Just4pLeisure | 0:f5d099885d3d | 194 | } |
Just4pLeisure | 0:f5d099885d3d | 195 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 196 | printf("sizeof CanIdStruct: %d bytes\r\n", sizeof(x)); |
Just4pLeisure | 0:f5d099885d3d | 197 | printf("sid10_3: %x\r\n", x.sid10_3); |
Just4pLeisure | 0:f5d099885d3d | 198 | printf("eid17_16: %x\r\n", x.eid17_16); |
Just4pLeisure | 0:f5d099885d3d | 199 | printf("ide: %x\r\n", x.ide); |
Just4pLeisure | 0:f5d099885d3d | 200 | printf("srtr: %x\r\n", x.srtr); |
Just4pLeisure | 0:f5d099885d3d | 201 | printf("sid2_0: %x\r\n", x.sid2_0); |
Just4pLeisure | 0:f5d099885d3d | 202 | printf("eid15_8: %x\r\n", x.eid15_8); |
Just4pLeisure | 0:f5d099885d3d | 203 | printf("eid7_0: %x\r\n", x.eid7_0); |
Just4pLeisure | 0:f5d099885d3d | 204 | #endif |
Just4pLeisure | 0:f5d099885d3d | 205 | mcpWriteMultiple(obj, mcp_addr, y, sizeof(x) ); // Copy CANid to the MCP2515 (as an array) |
Just4pLeisure | 0:f5d099885d3d | 206 | } |
Just4pLeisure | 0:f5d099885d3d | 207 | |
Just4pLeisure | 0:f5d099885d3d | 208 | /** write a CAN message to the MCP2515 |
Just4pLeisure | 0:f5d099885d3d | 209 | */ |
Just4pLeisure | 0:f5d099885d3d | 210 | uint8_t mcpCanWrite(can_t *obj, CAN_Message msg) |
Just4pLeisure | 0:f5d099885d3d | 211 | { |
Just4pLeisure | 0:f5d099885d3d | 212 | union { // Access CANMsg as: |
Just4pLeisure | 0:f5d099885d3d | 213 | CANMsg x; // the organised struct |
Just4pLeisure | 0:f5d099885d3d | 214 | uint8_t y[]; // or contiguous memory array |
Just4pLeisure | 0:f5d099885d3d | 215 | }; |
Just4pLeisure | 0:f5d099885d3d | 216 | uint8_t bufferCommand[] = {MCP_WRITE_TX0, MCP_WRITE_TX1, MCP_WRITE_TX2}; |
Just4pLeisure | 0:f5d099885d3d | 217 | uint8_t rtsCommand[] = {MCP_RTS_TX0, MCP_RTS_TX1, MCP_RTS_TX2}; |
Just4pLeisure | 0:f5d099885d3d | 218 | uint8_t status = mcpStatus(obj); |
Just4pLeisure | 0:f5d099885d3d | 219 | uint32_t num = 0; |
Just4pLeisure | 0:f5d099885d3d | 220 | // Check if there is a free message buffer |
Just4pLeisure | 0:f5d099885d3d | 221 | if (!(status & MCP_STAT_TX0REQ)) { // TX Message Buffer 0 free? |
Just4pLeisure | 0:f5d099885d3d | 222 | num = 0; |
Just4pLeisure | 0:f5d099885d3d | 223 | } else if (!(status & MCP_STAT_TX1REQ)) { // TX Message Buffer 1 free? |
Just4pLeisure | 0:f5d099885d3d | 224 | num = 1; |
Just4pLeisure | 0:f5d099885d3d | 225 | } else if (!(status & MCP_STAT_TX2REQ)) { // TX Message Buffer 2 free? |
Just4pLeisure | 0:f5d099885d3d | 226 | num = 2; |
Just4pLeisure | 0:f5d099885d3d | 227 | } else { |
Just4pLeisure | 0:f5d099885d3d | 228 | return 0; // No free transmit buffers in the MCP2515 CAN controller chip |
Just4pLeisure | 0:f5d099885d3d | 229 | } |
Just4pLeisure | 0:f5d099885d3d | 230 | // populate CANMsg structure |
Just4pLeisure | 0:f5d099885d3d | 231 | for (uint32_t i = 0; i < sizeof(x); i++) y[i] = NULL; // Initialise CANMsg structure |
Just4pLeisure | 0:f5d099885d3d | 232 | x.id.ide = msg.format; // Extended Identifier Flag |
Just4pLeisure | 0:f5d099885d3d | 233 | if (x.id.ide == CANExtended) { |
Just4pLeisure | 0:f5d099885d3d | 234 | x.id.sid10_3 = (uint8_t) (msg.id >> 21); // SID10..3 |
Just4pLeisure | 0:f5d099885d3d | 235 | x.id.sid2_0 = (uint8_t) (msg.id >> 18) & 0x07; // SID2..0 |
Just4pLeisure | 0:f5d099885d3d | 236 | x.id.eid17_16 = (uint8_t) (msg.id >> 16) & 0x03; // EID17..16 |
Just4pLeisure | 0:f5d099885d3d | 237 | x.id.eid15_8 = (uint8_t) (msg.id >> 8); // EID15..8 |
Just4pLeisure | 0:f5d099885d3d | 238 | x.id.eid7_0 = (uint8_t) msg.id; // EID7..0 |
Just4pLeisure | 0:f5d099885d3d | 239 | } else { |
Just4pLeisure | 0:f5d099885d3d | 240 | x.id.sid10_3 = (uint8_t) (msg.id >> 3); // SID10..3 |
Just4pLeisure | 0:f5d099885d3d | 241 | x.id.sid2_0 = (uint8_t) (msg.id & 0x07); // SID2..0 |
Just4pLeisure | 0:f5d099885d3d | 242 | } |
Just4pLeisure | 0:f5d099885d3d | 243 | x.dlc = msg.len & 0x0f; // Number of bytes in can message |
Just4pLeisure | 0:f5d099885d3d | 244 | x.ertr = msg.type; // Data or remote message |
Just4pLeisure | 0:f5d099885d3d | 245 | memcpy(x.data,msg.data,x.dlc); // Get the Data bytes |
Just4pLeisure | 0:f5d099885d3d | 246 | // write CANmsg to the specified TX buffer 'num' |
Just4pLeisure | 0:f5d099885d3d | 247 | mcpWriteBuffer(obj, bufferCommand[num], y, sizeof(x)); // Write the message ,CANMsg, to the MCP2515's Tx buffer 'num' (as an array) |
Just4pLeisure | 0:f5d099885d3d | 248 | mcpBufferRTS(obj, rtsCommand[num]); |
Just4pLeisure | 0:f5d099885d3d | 249 | return 1; // Indicate that message has been transmitted |
Just4pLeisure | 0:f5d099885d3d | 250 | } |
Just4pLeisure | 0:f5d099885d3d | 251 | |
Just4pLeisure | 0:f5d099885d3d | 252 | /** read a CAN message from the MCP2515 |
Just4pLeisure | 0:f5d099885d3d | 253 | */ |
Just4pLeisure | 0:f5d099885d3d | 254 | uint8_t mcpCanRead(can_t *obj, CAN_Message *msg) |
Just4pLeisure | 0:f5d099885d3d | 255 | { |
Just4pLeisure | 0:f5d099885d3d | 256 | union { // Access CANMsg as: |
Just4pLeisure | 0:f5d099885d3d | 257 | CANMsg x; // the organised struct |
Just4pLeisure | 0:f5d099885d3d | 258 | uint8_t y[]; // or contiguous memory array |
Just4pLeisure | 0:f5d099885d3d | 259 | }; |
Just4pLeisure | 0:f5d099885d3d | 260 | uint8_t bufferCommand[] = {MCP_READ_RX0, MCP_READ_RX1}; |
Just4pLeisure | 0:f5d099885d3d | 261 | uint8_t status = mcpReceiveStatus(obj); |
Just4pLeisure | 0:f5d099885d3d | 262 | bool num = 0; |
Just4pLeisure | 0:f5d099885d3d | 263 | // Check if there is a message the buffers |
Just4pLeisure | 0:f5d099885d3d | 264 | if (status & MCP_RXSTAT_RXB0) { // Msg in Buffer 0? |
Just4pLeisure | 0:f5d099885d3d | 265 | num = 0; |
Just4pLeisure | 0:f5d099885d3d | 266 | } else if (status & MCP_RXSTAT_RXB1) { // Msg in Buffer 1? |
Just4pLeisure | 0:f5d099885d3d | 267 | num = 1; |
Just4pLeisure | 0:f5d099885d3d | 268 | } else { |
Just4pLeisure | 0:f5d099885d3d | 269 | return 0; // No messages waiting |
Just4pLeisure | 0:f5d099885d3d | 270 | } |
Just4pLeisure | 0:f5d099885d3d | 271 | mcpReadBuffer(obj, bufferCommand[0], y, sizeof(x)); // Read the message into CANMsg (as an array) |
Just4pLeisure | 0:f5d099885d3d | 272 | mcpBitModify(obj, MCP_CANINTF, (!num ? MCP_RX0IF : MCP_RX1IF), 0); // Free the message buffer |
Just4pLeisure | 0:f5d099885d3d | 273 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 274 | printf("sizeof CanMsgStruct: %d bytes\r\n", sizeof(x)); |
Just4pLeisure | 0:f5d099885d3d | 275 | printf("sizeof CanMsgArray: %d bytes\r\n", sizeof(y)); |
Just4pLeisure | 0:f5d099885d3d | 276 | printf("sid10_3: %x\r\n", x.id.sid10_3); |
Just4pLeisure | 0:f5d099885d3d | 277 | printf("eid17_16: %x\r\n", x.id.eid17_16); |
Just4pLeisure | 0:f5d099885d3d | 278 | printf("ide: %x\r\n", x.id.ide); |
Just4pLeisure | 0:f5d099885d3d | 279 | printf("srtr: %x\r\n", x.id.srtr); |
Just4pLeisure | 0:f5d099885d3d | 280 | printf("sid2_0: %x\r\n", x.id.sid2_0); |
Just4pLeisure | 0:f5d099885d3d | 281 | printf("eid15_8: %x\r\n", x.id.eid15_8); |
Just4pLeisure | 0:f5d099885d3d | 282 | printf("eid7_0: %x\r\n", x.id.eid7_0); |
Just4pLeisure | 0:f5d099885d3d | 283 | printf("dlc: %x\r\n", x.dlc); |
Just4pLeisure | 0:f5d099885d3d | 284 | printf("ertr: %x\r\n", x.ertr); |
Just4pLeisure | 0:f5d099885d3d | 285 | printf("data: "); |
Just4pLeisure | 0:f5d099885d3d | 286 | for (char i=0; i<8; i++) |
Just4pLeisure | 0:f5d099885d3d | 287 | printf("%02x,", x.data[i]); |
Just4pLeisure | 0:f5d099885d3d | 288 | printf("\r\n"); |
Just4pLeisure | 0:f5d099885d3d | 289 | #endif |
Just4pLeisure | 0:f5d099885d3d | 290 | msg->format = (status & MCP_RXSTAT_IDE) ? CANExtended : CANStandard;// Extended CAN id Flag |
Just4pLeisure | 0:f5d099885d3d | 291 | if (msg->format == CANExtended) { // Assemble the Extended CAN id |
Just4pLeisure | 0:f5d099885d3d | 292 | msg->id = (x.id.sid10_3 << 21) | |
Just4pLeisure | 0:f5d099885d3d | 293 | (x.id.sid2_0 << 18) | |
Just4pLeisure | 0:f5d099885d3d | 294 | (x.id.eid17_16 << 16) | |
Just4pLeisure | 0:f5d099885d3d | 295 | (x.id.eid15_8 << 8) | |
Just4pLeisure | 0:f5d099885d3d | 296 | (x.id.eid7_0); |
Just4pLeisure | 0:f5d099885d3d | 297 | } else { // Assemble the Standard CAN id |
Just4pLeisure | 0:f5d099885d3d | 298 | msg->id = (x.id.sid10_3 << 3) | |
Just4pLeisure | 0:f5d099885d3d | 299 | (x.id.sid2_0); |
Just4pLeisure | 0:f5d099885d3d | 300 | } |
Just4pLeisure | 0:f5d099885d3d | 301 | msg->len = x.dlc; // Number of bytes in CAN message |
Just4pLeisure | 0:f5d099885d3d | 302 | msg->type = (status & MCP_RXSTAT_RTR) ? CANRemote : CANData; // Determine if a Remote or Data message type |
Just4pLeisure | 0:f5d099885d3d | 303 | memcpy(msg->data,x.data,x.dlc); // Get the Data bytes |
Just4pLeisure | 0:f5d099885d3d | 304 | return 1; // Indicate that message has been retrieved |
Just4pLeisure | 0:f5d099885d3d | 305 | } |
Just4pLeisure | 0:f5d099885d3d | 306 | |
Just4pLeisure | 0:f5d099885d3d | 307 | /** initialise an Acceptance Mask |
Just4pLeisure | 0:f5d099885d3d | 308 | */ |
Just4pLeisure | 0:f5d099885d3d | 309 | uint8_t mcpInitMask(can_t *obj, uint8_t num, uint32_t ulData, bool ext) |
Just4pLeisure | 0:f5d099885d3d | 310 | { |
Just4pLeisure | 0:f5d099885d3d | 311 | uint8_t mask[2] = { MCP_RXM0SIDH, MCP_RXM1SIDH }; |
Just4pLeisure | 0:f5d099885d3d | 312 | |
Just4pLeisure | 0:f5d099885d3d | 313 | if (num > 1) { |
Just4pLeisure | 0:f5d099885d3d | 314 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 315 | printf("Trying to set an invalid Mask number: %d\r\n", num); |
Just4pLeisure | 0:f5d099885d3d | 316 | #endif |
Just4pLeisure | 0:f5d099885d3d | 317 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 318 | } |
Just4pLeisure | 0:f5d099885d3d | 319 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 320 | printf("Begin to set Mask!!\r\n"); |
Just4pLeisure | 0:f5d099885d3d | 321 | #endif |
Just4pLeisure | 1:ad71faa09868 | 322 | uint8_t initialMode = mcpRead(obj, MCP_CANCTRL) & MODE_MASK; // Store the current operation mode |
Just4pLeisure | 0:f5d099885d3d | 323 | if(!mcpSetMode(obj, MODE_CONFIG)) { |
Just4pLeisure | 0:f5d099885d3d | 324 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 325 | } |
Just4pLeisure | 0:f5d099885d3d | 326 | mcpWriteId(obj, mask[num], ext, ulData); |
Just4pLeisure | 1:ad71faa09868 | 327 | if(!mcpSetMode(obj, initialMode)) { |
Just4pLeisure | 0:f5d099885d3d | 328 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 329 | } |
Just4pLeisure | 0:f5d099885d3d | 330 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 331 | printf("Successfully set Mask number: %d\r\n", num); |
Just4pLeisure | 0:f5d099885d3d | 332 | #endif |
Just4pLeisure | 0:f5d099885d3d | 333 | return 1; |
Just4pLeisure | 0:f5d099885d3d | 334 | } |
Just4pLeisure | 0:f5d099885d3d | 335 | |
Just4pLeisure | 0:f5d099885d3d | 336 | /** initialise an Acceptance Filter |
Just4pLeisure | 0:f5d099885d3d | 337 | */ |
Just4pLeisure | 0:f5d099885d3d | 338 | uint8_t mcpInitFilter(can_t *obj, uint8_t num, uint32_t ulData, bool ext) |
Just4pLeisure | 0:f5d099885d3d | 339 | { |
Just4pLeisure | 0:f5d099885d3d | 340 | uint8_t filter[6] = { MCP_RXF0SIDH, MCP_RXF1SIDH, MCP_RXF2SIDH, MCP_RXF3SIDH, MCP_RXF4SIDH, MCP_RXF5SIDH }; |
Just4pLeisure | 0:f5d099885d3d | 341 | |
Just4pLeisure | 0:f5d099885d3d | 342 | if (num > 5) { |
Just4pLeisure | 0:f5d099885d3d | 343 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 344 | printf("Trying to set an invalid Filter number: %d\r\n", num); |
Just4pLeisure | 0:f5d099885d3d | 345 | #endif |
Just4pLeisure | 0:f5d099885d3d | 346 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 347 | } |
Just4pLeisure | 0:f5d099885d3d | 348 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 349 | printf("Begin to set Filter!!\r\n"); |
Just4pLeisure | 0:f5d099885d3d | 350 | #endif |
Just4pLeisure | 1:ad71faa09868 | 351 | uint8_t initialMode = mcpRead(obj, MCP_CANCTRL) & MODE_MASK; // Store the current operation mode |
Just4pLeisure | 0:f5d099885d3d | 352 | if(!mcpSetMode(obj, MODE_CONFIG)) { |
Just4pLeisure | 0:f5d099885d3d | 353 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 354 | } |
Just4pLeisure | 0:f5d099885d3d | 355 | mcpWriteId(obj, filter[num], ext, ulData); |
Just4pLeisure | 1:ad71faa09868 | 356 | if(!mcpSetMode(obj, initialMode)) { |
Just4pLeisure | 0:f5d099885d3d | 357 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 358 | } |
Just4pLeisure | 0:f5d099885d3d | 359 | #ifdef DEBUG |
Just4pLeisure | 0:f5d099885d3d | 360 | printf("Successfully set Filter: %d\r\n", num); |
Just4pLeisure | 0:f5d099885d3d | 361 | #endif |
Just4pLeisure | 0:f5d099885d3d | 362 | return 1; |
Just4pLeisure | 0:f5d099885d3d | 363 | } |
Just4pLeisure | 0:f5d099885d3d | 364 | |
Just4pLeisure | 0:f5d099885d3d | 365 | /* Number of message reception errors |
Just4pLeisure | 0:f5d099885d3d | 366 | */ |
Just4pLeisure | 0:f5d099885d3d | 367 | uint8_t mcpReceptionErrorCount(can_t *obj) |
Just4pLeisure | 0:f5d099885d3d | 368 | { |
Just4pLeisure | 0:f5d099885d3d | 369 | return (mcpRead(obj, MCP_REC)); |
Just4pLeisure | 0:f5d099885d3d | 370 | } |
Just4pLeisure | 0:f5d099885d3d | 371 | |
Just4pLeisure | 0:f5d099885d3d | 372 | /* Number of message transmission errors |
Just4pLeisure | 0:f5d099885d3d | 373 | */ |
Just4pLeisure | 0:f5d099885d3d | 374 | uint8_t mcpTransmissionErrorCount(can_t *obj) |
Just4pLeisure | 0:f5d099885d3d | 375 | { |
Just4pLeisure | 0:f5d099885d3d | 376 | return (mcpRead(obj, MCP_TEC)); |
Just4pLeisure | 0:f5d099885d3d | 377 | } |
Just4pLeisure | 0:f5d099885d3d | 378 | |
Just4pLeisure | 0:f5d099885d3d | 379 | /* Select between monitor (silent = 1) and normal (silent = 0) modes |
Just4pLeisure | 0:f5d099885d3d | 380 | */ |
Just4pLeisure | 0:f5d099885d3d | 381 | void mcpMonitor(can_t *obj, const bool silent) |
Just4pLeisure | 0:f5d099885d3d | 382 | { |
Just4pLeisure | 0:f5d099885d3d | 383 | silent ? mcpSetMode(obj, MODE_LISTENONLY) : mcpSetMode(obj, MODE_NORMAL); |
Just4pLeisure | 0:f5d099885d3d | 384 | } |
Just4pLeisure | 0:f5d099885d3d | 385 | |
Just4pLeisure | 0:f5d099885d3d | 386 | /* Change CAN operation to the specified mode |
Just4pLeisure | 0:f5d099885d3d | 387 | */ |
Just4pLeisure | 0:f5d099885d3d | 388 | uint8_t mcpMode(can_t *obj, const CANMode mode) |
Just4pLeisure | 0:f5d099885d3d | 389 | { |
Just4pLeisure | 0:f5d099885d3d | 390 | uint8_t which[] = { MODE_NORMAL, MODE_SLEEP, MODE_LOOPBACK, MODE_LISTENONLY, MODE_CONFIG, MODE_CONFIG}; |
Just4pLeisure | 0:f5d099885d3d | 391 | |
Just4pLeisure | 0:f5d099885d3d | 392 | if (mode == _RESET) { |
Just4pLeisure | 0:f5d099885d3d | 393 | mcpReset(obj); |
Just4pLeisure | 0:f5d099885d3d | 394 | } |
Just4pLeisure | 0:f5d099885d3d | 395 | if (mcpSetMode(obj, which[mode])) { |
Just4pLeisure | 0:f5d099885d3d | 396 | return 1; |
Just4pLeisure | 0:f5d099885d3d | 397 | } |
Just4pLeisure | 0:f5d099885d3d | 398 | return 0; |
Just4pLeisure | 0:f5d099885d3d | 399 | } |