port.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * FreeModbus Libary: BARE Demo Application
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id$
  20. */
  21. /* ----------------------- Modbus includes ----------------------------------*/
  22. #include "mb.h"
  23. #include "mbport.h"
  24. #include "string.h"
  25. #include "main.h"
  26. #include "board_cfg.h"
  27. #include "common.h"
  28. #include "flash.h"
  29. #include "gd32e23x.h"
  30. #include "relay.h"
  31. /* ----------------------- Defines ------------------------------------------*/
  32. //输入寄存器
  33. #define REG_INPUT_START 1000
  34. #define REG_INPUT_NREGS 1
  35. //保持寄存器
  36. #define REG_HOLD_START START_R_REGISTER
  37. #define REG_HOLD_NREGS 400
  38. //线圈
  39. #define REG_COILS_START 0
  40. #define REG_COILS_NREGS 1
  41. //开关寄存器
  42. #define REG_DISCRETE_START 1000
  43. #define REG_DISCRETE_NREGS 1
  44. /* ----------------------- Static variables ---------------------------------*/
  45. static USHORT usRegInputStart = REG_INPUT_START;
  46. static USHORT usRegInputBuf[REG_INPUT_NREGS];
  47. static USHORT usRegHoldStart = REG_HOLD_START;
  48. static USHORT usRegHoldBuf[REG_HOLD_NREGS];
  49. static USHORT usRegCoilsStart = REG_COILS_START;
  50. static uint8_t usRegCoilsBuf[REG_COILS_NREGS];
  51. static USHORT usRegDiscreteStart = REG_DISCRETE_START;
  52. static uint8_t usRegDiscreteBuf[REG_DISCRETE_NREGS];
  53. #define offsetof(type,member) ((int) &((type *)0)->member)
  54. /****************************************************************************
  55. * 名 称:eMBRegInputCB
  56. * 功 能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
  57. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  58. * usAddress: 寄存器地址
  59. * usNRegs: 要读取的寄存器个数
  60. * 出口参数:
  61. * 注 意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte)
  62. * +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)
  63. * +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
  64. * +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
  65. * 3 区
  66. ****************************************************************************/
  67. eMBErrorCode
  68. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  69. {
  70. eMBErrorCode eStatus = MB_ENOERR;
  71. int iRegIndex;
  72. usAddress = usAddress - 1;
  73. if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
  74. {
  75. iRegIndex = ( int )( usAddress - usRegInputStart );
  76. while( usNRegs > 0 )
  77. {
  78. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
  79. *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
  80. iRegIndex++;
  81. usNRegs--;
  82. }
  83. }
  84. else
  85. {
  86. eStatus = MB_ENOREG;
  87. }
  88. return eStatus;
  89. }
  90. /****************************************************************************
  91. * 名 称:eMBRegHoldingCB
  92. * 功 能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister
  93. * 16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister
  94. * 03 读保持寄存器 eMBFuncReadHoldingRegister
  95. * 23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
  96. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  97. * usAddress: 寄存器地址
  98. * usNRegs: 要读写的寄存器个数
  99. * eMode: 功能码
  100. * 出口参数:
  101. * 注 意:4 区
  102. ****************************************************************************/
  103. unsigned char WusNRegs;
  104. unsigned int WiRegIndex;
  105. extern META_DATA_S meta_data;
  106. extern float elec[RelaySlaveChaNum];
  107. static void break_2_boot_upgreade(void)
  108. {
  109. meta_data.errno = 0;
  110. meta_data.operation_flag = 1;
  111. meta_data.recent_running_time = 25;
  112. flash_erase_meta();
  113. flash_write_meta(&meta_data);
  114. __disable_irq();
  115. //__set_FAULTMASK(1);
  116. NVIC_SystemReset();
  117. }
  118. static void soft_reset(void)
  119. {
  120. __disable_irq();
  121. //__set_FAULTMASK(1);
  122. NVIC_SystemReset();
  123. }
  124. eMBErrorCode
  125. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
  126. {
  127. eMBErrorCode eStatus = MB_ENOERR;
  128. int iRegIndex;
  129. usAddress = usAddress - 1;
  130. uint32_t T_cnt = 0;
  131. board_t *board = get_board();
  132. if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
  133. {
  134. iRegIndex = (int)(usAddress - usRegHoldStart);
  135. WiRegIndex = iRegIndex;
  136. uint16_t start = 0;
  137. uint32_t *base = 0;
  138. switch(eMode)
  139. {
  140. case MB_REG_READ://读寄存器
  141. {
  142. if(usAddress == REG_HOLD_START)
  143. {
  144. base = (uint32_t*)&board->md_data.r_data.dev_info;
  145. }else if((usAddress >= INPUT_INFO) && usAddress <= (INPUT_INFO+INPUT_LENTH))
  146. {
  147. start = usAddress-INPUT_INFO;
  148. base = (uint32_t*)&(board->md_data.r_data.i_voltage);
  149. base += start;
  150. }else if((usAddress >= OUTPUT_INFO) && (usAddress <= (OUTPUT_INFO + OUTPUT_LENTH)))
  151. {
  152. start = usAddress-OUTPUT_INFO;
  153. base = (uint32_t*)&(board->md_data.r_data.output[0].voltage);
  154. base += start;
  155. }else if(usAddress == DETECTION_INFO)
  156. {
  157. base = (uint32_t*)&(board->md_data.r_data.detection);
  158. }else if(usAddress == IN_WANNING_INFO)
  159. {
  160. base = (uint32_t*)&(board->md_data.r_data.w_input);
  161. }
  162. else if((usAddress >= OUT_WANNING_INFO) && (usAddress <=(OUT_WANNING_INFO + OUT_WANNING_INFO_LENTH)))
  163. {
  164. start = usAddress-OUT_WANNING_INFO;
  165. base = (uint32_t*)&(board->md_data.r_data.wanning[0]);
  166. base += start;
  167. }else if((usAddress >= THR_INPUT) && (usAddress <= (THR_INPUT + THR_INPUT_LENTH)))
  168. {
  169. start = usAddress-THR_INPUT;
  170. base = (uint32_t*)&(board->md_data.rw_data.i_V_max);
  171. base += start;
  172. }else if((usAddress >= THR_OUTPUT_V_MAX) && (usAddress <= (THR_OUTPUT_V_MAX + THR_OUTPUT_V_MAX_LENTH)))
  173. {
  174. start = usAddress-THR_OUTPUT_V_MAX;
  175. base = (uint32_t*)&(board->md_data.rw_data.o_V_max[0]);
  176. base += start;
  177. }else if((usAddress >= THR_OUTPUT_V_MIN) && (usAddress <= (THR_OUTPUT_V_MIN + THR_OUTPUT_V_MIN_LENTH)))
  178. {
  179. start = usAddress-THR_OUTPUT_V_MIN;
  180. base = (uint32_t*)&(board->md_data.rw_data.o_V_min[0]);
  181. base += start;
  182. }else if((usAddress >= THR_OUTPUT_I_MAX) &&(usAddress <= (THR_OUTPUT_I_MAX + THR_OUTPUT_I_MAX_LENTH)))
  183. {
  184. start = usAddress-THR_OUTPUT_I_MAX;
  185. base = (uint32_t*)&(board->md_data.rw_data.o_I_max[0]);
  186. base += start;
  187. }else if((usAddress >= THR_OUTPUT_P_MAX) &&(usAddress <= THR_OUTPUT_P_MAX + THR_OUTPUT_P_MAX_LENTH))
  188. {
  189. start = usAddress-THR_OUTPUT_P_MAX;
  190. base = (uint32_t*)&(board->md_data.rw_data.o_P_max[0]);
  191. base += start;
  192. }else if((usAddress >= THR_OUTPUT_C_MAX) &&(usAddress <= THR_OUTPUT_C_MAX + THR_OUTPUT_C_MAX_LENTH))
  193. {
  194. start = usAddress-THR_OUTPUT_C_MAX;
  195. base = (uint32_t*)&(board->md_data.rw_data.o_C_max[0]);
  196. base += start;
  197. }
  198. else if((usAddress >= DELAY_ON) && (usAddress <=(DELAY_ON +DELAY_ON_LEN)))
  199. {
  200. start = usAddress-DELAY_ON;
  201. base = (uint32_t*)&(board->md_data.rw_data.delay_on[0]);
  202. }else if((usAddress >= DELAY_OFF) && (usAddress <= DELAY_OFF_LEN))
  203. {
  204. start = usAddress-DELAY_OFF;
  205. base = (uint32_t*)&(board->md_data.rw_data.delay_off[0]);
  206. base += start;
  207. }else if((usAddress >= PHR_LACK) && (usAddress <= (PHR_LACK + PHR_LACK_LENTH)))
  208. {
  209. start = usAddress-PHR_LACK;
  210. base = (uint32_t*)&(board->md_data.r_data.Lack_A);
  211. base += start;
  212. }else if(usAddress >= OVER_FUNC && (usAddress <=(OVER_FUNC + OVER_FUNC_LENTH)))
  213. {
  214. start = usAddress-OVER_FUNC;
  215. base = (uint32_t*)&(board->md_data.rw_data.over_func[0]);
  216. base += start;
  217. }else if(usAddress == DEVICE_DETAIL_INFO)
  218. {
  219. base = (uint32_t*)&(board->md_data.r_data.info);
  220. }else if(usAddress >= CHANNEL_CONTROL && usAddress <= (CHANNEL_CONTROL + CHANNEL_CONTROL_LENTH))
  221. {
  222. start = usAddress-CHANNEL_CONTROL;
  223. base = (uint32_t*)&(board->md_data.rw_data.channel_ctrl[0]);
  224. base += start;
  225. }else if(usAddress >= REALAY_CTRL && usAddress <= (REALAY_CTRL +REALAY_CTRL_LENTH))
  226. {
  227. start = usAddress-REALAY_CTRL;
  228. base = (uint32_t*)&(board->md_data.r_data.status[0]);
  229. base += start;
  230. }
  231. else
  232. {
  233. eStatus = MB_ENOREG;;;
  234. }
  235. if(base)
  236. {
  237. memcpy(&usRegHoldBuf[iRegIndex],base,usNRegs*2);
  238. }
  239. while(usNRegs > 0)
  240. {
  241. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
  242. *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
  243. iRegIndex++;
  244. usNRegs--;
  245. }
  246. break;
  247. }
  248. case MB_REG_WRITE://写寄存器
  249. {
  250. WusNRegs = usNRegs;
  251. while(usNRegs > 0)
  252. {
  253. usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
  254. usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
  255. iRegIndex++;
  256. usNRegs--;
  257. }
  258. {
  259. w_eeprom_data_t data = {0};
  260. if(usAddress >= REALAY_CTRL && (usAddress <= (REALAY_CTRL + REALAY_CTRL_LENTH)))
  261. {
  262. #if(!SUPPORT_MON)
  263. async_data da = {0};
  264. da.fire_or_zero = _FIRE;
  265. da.w_eep_flag = FLAG_W_EEP;
  266. #if (AC_3_3 || AC_3_4 || AC_3_3_MON)
  267. #if SUPPORT_DETECTION
  268. if(board->md_data.r_data.detection & 0x1)
  269. #endif
  270. {
  271. start = usAddress - REALAY_CTRL;
  272. da.method = PROMPT_CTRL;
  273. da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
  274. if(start == 9)
  275. {
  276. da.fire_or_zero = _ZERO;
  277. da.reg = &board->relay_zero_staging_status;
  278. }else
  279. {
  280. da.channel = start;
  281. da.reg = &board->relay_staging_staus[start];
  282. }
  283. set_relay_sta_async(&da);
  284. }
  285. #else
  286. start = usAddress - REALAY_CTRL;
  287. uint32_t flag = 0;
  288. if(board->md_data.r_data.detection != 0x3)
  289. {
  290. if(start >= PT_SUB_COUNT)
  291. {
  292. if(board->md_data.rw_data.channel_ctrl[start] != 0 || board->md_data.rw_data.channel_ctrl[start - PT_SUB_COUNT] != 0)
  293. {
  294. if(board->md_data.rw_data.channel_ctrl[start] == 0 && (board->md_data.r_data.detection & 0x2))
  295. {
  296. flag = 1;
  297. }
  298. }
  299. }else{
  300. if(board->md_data.rw_data.channel_ctrl[start] != 0 || board->md_data.rw_data.channel_ctrl[start + PT_SUB_COUNT] != 0)
  301. {
  302. if(board->md_data.rw_data.channel_ctrl[start] == 0 && (board->md_data.r_data.detection & 0x1))
  303. {
  304. flag = 1;
  305. }
  306. }
  307. }
  308. }else
  309. {
  310. if(board->md_data.rw_data.channel_ctrl[start] ==0)
  311. {
  312. flag = 1;
  313. }
  314. }
  315. if(flag)
  316. {
  317. da.method = PROMPT_CTRL;
  318. da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
  319. da.channel = start;
  320. da.reg = &board->relay_staging_staus[start];
  321. set_relay_sta_async(&da);
  322. }
  323. #endif
  324. }else if(usAddress >= COMBINE_RELAY && usAddress <=(COMBINE_RELAY + COMBINE_RELAY_LENTH))
  325. {
  326. async_data da = {0};
  327. da.fire_or_zero = _FIRE;
  328. da.w_eep_flag = FLAG_W_EEP;
  329. #if (AC_3_3 || AC_3_4 || AC_3_3_MON)
  330. #if SUPPORT_DETECTION
  331. if(board->md_data.r_data.detection & 0x1)
  332. #endif
  333. {
  334. start = (usAddress - COMBINE_RELAY) * 3; //3 pt
  335. if(board->md_data.rw_data.channel_ctrl[start ] == 0 &&
  336. board->md_data.rw_data.channel_ctrl[start+1] == 0 &&
  337. board->md_data.rw_data.channel_ctrl[start+2] == 0)
  338. {
  339. for(int i = 0; i < RelaySlaveChaNum;i += 3)
  340. {
  341. for(int j = 0; j < 3;j++)
  342. {
  343. da.channel = j + i*3;
  344. da.method = PROMPT_CTRL;
  345. da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
  346. da.reg = &board->relay_staging_staus[da.channel];
  347. set_relay_sta_async(&da);
  348. }
  349. }
  350. #if SUPPORT_ZERO_CRTL
  351. // da.fire_or_zero = _ZERO;
  352. // da.reg = &board->relay_zero_staging_status;
  353. // set_relay_sta_async(&da);
  354. #endif
  355. }
  356. }
  357. #else
  358. if(board->md_data.r_data.detection == 0x3)
  359. {
  360. start = usAddress - COMBINE_RELAY;
  361. // for(int i = start;i < PT_SUB_COUNT;i++)
  362. // {
  363. if(board->md_data.rw_data.channel_ctrl[start ] == 0 && board->md_data.rw_data.channel_ctrl[start + PT_SUB_COUNT] == 0)
  364. {
  365. da.channel = start;
  366. da.method = PROMPT_CTRL;
  367. da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
  368. da.reg = &board->relay_staging_staus[da.channel];
  369. set_relay_sta_async(&da);
  370. da.channel = start + PT_SUB_COUNT;
  371. da.reg = &board->relay_staging_staus[da.channel];
  372. set_relay_sta_async(&da);
  373. }
  374. // }
  375. }
  376. #endif
  377. #endif
  378. }
  379. else if(usAddress == ALL_ON || usAddress == ALL_OFF)
  380. {
  381. #if (!SUPPORT_MON)
  382. async_data da = {0};
  383. da.fire_or_zero = _FIRE;
  384. da.w_eep_flag = FLAG_W_EEP;
  385. #if (AC_3_3 || AC_3_4 ||AC_3_3_MON)
  386. #if SUPPORT_DETECTION
  387. if(board->md_data.r_data.detection & 0x1)
  388. #endif
  389. {
  390. for(uint8_t j = 0; j < RelaySlaveChaNum;j++)
  391. {
  392. da.channel = j;
  393. da.method = DELAY_CTRL;
  394. da.reg = &board->relay_staging_staus[j];
  395. da.status = (usAddress == ALL_ON)? RELAY_OPEN : RELAY_CLOSE;
  396. set_relay_sta_async(&da);
  397. }
  398. #if SUPPORT_ZERO_CRTL
  399. da.fire_or_zero = _ZERO;
  400. da.method = DELAY_CTRL;
  401. da.reg = &board->relay_zero_staging_status;
  402. set_relay_sta_async(&da);
  403. #endif
  404. }
  405. #else
  406. da.method = DELAY_CTRL;
  407. da.status = (usAddress == ALL_ON) ? RELAY_OPEN : RELAY_CLOSE;
  408. if(board->md_data.r_data.detection != 0x3)
  409. {
  410. uint8_t sync_buff[PT_SUB_COUNT] = {0};
  411. if(board->md_data.r_data.detection & 0x1)
  412. {
  413. for(int i = 0;i < PT_SUB_COUNT;i++)
  414. {
  415. if(board->md_data.rw_data.channel_ctrl[i ] == 0 && board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
  416. {
  417. da.channel = i;
  418. da.reg = &board->relay_staging_staus[da.channel];
  419. set_relay_sta_async(&da);
  420. da.channel = i + PT_SUB_COUNT;
  421. da.reg = &board->relay_staging_staus[da.channel];
  422. set_relay_sta_async(&da);
  423. sync_buff[i] = 1;
  424. }else
  425. {
  426. if(board->md_data.rw_data.channel_ctrl[i ] == 0)
  427. {
  428. da.channel = i;
  429. da.reg = &board->relay_staging_staus[da.channel];
  430. set_relay_sta_async(&da);
  431. }
  432. }
  433. }
  434. }
  435. if(board->md_data.r_data.detection & 0x2)
  436. {
  437. for(int i = 0;i < PT_SUB_COUNT;i++)
  438. {
  439. if(board->md_data.rw_data.channel_ctrl[i ] == 0 && board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
  440. {
  441. if(!sync_buff[i])
  442. {
  443. da.channel = i;
  444. da.reg = &board->relay_staging_staus[da.channel];
  445. set_relay_sta_async(&da);
  446. da.channel = i + PT_SUB_COUNT;
  447. da.reg = &board->relay_staging_staus[da.channel];
  448. set_relay_sta_async(&da);
  449. }
  450. }else
  451. {
  452. if(board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
  453. {
  454. da.channel = i + PT_SUB_COUNT;
  455. da.reg = &board->relay_staging_staus[da.channel];
  456. set_relay_sta_async(&da);
  457. }
  458. }
  459. }
  460. }
  461. }else
  462. {
  463. for(int i = 0;i < PT_SUB_COUNT;i++)
  464. {
  465. if(board->md_data.rw_data.channel_ctrl[i ] == 0)
  466. {
  467. da.channel = i;
  468. da.reg = &board->relay_staging_staus[da.channel];
  469. set_relay_sta_async(&da);
  470. }
  471. if(board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
  472. {
  473. da.channel = i + PT_SUB_COUNT;
  474. da.reg = &board->relay_staging_staus[da.channel];
  475. set_relay_sta_async(&da);
  476. }
  477. }
  478. }
  479. #endif
  480. #endif
  481. }else if(usAddress >= THR_INPUT && usAddress <=(THR_OUTPUT_C_MAX+ THR_OUTPUT_C_MAX_LENTH))
  482. {
  483. uint16_t rom_start;
  484. if(usAddress >= THR_INPUT && (usAddress <=(THR_INPUT +THR_INPUT_LENTH)))
  485. {
  486. start = usAddress - THR_INPUT;
  487. base = (uint32_t*)(&board->md_data.rw_data.i_V_max) + start;
  488. rom_start = sizeof(uint32_t) * 18 + EEPROM_OTHER_ADDR;
  489. }else if(usAddress >= THR_OUTPUT_V_MAX && usAddress <=(THR_OUTPUT_V_MAX +THR_OUTPUT_V_MAX_LENTH))
  490. {
  491. start = usAddress - THR_OUTPUT_V_MAX;
  492. base = (uint32_t*)(&board->md_data.rw_data.o_V_max[0]) + start;
  493. rom_start = sizeof(uint32_t) * 23 + EEPROM_OTHER_ADDR;
  494. }else if(usAddress >= THR_OUTPUT_V_MIN && usAddress <=(THR_OUTPUT_V_MIN +THR_OUTPUT_V_MIN_LENTH))
  495. {
  496. start = usAddress - THR_OUTPUT_V_MIN;
  497. base = (uint32_t*)(&board->md_data.rw_data.o_V_min[0]) + start;
  498. rom_start = sizeof(uint32_t) * 31 + EEPROM_OTHER_ADDR;
  499. }else if(usAddress >= THR_OUTPUT_I_MAX && usAddress <=(THR_OUTPUT_I_MAX +THR_OUTPUT_I_MAX_LENTH))
  500. {
  501. start = usAddress - THR_OUTPUT_I_MAX;
  502. base = (uint32_t*)(&board->md_data.rw_data.o_I_max[0]) + start;
  503. rom_start = sizeof(uint32_t) * 39 + EEPROM_OTHER_ADDR;
  504. }else if(usAddress >= THR_OUTPUT_P_MAX && usAddress <=(THR_OUTPUT_P_MAX +THR_OUTPUT_P_MAX_LENTH))
  505. {
  506. start = usAddress - THR_OUTPUT_P_MAX;
  507. base = (uint32_t*)(&board->md_data.rw_data.o_P_max[0]) + start;
  508. rom_start = sizeof(uint32_t) * 47 + EEPROM_OTHER_ADDR;
  509. }else if(usAddress >= THR_OUTPUT_C_MAX && usAddress <=(THR_OUTPUT_C_MAX +THR_OUTPUT_C_MAX_LENTH))
  510. {
  511. start = usAddress - THR_OUTPUT_C_MAX;
  512. base = (uint32_t*)(&board->md_data.rw_data.o_C_max[0]) + start;
  513. rom_start = sizeof(uint32_t) * 55 + EEPROM_OTHER_ADDR;
  514. }
  515. if(base)
  516. {
  517. memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  518. for(uint8_t j = 0; j < RelaySlaveChaNum;j++)
  519. board->check_threshold(THRESHOLD_OUT,j);
  520. board->check_threshold(THRESHOLD_IN,0);
  521. data.lenth =WusNRegs*2;
  522. memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  523. data.start_addr = (rom_start+(start*4));
  524. board->q_cache.en_queue(&data);
  525. }
  526. }else if(usAddress >= DELAY_ON && usAddress <= (DELAY_OFF + DELAY_OFF_LEN))
  527. {
  528. uint16_t rom_start;
  529. if(usAddress >= DELAY_ON && usAddress <= (DELAY_ON + DELAY_ON_LEN))
  530. {
  531. start = usAddress - DELAY_ON;
  532. base = (uint32_t*)(&board->md_data.rw_data.delay_on[0]) + start;
  533. rom_start = EEPROM_OTHER_ADDR;
  534. }else if(usAddress >= DELAY_OFF && usAddress <= (DELAY_OFF + DELAY_OFF_LEN))
  535. {
  536. start = usAddress - DELAY_OFF;
  537. base = (uint32_t*)(&board->md_data.rw_data.delay_off[0]) + start;
  538. rom_start = sizeof(uint32_t) * 8 + EEPROM_OTHER_ADDR;
  539. }
  540. if(base)
  541. {
  542. memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  543. data.lenth =WusNRegs*2;
  544. memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  545. data.start_addr = (rom_start+(start*4));
  546. board->q_cache.en_queue(&data);
  547. #if SUPPORT_ZERO_CRTL
  548. board->md_data.rw_data.delay_N_on = board->md_data.rw_data.delay_on[0];
  549. board->md_data.rw_data.delay_N_off = board->md_data.rw_data.delay_off[0];
  550. memcpy(&data.data,&board->md_data.rw_data.delay_N_on,2*sizeof(uint32_t));
  551. data.lenth =2*sizeof(uint32_t);
  552. data.start_addr = (EEPROM_OTHER_ADDR+(16*4));
  553. board->q_cache.en_queue(&data);
  554. #endif
  555. }
  556. }else if(usAddress >= OVER_FUNC && usAddress <= (CHANNEL_CONTROL + CHANNEL_CONTROL_LENTH))
  557. {
  558. uint16_t rom_start;
  559. if(usAddress >= OVER_FUNC && usAddress <=(OVER_FUNC + OVER_FUNC_LENTH))
  560. {
  561. start = usAddress - OVER_FUNC;
  562. base = (uint32_t*)(&board->md_data.rw_data.over_func[0]) + start;
  563. rom_start = EEPROM_OTHER_ADDR + sizeof(uint32_t) * 63;
  564. }else if(usAddress >= CHANNEL_CONTROL && usAddress <=(CHANNEL_CONTROL + CHANNEL_CONTROL_LENTH))
  565. {
  566. start = usAddress - CHANNEL_CONTROL;
  567. base = (uint32_t*)(&board->md_data.rw_data.channel_ctrl[0]) + start;
  568. rom_start = EEPROM_OTHER_ADDR + sizeof(uint32_t) * 71;
  569. async_data da = {0};
  570. da.fire_or_zero = _FIRE;
  571. da.w_eep_flag = FLAG_W_EEP;
  572. da.method = PROMPT_CTRL;
  573. da.channel = start;
  574. da.reg = &board->relay_staging_staus[da.channel];
  575. if(usRegHoldBuf[WiRegIndex] == 1)
  576. {
  577. da.status = RELAY_OPEN;
  578. set_relay_sta_async(&da);
  579. }else if(usRegHoldBuf[WiRegIndex] == 2)
  580. {
  581. da.status = RELAY_CLOSE;
  582. set_relay_sta_async(&da);
  583. }
  584. }else if(usAddress >= CLEAR_CONSUMER && (usAddress <=(CLEAR_CONSUMER + CLEAR_CONSUMER_LENTH)))
  585. {
  586. start = usAddress - CLEAR_CONSUMER;
  587. elec[start] = 0.0;
  588. board->ele_restore[start] = 0;
  589. }else if(usAddress == CLEAR_ALL_CONSUMER)
  590. {
  591. for(int i = 0; i < RelaySlaveChaNum;i++)
  592. {
  593. elec[i] = 0.0;
  594. board->ele_restore[i] = 0;
  595. }
  596. }else if(usAddress == UPGRADE)
  597. {
  598. }else
  599. {
  600. }
  601. if(base)
  602. {
  603. memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  604. data.lenth =WusNRegs*2;
  605. memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  606. data.start_addr = (rom_start+(start*4));
  607. board->q_cache.en_queue(&data);
  608. }
  609. }else if(usAddress >= SET_V_K_VALUE && usAddress <= (SET_V_K_VALUE + SET_V_K_VALUE_LEN))
  610. {
  611. uint16_t rom_start = 0;
  612. start = usAddress - SET_V_K_VALUE;
  613. base = (uint32_t*)(&board->v_k[0]) + start;
  614. memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  615. rom_start = EEPROM_SET_V_K_ADDR;
  616. data.lenth =WusNRegs*2;
  617. data.start_addr = (rom_start+(start*4));
  618. memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  619. board->q_cache.en_queue(&data);
  620. }else if(usAddress >= SET_I_K_VALUE && usAddress <= (SET_I_K_VALUE + SET_I_K_VALUE_LEN))
  621. {
  622. uint16_t rom_start = 0;
  623. start = usAddress - SET_I_K_VALUE;
  624. base = (uint32_t*)(&board->i_k[0]) + start;
  625. memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  626. rom_start = EEPROM_SET_K_I_ADDR;
  627. data.lenth =WusNRegs*2;
  628. data.start_addr = (rom_start+(start*4));
  629. memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
  630. board->q_cache.en_queue(&data);
  631. }
  632. }
  633. break;
  634. }
  635. }
  636. }
  637. else//错误
  638. {
  639. eStatus = MB_ENOREG;
  640. }
  641. return eStatus;
  642. }
  643. /****************************************************************************
  644. * 名 称:eMBRegCoilsCB
  645. * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
  646. * 05 写线圈 eMBFuncWriteCoil
  647. * 15 写多个线圈 eMBFuncWriteMultipleCoils
  648. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  649. * usAddress: 线圈地址
  650. * usNCoils: 要读写的线圈个数
  651. * eMode: 功能码
  652. * 出口参数:
  653. * 注 意:如继电器
  654. * 0 区
  655. ****************************************************************************/
  656. eMBErrorCode
  657. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
  658. {
  659. eMBErrorCode eStatus = MB_ENOERR;
  660. USHORT iRegIndex;
  661. USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
  662. UCHAR ucStatus = 0;
  663. UCHAR ucBits = 0;
  664. UCHAR ucDisp = 0;
  665. usAddress = usAddress - 1;
  666. if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
  667. {
  668. iRegIndex = (int)(usAddress - usRegCoilsStart);
  669. switch(eMode)
  670. {
  671. case MB_REG_READ://读线圈
  672. while(usCoilGroups--)
  673. {
  674. ucDisp = 0;
  675. ucBits = 8;
  676. while((usNCoils--) != 0 && (ucBits--) != 0)
  677. {
  678. ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
  679. }
  680. *pucRegBuffer++ = ucStatus;
  681. }
  682. break;
  683. case MB_REG_WRITE://写线圈
  684. while(usCoilGroups--)
  685. {
  686. ucStatus = *pucRegBuffer++;
  687. ucBits = 8;
  688. while((usNCoils--) != 0 && (ucBits--) != 0)
  689. {
  690. usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
  691. ucStatus >>= 1;
  692. }
  693. }
  694. }
  695. }
  696. else//错误
  697. {
  698. eStatus = MB_ENOREG;
  699. }
  700. return eStatus;
  701. }
  702. /****************************************************************************
  703. * 名 称:eMBRegDiscreteCB
  704. * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
  705. * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
  706. * usAddress: 寄存器地址
  707. * usNDiscrete: 要读取的寄存器个数
  708. * 出口参数:
  709. * 注 意:1 区
  710. ****************************************************************************/
  711. eMBErrorCode
  712. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  713. {
  714. eMBErrorCode eStatus = MB_ENOERR;
  715. USHORT iRegIndex;
  716. USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
  717. UCHAR ucStatus = 0;
  718. UCHAR ucBits = 0;
  719. UCHAR ucDisp = 0;
  720. usAddress = usAddress - 1;
  721. if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
  722. {
  723. iRegIndex = (int)(usAddress - usRegDiscreteStart);
  724. while(usDiscreteGroups--)
  725. {
  726. ucDisp = 0;
  727. ucBits = 8;
  728. while((usNDiscrete--) != 0 && (ucBits--) != 0)
  729. {
  730. if(usRegDiscreteBuf[iRegIndex])
  731. {
  732. ucStatus |= (1 << ucDisp);
  733. }
  734. ucDisp++;
  735. }
  736. *pucRegBuffer++ = ucStatus;
  737. }
  738. }
  739. else//错误
  740. {
  741. eStatus = MB_ENOREG;
  742. }
  743. return eStatus;
  744. }