Sam Grove / Mbed 2 deprecated canopen_slavenode

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**************************************************************
00002     CAN OPEN SAMPLE - MBED SLAVE DS401
00003 **************************************************************/
00004 /*
00005 This file is part of CanFestival, a library implementing CanOpen Stack.
00006 
00007 Copyright (C): Edouard TISSERANT and Francis DUPIN
00008 mbed Port: sgrove
00009 
00010 See COPYING file for copyrights details.
00011 
00012 This library is free software; you can redistribute it and/or
00013 modify it under the terms of the GNU Lesser General Public
00014 License as published by the Free Software Foundation; either
00015 version 2.1 of the License, or (at your option) any later version.
00016 
00017 This library is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 Lesser General Public License for more details.
00021 
00022 You should have received a copy of the GNU Lesser General Public
00023 License along with this library; if not, write to the Free Software
00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 */
00026 /******************************************************************************
00027 Project description:
00028 Test project for a DS 401 slave, running on mbed
00029 Short description:
00030 
00031     reads from the COM port and changes ID, input value, ect.
00032     57600 8-n-1
00033     "help" will print a list of available commands
00034     prints rx'd messages to the console
00035     CAN running at 125k
00036  
00037 
00038 ******************************************************************************/
00039 
00040 #include "mbed.h"
00041 #include "main.h"
00042 #include "canfestival.h"
00043 #include "can_mbed.h"
00044 #include "mbed_slave.h"
00045 #include "ds401.h"
00046 #include "port_helper.h"
00047 
00048 // Set used for main program timing control
00049 Ticker SysTimer;
00050 // flag used by the main program timing control interrupt
00051 volatile uint8_t timer_interrupt = 0;
00052 volatile uint8_t change_node_id = 0;
00053 
00054 // CAN - put and take data from the stack
00055 uint8_t nodeID;
00056 uint8_t digital_input[1] = {0};
00057 uint8_t digital_output[1] = {0};
00058 uint8_t last_digital_output;
00059 
00060 // read a can message from the stack
00061 static Message m = Message_Initializer;
00062 
00063 int main() 
00064 {
00065     // initialize the helper code - just for debugging
00066     initHelper();
00067     // start the system timer 
00068     SysTimer.attach_us(&serviceSysTimer, CYCLE_TIME);
00069     // start of CANfestival stack calls
00070     canInit(CAN_BAUDRATE);              // Initialize the CANopen bus
00071     initTimer();                        // Start timer for the CANopen stack
00072     nodeID = 10;                           // node id can be anything (1-127)
00073     setNodeId (&mbed_slave_Data, nodeID);
00074     // Init the state
00075     setState(&mbed_slave_Data, Initialisation);
00076     
00077     // just keep loopin'
00078     while(1){
00079         // Cycle timer, invoke action on every time slice
00080         if (sys_timer){ 
00081             // Reset timer
00082             reset_sys_timer(); 
00083 
00084             // look for host communication to change things
00085             serviceCOMCommands();
00086             
00087             // service ds401 digital inputs and outputs
00088             digital_input_handler(&mbed_slave_Data, digital_input, sizeof(digital_input));
00089             digital_output_handler(&mbed_slave_Data, digital_output, sizeof(digital_output));
00090             
00091             if (last_digital_output != digital_output[0]){
00092                 // do something
00093                 printf("new output = %d", digital_output[0]);
00094             }
00095             // save the last value = IO can get continuously bashed
00096             last_digital_output = digital_output[0];     
00097 
00098             // Check if CAN address has been changed
00099             if(change_node_id == 1){
00100                 reset_node_id();
00101                 setState(&mbed_slave_Data, Stopped);               // Stop the node, to change the node ID
00102                 setNodeId(&mbed_slave_Data, nodeID);               // Now the CAN adress is changed
00103                 setState(&mbed_slave_Data, Initialisation);     // Set to Pre_operational, master must boot it again
00104             }
00105         }
00106 
00107         // a message was received - pass it to the CANstack
00108         if (canReceive(&m)){     
00109             // interrupts need to be disabled here
00110             __disable_irq();
00111             // process it
00112             canDispatch(&mbed_slave_Data, &m);
00113             // and re-enabled here
00114             __enable_irq();
00115             // print it to the console for debugging
00116             printMsg(m);
00117         }
00118     }
00119 }
00120 
00121 // ISR for the Ticker
00122 void serviceSysTimer()
00123 {
00124     // just used for debugging purposes
00125     serviceHelper();
00126     // set a flag cleared by the main loop
00127     timer_interrupt = 1;
00128 }
00129