uart_update_master.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #include "app_config.h"
  2. #if(USER_UART_UPDATE_ENABLE) && (UART_UPDATE_ROLE == UART_UPDATE_MASTER)
  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. #include "timer.h"
  13. #include "system/fs/fs.h"
  14. static volatile u32 uart_to_cnt = 0;
  15. static volatile u32 uart_file_offset = 0;
  16. static volatile u16 rx_cnt; //收数据计数
  17. typedef struct _uart_updte_ctl_t {
  18. OS_SEM sem;
  19. OS_SEM rx_sem;
  20. volatile u16 timemax;
  21. volatile u16 timeout;
  22. volatile u8 flag;
  23. volatile u8 err_code;
  24. u8 update_sta;
  25. u8 update_percent;
  26. u32 update_total_size;
  27. u32 update_send_size;
  28. u8 rx_cmd[0x30];
  29. u16 cmd_len;
  30. u16 uart_timer_hdl;
  31. } uart_update_ctl_t;
  32. static uart_update_ctl_t uart_update_ctl;
  33. #define __this (&uart_update_ctl)
  34. #define LOG_TAG "[UART_UPDATE]"
  35. #define LOG_ERROR_ENABLE
  36. #define LOG_DEBUG_ENABLE
  37. #define LOG_INFO_ENABLE
  38. #define LOG_CLI_ENABLE
  39. #include "debug.h"
  40. #define RETRY_TIME 4//重试n次
  41. #define PACKET_TIMEOUT 200//ms
  42. #define FILE_READ_UNIT 512 //
  43. #define UART_DEFAULT_BAUD 9600
  44. #define UART_UPDATE_BAUD (50*10000L)
  45. #define TIME_TICK_UNIT (10) //unit:ms
  46. //命令
  47. #define CMD_UPDATE_START 0x01
  48. #define CMD_UPDATE_READ 0x02
  49. #define CMD_UPDATE_END 0x03
  50. #define CMD_SEND_UPDATE_LEN 0x04
  51. #define CMD_KEEP_ALIVE 0x05
  52. #define READ_LIT_U16(a) (*((u8*)(a)) + (*((u8*)(a)+1)<<8))
  53. #define READ_LIT_U32(a) (*((u8*)(a)) + (*((u8*)(a)+1)<<8) + (*((u8*)(a)+2)<<16) + (*((u8*)(a)+3)<<24))
  54. #define WRITE_LIT_U16(a,src) {*((u8*)(a)+1) = (u8)(src>>8); *((u8*)(a)+0) = (u8)(src&0xff); }
  55. #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);}
  56. #define THIS_TASK_NAME "uart_update"
  57. static protocal_frame_t protocal_frame __attribute__((aligned(4)));
  58. u32 update_baudrate = 9600; //初始波特率
  59. static uart_update_cfg update_cfg;
  60. void *fd = NULL;
  61. u32 uart_dev_receive_data(void *buf, u32 relen, u32 addr);
  62. void uart_set_dir(u8 mode);
  63. void uart_update_write(u8 *data, u32 len);
  64. void uart_update_set_baud(u32 baudrate);
  65. void uart_close_deal(void);
  66. void uart_hw_init(uart_update_cfg update_cfg, void (*cb)(void *, u32));
  67. void uart_data_decode(u8 *buf, u16 len);
  68. /* enum { */
  69. /* SEEK_SET = 0x0, */
  70. /* SEEK_CUR = 0x1, */
  71. /* SEEK_END = 0X2, */
  72. /* }; */
  73. enum {
  74. CMD_UART_UPDATE_START = 0x1,
  75. CMD_UART_UPDATE_READ,
  76. CMD_UART_UPDATE_END,
  77. CMD_UART_UPDATE_UPDATE_LEN,
  78. CMD_UART_JEEP_ALIVE,
  79. CMD_UART_UPDATE_READY,
  80. };
  81. enum {
  82. RX_DATA_READY = 0,
  83. RX_DATA_TIMEOUT,
  84. RX_DATA_SUCC,
  85. };
  86. void uart_data_decode(u8 *buf, u16 len)
  87. {
  88. u16 crc, crc0, i, ch;
  89. /* printf("decode_len:%d\n", len); */
  90. /* put_buf(buf, len); */
  91. for (i = 0; i < len; i++) {
  92. ch = buf[i];
  93. __recheck:
  94. if (rx_cnt == 0) {
  95. if (ch == SYNC_MARK0) {
  96. protocal_frame.raw_data[rx_cnt++] = ch;
  97. }
  98. } else if (rx_cnt == 1) {
  99. protocal_frame.raw_data[rx_cnt++] = ch;
  100. if (ch != SYNC_MARK1) {
  101. rx_cnt = 0;
  102. goto __recheck;
  103. }
  104. } else if (rx_cnt < 4) {
  105. protocal_frame.raw_data[rx_cnt++] = ch;
  106. } else {
  107. protocal_frame.raw_data[rx_cnt++] = ch;
  108. if (rx_cnt == (protocal_frame.data.length + SYNC_SIZE)) {
  109. rx_cnt = 0;
  110. extern u16 CRC16(void *ptr, u32 len);
  111. crc = CRC16(protocal_frame.raw_data, protocal_frame.data.length + SYNC_SIZE - 2);
  112. memcpy(&crc0, &protocal_frame.raw_data[protocal_frame.data.length + SYNC_SIZE - 2], 2);
  113. if (crc0 == crc) {
  114. __this->timemax = 0;
  115. if (protocal_frame.data.length <= sizeof(__this->rx_cmd)) {
  116. memcpy(__this->rx_cmd, &(protocal_frame.data.data), protocal_frame.data.length);
  117. __this->flag = RX_DATA_SUCC;
  118. os_sem_post(&__this->rx_sem);
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }
  125. static bool uart_send_packet(u8 *buf, u16 length)
  126. {
  127. bool ret;
  128. u16 crc;
  129. u8 *buffer;
  130. buffer = (u8 *)&protocal_frame;
  131. protocal_frame.data.mark0 = SYNC_MARK0;
  132. protocal_frame.data.mark1 = SYNC_MARK1;
  133. protocal_frame.data.length = length;
  134. memcpy((char *)&buffer[4], buf, length);
  135. crc = CRC16(buffer, length + SYNC_SIZE - 2);
  136. memcpy(buffer + 4 + length, &crc, 2);
  137. uart_set_dir(0);//设为输出
  138. uart_update_write((u8 *)&protocal_frame, length + SYNC_SIZE);
  139. uart_set_dir(1);
  140. return ret;
  141. }
  142. //read file by file operation handle
  143. u32 ufw_data_read_api(u8 *buff, u32 addr, u32 size)
  144. {
  145. //To do...
  146. if (fd) {
  147. fseek(fd, addr, SEEK_SET);
  148. return fread(fd, buff, size);
  149. }
  150. return 0;
  151. }
  152. //open file and get file operation handle
  153. bool ufw_file_op_init(char *update_path)
  154. {
  155. if (fd) {
  156. fclose(fd);
  157. }
  158. //To do
  159. fd = fopen(update_path, "r");
  160. if (!fd) {
  161. return false;
  162. }
  163. return true;
  164. }
  165. //close the file and release resource
  166. void ufw_file_op_close(void)
  167. {
  168. if (fd) {
  169. fclose(fd);
  170. fd = NULL;
  171. }
  172. //To do
  173. }
  174. static u32 update_data_read_from_file(void *p, u32 addr, u32 len)
  175. {
  176. u8 *buffer;
  177. struct file_info *p_file_info;
  178. u32 read_len = 0;
  179. if (len > FILE_READ_UNIT) {
  180. return (u32) - 1;
  181. }
  182. buffer = malloc(len + sizeof(struct file_info));
  183. if (buffer) {
  184. p_file_info = (struct file_info *)buffer;
  185. p_file_info->cmd = CMD_UART_UPDATE_READ;
  186. p_file_info->addr = addr;
  187. p_file_info->len = len;
  188. read_len = ufw_data_read_api(buffer + sizeof(struct file_info), addr, len);
  189. } else {
  190. return (u32) - 2;
  191. }
  192. uart_send_packet(buffer, len + sizeof(struct file_info));
  193. if (buffer) {
  194. free(buffer);
  195. }
  196. return read_len;
  197. }
  198. enum {
  199. UPDATE_STA_NONE = 0,
  200. UPDATE_STA_READY,
  201. UPDATE_STA_START,
  202. UPDATE_STA_TIMEOUT,
  203. UPDATE_STA_LOADER_DOWNLOAD_FINISH,
  204. UPDATE_STA_SUCC,
  205. UPDATE_STA_FAIL,
  206. };
  207. enum {
  208. UPDATE_ERR_NONE = 0,
  209. UPDATE_ERR_KEY,
  210. UPDATE_ERR_VERIFY,
  211. UPDATE_ERR_LOADER_DOWNLOAD_SUCC = 0x80,
  212. };
  213. static void uart_update_err_code_handle(u8 code)
  214. {
  215. if (code) {
  216. if (UPDATE_ERR_LOADER_DOWNLOAD_SUCC == code) {
  217. __this->update_percent = 100;
  218. __this->update_send_size = 0;
  219. __this->update_total_size = 0;
  220. __this->update_sta = UPDATE_STA_LOADER_DOWNLOAD_FINISH;
  221. log_info("loader dn succ\n");
  222. } else {
  223. __this->update_sta = UPDATE_STA_FAIL;
  224. log_error("update_err:%x\n", code);
  225. }
  226. } else {
  227. __this->update_percent = 100;
  228. __this->update_sta = UPDATE_STA_SUCC;
  229. log_info("update all succ\n");
  230. }
  231. }
  232. static int uart_update_wait_rev_data(timeout)
  233. {
  234. u32 err;
  235. __this->flag = RX_DATA_READY;
  236. __this->timeout = 0;
  237. __this->timemax = (timeout + 1) / TIME_TICK_UNIT;
  238. err = os_sem_pend(&__this->rx_sem, 2000);
  239. if (OS_NO_ERR != err) {
  240. log_info("wait tm out\n");
  241. }
  242. __this->timemax = __this->timeout = 0;
  243. if (__this->flag == RX_DATA_SUCC) {
  244. return TRUE;
  245. }
  246. return FALSE;
  247. }
  248. int uart_update_api_write_then_read(u8 *buf, u8 length, u8 timeout)
  249. {
  250. int ret = FALSE;
  251. uart_send_packet(buf, length);
  252. if (timeout) {
  253. ret = uart_update_wait_rev_data(timeout);
  254. }
  255. return ret;
  256. }
  257. bool uart_update_send_update_ready(char *file_update_path)
  258. {
  259. u8 ut_cmd[1];
  260. ut_cmd[0] = CMD_UART_UPDATE_READY;
  261. if (ufw_file_op_init(file_update_path)) {
  262. __this->update_sta = UPDATE_STA_READY;
  263. uart_send_packet(ut_cmd, sizeof(ut_cmd));
  264. log_info("uart_update_send_update_ready\n");
  265. return TRUE;
  266. }
  267. return FALSE;
  268. }
  269. bool get_uart_update_sta(void)
  270. {
  271. return (__this->update_sta == UPDATE_STA_READY || __this->update_sta == UPDATE_STA_START) ? TRUE : FALSE;
  272. }
  273. static void update_process_run(void)
  274. {
  275. update_baudrate = UART_DEFAULT_BAUD;
  276. uart_update_set_baud(update_baudrate);
  277. __this->update_total_size = 0;
  278. __this->update_percent = 0;
  279. __this->update_send_size = 0;
  280. u8 *pbuf = &__this->rx_cmd;
  281. while (1) {
  282. if (OS_NO_ERR != os_sem_pend(&__this->rx_sem, 800)) {
  283. log_info("uart_timeout\n");
  284. __this->update_sta = UPDATE_STA_TIMEOUT;
  285. update_baudrate = 9600;
  286. uart_update_set_baud(update_baudrate);
  287. continue;
  288. }
  289. //os_time_dly(1);
  290. switch (pbuf[0]) {
  291. case CMD_UPDATE_START:
  292. log_info("CMD_UPDATE_START\n");
  293. __this->update_sta = UPDATE_STA_START;
  294. update_baudrate = UART_UPDATE_BAUD;
  295. WRITE_LIT_U32(pbuf + 1, update_baudrate);
  296. uart_send_packet(pbuf, 1 + sizeof(u32));
  297. log_info("use baud:%x\n", update_baudrate);
  298. uart_update_set_baud(update_baudrate);
  299. break;
  300. case CMD_UPDATE_READ: {
  301. u32 addr = READ_LIT_U32(&pbuf[1]);
  302. u32 len = READ_LIT_U32(&pbuf[1 + sizeof(u32)]);
  303. if (__this->update_total_size) {
  304. __this->update_send_size += len;
  305. __this->update_percent = (__this->update_send_size * 100) / __this->update_total_size;
  306. if (__this->update_percent >= 99) {
  307. __this->update_percent = 99;
  308. }
  309. log_info("send data process:%x\n", __this->update_percent);
  310. }
  311. log_info("CMD_UPDATE_READ\n");
  312. update_data_read_from_file(NULL, addr, len);
  313. }
  314. break;
  315. case CMD_UPDATE_END:
  316. log_info("CMD_UPDATE_END\n");
  317. uart_update_err_code_handle(pbuf[1]);
  318. uart_send_packet(pbuf, 1);
  319. break;
  320. case CMD_SEND_UPDATE_LEN:
  321. __this->update_total_size = READ_LIT_U32(&pbuf[1]);
  322. __this->update_percent = 0;
  323. __this->update_send_size = 0;
  324. log_info("update_total_size:%x\n", __this->update_total_size);
  325. uart_send_packet(pbuf, 1);
  326. break;
  327. case CMD_KEEP_ALIVE:
  328. uart_send_packet(pbuf, 1);
  329. break;
  330. }
  331. }
  332. }
  333. static void uart_timeout_handler(void *priv)
  334. {
  335. if (__this->timemax) {
  336. __this->timeout++;
  337. if (__this->timeout > __this->timemax) {
  338. __this->timemax = 0;
  339. __this->flag = RX_DATA_TIMEOUT;
  340. os_sem_post(&__this->rx_sem);
  341. }
  342. }
  343. }
  344. static void update_loader_download_task(void *p)
  345. {
  346. log_info("create %s task\n", THIS_TASK_NAME);
  347. os_sem_create(&__this->sem, 0);
  348. os_sem_create(&__this->rx_sem, 0);
  349. while (1) {
  350. update_process_run();
  351. }
  352. }
  353. void uart_update_init(uart_update_cfg *cfg)
  354. {
  355. memcpy(&update_cfg, cfg, sizeof(uart_update_cfg));
  356. task_create(update_loader_download_task, NULL, THIS_TASK_NAME);
  357. uart_hw_init(update_cfg, uart_data_decode);
  358. }
  359. void uart_update_exit(void)
  360. {
  361. log_info("uart update exit\n");
  362. if (__this->uart_timer_hdl) {
  363. sys_timer_del(__this->uart_timer_hdl);
  364. }
  365. os_sem_del(&__this->sem, 0);
  366. os_sem_del(&__this->rx_sem, 0);
  367. ufw_file_op_close();
  368. uart_close_deal();
  369. task_kill(THIS_TASK_NAME);
  370. }
  371. static void clock_critical_enter(void)
  372. {
  373. }
  374. static void clock_critical_exit(void)
  375. {
  376. uart_update_set_baud(update_baudrate);
  377. }
  378. CLOCK_CRITICAL_HANDLE_REG(uart_update, clock_critical_enter, clock_critical_exit)
  379. #endif