Sam Grove / Mbed 2 deprecated canopen_masternode

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 MASTER
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     
00029     Allow a user to change the bus state by sending COM messages to the device
00030     57600 baud 8-n-1 send "help" for a list of options and formating info
00031     CAN speed - 125k
00032 
00033 ******************************************************************************/
00034 
00035 #include "mbed.h"
00036 #include "main.h"
00037 #include "canfestival.h"
00038 #include "can_mbed.h"
00039 #include "mbed_master.h"
00040 #include "port_helper.h"
00041 
00042 // Set used for main program timing control
00043 Ticker SysTimer;
00044 // flag used by the main program timing control interrupt
00045 uint8_t timer_interrupt = 0;
00046 
00047 // CAN - put and take data from the stack
00048 uint8_t nodeID;
00049 uint8_t change_node_id;
00050 NMTStateAdjustment stack_state = reset;
00051 
00052 // read a can message from the stack
00053 static Message m = Message_Initializer;
00054 const Message empty_msg = Message_Initializer;
00055 
00056 int main() 
00057 {
00058     // start the debug helper
00059     initHelper();
00060     // start the system timer 
00061     SysTimer.attach_us(&serviceSysTimer, CYCLE_TIME*10);
00062     // Initialize the CANopen bus
00063     canInit(CAN_BAUDRATE);
00064     // Start timer for the CANopen stack
00065     initTimer();  
00066     // set the default node id (1-127)
00067     nodeID = 0x7f;
00068     // Init the state
00069     setState(&mbed_master_Data, Initialisation);
00070     setNodeId (&mbed_master_Data, nodeID);
00071     // Put the master in operational mode
00072     setState(&mbed_master_Data, Operational);
00073 
00074     // keep on loopin'
00075     while(1){
00076         
00077         // just testing the rx functionality
00078         if (canReceive(&m)){
00079             // the stack can crash with interrupts enabled
00080             __disable_irq();
00081             // pass the message on and process it
00082             canDispatch(&mbed_master_Data, &m); 
00083             // get back to work
00084             __enable_irq();
00085             // and print the message for debugging
00086             printMsg(m);
00087             // empty it for the next use
00088             m = empty_msg;
00089         }
00090 
00091         // set the main loop exectuion speed at 10mS
00092         if (sys_timer){
00093             // Reset timer
00094             reset_sys_timer();    
00095 
00096             // look for COM requests
00097             serviceCOMCommands();
00098 
00099             // change operational state??
00100             switch(stack_state){
00101 
00102                 case start:
00103                     masterSendNMTstateChange (&mbed_master_Data, 0x00, NMT_Start_Node);
00104                     startSYNC (&mbed_master_Data);
00105                     stack_state = undefined;
00106                     break;
00107 
00108                 case reset:
00109                     masterSendNMTstateChange (&mbed_master_Data, 0x00, NMT_Reset_Node);
00110                     startSYNC (&mbed_master_Data);
00111                     stack_state = undefined;
00112                     break;
00113 
00114                 case stop:
00115                     masterSendNMTstateChange (&mbed_master_Data, 0x00, NMT_Stop_Node );
00116                     stopSYNC (&mbed_master_Data);
00117                     stack_state = undefined;
00118                     break;
00119                 
00120                 default:
00121                     break;
00122             }
00123 
00124             // Check if CAN address has been changed
00125             if(change_node_id == 1){
00126                 reset_node_id();
00127                 setState(&mbed_master_Data, Stopped);             // Stop the node, to change the node ID
00128                 setNodeId(&mbed_master_Data, nodeID);             // Now the CAN adress is changed
00129                 setState(&mbed_master_Data, Initialisation);      // Set to Pre_operational, master must boot it again
00130                 setState(&mbed_master_Data, Operational);
00131                 stack_state = reset;
00132             }
00133 
00134             // other events
00135             //sendPDOevent (&mbed_master_Data);
00136         }
00137     }
00138 }
00139 
00140 // ISR for the Ticker
00141 void serviceSysTimer()
00142 {
00143     // just used for debugging purposes
00144     serviceHelper();
00145     // set a flag cleared by the main loop
00146     timer_interrupt = 1;
00147 }
00148