main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. #include "gd32e23x.h"
  2. #include "systick.h"
  3. #include <stdio.h>
  4. #include "main.h"
  5. #include "Fwdgt.h"
  6. #include "mb.h"
  7. #include "Led.h"
  8. #include "Key.h"
  9. #include "cfg.h"
  10. #include "main.h"
  11. #include "gd32e230x_conf.h"
  12. #include "string.h"
  13. #include "Timer.h"
  14. //#define USR_LED_RCU RCU_GPIOB
  15. //#define USR_LED_IO GPIOB
  16. //#define USR_LED GPIO_PIN_11
  17. uint8_t programe_erase_flag = 0;
  18. uint8_t programe_write_complete_flag = 0;
  19. uint8_t meta_erase_flag = 0;
  20. uint8_t meta_write_complete_flag = 0;
  21. uint8_t get_head_over_flag = 0;
  22. get_md_msg _msg = {0};
  23. static uint32_t start_addr = 0x00000000;
  24. uint32_t my_addr = 0;
  25. static uint32_t erase_size = 0;
  26. uint16_t CurrentAddr = 0;
  27. uint32_t operation_cmd_cnt = 0;
  28. uint8_t flash_flag = 0;
  29. MD_DATA Md_Data[Mb_Wcmd_Width] = {0};
  30. META_DATA_S meta_data = {0};
  31. uint8_t read_meta_data[100] = {0};
  32. g_MD_Data g_md_data={
  33. .count = Mb_Wcmd_Width,
  34. .data = {0},
  35. };
  36. g_MD_Data* get_md_data()
  37. {
  38. return &g_md_data;
  39. }
  40. void flash_erase_page(uint32_t addr)
  41. {
  42. fmc_unlock();
  43. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  44. fmc_page_erase(addr);
  45. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  46. fmc_lock();
  47. }
  48. void flash_write_meta(META_DATA_S *meta_data)
  49. {
  50. fmc_unlock();
  51. uint32_t address = META_DATA;
  52. uint32_t *data = (uint32_t*)(meta_data);
  53. uint32_t data0 = 0;
  54. for(int i = 0; i < sizeof(META_DATA_S);i++)
  55. {
  56. data0 = *data;
  57. fmc_word_program(address,data0);
  58. data ++;
  59. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  60. address +=4;
  61. }
  62. fmc_lock();
  63. }
  64. user_code application = NULL;
  65. __IO uint32_t JumpAddress = NULL;
  66. void UserJumpToApplication(uint32_t addr)
  67. {
  68. /* Test if user code is programmed starting from address "APPLICATION_ADDRESS" */
  69. uint32_t JumpAddress = 0;
  70. if (((*(uint32_t*)addr)&0x2FFE0000)==0x20000000)
  71. {
  72. /* Jump to user application */
  73. __disable_irq();
  74. // del_md_timer();
  75. // del_modbus_uart();
  76. // delete_Timer14();
  77. // DeInitKey();
  78. SCB->VTOR = addr;
  79. //nvic_vector_table_set(NVIC_VECTTAB_FLASH,0x5400);
  80. JumpAddress = *(__IO uint32_t*) (addr + 4);
  81. application = (user_code)JumpAddress;
  82. /* Initialize user application's Stack Pointer */
  83. __set_MSP(*(__IO uint32_t*)(addr));
  84. application();
  85. }
  86. }
  87. void flash_get_meta(META_DATA_S *meta)
  88. {
  89. meta->operation_flag = *(uint32_t*)(UPGRADE_FLAG_ADDR);
  90. meta->partition = *(uint32_t*)(STARTUP_PARTATION);
  91. meta->errno = *(uint32_t*)(STARTUP_ERROR);
  92. meta->error_count = *(uint32_t*)(STARTUP_NUMBEAR);
  93. meta->rom_addr = *(uint32_t*)(STARTUP_ROM_ADDR);
  94. meta->recent_running_time = *(uint32_t*)(STARTUP_LAST_RUNNING_TIME);
  95. meta->running_app_version = *(uint32_t*)(STARTUP_APP_VERSION);
  96. meta->app1_version = *(uint32_t*)(APP1_VERSION);
  97. meta->app1_rom_addr = *(uint32_t*)(APP1_ROM_ADDR);
  98. meta->app1_partition = *(uint32_t*)(APP1_PARTATION);
  99. meta->app2_version = *(uint32_t*)(APP2_VERSION);
  100. meta->app2_rom_addr = *(uint32_t*)(APP2_ROM_ADDR);
  101. meta->app2_partition = *(uint32_t*)(APP2_PARTATION);
  102. }
  103. void flash_write_programe(uint32_t *addr,uint32_t* source,uint32_t lenth)
  104. {
  105. fmc_unlock();
  106. uint32_t *data = source;
  107. uint32_t *address = addr;
  108. for(int i = 0; i < ((lenth % 4) == 0 ? (lenth/4):((lenth/4)+1));i++,*address += 4)
  109. {
  110. fmc_word_program(*address,*data++);
  111. fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
  112. }
  113. *addr = *address;
  114. fmc_lock();
  115. }
  116. uint16_t cnt = 0;
  117. uint8_t md_status = 0;
  118. void operation_Flash_data(Flash_Data *data)
  119. {
  120. if(!data)
  121. return;
  122. if(data->index == 0)
  123. {
  124. if(md_status == UPGRADE_STATUS_IDE)
  125. {
  126. if(strncmp((const char *)(data->recive),MODBUS_HEAD,strlen(MODBUS_HEAD)) == 0){
  127. //memcpy(head_buffer,data->recive,data->lenth);
  128. int lenth = strlen(MODBUS_HEAD);
  129. memcpy(&_msg.magic,data->recive,lenth);
  130. _msg.size = (data->recive[lenth]) | (data->recive[lenth+1] << 8);
  131. _msg.patition = (data->recive[lenth+2]) | (data->recive[lenth+3] << 8) | (data->recive[lenth+4] << 16) | (data->recive[lenth+5] << 24);
  132. _msg.version = (data->recive[lenth+6]) | (data->recive[lenth+6] << 8) | (data->recive[lenth+8] << 16) | (data->recive[lenth+9] << 24);
  133. get_head_over_flag = 1;
  134. }
  135. }
  136. }else
  137. {
  138. if(md_status == UPGRADE_STATUS_WRITE_PROGRAME)
  139. {
  140. flash_write_programe(&my_addr,(uint32_t*)(data->recive),data->lenth);
  141. if(data->index < 0)
  142. {
  143. programe_write_complete_flag = 1;
  144. }
  145. }
  146. }
  147. }
  148. void ModBus_CMD(void)
  149. {
  150. if(g_md_data.data[operation_cmd_cnt].flag == 1)
  151. {
  152. uint32_t base_addr = g_md_data.data[operation_cmd_cnt].base_addr;
  153. //check data is or not header metadata;
  154. switch(base_addr)
  155. {
  156. case MD_WRITE_Addr:
  157. {
  158. operation_Flash_data(&g_md_data.data[operation_cmd_cnt].flash_Data);
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. g_md_data.data[operation_cmd_cnt].flag = 0;
  165. operation_cmd_cnt++;
  166. if(operation_cmd_cnt >= Mb_Wcmd_Width)
  167. {
  168. operation_cmd_cnt = 0;
  169. }
  170. }
  171. }
  172. Upgrade upgrade = {0};
  173. Upgrade *get_upgrade()
  174. {
  175. return &upgrade;
  176. }
  177. int main(void)
  178. {
  179. SystemInit();
  180. //systick_config();
  181. InitLED();
  182. InitKey();
  183. InitTimer();
  184. CurrentAddr = ReadKeyValue();
  185. eMBInit(MB_RTU, CurrentAddr, 0, 115200, MB_PAR_NONE);
  186. eMBEnable();
  187. flash_get_meta(&meta_data);
  188. if(meta_data.operation_flag == 0x0)
  189. {
  190. if(meta_data.rom_addr == PROGRAME_APP1 || meta_data.rom_addr == PROGRAME_APP2)
  191. {
  192. if(meta_data.errno != NOERROR && meta_data.error_count >= MAX_ERROR_COUNT)
  193. {
  194. //check current version is last versions?
  195. if(meta_data.app1_version == meta_data.app2_version)
  196. {
  197. }else
  198. {
  199. uint32_t version_max = meta_data.app1_version > meta_data.app2_version ? meta_data.app1_version :meta_data.app2_version;
  200. uint32_t version_min = meta_data.app1_version < meta_data.app2_version ? meta_data.app1_version :meta_data.app2_version;
  201. if(version_max == ERROR_VERSION)
  202. {
  203. }else if(meta_data.running_app_version == version_max)
  204. {
  205. //swtich lower version to running
  206. flash_erase_page(META_DATA);
  207. if(version_max == meta_data.app2_version){
  208. meta_data.partition = meta_data.app1_partition;
  209. meta_data.rom_addr = meta_data.app1_rom_addr;
  210. }else
  211. {
  212. meta_data.partition = meta_data.app2_partition;
  213. meta_data.rom_addr = meta_data.app2_rom_addr;
  214. }
  215. meta_data.running_app_version = version_min;
  216. meta_data.errno = 0;
  217. meta_data.error_count = 0;
  218. flash_write_meta(&meta_data);
  219. UserJumpToApplication(meta_data.rom_addr);
  220. }else
  221. {
  222. ;;
  223. }
  224. }
  225. }else
  226. {
  227. UserJumpToApplication(meta_data.rom_addr); //
  228. }
  229. }
  230. }else
  231. {
  232. //wait to upgrade
  233. }
  234. md_status = UPGRADE_STATUS_IDE;
  235. while(1)
  236. {
  237. eMBPoll();
  238. if(flash_flag)
  239. {
  240. LEDFlicker2();
  241. flash_flag = 0;
  242. }
  243. ModBus_CMD();
  244. switch(md_status)
  245. {
  246. case UPGRADE_STATUS_IDE:
  247. {
  248. //get and jie xi meta_data
  249. if(get_head_over_flag)
  250. {
  251. //½âÎöMetaDataºÍ²Á³ýflash
  252. if(_msg.patition == ONE){
  253. start_addr =PROGRAME_APP1;
  254. erase_size = PROGRAME_APP1_SIZE;
  255. md_status = UPGRADE_STATUS_ERASE_PROGRAME;
  256. }else if(_msg.patition == SECOND){
  257. start_addr = PROGRAME_APP2;
  258. erase_size = PROGRAME_APP2_SIZE;
  259. md_status = UPGRADE_STATUS_ERASE_PROGRAME;
  260. }else{
  261. md_status = UPGRADE_STATUS_IDE;
  262. get_head_over_flag = 0;
  263. }
  264. }
  265. }
  266. break;
  267. case UPGRADE_STATUS_ERASE_PROGRAME:
  268. {
  269. uint32_t data = *(uint32_t*)(PROGRAME_APP1);
  270. if(programe_erase_flag == 0)
  271. {
  272. my_addr = start_addr;
  273. for(uint32_t i = 0;i < (erase_size/PAGE_SIZE);i++)
  274. {
  275. flash_erase_page(my_addr);
  276. my_addr += PAGE_SIZE;
  277. }
  278. my_addr = start_addr;
  279. programe_erase_flag = 1;
  280. md_status = UPGRADE_STATUS_WRITE_PROGRAME;
  281. }
  282. }
  283. break;
  284. case UPGRADE_STATUS_WRITE_PROGRAME:
  285. {
  286. if(programe_write_complete_flag)
  287. {
  288. md_status = UPGRADE_STATUS_ERASE_META;
  289. }
  290. }
  291. break;
  292. case UPGRADE_STATUS_ERASE_META:
  293. {
  294. if(meta_erase_flag == 0)
  295. {
  296. meta_data.partition = _msg.patition;
  297. meta_data.running_app_version = _msg.version;
  298. meta_data.errno = 0;
  299. meta_data.error_count = 0;
  300. if(meta_data.partition == ONE){
  301. meta_data.rom_addr =PROGRAME_APP1;
  302. meta_data.app1_partition = _msg.patition;
  303. meta_data.app1_rom_addr = PROGRAME_APP1;
  304. meta_data.app1_version = _msg.version;
  305. }
  306. else if(meta_data.partition == SECOND)
  307. {
  308. meta_data.rom_addr =PROGRAME_APP2;
  309. meta_data.app2_partition = _msg.patition;
  310. meta_data.app2_rom_addr = PROGRAME_APP2;
  311. meta_data.app2_version = _msg.version;
  312. }else
  313. {
  314. // errno
  315. md_status = UPGRADE_STATUS_IDE;
  316. get_head_over_flag = 0;
  317. break;
  318. }
  319. meta_data.operation_flag = 0;
  320. meta_data.recent_running_time = 0;
  321. md_status = UPGRADE_STATUS_WTITE_META;
  322. flash_erase_page(META_DATA);
  323. meta_erase_flag = 1;
  324. }
  325. }
  326. break;
  327. case UPGRADE_STATUS_WTITE_META:
  328. {
  329. if(meta_write_complete_flag == 0){
  330. flash_write_meta(&meta_data);
  331. md_status = UPGRADE_STATUS_FINISH;
  332. meta_write_complete_flag = 1;
  333. }
  334. }
  335. break;
  336. case UPGRADE_STATUS_FINISH:
  337. {
  338. UserJumpToApplication(start_addr);
  339. md_status = UPGRADE_STATUS_IDE;
  340. meta_write_complete_flag = 0; //
  341. meta_erase_flag = 0;
  342. get_head_over_flag = 0;
  343. programe_erase_flag = 0;
  344. programe_write_complete_flag = 0;
  345. }
  346. break;
  347. default:
  348. break;
  349. }
  350. }
  351. return 0;
  352. }