Seed can implementation
Fork of SEEED_CAN by
seeed_can_api.h@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.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 | 0:f5d099885d3d | 79 | enum CANFormat { |
Just4pLeisure | 0:f5d099885d3d | 80 | CANStandard = 0, |
Just4pLeisure | 0:f5d099885d3d | 81 | CANExtended = 1 |
Just4pLeisure | 0:f5d099885d3d | 82 | }; |
Just4pLeisure | 0:f5d099885d3d | 83 | typedef enum CANFormat CANFormat; |
Just4pLeisure | 0:f5d099885d3d | 84 | |
Just4pLeisure | 0:f5d099885d3d | 85 | enum CANType { |
Just4pLeisure | 0:f5d099885d3d | 86 | CANData = 0, |
Just4pLeisure | 0:f5d099885d3d | 87 | CANRemote = 1 |
Just4pLeisure | 0:f5d099885d3d | 88 | }; |
Just4pLeisure | 0:f5d099885d3d | 89 | typedef enum CANType CANType; |
Just4pLeisure | 0:f5d099885d3d | 90 | |
Just4pLeisure | 0:f5d099885d3d | 91 | struct CAN_Message { |
Just4pLeisure | 0:f5d099885d3d | 92 | unsigned int id; // 11 or 29 bit identifier |
Just4pLeisure | 0:f5d099885d3d | 93 | unsigned char data[8]; // Data field |
Just4pLeisure | 0:f5d099885d3d | 94 | unsigned char len; // Length of data field in bytes |
Just4pLeisure | 0:f5d099885d3d | 95 | CANFormat format; // 0 - STANDARD, 1- EXTENDED IDENTIFIER |
Just4pLeisure | 0:f5d099885d3d | 96 | CANType type; // 0 - DATA FRAME, 1 - REMOTE FRAME |
Just4pLeisure | 0:f5d099885d3d | 97 | }; |
Just4pLeisure | 0:f5d099885d3d | 98 | typedef struct CAN_Message CAN_Message; |
Just4pLeisure | 0:f5d099885d3d | 99 | |
Just4pLeisure | 0:f5d099885d3d | 100 | enum MCP_Mode { |
Just4pLeisure | 0:f5d099885d3d | 101 | _NORMAL, |
Just4pLeisure | 0:f5d099885d3d | 102 | _SLEEP, |
Just4pLeisure | 0:f5d099885d3d | 103 | _LOOPBACK, |
Just4pLeisure | 0:f5d099885d3d | 104 | _MONITOR, |
Just4pLeisure | 0:f5d099885d3d | 105 | _CONFIG, |
Just4pLeisure | 0:f5d099885d3d | 106 | _RESET, |
Just4pLeisure | 0:f5d099885d3d | 107 | }; |
Just4pLeisure | 0:f5d099885d3d | 108 | typedef MCP_Mode CANMode; |
Just4pLeisure | 0:f5d099885d3d | 109 | |
Just4pLeisure | 0:f5d099885d3d | 110 | /** CAN driver functions |
Just4pLeisure | 0:f5d099885d3d | 111 | */ |
Just4pLeisure | 0:f5d099885d3d | 112 | uint8_t mcpInit(can_t *obj, const uint32_t bitRate); // Initialise the MCP2515 and set the bit rate |
Just4pLeisure | 0:f5d099885d3d | 113 | uint8_t mcpSetMode(can_t *obj, const uint8_t newmode); // set the MCP2515's operation mode |
Just4pLeisure | 0:f5d099885d3d | 114 | uint8_t mcpSetBitRate(can_t *obj, const uint32_t bitRate); // set bitrate |
Just4pLeisure | 0:f5d099885d3d | 115 | |
Just4pLeisure | 0:f5d099885d3d | 116 | void mcpWriteId(can_t *obj, // write a CAN id |
Just4pLeisure | 0:f5d099885d3d | 117 | const uint8_t mcp_addr, |
Just4pLeisure | 0:f5d099885d3d | 118 | const uint8_t ext, |
Just4pLeisure | 0:f5d099885d3d | 119 | const uint32_t id ); |
Just4pLeisure | 0:f5d099885d3d | 120 | uint8_t mcpCanRead(can_t *obj, CAN_Message *msg); // read a CAN message |
Just4pLeisure | 0:f5d099885d3d | 121 | uint8_t mcpCanWrite(can_t *obj, CAN_Message msg); // write a CAN message |
Just4pLeisure | 0:f5d099885d3d | 122 | |
Just4pLeisure | 0:f5d099885d3d | 123 | uint8_t mcpInitMask(can_t *obj, uint8_t num, uint32_t ulData, bool ext); // initialise an Acceptance Mask |
Just4pLeisure | 0:f5d099885d3d | 124 | uint8_t mcpInitFilter(can_t *obj, uint8_t num, uint32_t ulData, bool ext); // initialise an Acceptance Filter |
Just4pLeisure | 0:f5d099885d3d | 125 | |
Just4pLeisure | 0:f5d099885d3d | 126 | uint8_t mcpReceptionErrorCount(can_t *obj); // number of message reception errors |
Just4pLeisure | 0:f5d099885d3d | 127 | uint8_t mcpTransmissionErrorCount(can_t *obj); // number of message transmission errors |
Just4pLeisure | 0:f5d099885d3d | 128 | |
Just4pLeisure | 0:f5d099885d3d | 129 | void mcpMonitor(can_t *obj, const bool silent); // Select between monitor (silent = 1) and normal (silent = 0) modes |
Just4pLeisure | 0:f5d099885d3d | 130 | uint8_t mcpMode(can_t *obj, const CANMode mode); // Change CAN operation to the specified mode |
Just4pLeisure | 0:f5d099885d3d | 131 | |
Just4pLeisure | 0:f5d099885d3d | 132 | #ifdef __cplusplus |
Just4pLeisure | 0:f5d099885d3d | 133 | }; |
Just4pLeisure | 0:f5d099885d3d | 134 | #endif |
Just4pLeisure | 0:f5d099885d3d | 135 | |
Just4pLeisure | 0:f5d099885d3d | 136 | #endif // SEEED_CAN_API_H |