CANfestival - an open source CANopen framework

Dependencies:   mbed

Committer:
sam_grove
Date:
Wed Sep 26 05:43:05 2012 +0000
Revision:
6:bc64031ac849
Parent:
0:6219434a0cb5
Change a typecast in can_mbed.cpp from unit8_t * to char * to fit the CANMessage constructor

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 0:6219434a0cb5 1 /**************************************************************
sam_grove 0:6219434a0cb5 2 CAN OPEN SAMPLE - MBED SLAVE DS401
sam_grove 0:6219434a0cb5 3 **************************************************************/
sam_grove 0:6219434a0cb5 4 /*
sam_grove 0:6219434a0cb5 5 This file is part of CanFestival, a library implementing CanOpen Stack.
sam_grove 0:6219434a0cb5 6
sam_grove 0:6219434a0cb5 7 Copyright (C): Edouard TISSERANT and Francis DUPIN
sam_grove 0:6219434a0cb5 8 mbed Port: sgrove
sam_grove 0:6219434a0cb5 9
sam_grove 0:6219434a0cb5 10 See COPYING file for copyrights details.
sam_grove 0:6219434a0cb5 11
sam_grove 0:6219434a0cb5 12 This library is free software; you can redistribute it and/or
sam_grove 0:6219434a0cb5 13 modify it under the terms of the GNU Lesser General Public
sam_grove 0:6219434a0cb5 14 License as published by the Free Software Foundation; either
sam_grove 0:6219434a0cb5 15 version 2.1 of the License, or (at your option) any later version.
sam_grove 0:6219434a0cb5 16
sam_grove 0:6219434a0cb5 17 This library is distributed in the hope that it will be useful,
sam_grove 0:6219434a0cb5 18 but WITHOUT ANY WARRANTY; without even the implied warranty of
sam_grove 0:6219434a0cb5 19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
sam_grove 0:6219434a0cb5 20 Lesser General Public License for more details.
sam_grove 0:6219434a0cb5 21
sam_grove 0:6219434a0cb5 22 You should have received a copy of the GNU Lesser General Public
sam_grove 0:6219434a0cb5 23 License along with this library; if not, write to the Free Software
sam_grove 0:6219434a0cb5 24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
sam_grove 0:6219434a0cb5 25 */
sam_grove 0:6219434a0cb5 26 /******************************************************************************
sam_grove 0:6219434a0cb5 27 Project description:
sam_grove 0:6219434a0cb5 28 Test project for a DS 401 slave, running on mbed
sam_grove 0:6219434a0cb5 29 Short description:
sam_grove 0:6219434a0cb5 30
sam_grove 0:6219434a0cb5 31 reads from the COM port and changes ID, input value, ect.
sam_grove 0:6219434a0cb5 32 57600 8-n-1
sam_grove 0:6219434a0cb5 33 "help" will print a list of available commands
sam_grove 0:6219434a0cb5 34 prints rx'd messages to the console
sam_grove 0:6219434a0cb5 35 CAN running at 125k
sam_grove 0:6219434a0cb5 36
sam_grove 0:6219434a0cb5 37
sam_grove 0:6219434a0cb5 38 ******************************************************************************/
sam_grove 0:6219434a0cb5 39
sam_grove 0:6219434a0cb5 40 #include "mbed.h"
sam_grove 0:6219434a0cb5 41 #include "main.h"
sam_grove 0:6219434a0cb5 42 #include "canfestival.h"
sam_grove 0:6219434a0cb5 43 #include "can_mbed.h"
sam_grove 0:6219434a0cb5 44 #include "mbed_slave.h"
sam_grove 0:6219434a0cb5 45 #include "ds401.h"
sam_grove 0:6219434a0cb5 46 #include "port_helper.h"
sam_grove 0:6219434a0cb5 47
sam_grove 0:6219434a0cb5 48 // Set used for main program timing control
sam_grove 0:6219434a0cb5 49 Ticker SysTimer;
sam_grove 0:6219434a0cb5 50 // flag used by the main program timing control interrupt
sam_grove 0:6219434a0cb5 51 volatile uint8_t timer_interrupt = 0;
sam_grove 0:6219434a0cb5 52 volatile uint8_t change_node_id = 0;
sam_grove 0:6219434a0cb5 53
sam_grove 0:6219434a0cb5 54 // CAN - put and take data from the stack
sam_grove 0:6219434a0cb5 55 uint8_t nodeID;
sam_grove 0:6219434a0cb5 56 uint8_t digital_input[1] = {0};
sam_grove 0:6219434a0cb5 57 uint8_t digital_output[1] = {0};
sam_grove 0:6219434a0cb5 58 uint8_t last_digital_output;
sam_grove 0:6219434a0cb5 59
sam_grove 0:6219434a0cb5 60 // read a can message from the stack
sam_grove 0:6219434a0cb5 61 static Message m = Message_Initializer;
sam_grove 0:6219434a0cb5 62
sam_grove 0:6219434a0cb5 63 int main()
sam_grove 0:6219434a0cb5 64 {
sam_grove 0:6219434a0cb5 65 // initialize the helper code - just for debugging
sam_grove 0:6219434a0cb5 66 initHelper();
sam_grove 0:6219434a0cb5 67 // start the system timer
sam_grove 0:6219434a0cb5 68 SysTimer.attach_us(&serviceSysTimer, CYCLE_TIME);
sam_grove 0:6219434a0cb5 69 // start of CANfestival stack calls
sam_grove 0:6219434a0cb5 70 canInit(CAN_BAUDRATE); // Initialize the CANopen bus
sam_grove 0:6219434a0cb5 71 initTimer(); // Start timer for the CANopen stack
sam_grove 0:6219434a0cb5 72 nodeID = 10; // node id can be anything (1-127)
sam_grove 0:6219434a0cb5 73 setNodeId (&mbed_slave_Data, nodeID);
sam_grove 0:6219434a0cb5 74 // Init the state
sam_grove 0:6219434a0cb5 75 setState(&mbed_slave_Data, Initialisation);
sam_grove 0:6219434a0cb5 76
sam_grove 0:6219434a0cb5 77 // just keep loopin'
sam_grove 0:6219434a0cb5 78 while(1){
sam_grove 0:6219434a0cb5 79 // Cycle timer, invoke action on every time slice
sam_grove 0:6219434a0cb5 80 if (sys_timer){
sam_grove 0:6219434a0cb5 81 // Reset timer
sam_grove 0:6219434a0cb5 82 reset_sys_timer();
sam_grove 0:6219434a0cb5 83
sam_grove 0:6219434a0cb5 84 // look for host communication to change things
sam_grove 0:6219434a0cb5 85 serviceCOMCommands();
sam_grove 0:6219434a0cb5 86
sam_grove 0:6219434a0cb5 87 // service ds401 digital inputs and outputs
sam_grove 0:6219434a0cb5 88 digital_input_handler(&mbed_slave_Data, digital_input, sizeof(digital_input));
sam_grove 0:6219434a0cb5 89 digital_output_handler(&mbed_slave_Data, digital_output, sizeof(digital_output));
sam_grove 0:6219434a0cb5 90
sam_grove 0:6219434a0cb5 91 if (last_digital_output != digital_output[0]){
sam_grove 0:6219434a0cb5 92 // do something
sam_grove 0:6219434a0cb5 93 printf("new output = %d", digital_output[0]);
sam_grove 0:6219434a0cb5 94 }
sam_grove 0:6219434a0cb5 95 // save the last value = IO can get continuously bashed
sam_grove 0:6219434a0cb5 96 last_digital_output = digital_output[0];
sam_grove 0:6219434a0cb5 97
sam_grove 0:6219434a0cb5 98 // Check if CAN address has been changed
sam_grove 0:6219434a0cb5 99 if(change_node_id == 1){
sam_grove 0:6219434a0cb5 100 reset_node_id();
sam_grove 0:6219434a0cb5 101 setState(&mbed_slave_Data, Stopped); // Stop the node, to change the node ID
sam_grove 0:6219434a0cb5 102 setNodeId(&mbed_slave_Data, nodeID); // Now the CAN adress is changed
sam_grove 0:6219434a0cb5 103 setState(&mbed_slave_Data, Initialisation); // Set to Pre_operational, master must boot it again
sam_grove 0:6219434a0cb5 104 }
sam_grove 0:6219434a0cb5 105 }
sam_grove 0:6219434a0cb5 106
sam_grove 0:6219434a0cb5 107 // a message was received - pass it to the CANstack
sam_grove 0:6219434a0cb5 108 if (canReceive(&m)){
sam_grove 0:6219434a0cb5 109 // interrupts need to be disabled here
sam_grove 0:6219434a0cb5 110 __disable_irq();
sam_grove 0:6219434a0cb5 111 // process it
sam_grove 0:6219434a0cb5 112 canDispatch(&mbed_slave_Data, &m);
sam_grove 0:6219434a0cb5 113 // and re-enabled here
sam_grove 0:6219434a0cb5 114 __enable_irq();
sam_grove 0:6219434a0cb5 115 // print it to the console for debugging
sam_grove 0:6219434a0cb5 116 printMsg(m);
sam_grove 0:6219434a0cb5 117 }
sam_grove 0:6219434a0cb5 118 }
sam_grove 0:6219434a0cb5 119 }
sam_grove 0:6219434a0cb5 120
sam_grove 0:6219434a0cb5 121 // ISR for the Ticker
sam_grove 0:6219434a0cb5 122 void serviceSysTimer()
sam_grove 0:6219434a0cb5 123 {
sam_grove 0:6219434a0cb5 124 // just used for debugging purposes
sam_grove 0:6219434a0cb5 125 serviceHelper();
sam_grove 0:6219434a0cb5 126 // set a flag cleared by the main loop
sam_grove 0:6219434a0cb5 127 timer_interrupt = 1;
sam_grove 0:6219434a0cb5 128 }
sam_grove 0:6219434a0cb5 129