add roll thread

This commit is contained in:
wmano
2025-09-28 23:00:52 +08:00
parent c604a73d69
commit 037fdb64eb
14 changed files with 427 additions and 270 deletions
+58 -97
View File
@@ -11,10 +11,10 @@
#include <rtdbg.h>
rt_uint16_t usSDiscInStart = S_DISCRETE_INPUT_START;
rt_uint8_t ucSDiscInBuf[(S_DISCRETE_INPUT_NDISCRETES+7)/8];
rt_uint8_t ucSDiscInBuf[(S_DISCRETE_INPUT_NDISCRETES+BITS_UCHAR-1)/BITS_UCHAR];
rt_uint16_t usSCoilStart = S_COIL_START;
rt_uint8_t ucSCoilBuf[(S_COIL_NCOILS+7)/8];
rt_uint8_t ucSCoilBuf[(S_COIL_NCOILS+BITS_UCHAR-1)/BITS_UCHAR];
rt_uint16_t usSRegInStart = S_REG_INPUT_START;
rt_uint16_t usSRegInBuf[S_REG_INPUT_NREGS];
@@ -22,7 +22,7 @@ rt_uint16_t usSRegInBuf[S_REG_INPUT_NREGS];
rt_uint16_t usSRegHoldStart = S_REG_HOLDING_START;
rt_uint16_t usSRegHoldBuf[S_REG_HOLDING_NREGS];
#define BITS_UCHAR 8U
struct chrg_comm_t chrgcomm = {
.name = THR_NAME_COMM,
@@ -58,16 +58,16 @@ struct chrg_comm_t chrgcomm = {
* ucBits[2] = {0, 0};
*
* // Set bit 4 to 1 (read: set 1 bit starting at bit offset 4 to value 1)
* xMBUtilSetBits( ucBits, 4, 1, 1 );
* mb_set_bits( ucBits, 4, 1, 1 );
*
* // Set bit 7 to 1 and bit 8 to 0.
* xMBUtilSetBits( ucBits, 7, 2, 0x01 );
* mb_set_bits( ucBits, 7, 2, 0x01 );
*
* // Set bits 8 - 11 to 0x05 and bits 12 - 15 to 0x0A;
* xMBUtilSetBits( ucBits, 8, 8, 0x5A);
* mb_set_bits( ucBits, 8, 8, 0x5A);
* \endcode
*/
void xMBUtilSetBits (rt_uint8_t *ucByteBuf, rt_uint16_t usBitOffset,
void mb_set_bits (rt_uint8_t *ucByteBuf, rt_uint16_t usBitOffset,
rt_uint8_t ucNBits, rt_uint8_t ucValue)
{
rt_uint16_t usWordBuf;
@@ -76,7 +76,7 @@ void xMBUtilSetBits (rt_uint8_t *ucByteBuf, rt_uint16_t usBitOffset,
rt_uint16_t usNPreBits;
rt_uint16_t usValue = ucValue;
RT_ASSERT(ucNBits <= 8);
RT_ASSERT(ucNBits <= BITS_UCHAR);
RT_ASSERT((size_t)BITS_UCHAR == sizeof(rt_uint8_t) * 8);
/* Calculate byte offset for first byte containing the bit values starting
@@ -121,10 +121,10 @@ void xMBUtilSetBits (rt_uint8_t *ucByteBuf, rt_uint16_t usBitOffset,
* rt_uint8_t ucResult;
*
* // Extract the bits 3 - 10.
* ucResult = xMBUtilGetBits( ucBits, 3, 8 );
* ucResult = mb_get_bits( ucBits, 3, 8 );
* \endcode
*/
rt_uint8_t xMBUtilGetBits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset, rt_uint8_t ucNBits)
rt_uint8_t mb_get_bits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset, rt_uint8_t ucNBits)
{
rt_uint16_t usWordBuf;
rt_uint16_t usMask;
@@ -154,7 +154,7 @@ rt_uint8_t xMBUtilGetBits (rt_uint8_t * ucByteBuf, rt_uint16_t usBitOffset, rt_u
return (rt_uint8_t)usWordBuf;
}
static eMBException prveMBError2Exception (eMBErrorCode eErrorCode)
static eMBException mb_error (eMBErrorCode eErrorCode)
{
eMBException eStatus;
@@ -180,17 +180,7 @@ static eMBException prveMBError2Exception (eMBErrorCode eErrorCode)
}
/**
* 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 mb_reg_input_cb (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_uint16_t usNRegs)
{
eMBErrorCode eStatus = MB_ENOERR;
rt_uint16_t iRegIndex;
@@ -203,7 +193,7 @@ eMBErrorCode eMBRegInputCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_
&& (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] >> BITS_UCHAR);
*pucRegBuffer++ = (rt_uint8_t)(pusRegInputBuf[iRegIndex] & 0xFF);
iRegIndex++;
usNRegs--;
@@ -215,17 +205,7 @@ eMBErrorCode eMBRegInputCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_
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 *req, rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
eMBErrorCode mb_reg_holding_cb (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
rt_uint16_t usNRegs, eMBRegisterMode eMode)
{
eMBErrorCode eStatus = MB_ENOERR;
@@ -242,7 +222,7 @@ eMBErrorCode eMBRegHoldingCB (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint
/* 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] >> BITS_UCHAR);
*pucRegBuffer++ = (rt_uint8_t)(pusRegHoldingBuf[iRegIndex] & 0xFF);
iRegIndex++;
usNRegs--;
@@ -252,7 +232,7 @@ eMBErrorCode eMBRegHoldingCB (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint
/* write current register values with new values from the protocol stack. */
case MB_REG_WRITE:
while (usNRegs > 0) {
pusRegHoldingBuf[iRegIndex] = *req++ << 8;
pusRegHoldingBuf[iRegIndex] = *req++ << BITS_UCHAR;
pusRegHoldingBuf[iRegIndex] |= *req++;
iRegIndex++;
usNRegs--;
@@ -266,17 +246,7 @@ eMBErrorCode eMBRegHoldingCB (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint
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 *req, rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
eMBErrorCode mb_reg_coils_cb (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
rt_uint16_t usNCoils, eMBRegisterMode eMode)
{
eMBErrorCode eStatus = MB_ENOERR;
@@ -286,37 +256,37 @@ eMBErrorCode eMBRegCoilsCB (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint16
rt_uint16_t COIL_NCOILS = S_COIL_NCOILS;
rt_uint16_t usCoilStart = usSCoilStart;
iNReg = (usNCoils+7)/8;
iNReg = (usNCoils+BITS_UCHAR-1)/BITS_UCHAR;
if ((usAddress >= COIL_START) && (usAddress + usNCoils <= COIL_START + COIL_NCOILS)) {
iRegIndex = (rt_uint16_t)(usAddress - usCoilStart) / 8;
iRegBitIndex = (rt_uint16_t)(usAddress - usCoilStart) % 8;
iRegIndex = (rt_uint16_t)(usAddress - usCoilStart) / BITS_UCHAR;
iRegBitIndex = (rt_uint16_t)(usAddress - usCoilStart) % BITS_UCHAR;
switch (eMode) {
/* read current coil values from the protocol stack. */
case MB_REG_READ:
while (iNReg > 0) {
*pucRegBuffer++ = xMBUtilGetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, 8);
*pucRegBuffer++ = mb_get_bits(&pucCoilBuf[iRegIndex++], iRegBitIndex, BITS_UCHAR);
iNReg--;
}
pucRegBuffer--;
/* last coils */
usNCoils = usNCoils % 8;
usNCoils = usNCoils % BITS_UCHAR;
/* filling zero to high bit */
*pucRegBuffer = *pucRegBuffer << (8 - usNCoils);
*pucRegBuffer = *pucRegBuffer >> (8 - usNCoils);
*pucRegBuffer = *pucRegBuffer << (BITS_UCHAR - usNCoils);
*pucRegBuffer = *pucRegBuffer >> (BITS_UCHAR - 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, *req++);
mb_set_bits(&pucCoilBuf[iRegIndex++], iRegBitIndex, BITS_UCHAR, *req++);
iNReg--;
}
/* last coils */
usNCoils = usNCoils % 8;
/* xMBUtilSetBits has bug when ucNBits is zero */
usNCoils = usNCoils % BITS_UCHAR;
/* mb_set_bits has bug when ucNBits is zero */
if (usNCoils != 0) {
xMBUtilSetBits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils, *req++);
mb_set_bits(&pucCoilBuf[iRegIndex++], iRegBitIndex, usNCoils, *req++);
}
break;
}
@@ -327,16 +297,7 @@ eMBErrorCode eMBRegCoilsCB (rt_uint8_t *req, rt_uint8_t *pucRegBuffer, rt_uint16
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 mb_reg_discrete_cb (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress, rt_uint16_t usNDiscrete)
{
eMBErrorCode eStatus = MB_ENOERR;
rt_uint16_t iRegIndex , iRegBitIndex , iNReg;
@@ -344,23 +305,23 @@ eMBErrorCode eMBRegDiscreteCB (rt_uint8_t *pucRegBuffer, rt_uint16_t usAddress,
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;
iNReg = (usNDiscrete+BITS_UCHAR-1) / BITS_UCHAR;
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;
iRegIndex = (rt_uint16_t) (usAddress - usDiscreteInputStart) / BITS_UCHAR;
iRegBitIndex = (rt_uint16_t) (usAddress - usDiscreteInputStart) % BITS_UCHAR;
while (iNReg > 0) {
*pucRegBuffer++ = xMBUtilGetBits(&pucDiscreteInputBuf[iRegIndex++], iRegBitIndex, 8);
*pucRegBuffer++ = mb_get_bits(&pucDiscreteInputBuf[iRegIndex++], iRegBitIndex, BITS_UCHAR);
iNReg--;
}
pucRegBuffer--;
/* last discrete */
usNDiscrete = usNDiscrete % 8;
usNDiscrete = usNDiscrete % BITS_UCHAR;
/* filling zero to high bit */
*pucRegBuffer = *pucRegBuffer << (8 - usNDiscrete);
*pucRegBuffer = *pucRegBuffer >> (8 - usNDiscrete);
*pucRegBuffer = *pucRegBuffer << (BITS_UCHAR - usNDiscrete);
*pucRegBuffer = *pucRegBuffer >> (BITS_UCHAR - usNDiscrete);
} else {
eStatus = MB_ENOREG;
}
@@ -387,13 +348,13 @@ eMBException funcReadCoils (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
return MB_EX_ILLEGAL_DATA_VALUE;
}
ucNBytes = (cntCoils+7)/8;
ucNBytes = (cntCoils+BITS_UCHAR-1)/BITS_UCHAR;
resFrame[1] = MB_READ_COILS;
resFrame[2] = ucNBytes;
eRegStatus = eMBRegCoilsCB(RT_NULL, &resFrame[3], regAddress, cntCoils, MB_REG_READ);
eRegStatus = mb_reg_coils_cb(RT_NULL, &resFrame[3], regAddress, cntCoils, MB_REG_READ);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
} else {
*resLen = 3+ucNBytes;
}
@@ -420,13 +381,13 @@ eMBException funcReadDiscreteInputs (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
return MB_EX_ILLEGAL_DATA_VALUE;
}
ucNBytes = (cntDisCrete+7)/8;
ucNBytes = (cntDisCrete+BITS_UCHAR-1)/BITS_UCHAR;
resFrame[1] = MB_READ_DISCRETE_INPUTS;
resFrame[2] = ucNBytes;
eRegStatus = eMBRegDiscreteCB(&resFrame[3], regAddress, cntDisCrete);
eRegStatus = mb_reg_discrete_cb(&resFrame[3], regAddress, cntDisCrete);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
} else {
*resLen = 3+ucNBytes;
}
@@ -456,9 +417,9 @@ eMBException funcReadHoldingRegister (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
resFrame[1] = MB_READ_HOLDING_REGISTER;
resFrame[2] = cntReg * 2;
eRegStatus = eMBRegHoldingCB(RT_NULL, &resFrame[3], regAddress, cntReg, MB_REG_READ);
eRegStatus = mb_reg_holding_cb(RT_NULL, &resFrame[3], regAddress, cntReg, MB_REG_READ);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
} else {
*resLen = 3+cntReg * 2;
}
@@ -488,9 +449,9 @@ eMBException funcReadInputRegister (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
resFrame[1] = MB_READ_INPUT_REGISTER;
resFrame[2] = cntReg * 2;
eRegStatus = eMBRegDiscreteCB(&resFrame[3], regAddress, cntReg);
eRegStatus = mb_reg_discrete_cb(&resFrame[3], regAddress, cntReg);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
} else {
*resLen = 3+cntReg*2;
}
@@ -527,9 +488,9 @@ eMBException funcWriteCoil (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
memcpy(resFrame, reqFrame, reqLen);
*resLen = reqLen-2;
eRegStatus = eMBRegCoilsCB(RT_NULL, ucBuf, regAddress, 1, MB_REG_WRITE);
eRegStatus = mb_reg_coils_cb(RT_NULL, ucBuf, regAddress, 1, MB_REG_WRITE);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
}
return eStatus;
@@ -552,9 +513,9 @@ eMBException funcWriteHoldingRegister (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
memcpy(resFrame, reqFrame, reqLen);
*resLen = reqLen-2;
eRegStatus = eMBRegHoldingCB(&reqFrame[4], &resFrame[3], regAddress, 1, MB_REG_WRITE);
eRegStatus = mb_reg_holding_cb(&reqFrame[4], &resFrame[3], regAddress, 1, MB_REG_WRITE);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
}
return eStatus;
@@ -577,7 +538,7 @@ eMBException funcWriteMultipleCoils (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
regAddress = u8v_to_u16(&reqFrame[2]);
cntCoils = u8v_to_u16(&reqFrame[4]);
ucByteCount = reqFrame[6];
ucByteCountVerify = (cntCoils+7)/8;
ucByteCountVerify = (cntCoils+BITS_UCHAR-1)/BITS_UCHAR;
if ((0 == cntCoils)
||(cntCoils > MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX)
@@ -588,9 +549,9 @@ eMBException funcWriteMultipleCoils (rt_uint8_t *reqFrame, rt_uint16_t reqLen,
memcpy(resFrame, reqFrame, reqLen);
*resLen = 6;
eRegStatus = eMBRegCoilsCB(reqFrame, &resFrame[3], regAddress, cntCoils, MB_REG_WRITE);
eRegStatus = mb_reg_coils_cb(reqFrame, &resFrame[3], regAddress, cntCoils, MB_REG_WRITE);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
}
return eStatus;
@@ -622,9 +583,9 @@ eMBException funcWriteMultipleHoldingRegister (rt_uint8_t *reqFrame, rt_uint16_t
memcpy(resFrame, reqFrame, reqLen);
*resLen = 6;
eRegStatus = eMBRegHoldingCB(reqFrame, &resFrame[3], regAddress, cntReg, MB_REG_WRITE);
eRegStatus = mb_reg_holding_cb(reqFrame, &resFrame[3], regAddress, cntReg, MB_REG_WRITE);
if (eRegStatus != MB_ENOERR) {
eStatus = prveMBError2Exception(eRegStatus);
eStatus = mb_error(eRegStatus);
}
return eStatus;
@@ -654,8 +615,8 @@ static int chrg_comm_parse_msg (struct chrg_comm_t *pCOMM)
return -2;
}
if (0 != usMBCRC16(pCOMM->rx_buf, pCOMM->rx_len)) {
rt_kprintf("crc %04X ", usMBCRC16(pCOMM->rx_buf, pCOMM->rx_len-2));
if (0 != mb_crc16(pCOMM->rx_buf, pCOMM->rx_len)) {
rt_kprintf("crc %04X ", mb_crc16(pCOMM->rx_buf, pCOMM->rx_len-2));
return -3;
}
@@ -692,7 +653,7 @@ static int chrg_comm_parse_msg (struct chrg_comm_t *pCOMM)
pCOMM->tx_len = 3;
}
crc = usMBCRC16(pCOMM->tx_buf, pCOMM->tx_len);
crc = mb_crc16(pCOMM->tx_buf, pCOMM->tx_len);
u16_to_u8v(crc, &pCOMM->tx_buf[pCOMM->tx_len]);
pCOMM->tx_len += 2;
+2 -2
View File
@@ -5,11 +5,11 @@
#include "chrg_tty.h"
#define THR_NAME_POLL "thr.poll"
#define THR_NAME_SEND "thr.send"
#define MODBUS_ADDRESS_DEFAULT 0x01
#define BITS_UCHAR 8U
#define S_DISCRETE_INPUT_START 0
#define S_DISCRETE_INPUT_NDISCRETES 16
#define S_COIL_START 0
+2 -2
View File
@@ -2,7 +2,7 @@
#include <rtthread.h>
#include "chrg_north.h"
#include "chrg_roll.h"
#include "chrg_utils.h"
#define LOG_TAG "chrg.nor"
@@ -59,7 +59,7 @@ static void chrg_north_thread_entry (void *data)
//rt_kprintf("%s\r\n", (char *)pNOR->rx_buf);
//chrg_tty_send(pTTY, pNOR->rx_buf, len);
chrg_roll_evt_send();
}
@@ -20,6 +20,7 @@ struct chrg_north_t {
rt_uint16_t rx_len;
rt_uint8_t rx_buf[SIZE_BUF_TTY];
eIDX_CH idx; // 当前通路
struct chrg_switch_t sw[TOTAL_CHS];
};
+144
View File
@@ -0,0 +1,144 @@
#include <rtthread.h>
#include "chrg_north.h"
#include "chrg_south.h"
#include "chrg_sink.h"
#include "chrg_source.h"
#include "chrg_roll.h"
#define LOG_TAG "chrg.roll"
#include <rtdbg.h>
struct chrg_roll_t chrgroll = {
.name = THR_NAME_ROLL,
.tid = RT_NULL,
.idx = 0,
.en_nor = {1, 1, 1, 1},
.en_sou = {1, 1, 1, 1, 0, 0, 0, 0},
};
rt_uint8_t chrg_roll_get_idx (void)
{
rt_uint8_t idx = 0;
struct chrg_roll_t *pROLL = &chrgroll;
rt_mutex_take(pROLL->mutex, RT_WAITING_FOREVER);
idx = pROLL->idx;
rt_mutex_release(pROLL->mutex);
return idx;
}
void chrg_roll_evt_send (void)
{
struct chrg_roll_t *pROLL = &chrgroll;
if (RT_NULL != pROLL->evt) {
rt_event_send(pROLL->evt, ROLL_EVT_RECVED);
}
}
static void chrg_roll_thread_entry (void *data)
{
struct chrg_roll_t *pROLL = (struct chrg_roll_t *)data;
if (RT_NULL == pROLL) {
return ;
}
rt_uint8_t idx = 0;
rt_err_t ret = RT_EOK;
rt_uint32_t recved = 0;
struct chrg_north_t *pNOR = &chrgnorth;
struct chrg_south_t *pSOU = &chrgsouth;
struct chrg_switch_t *pSW = RT_NULL;
while (1) {
rt_mutex_take(pROLL->mutex, RT_WAITING_FOREVER);
pROLL->idx = idx%(TOTAL_CHS+TOTAL_SOUTH_CHS);
rt_mutex_release(pROLL->mutex);
if (pROLL->idx < TOTAL_CHS) {
if (0 == pROLL->en_nor[pROLL->idx]) {
idx++;
rt_thread_mdelay(1);
continue;
} else {
pNOR->idx = (eIDX_CH)pROLL->idx;
chrg_switch_north(pNOR->idx);
pSW = &pNOR->sw[pNOR->idx];
if (ID_SOURCE == pSW->id) {
source_get_vc(pSW->source.voltage, pSW->source.current);
} else if (ID_SINK == pSW->id) {
sink_get_vc(pSW->sink.loadv, pSW->sink.loadc);
}
}
} else {
if (0 == pROLL->en_sou[pROLL->idx]) {
idx++;
rt_thread_mdelay(1);
continue;
} else {
pSOU->idx = (eIDX_SOUTH_CH)(pROLL->idx - TOTAL_CHS);
chrg_switch_south(pSOU->idx);
pSW = &pSOU->sw[pSOU->idx];
if (ID_SOURCE == pSW->id) {
source_get_vc(pSW->source.voltage, pSW->source.current);
} else if (ID_SINK == pSW->id) {
sink_get_vc(pSW->sink.loadv, pSW->sink.loadc);
}
}
}
idx++;
ret = rt_event_recv(pROLL->evt, ROLL_EVT_RECVED,
(RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR), 100, &recved); // wait 100ms
if (RT_EOK != ret) {
//LOG_E("evt recv failed. [%d]", ret);
continue;
}
if (recved & ROLL_EVT_RECVED) {
}
}
}
static int chrg_roll_init (void)
{
struct chrg_roll_t *pROLL = &chrgroll;
pROLL->mutex = rt_mutex_create(pROLL->name, RT_IPC_FLAG_FIFO);
if (RT_NULL == pROLL->mutex) {
LOG_E("create mutex '%s' failed.", pROLL->name);
return -RT_ERROR;
}
pROLL->evt = rt_event_create(pROLL->name, RT_IPC_FLAG_FIFO);
if (RT_NULL == pROLL->evt) {
LOG_E("create ev '%s' failed.", pROLL->name);
return -RT_ERROR;
}
pROLL->tid = rt_thread_create(pROLL->name, chrg_roll_thread_entry, pROLL, 2048,
RT_MAIN_THREAD_PRIORITY, 20);
if (RT_NULL != pROLL->tid) {
rt_thread_startup(pROLL->tid);
} else {
LOG_E("create thread '%s' failed.", pROLL->name);
}
return RT_EOK;
}
INIT_APP_EXPORT(chrg_roll_init);
+34
View File
@@ -0,0 +1,34 @@
#ifndef __CHRG_ROLL_H__
#define __CHRG_ROLL_H__
#include <rtthread.h>
#include "chrg_switch.h"
#define THR_NAME_ROLL "thr.roll"
#define ROLL_EVT_RECVED (1<<0) // 收到数据
struct chrg_roll_t {
const char *name;
struct rt_thread *tid;
rt_mutex_t mutex;
rt_event_t evt;
rt_uint8_t idx; // 轮巡索引 0-3:nor 4-11:sou
rt_uint8_t en_nor[TOTAL_CHS]; // 北向通路使能
rt_uint8_t en_sou[TOTAL_SOUTH_CHS]; // 南向通路使能
};
extern struct chrg_roll_t chrgroll;
extern rt_uint8_t chrg_roll_get_idx (void);
extern void chrg_roll_evt_send (void);
#endif
+5 -1
View File
@@ -2,7 +2,7 @@
#include <rtthread.h>
#include "chrg_south.h"
#include "chrg_roll.h"
#include "chrg_utils.h"
#define LOG_TAG "chrg.sou"
@@ -53,9 +53,13 @@ static void chrg_south_thread_entry (void *data)
continue;
}
// pick data & update sw
//rt_kprintf("%s\r\n", (char *)pSOU->rx_buf);
//chrg_tty_send(pTTY, pSOU->rx_buf, len);
chrg_roll_evt_send();
}
@@ -19,6 +19,7 @@ struct chrg_south_t {
rt_uint16_t rx_len;
rt_uint8_t rx_buf[SIZE_BUF_TTY];
eIDX_SOUTH_CH idx; // 当前通路
struct chrg_switch_t sw[TOTAL_SOUTH_CHS];
};
+7
View File
@@ -1,4 +1,11 @@
#include "chrg_sink.h"
int sink_get_vc (rt_uint16_t vol, rt_uint16_t cur)
{
return 0;
}
+1 -1
View File
@@ -142,7 +142,7 @@ struct chrg_sink_t {
rt_uint8_t cc_lvl; // 设置CC线电平等级(0-10,默认7)
};
extern int sink_get_vc (rt_uint16_t vol, rt_uint16_t cur);
#endif
+1 -1
View File
@@ -85,7 +85,7 @@ struct chrg_src_t {
};
extern int source_get_vc (rt_uint16_t vol, rt_uint16_t cur);
#endif
+1 -1
View File
@@ -53,7 +53,7 @@ static const rt_uint8_t aucCRCLo[] = {
0x41, 0x81, 0x80, 0x40
};
rt_uint16_t usMBCRC16 (rt_uint8_t *pucFrame, rt_uint16_t usLen)
rt_uint16_t mb_crc16 (rt_uint8_t *pucFrame, rt_uint16_t usLen)
{
rt_uint8_t ucCRCHi = 0xFF;
rt_uint8_t ucCRCLo = 0xFF;
+1 -1
View File
@@ -3,7 +3,7 @@
#include <rtthread.h>
extern rt_uint16_t usMBCRC16 (rt_uint8_t * pucFrame, rt_uint16_t usLen);
extern rt_uint16_t mb_crc16 (rt_uint8_t * pucFrame, rt_uint16_t usLen);
extern rt_uint8_t calc_crc8 (rt_uint8_t *data, rt_uint8_t length);