i15765sid_function.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. #include "UDS.h"
  2. #include <stdio.h>
  3. //bootloader
  4. uint8_t DataFormatIdentifier = 0;
  5. uint8_t addressAndLengthFormatIdentifier = 0;
  6. const uint16_t MaxNumberOfBlockLength = 128;
  7. uint32_t load_MemoryAddress = 0;
  8. uint32_t load_MemorySize = 0;
  9. uint32_t load_BlockNum = 0;
  10. uint32_t load_BlockCount = 0;
  11. uint32_t earse_MemoryAddress = 0;
  12. uint32_t earse_MemorySize = 0;
  13. uint8_t load_sequence_state = 0;//0=请求升级,1=擦除成功进入数据传输流程,2=请求下载数据,3=正在下载数据,
  14. uint16_t BlockSequenceCounter = 0;
  15. uint16_t BlockSequenceCounter_pre = 0;
  16. uint32 CRC32_value = 0;
  17. uint32 CRC32_value_app2 = 0;
  18. uint32 WriteFlashAlreadySize = 0;
  19. uint8_t DiagnosticSessionType = 0x02;
  20. extern uint8_t ota_start_flag;
  21. uint8_t app2_crc_flag = 0;//crc校验成功标志位,成功后应答版本号位新的。
  22. void NegativeResponse(uint8_t sid,uint8_t NegativeResponseCode)
  23. {
  24. i15765_t msg_req;
  25. uint8_t buf[8];
  26. uint8_t status_rq;
  27. msg_req.ID = UDS_TX_ID;
  28. msg_req.buf = buf;
  29. buf[0] = 0x7F;//NegativeResponseServiceIdentifier
  30. buf[1] = sid;
  31. buf[2] = NegativeResponseCode;
  32. msg_req.buf_len = 3;
  33. /* transmit message */
  34. i15765_tx_app(&msg_req, &status_rq);
  35. }
  36. void SID_11_function(i15765_t *msg)
  37. {
  38. i15765_t msg_req;
  39. uint8_t buf[8];
  40. uint8_t status_rq;
  41. uint8_t PositiveResponseEnable = 0;
  42. uint8_t SuppressPosRspMsgIndicationBit;
  43. uint8_t ResetType;
  44. /* basic stuff */
  45. msg_req.buf = buf;
  46. msg_req.pri = 6;
  47. msg_req.ta = 0x0C;
  48. if(msg->tat == I15765_TAT_NF11)
  49. {
  50. msg_req.tat = I15765_TAT_NF11;
  51. }
  52. else
  53. {
  54. msg_req.tat = I15765_TAT_NP11;
  55. }
  56. #if 0
  57. if(DiagnosticSessionType==0x02)
  58. {
  59. NegativeResponse(msg->buf[0],0x7F);
  60. return;
  61. }
  62. #endif
  63. if(DiagnosticSessionType==0x01)
  64. {
  65. NegativeResponse(msg->buf[0],0x7F);
  66. return;
  67. }
  68. if(msg->buf_len!=2)
  69. {
  70. NegativeResponse(msg->buf[0],0x13);
  71. return;
  72. }
  73. SuppressPosRspMsgIndicationBit = msg->buf[1]&0x80;
  74. ResetType = msg->buf[1]&0x7F;
  75. switch(ResetType)
  76. {
  77. case 0x01:
  78. {
  79. if(1)
  80. {
  81. PositiveResponseEnable = 1;
  82. buf[0] = msg->buf[0]+0x40;// PositiveResponseServiceIdentifier
  83. buf[1] = msg->buf[1];// Sub-Function=ResetType
  84. msg_req.buf_len = 2;
  85. if(SuppressPosRspMsgIndicationBit != 0x80)
  86. {
  87. i15765_tx_app(&msg_req, &status_rq);
  88. }
  89. }
  90. else
  91. {
  92. PositiveResponseEnable = 0;
  93. NegativeResponse(msg->buf[0],0x22);
  94. return;
  95. }
  96. break;
  97. }
  98. case 0x03:
  99. {
  100. if(1)
  101. {
  102. PositiveResponseEnable = 1;
  103. buf[0] = msg->buf[0]+0x40;// PositiveResponseServiceIdentifier
  104. buf[1] = msg->buf[1];// Sub-Function=ResetType
  105. msg_req.buf_len = 2;
  106. if(SuppressPosRspMsgIndicationBit != 0x80)
  107. {
  108. i15765_tx_app(&msg_req, &status_rq);
  109. }
  110. }
  111. else
  112. {
  113. PositiveResponseEnable = 0;
  114. NegativeResponse(msg->buf[0],0x22);
  115. return;
  116. }
  117. break;
  118. }
  119. default:
  120. {
  121. NegativeResponse(msg->buf[0],0x12);
  122. return;
  123. }
  124. }
  125. if(PositiveResponseEnable==1)
  126. {
  127. if(ResetType == 0x01)//HardReset
  128. {
  129. //JumpReset();
  130. }
  131. if(ResetType == 0x03)//SoftReset
  132. {
  133. //JumpReset();
  134. }
  135. }
  136. }
  137. void SID_2E_function(i15765_t *msg)
  138. {
  139. //用于应答
  140. i15765_t msg_req;
  141. uint8_t buf[8];
  142. uint8_t status_rq;
  143. uint8_t PositiveResponseEnable = 0;//判断是否为肯定响应 1:肯定响应 0:否定响应
  144. uint16_t DID = 0;
  145. msg_req.buf = buf;
  146. //2 数据长度错误
  147. if(msg->buf_len < 4)
  148. {
  149. NegativeResponse(msg->buf[0],0x13);
  150. }
  151. //扩展诊断回话 或 编程会话
  152. if((DiagnosticSessionType==0x02)||(DiagnosticSessionType==0x03))
  153. {
  154. DID = (uint16_t)msg->buf[1]<<8 | msg->buf[2];
  155. //版本信息全读
  156. //EEPROM_read(VERSION_ADRESS,256,VersionInfo);
  157. switch(DID)
  158. {
  159. //VIN: 17 byte
  160. case 0xF190:
  161. {
  162. break;
  163. }
  164. default:
  165. {
  166. NegativeResponse(msg->buf[0],0x31);//请求超出范围
  167. break;
  168. }
  169. }
  170. }
  171. if(PositiveResponseEnable==1)
  172. {
  173. // EEPROM_write(VERSION_ADRESS,256,VersionInfo);
  174. buf[0] = msg->buf[0]+0x40; // PositiveResponseServiceIdentifier
  175. buf[1] = msg->buf[1]; // DID MSB
  176. buf[2] = msg->buf[2]; // DID LSB
  177. msg_req.buf_len = 3;
  178. /* transmit message */
  179. i15765_tx_app(&msg_req, &status_rq);
  180. }
  181. }
  182. void SID_31_function(i15765_t *msg)
  183. {
  184. i15765_t msg_req = {0};
  185. uint8_t response_buf[20] = {0};
  186. uint8_t status_rq = 0;
  187. uint8_t PositiveResponseEnable = 0;
  188. int oReturnCheck;//
  189. uint8_t result_code = 0x00;
  190. // 初始化请求消息
  191. msg_req.pri = 6;
  192. msg_req.ta = 0x0C;
  193. msg_req.buf = response_buf;
  194. if(msg->tat == I15765_TAT_NF11)
  195. {
  196. msg_req.tat = I15765_TAT_NF11;
  197. }
  198. else
  199. {
  200. msg_req.tat = I15765_TAT_NP11;
  201. }
  202. // 参数检查
  203. if (msg == NULL || msg->buf == NULL || msg->buf_len < 4)
  204. {
  205. NegativeResponse(0x31, 0x13); // 参数长度错误
  206. return;
  207. }
  208. // 检查RoutineControl类型
  209. uint8_t RoutineControlType = msg->buf[1] & 0x7F;
  210. if (RoutineControlType != 0x01) // 只支持StartRoutine
  211. {
  212. NegativeResponse(msg->buf[0], 0x12); // 不支持的服务类型
  213. return;
  214. }
  215. uint16_t RoutineIdentifier = (uint16_t)msg->buf[2] << 8 | msg->buf[3];
  216. // 处理不同的RoutineIdentifier
  217. switch (RoutineIdentifier)
  218. {
  219. case 0x0203: // 请求升级
  220. if (msg->buf_len != 4)
  221. {
  222. NegativeResponse(msg->buf[0], 0x13);
  223. return;
  224. }
  225. ota_start_flag = 1;
  226. result_code = 0x00;
  227. load_sequence_state = 0x00;
  228. app2_crc_flag = 0;
  229. PositiveResponseEnable = 1;
  230. break;
  231. case 0xFF00: // 擦除Flash
  232. if (msg->buf_len != 13)
  233. {
  234. NegativeResponse(msg->buf[0], 0x13);
  235. return;
  236. }
  237. // 提取擦除地址和大小
  238. earse_MemoryAddress = (uint32_t)((msg->buf[5] << 24) | (msg->buf[6] << 16) | (msg->buf[7] << 8) | msg->buf[8]);
  239. earse_MemorySize = (uint32_t)((msg->buf[9] << 24) | (msg->buf[10] << 16) | (msg->buf[11] << 8) | msg->buf[12]);
  240. // 验证地址范围
  241. //if ((earse_MemoryAddress != APP_START_ADDRESS) || (earse_MemorySize > (APP_STOP_ADDRESS - APP_START_ADDRESS)))
  242. //if ((earse_MemoryAddress != APP2_ADDRESS) || (earse_MemorySize > (APP2_ADDRESS - APP1_ADDRESS)))
  243. if ((earse_MemoryAddress != APP1_ADDRESS) || (earse_MemorySize > (APP2_ADDRESS - APP1_ADDRESS)))
  244. {
  245. NegativeResponse(msg->buf[0], 0x31);
  246. return;
  247. }
  248. // 执行擦除操作
  249. oReturnCheck = flash_erase_app(2);
  250. // 根据擦除结果设置响应
  251. result_code = (oReturnCheck == 0) ? 0x00 : 0x01;
  252. if (result_code == 0x00)
  253. {
  254. load_sequence_state = 0x01; // 进入数据传输流程
  255. }
  256. PositiveResponseEnable = 1;
  257. break;
  258. case 0x0202: // CRC32验证
  259. if (msg->buf_len != 8)
  260. {
  261. NegativeResponse(msg->buf[0], 0x13);
  262. return;
  263. }
  264. // 提取CRC32值
  265. CRC32_value = (uint32_t)(
  266. (msg->buf[4] << 24) |
  267. (msg->buf[5] << 16) |
  268. (msg->buf[6] << 8) |
  269. msg->buf[7]
  270. );
  271. // 验证CRC32
  272. result_code = (CRC32_value == crc32val) ? 0x00 : 0x01;
  273. if(result_code==0)//校验成功
  274. {
  275. app2_crc_flag = 1;
  276. }
  277. else
  278. {
  279. app2_crc_flag = 0;
  280. }
  281. PositiveResponseEnable = 1;
  282. break;
  283. case 0x0E0E: // 切换旧app版本
  284. if (msg->buf_len != 4)
  285. {
  286. NegativeResponse(msg->buf[0], 0x13);
  287. return;
  288. }
  289. response_buf[0] = msg->buf[0] + 0x40; // 正响应服务ID
  290. response_buf[1] = msg->buf[1]; // RoutineControlType
  291. response_buf[2] = msg->buf[2]; // RoutineIdentifier高字节
  292. response_buf[3] = msg->buf[3]; // RoutineIdentifier低字节
  293. response_buf[4] = 0; // 结果代码
  294. // 设置响应消息长度
  295. msg_req.buf_len = 5;
  296. // 发送响应
  297. i15765_tx_app(&msg_req, &status_rq);
  298. jump_to_app(APP1_ADDRESS);
  299. break;
  300. case 0x0E0F: // 切换新app版本
  301. if (msg->buf_len != 4)
  302. {
  303. NegativeResponse(msg->buf[0], 0x13);
  304. return;
  305. }
  306. #if 1
  307. //计算固件大小,判断是否可以双备份升级
  308. if(earse_MemorySize < (APP2_ADDRESS - APP1_ADDRESS))
  309. {
  310. //flash_copy_app();
  311. app2_copy_to_app1();
  312. //校验CRC
  313. crc32val = 0xFFFFFFFF;
  314. uint8_t app2_data[1];
  315. for(uint32_t i = 0;i < earse_MemorySize; i++)
  316. {
  317. app2_data[0] = *(volatile uint8_t*)(APP1_ADDRESS + i);
  318. crc32val = crc32(crc32val, app2_data, 1);
  319. }
  320. if(CRC32_value == crc32val)
  321. {
  322. response_buf[0] = msg->buf[0] + 0x40; // 正响应服务ID
  323. response_buf[1] = msg->buf[1]; // RoutineControlType
  324. response_buf[2] = msg->buf[2]; // RoutineIdentifier高字节
  325. response_buf[3] = msg->buf[3]; // RoutineIdentifier低字节
  326. response_buf[4] = 0; // 结果代码
  327. // 设置响应消息长度
  328. msg_req.buf_len = 5;
  329. // 发送响应
  330. i15765_tx_app(&msg_req, &status_rq);
  331. app_status_set(1,true);
  332. jump_to_app(APP1_ADDRESS);
  333. }
  334. else
  335. {
  336. response_buf[0] = msg->buf[0] + 0x40; // 正响应服务ID
  337. response_buf[1] = msg->buf[1]; // RoutineControlType
  338. response_buf[2] = msg->buf[2]; // RoutineIdentifier高字节
  339. response_buf[3] = msg->buf[3]; // RoutineIdentifier低字节
  340. response_buf[4] = 1; // 结果代码
  341. // 设置响应消息长度
  342. msg_req.buf_len = 5;
  343. // 发送响应
  344. i15765_tx_app(&msg_req, &status_rq);
  345. return;
  346. }
  347. }
  348. #endif
  349. default:
  350. NegativeResponse(msg->buf[0], 0x31); // 不支持的服务
  351. return;
  352. }
  353. // 发送正响应
  354. if (PositiveResponseEnable == 1)
  355. {
  356. // 构建响应数据
  357. response_buf[0] = msg->buf[0] + 0x40; // 正响应服务ID
  358. response_buf[1] = msg->buf[1]; // RoutineControlType
  359. response_buf[2] = msg->buf[2]; // RoutineIdentifier高字节
  360. response_buf[3] = msg->buf[3]; // RoutineIdentifier低字节
  361. response_buf[4] = result_code; // 结果代码
  362. // 设置响应消息长度
  363. msg_req.buf_len = 5;
  364. // 发送响应
  365. i15765_tx_app(&msg_req, &status_rq);
  366. }
  367. }
  368. void SID_34_function(i15765_t *msg)
  369. {
  370. i15765_t msg_req;
  371. uint8_t buf[8];
  372. uint8_t status_rq;
  373. uint8_t PositiveResponseEnable = 0;
  374. msg_req.buf = buf;
  375. if(msg->buf_len!=11)
  376. {
  377. NegativeResponse(msg->buf[0],0x13);
  378. return;
  379. }
  380. //3
  381. if(load_sequence_state != 0x01)
  382. {
  383. NegativeResponse(msg->buf[0],0x24); //(24)
  384. return;
  385. }
  386. DataFormatIdentifier = msg->buf[1];
  387. addressAndLengthFormatIdentifier = msg->buf[2];//0x44
  388. //Flashַ
  389. load_MemoryAddress = (uint32_t)((uint32_t)msg->buf[3]<<24)|((uint32_t)msg->buf[4]<<16)|((uint32_t)msg->buf[5]<<8)|msg->buf[6];
  390. load_MemorySize = (uint32_t)((uint32_t)msg->buf[7]<<24)|((uint32_t)msg->buf[8]<<16)|((uint32_t)msg->buf[9]<<8)|msg->buf[10];
  391. if(load_MemorySize%MaxNumberOfBlockLength == 0)
  392. {
  393. load_BlockNum = load_MemorySize/MaxNumberOfBlockLength;
  394. }
  395. else
  396. {
  397. load_BlockNum = load_MemorySize/MaxNumberOfBlockLength + 1;
  398. }
  399. #if 0
  400. //
  401. if(load_MemoryAddress != load_MemoryAddress_next)
  402. {
  403. NegativeResponse(msg->buf[0],0x31); //
  404. return;
  405. }
  406. load_MemoryAddress_next = load_MemoryAddress + load_MemorySize;
  407. //////////////////////////////////////////////////////////////////////////////////////////////////////////////
  408. #endif
  409. if((load_MemoryAddress == APP1_ADDRESS)&& load_MemorySize <= (APP2_ADDRESS - APP1_ADDRESS))
  410. {
  411. load_MemoryAddress = APP2_ADDRESS;//虽是APP1的固件地址,但写在备份区APP2地址
  412. PositiveResponseEnable = 1;
  413. buf[0] = msg->buf[0]+0x40; // PositiveResponseServiceIdentifier
  414. buf[1] = msg->buf[1]; // LengthFormatIdentifier
  415. if(MaxNumberOfBlockLength > 0xFF)
  416. {
  417. //MaxNumberOfBlockLength
  418. buf[2] = (uint8)((MaxNumberOfBlockLength>>8)&0xFF);
  419. buf[3] = (uint8)(MaxNumberOfBlockLength&0xFF);
  420. msg_req.buf_len = 4;
  421. }
  422. else
  423. {
  424. //MaxNumberOfBlockLength
  425. buf[2] = (uint8) MaxNumberOfBlockLength;
  426. msg_req.buf_len = 3;
  427. }
  428. load_sequence_state = 0x02;
  429. BlockSequenceCounter_pre = 0;
  430. crc32val = 0xFFFFFFFF;
  431. load_BlockCount = 0;
  432. }
  433. else
  434. {
  435. NegativeResponse(msg->buf[0],31);//
  436. return;
  437. }
  438. if(PositiveResponseEnable==1)
  439. {
  440. /* transmit message */
  441. i15765_tx_app(&msg_req, &status_rq);
  442. }
  443. }
  444. void SID_36_function(i15765_t *msg)
  445. {
  446. i15765_t msg_req;
  447. uint8_t buf[8];
  448. uint8_t status_rq;
  449. uint8_t PositiveResponseEnable = 0;
  450. uint16_t i = 0;
  451. int oReturnCheck;
  452. uint8_t TransferRequestParameterRecord[4096];
  453. msg_req.buf = buf;
  454. if(load_sequence_state != 0x02)
  455. {
  456. NegativeResponse(msg->buf[0],0x24);
  457. return;
  458. }
  459. BlockSequenceCounter = msg->buf[1];
  460. if(BlockSequenceCounter == 0x00)
  461. {
  462. if(BlockSequenceCounter_pre != 0xFF)
  463. {
  464. NegativeResponse(msg->buf[0],0x73);//
  465. return;
  466. }
  467. }
  468. else if(BlockSequenceCounter != (BlockSequenceCounter_pre+1))
  469. {
  470. NegativeResponse(msg->buf[0],0x73);//
  471. return;
  472. }
  473. BlockSequenceCounter_pre = BlockSequenceCounter;
  474. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  475. for(i=0; i < msg->buf_len-2;i++)
  476. {
  477. TransferRequestParameterRecord[i] = msg->buf[2+i];
  478. }
  479. crc32val = crc32(crc32val, TransferRequestParameterRecord, msg->buf_len-2);
  480. oReturnCheck = flash_write_page((load_MemoryAddress + load_BlockCount*MaxNumberOfBlockLength), TransferRequestParameterRecord, msg->buf_len-2, false);
  481. if(!oReturnCheck)
  482. {
  483. WriteFlashAlreadySize = WriteFlashAlreadySize + msg->buf_len-2;
  484. //
  485. PositiveResponseEnable = 1;
  486. buf[0] = msg->buf[0]+0x40; // PositiveResponseServiceIdentifier
  487. buf[1] = msg->buf[1]; // BlockSequenceCounter
  488. buf[2] = (uint8)((crc32val>>24)&0xFF);
  489. buf[3] = (uint8)((crc32val>>16)&0xFF);
  490. buf[4] = (uint8)((crc32val>>8)&0xFF);
  491. buf[5] = (uint8)((crc32val>>0)&0xFF);
  492. msg_req.buf_len = 6;
  493. /////////////////////////////////////////////////////////////////
  494. load_BlockCount++;
  495. if(load_BlockCount==load_BlockNum)
  496. {
  497. load_sequence_state = 0x03;
  498. }
  499. else
  500. {
  501. load_sequence_state = 0x02;
  502. }
  503. }
  504. else
  505. {
  506. // Indicate that the flash programming failed.
  507. NegativeResponse(msg->buf[0],0x70);//
  508. return;
  509. }
  510. ////////////////////////////////////////////////////////////////////
  511. if(PositiveResponseEnable==1)
  512. {
  513. /* transmit message */
  514. i15765_tx_app(&msg_req, &status_rq);
  515. }
  516. }
  517. void SID_37_function(i15765_t *msg)
  518. {
  519. i15765_t msg_req;
  520. uint8_t buf[8];
  521. uint8_t status_rq;
  522. /* basic stuff */
  523. msg_req.buf = buf;
  524. msg_req.pri = 6;
  525. msg_req.ta = 0x0C;
  526. if(msg->buf_len != 1)
  527. {
  528. NegativeResponse(msg->buf[0],0x13); //
  529. return;
  530. }
  531. // if(load_sequence_state != 0x02)
  532. if(load_sequence_state != 0x03)
  533. {
  534. NegativeResponse(msg->buf[0],0x24); //(24)
  535. return;
  536. }
  537. else
  538. {
  539. buf[0] = msg->buf[0]+0x40; // PositiveResponseServiceIdentifier
  540. msg_req.buf_len = 1;
  541. /* transmit message */
  542. i15765_tx_app(&msg_req, &status_rq);
  543. //
  544. load_sequence_state = 0x00;
  545. load_BlockCount = 0;
  546. BlockSequenceCounter_pre = 0;
  547. }
  548. }