Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
CANInterface.cpp@0:1ce2190bc4d2, 2014-04-30 (annotated)
- Committer:
- ashleymills
- Date:
- Wed Apr 30 13:51:22 2014 +0000
- Revision:
- 0:1ce2190bc4d2
- Child:
- 1:abf508583871
Simple can interface. Based on work by SKPang, but heavily cleaned up and abstracted.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| ashleymills | 0:1ce2190bc4d2 | 1 | #include "CANInterface.h" |
| ashleymills | 0:1ce2190bc4d2 | 2 | |
| ashleymills | 0:1ce2190bc4d2 | 3 | CANInterface::CANInterface(PinName rd, PinName td) { |
| ashleymills | 0:1ce2190bc4d2 | 4 | _can = new CAN(rd,td); |
| ashleymills | 0:1ce2190bc4d2 | 5 | _can->frequency(CANInterface::CAN_SPEED_500); |
| ashleymills | 0:1ce2190bc4d2 | 6 | _timer = new Timer(); |
| ashleymills | 0:1ce2190bc4d2 | 7 | _timeout = 200; |
| ashleymills | 0:1ce2190bc4d2 | 8 | } |
| ashleymills | 0:1ce2190bc4d2 | 9 | |
| ashleymills | 0:1ce2190bc4d2 | 10 | int CANInterface::makeRequest(uint8_t pid, uint8_t *outBuf, int *outLen) { |
| ashleymills | 0:1ce2190bc4d2 | 11 | char canMsg[8] = {0x02,0x01,pid,0x00,0x00,0x00,0x00,0x00}; |
| ashleymills | 0:1ce2190bc4d2 | 12 | int readOK = 0; |
| ashleymills | 0:1ce2190bc4d2 | 13 | |
| ashleymills | 0:1ce2190bc4d2 | 14 | // write request |
| ashleymills | 0:1ce2190bc4d2 | 15 | CANMessage request(CAN_PID_REQUEST,canMsg,8); |
| ashleymills | 0:1ce2190bc4d2 | 16 | if(_can->write(request)==0) { |
| ashleymills | 0:1ce2190bc4d2 | 17 | return 1; |
| ashleymills | 0:1ce2190bc4d2 | 18 | } |
| ashleymills | 0:1ce2190bc4d2 | 19 | |
| ashleymills | 0:1ce2190bc4d2 | 20 | // get response |
| ashleymills | 0:1ce2190bc4d2 | 21 | CANMessage response; |
| ashleymills | 0:1ce2190bc4d2 | 22 | |
| ashleymills | 0:1ce2190bc4d2 | 23 | // start request timer |
| ashleymills | 0:1ce2190bc4d2 | 24 | _timer->reset(); |
| ashleymills | 0:1ce2190bc4d2 | 25 | _timer->start(); |
| ashleymills | 0:1ce2190bc4d2 | 26 | |
| ashleymills | 0:1ce2190bc4d2 | 27 | // wait for response |
| ashleymills | 0:1ce2190bc4d2 | 28 | while(_timer->read_ms() < _timeout) { |
| ashleymills | 0:1ce2190bc4d2 | 29 | |
| ashleymills | 0:1ce2190bc4d2 | 30 | // try and read response |
| ashleymills | 0:1ce2190bc4d2 | 31 | if(_can->read(response)) { |
| ashleymills | 0:1ce2190bc4d2 | 32 | readOK = 1; |
| ashleymills | 0:1ce2190bc4d2 | 33 | _timer->stop(); |
| ashleymills | 0:1ce2190bc4d2 | 34 | if((response.id == CAN_PID_REPLY) && (response.data[2] == pid)) { |
| ashleymills | 0:1ce2190bc4d2 | 35 | memcpy(outBuf,response.data,response.len-2); |
| ashleymills | 0:1ce2190bc4d2 | 36 | outBuf[0] = response.data[3]; |
| ashleymills | 0:1ce2190bc4d2 | 37 | outBuf[1] = response.data[4]; |
| ashleymills | 0:1ce2190bc4d2 | 38 | *outLen = response.len-2; |
| ashleymills | 0:1ce2190bc4d2 | 39 | break; |
| ashleymills | 0:1ce2190bc4d2 | 40 | } |
| ashleymills | 0:1ce2190bc4d2 | 41 | } |
| ashleymills | 0:1ce2190bc4d2 | 42 | } |
| ashleymills | 0:1ce2190bc4d2 | 43 | return readOK; |
| ashleymills | 0:1ce2190bc4d2 | 44 | } |