Modifications in 4DGL library. Added CDU_hw_sw.h for version info. Added pins.h for hardware pin remapping
Dependencies: 4DGL-UC MODSERIAL mbed mbos
Fork of CDU_Mbed_30 by
USB_receive_6.cpp@21:f348e6f0f7d4, 2014-10-10 (annotated)
- Committer:
- WillemBraat
- Date:
- Fri Oct 10 18:23:36 2014 +0000
- Revision:
- 21:f348e6f0f7d4
- Parent:
- 14:5767d651d624
Additional files: CDU_Maintenance.cpp / mbps_def3.h
; Rewrite of main() function for power checks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LvdK | 14:5767d651d624 | 1 | // L. van der Kolk, ELVEDEKA, Holland // |
LvdK | 14:5767d651d624 | 2 | // File: USB_receive_6.cpp |
LvdK | 14:5767d651d624 | 3 | |
LvdK | 14:5767d651d624 | 4 | #include "mbed.h" |
LvdK | 14:5767d651d624 | 5 | #include "MODSERIAL.h" |
LvdK | 14:5767d651d624 | 6 | |
LvdK | 14:5767d651d624 | 7 | extern MODSERIAL USB; |
LvdK | 14:5767d651d624 | 8 | extern int FSdata_received_flag; |
LvdK | 14:5767d651d624 | 9 | |
LvdK | 14:5767d651d624 | 10 | //------- debug only --------------------- |
LvdK | 14:5767d651d624 | 11 | //extern MODSERIAL SERIAL_DEBUG; |
LvdK | 14:5767d651d624 | 12 | //---------------------------------------- |
LvdK | 14:5767d651d624 | 13 | |
LvdK | 14:5767d651d624 | 14 | void decode_string(int nummer_of_chars); |
LvdK | 14:5767d651d624 | 15 | void read_datafields(int command_number); |
LvdK | 14:5767d651d624 | 16 | |
LvdK | 14:5767d651d624 | 17 | #define max_string_length 100 // : max length of received string starting with $ and ending with CR/LF |
LvdK | 14:5767d651d624 | 18 | #define min_string_length 10 // : min length of received string starting with $ and ending with CR/LF |
LvdK | 14:5767d651d624 | 19 | char string_received[max_string_length + 3]; // : holds received string starting with $ and ending with CR/LF |
LvdK | 14:5767d651d624 | 20 | |
LvdK | 14:5767d651d624 | 21 | #define message_header "$PCDU" // : common message header in all messages |
LvdK | 14:5767d651d624 | 22 | #define max_commas 10 // : max. nr of possible field separating commas in a valid message string to CDU |
LvdK | 14:5767d651d624 | 23 | int comma[max_commas]; // : array with positions of all found commas in string_receved[] |
LvdK | 14:5767d651d624 | 24 | |
LvdK | 14:5767d651d624 | 25 | #define max_nr_of_commands 13 // : max nr of possible FS-to-CDU commands |
LvdK | 14:5767d651d624 | 26 | // Define array of pointers to possible FS-to-CDU commands with 3 characters: |
LvdK | 14:5767d651d624 | 27 | const char *command[max_nr_of_commands] = { |
LvdK | 14:5767d651d624 | 28 | "123", // : no valid CDU command nr. 0 , used for debugging only |
LvdK | 14:5767d651d624 | 29 | "MSG", // : command nr. 1 |
LvdK | 14:5767d651d624 | 30 | "EXC", // : command nr. 2 |
LvdK | 14:5767d651d624 | 31 | "BLT", // : command nr. 3 |
LvdK | 14:5767d651d624 | 32 | "SBY", // : command nr. 4 |
LvdK | 14:5767d651d624 | 33 | "CLS", // : command nr. 5 |
LvdK | 14:5767d651d624 | 34 | "SBC", // : command nr. 6 |
LvdK | 14:5767d651d624 | 35 | "WTX", // : command nr. 7 |
LvdK | 14:5767d651d624 | 36 | "ETX", // : command nr. 8 |
LvdK | 14:5767d651d624 | 37 | "KTX", // : command nr. 9 |
LvdK | 14:5767d651d624 | 38 | "FAI", // : command nr.10 |
LvdK | 14:5767d651d624 | 39 | "DPY", // : command nr.11 |
LvdK | 14:5767d651d624 | 40 | "OFS", // : command nr.12 |
LvdK | 14:5767d651d624 | 41 | }; |
LvdK | 14:5767d651d624 | 42 | |
LvdK | 14:5767d651d624 | 43 | void collect_FSdata() { |
LvdK | 14:5767d651d624 | 44 | // Function reads characters from FS written in receive buffer. |
LvdK | 14:5767d651d624 | 45 | // Wiil be called after each RX receive interrupt, so characters will allways be stored. |
LvdK | 14:5767d651d624 | 46 | // When analyze_busy is false, function will start reading characters from defined buffer and |
LvdK | 14:5767d651d624 | 47 | // collects strings starting with $ and ending with CR/LF. |
LvdK | 14:5767d651d624 | 48 | // Strings shorter than min_string_length or longer than max_string_length will be ignored, |
LvdK | 14:5767d651d624 | 49 | // others will be analyzed further. |
LvdK | 14:5767d651d624 | 50 | // After analyzing, flag analyze_busy will be set to false again. |
LvdK | 14:5767d651d624 | 51 | static int $_detected = false; // : no valid begin of string detected (init only on first call) |
LvdK | 14:5767d651d624 | 52 | static int string_pntr = 0; // : pointer at begin of string (init only on first call) |
LvdK | 14:5767d651d624 | 53 | static int nr_of_received_char = 0; // : counter of received characters (init only on first call) |
LvdK | 14:5767d651d624 | 54 | char rx_char; // : character which is analyzed |
LvdK | 14:5767d651d624 | 55 | |
LvdK | 14:5767d651d624 | 56 | if ( FSdata_received_flag == false ) { // : process received chars only if no decoding busy now |
LvdK | 14:5767d651d624 | 57 | while ( !USB.rxBufferEmpty() ) // : do as long as USB receive buffer is not empty |
LvdK | 14:5767d651d624 | 58 | { rx_char = USB.getc(); // : get a char from Rx buffer |
LvdK | 14:5767d651d624 | 59 | |
LvdK | 14:5767d651d624 | 60 | // ----------- Debug only ! --------------------------------------------- |
LvdK | 14:5767d651d624 | 61 | //SERIAL_DEBUG.putc(rx_char); // : unprotected immediate echo of char |
LvdK | 14:5767d651d624 | 62 | // ---------------------------------------------------------------------- |
LvdK | 14:5767d651d624 | 63 | |
LvdK | 14:5767d651d624 | 64 | // Check for string starting with $ char: |
LvdK | 14:5767d651d624 | 65 | if ( rx_char == '$' && $_detected == false ){ |
LvdK | 14:5767d651d624 | 66 | $_detected = true; // : begin of string is detected |
LvdK | 14:5767d651d624 | 67 | string_pntr = 0; // : set pointer to begin of string_received[] buffer |
LvdK | 14:5767d651d624 | 68 | } |
LvdK | 14:5767d651d624 | 69 | string_received[string_pntr] = rx_char; |
LvdK | 14:5767d651d624 | 70 | string_pntr++; |
LvdK | 14:5767d651d624 | 71 | if (string_pntr >= max_string_length) { |
LvdK | 14:5767d651d624 | 72 | // command string looks too long, so start all over again: |
LvdK | 14:5767d651d624 | 73 | string_pntr = 0; // : set pointer back to begin of string_received[] again |
LvdK | 14:5767d651d624 | 74 | nr_of_received_char = 0; // : reset number of received chars |
LvdK | 14:5767d651d624 | 75 | $_detected = false; |
LvdK | 14:5767d651d624 | 76 | FSdata_received_flag = false; |
LvdK | 14:5767d651d624 | 77 | } |
LvdK | 14:5767d651d624 | 78 | if ( rx_char == '\r' && $_detected == true ) { |
LvdK | 14:5767d651d624 | 79 | if ( string_pntr > min_string_length ) { // : check minimum string length |
LvdK | 14:5767d651d624 | 80 | // Received string can be interesting now because |
LvdK | 14:5767d651d624 | 81 | // it starts with '$' AND it ends on New-Line AND |
LvdK | 14:5767d651d624 | 82 | // it has a minimum length AND it is not too long: |
LvdK | 14:5767d651d624 | 83 | string_received[string_pntr] = '\0'; // : mark end of string |
LvdK | 14:5767d651d624 | 84 | |
LvdK | 14:5767d651d624 | 85 | FSdata_received_flag = true; |
LvdK | 14:5767d651d624 | 86 | |
LvdK | 14:5767d651d624 | 87 | // ----------- Debug only ! ------------------------------------------- |
LvdK | 14:5767d651d624 | 88 | // SERIAL_DEBUG.printf("string_received from USB: %s",string_received ); |
LvdK | 14:5767d651d624 | 89 | // -------------------------------------------------------------------- |
LvdK | 14:5767d651d624 | 90 | |
LvdK | 14:5767d651d624 | 91 | nr_of_received_char = string_pntr; |
LvdK | 14:5767d651d624 | 92 | //Call decoder to analyse this string: |
LvdK | 14:5767d651d624 | 93 | decode_string(nr_of_received_char); |
LvdK | 14:5767d651d624 | 94 | // Get ready for receiving new commands: |
LvdK | 14:5767d651d624 | 95 | $_detected = false; |
LvdK | 14:5767d651d624 | 96 | string_pntr = 0; // : set pointer back to begin of string_received[] again |
LvdK | 14:5767d651d624 | 97 | nr_of_received_char = 0; // : reset number of received chars |
LvdK | 14:5767d651d624 | 98 | } |
LvdK | 14:5767d651d624 | 99 | else { |
LvdK | 14:5767d651d624 | 100 | $_detected = false; |
LvdK | 14:5767d651d624 | 101 | string_pntr = 0; // : set pointer back to begin of string_received[] again |
LvdK | 14:5767d651d624 | 102 | nr_of_received_char = 0; // : reset number of received chars |
LvdK | 14:5767d651d624 | 103 | FSdata_received_flag = false; |
LvdK | 14:5767d651d624 | 104 | } |
LvdK | 14:5767d651d624 | 105 | } |
LvdK | 14:5767d651d624 | 106 | } |
LvdK | 14:5767d651d624 | 107 | |
LvdK | 14:5767d651d624 | 108 | } |
LvdK | 14:5767d651d624 | 109 | } |
LvdK | 14:5767d651d624 | 110 | |
LvdK | 14:5767d651d624 | 111 | void decode_string(int nummer_of_chars) |
LvdK | 14:5767d651d624 | 112 | { // -- This function decodes a received $.....CR/LF string written in string_received[] -- |
LvdK | 14:5767d651d624 | 113 | // First it checks for a valid checksum and reads the positions of commas in the string. |
LvdK | 14:5767d651d624 | 114 | // If checksum is OK, it will continue to look for valid 3 char FS-to-CDU commands. |
LvdK | 14:5767d651d624 | 115 | // When a valid command is found, data fields will be analyzed further using the found positions |
LvdK | 14:5767d651d624 | 116 | // of commas in the string. |
LvdK | 14:5767d651d624 | 117 | int i,c, equal; |
LvdK | 14:5767d651d624 | 118 | char byte_read, exor_byte; |
LvdK | 14:5767d651d624 | 119 | char command_string[6], received_checksum[4], calc_checksum[4]; |
LvdK | 14:5767d651d624 | 120 | // Get checksum and position of commas in string_received[] : |
LvdK | 14:5767d651d624 | 121 | exor_byte = 0; |
LvdK | 14:5767d651d624 | 122 | i = 1; // : i points to first char after $ |
LvdK | 14:5767d651d624 | 123 | c = 1; // : position of first comma |
LvdK | 14:5767d651d624 | 124 | do { |
LvdK | 14:5767d651d624 | 125 | byte_read = string_received[i]; |
LvdK | 14:5767d651d624 | 126 | if (byte_read == ',' && c < max_commas) { |
LvdK | 14:5767d651d624 | 127 | comma[c] = i; |
LvdK | 14:5767d651d624 | 128 | c++; |
LvdK | 14:5767d651d624 | 129 | } |
LvdK | 14:5767d651d624 | 130 | if (byte_read == '*') break; |
LvdK | 14:5767d651d624 | 131 | exor_byte = exor_byte ^ byte_read; |
LvdK | 14:5767d651d624 | 132 | i++; |
LvdK | 14:5767d651d624 | 133 | } while ( i < nummer_of_chars ); |
LvdK | 14:5767d651d624 | 134 | |
LvdK | 14:5767d651d624 | 135 | |
LvdK | 14:5767d651d624 | 136 | // ----------- Debug only ------------------------------------------- |
LvdK | 14:5767d651d624 | 137 | //SERIAL_DEBUG.printf("commas found : %d\n",c-1 ); // : show commas |
LvdK | 14:5767d651d624 | 138 | // ------------------------------------------------------------------ |
LvdK | 14:5767d651d624 | 139 | |
LvdK | 14:5767d651d624 | 140 | i++; // : i points to first checksum char after char * |
LvdK | 14:5767d651d624 | 141 | strncpy(received_checksum,&string_received[i],2); // : copy 2 char checksum after * |
LvdK | 14:5767d651d624 | 142 | // Get calculated checksum by transforming exor_byte in 2 hex chars (with upper case A-F) : |
LvdK | 14:5767d651d624 | 143 | sprintf(calc_checksum,"%02X",exor_byte); // : + extra NULL char added by sprintf |
LvdK | 14:5767d651d624 | 144 | equal = strncmp(received_checksum,calc_checksum,2); |
LvdK | 14:5767d651d624 | 145 | |
LvdK | 14:5767d651d624 | 146 | // ****************************************************************************************** |
LvdK | 14:5767d651d624 | 147 | // -- Force checksum to allways OK : < -- debug only !! |
LvdK | 14:5767d651d624 | 148 | equal = 0; |
LvdK | 14:5767d651d624 | 149 | // ****************************************************************************************** |
LvdK | 14:5767d651d624 | 150 | |
LvdK | 14:5767d651d624 | 151 | if (equal != 0) { |
LvdK | 14:5767d651d624 | 152 | // ----------- Debug only ------------------------------------------------------- |
LvdK | 14:5767d651d624 | 153 | //SERIAL_DEBUG.printf("checksum is NOT OK ! \n" ); // : show message for debugging |
LvdK | 14:5767d651d624 | 154 | // ------------------------------------------------------------------------------ |
LvdK | 14:5767d651d624 | 155 | FSdata_received_flag = false; |
LvdK | 14:5767d651d624 | 156 | } |
LvdK | 14:5767d651d624 | 157 | else { // checksum is OK, go on: |
LvdK | 14:5767d651d624 | 158 | // Check for 5 char "$PCDU" header: |
LvdK | 14:5767d651d624 | 159 | equal = strncmp(string_received,message_header,strlen(message_header)); |
LvdK | 14:5767d651d624 | 160 | if (equal != 0) { |
LvdK | 14:5767d651d624 | 161 | |
LvdK | 14:5767d651d624 | 162 | // ----------- Debug only --------------------------------------------------- |
LvdK | 14:5767d651d624 | 163 | //SERIAL_DEBUG.printf("no $PCDU header in message !\n" ); // : show message |
LvdK | 14:5767d651d624 | 164 | // -------------------------------------------------------------------------- |
LvdK | 14:5767d651d624 | 165 | |
LvdK | 14:5767d651d624 | 166 | FSdata_received_flag = false; |
LvdK | 14:5767d651d624 | 167 | } |
LvdK | 14:5767d651d624 | 168 | else { |
LvdK | 14:5767d651d624 | 169 | // Read 3 char command after message_header: |
LvdK | 14:5767d651d624 | 170 | strncpy(command_string,&string_received[strlen(message_header)],3); |
LvdK | 14:5767d651d624 | 171 | |
LvdK | 14:5767d651d624 | 172 | // ----------- Debug only --------------------------------------------------------------------- |
LvdK | 14:5767d651d624 | 173 | //SERIAL_DEBUG.printf("\ncommand found : %3s\n",command_string ); // : show command for debugging |
LvdK | 14:5767d651d624 | 174 | //SERIAL_DEBUG.printf("commas found : %d\n",c-1 ); // : show commas for debugging |
LvdK | 14:5767d651d624 | 175 | // -------------------------------------------------------------------------------------------- |
LvdK | 14:5767d651d624 | 176 | |
LvdK | 14:5767d651d624 | 177 | // Compare found string with known 3 char command list: |
LvdK | 14:5767d651d624 | 178 | i = 0; |
LvdK | 14:5767d651d624 | 179 | do { |
LvdK | 14:5767d651d624 | 180 | equal = strncmp(&command_string[0],command[i],3); |
LvdK | 14:5767d651d624 | 181 | if( equal == 0) break; |
LvdK | 14:5767d651d624 | 182 | i++; |
LvdK | 14:5767d651d624 | 183 | } while ( i < max_nr_of_commands); |
LvdK | 14:5767d651d624 | 184 | |
LvdK | 14:5767d651d624 | 185 | // ----------- Debug only --------------------------------------------------------------- |
LvdK | 14:5767d651d624 | 186 | //SERIAL_DEBUG.printf("command number is : %d\n",i ); // : show command nr for debugging |
LvdK | 14:5767d651d624 | 187 | // -------------------------------------------------------------------------------------- |
LvdK | 14:5767d651d624 | 188 | |
LvdK | 14:5767d651d624 | 189 | if (equal == 0) { |
LvdK | 14:5767d651d624 | 190 | // Command is known now, so now read all data fields: |
LvdK | 14:5767d651d624 | 191 | read_datafields(i); |
LvdK | 14:5767d651d624 | 192 | } |
LvdK | 14:5767d651d624 | 193 | else FSdata_received_flag = false; |
LvdK | 14:5767d651d624 | 194 | } |
LvdK | 14:5767d651d624 | 195 | } |
LvdK | 14:5767d651d624 | 196 | |
LvdK | 14:5767d651d624 | 197 | } |
LvdK | 14:5767d651d624 | 198 |