Luis Silvio Cordova Rivadeneira
/
Can_open_slavenode
Can_open_slavenode
framework/driver/mbed/can_mbed.cpp@6:bc64031ac849, 2012-09-26 (annotated)
- Committer:
- sam_grove
- Date:
- Wed Sep 26 05:43:05 2012 +0000
- Revision:
- 6:bc64031ac849
- Parent:
- 2:e0be90742924
Change a typecast in can_mbed.cpp from unit8_t * to char * to fit the CANMessage constructor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove |
0:6219434a0cb5 | 1 | /* |
sam_grove |
0:6219434a0cb5 | 2 | This file is part of CanFestival, a library implementing CanOpen Stack. |
sam_grove |
0:6219434a0cb5 | 3 | |
sam_grove |
0:6219434a0cb5 | 4 | Copyright (C): Edouard TISSERANT and Francis DUPIN |
sam_grove |
0:6219434a0cb5 | 5 | mbed Port: sgrove |
sam_grove |
0:6219434a0cb5 | 6 | |
sam_grove |
0:6219434a0cb5 | 7 | See COPYING file for copyrights details. |
sam_grove |
0:6219434a0cb5 | 8 | |
sam_grove |
0:6219434a0cb5 | 9 | This library is free software; you can redistribute it and/or |
sam_grove |
0:6219434a0cb5 | 10 | modify it under the terms of the GNU Lesser General Public |
sam_grove |
0:6219434a0cb5 | 11 | License as published by the Free Software Foundation; either |
sam_grove |
0:6219434a0cb5 | 12 | version 2.1 of the License, or (at your option) any later version. |
sam_grove |
0:6219434a0cb5 | 13 | |
sam_grove |
0:6219434a0cb5 | 14 | This library is distributed in the hope that it will be useful, |
sam_grove |
0:6219434a0cb5 | 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
sam_grove |
0:6219434a0cb5 | 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
sam_grove |
0:6219434a0cb5 | 17 | Lesser General Public License for more details. |
sam_grove |
0:6219434a0cb5 | 18 | |
sam_grove |
0:6219434a0cb5 | 19 | You should have received a copy of the GNU Lesser General Public |
sam_grove |
0:6219434a0cb5 | 20 | License along with this library; if not, write to the Free Software |
sam_grove |
0:6219434a0cb5 | 21 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
sam_grove |
0:6219434a0cb5 | 22 | */ |
sam_grove |
0:6219434a0cb5 | 23 | |
sam_grove |
0:6219434a0cb5 | 24 | //#define DEBUG_WAR_CONSOLE_ON |
sam_grove |
0:6219434a0cb5 | 25 | //#define DEBUG_ERR_CONSOLE_ON |
sam_grove |
0:6219434a0cb5 | 26 | |
sam_grove |
0:6219434a0cb5 | 27 | #include "can_mbed.h" |
sam_grove |
0:6219434a0cb5 | 28 | #include "canfestival.h" |
sam_grove |
0:6219434a0cb5 | 29 | |
sam_grove |
0:6219434a0cb5 | 30 | volatile unsigned char msg_received = 0; |
sam_grove |
0:6219434a0cb5 | 31 | // initialize the CAN object from the mbed api |
sam_grove |
0:6219434a0cb5 | 32 | CAN CANopen(p9, p10); |
sam_grove |
0:6219434a0cb5 | 33 | |
sam_grove |
0:6219434a0cb5 | 34 | unsigned char canInit(unsigned int bitrate) |
sam_grove |
0:6219434a0cb5 | 35 | /****************************************************************************** |
sam_grove |
0:6219434a0cb5 | 36 | Initialize the hardware to send and receive CAN messages |
sam_grove |
0:6219434a0cb5 | 37 | INPUT bitrate bitrate in kilobit |
sam_grove |
0:6219434a0cb5 | 38 | OUTPUT 1 if successful |
sam_grove |
0:6219434a0cb5 | 39 | ******************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 40 | { |
sam_grove |
0:6219434a0cb5 | 41 | // make sure the requested baudrate is supported |
sam_grove |
0:6219434a0cb5 | 42 | if (CANopen.frequency(bitrate*1000) == 0){ |
sam_grove |
0:6219434a0cb5 | 43 | return 0; |
sam_grove |
0:6219434a0cb5 | 44 | } |
sam_grove |
0:6219434a0cb5 | 45 | // desired baud was set |
sam_grove |
0:6219434a0cb5 | 46 | return 1; |
sam_grove |
0:6219434a0cb5 | 47 | } |
sam_grove |
0:6219434a0cb5 | 48 | |
sam_grove |
0:6219434a0cb5 | 49 | unsigned char canSend(CAN_PORT notused, Message *m) |
sam_grove |
0:6219434a0cb5 | 50 | /****************************************************************************** |
sam_grove |
0:6219434a0cb5 | 51 | The driver send a CAN message passed from the CANopen stack |
sam_grove |
0:6219434a0cb5 | 52 | INPUT CAN_PORT is not used (only 1 avaiable) |
sam_grove |
0:6219434a0cb5 | 53 | Message *m pointer to message to send |
sam_grove |
0:6219434a0cb5 | 54 | OUTPUT 1 if hardware -> CAN frame |
sam_grove |
0:6219434a0cb5 | 55 | ******************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 56 | { |
sam_grove |
0:6219434a0cb5 | 57 | // convert the message from a CANopen object to a mbed object |
sam_grove | 6:bc64031ac849 | 58 | CANMessage msg(m->cob_id, (char*)m->data, m->len, static_cast<CANType>(m->rtr), CANStandard); |
sam_grove |
0:6219434a0cb5 | 59 | // make sure the message was sent |
sam_grove |
0:6219434a0cb5 | 60 | if (CANopen.write(msg) == 0){ |
sam_grove |
0:6219434a0cb5 | 61 | return 0; |
sam_grove |
0:6219434a0cb5 | 62 | } |
sam_grove |
0:6219434a0cb5 | 63 | // message was sent |
sam_grove |
0:6219434a0cb5 | 64 | return 1; |
sam_grove |
0:6219434a0cb5 | 65 | } |
sam_grove |
0:6219434a0cb5 | 66 | |
sam_grove |
0:6219434a0cb5 | 67 | unsigned char canReceive(Message *m) |
sam_grove |
0:6219434a0cb5 | 68 | /****************************************************************************** |
sam_grove |
0:6219434a0cb5 | 69 | The driver pass a received CAN message to the stack |
sam_grove |
0:6219434a0cb5 | 70 | INPUT Message *m pointer to received CAN message |
sam_grove |
0:6219434a0cb5 | 71 | OUTPUT 1 if a message received |
sam_grove |
0:6219434a0cb5 | 72 | ******************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 73 | { |
sam_grove |
0:6219434a0cb5 | 74 | // object to store the last message |
sam_grove |
0:6219434a0cb5 | 75 | CANMessage msg; |
sam_grove |
0:6219434a0cb5 | 76 | // look if something has been rec'd |
sam_grove |
0:6219434a0cb5 | 77 | if (CANopen.read(msg) == 0){ |
sam_grove |
0:6219434a0cb5 | 78 | return 0; |
sam_grove |
0:6219434a0cb5 | 79 | } |
sam_grove |
0:6219434a0cb5 | 80 | // conver the CANMessage object to a Message object |
sam_grove |
0:6219434a0cb5 | 81 | m->cob_id = msg.id; |
sam_grove |
0:6219434a0cb5 | 82 | m->len = msg.len; |
sam_grove |
0:6219434a0cb5 | 83 | m->rtr = static_cast<UNS8>(msg.type); |
sam_grove |
0:6219434a0cb5 | 84 | // clear erroneous data from the last use |
sam_grove |
2:e0be90742924 | 85 | for (int i=0; i<=7; i++){ |
sam_grove |
2:e0be90742924 | 86 | if (i <= (msg.len-1)) |
sam_grove |
2:e0be90742924 | 87 | m->data[i] = msg.data[i]; |
sam_grove |
2:e0be90742924 | 88 | else |
sam_grove |
2:e0be90742924 | 89 | m->data[i] = 0; |
sam_grove |
2:e0be90742924 | 90 | } |
sam_grove |
2:e0be90742924 | 91 | // messge processed |
sam_grove |
0:6219434a0cb5 | 92 | return 1; |
sam_grove |
0:6219434a0cb5 | 93 | } |
sam_grove |
0:6219434a0cb5 | 94 | |
sam_grove |
0:6219434a0cb5 | 95 | /***************************************************************************/ |
sam_grove |
0:6219434a0cb5 | 96 | unsigned char canChangeBaudRate_driver( CAN_HANDLE fd, char* baud) |
sam_grove |
0:6219434a0cb5 | 97 | { |
sam_grove |
2:e0be90742924 | 98 | // not sure how baud is passed as a char* yet |
sam_grove |
0:6219434a0cb5 | 99 | return 0; |
sam_grove |
0:6219434a0cb5 | 100 | } |
sam_grove |
0:6219434a0cb5 | 101 |