common.c 5.7 KB

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