#include "assert.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "eic.h"
#include "mb.h"
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4
static unsigned short usRegInputStart = REG_INPUT_START;
static unsigned short usRegInputBuf[REG_INPUT_NREGS];
static void vModbusTask( void *pvParameters );
int
main( void )
{
EIC_Init( );
EIC_IRQConfig( ENABLE );
( void )xTaskCreate( vModbusTask, NULL, configMINIMAL_STACK_SIZE, NULL,
tskIDLE_PRIORITY, NULL );
vTaskStartScheduler( );
return 0;
}
static void
vModbusTask( void *pvParameters )
{
portTickType xLastWakeTime;
( void )eMBInit( MB_RTU, 0x0A, 38400, MB_PAR_EVEN );
( void )eMBEnable( );
for( ;; )
{
( void )eMBPoll( );
usRegInputBuf[0]++;
xLastWakeTime = xTaskGetTickCount( );
usRegInputBuf[1] = ( unsigned portSHORT )( xLastWakeTime >> 16UL );
usRegInputBuf[2] = ( unsigned portSHORT )( xLastWakeTime & 0xFFFFUL );
usRegInputBuf[3] = 33;
}
}
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 )
{
return MB_ENOREG;
}
eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
eMBRegisterMode eMode )
{
return MB_ENOREG;
}
eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
return MB_ENOREG;
}
void
__assert( const char *pcFile, const char *pcLine, int iLineNumber )
{
portENTER_CRITICAL( );
for( ;; );
}