Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
np_app_ncn_interface.c
00001 /* 00002 * np_app_ncn_interface.c 00003 * 00004 * Created on: July 14, 2016 00005 * Author: Alan.Lin 00006 * 00007 * Copyright: NexPack Ltd. 00008 */ 00009 00010 #include "np_config.h" 00011 #include "np_app_ncn_interface.h" 00012 #include "np_app_spi.h" 00013 #include "np_app_crc.h" 00014 #include "np_command.h" 00015 #include "np_apis.h" 00016 #include "np_app_pm.h" 00017 #include "np_driver_pm.h" 00018 #include "np_system.h" 00019 00020 uint8_t mdk_app_version[3] = {0x00}; //the code of developer version 00021 uint8_t mdk_mdk_version[3] = {0x00}; //the mdk version 00022 uint8_t t_message[64]; // buffer for message cache 00023 uint8_t source_address; // the message is coming from this port address 00024 volatile uint8_t flag_jump_bsl; //uint8_t flag_jump_bsl = 0x00; 00025 uint8_t my_node = 0x00; // To record "this" module's node number 00026 00027 /* 00028 * Description:MDK set a manually output address for next POST message 00029 * Parameter: null 00030 * Return: null 00031 */ 00032 void np_function_set_post_address(uint8_t address) { 00033 source_address = address; 00034 } 00035 00036 /* 00037 * Description: get one message from the FIFO buffer 00038 * Parameter : pData - pointer of the space for hold the one message 00039 * Return : the length of the message 00040 */ 00041 uint8_t np_function_ncn_interface_get_message(uint8_t *pData) { 00042 uint8_t t_i = 0x00; 00043 00044 // Clean the usage 00045 spi_get_buf.used_buffer_length -= spi_get_buf.message_len[spi_get_buf.readout_message_pointer]; 00046 // Case If: 00047 // One message over 79 to another 0 to 79 loop 00048 // Case else: 00049 // One message from n to m, but n and m is one same 0 to 79 loop 00050 if ( (spi_get_buf.readout_buffer_pointer + spi_get_buf.message_len[spi_get_buf.readout_message_pointer]) > FIFO_BUFFER_OVERLOAD ) { 00051 // Read until pointer reach 79 00052 while (spi_get_buf.readout_buffer_pointer <= FIFO_BUFFER_OVERLOAD ) { 00053 pData[t_i++] = spi_get_buf.buffer[spi_get_buf.readout_buffer_pointer++]; 00054 spi_get_buf.message_len[spi_get_buf.readout_message_pointer]--; 00055 } 00056 // Pointer reset to 0 00057 spi_get_buf.readout_buffer_pointer = 0; 00058 // Read out the rest 00059 while ( spi_get_buf.message_len[spi_get_buf.readout_message_pointer] ) { 00060 pData[t_i++] = spi_get_buf.buffer[spi_get_buf.readout_buffer_pointer++]; 00061 spi_get_buf.message_len[spi_get_buf.readout_message_pointer]--; 00062 } 00063 } else { 00064 while ( spi_get_buf.message_len[spi_get_buf.readout_message_pointer] ) { 00065 pData[t_i++] = spi_get_buf.buffer[spi_get_buf.readout_buffer_pointer++]; 00066 spi_get_buf.message_len[spi_get_buf.readout_message_pointer]--; 00067 } 00068 } 00069 00070 spi_get_buf.readout_message_pointer++; 00071 if ( spi_get_buf.readout_message_pointer > FIFO_MESSAGE_OVERLOAD) 00072 spi_get_buf.readout_message_pointer = 0; 00073 return t_i; 00074 } 00075 00076 /* 00077 * Description: add one message into the FIFO buffer 00078 * Parameter: 00079 * command :The command of this message; 00080 * pData :pointer of the new parameters inside the message, 00081 * pLen :the length of the parameters 00082 * Return: null 00083 */ 00084 uint8_t np_function_ncn_interface_post_message(uint16_t command, uint8_t *pData, uint8_t pLen) { 00085 uint8_t t_i = 0x00; 00086 uint16_t t_crc16 = 0x0000; 00087 uint8_t message_len = 0x00; 00088 00089 message_len = pLen+11; 00090 if ( (spi_post_buf.used_buffer_length+message_len) > FIFO_BUFFER_OVERLOAD ) { 00091 // Over buffer, ERROR! 00092 return 1; 00093 } 00094 // Assembling a message for sending out 00095 t_message[0] = 0x7E; 00096 t_message[1] = my_node; 00097 t_message[2] = source_address; 00098 t_message[3] = pLen+3; 00099 t_message[4] = command>>8; 00100 t_message[5] = command; 00101 t_message[6] = pLen; 00102 for ( t_i = 0; t_i < pLen; t_i++ ) 00103 t_message[7+t_i] = pData[t_i]; 00104 t_crc16 = np_function_crc_calculate(&t_message[1], pLen+6); 00105 t_message[7+pLen] = t_crc16>>8; 00106 t_message[8+pLen] = t_crc16; 00107 t_message[9+pLen] = 0x7E; 00108 t_message[10+pLen] = '$'; //keep first TX byte of next communication is '$' .Meaningless on here 00109 00110 // Fill in the information into the POST FIFO structure 00111 spi_post_buf.used_buffer_length += message_len; 00112 spi_post_buf.message_len[spi_post_buf.fillin_message_pointer++] = message_len; 00113 if ( spi_post_buf.fillin_message_pointer > FIFO_MESSAGE_OVERLOAD ) { 00114 spi_post_buf.fillin_message_pointer = 0; 00115 } 00116 t_i = 0; 00117 // Caes if: 00118 // The reset of the space need to put into the next buffer loop 00119 // Case else: 00120 // The reset of the space enough in the same buffer loop 00121 if ( (spi_post_buf.fillin_buffer_pointer+message_len) > FIFO_BUFFER_OVERLOAD ) { 00122 while ( spi_post_buf.fillin_buffer_pointer <= FIFO_BUFFER_OVERLOAD ) { 00123 spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; 00124 message_len--; 00125 } 00126 spi_post_buf.fillin_buffer_pointer = 0; 00127 while ( message_len ) { 00128 spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; 00129 message_len--; 00130 } 00131 } else { 00132 while ( message_len ) { 00133 spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; 00134 message_len--; 00135 } 00136 } 00137 00138 spi_post_buf.message_num++; 00139 00140 return 0; 00141 } 00142 00143 /* 00144 * Description: add one message into the FIFO buffer, but information especially to Gateway 00145 * Parameter: 00146 * command :The command of this message; 00147 * pData :pointer of the new parameters inside the message, 00148 * pLen :the length of the parameters 00149 * Return: null 00150 */ 00151 uint8_t np_function_ncn_interface_post_message_to_station(uint16_t command, uint8_t *pData, uint8_t pLen) { 00152 uint8_t t_i = 0x00; 00153 uint16_t t_crc16 = 0x0000; 00154 uint8_t message_len = 0x00; 00155 00156 message_len = pLen+11; 00157 if ( (spi_post_buf.used_buffer_length+message_len) > FIFO_BUFFER_OVERLOAD ) { 00158 // Over buffer, ERROR! 00159 return 1; 00160 } 00161 // Assembling a message for sending out 00162 t_message[0] = 0x7E; 00163 t_message[1] = my_node; 00164 t_message[2] = 0x01; 00165 t_message[3] = pLen+3; 00166 t_message[4] = command>>8; 00167 t_message[5] = command; 00168 t_message[6] = pLen; 00169 for ( t_i = 0; t_i < pLen; t_i++ ) 00170 t_message[7+t_i] = pData[t_i]; 00171 t_crc16 = np_function_crc_calculate(&t_message[1], pLen+6); 00172 t_message[7+pLen] = t_crc16>>8; 00173 t_message[8+pLen] = t_crc16; 00174 t_message[9+pLen] = 0x7E; 00175 t_message[10+pLen] = '$'; //keep first TX byte of next communication is '$' .Meaningless on here 00176 00177 // Fill in the information into the POST FIFO structure 00178 spi_post_buf.used_buffer_length += message_len; 00179 spi_post_buf.message_len[spi_post_buf.fillin_message_pointer++] = message_len; 00180 if ( spi_post_buf.fillin_message_pointer > FIFO_MESSAGE_OVERLOAD ) { 00181 spi_post_buf.fillin_message_pointer = 0; 00182 } 00183 t_i = 0; 00184 // Caes if: 00185 // The reset of the space need to put into the next buffer loop 00186 // Case else: 00187 // The reset of the space enough in the same buffer loop 00188 if ( (spi_post_buf.fillin_buffer_pointer+message_len) > FIFO_BUFFER_OVERLOAD ) { 00189 while ( spi_post_buf.fillin_buffer_pointer <= FIFO_BUFFER_OVERLOAD ) { 00190 spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; 00191 message_len--; 00192 } 00193 spi_post_buf.fillin_buffer_pointer = 0; 00194 while ( message_len ) { 00195 spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; 00196 message_len--; 00197 } 00198 } else { 00199 while ( message_len ) { 00200 spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; 00201 message_len--; 00202 } 00203 } 00204 00205 spi_post_buf.message_num++; 00206 00207 00208 return 0; 00209 } 00210 00211 00212 /* 00213 * Description: unpack the message and process it, here is the core of the message(s), 00214 * received -> understanding -> process 00215 * Parameter: null 00216 * Return: null 00217 */ 00218 void np_function_ncn_interface_parse(void) { 00219 uint16_t t_crc_value = 0x0000; 00220 uint16_t command = 0x0000; 00221 00222 np_function_ncn_interface_get_message(t_message); 00223 00224 // CRC16 Check 00225 t_crc_value = t_message[4+t_message[3]]; 00226 t_crc_value = (t_crc_value<<8)|t_message[5+t_message[3]]; 00227 if ( np_function_crc_check(&t_message[1], t_message[3]+3, t_crc_value) ) { 00228 // CRC16 NOT pass 00229 return; 00230 } 00231 00232 // If this message is for me? 00233 if ( t_message[2] != my_node ) { 00234 // NOT for me 00235 return; 00236 } 00237 00238 np_function_set_post_address(t_message[1]); 00239 00240 // Only one command inside payload, if more than one, this logic need to change 00241 command = t_message[4]; 00242 command = (command<<8)|t_message[5]; 00243 00244 // Developer command code from 0x2700-0x27ff 00245 if ( (command >= 0x2700)&&(command <= 0x27ff) ) { 00246 uint8_t t_i = 0; 00247 uint8_t t_we_have_response = FALSE; 00248 // Search and run the right function 00249 for ( ; t_i < mdk_cmd_func_number; t_i++ ) { 00250 if ( command == mdk_cmd_func_table_ptr[t_i].command ) { 00251 mdk_cmd_func_table_ptr[t_i].function(&t_message[7], t_message[6]); 00252 t_we_have_response = TRUE; 00253 } 00254 } 00255 // If the command is not existing, then we need to response a ERROR 00256 if ( t_we_have_response == FALSE ) { 00257 uint8_t data[2]; 00258 data[0] = command >> 8; 00259 data[0] = command & 0xFF; 00260 np_function_ncn_interface_post_message(ERROR_NO_CMD, data, 0x02); 00261 } 00262 } else { 00263 // NP define command code 00264 switch (command) { 00265 case ASK_APP_INFO:{ 00266 np_function_ncn_interface_post_message(ANS_APP_INFO, 00267 mdk_app_version, 0x03); 00268 } 00269 break; 00270 00271 case REQ_INTO_BSL:{ 00272 RAM_SOURCE_ADDR = source_address; 00273 flag_jump_bsl = 0x01; 00274 } 00275 break; 00276 00277 case CMD_MDK_REQ_START: { 00278 uint8_t data = SUCCESS; 00279 // np_api_start(); 00280 np_function_ncn_interface_post_message(CMD_MDK_RSP_START, &data, 00281 0x01); 00282 break; 00283 } 00284 case CMD_MDK_REQ_STOP: { 00285 uint8_t data = SUCCESS; 00286 // np_api_stop(); 00287 np_function_ncn_interface_post_message(CMD_MDK_RSP_STOP, &data, 00288 0x01); 00289 break; 00290 } 00291 00292 case CMD_MDK_REQ_LPM4:{ 00293 uint8_t data = SUCCESS; 00294 // if(t_message[7] == 1){ 00295 // np_function_lpm4_automode_set(); 00296 // if(np_function_pm_mode_get() & LPM_0){ 00297 // np_function_set_run_the_loop(TRUE); 00298 // } 00299 // }else if(t_message[7] == 0){ 00300 // if(developer_lpm4_setting == 0){ 00301 // np_function_lpm4_automode_clear(); 00302 // } 00303 // } 00304 np_function_ncn_interface_post_message(CMD_MDK_RSP_LPM4, &data,0x01); 00305 } 00306 break; 00307 00308 case ASK_STATUS: { 00309 uint8_t t_data[1] = { 0x01 }; 00310 np_function_ncn_interface_post_message(ANS_STATUS, t_data, 0x01); 00311 break; 00312 } 00313 00314 case REQ_TEST_COMM:{ 00315 #if CASE_TEST_CODE_ENABLE 00316 #else 00317 //MDK version 00318 np_function_ncn_interface_post_message(RSP_TEST_COMM,mdk_mdk_version, 0x03); 00319 #endif 00320 } 00321 break; 00322 00323 default: { 00324 uint8_t data[2]; 00325 data[0] = command >> 8; 00326 data[1] = command; 00327 np_function_ncn_interface_post_message(ERROR_NO_CMD, data, 0x02); 00328 break; 00329 } 00330 } 00331 } 00332 } 00333 00334 /* 00335 * Description: the message handler 00336 * Parameter: null 00337 * Return: null 00338 */ 00339 void np_function_ncn_interface_loop(void) { 00340 if ( spi_get_buf.message_num ) { 00341 np_function_ncn_interface_parse(); 00342 spi_get_buf.message_num--; 00343 } 00344 if ( spi_status == SPI_BUSY ) 00345 return; 00346 if ( spi_post_buf.message_num ) { 00347 if ( np_app_spi_is_aviable() ) { 00348 np_app_spi_apply_post(); 00349 } 00350 } 00351 } 00352
Generated on Tue Jul 12 2022 12:58:33 by
1.7.2