main.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "common.h"
  2. #include "eeprom.h"
  3. #include "flash.h"
  4. #include "detection.h"
  5. #include "fwdgt.h"
  6. #include "haddr.h"
  7. #include "relay.h"
  8. #include "lock.h"
  9. #include "uart.h"
  10. #include "timer.h"
  11. #include "led.h"
  12. #include "systick.h"
  13. #include "mb.h"
  14. #include "flash.h"
  15. #include "electricity.h"
  16. #include "gd32e23x.h"
  17. #include "string.h"
  18. #include "query.h"
  19. #include "cfg.h"
  20. board_t ac_board = {0};
  21. META_DATA_S meta_data = {0};
  22. float TotalWh[RelaySlaveChaNum] ={0.0};
  23. board_t *get_board()
  24. {
  25. return &ac_board;
  26. }
  27. extern uint16_t rl_delay_cnt[RelaySlaveChaNum];
  28. #if SUPPORT_PWM_OUT_RELAY
  29. extern uint8_t rl_open_delay_flag[RelaySlaveChaNum];
  30. extern uint8_t rl_pwm_flag[RelaySlaveChaNum];
  31. #endif
  32. static void task_query(void);
  33. static void task_1s(void);
  34. static void task_10s(void);
  35. static void task_100ms(void);
  36. static void relay_ctrl(void);
  37. static void app_init(META_DATA_S *meta);
  38. static void break_2_boot_upgreade(void);
  39. int main(){
  40. app_init(&meta_data);
  41. flash_disable_read();
  42. ac_board.md_data.r_data.dev_chn = ACSingCurrid << 8 | RelaySlaveChaNum;
  43. ac_board.break_uboot = break_2_boot_upgreade;
  44. InitSysTick();
  45. led_init();
  46. lock_init();
  47. detection_init();
  48. relay_init();
  49. haddr_init();
  50. eeprom_init();
  51. read_config_from_eeprom();
  52. electricity_init();
  53. time_init();
  54. query_init();
  55. key_init();
  56. //device
  57. fwdgt_Init();
  58. #if SUPPORT_PWM_OUT_RELAY
  59. eMBInit(MB_RTU, ac_board.haddr, 0, 38400, MB_PAR_NONE);
  60. #else
  61. eMBInit(MB_RTU, ac_board.haddr, 0, 115200, MB_PAR_NONE);
  62. #endif
  63. eMBEnable();
  64. ac_board.md_data.r_data.info.channels = RelaySlaveChaNum;
  65. strcpy((char*)&(ac_board.md_data.r_data.info.date),__DATE__);
  66. strcat((char*)&(ac_board.md_data.r_data.info.date)," ");
  67. strcat((char*)&(ac_board.md_data.r_data.info.date),__TIME__);
  68. strcpy((char*)&(ac_board.md_data.r_data.info.ver),VERSION);
  69. flash_get_infomation(&ac_board.md_data.r_data.info);
  70. #if SUPPORT_RELAY_REMENBER
  71. ac_board.handle_input_pt[0] = 1;
  72. #if SURPPORT_2_PT
  73. ac_board.handle_input_pt[1] = 1;
  74. #endif
  75. ac_board.detection();
  76. #if SUPPORT_SELF_LOCK
  77. ac_board.get_lock_key();
  78. #endif
  79. if((ac_board.md_data.r_data.detection & 0x1) == 0)
  80. {
  81. for(uint8_t i = 0; i < PT_SUB_COUNT;i++)
  82. {
  83. ac_board.handle_status[i] = ac_board.relay_staging_staus[i];
  84. ac_board.relay_staging_staus[i] = 0;
  85. #if SUPPORT_SELF_LOCK
  86. ac_board.handle_k_status[i] = ac_board.val_key[i];
  87. #endif
  88. }
  89. ac_board.handle_input_pt[0] = 0;
  90. }
  91. #if SUPPORT_SELF_LOCK
  92. else
  93. {
  94. for(uint8_t i = 0; i < PT_SUB_COUNT;i++)
  95. {
  96. if(ac_board.val_key[i] == 0)
  97. {
  98. ac_board.handle_status[i] = ac_board.relay_staging_staus[i];
  99. ac_board.relay_staging_staus[i] = 0;
  100. }else
  101. {
  102. ac_board.handle_k_status[i] = ac_board.val_key[i];
  103. }
  104. }
  105. }
  106. #endif
  107. #if SURPPORT_2_PT
  108. if((ac_board.md_data.r_data.detection & 0x2) == 0)
  109. {
  110. for(uint8_t i = PT_SUB_COUNT; i < RelaySlaveChaNum;i++)
  111. {
  112. ac_board.handle_status[i] = ac_board.relay_staging_staus[i];
  113. ac_board.relay_staging_staus[i] = 0;
  114. }
  115. ac_board.handle_input_pt[1] = 0;
  116. }
  117. #endif
  118. #endif
  119. while(1)
  120. {
  121. if(eMBCheck())
  122. {
  123. #if SUPPORT_PWM_OUT_RELAY
  124. eMBInit(MB_RTU, ac_board.haddr, 0, 38400, MB_PAR_NONE);
  125. #else
  126. eMBInit(MB_RTU, ac_board.haddr, 0, 115200, MB_PAR_NONE);
  127. #endif
  128. eMBEnable();
  129. DelayNms(1);
  130. }
  131. ac_board.feed_dog();
  132. task_query();
  133. task_100ms();
  134. task_1s();
  135. task_10s();
  136. eMBPoll();
  137. #if SUPPORT_SELF_LOCK
  138. ac_board.opt_lock_key();
  139. #endif
  140. #ifndef SUPPORT_MON
  141. ac_board.opt_detection();
  142. relay_ctrl();
  143. #endif
  144. if(ac_board.upgrade_flag)
  145. {
  146. ac_board.upgrade_flag = 0;
  147. ac_board.break_uboot();
  148. }
  149. }
  150. return 0;
  151. }
  152. static void app_init(META_DATA_S *meta)
  153. {
  154. flash_get_meta_data(meta);
  155. nvic_vector_table_set(NVIC_VECTTAB_FLASH,0X08004000 - NVIC_VECTTAB_FLASH);
  156. __enable_irq();
  157. }
  158. static void break_2_boot_upgreade(void)
  159. {
  160. meta_data.errno = 0;
  161. meta_data.operation_flag = 1;
  162. meta_data.recent_running_time = ac_board.md_data.r_data.info.runing_time;
  163. flash_erase_meta();
  164. flash_write_meta(&meta_data);
  165. __disable_irq();
  166. NVIC_SystemReset();
  167. }
  168. static void task_query(void)
  169. {
  170. w_eeprom_data_t data = {0};
  171. if(ac_board.q_cache.de_queue(&data))
  172. {
  173. ac_board.write_eeprom_page(data.start_addr,(uint8_t*)&data.data,data.lenth);
  174. }
  175. }
  176. static void task_1s(void)
  177. {
  178. if(ac_board.flag_1s)
  179. {
  180. ac_board.flag_1s = 0;
  181. ac_board.md_data.r_data.info.runing_time++;
  182. ac_board.led_flash();
  183. #ifndef SUPPORT_MON
  184. ac_board.detection();
  185. #endif
  186. #if SUPPORT_SELF_LOCK
  187. ac_board.get_lock_key();
  188. #endif
  189. }
  190. }
  191. static void task_100ms(void)
  192. {
  193. static uint8_t channel = 0;
  194. if(ac_board.flag_100ms)
  195. {
  196. ac_board.flag_100ms = 0;
  197. ac_board.read_ele(channel);
  198. ac_board.check_wanning(ELE_OUTPUT,channel);
  199. #ifndef SUPPORT_MON
  200. ac_board.opt_overfunc(channel);
  201. #endif
  202. channel++;
  203. if(channel == RelaySlaveChaNum)
  204. {
  205. channel = 0;
  206. }
  207. }
  208. }
  209. static void task_10s(void)
  210. {
  211. if(ac_board.flag_10s)
  212. {
  213. ac_board.flag_10s = 0;
  214. for(int i = 0; i < RelaySlaveChaNum;i++)
  215. {
  216. ac_board.con_da.iic_da.iic_con.iic_consumer[i] = ac_board.md_data.r_data.ac_ele.ele_info[i].consumer;
  217. }
  218. if(ac_board.con_da.row >= MAX_ROW)
  219. {
  220. ac_board.con_da.row = 0;
  221. }
  222. if(ac_board.con_da.index >= UINT64_MAX)
  223. {
  224. iic_data chache1={0};
  225. ac_board.con_da.index = 0;
  226. for(int i = 0 ; i < MAX_ROW;i++)
  227. ac_board.write_eeprom_page(EEPROM_CONSUMER_ADDR +(CONSUM_PAGE*i),(uint8_t *)&chache1,CONSUM_PAGE);
  228. ac_board.con_da.row = 0;
  229. }
  230. eMBPoll();
  231. ac_board.con_da.iic_da.iic_con.recode_index1 = ac_board.con_da.index;
  232. ac_board.write_eeprom_page(EEPROM_CONSUMER_ADDR+(CONSUM_PAGE*ac_board.con_da.row),(uint8_t*)&ac_board.con_da.iic_da.iic_con,CONSUM_PAGE);
  233. ac_board.con_da.index++;
  234. ac_board.con_da.row++;
  235. eMBPoll();
  236. }
  237. }
  238. //static void app_init(META_DATA_S *meta)
  239. //{
  240. // flash_get_meta_data(meta);
  241. // nvic_vector_table_set(NVIC_VECTTAB_FLASH,meta->rom_addr - NVIC_VECTTAB_FLASH);
  242. // __enable_irq();
  243. //}
  244. static void relay_ctrl(void)
  245. {
  246. for(uint8_t i = 0; i < RelaySlaveChaNum;i++)
  247. {
  248. if(IS_RL_ORDER_FLAG_EN(ac_board.relay_staging_staus[i]))
  249. {
  250. uint8_t rl_old_state = ac_board.md_data.r_data.state.state[i].state;
  251. uint8_t rl_now_state = GET_RL_STATUS(ac_board.relay_staging_staus[i]);
  252. if(rl_now_state > rl_old_state)
  253. {
  254. SET_RL_ON_FLAG(ac_board.relay_staging_staus[i]);
  255. CLEAR_RL_OFF_FLAG(ac_board.relay_staging_staus[i]);
  256. if(IS_RL_ON_DELAY_FLAG_EN(ac_board.relay_staging_staus[i]))
  257. {
  258. ac_board.relay_limit_counter[i] = ac_board.md_data.rw_data.open.delay_open[i]/10;
  259. CLEAR_RL_COUNTER_END_FLAG(ac_board.relay_staging_staus[i]);
  260. SET_RL_COUNTER_BEGIN_FLAG(ac_board.relay_staging_staus[i]);
  261. rl_delay_cnt[i] = 0;
  262. }else
  263. {
  264. ac_board.relay_limit_counter[i] = 0;
  265. SET_RL_COUNTER_END_FLAG(ac_board.relay_staging_staus[i]);
  266. CLEAR_RL_COUNTER_BEGIN_FLAG(ac_board.relay_staging_staus[i]);
  267. }
  268. }else if (rl_now_state < rl_old_state)
  269. {
  270. CLEAR_RL_ON_FLAG(ac_board.relay_staging_staus[i]);
  271. SET_RL_OFF_FLAG(ac_board.relay_staging_staus[i]);
  272. if(IS_RL_OFF_DELAY_FLAG_EN(ac_board.relay_staging_staus[i]))
  273. {
  274. ac_board.relay_limit_counter[i] = ac_board.md_data.rw_data.close.delay_close[i] / 10;
  275. CLEAR_RL_COUNTER_END_FLAG(ac_board.relay_staging_staus[i]);
  276. SET_RL_COUNTER_BEGIN_FLAG(ac_board.relay_staging_staus[i]);
  277. rl_delay_cnt[i] = 0;
  278. }else
  279. {
  280. ac_board.relay_limit_counter[i] = 0;
  281. SET_RL_COUNTER_END_FLAG(ac_board.relay_staging_staus[i]);
  282. CLEAR_RL_COUNTER_BEGIN_FLAG(ac_board.relay_staging_staus[i]);
  283. }
  284. }else{
  285. ac_board.relay_limit_counter[i] = 0;
  286. CLEAR_RL_COUNTER_END_FLAG(ac_board.relay_staging_staus[i]);
  287. CLEAR_RL_COUNTER_BEGIN_FLAG(ac_board.relay_staging_staus[i]);
  288. CLEAR_RL_ON_FLAG(ac_board.relay_staging_staus[i]);
  289. CLEAR_RL_OFF_FLAG(ac_board.relay_staging_staus[i]);
  290. }
  291. CLEAR_RL_ORDER_FLAG(ac_board.relay_staging_staus[i]);
  292. }
  293. }
  294. for(uint8_t i = 0; i < RelaySlaveChaNum;i++)
  295. {
  296. if(IS_RL_COUNTER_END_FLAG_EN(ac_board.relay_staging_staus[i]))
  297. {
  298. CLEAR_RL_COUNTER_END_FLAG(ac_board.relay_staging_staus[i]);
  299. if(IS_RL_ON_FLAG_EN(ac_board.relay_staging_staus[i]))
  300. {
  301. CLEAR_RL_ON_FLAG(ac_board.relay_staging_staus[i]);
  302. ac_board.open(i);
  303. #if SUPPORT_PWM_OUT_RELAY
  304. rl_open_delay_flag[i] = 1;
  305. #endif
  306. }else if(IS_RL_OFF_FLAG_EN(ac_board.relay_staging_staus[i]))
  307. {
  308. #if SUPPORT_PWM_OUT_RELAY
  309. rl_pwm_flag[i] = 0;
  310. #endif
  311. CLEAR_RL_OFF_FLAG(ac_board.relay_staging_staus[i]);
  312. ac_board.close(i);
  313. }
  314. }
  315. }
  316. }