Sam Grove / Mbed 2 deprecated canopen_masternode

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers port_helper.cpp Source File

port_helper.cpp

00001 
00002 
00003 #include "port_helper.h"
00004 #include "main.h"
00005 
00006 Serial Debug(USBTX, USBRX);
00007 DigitalOut running_status(LED1);
00008 
00009 extern uint8_t nodeID;
00010 extern uint8_t change_node_id;
00011 extern NMTStateAdjustment stack_state;
00012 
00013 void initHelper()
00014 {
00015     Debug.baud(57600);
00016     printf(" CANopen port of CANfestival master node \n");
00017 }
00018 
00019 void serviceHelper()
00020 {
00021     static uint32_t cnt = 0;
00022     // indicate that the stack is running
00023     if (cnt++ > 100){
00024         cnt = 0;
00025         running_status = !running_status;
00026     }    
00027 }
00028 
00029 
00030 void printMsg(Message& msg)
00031 {
00032     CANMessage m(msg.cob_id, (char*)msg.data, msg.len, static_cast<CANType>(msg.rtr), CANStandard);
00033     // call the CANMessage formatted method
00034     printMsg(m);
00035 }
00036 
00037 void printMsg(CANMessage& msg)
00038 {
00039     switch(msg.len){
00040         case 1:
00041             printf("ID: 0x%04X DLC: %d Data(0): 0x%02X\n", msg.id, msg.len, msg.data[0]);
00042             break;
00043         case 2:
00044             printf("ID: 0x%04X DLC: %d Data(0->1): 0x%02X 0x%02X\n", msg.id, msg.len, msg.data[0], msg.data[1]);
00045             break;
00046         case 3:
00047             printf("ID: 0x%04X DLC: %d Data(0->2): 0x%02X 0x%02X 0x%02X\n",
00048                 msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2]);
00049             break;
00050         case 4:
00051             printf("ID: 0x%04X DLC: %d Data(0->3): 0x%02X 0x%02X 0x%02X 0x%02X\n",
00052                 msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], msg.data[3]);
00053             break;
00054         case 5:
00055             printf("ID: 0x%04X DLC: %d Data(0->4): 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
00056                 msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4]);
00057             break;
00058         case 6:
00059             printf("ID: 0x%04X DLC: %d Data(0->5): 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
00060                 msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4], msg.data[5]);
00061             break;
00062         case 7:
00063             printf("ID: 0x%04X DLC: %d Data(0->6): 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
00064                 msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4], msg.data[5], msg.data[6]);
00065             break;
00066         case 8:
00067             printf("ID: 0x%04X DLC: %d Data(0->7): 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
00068                 msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], msg.data[3], msg.data[4], msg.data[5], msg.data[6], msg.data[7]);
00069             break;
00070         default: 
00071             break;
00072     }
00073     //printf("ID: 0x%04X DLC: %d Data(0->7): 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
00074     //    msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], 
00075     //    msg.data[3], msg.data[4], msg.data[5], msg.data[6],msg.data[7]);
00076 }
00077 
00078 void serviceCOMCommands()
00079 {
00080      static char msg[64];
00081     static char w_loc=0;
00082     if (Debug.readable()){
00083         // store the data
00084         if (w_loc < 64){
00085             msg[w_loc++] = Debug.getc();
00086         }
00087         // clear the buffer and try again
00088         else{
00089             // clear the buffer
00090             memset(msg, 0, 64);
00091             w_loc = 0;
00092             return;
00093         }
00094         // parse the message and act on it
00095         if ((msg[w_loc-1] == '\r') || (msg[w_loc-1] == '\n') ){
00096             // format the message
00097             // process the message and look for something familiar
00098             if (strncmp(msg, "help", strlen("help")) == 0){
00099                  // print the help menu
00100                 printf("\t HELP OPTIONS (case sesnitive):\n");
00101                 printf("\t  help - display the available options\n");
00102                 printf("\t  id=xxx - change the node id\n");
00103                 printf("\t  about - get info about the node\n");
00104                 printf("\t  state=xxxx - start, stop, reset\n");
00105             }
00106             else if (strncmp(msg, "id=", strlen("id=")) == 0){
00107                 // change the node ID
00108                 int res = atoi(msg+strlen("id="));
00109                 if ( (res > 0) && (res < 128) ){
00110                     nodeID = res;
00111                     printf("the new node_id = %d\n", nodeID);
00112                     change_node_id = 1;
00113                 }
00114                 else{
00115                     printf("invalid parameter\n");
00116                 }
00117             }
00118             else if (strncmp(msg, "about", strlen("about")) == 0){
00119                 // our call tag
00120                 printf(" CANopen port of CANfestival master node \n");
00121             }
00122             else if (strncmp(msg, "state=", strlen("state=")) == 0){
00123                 // ohh what to do
00124                 if (strncmp(msg+strlen("state="), "start", strlen("start")) == 0){
00125                     printf("stack state = start\n");
00126                     // signal back
00127                     stack_state = start;
00128                 }
00129                 else if (strncmp(msg+strlen("state="), "stop", strlen("stop")) == 0){
00130                      printf("stack state = stop\n");
00131                     // signal back
00132                     stack_state = stop;
00133                 }
00134                 else if (strncmp(msg+strlen("state="), "reset", strlen("reset")) == 0){
00135                      printf("stack state = reset\n");
00136                     // signal back
00137                     stack_state = reset;
00138                 }
00139             }
00140             // clear the buffer
00141             memset(msg, 0, 64);
00142             w_loc = 0;
00143         }
00144     }
00145 }
00146 
00147 /*
00148 WDT_IRQHandler           
00149 TIMER0_IRQHandler         
00150 TIMER1_IRQHandler         
00151 TIMER2_IRQHandler         
00152 TIMER3_IRQHandler         
00153 UART0_IRQHandler          
00154 UART1_IRQHandler          
00155 UART2_IRQHandler          
00156 UART3_IRQHandler          
00157 PWM1_IRQHandler           
00158 I2C0_IRQHandler           
00159 I2C1_IRQHandler           
00160 I2C2_IRQHandler           
00161 SPI_IRQHandler            
00162 SSP0_IRQHandler           
00163 SSP1_IRQHandler           
00164 PLL0_IRQHandler           
00165 RTC_IRQHandler            
00166 EINT0_IRQHandler          
00167 EINT1_IRQHandler          
00168 EINT2_IRQHandler          
00169 EINT3_IRQHandler          
00170 ADC_IRQHandler            
00171 BOD_IRQHandler            
00172 USB_IRQHandler            
00173 CAN_IRQHandler            
00174 DMA_IRQHandler          
00175 I2S_IRQHandler            
00176 ENET_IRQHandler       
00177 RIT_IRQHandler          
00178 MCPWM_IRQHandler             
00179 QEI_IRQHandler            
00180 PLL1_IRQHandler           
00181 USBActivity_IRQHandler
00182 CANActivity_IRQHandler
00183 */