Dependents:   Kamal_CAN ReadFromSerial446

Committer:
tecnosys
Date:
Mon Oct 18 13:40:02 2010 +0000
Revision:
8:872137b3a8a8
Parent:
1:dbc44582f2f8
0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tecnosys 0:d8f50b1e384f 1
tecnosys 0:d8f50b1e384f 2 #ifndef _MCP2515_H_
tecnosys 0:d8f50b1e384f 3 #define _MCP2515_H_
tecnosys 0:d8f50b1e384f 4
tecnosys 0:d8f50b1e384f 5 #include <mbed.h>
tecnosys 0:d8f50b1e384f 6 #include "mcp2515_defs.h"
tecnosys 0:d8f50b1e384f 7
tecnosys 0:d8f50b1e384f 8 #define MCPDEBUG (1)
tecnosys 0:d8f50b1e384f 9 #define MCPDEBUG_TXBUF (0)
tecnosys 0:d8f50b1e384f 10
tecnosys 0:d8f50b1e384f 11 #define MCP_CS_PORT PORTB
tecnosys 0:d8f50b1e384f 12 #define MCP_CS_DDR DDRB
tecnosys 0:d8f50b1e384f 13 #define MCP_CS_BIT PB0
tecnosys 0:d8f50b1e384f 14
tecnosys 0:d8f50b1e384f 15 #define MCP_N_TXBUFFERS (3)
tecnosys 0:d8f50b1e384f 16
tecnosys 0:d8f50b1e384f 17 #define MCP_RXBUF_0 (MCP_RXB0SIDH)
tecnosys 0:d8f50b1e384f 18 #define MCP_RXBUF_1 (MCP_RXB1SIDH)
tecnosys 0:d8f50b1e384f 19
tecnosys 0:d8f50b1e384f 20 #define MCP_TXBUF_0 (MCP_TXB0SIDH)
tecnosys 0:d8f50b1e384f 21 #define MCP_TXBUF_1 (MCP_TXB1SIDH)
tecnosys 0:d8f50b1e384f 22 #define MCP_TXBUF_2 (MCP_TXB2SIDH)
tecnosys 0:d8f50b1e384f 23
tecnosys 0:d8f50b1e384f 24 // #define MCP2515_SELECT() (SPI_SS_LOW())
tecnosys 0:d8f50b1e384f 25 // #define MCP2515_UNSELECT() (SPI_SS_HIGH())
tecnosys 0:d8f50b1e384f 26
tecnosys 0:d8f50b1e384f 27 #define MCP2515_SELECT() ( MCP_CS_PORT &= ~(1<<MCP_CS_BIT) )
tecnosys 0:d8f50b1e384f 28 #define MCP2515_UNSELECT() ( MCP_CS_PORT |= (1<<MCP_CS_BIT) )
tecnosys 0:d8f50b1e384f 29
tecnosys 0:d8f50b1e384f 30 #define MCP2515_OK (0)
tecnosys 0:d8f50b1e384f 31 #define MCP2515_FAIL (1)
tecnosys 0:d8f50b1e384f 32 #define MCP_ALLTXBUSY (2)
tecnosys 0:d8f50b1e384f 33
tecnosys 0:d8f50b1e384f 34 typedef unsigned char u8;
tecnosys 0:d8f50b1e384f 35
tecnosys 0:d8f50b1e384f 36 // Connections to the chip
tecnosys 0:d8f50b1e384f 37 //volatile bit mcp2515_rst @ PORTC . 1;
tecnosys 0:d8f50b1e384f 38 //volatile bit mcp2515_rst_tris @ TRISC . 1;
tecnosys 0:d8f50b1e384f 39
tecnosys 0:d8f50b1e384f 40 // Function declarations
tecnosys 0:d8f50b1e384f 41 /*
tecnosys 0:d8f50b1e384f 42 void can_reset();
tecnosys 0:d8f50b1e384f 43 char can_read_register(char reg);
tecnosys 0:d8f50b1e384f 44 void can_write_bits(char address, char data, char mask);
tecnosys 0:d8f50b1e384f 45 void can_init(short id);
tecnosys 0:d8f50b1e384f 46 char can_peek_message();
tecnosys 0:d8f50b1e384f 47 char can_rx_byte();
tecnosys 1:dbc44582f2f8 48 void can_set_id(short id);
tecnosys 0:d8f50b1e384f 49 void can_init_buffer();
tecnosys 0:d8f50b1e384f 50 void can_load_byte(char input);
tecnosys 0:d8f50b1e384f 51 void can_tx_buffer();
tecnosys 0:d8f50b1e384f 52 void can_read_status();
tecnosys 0:d8f50b1e384f 53 */
tecnosys 0:d8f50b1e384f 54
tecnosys 0:d8f50b1e384f 55 #include "mbed.h"
tecnosys 0:d8f50b1e384f 56 #include "SPI.h"
tecnosys 1:dbc44582f2f8 57 class mcp2515 {
tecnosys 1:dbc44582f2f8 58 SPI& spi;
tecnosys 1:dbc44582f2f8 59 DigitalOut _ncs;
tecnosys 1:dbc44582f2f8 60 //Serial& console =0;
tecnosys 1:dbc44582f2f8 61 bool debug;
tecnosys 1:dbc44582f2f8 62 public:
tecnosys 1:dbc44582f2f8 63 // mcp2515(PinName mosi, PinName miso, PinName clk, PinName ncs);
tecnosys 1:dbc44582f2f8 64 mcp2515(SPI& _spi, PinName ncs);
tecnosys 1:dbc44582f2f8 65 // void setInterface(SPI& spi);
tecnosys 1:dbc44582f2f8 66 // float read (void);
tecnosys 1:dbc44582f2f8 67 // void setThigh (float);
tecnosys 1:dbc44582f2f8 68 // void setTlow (float);
tecnosys 1:dbc44582f2f8 69 // float getThigh (void);
tecnosys 1:dbc44582f2f8 70 // float getTlow (void);
tecnosys 1:dbc44582f2f8 71 void _reset();
tecnosys 1:dbc44582f2f8 72 uint8_t configRate( uint8_t canSpeed);
tecnosys 1:dbc44582f2f8 73 void setRegister( uint8_t address, uint8_t value);
tecnosys 1:dbc44582f2f8 74
tecnosys 1:dbc44582f2f8 75 uint8_t readRegister( uint8_t address);
tecnosys 1:dbc44582f2f8 76 void modifyRegister( uint8_t address, uint8_t mask, uint8_t data);
tecnosys 1:dbc44582f2f8 77 uint8_t setCANCTRL_Mode(uint8_t newmode);
tecnosys 1:dbc44582f2f8 78 uint8_t init( int canSpeed);
tecnosys 1:dbc44582f2f8 79 void initCANBuffers(void);
tecnosys 1:dbc44582f2f8 80 void setRegisterS( uint8_t address, uint8_t values[], uint8_t n);
tecnosys 1:dbc44582f2f8 81 void write_can_id( uint8_t mcp_addr, uint8_t ext, uint32_t can_id );
tecnosys 1:dbc44582f2f8 82 //
tecnosys 0:d8f50b1e384f 83
tecnosys 1:dbc44582f2f8 84 void readRegisterS( uint8_t address, uint8_t values[], uint8_t n);
tecnosys 1:dbc44582f2f8 85 uint8_t readXXStatus_helper( uint8_t cmd);
tecnosys 1:dbc44582f2f8 86 uint8_t readStatus(void);
tecnosys 1:dbc44582f2f8 87 uint8_t RXStatus(void);
tecnosys 1:dbc44582f2f8 88
tecnosys 1:dbc44582f2f8 89 void read_can_id( uint8_t mcp_addr, uint8_t* ext, uint32_t* can_id );
tecnosys 1:dbc44582f2f8 90 void read_can_idN( uint8_t mcp_addr, CANFormat* ext, unsigned int* can_id );
tecnosys 1:dbc44582f2f8 91 void read_canMsg( uint8_t buffer_sidh_addr, CANMessage* msg);
tecnosys 1:dbc44582f2f8 92 void write_canMsg( uint8_t buffer_sidh_addr, CANMessage* msg);
tecnosys 1:dbc44582f2f8 93
tecnosys 1:dbc44582f2f8 94 uint8_t getNextFreeTXBuf(uint8_t *txbuf_n);
tecnosys 1:dbc44582f2f8 95
tecnosys 1:dbc44582f2f8 96 void start_transmit( uint8_t buffer_sidh_addr);
tecnosys 1:dbc44582f2f8 97
tecnosys 1:dbc44582f2f8 98 void dumpExtendedStatus(void);
tecnosys 1:dbc44582f2f8 99
tecnosys 1:dbc44582f2f8 100 void setDebugConsole(Serial c);
tecnosys 0:d8f50b1e384f 101
tecnosys 1:dbc44582f2f8 102 int configRate2(int bit_rate);
tecnosys 1:dbc44582f2f8 103
tecnosys 1:dbc44582f2f8 104 // Private functions
tecnosys 1:dbc44582f2f8 105 private :
tecnosys 1:dbc44582f2f8 106 void _select (void);
tecnosys 1:dbc44582f2f8 107 void _deselect (void);
tecnosys 1:dbc44582f2f8 108 int status();
tecnosys 1:dbc44582f2f8 109 void _pollbusy() ;
tecnosys 1:dbc44582f2f8 110
tecnosys 1:dbc44582f2f8 111 uint8_t _spi_readwrite(uint8_t data);
tecnosys 1:dbc44582f2f8 112 uint8_t _spi_read(void);
tecnosys 1:dbc44582f2f8 113
tecnosys 1:dbc44582f2f8 114
tecnosys 1:dbc44582f2f8 115 };
tecnosys 0:d8f50b1e384f 116
tecnosys 0:d8f50b1e384f 117 #endif // _MCP2515_H_