MCP2515 CAN library
Fork of mcp2515 by
CAN3.cpp@1:dbc44582f2f8, 2010-06-13 (annotated)
- Committer:
- tecnosys
- Date:
- Sun Jun 13 09:52:03 2010 +0000
- Revision:
- 1:dbc44582f2f8
- Child:
- 7:2abb9b7eec25
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tecnosys | 1:dbc44582f2f8 | 1 | #include "CAN3.h" |
tecnosys | 1:dbc44582f2f8 | 2 | |
tecnosys | 1:dbc44582f2f8 | 3 | #include "mbed.h" |
tecnosys | 1:dbc44582f2f8 | 4 | #include "mcp2515.h" |
tecnosys | 1:dbc44582f2f8 | 5 | #include "mcp2515_can.h" |
tecnosys | 1:dbc44582f2f8 | 6 | #include "mcp2515_defs.h" |
tecnosys | 1:dbc44582f2f8 | 7 | |
tecnosys | 1:dbc44582f2f8 | 8 | |
tecnosys | 1:dbc44582f2f8 | 9 | CAN3::CAN3(SPI& _spi, PinName ncs, PinName itr) |
tecnosys | 1:dbc44582f2f8 | 10 | : spi(_spi), _mcp(spi, ncs), _itr(itr) { |
tecnosys | 1:dbc44582f2f8 | 11 | printf("\n\rcan = %d",this); |
tecnosys | 1:dbc44582f2f8 | 12 | |
tecnosys | 1:dbc44582f2f8 | 13 | } |
tecnosys | 1:dbc44582f2f8 | 14 | |
tecnosys | 1:dbc44582f2f8 | 15 | uint8_t CAN3::read(CANMessage *msg) { |
tecnosys | 1:dbc44582f2f8 | 16 | uint8_t stat, res; |
tecnosys | 1:dbc44582f2f8 | 17 | |
tecnosys | 1:dbc44582f2f8 | 18 | stat = _mcp.readStatus(); |
tecnosys | 1:dbc44582f2f8 | 19 | |
tecnosys | 1:dbc44582f2f8 | 20 | if ( stat & MCP_STAT_RX0IF ) { |
tecnosys | 1:dbc44582f2f8 | 21 | // Msg in Buffer 0 |
tecnosys | 1:dbc44582f2f8 | 22 | _mcp.read_canMsg( MCP_RXBUF_0, msg); |
tecnosys | 1:dbc44582f2f8 | 23 | _mcp.modifyRegister(MCP_CANINTF, MCP_RX0IF, 0); |
tecnosys | 1:dbc44582f2f8 | 24 | res = CAN_OK; |
tecnosys | 1:dbc44582f2f8 | 25 | } else if ( stat & MCP_STAT_RX1IF ) { |
tecnosys | 1:dbc44582f2f8 | 26 | // Msg in Buffer 1 |
tecnosys | 1:dbc44582f2f8 | 27 | _mcp.read_canMsg( MCP_RXBUF_1, msg); |
tecnosys | 1:dbc44582f2f8 | 28 | _mcp.modifyRegister(MCP_CANINTF, MCP_RX1IF, 0); |
tecnosys | 1:dbc44582f2f8 | 29 | res = CAN_OK; |
tecnosys | 1:dbc44582f2f8 | 30 | } else { |
tecnosys | 1:dbc44582f2f8 | 31 | res = CAN_NOMSG; |
tecnosys | 1:dbc44582f2f8 | 32 | } |
tecnosys | 1:dbc44582f2f8 | 33 | |
tecnosys | 1:dbc44582f2f8 | 34 | return res; |
tecnosys | 1:dbc44582f2f8 | 35 | } |
tecnosys | 1:dbc44582f2f8 | 36 | |
tecnosys | 1:dbc44582f2f8 | 37 | uint8_t CAN3::checkReceive(void) { |
tecnosys | 1:dbc44582f2f8 | 38 | uint8_t res; |
tecnosys | 1:dbc44582f2f8 | 39 | |
tecnosys | 1:dbc44582f2f8 | 40 | res = _mcp.readStatus(); // RXnIF in Bit 1 and 0 |
tecnosys | 1:dbc44582f2f8 | 41 | if ( res & MCP_STAT_RXIF_MASK ) { |
tecnosys | 1:dbc44582f2f8 | 42 | return CAN_MSGAVAIL; |
tecnosys | 1:dbc44582f2f8 | 43 | } else { |
tecnosys | 1:dbc44582f2f8 | 44 | return CAN_NOMSG; |
tecnosys | 1:dbc44582f2f8 | 45 | } |
tecnosys | 1:dbc44582f2f8 | 46 | } |
tecnosys | 1:dbc44582f2f8 | 47 | |
tecnosys | 1:dbc44582f2f8 | 48 | void CAN3::write(CANMessage* test) { |
tecnosys | 1:dbc44582f2f8 | 49 | uint8_t txbuf_n; |
tecnosys | 1:dbc44582f2f8 | 50 | _mcp.getNextFreeTXBuf(&txbuf_n); |
tecnosys | 1:dbc44582f2f8 | 51 | _mcp.write_canMsg(txbuf_n,test); |
tecnosys | 1:dbc44582f2f8 | 52 | _mcp.start_transmit( txbuf_n ); |
tecnosys | 1:dbc44582f2f8 | 53 | } |
tecnosys | 1:dbc44582f2f8 | 54 | |
tecnosys | 1:dbc44582f2f8 | 55 | void CAN3::rise(void (*fptr)(void)) { |
tecnosys | 1:dbc44582f2f8 | 56 | _itr.rise(fptr); |
tecnosys | 1:dbc44582f2f8 | 57 | } |
tecnosys | 1:dbc44582f2f8 | 58 | |
tecnosys | 1:dbc44582f2f8 | 59 | void CAN3::fall(void (*fptr2)(void)) { |
tecnosys | 1:dbc44582f2f8 | 60 | _itr.fall(fptr2); |
tecnosys | 1:dbc44582f2f8 | 61 | } |
tecnosys | 1:dbc44582f2f8 | 62 | |
tecnosys | 1:dbc44582f2f8 | 63 | void CAN3::frequency(int canSpeed) { |
tecnosys | 1:dbc44582f2f8 | 64 | |
tecnosys | 1:dbc44582f2f8 | 65 | uint8_t res; |
tecnosys | 1:dbc44582f2f8 | 66 | |
tecnosys | 1:dbc44582f2f8 | 67 | res = _mcp.init(canSpeed); //CAN_500KBPS_8MHZ |
tecnosys | 1:dbc44582f2f8 | 68 | wait(.001); |
tecnosys | 1:dbc44582f2f8 | 69 | if (res == MCP2515_OK) { |
tecnosys | 1:dbc44582f2f8 | 70 | printf("CAN Init OK\n\r"); |
tecnosys | 1:dbc44582f2f8 | 71 | |
tecnosys | 1:dbc44582f2f8 | 72 | } else { |
tecnosys | 1:dbc44582f2f8 | 73 | printf("CAN Init FAIL\n\r"); |
tecnosys | 1:dbc44582f2f8 | 74 | } |
tecnosys | 1:dbc44582f2f8 | 75 | |
tecnosys | 1:dbc44582f2f8 | 76 | _mcp.setRegister(MCP_CANINTE, 0x3);//0x3); //MCP_RX_INT); |
tecnosys | 1:dbc44582f2f8 | 77 | _mcp.setRegister(MCP_CANINTF, 0x3);// 0xff); |
tecnosys | 1:dbc44582f2f8 | 78 | |
tecnosys | 1:dbc44582f2f8 | 79 | |
tecnosys | 1:dbc44582f2f8 | 80 | //RX0,1 as rx0,1 digital interrupt outputs |
tecnosys | 1:dbc44582f2f8 | 81 | //_mcp.setRegister(BFPCTRL, 0xf); |
tecnosys | 1:dbc44582f2f8 | 82 | |
tecnosys | 1:dbc44582f2f8 | 83 | //[Set TX0,1,2 as digital inputs |
tecnosys | 1:dbc44582f2f8 | 84 | //_mcp.setRegister(TXRTSCTRL, 0x0); |
tecnosys | 1:dbc44582f2f8 | 85 | |
tecnosys | 1:dbc44582f2f8 | 86 | printf("Setting Normal-Mode - \n\r "); |
tecnosys | 1:dbc44582f2f8 | 87 | if ( _mcp.setCANCTRL_Mode(MODE_NORMAL) == MCP2515_OK) { //MODE_NORMAL MODE_LOOPBACK |
tecnosys | 1:dbc44582f2f8 | 88 | printf("OK\n\r"); |
tecnosys | 1:dbc44582f2f8 | 89 | } else { |
tecnosys | 1:dbc44582f2f8 | 90 | printf("failed\n\r"); |
tecnosys | 1:dbc44582f2f8 | 91 | } |
tecnosys | 1:dbc44582f2f8 | 92 | |
tecnosys | 1:dbc44582f2f8 | 93 | _mcp.dumpExtendedStatus(); |
tecnosys | 1:dbc44582f2f8 | 94 | wait(.001); |
tecnosys | 1:dbc44582f2f8 | 95 | } |