debug tty
This commit is contained in:
@@ -2,7 +2,9 @@
|
||||
#include <rtthread.h>
|
||||
|
||||
#include "mb.h"
|
||||
#include "chrg_mb.h"
|
||||
#include "mbutils.h"
|
||||
#include "mbport.h"
|
||||
|
||||
#include "chrg_comm.h"
|
||||
|
||||
|
||||
@@ -10,39 +12,229 @@
|
||||
#define DBG_LEVEL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
extern rt_uint16_t usSRegHoldBuf[S_REG_HOLDING_NREGS];
|
||||
//Slave mode:DiscreteInputs variables
|
||||
rt_uint16_t usSDiscInStart = S_DISCRETE_INPUT_START;
|
||||
rt_uint8_t ucSDiscInBuf[(S_DISCRETE_INPUT_NDISCRETES+7)/8];
|
||||
|
||||
//Slave mode:Coils variables
|
||||
rt_uint16_t usSCoilStart = S_COIL_START;
|
||||
rt_uint8_t ucSCoilBuf[(S_COIL_NCOILS+7)/8];
|
||||
|
||||
//Slave mode:InputRegister variables
|
||||
rt_uint16_t usSRegInStart = S_REG_INPUT_START;
|
||||
rt_uint16_t usSRegInBuf[S_REG_INPUT_NREGS];
|
||||
|
||||
//Slave mode:HoldingRegister variables
|
||||
rt_uint16_t usSRegHoldStart = S_REG_HOLDING_START;
|
||||
rt_uint16_t usSRegHoldBuf[S_REG_HOLDING_NREGS];
|
||||
|
||||
/**
|
||||
* Modbus slave input register callback function.
|
||||
*
|
||||
* @param pucRegBuffer input register buffer
|
||||
* @param usAddress input register address
|
||||
* @param usNRegs input register number
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
eMBErrorCode eMBRegInputCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_uint16_t usNRegs)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
rt_uint16_t iRegIndex;
|
||||
rt_uint16_t *pusRegInputBuf = usSRegInBuf;
|
||||
rt_uint16_t REG_INPUT_START = S_REG_INPUT_START;
|
||||
rt_uint16_t REG_INPUT_NREGS = S_REG_INPUT_NREGS;
|
||||
rt_uint16_t usRegInStart = usSRegInStart;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= REG_INPUT_START)
|
||||
&& (usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS)) {
|
||||
iRegIndex = usAddress - usRegInStart;
|
||||
while (usNRegs > 0) {
|
||||
*pucRegBuffer++ = (rt_uint8_t)(pusRegInputBuf[iRegIndex] >> 8);
|
||||
*pucRegBuffer++ = (rt_uint8_t)(pusRegInputBuf[iRegIndex] & 0xFF);
|
||||
iRegIndex++;
|
||||
usNRegs--;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_ENOREG;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modbus slave holding register callback function.
|
||||
*
|
||||
* @param pucRegBuffer holding register buffer
|
||||
* @param usAddress holding register address
|
||||
* @param usNRegs holding register number
|
||||
* @param eMode read or write
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
eMBErrorCode eMBRegHoldingCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usNRegs, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
rt_uint16_t iRegIndex;
|
||||
rt_uint16_t *pusRegHoldingBuf = usSRegHoldBuf;
|
||||
rt_uint16_t REG_HOLDING_START = S_REG_HOLDING_START;
|
||||
rt_uint16_t REG_HOLDING_NREGS = S_REG_HOLDING_NREGS;
|
||||
rt_uint16_t usRegHoldStart = usSRegHoldStart;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= REG_HOLDING_START)
|
||||
&& (usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS)) {
|
||||
iRegIndex = usAddress - usRegHoldStart;
|
||||
switch (eMode) {
|
||||
/* read current register values from the protocol stack. */
|
||||
case MB_REG_READ:
|
||||
while (usNRegs > 0) {
|
||||
*pucRegBuffer++ = (rt_uint8_t)(pusRegHoldingBuf[iRegIndex] >> 8);
|
||||
*pucRegBuffer++ = (rt_uint8_t)(pusRegHoldingBuf[iRegIndex] & 0xFF);
|
||||
iRegIndex++;
|
||||
usNRegs--;
|
||||
}
|
||||
break;
|
||||
|
||||
/* write current register values with new values from the protocol stack. */
|
||||
case MB_REG_WRITE:
|
||||
while (usNRegs > 0) {
|
||||
pusRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
|
||||
pusRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
|
||||
iRegIndex++;
|
||||
usNRegs--;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_ENOREG;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modbus slave coils callback function.
|
||||
*
|
||||
* @param pucRegBuffer coils buffer
|
||||
* @param usAddress coils address
|
||||
* @param usNCoils coils number
|
||||
* @param eMode read or write
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
eMBErrorCode eMBRegCoilsCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
|
||||
rt_uint16_t usNCoils, eMBRegisterMode eMode)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
rt_uint16_t iRegIndex , iRegBitIndex , iNReg;
|
||||
rt_uint8_t *pucCoilBuf = ucSCoilBuf;
|
||||
rt_uint16_t COIL_START = S_COIL_START;
|
||||
rt_uint16_t COIL_NCOILS = S_COIL_NCOILS;
|
||||
rt_uint16_t usCoilStart = usSCoilStart;
|
||||
|
||||
iNReg = usNCoils / 8 + 1;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= COIL_START) &&
|
||||
(usAddress + usNCoils <= COIL_START + COIL_NCOILS)) {
|
||||
iRegIndex = (rt_uint16_t)(usAddress - usCoilStart) / 8;
|
||||
iRegBitIndex = (rt_uint16_t)(usAddress - usCoilStart) % 8;
|
||||
switch (eMode) {
|
||||
/* read current coil values from the protocol stack. */
|
||||
case MB_REG_READ:
|
||||
while (iNReg > 0) {
|
||||
*pucRegBuffer++ = xMBUtilGetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, 8);
|
||||
iNReg--;
|
||||
}
|
||||
pucRegBuffer--;
|
||||
/* last coils */
|
||||
usNCoils = usNCoils % 8;
|
||||
/* filling zero to high bit */
|
||||
*pucRegBuffer = *pucRegBuffer << (8 - usNCoils);
|
||||
*pucRegBuffer = *pucRegBuffer >> (8 - usNCoils);
|
||||
break;
|
||||
|
||||
/* write current coil values with new values from the protocol stack. */
|
||||
case MB_REG_WRITE:
|
||||
while (iNReg > 1) {
|
||||
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, 8, *pucRegBuffer++);
|
||||
iNReg--;
|
||||
}
|
||||
/* last coils */
|
||||
usNCoils = usNCoils % 8;
|
||||
/* xMBUtilSetBits has bug when ucNBits is zero */
|
||||
if (usNCoils != 0) {
|
||||
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils, *pucRegBuffer++);
|
||||
}
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
eStatus = MB_ENOREG;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modbus slave discrete callback function.
|
||||
*
|
||||
* @param pucRegBuffer discrete buffer
|
||||
* @param usAddress discrete address
|
||||
* @param usNDiscrete discrete number
|
||||
*
|
||||
* @return result
|
||||
*/
|
||||
eMBErrorCode eMBRegDiscreteCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_uint16_t usNDiscrete)
|
||||
{
|
||||
eMBErrorCode eStatus = MB_ENOERR;
|
||||
rt_uint16_t iRegIndex , iRegBitIndex , iNReg;
|
||||
rt_uint8_t *pucDiscreteInputBuf = ucSDiscInBuf;
|
||||
rt_uint16_t DISCRETE_INPUT_START = S_DISCRETE_INPUT_START;
|
||||
rt_uint16_t DISCRETE_INPUT_NDISCRETES = S_DISCRETE_INPUT_NDISCRETES;
|
||||
rt_uint16_t usDiscreteInputStart = usSDiscInStart;
|
||||
iNReg = usNDiscrete / 8 + 1;
|
||||
|
||||
/* it already plus one in modbus function method. */
|
||||
usAddress--;
|
||||
|
||||
if ((usAddress >= DISCRETE_INPUT_START)
|
||||
&& ((usAddress + usNDiscrete) <= (DISCRETE_INPUT_START + DISCRETE_INPUT_NDISCRETES))) {
|
||||
iRegIndex = (rt_uint16_t) (usAddress - usDiscreteInputStart) / 8;
|
||||
iRegBitIndex = (rt_uint16_t) (usAddress - usDiscreteInputStart) % 8;
|
||||
|
||||
while (iNReg > 0) {
|
||||
*pucRegBuffer++ = xMBUtilGetBits(&pucDiscreteInputBuf[iRegIndex++], iRegBitIndex, 8);
|
||||
iNReg--;
|
||||
}
|
||||
pucRegBuffer--;
|
||||
/* last discrete */
|
||||
usNDiscrete = usNDiscrete % 8;
|
||||
/* filling zero to high bit */
|
||||
*pucRegBuffer = *pucRegBuffer << (8 - usNDiscrete);
|
||||
*pucRegBuffer = *pucRegBuffer >> (8 - usNDiscrete);
|
||||
} else {
|
||||
eStatus = MB_ENOREG;
|
||||
}
|
||||
|
||||
return eStatus;
|
||||
}
|
||||
|
||||
struct chrg_comm_t chrgcomm = {
|
||||
.tid_poll = RT_NULL,
|
||||
.tid_send = RT_NULL,
|
||||
.name_poll = THR_NAME_POLL,
|
||||
.name_send = THR_NAME_SEND,
|
||||
.tid = RT_NULL,
|
||||
.name = THR_NAME_POLL,
|
||||
.addr = MODBUS_ADDRESS_DEFAULT,
|
||||
|
||||
};
|
||||
|
||||
static void chrg_comm_send (void *pdata)
|
||||
{
|
||||
struct chrg_comm_t *pCOMM = (struct chrg_comm_t *)pdata;
|
||||
if (RT_NULL == pCOMM) {
|
||||
LOG_E("send null.");
|
||||
return ;
|
||||
}
|
||||
|
||||
rt_uint16_t *usRegHoldingBuf;
|
||||
usRegHoldingBuf = usSRegHoldBuf;
|
||||
rt_base_t level;
|
||||
|
||||
while (1) {
|
||||
level = rt_hw_interrupt_disable();
|
||||
usRegHoldingBuf[3] = (rt_uint16_t)(rt_tick_get() / 100);
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void chrg_comm_poll (void *pdata)
|
||||
{
|
||||
struct chrg_comm_t *pCOMM = (struct chrg_comm_t *)pdata;
|
||||
@@ -57,7 +249,7 @@ static void chrg_comm_poll (void *pdata)
|
||||
|
||||
while (1) {
|
||||
eMBPoll();
|
||||
rt_thread_mdelay(200);
|
||||
rt_thread_mdelay(100);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -66,21 +258,12 @@ static int chrg_comm_init (void)
|
||||
{
|
||||
struct chrg_comm_t *pCOMM = &chrgcomm;
|
||||
|
||||
pCOMM->tid_poll = rt_thread_create(pCOMM->name_poll, chrg_comm_poll,
|
||||
pCOMM->tid = rt_thread_create(pCOMM->name, chrg_comm_poll,
|
||||
pCOMM, 1024, RT_MAIN_THREAD_PRIORITY-1, 10);
|
||||
if (RT_NULL != pCOMM->tid_poll) {
|
||||
rt_thread_startup(pCOMM->tid_poll);
|
||||
if (RT_NULL != pCOMM->tid) {
|
||||
rt_thread_startup(pCOMM->tid);
|
||||
} else {
|
||||
LOG_E("create thread '%s' failed.", pCOMM->name_poll);
|
||||
return -1;
|
||||
}
|
||||
|
||||
pCOMM->tid_send = rt_thread_create(pCOMM->name_send, chrg_comm_send,
|
||||
pCOMM, 1024, RT_MAIN_THREAD_PRIORITY-2, 10);
|
||||
if (RT_NULL != pCOMM->tid_send) {
|
||||
rt_thread_startup(pCOMM->tid_send);
|
||||
} else {
|
||||
LOG_E("create thread '%s' failed.", pCOMM->name_send);
|
||||
LOG_E("create thread '%s' failed.", pCOMM->name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -90,5 +273,18 @@ static int chrg_comm_init (void)
|
||||
|
||||
INIT_APP_EXPORT(chrg_comm_init);
|
||||
|
||||
int rs485_test_send (int argc, char **argv)
|
||||
{
|
||||
char i = 0;
|
||||
|
||||
for (i = 0; i < 16; i++) {
|
||||
xMBPortSerialPutByte(i);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
MSH_CMD_EXPORT(rs485_test_send, test rs485 send);
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user