main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include <common.h>
  2. #include "drv_io.h"
  3. #include "drv_can.h"
  4. #include "drv_watch_dog.h"
  5. #include "app.h"
  6. #include "app_rs485_1.h"
  7. #include "app_rs485_2.h"
  8. #include "app_can.h"
  9. #include "measure_temprature.h"
  10. #include "measure_vol.h"
  11. #include "app_bms_1.h"
  12. #include "app_bms_2.h"
  13. #include "app_end_ctr.h"
  14. #include "app_adas.h"
  15. #include "hardware_test.h"
  16. #include "low_power.h"
  17. #include "sw_build_info.h"
  18. #include "shark_xl.h"
  19. #ifdef CONFIG_CAN_IAP
  20. extern void qws_iap_write_magic(uint32_t magic);
  21. const uint8_t iap_board_name[] __attribute__((at(0x8002800))) = "PS100";
  22. const uint8_t soft_version[] __attribute__((at(0x8002A00))) = CONFIG_VERSION;
  23. const uint8_t iap_volume[] __attribute__((at(0x8002C00))) = "App";
  24. #else
  25. const uint8_t soft_version[] = CONFIG_VERSION;
  26. #endif
  27. uint8_t sn[PS100_SERIAL_NUM_SIZE];
  28. uint32_t g_event = NO_EVENT;
  29. void Flash_flag_clear(void)
  30. {
  31. fmc_flag_clear(FMC_FLAG_BANK0_PGERR | FMC_FLAG_BANK0_WPERR | FMC_FLAG_BANK0_END);
  32. }
  33. void Check_SN(void)
  34. {
  35. uint32_t capacity = (REG32(0x1FFFF7E0) & 0xFFFF) << 10;
  36. uint32_t address = 0x08000000 + (capacity - SN_FLASH_ADDRESS);
  37. uint8_t i = 0;
  38. uint16_t df_value = 0x3030;
  39. if (REG32(address) < 0xFFFFFFFF)
  40. {
  41. memcpy(sn,(uint8_t*)address,sizeof(sn));
  42. return;
  43. }
  44. fmc_unlock();
  45. Flash_flag_clear();
  46. fmc_page_erase(address);
  47. i = 0;
  48. while(i<PS100_SERIAL_NUM_SIZE)
  49. {
  50. Flash_flag_clear();
  51. fmc_halfword_program(address + i, df_value);
  52. i+= 2;
  53. }
  54. fmc_lock();
  55. address = 0x08000000 + (capacity - SN_FLASH_ADDRESS);
  56. memcpy(sn,(uint8_t*)address,sizeof(sn));
  57. }
  58. void Writer_SN(uint8_t *data)
  59. {
  60. uint32_t capacity = (REG32(0x1FFFF7E0) & 0xFFFF) << 10;
  61. uint32_t address = 0x08000000 + (capacity - SN_FLASH_ADDRESS);
  62. uint8_t i = 0;
  63. uint16_t df_value = 0xFFFF;
  64. fmc_unlock();
  65. Flash_flag_clear();
  66. fmc_page_erase(address);
  67. i = 0;
  68. while(i<PS100_SERIAL_NUM_SIZE)
  69. {
  70. Flash_flag_clear();
  71. df_value = data[i+1];
  72. df_value <<= 8;
  73. df_value |= data[i];
  74. fmc_halfword_program(address + i,df_value);
  75. i+= 2;
  76. }
  77. fmc_lock();
  78. address = 0x08000000 + (capacity - SN_FLASH_ADDRESS);
  79. memcpy(sn,(uint8_t*)address,sizeof(sn));
  80. }
  81. void init_NVIC(uint32_t NVoffset)
  82. {
  83. //ÉèÖÃÖжÏ
  84. nvic_vector_table_set(NVIC_VECTTAB_FLASH,NVoffset);
  85. //nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  86. nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  87. }
  88. void test_Initial(void)
  89. {
  90. rcu_periph_clock_enable(RCU_GPIOB);
  91. rcu_periph_clock_enable(RCU_GPIOD);
  92. gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_14);
  93. gpio_init(GPIOD,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_14);
  94. rcu_periph_clock_enable(RCU_AF);
  95. nvic_irq_enable(EXTI10_15_IRQn,1,0);
  96. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_14);
  97. exti_init(EXTI_14, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  98. exti_interrupt_flag_clear(EXTI_14);
  99. #if 1
  100. gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOD, GPIO_PIN_SOURCE_14);
  101. exti_init(EXTI_14, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
  102. exti_interrupt_flag_clear(EXTI_14);
  103. #endif
  104. }
  105. int main(void)
  106. {
  107. #ifdef CONFIG_CAN_IAP
  108. #else
  109. init_NVIC(NVIC_OFFSET);
  110. #endif
  111. systick_config();
  112. shark_xl_init();
  113. Misc_Initial();
  114. Sub_BMS_1_Initial();
  115. Sub_BMS_2_Initial();
  116. Can_Com_Initial();
  117. Measure_Temprature_Initial();
  118. Measure_Vol_Initial();
  119. CB_Operate_Initial();
  120. End_Ctr_Initial();
  121. ADAS_Initial();
  122. HT_Main();
  123. App_Initial();
  124. //printf("PS100 Initial!\r\n");
  125. Check_SN();
  126. Low_Power_Initial();
  127. Initial_Test_Info();
  128. #if DEBUG_MODE == 0
  129. Watch_Dog_Initial();
  130. #endif
  131. #ifdef CONFIG_CAN_IAP
  132. qws_iap_write_magic(0x11223344);
  133. #endif
  134. /*extern void test_io(void);
  135. for(;;)
  136. {
  137. test_io();
  138. }*/
  139. for(;;)
  140. {
  141. #if DEBUG_MODE == 0
  142. Feed_Watch_Dog();
  143. #endif
  144. Check_Can_Poll();
  145. Check_CB_Operate_State();
  146. Intelligent_Management_Battery();
  147. //Check_Battery_Small_Current();
  148. Check_Charge_In();
  149. Sub_BMS_1_lt_State();
  150. Sub_BMS_2_lt_State();
  151. Check_Sub_BMS_1();
  152. Check_Sub_BMS_2();
  153. Check_End_Ctr_Status();
  154. Check_Can_Self_Send_Status();
  155. Cal_Sheng_Yu_Li_Cheng();
  156. // Check_S11_May_Operate();
  157. Check_Enable_Test_Info();
  158. if(g_event & SUB_BMS_1_SEND_CMD_EVENT)
  159. {
  160. if(update_bat.ub_bat != UPDATE_BAT_1)
  161. Send_Sub_BMS_1_CMD();
  162. g_event &= ~SUB_BMS_1_SEND_CMD_EVENT;
  163. }
  164. if(g_event & SUB_BMS_2_SEND_CMD_EVENT)
  165. {
  166. if(update_bat.ub_bat != UPDATE_BAT_2)
  167. Send_Sub_BMS_2_CMD();
  168. g_event &= ~SUB_BMS_2_SEND_CMD_EVENT;
  169. }
  170. if(g_event & BMS_1_RESEND_CMD_EVENT)
  171. {
  172. if(Resend_can_bms_1_frame())
  173. {
  174. g_event &= ~BMS_1_RESEND_CMD_EVENT;
  175. }
  176. }
  177. if(g_event & BMS_2_RESEND_CMD_EVENT)
  178. {
  179. if(Resend_can_bms_2_frame())
  180. {
  181. g_event &= ~BMS_2_RESEND_CMD_EVENT;
  182. }
  183. }
  184. if(g_event & END_CTR_RESEND_CMD_EVENT)
  185. {
  186. if(Resend_can_ctr_frame())
  187. {
  188. g_event &= ~END_CTR_RESEND_CMD_EVENT;
  189. }
  190. }
  191. if(g_event & ADAS_RESEND_CMD_EVENT)
  192. {
  193. if(Resend_can_adas_frame())
  194. {
  195. g_event &= ~ADAS_RESEND_CMD_EVENT;
  196. }
  197. }
  198. //if(g_event & EVENT_CAN_RECEIVE_FINISH)
  199. {
  200. if(Handle_Can_Data() == 1)
  201. {
  202. // output fail
  203. ;
  204. }
  205. //g_event &= ~EVENT_CAN_RECEIVE_FINISH;
  206. }
  207. if(g_event & MEASURE_TEMPERATURE_EVENT)
  208. {
  209. getTemperature();
  210. g_event &= ~MEASURE_TEMPERATURE_EVENT;
  211. }
  212. if(g_event & SUB_BMS_1_RS485_DISC_EVENT)
  213. {
  214. RS485_Communication_Time_Out_1();
  215. g_event &= ~SUB_BMS_1_RS485_DISC_EVENT;
  216. }
  217. if(g_event & SUB_BMS_2_RS485_DISC_EVENT)
  218. {
  219. RS485_Communication_Time_Out_2();
  220. g_event &= ~SUB_BMS_2_RS485_DISC_EVENT;
  221. }
  222. if(g_event & RS485_RECEIVE_END_EVENT)
  223. {
  224. if(Handle_RS485_1_Data() == 1)
  225. {
  226. // output fail
  227. ;
  228. }
  229. g_event &= ~RS485_RECEIVE_END_EVENT;
  230. }
  231. if(g_event & RS485_2_RECEIVE_END_EVENT)
  232. {
  233. if(Handle_RS485_2_Data() == 1)
  234. {
  235. // output fail
  236. ;
  237. }
  238. g_event &= ~RS485_2_RECEIVE_END_EVENT;
  239. }
  240. if(g_event & BMS_1_SELF_TIMEOUT_EVENT)
  241. {
  242. Timeout_Resend_can_bms_1_frame();
  243. g_event &= ~BMS_1_SELF_TIMEOUT_EVENT;
  244. }
  245. if(g_event & BMS_2_SELF_TIMEOUT_EVENT)
  246. {
  247. Timeout_Resend_can_bms_2_frame();
  248. g_event &= ~BMS_2_SELF_TIMEOUT_EVENT;
  249. }
  250. if(g_event & END_CTR_SELF_TIMEOUT_EVENT)
  251. {
  252. Timeout_Resend_can_ctr_frame();
  253. g_event &= ~END_CTR_SELF_TIMEOUT_EVENT;
  254. }
  255. if(g_event & ADAS_SELF_TIMEOUT_EVENT)
  256. {
  257. Timeout_Resend_can_adas_frame();
  258. g_event &= ~ADAS_SELF_TIMEOUT_EVENT;
  259. }
  260. if(g_event & ADAS_PWM_1_TIMEOUT_EVENT)
  261. {
  262. ADAS_PWM_1_Enable();
  263. g_event &= ~ADAS_PWM_1_TIMEOUT_EVENT;
  264. }
  265. if(g_event & ADAS_PWM_2_TIMEOUT_EVENT)
  266. {
  267. ADAS_PWM_2_Enable();
  268. g_event &= ~ADAS_PWM_2_TIMEOUT_EVENT;
  269. }
  270. if(g_event & ADAS_MEAS_1_FINISH_EVENT)
  271. {
  272. ADAS_Measure_Finish_Ju_Li_1();
  273. ADAS_Self_Send_Up();
  274. g_event &= ~ADAS_MEAS_1_FINISH_EVENT;
  275. }
  276. if(g_event & ADAS_MEAS_2_FINISH_EVENT)
  277. {
  278. ADAS_Measure_Finish_Ju_Li_2();
  279. g_event &= ~ADAS_MEAS_2_FINISH_EVENT;
  280. }
  281. if(g_event & ENTER_SLEEP_EVENT)
  282. {
  283. Enter_Sleep();
  284. g_event &= ~ENTER_SLEEP_EVENT;
  285. }
  286. if(g_event & RE_INITIAL_CAN_EVENT)
  287. {
  288. //Can_Com_Initial();
  289. g_event &= ~RE_INITIAL_CAN_EVENT;
  290. }
  291. if(g_event & DEVICE_REBOOT_EVENT)
  292. {
  293. g_event &= ~DEVICE_REBOOT_EVENT;
  294. __set_FAULTMASK(1);
  295. NVIC_SystemReset();
  296. }
  297. if(g_event & SAVE_PARAM_EVENT)
  298. {
  299. g_event &= ~SAVE_PARAM_EVENT;
  300. Save_Param();
  301. Save_Test_Info();
  302. }
  303. }
  304. //return (1);
  305. }
  306. #ifdef USE_FULL_ASSERT
  307. /**
  308. * @brief Reports the name of the source file and the source line number
  309. * where the assert_param error has occurred.
  310. * @param file: pointer to the source file name
  311. * @param line: assert_param error line source number
  312. * @retval None
  313. */
  314. void assert_failed(u8_t* file, uint32_t line)
  315. {
  316. /* User can add his own implementation to report the file name and line number,
  317. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  318. /* Infinite loop */
  319. while (1)
  320. {
  321. }
  322. }
  323. #endif
  324. /*********************************************************************************************************
  325. END FILE
  326. *********************************************************************************************************/