common.c 6.3 KB

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