common.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. board->write_eeprom_page(EEPROM_DEVICES_ADDR,(uint8_t *)&board->md_data.r_data.dev_chn,EEPROM_DEVICES_LENTH);
  51. }
  52. {
  53. //channel open or close
  54. board->read_eeprom_page(EEPROM_STATUS_ADDR,(uint8_t *)&board->relay_staging_staus[0],2*RelaySlaveChaNum);
  55. {
  56. for(int i = 0 ;i < RelaySlaveChaNum;i++)
  57. {
  58. #ifndef SUPPORT_MON
  59. #if SUPPORT_CTRL2_LOCK2
  60. if(i < AVALIABLE_RELAY)
  61. {
  62. if(board->relay_staging_staus[i])
  63. {
  64. SET_RL_ON_DELAY_FLAG(board->relay_staging_staus[i]);
  65. CLEAR_RL_OFF_DELAY_FLAG(board->relay_staging_staus[i]);
  66. SET_RL_ORDER_FLAG(board->relay_staging_staus[i]);
  67. }
  68. }else
  69. {
  70. board->md_data.r_data.state.state[i].state = 1;
  71. }
  72. #else
  73. if(board->relay_staging_staus[i])
  74. {
  75. SET_RL_ON_DELAY_FLAG(board->relay_staging_staus[i]);
  76. CLEAR_RL_OFF_DELAY_FLAG(board->relay_staging_staus[i]);
  77. SET_RL_ORDER_FLAG(board->relay_staging_staus[i]);
  78. }
  79. #endif
  80. #else
  81. board->md_data.r_data.state.state[i].state = 1;
  82. #endif
  83. }
  84. }
  85. board->read_eeprom_page(EEPROM_OTHER_ADDR,(uint8_t *)&board->md_data.rw_data.open.delay_open[0],sizeof(ac_rw_data_t));
  86. for(uint8_t i = 0 ; i < RelaySlaveChaNum;i++)
  87. {
  88. board->check_threshold(THRESHOLD_OUT,i);
  89. }
  90. board->read_eeprom_page(EEPROM_K_V_ADDR,(uint8_t *)&board->v_k[0],EEPROM_K_V_LEN);
  91. board->read_eeprom_page(EEPROM_K_I_ADDR,(uint8_t *)&board->i_k[0],EEPROM_K_I_LEN);
  92. for(int i =0 ;i < 2;i++)
  93. {
  94. board->v_k[i] = board->v_k[i] > 10000 ? 3135 : board->v_k[i];
  95. board->i_k[i] = board->i_k[i] > 10000 ? 10000 : board->i_k[i];
  96. }
  97. iic_data chache={0};
  98. for(uint8_t i = 0 ; i < MAX_ROW;i++)
  99. {
  100. board->read_eeprom_page(EEPROM_CONSUMER_ADDR + (CONSUM_PAGE*i),(uint8_t*)&chache,sizeof(iic_data));
  101. if(board->con_da.index > chache.iic_con.recode_index1)
  102. {
  103. }else
  104. {
  105. board->con_da.index = chache.iic_con.recode_index1;
  106. board->con_da.row = i;
  107. }
  108. }
  109. board->read_eeprom_page(EEPROM_CONSUMER_ADDR +(CONSUM_PAGE*(board->con_da.row)),(uint8_t*)&chache,sizeof(iic_data));
  110. for(uint16_t i = 0 ; i < RelaySlaveChaNum;i++)
  111. {
  112. board->ele_restore[i] = chache.iic_con.iic_consumer[i];
  113. }
  114. board->con_da.row++; //point next write row
  115. if(board->con_da.row >= MAX_ROW)
  116. {
  117. board->con_da.row = 0;
  118. }
  119. board->con_da.index++; //point next write consumer index
  120. if(board->con_da.index >= UINT64_MAX)
  121. {
  122. iic_data chache1={0};
  123. board->con_da.index = 0;
  124. for(uint8_t i = 0 ; i < MAX_ROW;i++){
  125. board->write_eeprom_page(EEPROM_CONSUMER_ADDR +(CONSUM_PAGE*i),(uint8_t*)&chache1,CONSUM_PAGE);
  126. DelayNms(7);
  127. }
  128. }
  129. //read chn ctrl
  130. }
  131. }
  132. }
  133. void set_relay_sta_async(async_data *data)
  134. {
  135. if(!data)
  136. goto failed;
  137. board_t *board = get_board();
  138. if(board)
  139. {
  140. if(data->fire_or_zero == _FIRE)
  141. {
  142. //if(board->md_data.rw_data.rw_d.chn_ctrl[data->channel] != 0) goto failed;
  143. if(data->w_eep_flag == FLAG_W_EEP)
  144. {
  145. w_eeprom_data_t da = {0};
  146. da.lenth =2;
  147. memcpy((void*)&da.data,(void*)&data->status,2);
  148. da.start_addr = (EEPROM_STATUS_ADDR+(data->channel*2));
  149. board->q_cache.en_queue(&da);
  150. }
  151. }
  152. else if(data->fire_or_zero == _ZERO)
  153. {
  154. #if (ZERO_CONTRL == ZERO_LINE_CTRL_YES)
  155. if(data->w_eep_flag == FLAG_W_EEP)
  156. {
  157. w_eeprom_data_t da = {0};
  158. da.lenth =2;
  159. memcpy(&da.data,&data->status,2);
  160. da.start_addr = (EEPROM_STATUS_ADDR+((data->channel+13)*2));
  161. board->q_cache.en_queue(&da);
  162. }
  163. #else
  164. goto failed;
  165. #endif
  166. }
  167. else
  168. {
  169. goto failed;
  170. }
  171. SET_RL_STATUS(*data->reg,data->status);
  172. if(data->method == DELAY_CTRL)
  173. {
  174. if(data->status)
  175. {
  176. SET_RL_ON_DELAY_FLAG(*(data->reg));
  177. CLEAR_RL_OFF_DELAY_FLAG(*(data->reg));
  178. }else
  179. {
  180. CLEAR_RL_ON_DELAY_FLAG(*(data->reg));
  181. SET_RL_OFF_DELAY_FLAG(*(data->reg));
  182. }
  183. }else
  184. {
  185. if(data->status)
  186. {
  187. CLEAR_RL_ON_DELAY_FLAG(*(data->reg));
  188. CLEAR_RL_OFF_DELAY_FLAG(*(data->reg));
  189. }else
  190. {
  191. CLEAR_RL_ON_DELAY_FLAG(*(data->reg));
  192. CLEAR_RL_OFF_DELAY_FLAG(*(data->reg));
  193. }
  194. }
  195. SET_RL_ORDER_FLAG(*(data->reg));
  196. }
  197. failed:
  198. return;
  199. }
  200. void set_relay_status_async(uint16_t *reg,uint8_t method,uint16_t status)
  201. {
  202. board_t *board = get_board();
  203. SET_RL_STATUS(*reg,status);
  204. if(method == DELAY_CTRL)
  205. {
  206. if(status)
  207. {
  208. SET_RL_ON_DELAY_FLAG(*reg);
  209. CLEAR_RL_OFF_DELAY_FLAG(*reg);
  210. }else
  211. {
  212. CLEAR_RL_ON_DELAY_FLAG(*reg);
  213. SET_RL_OFF_DELAY_FLAG(*reg);
  214. }
  215. }else
  216. {
  217. if(status)
  218. {
  219. CLEAR_RL_ON_DELAY_FLAG(*reg);
  220. CLEAR_RL_OFF_DELAY_FLAG(*reg);
  221. }else
  222. {
  223. CLEAR_RL_ON_DELAY_FLAG(*reg);
  224. CLEAR_RL_OFF_DELAY_FLAG(*reg);
  225. }
  226. }
  227. SET_RL_ORDER_FLAG(*reg);
  228. }