main.c 7.4 KB

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