main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. /*extern void test_io(void);
  134. for(;;)
  135. {
  136. test_io();
  137. }*/
  138. for(;;)
  139. {
  140. #if DEBUG_MODE == 0
  141. Feed_Watch_Dog();
  142. #endif
  143. Check_Can_Poll();
  144. Check_CB_Operate_State();
  145. Intelligent_Management_Battery();
  146. Check_End_Ctr_Status();
  147. Check_Can_Self_Send_Status();
  148. Cal_Sheng_Yu_Li_Cheng();
  149. // Check_S11_May_Operate();
  150. Check_Enable_Test_Info();
  151. shark_uart_poll();
  152. shark_charge_poll();
  153. if(g_event & BMS_1_RESEND_CMD_EVENT)
  154. {
  155. if(Resend_can_bms_1_frame())
  156. {
  157. g_event &= ~BMS_1_RESEND_CMD_EVENT;
  158. }
  159. }
  160. if(g_event & BMS_2_RESEND_CMD_EVENT)
  161. {
  162. if(Resend_can_bms_2_frame())
  163. {
  164. g_event &= ~BMS_2_RESEND_CMD_EVENT;
  165. }
  166. }
  167. if(g_event & END_CTR_RESEND_CMD_EVENT)
  168. {
  169. if(Resend_can_ctr_frame())
  170. {
  171. g_event &= ~END_CTR_RESEND_CMD_EVENT;
  172. }
  173. }
  174. if(g_event & ADAS_RESEND_CMD_EVENT)
  175. {
  176. if(Resend_can_adas_frame())
  177. {
  178. g_event &= ~ADAS_RESEND_CMD_EVENT;
  179. }
  180. }
  181. //if(g_event & EVENT_CAN_RECEIVE_FINISH)
  182. {
  183. if(Handle_Can_Data() == 1)
  184. {
  185. // output fail
  186. ;
  187. }
  188. //g_event &= ~EVENT_CAN_RECEIVE_FINISH;
  189. }
  190. if(g_event & MEASURE_TEMPERATURE_EVENT)
  191. {
  192. getTemperature();
  193. g_event &= ~MEASURE_TEMPERATURE_EVENT;
  194. }
  195. if(g_event & BMS_1_SELF_TIMEOUT_EVENT)
  196. {
  197. Timeout_Resend_can_bms_1_frame();
  198. g_event &= ~BMS_1_SELF_TIMEOUT_EVENT;
  199. }
  200. if(g_event & BMS_2_SELF_TIMEOUT_EVENT)
  201. {
  202. Timeout_Resend_can_bms_2_frame();
  203. g_event &= ~BMS_2_SELF_TIMEOUT_EVENT;
  204. }
  205. if(g_event & END_CTR_SELF_TIMEOUT_EVENT)
  206. {
  207. Timeout_Resend_can_ctr_frame();
  208. g_event &= ~END_CTR_SELF_TIMEOUT_EVENT;
  209. }
  210. if(g_event & ADAS_SELF_TIMEOUT_EVENT)
  211. {
  212. Timeout_Resend_can_adas_frame();
  213. g_event &= ~ADAS_SELF_TIMEOUT_EVENT;
  214. }
  215. if(g_event & ADAS_PWM_1_TIMEOUT_EVENT)
  216. {
  217. ADAS_PWM_1_Enable();
  218. g_event &= ~ADAS_PWM_1_TIMEOUT_EVENT;
  219. }
  220. if(g_event & ADAS_PWM_2_TIMEOUT_EVENT)
  221. {
  222. ADAS_PWM_2_Enable();
  223. g_event &= ~ADAS_PWM_2_TIMEOUT_EVENT;
  224. }
  225. if(g_event & ADAS_MEAS_1_FINISH_EVENT)
  226. {
  227. ADAS_Measure_Finish_Ju_Li_1();
  228. ADAS_Self_Send_Up();
  229. g_event &= ~ADAS_MEAS_1_FINISH_EVENT;
  230. }
  231. if(g_event & ADAS_MEAS_2_FINISH_EVENT)
  232. {
  233. ADAS_Measure_Finish_Ju_Li_2();
  234. g_event &= ~ADAS_MEAS_2_FINISH_EVENT;
  235. }
  236. if(g_event & ENTER_SLEEP_EVENT)
  237. {
  238. Enter_Sleep();
  239. g_event &= ~ENTER_SLEEP_EVENT;
  240. }
  241. if(g_event & RE_INITIAL_CAN_EVENT)
  242. {
  243. //Can_Com_Initial();
  244. g_event &= ~RE_INITIAL_CAN_EVENT;
  245. }
  246. if(g_event & DEVICE_REBOOT_EVENT)
  247. {
  248. g_event &= ~DEVICE_REBOOT_EVENT;
  249. __set_FAULTMASK(1);
  250. NVIC_SystemReset();
  251. }
  252. if(g_event & SAVE_PARAM_EVENT)
  253. {
  254. g_event &= ~SAVE_PARAM_EVENT;
  255. Save_Param();
  256. Save_Test_Info();
  257. }
  258. }
  259. //return (1);
  260. }
  261. #ifdef USE_FULL_ASSERT
  262. /**
  263. * @brief Reports the name of the source file and the source line number
  264. * where the assert_param error has occurred.
  265. * @param file: pointer to the source file name
  266. * @param line: assert_param error line source number
  267. * @retval None
  268. */
  269. void assert_failed(u8_t* file, uint32_t line)
  270. {
  271. /* User can add his own implementation to report the file name and line number,
  272. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  273. /* Infinite loop */
  274. while (1)
  275. {
  276. }
  277. }
  278. #endif
  279. /*********************************************************************************************************
  280. END FILE
  281. *********************************************************************************************************/