![](/media/cache/profiles/IMG_2753.JPG.50x50_q85.jpg)
Can_open_masternode
source/port_helper.cpp@0:9dd7c6129683, 2011-05-30 (annotated)
- Committer:
- sam_grove
- Date:
- Mon May 30 07:36:47 2011 +0000
- Revision:
- 0:9dd7c6129683
- Child:
- 1:cb8989598a0f
Initial public release of master node example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove |
0:9dd7c6129683 | 1 | |
sam_grove |
0:9dd7c6129683 | 2 | |
sam_grove |
0:9dd7c6129683 | 3 | #include "port_helper.h" |
sam_grove |
0:9dd7c6129683 | 4 | #include "main.h" |
sam_grove |
0:9dd7c6129683 | 5 | |
sam_grove |
0:9dd7c6129683 | 6 | Serial Debug(USBTX, USBRX); |
sam_grove |
0:9dd7c6129683 | 7 | DigitalOut running_status(LED1); |
sam_grove |
0:9dd7c6129683 | 8 | |
sam_grove |
0:9dd7c6129683 | 9 | extern uint8_t nodeID; |
sam_grove |
0:9dd7c6129683 | 10 | extern uint8_t change_node_id; |
sam_grove |
0:9dd7c6129683 | 11 | extern NMTStateAdjustment stack_state; |
sam_grove |
0:9dd7c6129683 | 12 | |
sam_grove |
0:9dd7c6129683 | 13 | void initHelper() |
sam_grove |
0:9dd7c6129683 | 14 | { |
sam_grove |
0:9dd7c6129683 | 15 | Debug.baud(57600); |
sam_grove |
0:9dd7c6129683 | 16 | printf(" CANopen port of CANfestival master node \n"); |
sam_grove |
0:9dd7c6129683 | 17 | } |
sam_grove |
0:9dd7c6129683 | 18 | |
sam_grove |
0:9dd7c6129683 | 19 | void serviceHelper() |
sam_grove |
0:9dd7c6129683 | 20 | { |
sam_grove |
0:9dd7c6129683 | 21 | static uint32_t cnt = 0; |
sam_grove |
0:9dd7c6129683 | 22 | // indicate that the stack is running |
sam_grove |
0:9dd7c6129683 | 23 | if (cnt++ > 100){ |
sam_grove |
0:9dd7c6129683 | 24 | cnt = 0; |
sam_grove |
0:9dd7c6129683 | 25 | running_status = !running_status; |
sam_grove |
0:9dd7c6129683 | 26 | } |
sam_grove |
0:9dd7c6129683 | 27 | } |
sam_grove |
0:9dd7c6129683 | 28 | |
sam_grove |
0:9dd7c6129683 | 29 | |
sam_grove |
0:9dd7c6129683 | 30 | void printMsg(Message& msg) |
sam_grove |
0:9dd7c6129683 | 31 | { |
sam_grove |
0:9dd7c6129683 | 32 | CANMessage m(msg.cob_id, (char*)msg.data, msg.len, static_cast<CANType>(msg.rtr), CANStandard); |
sam_grove |
0:9dd7c6129683 | 33 | // call the CANMessage formatted method |
sam_grove |
0:9dd7c6129683 | 34 | printMsg(m); |
sam_grove |
0:9dd7c6129683 | 35 | } |
sam_grove |
0:9dd7c6129683 | 36 | |
sam_grove |
0:9dd7c6129683 | 37 | void printMsg(CANMessage& msg) |
sam_grove |
0:9dd7c6129683 | 38 | { |
sam_grove |
0:9dd7c6129683 | 39 | 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", |
sam_grove |
0:9dd7c6129683 | 40 | msg.id, msg.len, msg.data[0], msg.data[1], msg.data[2], |
sam_grove |
0:9dd7c6129683 | 41 | msg.data[3], msg.data[4], msg.data[5], msg.data[6],msg.data[7]); |
sam_grove |
0:9dd7c6129683 | 42 | } |
sam_grove |
0:9dd7c6129683 | 43 | |
sam_grove |
0:9dd7c6129683 | 44 | void serviceCOMCommands() |
sam_grove |
0:9dd7c6129683 | 45 | { |
sam_grove |
0:9dd7c6129683 | 46 | static char msg[64]; |
sam_grove |
0:9dd7c6129683 | 47 | static char w_loc=0; |
sam_grove |
0:9dd7c6129683 | 48 | if (Debug.readable()){ |
sam_grove |
0:9dd7c6129683 | 49 | // store the data |
sam_grove |
0:9dd7c6129683 | 50 | if (w_loc < 64){ |
sam_grove |
0:9dd7c6129683 | 51 | msg[w_loc++] = Debug.getc(); |
sam_grove |
0:9dd7c6129683 | 52 | } |
sam_grove |
0:9dd7c6129683 | 53 | // clear the buffer and try again |
sam_grove |
0:9dd7c6129683 | 54 | else{ |
sam_grove |
0:9dd7c6129683 | 55 | // clear the buffer |
sam_grove |
0:9dd7c6129683 | 56 | memset(msg, 0, 64); |
sam_grove |
0:9dd7c6129683 | 57 | w_loc = 0; |
sam_grove |
0:9dd7c6129683 | 58 | return; |
sam_grove |
0:9dd7c6129683 | 59 | } |
sam_grove |
0:9dd7c6129683 | 60 | // parse the message and act on it |
sam_grove |
0:9dd7c6129683 | 61 | if ((msg[w_loc-1] == '\r') || (msg[w_loc-1] == '\n') ){ |
sam_grove |
0:9dd7c6129683 | 62 | // format the message |
sam_grove |
0:9dd7c6129683 | 63 | // process the message and look for something familiar |
sam_grove |
0:9dd7c6129683 | 64 | if (strncmp(msg, "help", strlen("help")) == 0){ |
sam_grove |
0:9dd7c6129683 | 65 | // print the help menu |
sam_grove |
0:9dd7c6129683 | 66 | printf("\t HELP OPTIONS (case sesnitive):\n"); |
sam_grove |
0:9dd7c6129683 | 67 | printf("\t help - display the available options\n"); |
sam_grove |
0:9dd7c6129683 | 68 | printf("\t id=xxx - change the node id\n"); |
sam_grove |
0:9dd7c6129683 | 69 | printf("\t about - get info about the node\n"); |
sam_grove |
0:9dd7c6129683 | 70 | printf("\t state=xxxx - start, stop, reset\n"); |
sam_grove |
0:9dd7c6129683 | 71 | } |
sam_grove |
0:9dd7c6129683 | 72 | else if (strncmp(msg, "id=", strlen("id=")) == 0){ |
sam_grove |
0:9dd7c6129683 | 73 | // change the node ID |
sam_grove |
0:9dd7c6129683 | 74 | int res = atoi(msg+strlen("id=")); |
sam_grove |
0:9dd7c6129683 | 75 | if ( (res > 0) && (res < 128) ){ |
sam_grove |
0:9dd7c6129683 | 76 | nodeID = res; |
sam_grove |
0:9dd7c6129683 | 77 | printf("the new node_id = %d\n", nodeID); |
sam_grove |
0:9dd7c6129683 | 78 | change_node_id = 1; |
sam_grove |
0:9dd7c6129683 | 79 | } |
sam_grove |
0:9dd7c6129683 | 80 | else{ |
sam_grove |
0:9dd7c6129683 | 81 | printf("invalid parameter"); |
sam_grove |
0:9dd7c6129683 | 82 | } |
sam_grove |
0:9dd7c6129683 | 83 | } |
sam_grove |
0:9dd7c6129683 | 84 | else if (strncmp(msg, "about", strlen("about")) == 0){ |
sam_grove |
0:9dd7c6129683 | 85 | // our call tag |
sam_grove |
0:9dd7c6129683 | 86 | printf(" CANopen port of CANfestival master node \n"); |
sam_grove |
0:9dd7c6129683 | 87 | } |
sam_grove |
0:9dd7c6129683 | 88 | else if (strncmp(msg, "state=", strlen("state=")) == 0){ |
sam_grove |
0:9dd7c6129683 | 89 | // ohh what to do |
sam_grove |
0:9dd7c6129683 | 90 | if (strncmp(msg+strlen("state="), "start", strlen("start")) == 0){ |
sam_grove |
0:9dd7c6129683 | 91 | printf("stack state = start"); |
sam_grove |
0:9dd7c6129683 | 92 | // signal back |
sam_grove |
0:9dd7c6129683 | 93 | stack_state = start; |
sam_grove |
0:9dd7c6129683 | 94 | } |
sam_grove |
0:9dd7c6129683 | 95 | else if (strncmp(msg+strlen("state="), "stop", strlen("stop")) == 0){ |
sam_grove |
0:9dd7c6129683 | 96 | printf("stack state = stop"); |
sam_grove |
0:9dd7c6129683 | 97 | // signal back |
sam_grove |
0:9dd7c6129683 | 98 | stack_state = stop; |
sam_grove |
0:9dd7c6129683 | 99 | } |
sam_grove |
0:9dd7c6129683 | 100 | else if (strncmp(msg+strlen("state="), "reset", strlen("reset")) == 0){ |
sam_grove |
0:9dd7c6129683 | 101 | printf("stack state = reset"); |
sam_grove |
0:9dd7c6129683 | 102 | // signal back |
sam_grove |
0:9dd7c6129683 | 103 | stack_state = reset; |
sam_grove |
0:9dd7c6129683 | 104 | } |
sam_grove |
0:9dd7c6129683 | 105 | } |
sam_grove |
0:9dd7c6129683 | 106 | // clear the buffer |
sam_grove |
0:9dd7c6129683 | 107 | memset(msg, 0, 64); |
sam_grove |
0:9dd7c6129683 | 108 | w_loc = 0; |
sam_grove |
0:9dd7c6129683 | 109 | } |
sam_grove |
0:9dd7c6129683 | 110 | } |
sam_grove |
0:9dd7c6129683 | 111 | } |
sam_grove |
0:9dd7c6129683 | 112 | |
sam_grove |
0:9dd7c6129683 | 113 | /* |
sam_grove |
0:9dd7c6129683 | 114 | WDT_IRQHandler |
sam_grove |
0:9dd7c6129683 | 115 | TIMER0_IRQHandler |
sam_grove |
0:9dd7c6129683 | 116 | TIMER1_IRQHandler |
sam_grove |
0:9dd7c6129683 | 117 | TIMER2_IRQHandler |
sam_grove |
0:9dd7c6129683 | 118 | TIMER3_IRQHandler |
sam_grove |
0:9dd7c6129683 | 119 | UART0_IRQHandler |
sam_grove |
0:9dd7c6129683 | 120 | UART1_IRQHandler |
sam_grove |
0:9dd7c6129683 | 121 | UART2_IRQHandler |
sam_grove |
0:9dd7c6129683 | 122 | UART3_IRQHandler |
sam_grove |
0:9dd7c6129683 | 123 | PWM1_IRQHandler |
sam_grove |
0:9dd7c6129683 | 124 | I2C0_IRQHandler |
sam_grove |
0:9dd7c6129683 | 125 | I2C1_IRQHandler |
sam_grove |
0:9dd7c6129683 | 126 | I2C2_IRQHandler |
sam_grove |
0:9dd7c6129683 | 127 | SPI_IRQHandler |
sam_grove |
0:9dd7c6129683 | 128 | SSP0_IRQHandler |
sam_grove |
0:9dd7c6129683 | 129 | SSP1_IRQHandler |
sam_grove |
0:9dd7c6129683 | 130 | PLL0_IRQHandler |
sam_grove |
0:9dd7c6129683 | 131 | RTC_IRQHandler |
sam_grove |
0:9dd7c6129683 | 132 | EINT0_IRQHandler |
sam_grove |
0:9dd7c6129683 | 133 | EINT1_IRQHandler |
sam_grove |
0:9dd7c6129683 | 134 | EINT2_IRQHandler |
sam_grove |
0:9dd7c6129683 | 135 | EINT3_IRQHandler |
sam_grove |
0:9dd7c6129683 | 136 | ADC_IRQHandler |
sam_grove |
0:9dd7c6129683 | 137 | BOD_IRQHandler |
sam_grove |
0:9dd7c6129683 | 138 | USB_IRQHandler |
sam_grove |
0:9dd7c6129683 | 139 | CAN_IRQHandler |
sam_grove |
0:9dd7c6129683 | 140 | DMA_IRQHandler |
sam_grove |
0:9dd7c6129683 | 141 | I2S_IRQHandler |
sam_grove |
0:9dd7c6129683 | 142 | ENET_IRQHandler |
sam_grove |
0:9dd7c6129683 | 143 | RIT_IRQHandler |
sam_grove |
0:9dd7c6129683 | 144 | MCPWM_IRQHandler |
sam_grove |
0:9dd7c6129683 | 145 | QEI_IRQHandler |
sam_grove |
0:9dd7c6129683 | 146 | PLL1_IRQHandler |
sam_grove |
0:9dd7c6129683 | 147 | USBActivity_IRQHandler |
sam_grove |
0:9dd7c6129683 | 148 | CANActivity_IRQHandler |
sam_grove |
0:9dd7c6129683 | 149 | */ |