Seed can implementation
Fork of SEEED_CAN by
seeed_can_api.h@2:fd026fcfde94, 2013-11-12 (annotated)
- Committer:
- Just4pLeisure
- Date:
- Tue Nov 12 20:54:16 2013 +0000
- Revision:
- 2:fd026fcfde94
- Parent:
- 1:ad71faa09868
Updated 12-11-2013 - attach functions are now usable. Changes to allow the the library to be used with more mbeds (e.g. LPC1768). I have also added new functions/methods which give more information about errors and interrupts.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Just4pLeisure | 0:f5d099885d3d | 1 | /* seeed_can_api.h |
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 | #ifndef _SEEED_CAN_API_H_ |
Just4pLeisure | 0:f5d099885d3d | 17 | #define _SEEED_CAN_API_H_ |
Just4pLeisure | 0:f5d099885d3d | 18 | |
Just4pLeisure | 0:f5d099885d3d | 19 | #include "seeed_can_spi.h" |
Just4pLeisure | 0:f5d099885d3d | 20 | |
Just4pLeisure | 0:f5d099885d3d | 21 | // print debug information... |
Just4pLeisure | 0:f5d099885d3d | 22 | //#define DEBUG |
Just4pLeisure | 0:f5d099885d3d | 23 | |
Just4pLeisure | 0:f5d099885d3d | 24 | #ifdef __cplusplus |
Just4pLeisure | 0:f5d099885d3d | 25 | extern "C" { |
Just4pLeisure | 0:f5d099885d3d | 26 | #endif |
Just4pLeisure | 0:f5d099885d3d | 27 | |
Just4pLeisure | 0:f5d099885d3d | 28 | /** CAN driver typedefs |
Just4pLeisure | 0:f5d099885d3d | 29 | */ |
Just4pLeisure | 0:f5d099885d3d | 30 | |
Just4pLeisure | 0:f5d099885d3d | 31 | // The 'MCP_xyz' structs mimic MCP register organisations |
Just4pLeisure | 0:f5d099885d3d | 32 | // A union of the struct and a simple array can be used to: |
Just4pLeisure | 0:f5d099885d3d | 33 | // Process and manipulate the struct in an ordered way |
Just4pLeisure | 0:f5d099885d3d | 34 | // Copy the struct to/from the MCP2512 as an array |
Just4pLeisure | 0:f5d099885d3d | 35 | |
Just4pLeisure | 1:ad71faa09868 | 36 | /// Type definition to hold an MCP2515 Timing Register set structure |
Just4pLeisure | 0:f5d099885d3d | 37 | struct MCP_CANtiming { |
Just4pLeisure | 0:f5d099885d3d | 38 | uint8_t phseg2 : 3; // PS2 length bits 2..0 (PHSEG2 + 1) Tq (minimum valid setting is 2 Tq) |
Just4pLeisure | 0:f5d099885d3d | 39 | uint8_t reserved1 : 3; // Unused bits (read as '0') |
Just4pLeisure | 0:f5d099885d3d | 40 | uint8_t wakfil : 1; // Wake-up filter enable bit (1 = enabled, 0 = disabled) |
Just4pLeisure | 0:f5d099885d3d | 41 | uint8_t sof : 1; // Start of frame bit (1 = CLKOUT pin is SOF, 0 = CLKOUT is clock) |
Just4pLeisure | 0:f5d099885d3d | 42 | uint8_t prseg : 3; // Propagation Segment length bits 2..0 (PRSEG + 1) Tq (minimum valid setting is 1 Tq) |
Just4pLeisure | 0:f5d099885d3d | 43 | uint8_t phseg1 : 3; // PS2 length bits 2..0 (PRSEG + 1) Tq (minimum valid setting is 1 Tq) |
Just4pLeisure | 0:f5d099885d3d | 44 | uint8_t sam : 1; // Sample Point Configuration bit (1 = Bus line is sampled 3 times, 0 = sampled once) |
Just4pLeisure | 0:f5d099885d3d | 45 | uint8_t btlmode : 1; // PS2 Bit Time Length (1 = determined by phseg2, 0 = greater of PS1 an IPT (2 Tq)) |
Just4pLeisure | 0:f5d099885d3d | 46 | uint8_t brp : 6; // Baud Rate Prescaler bits 5..0 (Tq = 2 x (BRP + 1) / Fosc) |
Just4pLeisure | 0:f5d099885d3d | 47 | uint8_t sjw : 2; // Synchronisation Jump Width Length bits 1..0 ((SJW + 1) Tq) |
Just4pLeisure | 0:f5d099885d3d | 48 | }; |
Just4pLeisure | 0:f5d099885d3d | 49 | typedef struct MCP_CANtiming CANtiming; |
Just4pLeisure | 0:f5d099885d3d | 50 | |
Just4pLeisure | 0:f5d099885d3d | 51 | /// Type definition to hold an MCP2515 CAN id structure |
Just4pLeisure | 0:f5d099885d3d | 52 | struct MCP_CANid { |
Just4pLeisure | 0:f5d099885d3d | 53 | uint8_t sid10_3 : 8; // Bits 10..3 of a standard identifier |
Just4pLeisure | 0:f5d099885d3d | 54 | uint8_t eid17_16 : 2; // Bits 17..16 of an extended identifier |
Just4pLeisure | 0:f5d099885d3d | 55 | uint8_t reserved1 : 1; // Unused bit in RXBbSIDL |
Just4pLeisure | 0:f5d099885d3d | 56 | uint8_t ide : 1; // Extended Identifier Flag |
Just4pLeisure | 0:f5d099885d3d | 57 | // CANformat 'type' |
Just4pLeisure | 0:f5d099885d3d | 58 | uint8_t srtr : 1; // Standard Frame Remote Transmit Request Bit (valid when IDE = 0) |
Just4pLeisure | 0:f5d099885d3d | 59 | // CANtype 'type' |
Just4pLeisure | 0:f5d099885d3d | 60 | // Unimplimented in TXBnSIDL, filters and acceptance masks |
Just4pLeisure | 0:f5d099885d3d | 61 | uint8_t sid2_0 : 3; // Bits 2..0 of a standard identifier |
Just4pLeisure | 0:f5d099885d3d | 62 | uint8_t eid15_8 : 8; // Bits 15..8 of an extended identifier |
Just4pLeisure | 0:f5d099885d3d | 63 | uint8_t eid7_0 : 8; // Bits 7..0 of an extended identifier |
Just4pLeisure | 0:f5d099885d3d | 64 | }; |
Just4pLeisure | 0:f5d099885d3d | 65 | typedef struct MCP_CANid CANid; |
Just4pLeisure | 0:f5d099885d3d | 66 | |
Just4pLeisure | 1:ad71faa09868 | 67 | /// Type definition to hold an MCP2515 CAN id structure |
Just4pLeisure | 0:f5d099885d3d | 68 | struct MCP_CANMsg { |
Just4pLeisure | 0:f5d099885d3d | 69 | CANid id; |
Just4pLeisure | 0:f5d099885d3d | 70 | uint8_t dlc : 4; // Bits 3..0: DLC - Data Length Counter |
Just4pLeisure | 0:f5d099885d3d | 71 | uint8_t reserved1 : 2; // unused bits in RXBnDLC |
Just4pLeisure | 0:f5d099885d3d | 72 | uint8_t ertr : 1; // Extended Frame Remote Transmit Request Bit (valid when IDE = 1) |
Just4pLeisure | 0:f5d099885d3d | 73 | // CANtype 'type' |
Just4pLeisure | 0:f5d099885d3d | 74 | uint8_t reserved2 : 1; // unused bit in RXBnDLC |
Just4pLeisure | 0:f5d099885d3d | 75 | uint8_t data[8]; // CAN Message Data Bytes 0-7 |
Just4pLeisure | 0:f5d099885d3d | 76 | }; |
Just4pLeisure | 0:f5d099885d3d | 77 | typedef struct MCP_CANMsg CANMsg; |
Just4pLeisure | 0:f5d099885d3d | 78 | |
Just4pLeisure | 2:fd026fcfde94 | 79 | #ifndef MBED_CAN_HELPER_H // These are already defined for some mbed family members |
Just4pLeisure | 0:f5d099885d3d | 80 | enum CANFormat { |
Just4pLeisure | 0:f5d099885d3d | 81 | CANStandard = 0, |
Just4pLeisure | 0:f5d099885d3d | 82 | CANExtended = 1 |
Just4pLeisure | 0:f5d099885d3d | 83 | }; |
Just4pLeisure | 0:f5d099885d3d | 84 | typedef enum CANFormat CANFormat; |
Just4pLeisure | 0:f5d099885d3d | 85 | |
Just4pLeisure | 0:f5d099885d3d | 86 | enum CANType { |
Just4pLeisure | 0:f5d099885d3d | 87 | CANData = 0, |
Just4pLeisure | 0:f5d099885d3d | 88 | CANRemote = 1 |
Just4pLeisure | 0:f5d099885d3d | 89 | }; |
Just4pLeisure | 0:f5d099885d3d | 90 | typedef enum CANType CANType; |
Just4pLeisure | 0:f5d099885d3d | 91 | |
Just4pLeisure | 0:f5d099885d3d | 92 | struct CAN_Message { |
Just4pLeisure | 0:f5d099885d3d | 93 | unsigned int id; // 11 or 29 bit identifier |
Just4pLeisure | 0:f5d099885d3d | 94 | unsigned char data[8]; // Data field |
Just4pLeisure | 0:f5d099885d3d | 95 | unsigned char len; // Length of data field in bytes |
Just4pLeisure | 0:f5d099885d3d | 96 | CANFormat format; // 0 - STANDARD, 1- EXTENDED IDENTIFIER |
Just4pLeisure | 0:f5d099885d3d | 97 | CANType type; // 0 - DATA FRAME, 1 - REMOTE FRAME |
Just4pLeisure | 0:f5d099885d3d | 98 | }; |
Just4pLeisure | 0:f5d099885d3d | 99 | typedef struct CAN_Message CAN_Message; |
Just4pLeisure | 2:fd026fcfde94 | 100 | #endif |
Just4pLeisure | 0:f5d099885d3d | 101 | |
Just4pLeisure | 0:f5d099885d3d | 102 | enum MCP_Mode { |
Just4pLeisure | 2:fd026fcfde94 | 103 | _M_NORMAL, |
Just4pLeisure | 2:fd026fcfde94 | 104 | _M_SLEEP, |
Just4pLeisure | 2:fd026fcfde94 | 105 | _M_LOOPBACK, |
Just4pLeisure | 2:fd026fcfde94 | 106 | _M_MONITOR, |
Just4pLeisure | 2:fd026fcfde94 | 107 | _M_CONFIG, |
Just4pLeisure | 2:fd026fcfde94 | 108 | _M_RESET, |
Just4pLeisure | 0:f5d099885d3d | 109 | }; |
Just4pLeisure | 0:f5d099885d3d | 110 | typedef MCP_Mode CANMode; |
Just4pLeisure | 0:f5d099885d3d | 111 | |
Just4pLeisure | 2:fd026fcfde94 | 112 | enum MCP_Error_Flags { |
Just4pLeisure | 2:fd026fcfde94 | 113 | _E_ALL, |
Just4pLeisure | 2:fd026fcfde94 | 114 | _E_ERRORS, |
Just4pLeisure | 2:fd026fcfde94 | 115 | _E_WARNINGS, |
Just4pLeisure | 2:fd026fcfde94 | 116 | _E_RX1OVR, |
Just4pLeisure | 2:fd026fcfde94 | 117 | _E_RX0OVR, |
Just4pLeisure | 2:fd026fcfde94 | 118 | _E_TXBO, |
Just4pLeisure | 2:fd026fcfde94 | 119 | _E_TXEP, |
Just4pLeisure | 2:fd026fcfde94 | 120 | _E_RXEP, |
Just4pLeisure | 2:fd026fcfde94 | 121 | _E_TXWAR, |
Just4pLeisure | 2:fd026fcfde94 | 122 | _E_RXWAR, |
Just4pLeisure | 2:fd026fcfde94 | 123 | _E_EWARN |
Just4pLeisure | 2:fd026fcfde94 | 124 | }; |
Just4pLeisure | 2:fd026fcfde94 | 125 | typedef MCP_Error_Flags CANFlags; |
Just4pLeisure | 2:fd026fcfde94 | 126 | |
Just4pLeisure | 2:fd026fcfde94 | 127 | enum MCP_Interrupt_Flags { |
Just4pLeisure | 2:fd026fcfde94 | 128 | _I_NONE, |
Just4pLeisure | 2:fd026fcfde94 | 129 | _I_ANY, |
Just4pLeisure | 2:fd026fcfde94 | 130 | _I_RX, |
Just4pLeisure | 2:fd026fcfde94 | 131 | _I_TX, |
Just4pLeisure | 2:fd026fcfde94 | 132 | _I_RX0, |
Just4pLeisure | 2:fd026fcfde94 | 133 | _I_RX1, |
Just4pLeisure | 2:fd026fcfde94 | 134 | _I_TX0, |
Just4pLeisure | 2:fd026fcfde94 | 135 | _I_TX1, |
Just4pLeisure | 2:fd026fcfde94 | 136 | _I_TX2, |
Just4pLeisure | 2:fd026fcfde94 | 137 | _I_ERROR, |
Just4pLeisure | 2:fd026fcfde94 | 138 | _I_WAKE, |
Just4pLeisure | 2:fd026fcfde94 | 139 | _I_M_ERR |
Just4pLeisure | 2:fd026fcfde94 | 140 | }; |
Just4pLeisure | 2:fd026fcfde94 | 141 | typedef MCP_Interrupt_Flags CANIrqs; |
Just4pLeisure | 2:fd026fcfde94 | 142 | |
Just4pLeisure | 0:f5d099885d3d | 143 | /** CAN driver functions |
Just4pLeisure | 0:f5d099885d3d | 144 | */ |
Just4pLeisure | 2:fd026fcfde94 | 145 | uint8_t mcpInit(mcp_can_t *obj, // Initialise the MCP2515 and set the bit rate |
Just4pLeisure | 2:fd026fcfde94 | 146 | const uint32_t bitRate, |
Just4pLeisure | 2:fd026fcfde94 | 147 | const CANMode mode); |
Just4pLeisure | 2:fd026fcfde94 | 148 | uint8_t mcpSetMode(mcp_can_t *obj, const uint8_t newmode); // set the MCP2515's operation mode |
Just4pLeisure | 2:fd026fcfde94 | 149 | uint8_t mcpSetBitRate(mcp_can_t *obj, const uint32_t bitRate); // set bitrate |
Just4pLeisure | 0:f5d099885d3d | 150 | |
Just4pLeisure | 2:fd026fcfde94 | 151 | void mcpWriteId(mcp_can_t *obj, // write a CAN id |
Just4pLeisure | 0:f5d099885d3d | 152 | const uint8_t mcp_addr, |
Just4pLeisure | 0:f5d099885d3d | 153 | const uint8_t ext, |
Just4pLeisure | 0:f5d099885d3d | 154 | const uint32_t id ); |
Just4pLeisure | 2:fd026fcfde94 | 155 | uint8_t mcpCanRead(mcp_can_t *obj, CAN_Message *msg); // read a CAN message |
Just4pLeisure | 2:fd026fcfde94 | 156 | uint8_t mcpCanWrite(mcp_can_t *obj, CAN_Message msg); // write a CAN message |
Just4pLeisure | 0:f5d099885d3d | 157 | |
Just4pLeisure | 2:fd026fcfde94 | 158 | uint8_t mcpInitMask(mcp_can_t *obj, // initialise an Acceptance Mask |
Just4pLeisure | 2:fd026fcfde94 | 159 | uint8_t num, |
Just4pLeisure | 2:fd026fcfde94 | 160 | uint32_t ulData, |
Just4pLeisure | 2:fd026fcfde94 | 161 | bool ext); |
Just4pLeisure | 2:fd026fcfde94 | 162 | uint8_t mcpInitFilter(mcp_can_t *obj, // initialise an Acceptance Filter |
Just4pLeisure | 2:fd026fcfde94 | 163 | uint8_t num, |
Just4pLeisure | 2:fd026fcfde94 | 164 | uint32_t ulData, |
Just4pLeisure | 2:fd026fcfde94 | 165 | bool ext); |
Just4pLeisure | 0:f5d099885d3d | 166 | |
Just4pLeisure | 2:fd026fcfde94 | 167 | uint8_t mcpErrorType(mcp_can_t *obj, const CANFlags type); // Report on the specified errors and warnings |
Just4pLeisure | 2:fd026fcfde94 | 168 | uint8_t mcpErrorFlags(mcp_can_t *obj); // Return contents of the error and warning flags register |
Just4pLeisure | 2:fd026fcfde94 | 169 | uint8_t mcpReceptionErrorCount(mcp_can_t *obj); // number of message reception errors |
Just4pLeisure | 2:fd026fcfde94 | 170 | uint8_t mcpTransmissionErrorCount(mcp_can_t *obj); // number of message transmission errors |
Just4pLeisure | 0:f5d099885d3d | 171 | |
Just4pLeisure | 2:fd026fcfde94 | 172 | void mcpMonitor(mcp_can_t *obj, const bool silent); // Select between monitor (silent = 1) and normal (silent = 0) modes |
Just4pLeisure | 2:fd026fcfde94 | 173 | uint8_t mcpMode(mcp_can_t *obj, const CANMode mode); // Change CAN operation to the specified mode |
Just4pLeisure | 0:f5d099885d3d | 174 | |
Just4pLeisure | 2:fd026fcfde94 | 175 | void mcpSetInterrupts(mcp_can_t *obj, const CANIrqs irqSet); // Configure interrupt sources |
Just4pLeisure | 2:fd026fcfde94 | 176 | uint8_t mcpInterruptType(mcp_can_t *obj, const CANIrqs irqFlag); // Report on the specified interrupt causes |
Just4pLeisure | 2:fd026fcfde94 | 177 | uint8_t mcpInterruptFlags(mcp_can_t *obj); // Return contents of the interrupt flags register |
Just4pLeisure | 2:fd026fcfde94 | 178 | |
Just4pLeisure | 0:f5d099885d3d | 179 | #ifdef __cplusplus |
Just4pLeisure | 0:f5d099885d3d | 180 | }; |
Just4pLeisure | 0:f5d099885d3d | 181 | #endif |
Just4pLeisure | 0:f5d099885d3d | 182 | |
Just4pLeisure | 0:f5d099885d3d | 183 | #endif // SEEED_CAN_API_H |