common.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #include "common.h"
  2. #include "board_cfg.h"
  3. #include "systick.h"
  4. #include "string.h"
  5. #if EEPROM_TEST
  6. #define TEST_ADDR (120 * 32)
  7. #define LENTH (32)
  8. #endif
  9. void reset_config_for_eeprom(void)
  10. {
  11. board_t *board = get_board();
  12. if(board)
  13. {
  14. for(uint8_t i = 0; i < MAX_CHANNEL;i++)
  15. {
  16. board->md_data.rw_data.open.delay_open[i] = (1+i) *1000;
  17. board->md_data.rw_data.close.delay_close[i] = (1+i) *1000;
  18. }
  19. memset((void*)&board->md_data.rw_data.thr.th[0].v_top,0,sizeof(ac_rw_data_t)-sizeof(ac_delay_open_t)-sizeof(ac_delay_close_t));
  20. board->write_eeprom_page(EEPROM_OTHER_ADDR,(uint8_t *)&board->md_data.rw_data.open.delay_open[0], sizeof(ac_rw_data_t));
  21. }
  22. }
  23. void read_config_from_eeprom(void)
  24. {
  25. board_t *board = get_board();
  26. if(board)
  27. {
  28. uint16_t device = 0;
  29. board->read_eeprom(EEPROM_DEVICES_ADDR,(uint8_t *)&device,EEPROM_DEVICES_LENTH);
  30. if(device != board->md_data.r_data.dev_chn)
  31. {
  32. //default write eeprom
  33. board->write_eeprom_page(EEPROM_STATUS_ADDR,(uint8_t *)&board->relay_staging_staus[0],2*RelaySlaveChaNum);
  34. for(uint8_t i = 0; i < RelaySlaveChaNum;i++)
  35. {
  36. board->md_data.rw_data.open.delay_open[i] = (1+i) * 1000;
  37. board->md_data.rw_data.close.delay_close[i] = (1+i) * 1000;
  38. }
  39. board->write_eeprom_page(EEPROM_OTHER_ADDR,(uint8_t *)&board->md_data.rw_data.open.delay_open[0], sizeof(ac_rw_data_t));
  40. iic_data chache={0};
  41. for(uint8_t i = 0 ; i < MAX_ROW;i++){
  42. board->write_eeprom_page(EEPROM_CONSUMER_ADDR +(CONSUM_PAGE*i),(uint8_t*)&chache.data[0],CONSUM_PAGE);
  43. }
  44. board->v_k[0] = 3135;
  45. board->v_k[1] = 3135;
  46. board->i_k[0] = 10000;
  47. board->i_k[1] = 10000;
  48. board->write_eeprom_page(EEPROM_K_V_ADDR,(uint8_t *)&board->v_k[0],EEPROM_K_V_LEN);
  49. board->write_eeprom_page(EEPROM_K_I_ADDR,(uint8_t *)&board->i_k[0],EEPROM_K_I_LEN);
  50. #if SUPPORT_MICRO_SWITCH
  51. board->write_eeprom_page(EEPROM_SELF_LOCK_STATUS,(uint8_t *)&board->val_key[0],EEPROM_SELF_LOCK_STATUS_LEN);
  52. #endif
  53. board->write_eeprom_page(EEPROM_DEVICES_ADDR,(uint8_t *)&board->md_data.r_data.dev_chn,EEPROM_DEVICES_LENTH);
  54. }
  55. {
  56. //channel open or close
  57. board->read_eeprom_page(EEPROM_STATUS_ADDR,(uint8_t *)&board->relay_staging_staus[0],2*RelaySlaveChaNum);
  58. {
  59. for(int i = 0 ;i < RelaySlaveChaNum;i++)
  60. {
  61. #ifndef SUPPORT_MON
  62. #if SUPPORT_CTRL2_LOCK2
  63. if(i < AVALIABLE_RELAY)
  64. {
  65. if(board->relay_staging_staus[i])
  66. {
  67. SET_RL_ON_DELAY_FLAG(board->relay_staging_staus[i]);
  68. CLEAR_RL_OFF_DELAY_FLAG(board->relay_staging_staus[i]);
  69. SET_RL_ORDER_FLAG(board->relay_staging_staus[i]);
  70. }
  71. }else
  72. {
  73. board->md_data.r_data.state.state[i].state = 1;
  74. }
  75. #else
  76. if(board->relay_staging_staus[i])
  77. {
  78. SET_RL_ON_DELAY_FLAG(board->relay_staging_staus[i]);
  79. CLEAR_RL_OFF_DELAY_FLAG(board->relay_staging_staus[i]);
  80. SET_RL_ORDER_FLAG(board->relay_staging_staus[i]);
  81. }
  82. #endif
  83. #else
  84. board->md_data.r_data.state.state[i].state = 1;
  85. #endif
  86. }
  87. }
  88. board->read_eeprom_page(EEPROM_OTHER_ADDR,(uint8_t *)&board->md_data.rw_data.open.delay_open[0],sizeof(ac_rw_data_t));
  89. for(uint8_t i = 0 ; i < RelaySlaveChaNum;i++)
  90. {
  91. board->check_threshold(THRESHOLD_OUT,i);
  92. }
  93. board->read_eeprom_page(EEPROM_K_V_ADDR,(uint8_t *)&board->v_k[0],EEPROM_K_V_LEN);
  94. board->read_eeprom_page(EEPROM_K_I_ADDR,(uint8_t *)&board->i_k[0],EEPROM_K_I_LEN);
  95. #if SUPPORT_MICRO_SWITCH
  96. board->read_eeprom_page(EEPROM_SELF_LOCK_STATUS,(uint8_t *)&board->val_key[0],EEPROM_SELF_LOCK_STATUS_LEN);
  97. for(int i = 0; i < RelaySlaveChaNum;i++)
  98. {
  99. if(board->val_key[i] > 1)
  100. {
  101. board->val_key[i] = 0;
  102. }
  103. }
  104. #endif
  105. for(int i =0 ;i < 2;i++)
  106. {
  107. board->v_k[i] = board->v_k[i] > 10000 ? 3135 : board->v_k[i];
  108. board->i_k[i] = board->i_k[i] > 10000 ? 10000 : board->i_k[i];
  109. }
  110. iic_data chache={0};
  111. for(uint8_t i = 0 ; i < MAX_ROW;i++)
  112. {
  113. board->read_eeprom_page(EEPROM_CONSUMER_ADDR + (CONSUM_PAGE*i),(uint8_t*)&chache,sizeof(iic_data));
  114. if(board->con_da.index > chache.iic_con.recode_index1)
  115. {
  116. }else
  117. {
  118. board->con_da.index = chache.iic_con.recode_index1;
  119. board->con_da.row = i;
  120. }
  121. }
  122. board->read_eeprom_page(EEPROM_CONSUMER_ADDR +(CONSUM_PAGE*(board->con_da.row)),(uint8_t*)&chache,sizeof(iic_data));
  123. for(uint16_t i = 0 ; i < RelaySlaveChaNum;i++)
  124. {
  125. board->ele_restore[i] = chache.iic_con.iic_consumer[i];
  126. }
  127. board->con_da.row++; //point next write row
  128. if(board->con_da.row >= MAX_ROW)
  129. {
  130. board->con_da.row = 0;
  131. }
  132. board->con_da.index++; //point next write consumer index
  133. if(board->con_da.index >= UINT64_MAX)
  134. {
  135. iic_data chache1={0};
  136. board->con_da.index = 0;
  137. for(uint8_t i = 0 ; i < MAX_ROW;i++){
  138. board->write_eeprom_page(EEPROM_CONSUMER_ADDR +(CONSUM_PAGE*i),(uint8_t*)&chache1,CONSUM_PAGE);
  139. DelayNms(7);
  140. }
  141. }
  142. //read chn ctrl
  143. }
  144. }
  145. }
  146. void set_relay_sta_async(async_data *data)
  147. {
  148. if(!data)
  149. goto failed;
  150. board_t *board = get_board();
  151. if(board)
  152. {
  153. if(data->fire_or_zero == _FIRE)
  154. {
  155. //if(board->md_data.rw_data.rw_d.chn_ctrl[data->channel] != 0) goto failed;
  156. if(data->w_eep_flag == FLAG_W_EEP)
  157. {
  158. w_eeprom_data_t da = {0};
  159. da.lenth =2;
  160. memcpy((void*)&da.data,(void*)&data->status,2);
  161. da.start_addr = (EEPROM_STATUS_ADDR+(data->channel*2));
  162. board->q_cache.en_queue(&da);
  163. }
  164. }
  165. else if(data->fire_or_zero == _ZERO)
  166. {
  167. #if (ZERO_CONTRL == ZERO_LINE_CTRL_YES)
  168. if(data->w_eep_flag == FLAG_W_EEP)
  169. {
  170. w_eeprom_data_t da = {0};
  171. da.lenth =2;
  172. memcpy(&da.data,&data->status,2);
  173. da.start_addr = (EEPROM_STATUS_ADDR+((data->channel+13)*2));
  174. board->q_cache.en_queue(&da);
  175. }
  176. #else
  177. goto failed;
  178. #endif
  179. }
  180. else
  181. {
  182. goto failed;
  183. }
  184. SET_RL_STATUS(*data->reg,data->status);
  185. if(data->method == DELAY_CTRL)
  186. {
  187. if(data->status)
  188. {
  189. SET_RL_ON_DELAY_FLAG(*(data->reg));
  190. CLEAR_RL_OFF_DELAY_FLAG(*(data->reg));
  191. }else
  192. {
  193. CLEAR_RL_ON_DELAY_FLAG(*(data->reg));
  194. SET_RL_OFF_DELAY_FLAG(*(data->reg));
  195. }
  196. }else
  197. {
  198. if(data->status)
  199. {
  200. CLEAR_RL_ON_DELAY_FLAG(*(data->reg));
  201. CLEAR_RL_OFF_DELAY_FLAG(*(data->reg));
  202. }else
  203. {
  204. CLEAR_RL_ON_DELAY_FLAG(*(data->reg));
  205. CLEAR_RL_OFF_DELAY_FLAG(*(data->reg));
  206. }
  207. }
  208. SET_RL_ORDER_FLAG(*(data->reg));
  209. }
  210. failed:
  211. return;
  212. }
  213. void set_relay_status_async(uint16_t *reg,uint8_t method,uint16_t status)
  214. {
  215. board_t *board = get_board();
  216. SET_RL_STATUS(*reg,status);
  217. if(method == DELAY_CTRL)
  218. {
  219. if(status)
  220. {
  221. SET_RL_ON_DELAY_FLAG(*reg);
  222. CLEAR_RL_OFF_DELAY_FLAG(*reg);
  223. }else
  224. {
  225. CLEAR_RL_ON_DELAY_FLAG(*reg);
  226. SET_RL_OFF_DELAY_FLAG(*reg);
  227. }
  228. }else
  229. {
  230. if(status)
  231. {
  232. CLEAR_RL_ON_DELAY_FLAG(*reg);
  233. CLEAR_RL_OFF_DELAY_FLAG(*reg);
  234. }else
  235. {
  236. CLEAR_RL_ON_DELAY_FLAG(*reg);
  237. CLEAR_RL_OFF_DELAY_FLAG(*reg);
  238. }
  239. }
  240. SET_RL_ORDER_FLAG(*reg);
  241. }