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