Dependents:   Kamal_CAN ReadFromSerial446

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mcp2515.h Source File

mcp2515.h

00001 
00002 #ifndef _MCP2515_H_
00003 #define _MCP2515_H_
00004 
00005 #include <mbed.h>
00006 #include "mcp2515_defs.h"
00007 
00008 #define MCPDEBUG (1)
00009 #define MCPDEBUG_TXBUF (0)
00010 
00011 #define MCP_CS_PORT PORTB
00012 #define MCP_CS_DDR  DDRB
00013 #define MCP_CS_BIT  PB0
00014 
00015 #define MCP_N_TXBUFFERS (3)
00016 
00017 #define MCP_RXBUF_0 (MCP_RXB0SIDH)
00018 #define MCP_RXBUF_1 (MCP_RXB1SIDH)
00019 
00020 #define MCP_TXBUF_0 (MCP_TXB0SIDH)
00021 #define MCP_TXBUF_1 (MCP_TXB1SIDH)
00022 #define MCP_TXBUF_2 (MCP_TXB2SIDH)
00023 
00024 // #define MCP2515_SELECT() (SPI_SS_LOW())
00025 // #define MCP2515_UNSELECT() (SPI_SS_HIGH())
00026 
00027 #define MCP2515_SELECT()   ( MCP_CS_PORT &= ~(1<<MCP_CS_BIT) )
00028 #define MCP2515_UNSELECT() ( MCP_CS_PORT |=  (1<<MCP_CS_BIT) )
00029 
00030 #define MCP2515_OK         (0)
00031 #define MCP2515_FAIL       (1)
00032 #define MCP_ALLTXBUSY      (2)
00033 
00034 typedef unsigned char u8;
00035 
00036 // Connections to the chip
00037 //volatile bit mcp2515_rst @ PORTC . 1;
00038 //volatile bit mcp2515_rst_tris @ TRISC . 1;
00039 
00040 // Function declarations
00041 /*
00042 void can_reset();
00043 char can_read_register(char reg);
00044 void can_write_bits(char address, char data, char mask);
00045 void can_init(short id);
00046 char can_peek_message();
00047 char can_rx_byte();
00048 void can_set_id(short id);
00049 void can_init_buffer();
00050 void can_load_byte(char input);
00051 void can_tx_buffer();
00052 void can_read_status();
00053 */
00054 
00055 #include "mbed.h"
00056 #include "SPI.h"
00057 class mcp2515  {
00058    SPI& spi;
00059     DigitalOut _ncs;
00060     //Serial&  console =0;
00061     bool    debug;
00062 public:
00063     //  mcp2515(PinName mosi, PinName miso, PinName clk, PinName ncs);
00064     mcp2515(SPI& _spi, PinName ncs);
00065     // void setInterface(SPI& spi);
00066     // float read (void);
00067     // void setThigh (float);
00068     // void setTlow (float);
00069     // float getThigh (void);
00070     // float getTlow (void);
00071      void _reset();
00072     uint8_t configRate(  uint8_t canSpeed);
00073     void    setRegister(  uint8_t address,   uint8_t value);
00074     
00075     uint8_t readRegister(  uint8_t address);
00076     void    modifyRegister(  uint8_t address,   uint8_t mask,   uint8_t data);
00077     uint8_t setCANCTRL_Mode(uint8_t newmode);
00078     uint8_t init(  int canSpeed);
00079     void    initCANBuffers(void);
00080     void    setRegisterS(  uint8_t address,   uint8_t values[],   uint8_t n);
00081     void    write_can_id(   uint8_t mcp_addr,   uint8_t ext,   uint32_t can_id );
00082     //
00083 
00084     void    readRegisterS(  uint8_t address, uint8_t values[],   uint8_t n);
00085     uint8_t readXXStatus_helper(  uint8_t cmd);
00086     uint8_t readStatus(void);
00087     uint8_t RXStatus(void);
00088 
00089     void read_can_id(   uint8_t mcp_addr, uint8_t* ext, uint32_t* can_id );
00090     void read_can_idN(   uint8_t mcp_addr, CANFormat* ext, unsigned int* can_id );
00091     void read_canMsg(   uint8_t buffer_sidh_addr, CANMessage* msg);
00092     void write_canMsg(   uint8_t buffer_sidh_addr,  CANMessage* msg);
00093 
00094     uint8_t getNextFreeTXBuf(uint8_t *txbuf_n);
00095 
00096     void start_transmit(  uint8_t buffer_sidh_addr);
00097 
00098     void dumpExtendedStatus(void);
00099 
00100     void setDebugConsole(Serial c);
00101     
00102     int configRate2(int bit_rate);
00103  
00104     // Private functions
00105 private :
00106     void _select (void);
00107     void _deselect (void);
00108     int status();
00109     void _pollbusy() ;
00110 
00111     uint8_t _spi_readwrite(uint8_t data);
00112     uint8_t _spi_read(void);
00113 
00114 
00115 };
00116 
00117 #endif // _MCP2515_H_