| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798 |
- /*
- * FreeModbus Libary: BARE Demo Application
- * Copyright (C) 2006 Christian Walter <wolti@sil.at>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * File: $Id$
- */
- /* ----------------------- Modbus includes ----------------------------------*/
- #include "mb.h"
- #include "mbport.h"
- #include "string.h"
- #include "main.h"
- #include "board_cfg.h"
- #include "common.h"
- #include "flash.h"
- #include "gd32e23x.h"
- #include "relay.h"
- /* ----------------------- Defines ------------------------------------------*/
- //输入寄存器
- #define REG_INPUT_START 1000
- #define REG_INPUT_NREGS 1
- //保持寄存器
- #define REG_HOLD_START START_R_REGISTER
- #define REG_HOLD_NREGS 400
- //线圈
- #define REG_COILS_START 0
- #define REG_COILS_NREGS 1
- //开关寄存器
- #define REG_DISCRETE_START 1000
- #define REG_DISCRETE_NREGS 1
- /* ----------------------- Static variables ---------------------------------*/
- static USHORT usRegInputStart = REG_INPUT_START;
- static USHORT usRegInputBuf[REG_INPUT_NREGS];
- static USHORT usRegHoldStart = REG_HOLD_START;
- static USHORT usRegHoldBuf[REG_HOLD_NREGS];
- static USHORT usRegCoilsStart = REG_COILS_START;
- static uint8_t usRegCoilsBuf[REG_COILS_NREGS];
- static USHORT usRegDiscreteStart = REG_DISCRETE_START;
- static uint8_t usRegDiscreteBuf[REG_DISCRETE_NREGS];
- #define offsetof(type,member) ((int) &((type *)0)->member)
- /****************************************************************************
- * 名 称:eMBRegInputCB
- * 功 能:读取输入寄存器,对应功能码是 04 eMBFuncReadInputRegister
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 寄存器地址
- * usNRegs: 要读取的寄存器个数
- * 出口参数:
- * 注 意:上位机发来的 帧格式是: SlaveAddr(1 Byte)+FuncCode(1 Byte)
- * +StartAddrHiByte(1 Byte)+StartAddrLoByte(1 Byte)
- * +LenAddrHiByte(1 Byte)+LenAddrLoByte(1 Byte)+
- * +CRCAddrHiByte(1 Byte)+CRCAddrLoByte(1 Byte)
- * 3 区
- ****************************************************************************/
- eMBErrorCode
- eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- int iRegIndex;
- usAddress = usAddress - 1;
-
- if( ( usAddress >= REG_INPUT_START ) && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
- {
- iRegIndex = ( int )( usAddress - usRegInputStart );
- while( usNRegs > 0 )
- {
- *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
- *pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
- iRegIndex++;
- usNRegs--;
- }
- }
- else
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- /****************************************************************************
- * 名 称:eMBRegHoldingCB
- * 功 能:对应功能码有:06 写保持寄存器 eMBFuncWriteHoldingRegister
- * 16 写多个保持寄存器 eMBFuncWriteMultipleHoldingRegister
- * 03 读保持寄存器 eMBFuncReadHoldingRegister
- * 23 读写多个保持寄存器 eMBFuncReadWriteMultipleHoldingRegister
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 寄存器地址
- * usNRegs: 要读写的寄存器个数
- * eMode: 功能码
- * 出口参数:
- * 注 意:4 区
- ****************************************************************************/
- unsigned char WusNRegs;
- unsigned int WiRegIndex;
- extern META_DATA_S meta_data;
- extern float elec[RelaySlaveChaNum];
- static void break_2_boot_upgreade(void)
- {
- meta_data.errno = 0;
- meta_data.operation_flag = 1;
- meta_data.recent_running_time = 25;
- flash_erase_meta();
- flash_write_meta(&meta_data);
- __disable_irq();
- //__set_FAULTMASK(1);
- NVIC_SystemReset();
- }
- static void soft_reset(void)
- {
- __disable_irq();
- //__set_FAULTMASK(1);
- NVIC_SystemReset();
- }
- eMBErrorCode
- eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- int iRegIndex;
- usAddress = usAddress - 1;
- uint32_t T_cnt = 0;
- board_t *board = get_board();
- if((usAddress >= REG_HOLD_START) && ((usAddress+usNRegs) <= (REG_HOLD_START + REG_HOLD_NREGS)))
- {
- iRegIndex = (int)(usAddress - usRegHoldStart);
- WiRegIndex = iRegIndex;
- uint16_t start = 0;
- uint32_t *base = 0;
- switch(eMode)
- {
- case MB_REG_READ://读寄存器
- {
- if(usAddress == REG_HOLD_START)
- {
- base = (uint32_t*)&board->md_data.r_data.dev_info;
- }else if((usAddress >= INPUT_INFO) && usAddress <= (INPUT_INFO+INPUT_LENTH))
- {
- start = usAddress-INPUT_INFO;
- base = (uint32_t*)&(board->md_data.r_data.i_voltage);
- base += start;
-
- }else if((usAddress >= OUTPUT_INFO) && (usAddress <= (OUTPUT_INFO + OUTPUT_LENTH)))
- {
- start = usAddress-OUTPUT_INFO;
- base = (uint32_t*)&(board->md_data.r_data.output[0].voltage);
- base += start;
- }else if(usAddress == DETECTION_INFO)
- {
- base = (uint32_t*)&(board->md_data.r_data.detection);
- }else if(usAddress == IN_WANNING_INFO)
- {
- base = (uint32_t*)&(board->md_data.r_data.w_input);
- }
- else if((usAddress >= OUT_WANNING_INFO) && (usAddress <=(OUT_WANNING_INFO + OUT_WANNING_INFO_LENTH)))
- {
- start = usAddress-OUT_WANNING_INFO;
- base = (uint32_t*)&(board->md_data.r_data.wanning[0]);
- base += start;
- }else if((usAddress >= THR_INPUT) && (usAddress <= (THR_INPUT + THR_INPUT_LENTH)))
- {
- start = usAddress-THR_INPUT;
- base = (uint32_t*)&(board->md_data.rw_data.i_V_max);
- base += start;
- }else if((usAddress >= THR_OUTPUT_V_MAX) && (usAddress <= (THR_OUTPUT_V_MAX + THR_OUTPUT_V_MAX_LENTH)))
- {
- start = usAddress-THR_OUTPUT_V_MAX;
- base = (uint32_t*)&(board->md_data.rw_data.o_V_max[0]);
- base += start;
- }else if((usAddress >= THR_OUTPUT_V_MIN) && (usAddress <= (THR_OUTPUT_V_MIN + THR_OUTPUT_V_MIN_LENTH)))
- {
- start = usAddress-THR_OUTPUT_V_MIN;
- base = (uint32_t*)&(board->md_data.rw_data.o_V_min[0]);
- base += start;
- }else if((usAddress >= THR_OUTPUT_I_MAX) &&(usAddress <= (THR_OUTPUT_I_MAX + THR_OUTPUT_I_MAX_LENTH)))
- {
- start = usAddress-THR_OUTPUT_I_MAX;
- base = (uint32_t*)&(board->md_data.rw_data.o_I_max[0]);
- base += start;
- }else if((usAddress >= THR_OUTPUT_P_MAX) &&(usAddress <= THR_OUTPUT_P_MAX + THR_OUTPUT_P_MAX_LENTH))
- {
- start = usAddress-THR_OUTPUT_P_MAX;
- base = (uint32_t*)&(board->md_data.rw_data.o_P_max[0]);
- base += start;
- }else if((usAddress >= THR_OUTPUT_C_MAX) &&(usAddress <= THR_OUTPUT_C_MAX + THR_OUTPUT_C_MAX_LENTH))
- {
- start = usAddress-THR_OUTPUT_C_MAX;
- base = (uint32_t*)&(board->md_data.rw_data.o_C_max[0]);
- base += start;
- }
- else if((usAddress >= DELAY_ON) && (usAddress <=(DELAY_ON +DELAY_ON_LEN)))
- {
- start = usAddress-DELAY_ON;
- base = (uint32_t*)&(board->md_data.rw_data.delay_on[0]);
- }else if((usAddress >= DELAY_OFF) && (usAddress <= DELAY_OFF_LEN))
- {
- start = usAddress-DELAY_OFF;
- base = (uint32_t*)&(board->md_data.rw_data.delay_off[0]);
- base += start;
- }else if((usAddress >= PHR_LACK) && (usAddress <= (PHR_LACK + PHR_LACK_LENTH)))
- {
- start = usAddress-PHR_LACK;
- base = (uint32_t*)&(board->md_data.r_data.Lack_A);
- base += start;
- }else if(usAddress >= OVER_FUNC && (usAddress <=(OVER_FUNC + OVER_FUNC_LENTH)))
- {
- start = usAddress-OVER_FUNC;
- base = (uint32_t*)&(board->md_data.rw_data.over_func[0]);
- base += start;
- }else if(usAddress == DEVICE_DETAIL_INFO)
- {
- base = (uint32_t*)&(board->md_data.r_data.info);
- }else if(usAddress >= CHANNEL_CONTROL && usAddress <= (CHANNEL_CONTROL + CHANNEL_CONTROL_LENTH))
- {
- start = usAddress-CHANNEL_CONTROL;
- base = (uint32_t*)&(board->md_data.rw_data.channel_ctrl[0]);
- base += start;
- }else if(usAddress >= REALAY_CTRL && usAddress <= (REALAY_CTRL +REALAY_CTRL_LENTH))
- {
- start = usAddress-REALAY_CTRL;
- base = (uint32_t*)&(board->md_data.r_data.status[0]);
- base += start;
- }
- else
- {
- eStatus = MB_ENOREG;;;
- }
- if(base)
- {
- memcpy(&usRegHoldBuf[iRegIndex],base,usNRegs*2);
- }
- while(usNRegs > 0)
- {
- *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] >> 8);
- *pucRegBuffer++ = (uint8_t)(usRegHoldBuf[iRegIndex] & 0xFF);
- iRegIndex++;
- usNRegs--;
- }
- break;
- }
-
- case MB_REG_WRITE://写寄存器
- {
- WusNRegs = usNRegs;
- while(usNRegs > 0)
- {
- usRegHoldBuf[iRegIndex] = *pucRegBuffer++ << 8;
- usRegHoldBuf[iRegIndex] |= *pucRegBuffer++;
- iRegIndex++;
- usNRegs--;
- }
- {
- w_eeprom_data_t data = {0};
- if(usAddress >= REALAY_CTRL && (usAddress <= (REALAY_CTRL + REALAY_CTRL_LENTH)))
- {
- #if(!SUPPORT_MON)
- async_data da = {0};
- da.fire_or_zero = _FIRE;
- da.w_eep_flag = FLAG_W_EEP;
- #if (AC_3_3 || AC_3_4 || AC_3_3_MON)
- #if SUPPORT_DETECTION
- if(board->md_data.r_data.detection & 0x1)
- #endif
- {
- start = usAddress - REALAY_CTRL;
- da.method = PROMPT_CTRL;
- da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
- if(start == 9)
- {
- da.fire_or_zero = _ZERO;
- da.reg = &board->relay_zero_staging_status;
- }else
- {
- da.channel = start;
- da.reg = &board->relay_staging_staus[start];
- }
- set_relay_sta_async(&da);
- }
- #else
-
- start = usAddress - REALAY_CTRL;
- uint32_t flag = 0;
- if(board->md_data.r_data.detection != 0x3)
- {
- if(start >= PT_SUB_COUNT)
- {
- if(board->md_data.rw_data.channel_ctrl[start] != 0 || board->md_data.rw_data.channel_ctrl[start - PT_SUB_COUNT] != 0)
- {
- if(board->md_data.rw_data.channel_ctrl[start] == 0 && (board->md_data.r_data.detection & 0x2))
- {
- flag = 1;
- }
- }
- }else{
- if(board->md_data.rw_data.channel_ctrl[start] != 0 || board->md_data.rw_data.channel_ctrl[start + PT_SUB_COUNT] != 0)
- {
- if(board->md_data.rw_data.channel_ctrl[start] == 0 && (board->md_data.r_data.detection & 0x1))
- {
- flag = 1;
- }
- }
- }
- }else
- {
- if(board->md_data.rw_data.channel_ctrl[start] ==0)
- {
- flag = 1;
- }
- }
- if(flag)
- {
- da.method = PROMPT_CTRL;
- da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
- da.channel = start;
- da.reg = &board->relay_staging_staus[start];
- set_relay_sta_async(&da);
- }
- #endif
- }else if(usAddress >= COMBINE_RELAY && usAddress <=(COMBINE_RELAY + COMBINE_RELAY_LENTH))
- {
- async_data da = {0};
- da.fire_or_zero = _FIRE;
- da.w_eep_flag = FLAG_W_EEP;
- #if (AC_3_3 || AC_3_4 || AC_3_3_MON)
- #if SUPPORT_DETECTION
- if(board->md_data.r_data.detection & 0x1)
- #endif
- {
- start = (usAddress - COMBINE_RELAY) * 3; //3 pt
-
- if(board->md_data.rw_data.channel_ctrl[start ] == 0 &&
- board->md_data.rw_data.channel_ctrl[start+1] == 0 &&
- board->md_data.rw_data.channel_ctrl[start+2] == 0)
- {
- for(int i = 0; i < RelaySlaveChaNum;i += 3)
- {
- for(int j = 0; j < 3;j++)
- {
- da.channel = j + i*3;
- da.method = PROMPT_CTRL;
- da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- }
- }
- #if SUPPORT_ZERO_CRTL
- // da.fire_or_zero = _ZERO;
- // da.reg = &board->relay_zero_staging_status;
- // set_relay_sta_async(&da);
- #endif
- }
-
- }
- #else
- if(board->md_data.r_data.detection == 0x3)
- {
- start = usAddress - COMBINE_RELAY;
- // for(int i = start;i < PT_SUB_COUNT;i++)
- // {
- if(board->md_data.rw_data.channel_ctrl[start ] == 0 && board->md_data.rw_data.channel_ctrl[start + PT_SUB_COUNT] == 0)
- {
- da.channel = start;
- da.method = PROMPT_CTRL;
- da.status = (usRegHoldBuf[WiRegIndex] & 0x1);
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
-
- da.channel = start + PT_SUB_COUNT;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- }
- // }
- }
- #endif
- #endif
- }
-
- else if(usAddress == ALL_ON || usAddress == ALL_OFF)
- {
- #if (!SUPPORT_MON)
- async_data da = {0};
- da.fire_or_zero = _FIRE;
- da.w_eep_flag = FLAG_W_EEP;
- #if (AC_3_3 || AC_3_4 ||AC_3_3_MON)
- #if SUPPORT_DETECTION
- if(board->md_data.r_data.detection & 0x1)
- #endif
- {
- for(uint8_t j = 0; j < RelaySlaveChaNum;j++)
- {
- da.channel = j;
- da.method = DELAY_CTRL;
- da.reg = &board->relay_staging_staus[j];
- da.status = (usAddress == ALL_ON)? RELAY_OPEN : RELAY_CLOSE;
- set_relay_sta_async(&da);
- }
- #if SUPPORT_ZERO_CRTL
- da.fire_or_zero = _ZERO;
- da.method = DELAY_CTRL;
- da.reg = &board->relay_zero_staging_status;
- set_relay_sta_async(&da);
- #endif
- }
- #else
- da.method = DELAY_CTRL;
- da.status = (usAddress == ALL_ON) ? RELAY_OPEN : RELAY_CLOSE;
- if(board->md_data.r_data.detection != 0x3)
- {
- uint8_t sync_buff[PT_SUB_COUNT] = {0};
- if(board->md_data.r_data.detection & 0x1)
- {
- for(int i = 0;i < PT_SUB_COUNT;i++)
- {
- if(board->md_data.rw_data.channel_ctrl[i ] == 0 && board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
- {
- da.channel = i;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
-
- da.channel = i + PT_SUB_COUNT;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- sync_buff[i] = 1;
- }else
- {
- if(board->md_data.rw_data.channel_ctrl[i ] == 0)
- {
- da.channel = i;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- }
- }
- }
- }
- if(board->md_data.r_data.detection & 0x2)
- {
- for(int i = 0;i < PT_SUB_COUNT;i++)
- {
- if(board->md_data.rw_data.channel_ctrl[i ] == 0 && board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
- {
- if(!sync_buff[i])
- {
- da.channel = i;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
-
- da.channel = i + PT_SUB_COUNT;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- }
- }else
- {
- if(board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
- {
- da.channel = i + PT_SUB_COUNT;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- }
- }
- }
- }
- }else
- {
- for(int i = 0;i < PT_SUB_COUNT;i++)
- {
- if(board->md_data.rw_data.channel_ctrl[i ] == 0)
- {
- da.channel = i;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- }
-
- if(board->md_data.rw_data.channel_ctrl[i + PT_SUB_COUNT] == 0)
- {
- da.channel = i + PT_SUB_COUNT;
- da.reg = &board->relay_staging_staus[da.channel];
- set_relay_sta_async(&da);
- }
- }
- }
- #endif
- #endif
- }else if(usAddress >= THR_INPUT && usAddress <=(THR_OUTPUT_C_MAX+ THR_OUTPUT_C_MAX_LENTH))
- {
- uint16_t rom_start;
-
- if(usAddress >= THR_INPUT && (usAddress <=(THR_INPUT +THR_INPUT_LENTH)))
- {
- start = usAddress - THR_INPUT;
- base = (uint32_t*)(&board->md_data.rw_data.i_V_max) + start;
- rom_start = sizeof(uint32_t) * 18 + EEPROM_OTHER_ADDR;
- }else if(usAddress >= THR_OUTPUT_V_MAX && usAddress <=(THR_OUTPUT_V_MAX +THR_OUTPUT_V_MAX_LENTH))
- {
- start = usAddress - THR_OUTPUT_V_MAX;
- base = (uint32_t*)(&board->md_data.rw_data.o_V_max[0]) + start;
- rom_start = sizeof(uint32_t) * 23 + EEPROM_OTHER_ADDR;
- }else if(usAddress >= THR_OUTPUT_V_MIN && usAddress <=(THR_OUTPUT_V_MIN +THR_OUTPUT_V_MIN_LENTH))
- {
- start = usAddress - THR_OUTPUT_V_MIN;
- base = (uint32_t*)(&board->md_data.rw_data.o_V_min[0]) + start;
- rom_start = sizeof(uint32_t) * 31 + EEPROM_OTHER_ADDR;
- }else if(usAddress >= THR_OUTPUT_I_MAX && usAddress <=(THR_OUTPUT_I_MAX +THR_OUTPUT_I_MAX_LENTH))
- {
- start = usAddress - THR_OUTPUT_I_MAX;
- base = (uint32_t*)(&board->md_data.rw_data.o_I_max[0]) + start;
- rom_start = sizeof(uint32_t) * 39 + EEPROM_OTHER_ADDR;
- }else if(usAddress >= THR_OUTPUT_P_MAX && usAddress <=(THR_OUTPUT_P_MAX +THR_OUTPUT_P_MAX_LENTH))
- {
- start = usAddress - THR_OUTPUT_P_MAX;
- base = (uint32_t*)(&board->md_data.rw_data.o_P_max[0]) + start;
- rom_start = sizeof(uint32_t) * 47 + EEPROM_OTHER_ADDR;
- }else if(usAddress >= THR_OUTPUT_C_MAX && usAddress <=(THR_OUTPUT_C_MAX +THR_OUTPUT_C_MAX_LENTH))
- {
- start = usAddress - THR_OUTPUT_C_MAX;
- base = (uint32_t*)(&board->md_data.rw_data.o_C_max[0]) + start;
- rom_start = sizeof(uint32_t) * 55 + EEPROM_OTHER_ADDR;
- }
- if(base)
- {
- memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- for(uint8_t j = 0; j < RelaySlaveChaNum;j++)
- board->check_threshold(THRESHOLD_OUT,j);
- board->check_threshold(THRESHOLD_IN,0);
- data.lenth =WusNRegs*2;
- memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- data.start_addr = (rom_start+(start*4));
- board->q_cache.en_queue(&data);
- }
- }else if(usAddress >= DELAY_ON && usAddress <= (DELAY_OFF + DELAY_OFF_LEN))
- {
- uint16_t rom_start;
- if(usAddress >= DELAY_ON && usAddress <= (DELAY_ON + DELAY_ON_LEN))
- {
- start = usAddress - DELAY_ON;
- base = (uint32_t*)(&board->md_data.rw_data.delay_on[0]) + start;
- rom_start = EEPROM_OTHER_ADDR;
- }else if(usAddress >= DELAY_OFF && usAddress <= (DELAY_OFF + DELAY_OFF_LEN))
- {
- start = usAddress - DELAY_OFF;
- base = (uint32_t*)(&board->md_data.rw_data.delay_off[0]) + start;
- rom_start = sizeof(uint32_t) * 8 + EEPROM_OTHER_ADDR;
- }
- if(base)
- {
- memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- data.lenth =WusNRegs*2;
- memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- data.start_addr = (rom_start+(start*4));
- board->q_cache.en_queue(&data);
- #if SUPPORT_ZERO_CRTL
- board->md_data.rw_data.delay_N_on = board->md_data.rw_data.delay_on[0];
- board->md_data.rw_data.delay_N_off = board->md_data.rw_data.delay_off[0];
-
- memcpy(&data.data,&board->md_data.rw_data.delay_N_on,2*sizeof(uint32_t));
- data.lenth =2*sizeof(uint32_t);
- data.start_addr = (EEPROM_OTHER_ADDR+(16*4));
- board->q_cache.en_queue(&data);
- #endif
- }
- }else if(usAddress >= OVER_FUNC && usAddress <= (CHANNEL_CONTROL + CHANNEL_CONTROL_LENTH))
- {
- uint16_t rom_start;
- if(usAddress >= OVER_FUNC && usAddress <=(OVER_FUNC + OVER_FUNC_LENTH))
- {
- start = usAddress - OVER_FUNC;
- base = (uint32_t*)(&board->md_data.rw_data.over_func[0]) + start;
- rom_start = EEPROM_OTHER_ADDR + sizeof(uint32_t) * 63;
- }else if(usAddress >= CHANNEL_CONTROL && usAddress <=(CHANNEL_CONTROL + CHANNEL_CONTROL_LENTH))
- {
- start = usAddress - CHANNEL_CONTROL;
- base = (uint32_t*)(&board->md_data.rw_data.channel_ctrl[0]) + start;
- rom_start = EEPROM_OTHER_ADDR + sizeof(uint32_t) * 71;
-
- async_data da = {0};
- da.fire_or_zero = _FIRE;
- da.w_eep_flag = FLAG_W_EEP;
- da.method = PROMPT_CTRL;
- da.channel = start;
- da.reg = &board->relay_staging_staus[da.channel];
- if(usRegHoldBuf[WiRegIndex] == 1)
- {
- da.status = RELAY_OPEN;
- set_relay_sta_async(&da);
- }else if(usRegHoldBuf[WiRegIndex] == 2)
- {
- da.status = RELAY_CLOSE;
- set_relay_sta_async(&da);
- }
-
-
- }else if(usAddress >= CLEAR_CONSUMER && (usAddress <=(CLEAR_CONSUMER + CLEAR_CONSUMER_LENTH)))
- {
- start = usAddress - CLEAR_CONSUMER;
- elec[start] = 0.0;
- board->ele_restore[start] = 0;
-
- }else if(usAddress == CLEAR_ALL_CONSUMER)
- {
- for(int i = 0; i < RelaySlaveChaNum;i++)
- {
- elec[i] = 0.0;
- board->ele_restore[i] = 0;
- }
- }else if(usAddress == UPGRADE)
- {
-
- }else
- {
-
- }
-
- if(base)
- {
- memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- data.lenth =WusNRegs*2;
- memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- data.start_addr = (rom_start+(start*4));
- board->q_cache.en_queue(&data);
- }
- }else if(usAddress >= SET_V_K_VALUE && usAddress <= (SET_V_K_VALUE + SET_V_K_VALUE_LEN))
- {
- uint16_t rom_start = 0;
- start = usAddress - SET_V_K_VALUE;
- base = (uint32_t*)(&board->v_k[0]) + start;
- memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- rom_start = EEPROM_SET_V_K_ADDR;
- data.lenth =WusNRegs*2;
- data.start_addr = (rom_start+(start*4));
- memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- board->q_cache.en_queue(&data);
-
- }else if(usAddress >= SET_I_K_VALUE && usAddress <= (SET_I_K_VALUE + SET_I_K_VALUE_LEN))
- {
- uint16_t rom_start = 0;
- start = usAddress - SET_I_K_VALUE;
- base = (uint32_t*)(&board->i_k[0]) + start;
- memcpy(base,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- rom_start = EEPROM_SET_K_I_ADDR;
- data.lenth =WusNRegs*2;
- data.start_addr = (rom_start+(start*4));
- memcpy(&data.data,&usRegHoldBuf[WiRegIndex],2*WusNRegs);
- board->q_cache.en_queue(&data);
- }
- }
- break;
- }
- }
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- /****************************************************************************
- * 名 称:eMBRegCoilsCB
- * 功 能:对应功能码有:01 读线圈 eMBFuncReadCoils
- * 05 写线圈 eMBFuncWriteCoil
- * 15 写多个线圈 eMBFuncWriteMultipleCoils
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 线圈地址
- * usNCoils: 要读写的线圈个数
- * eMode: 功能码
- * 出口参数:
- * 注 意:如继电器
- * 0 区
- ****************************************************************************/
- eMBErrorCode
- eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- USHORT iRegIndex;
- USHORT usCoilGroups = ((usNCoils - 1) / 8 + 1);
- UCHAR ucStatus = 0;
- UCHAR ucBits = 0;
- UCHAR ucDisp = 0;
- usAddress = usAddress - 1;
-
- if((usAddress >= REG_COILS_START) && ((usAddress + usNCoils) <= (REG_COILS_START + REG_COILS_NREGS)))
- {
- iRegIndex = (int)(usAddress - usRegCoilsStart);
- switch(eMode)
- {
- case MB_REG_READ://读线圈
- while(usCoilGroups--)
- {
- ucDisp = 0;
- ucBits = 8;
- while((usNCoils--) != 0 && (ucBits--) != 0)
- {
- ucStatus |= (usRegCoilsBuf[iRegIndex++] << (ucDisp++));
- }
- *pucRegBuffer++ = ucStatus;
- }
- break;
- case MB_REG_WRITE://写线圈
- while(usCoilGroups--)
- {
- ucStatus = *pucRegBuffer++;
- ucBits = 8;
- while((usNCoils--) != 0 && (ucBits--) != 0)
- {
- usRegCoilsBuf[iRegIndex++] = ucStatus & 0X01;
- ucStatus >>= 1;
- }
- }
- }
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
- /****************************************************************************
- * 名 称:eMBRegDiscreteCB
- * 功 能:读取离散寄存器,对应功能码有:02 读离散寄存器 eMBFuncReadDiscreteInputs
- * 入口参数:pucRegBuffer: 数据缓存区,用于响应主机
- * usAddress: 寄存器地址
- * usNDiscrete: 要读取的寄存器个数
- * 出口参数:
- * 注 意:1 区
- ****************************************************************************/
- eMBErrorCode
- eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
- {
- eMBErrorCode eStatus = MB_ENOERR;
- USHORT iRegIndex;
- USHORT usDiscreteGroups = ((usNDiscrete - 1) / 8 + 1);
- UCHAR ucStatus = 0;
- UCHAR ucBits = 0;
- UCHAR ucDisp = 0;
- usAddress = usAddress - 1;
-
- if((usAddress >= REG_DISCRETE_START) && ((usAddress + usNDiscrete) <= (REG_DISCRETE_START + REG_DISCRETE_NREGS)))
- {
- iRegIndex = (int)(usAddress - usRegDiscreteStart);
- while(usDiscreteGroups--)
- {
- ucDisp = 0;
- ucBits = 8;
- while((usNDiscrete--) != 0 && (ucBits--) != 0)
- {
- if(usRegDiscreteBuf[iRegIndex])
- {
- ucStatus |= (1 << ucDisp);
- }
- ucDisp++;
- }
- *pucRegBuffer++ = ucStatus;
- }
- }
- else//错误
- {
- eStatus = MB_ENOREG;
- }
- return eStatus;
- }
|