eeprom adding

Fork of SEEED_CAN by Sophie Dexter

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers seeed_can_api.h Source File

seeed_can_api.h

00001 /* seeed_can_api.h
00002  * Copyright (c) 2013 Sophie Dexter
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef _SEEED_CAN_API_H_
00017 #define _SEEED_CAN_API_H_
00018 
00019 #include "seeed_can_spi.h"
00020 
00021 // print debug information...
00022 //#define DEBUG
00023 
00024 #ifdef __cplusplus
00025 extern "C" {
00026 #endif
00027 
00028 /** CAN driver typedefs
00029  */
00030 
00031 // The 'MCP_xyz' structs mimic MCP register organisations
00032 // A union of the struct and a simple array can be used to:
00033 //  Process and manipulate the struct in an ordered way
00034 //  Copy the struct to/from the MCP2512 as an array
00035 
00036 /// Type definition to hold an MCP2515 Timing Register set structure
00037     struct MCP_CANtiming {
00038         uint8_t phseg2    : 3;      // PS2 length bits 2..0 (PHSEG2 + 1) Tq (minimum valid setting is 2 Tq)
00039         uint8_t reserved1 : 3;      // Unused bits (read as '0')
00040         uint8_t wakfil    : 1;      // Wake-up filter enable bit (1 = enabled, 0 = disabled)
00041         uint8_t sof       : 1;      // Start of frame bit (1 = CLKOUT pin is SOF, 0 = CLKOUT is clock)
00042         uint8_t prseg     : 3;      // Propagation Segment length bits 2..0 (PRSEG + 1) Tq (minimum valid setting is 1 Tq)
00043         uint8_t phseg1    : 3;      // PS2 length bits 2..0 (PRSEG + 1) Tq (minimum valid setting is 1 Tq)
00044         uint8_t sam       : 1;      // Sample Point Configuration bit (1 = Bus line is sampled 3 times, 0 = sampled once)
00045         uint8_t btlmode   : 1;      // PS2 Bit Time Length (1 = determined by phseg2, 0 = greater of PS1 an IPT (2 Tq))
00046         uint8_t brp       : 6;      // Baud Rate Prescaler bits 5..0 (Tq = 2 x (BRP + 1) / Fosc)
00047         uint8_t sjw       : 2;      // Synchronisation Jump Width Length bits 1..0 ((SJW + 1) Tq)
00048     };
00049     typedef struct MCP_CANtiming CANtiming;
00050 
00051 /// Type definition to hold an MCP2515 CAN id structure
00052     struct MCP_CANid {
00053         uint8_t sid10_3   : 8;      // Bits 10..3 of a standard identifier
00054         uint8_t eid17_16  : 2;      // Bits 17..16 of an extended identifier
00055         uint8_t reserved1 : 1;      // Unused bit in RXBbSIDL
00056         uint8_t ide       : 1;      // Extended Identifier Flag
00057         // CANformat 'type'
00058         uint8_t srtr      : 1;      // Standard Frame Remote Transmit Request Bit (valid when IDE = 0)
00059         // CANtype 'type'
00060         // Unimplimented  in TXBnSIDL, filters and acceptance masks
00061         uint8_t sid2_0    : 3;      // Bits 2..0 of a standard identifier
00062         uint8_t eid15_8   : 8;      // Bits 15..8 of an extended identifier
00063         uint8_t eid7_0    : 8;      // Bits 7..0 of an extended identifier
00064     };
00065     typedef struct MCP_CANid CANid;
00066 
00067 /// Type definition to hold an MCP2515 CAN id structure
00068     struct MCP_CANMsg {
00069         CANid id;
00070         uint8_t dlc       : 4;      // Bits 3..0: DLC - Data Length Counter
00071         uint8_t reserved1 : 2;      // unused bits in RXBnDLC
00072         uint8_t ertr      : 1;      // Extended Frame Remote Transmit Request Bit (valid when IDE = 1)
00073         // CANtype 'type'
00074         uint8_t reserved2 : 1;      // unused bit in RXBnDLC
00075         uint8_t data[8];            // CAN Message Data Bytes 0-7
00076     };
00077     typedef struct MCP_CANMsg CANMsg;
00078 
00079 #ifndef MBED_CAN_HELPER_H                                               // These are already defined for some mbed family members
00080     enum CANFormat {
00081         CANStandard = 0,
00082         CANExtended = 1
00083     };
00084     typedef enum CANFormat CANFormat;
00085 
00086     enum CANType {
00087         CANData   = 0,
00088         CANRemote = 1
00089     };
00090     typedef enum CANType CANType;
00091 
00092     struct CAN_Message {
00093         unsigned int   id;          // 11 or 29 bit identifier
00094         unsigned char  data[8];     // Data field
00095         unsigned char  len;         // Length of data field in bytes
00096         CANFormat      format;      // 0 - STANDARD, 1- EXTENDED IDENTIFIER
00097         CANType        type;        // 0 - DATA FRAME, 1 - REMOTE FRAME
00098     };
00099     typedef struct CAN_Message CAN_Message;
00100 #endif
00101     
00102     enum MCP_Mode {
00103         _M_NORMAL,
00104         _M_SLEEP,
00105         _M_LOOPBACK,
00106         _M_MONITOR,
00107         _M_CONFIG,
00108         _M_RESET,
00109     };
00110     typedef MCP_Mode CANMode;
00111 
00112     enum MCP_Error_Flags {
00113         _E_ALL,
00114         _E_ERRORS,
00115         _E_WARNINGS,
00116         _E_RX1OVR,
00117         _E_RX0OVR,
00118         _E_TXBO,
00119         _E_TXEP,
00120         _E_RXEP,
00121         _E_TXWAR,
00122         _E_RXWAR,
00123         _E_EWARN
00124     };
00125     typedef MCP_Error_Flags CANFlags;
00126 
00127     enum MCP_Interrupt_Flags {
00128         _I_NONE,
00129         _I_ANY,
00130         _I_RX,
00131         _I_TX,
00132         _I_RX0,
00133         _I_RX1,
00134         _I_TX0,
00135         _I_TX1,
00136         _I_TX2,
00137         _I_ERROR,
00138         _I_WAKE,
00139         _I_M_ERR
00140     };
00141     typedef MCP_Interrupt_Flags CANIrqs;
00142 
00143 /** CAN driver functions
00144  */
00145     uint8_t mcpInit(mcp_can_t *obj,                                     // Initialise the MCP2515 and set the bit rate
00146                     const uint32_t bitRate,
00147                     const CANMode mode);
00148     uint8_t mcpSetMode(mcp_can_t *obj, const uint8_t newmode);          // set the MCP2515's operation mode
00149     uint8_t mcpSetBitRate(mcp_can_t *obj, const uint32_t bitRate);      // set bitrate
00150     
00151     void mcpWriteId(mcp_can_t *obj,                                     // write a CAN id
00152                     const uint8_t mcp_addr,
00153                     const uint8_t ext,
00154                     const uint32_t id );
00155     uint8_t mcpCanRead(mcp_can_t *obj, CAN_Message *msg);               // read a CAN message
00156     uint8_t mcpCanWrite(mcp_can_t *obj, CAN_Message msg);               // write a CAN message
00157     
00158     uint8_t mcpInitMask(mcp_can_t *obj,                                 // initialise an Acceptance Mask
00159                         uint8_t num,
00160                         uint32_t ulData,
00161                         bool ext);
00162     uint8_t mcpInitFilter(mcp_can_t *obj,                               // initialise an Acceptance Filter
00163                           uint8_t num,
00164                           uint32_t ulData,
00165                           bool ext);
00166 
00167     uint8_t mcpErrorType(mcp_can_t *obj, const CANFlags type);          // Report on the specified errors and warnings
00168     uint8_t mcpErrorFlags(mcp_can_t *obj);                              // Return contents of the error and warning flags register
00169     uint8_t mcpReceptionErrorCount(mcp_can_t *obj);                     // number of message reception errors
00170     uint8_t mcpTransmissionErrorCount(mcp_can_t *obj);                  // number of message transmission errors
00171 
00172     void mcpMonitor(mcp_can_t *obj, const bool silent);                 // Select between monitor (silent = 1) and normal (silent = 0) modes
00173     uint8_t mcpMode(mcp_can_t *obj, const CANMode mode);                // Change CAN operation to the specified mode
00174 
00175     void mcpSetInterrupts(mcp_can_t *obj, const CANIrqs irqSet);        // Configure interrupt sources
00176     uint8_t mcpInterruptType(mcp_can_t *obj, const CANIrqs irqFlag);    // Report on the specified interrupt causes
00177     uint8_t mcpInterruptFlags(mcp_can_t *obj);                          // Return contents of the interrupt flags register
00178     
00179 #ifdef __cplusplus
00180 };
00181 #endif
00182 
00183 #endif    // SEEED_CAN_API_H