Can_open_masternode

Dependencies:   mbed

Committer:
sam_grove
Date:
Wed Sep 26 05:48:14 2012 +0000
Revision:
7:537bae5a6fc6
Parent:
0:9dd7c6129683
Pushing the project into the new repo

Who changed what in which revision?

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