common.c 6.7 KB

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