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