app_protocol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #include "app_protocol.h"
  2. #include "hc32_ll_mcan.h"
  3. #include "hal_uart.h"
  4. #include "hal_flash.h"
  5. #include "hal_spi.h"
  6. #include "hal_can.h"
  7. #include "project_config.h"
  8. static uint16_t g_dnpcrc_table[256] =
  9. {
  10. 0x0000, 0x365E, 0x6CBC, 0x5AE2, 0xD978, 0xEF26, 0xB5C4, 0x839A,
  11. 0xFF89, 0xC9D7, 0x9335, 0xA56B, 0x26F1, 0x10AF, 0x4A4D, 0x7C13,
  12. 0xB26B, 0x8435, 0xDED7, 0xE889, 0x6B13, 0x5D4D, 0x07AF, 0x31F1,
  13. 0x4DE2, 0x7BBC, 0x215E, 0x1700, 0x949A, 0xA2C4, 0xF826, 0xCE78,
  14. 0x29AF, 0x1FF1, 0x4513, 0x734D, 0xF0D7, 0xC689, 0x9C6B, 0xAA35,
  15. 0xD626, 0xE078, 0xBA9A, 0x8CC4, 0x0F5E, 0x3900, 0x63E2, 0x55BC,
  16. 0x9BC4, 0xAD9A, 0xF778, 0xC126, 0x42BC, 0x74E2, 0x2E00, 0x185E,
  17. 0x644D, 0x5213, 0x08F1, 0x3EAF, 0xBD35, 0x8B6B, 0xD189, 0xE7D7,
  18. 0x535E, 0x6500, 0x3FE2, 0x09BC, 0x8A26, 0xBC78, 0xE69A, 0xD0C4,
  19. 0xACD7, 0x9A89, 0xC06B, 0xF635, 0x75AF, 0x43F1, 0x1913, 0x2F4D,
  20. 0xE135, 0xD76B, 0x8D89, 0xBBD7, 0x384D, 0x0E13, 0x54F1, 0x62AF,
  21. 0x1EBC, 0x28E2, 0x7200, 0x445E, 0xC7C4, 0xF19A, 0xAB78, 0x9D26,
  22. 0x7AF1, 0x4CAF, 0x164D, 0x2013, 0xA389, 0x95D7, 0xCF35, 0xF96B,
  23. 0x8578, 0xB326, 0xE9C4, 0xDF9A, 0x5C00, 0x6A5E, 0x30BC, 0x06E2,
  24. 0xC89A, 0xFEC4, 0xA426, 0x9278, 0x11E2, 0x27BC, 0x7D5E, 0x4B00,
  25. 0x3713, 0x014D, 0x5BAF, 0x6DF1, 0xEE6B, 0xD835, 0x82D7, 0xB489,
  26. 0xA6BC, 0x90E2, 0xCA00, 0xFC5E, 0x7FC4, 0x499A, 0x1378, 0x2526,
  27. 0x5935, 0x6F6B, 0x3589, 0x03D7, 0x804D, 0xB613, 0xECF1, 0xDAAF,
  28. 0x14D7, 0x2289, 0x786B, 0x4E35, 0xCDAF, 0xFBF1, 0xA113, 0x974D,
  29. 0xEB5E, 0xDD00, 0x87E2, 0xB1BC, 0x3226, 0x0478, 0x5E9A, 0x68C4,
  30. 0x8F13, 0xB94D, 0xE3AF, 0xD5F1, 0x566B, 0x6035, 0x3AD7, 0x0C89,
  31. 0x709A, 0x46C4, 0x1C26, 0x2A78, 0xA9E2, 0x9FBC, 0xC55E, 0xF300,
  32. 0x3D78, 0x0B26, 0x51C4, 0x679A, 0xE400, 0xD25E, 0x88BC, 0xBEE2,
  33. 0xC2F1, 0xF4AF, 0xAE4D, 0x9813, 0x1B89, 0x2DD7, 0x7735, 0x416B,
  34. 0xF5E2, 0xC3BC, 0x995E, 0xAF00, 0x2C9A, 0x1AC4, 0x4026, 0x7678,
  35. 0x0A6B, 0x3C35, 0x66D7, 0x5089, 0xD313, 0xE54D, 0xBFAF, 0x89F1,
  36. 0x4789, 0x71D7, 0x2B35, 0x1D6B, 0x9EF1, 0xA8AF, 0xF24D, 0xC413,
  37. 0xB800, 0x8E5E, 0xD4BC, 0xE2E2, 0x6178, 0x5726, 0x0DC4, 0x3B9A,
  38. 0xDC4D, 0xEA13, 0xB0F1, 0x86AF, 0x0535, 0x336B, 0x6989, 0x5FD7,
  39. 0x23C4, 0x159A, 0x4F78, 0x7926, 0xFABC, 0xCCE2, 0x9600, 0xA05E,
  40. 0x6E26, 0x5878, 0x029A, 0x34C4, 0xB75E, 0x8100, 0xDBE2, 0xEDBC,
  41. 0x91AF, 0xA7F1, 0xFD13, 0xCB4D, 0x48D7, 0x7E89, 0x246B, 0x1235,
  42. };
  43. extern __IO en_flag_status_t spi_rx_complete_flag;
  44. //extern char spi_tx_buf[EXAMPLE_SPI_BUF_LEN];
  45. //extern char spi_rx_buf[EXAMPLE_SPI_BUF_LEN];
  46. extern QueueHandle_t can1_recieve2_data_queue;
  47. extern QueueHandle_t can2_recieve1_data_queue;
  48. static void creat_software_crc16(const uint8_t *pbuf, const uint32_t data_len, uint32_t *cur_crc);
  49. static unsigned short crc16_modbus(unsigned char *pdata, int len);
  50. static int8_t struct_convert_to_buffer(pack_t *pack,uint8_t *data);
  51. uint8_t flash_write_finish = false;
  52. int8_t pack_buffer[DATA_LENTH];
  53. gps_info_t gps_info;
  54. communication_data_t communication_data;
  55. output_control_t output_control;
  56. sleep_control_t sleep_control;
  57. communication_control_t communication_control;
  58. flash_write_t flash_write;
  59. sys_paratemer_info_t sys_paratemer_info = {0};
  60. SemaphoreHandle_t spi_communication_semaphore = NULL;
  61. static unsigned short crc16_modbus(unsigned char *pdata, int len)
  62. {
  63. int j = 0;
  64. int i = 0;
  65. uint16_t reg_crc = 0xffff;
  66. while (i < len )
  67. {
  68. reg_crc ^= pdata[i];
  69. i++;
  70. for (j = 0; j < 8; j++)
  71. {
  72. if ((reg_crc & 0x01) == 1)
  73. {
  74. reg_crc = (uint16_t)((reg_crc >> 1) ^ 0xa001);
  75. }
  76. else
  77. {
  78. reg_crc = (uint16_t)( reg_crc >> 1);
  79. }
  80. }
  81. }
  82. return reg_crc;
  83. }
  84. static void creat_software_crc16(const uint8_t *pbuf, const uint32_t data_len, uint32_t *cur_crc)
  85. {
  86. uint16_t crc = 0u;
  87. uint32_t index = 0u;
  88. for (index = 0u; index < data_len; index++)
  89. {
  90. crc = ( crc >> 8 ) ^ g_dnpcrc_table[( crc ^ pbuf[index]) & 0x00ff] ;
  91. }
  92. *cur_crc = (uint32_t)((~crc) & 0xFFFFu);
  93. }
  94. static int8_t struct_convert_to_buffer(pack_t *pack,uint8_t *data)
  95. {
  96. if(pack!=NULL && data!=NULL && pack->data_lenth<=DATA_LENTH)
  97. {
  98. data[0] = pack->frame_head;
  99. data[1] = pack->source_addr;
  100. data[2] = pack->destination_addr;
  101. data[3] = pack->data_head;
  102. data[4] = pack->cmd;
  103. data[5] = pack->cmd_type;
  104. data[6] = pack->data_lenth>>8;
  105. data[7] = pack->data_lenth & 0xff;
  106. memcpy(&data[8],pack->data,pack->data_lenth);
  107. data[pack->data_lenth+8] = pack->crc>>8;
  108. data[pack->data_lenth+9] = pack->crc&0xff;
  109. data[pack->data_lenth+10] = pack->frame_tail;
  110. return 0;
  111. }
  112. else
  113. {
  114. return -1;
  115. }
  116. }
  117. uint16_t Full_Frame_Verifi(uint8_t *srcdata, uint16_t srcLen, uint16_t* frame_start)
  118. {
  119. uint16_t frame_len = 0; // 获取帧长度
  120. uint8_t frame_flag = 0; //找到帧起始位
  121. for(uint32_t i = 0; i < (srcLen - sizeof(frame_pack_t)) && 0 == frame_flag; i++)
  122. {
  123. if(0X5A == srcdata[i])
  124. {
  125. frame_len = srcdata[i + FRAME_LEN_INDEX] + (srcdata[i + FRAME_LEN_INDEX + 1] << 8);
  126. if((i + frame_len + 1) < srcLen &&
  127. (srcdata[i + frame_len - 2] + (srcdata[i + frame_len - 1] << 8) == crc16_modbus(srcdata + i, frame_len -2)))
  128. {
  129. frame_flag = 1;
  130. *frame_start = i;
  131. break;
  132. }
  133. }
  134. }
  135. if(0 == frame_flag)
  136. return 0;
  137. else
  138. return frame_len;
  139. }
  140. void HardFault_Handler(void)
  141. {
  142. /* if Hard Fault exception occurs, go to infinite loop */
  143. while (1);
  144. }
  145. //sys_paratemer_info_t sys_paratemer_info_tmp = {0};
  146. //extern sys_paratemer_info_t sys_paratemer_info_tmp;
  147. uint8_t data_decode_mpu(uint8_t *input_data, uint32_t input_data_len, uint8_t* output, uint32_t* output_data_len)
  148. {
  149. uint16_t frame_start = 0; // 帧起始位
  150. uint16_t frame_len = 0;
  151. *output_data_len = 0;
  152. static frame_pack_t frame_tmp = {0};
  153. static frame_pack_t* frame_tmp_read = NULL;
  154. frame_tmp.frame_head = 0X5A;
  155. frame_tmp.destination_addr = MPU_ADDR;
  156. frame_tmp.source_addr = MCU_ADDR;
  157. frame_pack_t* frame_read_tmp;
  158. frame_len = Full_Frame_Verifi(input_data, input_data_len, &frame_start);
  159. if(0 == frame_len)
  160. return 0;
  161. frame_tmp_read = (frame_pack_t*)(input_data + frame_start);
  162. *output_data_len += sizeof(frame_pack_t);
  163. uint8_t* pack_data = input_data + sizeof(frame_pack_t) + frame_start;
  164. uint16_t pack_data_len = frame_len - sizeof(frame_pack_t) - 2; // 2结尾crc长度
  165. static flash_write_t* flash_tmp = NULL;
  166. static int frame_update_index = 0;
  167. switch(frame_tmp_read->cmd)
  168. {
  169. case 0X50:
  170. {
  171. frame_tmp.cmd = 0X50;
  172. switch(frame_tmp_read->cmd_type)
  173. {
  174. case 0X00: //log
  175. frame_tmp.cmd_type = 0X00;
  176. memcpy(output + *output_data_len, &mcu_decie_status, sizeof(cmd_05_01_device_status_t));
  177. *output_data_len += sizeof(cmd_05_01_device_status_t);
  178. break;
  179. case 0X01:
  180. frame_tmp.cmd_type = 0X01;
  181. memcpy(output + *output_data_len, &mcu_decie_status, sizeof(cmd_05_01_device_status_t));
  182. *output_data_len += sizeof(cmd_05_01_device_status_t);
  183. break;
  184. case 0X02:
  185. frame_tmp.cmd_type = 0X02;
  186. memcpy(output + *output_data_len, gps_05_02_data, gps_data_len);
  187. *output_data_len += gps_data_len;
  188. break;
  189. case 0X03:
  190. frame_tmp.cmd_type = 0X03;
  191. memcpy(output + *output_data_len, &com_05_03_data, sizeof(communication_data_t));
  192. *output_data_len += sizeof(communication_data_t);
  193. break;
  194. default:
  195. *output_data_len = 0;
  196. return 0;
  197. break;
  198. }
  199. break;
  200. }
  201. case 0X60:
  202. {
  203. frame_tmp.cmd = 0X60;
  204. switch(frame_tmp_read->cmd_type)
  205. {
  206. case 0X01:
  207. {
  208. *output_data_len = 0;
  209. break;
  210. }
  211. case 0X02:
  212. {
  213. *output_data_len = 0;
  214. break;
  215. }
  216. case 0X03:
  217. {
  218. frame_tmp.cmd_type = 0X03;
  219. #define SECTOR_SIZE_W (256)
  220. #define HEAD_FLASH_SIZE_03 (3)
  221. #define CRC_LEN_03 (2)
  222. flash_tmp = (flash_write_t * )(pack_data);
  223. frame_update_index = flash_tmp->write_data_index;
  224. static uint8_t flashW[SECTOR_SIZE_W] = {0};
  225. static uint16 flash_index = 0;
  226. static uint32_t write_flash_offset_addr = 0;
  227. uint16 flash_index_now = 0;
  228. uint8 flash_write_read_data[SECTOR_SIZE_W] = {0};
  229. uint16 dataLenNow = pack_data_len - HEAD_FLASH_SIZE_03 - CRC_LEN_03;
  230. flash_index_now = flash_tmp->write_data_index;
  231. if(1 == flash_tmp->data_type && flash_index == flash_index_now)
  232. {
  233. SPI_Cmd(MPU_SPI_UNIT, DISABLE);
  234. //写入flash
  235. memcpy(flashW, pack_data + HEAD_FLASH_SIZE_03, dataLenNow);
  236. nca9555_set_gpio_output_status(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03,HIGH);
  237. hc32_flash_write(APP_B_ADDR+write_flash_offset_addr, flashW, dataLenNow);
  238. nca9555_set_gpio_output_status(SLAVE_ADDR0,GPIO_PORT0,GPIO_Pin00|GPIO_Pin01|GPIO_Pin02|GPIO_Pin03,LOW);
  239. hc32_flash_read(APP_B_ADDR+write_flash_offset_addr, flash_write_read_data, dataLenNow);
  240. uint16_t read_crc = crc16_modbus(flash_write_read_data, dataLenNow);
  241. if((pack_data[HEAD_FLASH_SIZE_03 + dataLenNow] + ((pack_data[HEAD_FLASH_SIZE_03 + dataLenNow + 1]) << 8))\
  242. == read_crc )
  243. {
  244. write_flash_offset_addr += dataLenNow;
  245. flash_index += 1;
  246. }
  247. else
  248. {
  249. __nop();
  250. //crc错误
  251. }
  252. SPI_Cmd(MPU_SPI_UNIT, ENABLE);
  253. }
  254. else if(0 == flash_tmp->data_type && flash_index == flash_index_now)//整包crc验证
  255. {
  256. uint16_t all_data_crc = pack_data[HEAD_FLASH_SIZE_03] + (pack_data[HEAD_FLASH_SIZE_03 + 1] << 8);
  257. uint32_t all_data_lenth = pack_data[HEAD_FLASH_SIZE_03 + 2] + (pack_data[HEAD_FLASH_SIZE_03 + 3] << 8);
  258. uint32_t calculate_crc;
  259. creat_software_crc16((const uint8_t *)APP_B_ADDR,all_data_lenth,&calculate_crc);
  260. hc32_flash_read(BOOT_INFO_ADDR,(uint8_t*)&sys_paratemer_info,sizeof(sys_paratemer_info));
  261. sys_paratemer_info.default_init=0x8CA5;
  262. sys_paratemer_info.destination_address = (uint32_t)APP_A_ADDR;
  263. sys_paratemer_info.is_can_write_flag = 0;
  264. if(all_data_crc == calculate_crc)
  265. {
  266. sys_paratemer_info.is_ota_flag = 1;
  267. }
  268. else // a app->
  269. {
  270. //crc校验出错 升级失败
  271. sys_paratemer_info.is_ota_flag = 0;
  272. //transmit_tx_data->cmd = SLAVE_CTR_NACK; //crc校验出错 升级失败
  273. }
  274. hc32_flash_write(BOOT_INFO_ADDR,(uint8_t*)&sys_paratemer_info,sizeof(sys_paratemer_info));
  275. hc32_flash_read(BOOT_INFO_ADDR,(uint8_t*)&sys_paratemer_info,sizeof(sys_paratemer_info));
  276. __disable_irq();//关闭所有中断
  277. NVIC_SystemReset();
  278. //flash_write.write_data_count = 0;
  279. }
  280. output[(*output_data_len)++] = flash_index & 0XFF;
  281. output[(*output_data_len)++] = (flash_index >> 8) & 0XFF;
  282. frame_tmp.data_lenth = *output_data_len + 2;
  283. memcpy(output, &frame_tmp, sizeof(frame_pack_t)); //帧固定帧在此复制
  284. uint16_t crc16 = crc16_modbus(output, *output_data_len);
  285. output[(*output_data_len)++] = crc16 & 0XFF;
  286. output[(*output_data_len)++] = (crc16 >> 8) & 0XFF;
  287. return frame_len;
  288. break;
  289. }
  290. case 0X04:
  291. {
  292. break;
  293. }
  294. default:
  295. {
  296. *output_data_len = 0;
  297. return 0;
  298. break;
  299. }
  300. }
  301. }
  302. default:
  303. {
  304. *output_data_len = 0;
  305. return 0;
  306. break;
  307. }
  308. }
  309. frame_tmp.data_lenth = *output_data_len + 2;
  310. memcpy(output, &frame_tmp, sizeof(frame_pack_t)); //帧固定帧在此复制
  311. uint16_t crc16 = crc16_modbus(output, *output_data_len);
  312. output[(*output_data_len)++] = crc16 & 0XFF;
  313. output[(*output_data_len)++] = (crc16 >> 8) & 0XFF;
  314. return frame_len;
  315. }
  316. void mcu_to_mpu_task(void *arg)
  317. {
  318. can1_recieve2_data_queue = xQueueCreate(5,sizeof(hc32_can_rx_msg_t));//can1存放非升级帧的队列
  319. can2_recieve1_data_queue = xQueueCreate(5,sizeof(hc32_can_rx_msg_t));//can2存放非升级帧的队列
  320. spi_communication_semaphore = xSemaphoreCreateBinary();
  321. while(1)
  322. {
  323. if(pdTRUE == xSemaphoreTake(spi_communication_semaphore,1))
  324. {
  325. // data_process(spi_rx_buf,spi_tx_buf);
  326. // memset(spi_rx_buf,0,sizeof(spi_rx_buf));
  327. }
  328. }
  329. }