#include <stdio.h>
#include "lwip/opt.h"
#include "lwip/sio.h"
#include "lwip/sys.h"
#include "lwip/inet.h"
#include "ppp/ppp.h"
#include "arch/cc.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "eic.h"
#include "netif/serial.h"
#include "mb.h"
#define mainMB_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4
#define REG_HOLDING_START 2000
#define REG_HOLDING_NREGS 130
#define PPP_AUTH_ENABLED 1
#define PPP_USER "freemodbus"
#define PPP_PASS "insecure"
typedef enum
{
CONNECTING, CONNECTED, DISCONNECT
} ePPPThreadControl;
static USHORT usRegInputStart = REG_INPUT_START;
static USHORT usRegInputBuf[REG_INPUT_NREGS];
static USHORT usRegHoldingStart = REG_HOLDING_START;
static USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
static ePPPThreadControl ePPPThrCtl;
static void vlwIPInit( void );
static void vMBServerTask( void *arg );
static void vPPPStatusCB( void *ctx, int errCode, void *arg );
sio_fd_t stdio_fd;
sio_fd_t ppp_fd;
int
main( void )
{
EIC_Init( );
EIC_IRQConfig( ENABLE );
if( ( stdio_fd = sio_open_new( 0, 115200, 8, SIO_STOP_1, SIO_PAR_NONE ) ) == SIO_FD_NULL )
{
}
else
{
vlwIPInit( );
if( ( ppp_fd = sio_open_new( 1, 115200, 8, SIO_STOP_1, SIO_PAR_NONE ) ) == SIO_FD_NULL )
{
vMBPortLog( MB_LOG_ERROR, "PPP", "can't open PPP device!\r\n" );
}
else if( sys_thread_new( vMBServerTask, NULL, mainMB_TASK_PRIORITY ) == SYS_THREAD_NULL )
{
vMBPortLog( MB_LOG_ERROR, "MB-INIT", "can't start modbus task!\r\n" );
}
else
{
vMBPortLog( MB_LOG_INFO, "MB-INIT", "FreeModbus demo application starting...\r\n" );
vTaskStartScheduler( );
}
}
for( ;; );
}
void
vlwIPInit( void )
{
sys_init( );
mem_init( );
memp_init( );
pbuf_init( );
netif_init( );
ip_init( );
tcpip_init( NULL, NULL );
}
void
vMBServerTask( void *arg )
{
eMBErrorCode xStatus;
ePPPThreadControl ePPPThrCtlCur;
int ppp_con_fd;
portTickType xTicks;
pppInit( );
if( PPP_AUTH_ENABLED )
{
pppSetAuth( PPPAUTHTYPE_PAP, PPP_USER, PPP_PASS );
}
else
{
pppSetAuth( PPPAUTHTYPE_NONE, NULL, NULL );
}
do
{
vPortEnterCritical( );
ePPPThrCtl = CONNECTING;
vPortExitCritical( );
if( ( ppp_con_fd = pppOpen( ppp_fd, vPPPStatusCB, NULL ) ) == PPPERR_NONE )
{
do
{
vTaskDelay( ( portTickType ) ( 50UL / portTICK_RATE_MS ) );
vPortEnterCritical( );
ePPPThrCtlCur = ePPPThrCtl;
vPortExitCritical( );
}
while( ePPPThrCtlCur == CONNECTING );
if( ePPPThrCtlCur == CONNECTED )
{
if( eMBTCPInit( MB_TCP_PORT_USE_DEFAULT ) != MB_ENOERR )
{
vMBPortLog( MB_LOG_ERROR, "PPP", "can't initalize modbus stack!\r\n" );
}
else if( eMBEnable( ) != MB_ENOERR )
{
vMBPortLog( MB_LOG_ERROR, "PPP", "can't enable modbus stack!\r\n" );
}
else
{
do
{
vPortEnterCritical( );
ePPPThrCtlCur = ePPPThrCtl;
vPortExitCritical( );
xStatus = eMBPoll( );
xTicks = xTaskGetTickCount( );
usRegInputBuf[0] = ( USHORT ) ( xTicks );
usRegInputBuf[1] = ( USHORT ) ( xTicks >> 16UL );
}
while( ( xStatus == MB_ENOERR ) && ( ePPPThrCtlCur == CONNECTED ) );
( void )eMBDisable( );
( void )eMBClose( );
}
}
pppClose( ppp_con_fd );
}
vTaskDelay( ( portTickType ) ( 1000UL / portTICK_RATE_MS ) );
}
while( pdTRUE );
}
void
vPPPStatusCB( void *ctx, int err, void *arg )
{
extern char *_inet_ntoa( u32_t n );
ePPPThreadControl ePPPThrCtlNew;
struct ppp_addrs *ppp_addrs;
switch ( err )
{
case PPPERR_NONE:
ePPPThrCtlNew = CONNECTED;
ppp_addrs = arg;
vMBPortLog( MB_LOG_INFO, "PPP", "new PPP connection established\r\n" );
vMBPortLog( MB_LOG_INFO, "PPP", " our IP address = %s\r\n",
_inet_ntoa( ppp_addrs->our_ipaddr.addr ) );
vMBPortLog( MB_LOG_INFO, "PPP", " his IP address = %s\r\n",
_inet_ntoa( ppp_addrs->his_ipaddr.addr ) );
vMBPortLog( MB_LOG_INFO, "PPP", " netmask = %s\r\n",
_inet_ntoa( ppp_addrs->netmask.addr ) );
break;
default:
ePPPThrCtlNew = DISCONNECT;
vMBPortLog( MB_LOG_ERROR, "PPP", "PPP connection died ( err = %d )\r\n", err );
break;
}
vPortEnterCritical( );
ePPPThrCtl = ePPPThrCtlNew;
vPortExitCritical( );
}
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++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( UCHAR ) ( 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 )
{
return MB_ENOREG;
}
eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
return MB_ENOREG;
}