124 lines
3.2 KiB
C
124 lines
3.2 KiB
C
#include "cmsis_os.h"
|
|
#include "mb.h"
|
|
#include "tim.h"
|
|
/* ----------------------- Defines ------------------------------------------*/
|
|
#define REG_INPUT_START 0x0001U
|
|
#define REG_INPUT_NREGS 4
|
|
|
|
#define REG_HOLDING_START ( 1 )
|
|
#define REG_HOLDING_NREGS ( 32 )
|
|
/* ----------------------- Static variables ---------------------------------*/
|
|
static uint16_t usRegInputStart = REG_INPUT_START;
|
|
static uint16_t usRegInputBuf[REG_INPUT_NREGS]={0x05,0x06,0x07,0x08};
|
|
|
|
static USHORT usRegHoldingStart = REG_HOLDING_START;
|
|
static USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
|
|
|
|
#define TASK1_STK_SIZE 512
|
|
void task1(void *arg);
|
|
osThreadDef(task1, osPriorityNormal, 1, TASK1_STK_SIZE);
|
|
|
|
|
|
void task1(void *arg)
|
|
{
|
|
__HAL_TIM_CLEAR_FLAG(&htim6,TIM_FLAG_UPDATE);
|
|
eMBInit(MB_RTU, 0x01, 1, 9600, MB_PAR_ODD);
|
|
eMBEnable();
|
|
|
|
while (1) {
|
|
eMBPoll();
|
|
printf("FreeModbus eMBPoll function running\r\n");
|
|
osDelay(2000);
|
|
}
|
|
}
|
|
|
|
void application_entry(void *arg)
|
|
{
|
|
osThreadCreate(osThread(task1), NULL); // Create task1
|
|
}
|
|
|
|
eMBErrorCode
|
|
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
|
|
{
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
int iRegIndex;
|
|
|
|
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;
|
|
}
|
|
|
|
eMBErrorCode
|
|
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
|
|
{
|
|
eMBErrorCode eStatus = MB_ENOERR;
|
|
int iRegIndex;
|
|
|
|
if( ( usAddress >= REG_HOLDING_START ) && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
|
|
{
|
|
iRegIndex = ( int )( usAddress - usRegHoldingStart );
|
|
switch ( eMode )
|
|
{
|
|
case MB_REG_READ:
|
|
while( usNRegs > 0 )
|
|
{
|
|
*pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] >> 8 );
|
|
*pucRegBuffer++ = ( unsigned char )( usRegHoldingBuf[iRegIndex] & 0xFF );
|
|
iRegIndex++;
|
|
usNRegs--;
|
|
}
|
|
break;
|
|
|
|
case MB_REG_WRITE:
|
|
while( usNRegs > 0 )
|
|
{
|
|
usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
|
|
usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
|
|
iRegIndex++;
|
|
usNRegs--;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
eStatus = MB_ENOREG;
|
|
}
|
|
return eStatus;
|
|
}
|
|
|
|
eMBErrorCode
|
|
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
|
|
{
|
|
( void )pucRegBuffer;
|
|
( void )usAddress;
|
|
( void )usNCoils;
|
|
( void )eMode;
|
|
return MB_ENOREG;
|
|
}
|
|
|
|
eMBErrorCode
|
|
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
|
|
{
|
|
( void )pucRegBuffer;
|
|
( void )usAddress;
|
|
( void )usNDiscrete;
|
|
return MB_ENOREG;
|
|
}
|