uart_update.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "app_config.h"
  2. #if(USER_UART_UPDATE_ENABLE) && (UART_UPDATE_ROLE == UART_UPDATE_SLAVE)
  3. #include "typedef.h"
  4. #include "update_loader_download.h"
  5. #include "os/os_api.h"
  6. #include "system/task.h"
  7. #include "update.h"
  8. #include "gpio.h"
  9. #include "uart_update.h"
  10. #include "asm/uart_dev.h"
  11. #include "asm/clock.h"
  12. static volatile u32 uart_to_cnt = 0;
  13. static volatile u32 uart_file_offset = 0;
  14. static volatile u16 rx_cnt; //收数据计数
  15. static void (*uart_update_resume_hdl)(void *priv) = NULL;
  16. static int (*uart_update_sleep_hdl)(void *priv) = NULL;
  17. #define LOG_TAG "[UART_UPDATE]"
  18. #define LOG_ERROR_ENABLE
  19. #define LOG_DEBUG_ENABLE
  20. #define LOG_INFO_ENABLE
  21. #define LOG_CLI_ENABLE
  22. #include "debug.h"
  23. #define RETRY_TIME 4//重试n次
  24. #define PACKET_TIMEOUT 200//ms
  25. //命令
  26. #define CMD_UPDATE_START 0x01
  27. #define CMD_UPDATE_READ 0x02
  28. #define CMD_UPDATE_END 0x03
  29. #define CMD_SEND_UPDATE_LEN 0x04
  30. #define CMD_KEEP_ALIVE 0x05
  31. #define READ_LIT_U16(a) (*((u8*)(a)) + (*((u8*)(a)+1)<<8))
  32. #define READ_LIT_U32(a) (*((u8*)(a)) + (*((u8*)(a)+1)<<8) + (*((u8*)(a)+2)<<16) + (*((u8*)(a)+3)<<24))
  33. #define WRITE_LIT_U16(a,src) {*((u8*)(a)+1) = (u8)(src>>8); *((u8*)(a)+0) = (u8)(src&0xff); }
  34. #define WRITE_LIT_U32(a,src) {*((u8*)(a)+3) = (u8)((src)>>24); *((u8*)(a)+2) = (u8)(((src)>>16)&0xff);*((u8*)(a)+1) = (u8)(((src)>>8)&0xff);*((u8*)(a)+0) = (u8)((src)&0xff);}
  35. #define THIS_TASK_NAME "uart_update"
  36. #define NEED_TO_EXIT_LOW_POWER_AND_SNIFF 1
  37. #if NEED_TO_EXIT_LOW_POWER_AND_SNIFF
  38. #include "asm/power_interface.h"
  39. #endif
  40. static protocal_frame_t protocal_frame __attribute__((aligned(4)));
  41. u32 update_baudrate = 9600; //初始波特率
  42. static uart_update_cfg update_cfg;
  43. u32 uart_dev_receive_data(void *buf, u32 relen, u32 addr);
  44. void uart_set_dir(u8 mode);
  45. void uart_update_write(u8 *data, u32 len);
  46. void uart_update_set_baud(u32 baudrate);
  47. void uart_close_deal(void);
  48. void uart_hw_init(uart_update_cfg update_cfg, void (*cb)(void *, u32));
  49. void uart_data_decode(u8 *buf, u16 len);
  50. enum {
  51. SEEK_SET = 0x0,
  52. SEEK_CUR = 0x1,
  53. SEEK_END = 0X2,
  54. };
  55. enum {
  56. CMD_UART_UPDATE_START = 0x1,
  57. CMD_UART_UPDATE_READ,
  58. CMD_UART_UPDATE_END,
  59. CMD_UART_UPDATE_UPDATE_LEN,
  60. CMD_UART_JEEP_ALIVE,
  61. CMD_UART_UPDATE_READY,
  62. };
  63. static void uart_update_hdl_register(void (*resume_hdl)(void *priv), int (*sleep_hdl)(void *priv))
  64. {
  65. uart_update_resume_hdl = resume_hdl;
  66. uart_update_sleep_hdl = sleep_hdl;
  67. }
  68. /*----------------------------------------------------------------------------*/
  69. /**@brief 填充升级结构体私有参数
  70. @param p: 升级结构体指针(UPDATA_PARM)
  71. @return void
  72. @note
  73. */
  74. /*----------------------------------------------------------------------------*/
  75. static void uart_ufw_update_private_param_fill(UPDATA_PARM *p)
  76. {
  77. UPDATA_UART uart_param = {.control_baud = update_baudrate, .control_io_tx = update_cfg.tx, .control_io_rx = update_cfg.rx};
  78. memcpy(p->parm_priv, &uart_param, sizeof(uart_param));
  79. }
  80. /*----------------------------------------------------------------------------*/
  81. /**@brief 固件升级校验流程完成, cpu reset跳转升级新的固件
  82. @param type: 升级类型
  83. @return void
  84. @note
  85. */
  86. /*----------------------------------------------------------------------------*/
  87. static void uart_ufw_update_before_jump_handle(int type)
  88. {
  89. printf("soft reset to update >>>");
  90. cpu_reset(); //复位让主控进入升级内置flash
  91. }
  92. static void uart_update_state_cbk(int type, u32 state, void *priv)
  93. {
  94. update_ret_code_t *ret_code = (update_ret_code_t *)priv;
  95. if (ret_code) {
  96. printf("state:%x err:%x\n", ret_code->stu, ret_code->err_code);
  97. }
  98. switch (state) {
  99. case UPDATE_CH_EXIT:
  100. if ((0 == ret_code->stu) && (0 == ret_code->err_code)) {
  101. //update_mode_api(BT_UPDATA);
  102. update_mode_api_v2(UART_UPDATA,
  103. uart_ufw_update_private_param_fill,
  104. uart_ufw_update_before_jump_handle);
  105. }
  106. uart_file_offset = 0;
  107. update_baudrate = 9600;
  108. uart_update_set_baud(update_baudrate);
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. static void uart_update_callback(void *priv, u8 type, u8 cmd)
  115. {
  116. /* printf("cmd:%x\n", cmd); */
  117. /* if (cmd == UPDATE_LOADER_OK) { */
  118. /* update_mode_api(type, update_baudrate, update_cfg.tx, update_cfg.rx); */
  119. /* } else { */
  120. /* //失败将波特率设置回初始,并重置变量 */
  121. /* rx_cnt = 0; */
  122. /* update_baudrate = 9600; */
  123. /* uart_file_offset = 0; */
  124. /* uart_update_set_baud(update_baudrate); */
  125. /* //uart_hw_init(update_cfg, uart_data_decode); */
  126. /* } */
  127. }
  128. void uart_data_decode(u8 *buf, u16 len)
  129. {
  130. u16 crc, crc0, i, ch;
  131. /* printf("decode_len:%d\n", len); */
  132. /* put_buf(buf, len); */
  133. for (i = 0; i < len; i++) {
  134. ch = buf[i];
  135. __recheck:
  136. if (rx_cnt == 0) {
  137. if (ch == SYNC_MARK0) {
  138. protocal_frame.raw_data[rx_cnt++] = ch;
  139. }
  140. } else if (rx_cnt == 1) {
  141. protocal_frame.raw_data[rx_cnt++] = ch;
  142. if (ch != SYNC_MARK1) {
  143. rx_cnt = 0;
  144. goto __recheck;
  145. }
  146. } else if (rx_cnt < 4) {
  147. protocal_frame.raw_data[rx_cnt++] = ch;
  148. } else {
  149. protocal_frame.raw_data[rx_cnt++] = ch;
  150. if (rx_cnt == (protocal_frame.data.length + SYNC_SIZE)) {
  151. rx_cnt = 0;
  152. extern u16 CRC16(void *ptr, u32 len);
  153. crc = CRC16(protocal_frame.raw_data, protocal_frame.data.length + SYNC_SIZE - 2);
  154. memcpy(&crc0, &protocal_frame.raw_data[protocal_frame.data.length + SYNC_SIZE - 2], 2);
  155. if (crc0 == crc) {
  156. switch (protocal_frame.data.data[0]) {
  157. case CMD_UART_UPDATE_START:
  158. log_info("CMD_UART_UPDATE_START\n");
  159. os_taskq_post_msg(THIS_TASK_NAME, 1, MSG_UART_UPDATE_START_RSP);
  160. break;
  161. case CMD_UART_UPDATE_READ:
  162. log_info("CMD_UART_UPDATE_READ\n");
  163. if (uart_update_resume_hdl) {
  164. uart_update_resume_hdl(NULL);
  165. }
  166. break;
  167. case CMD_UART_UPDATE_END:
  168. log_info("CMD_UART_UPDATE_END\n");
  169. break;
  170. case CMD_UART_UPDATE_UPDATE_LEN:
  171. log_info("CMD_UART_UPDATE_LEN\n");
  172. break;
  173. case CMD_UART_JEEP_ALIVE:
  174. log_info("CMD_UART_KEEP_ALIVE\n");
  175. os_taskq_post_msg(THIS_TASK_NAME, 1, MSG_UART_UPDATE_ALIVE_RSP);
  176. break;
  177. case CMD_UART_UPDATE_READY:
  178. log_info("CMD_UART_UPDATE_READY\n");
  179. os_taskq_post_msg(THIS_TASK_NAME, 1, MSG_UART_UPDATE_READY);
  180. break;
  181. default:
  182. log_info("unkown cmd...\n");
  183. break;
  184. }
  185. } else {
  186. rx_cnt = 0;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. static bool uart_send_packet(u8 *buf, u16 length)
  193. {
  194. bool ret;
  195. u16 crc;
  196. u8 *buffer;
  197. buffer = (u8 *)&protocal_frame;
  198. protocal_frame.data.mark0 = SYNC_MARK0;
  199. protocal_frame.data.mark1 = SYNC_MARK1;
  200. protocal_frame.data.length = length;
  201. memcpy((char *)&buffer[4], buf, length);
  202. crc = CRC16(buffer, length + SYNC_SIZE - 2);
  203. memcpy(buffer + 4 + length, &crc, 2);
  204. uart_set_dir(0);//设为输出
  205. put_buf((u8 *)&protocal_frame, length + SYNC_SIZE);
  206. uart_update_write((u8 *)&protocal_frame, length + SYNC_SIZE);
  207. uart_set_dir(1);
  208. return ret;
  209. }
  210. u32 uart_dev_receive_data(void *buf, u32 relen, u32 addr)
  211. {
  212. u8 i;
  213. struct file_info file_cmd;
  214. for (i = 0; i < RETRY_TIME; i++) {
  215. if (i > 0) {
  216. putchar('r');
  217. }
  218. file_cmd.cmd = CMD_UPDATE_READ;
  219. file_cmd.addr = addr;
  220. file_cmd.len = relen;
  221. uart_send_packet(&file_cmd, sizeof(file_cmd));
  222. if (uart_update_sleep_hdl) {
  223. if (uart_update_sleep_hdl(NULL) == OS_TIMEOUT) {
  224. printf("uart_sleep\n");
  225. continue;
  226. }
  227. }
  228. memcpy(&file_cmd, protocal_frame.data.data, sizeof(file_cmd));
  229. if ((file_cmd.cmd != CMD_UPDATE_READ) || (file_cmd.addr != addr) || (file_cmd.len != relen)) {
  230. continue;
  231. }
  232. memcpy(buf, &protocal_frame.data.data[sizeof(file_cmd)], protocal_frame.data.length - sizeof(file_cmd));
  233. return (protocal_frame.data.length - sizeof(file_cmd));
  234. }
  235. putchar('R');
  236. return -1;
  237. }
  238. bool uart_update_cmd(u8 cmd, u8 *buf, u32 len)
  239. {
  240. u8 *pbuf, i;
  241. //for (i = 0; i < RETRY_TIME; i++)
  242. {
  243. pbuf = protocal_frame.data.data;
  244. pbuf[0] = cmd;
  245. if (buf) {
  246. memcpy(pbuf + 1, buf, len);
  247. }
  248. uart_send_packet(pbuf, len + 1);
  249. }
  250. return TRUE;
  251. }
  252. extern const update_op_api_t uart_ch_update_op;
  253. void uart_update_recv(u8 cmd, u8 *buf, u32 len)
  254. {
  255. u32 baudrate = 9600;
  256. switch (cmd) {
  257. case CMD_UPDATE_START:
  258. memcpy(&baudrate, buf, 4);
  259. g_printf("CMD_UPDATE_START:%d\n", baudrate);
  260. if (update_baudrate != baudrate) {
  261. update_baudrate = baudrate;
  262. uart_update_set_baud(baudrate);
  263. uart_update_cmd(CMD_UPDATE_START, &update_baudrate, 4);
  264. } else {
  265. update_mode_info_t info = {
  266. .type = UART_UPDATA,
  267. .state_cbk = uart_update_state_cbk,
  268. .p_op_api = &uart_ch_update_op,
  269. .task_en = 1,
  270. };
  271. app_active_update_task_init(&info);
  272. /* app_update_loader_downloader_init( //设置完波特率后开始升级 */
  273. /* UART_UPDATA, */
  274. /* uart_update_callback, */
  275. /* NULL, */
  276. /* &uart_ch_update_op); */
  277. }
  278. break;
  279. case CMD_UPDATE_END:
  280. break;
  281. case CMD_SEND_UPDATE_LEN:
  282. break;
  283. default:
  284. break;
  285. }
  286. }
  287. bool uart_send_update_len(u32 update_len)
  288. {
  289. u8 cmd[4];
  290. WRITE_LIT_U32(&cmd[0], update_len);
  291. return uart_update_cmd(CMD_SEND_UPDATE_LEN, cmd, 4);
  292. }
  293. u16 uart_f_open(void)
  294. {
  295. return 1;
  296. }
  297. u16 uart_f_read(void *handle, void *buf, u32 relen)
  298. {
  299. u32 len;
  300. printf("%s\n", __func__);
  301. len = uart_dev_receive_data(buf, relen, uart_file_offset);
  302. if (len == -1) {
  303. log_info("uart_f_read err\n");
  304. return -1;
  305. }
  306. uart_file_offset += len;
  307. return len;
  308. }
  309. int uart_f_seek(void *fp, u8 type, u32 offset)
  310. {
  311. if (type == SEEK_SET) {
  312. uart_file_offset = offset;
  313. } else if (type == SEEK_CUR) {
  314. uart_file_offset += offset;
  315. }
  316. return 0;//FR_OK;
  317. }
  318. u16 uart_f_stop(u8 err)
  319. {
  320. uart_update_cmd(CMD_UPDATE_END, &err, 1);
  321. update_baudrate = 9600; //把波特率设置回9600
  322. return 0;
  323. }
  324. const update_op_api_t uart_ch_update_op = {
  325. .ch_init = uart_update_hdl_register,
  326. .f_open = uart_f_open,
  327. .f_read = uart_f_read,
  328. .f_seek = uart_f_seek,
  329. .f_stop = uart_f_stop,
  330. };
  331. #if NEED_TO_EXIT_LOW_POWER_AND_SNIFF
  332. static u8 uart_trans_idle = 0;
  333. u8 uart_trans_idle_query(void)
  334. {
  335. return (!uart_trans_idle);
  336. }
  337. REGISTER_LP_TARGET(uart_lp_target) = {
  338. .name = "uart_update",
  339. .is_idle = uart_trans_idle_query,
  340. };
  341. extern int bt_comm_edr_sniff_clean(void);
  342. static void uart_trans_state_check(void *priv)
  343. {
  344. static u16 uart_trans_state_check_timer = 0;
  345. if (uart_trans_state_check_timer) {
  346. sys_timeout_del(uart_trans_state_check_timer);
  347. uart_trans_state_check_timer = 0;
  348. }
  349. if (priv) {
  350. uart_trans_idle = 0;
  351. } else {
  352. uart_trans_idle = 1;
  353. bt_comm_edr_sniff_clean();
  354. uart_trans_state_check_timer = sys_timeout_add((void *)&uart_trans_state_check_timer, uart_trans_state_check, 1000);
  355. }
  356. }
  357. #endif
  358. static void update_loader_download_task(void *p)
  359. {
  360. int ret;
  361. int msg[8];
  362. static u8 update_start = 0;
  363. const uart_bus_t *uart_bus;
  364. u32 uart_rxcnt = 0;
  365. while (1) {
  366. ret = os_taskq_pend("taskq", msg, ARRAY_SIZE(msg));
  367. #if NEED_TO_EXIT_LOW_POWER_AND_SNIFF
  368. uart_trans_state_check(NULL);
  369. #endif
  370. if (ret != OS_TASKQ) {
  371. continue;
  372. }
  373. if (msg[0] != Q_MSG) {
  374. continue;
  375. }
  376. switch (msg[1]) {
  377. case MSG_UART_UPDATE_READY:
  378. uart_update_cmd(CMD_UPDATE_START, NULL, 0);
  379. break;
  380. case MSG_UART_UPDATE_START_RSP: //收到START_RSP 进行波特率设置设置
  381. log_info("MSG_UART_UPDATE_START_RSP\n");
  382. uart_update_recv(protocal_frame.data.data[0], &protocal_frame.data.data[1], protocal_frame.data.length - 1);
  383. break;
  384. case MSG_UART_UPDATE_READ_RSP:
  385. log_info("MSG_UART_UPDATE_READ_RSP\n");
  386. break;
  387. case MSG_UART_UPDATE_ALIVE_RSP:
  388. log_info("MSG_UART_UPDATE_ALIVE_RSP\n");
  389. uart_update_cmd(CMD_UART_JEEP_ALIVE, NULL, 0);
  390. break;
  391. default:
  392. log_info("unkown msg..............\n");
  393. break;
  394. }
  395. }
  396. }
  397. void uart_update_init(uart_update_cfg *cfg)
  398. {
  399. memcpy(&update_cfg, cfg, sizeof(uart_update_cfg));
  400. task_create(update_loader_download_task, NULL, THIS_TASK_NAME);
  401. uart_hw_init(update_cfg, uart_data_decode);
  402. printf(">>>%s\n", __func__);
  403. }
  404. static void clock_critical_enter(void)
  405. {
  406. }
  407. static void clock_critical_exit(void)
  408. {
  409. uart_update_set_baud(update_baudrate);
  410. }
  411. CLOCK_CRITICAL_HANDLE_REG(uart_update, clock_critical_enter, clock_critical_exit)
  412. #endif