From d9714b556703561a0abcbe71970b1cd6922168a6 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 21 Jul 2012 21:23:18 +0000 Subject: Use NuttX types in FreeModBus port; Add FreeModBus demo at apps/examples/modbus; Add new termios APIs git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4964 42af7a65-404d-4744-a932-0658087f49c3 --- apps/modbus/nuttx/port.h | 24 ++++-------- apps/modbus/nuttx/portevent.c | 24 ++++++------ apps/modbus/nuttx/portother.c | 4 +- apps/modbus/nuttx/portserial.c | 88 +++++++++++++++++++++--------------------- apps/modbus/nuttx/porttimer.c | 16 ++++---- 5 files changed, 75 insertions(+), 81 deletions(-) (limited to 'apps/modbus/nuttx') diff --git a/apps/modbus/nuttx/port.h b/apps/modbus/nuttx/port.h index 6bb9ab1ba..d958791d9 100644 --- a/apps/modbus/nuttx/port.h +++ b/apps/modbus/nuttx/port.h @@ -43,12 +43,12 @@ PR_BEGIN_EXTERN_C #define MB_PORT_HAS_CLOSE 1 -#ifndef TRUE -# define TRUE true +#ifndef true +# define true true #endif -#ifndef FALSE -# define FALSE false +#ifndef false +# define false false #endif /* ----------------------- Type definitions ---------------------------------*/ @@ -61,23 +61,15 @@ typedef enum MB_LOG_DEBUG = 3 } eMBPortLogLevel; -typedef bool BOOL; -typedef uint8_t UCHAR; -typedef int8_t CHAR; -typedef uint16_t USHORT; -typedef int16_t SHORT; -typedef uint32_t ULONG; -typedef int32_t LONG; - /* ----------------------- Function prototypes ------------------------------*/ void vMBPortEnterCritical(void); void vMBPortExitCritical(void); -void vMBPortLog(eMBPortLogLevel eLevel, const CHAR * szModule, - const CHAR * szFmt, ...); +void vMBPortLog(eMBPortLogLevel eLevel, const char * szModule, + const char * szFmt, ...); void vMBPortTimerPoll(void); -BOOL xMBPortSerialPoll(void); -BOOL xMBPortSerialSetTimeout(ULONG dwTimeoutMs); +bool xMBPortSerialPoll(void); +bool xMBPortSerialSetTimeout(uint32_t dwTimeoutMs); #ifdef __cplusplus PR_END_EXTERN_C diff --git a/apps/modbus/nuttx/portevent.c b/apps/modbus/nuttx/portevent.c index e96eb3d73..72c02f8ed 100644 --- a/apps/modbus/nuttx/portevent.c +++ b/apps/modbus/nuttx/portevent.c @@ -25,36 +25,38 @@ #include #include +#include "port.h" + /* ----------------------- Variables ----------------------------------------*/ static eMBEventType eQueuedEvent; -static BOOL xEventInQueue; +static bool xEventInQueue; /* ----------------------- Start implementation -----------------------------*/ -BOOL +bool xMBPortEventInit( void ) { - xEventInQueue = FALSE; - return TRUE; + xEventInQueue = false; + return true; } -BOOL +bool xMBPortEventPost( eMBEventType eEvent ) { - xEventInQueue = TRUE; + xEventInQueue = true; eQueuedEvent = eEvent; - return TRUE; + return true; } -BOOL +bool xMBPortEventGet( eMBEventType * eEvent ) { - BOOL xEventHappened = FALSE; + bool xEventHappened = false; if( xEventInQueue ) { *eEvent = eQueuedEvent; - xEventInQueue = FALSE; - xEventHappened = TRUE; + xEventInQueue = false; + xEventHappened = true; } else { diff --git a/apps/modbus/nuttx/portother.c b/apps/modbus/nuttx/portother.c index d74d28de3..12f79defe 100644 --- a/apps/modbus/nuttx/portother.c +++ b/apps/modbus/nuttx/portother.c @@ -58,9 +58,9 @@ vMBPortLogFile( FILE * fNewLogFile ) } void -vMBPortLog( eMBPortLogLevel eLevel, const CHAR * szModule, const CHAR * szFmt, ... ) +vMBPortLog( eMBPortLogLevel eLevel, const char * szModule, const char * szFmt, ... ) { - CHAR szBuf[512]; + char szBuf[512]; int i; va_list args; FILE *fOutput = fLogFile == NULL ? stderr : fLogFile; diff --git a/apps/modbus/nuttx/portserial.c b/apps/modbus/nuttx/portserial.c index a30f7f4aa..f62166373 100644 --- a/apps/modbus/nuttx/portserial.c +++ b/apps/modbus/nuttx/portserial.c @@ -50,23 +50,23 @@ /* ----------------------- Static variables ---------------------------------*/ static int iSerialFd = -1; -static BOOL bRxEnabled; -static BOOL bTxEnabled; +static bool bRxEnabled; +static bool bTxEnabled; -static ULONG ulTimeoutMs; -static UCHAR ucBuffer[BUF_SIZE]; +static uint32_t ulTimeoutMs; +static uint8_t ucBuffer[BUF_SIZE]; static int uiRxBufferPos; static int uiTxBufferPos; static struct termios xOldTIO; /* ----------------------- Function prototypes ------------------------------*/ -static BOOL prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead ); -static BOOL prvbMBPortSerialWrite( UCHAR * pucBuffer, USHORT usNBytes ); +static bool prvbMBPortSerialRead( uint8_t * pucBuffer, uint16_t usNBytes, uint16_t * usNBytesRead ); +static bool prvbMBPortSerialWrite( uint8_t * pucBuffer, uint16_t usNBytes ); /* ----------------------- Begin implementation -----------------------------*/ void -vMBPortSerialEnable( BOOL bEnableRx, BOOL bEnableTx ) +vMBPortSerialEnable( bool bEnableRx, bool bEnableTx ) { /* it is not allowed that both receiver and transmitter are enabled. */ ASSERT( !bEnableRx || !bEnableTx ); @@ -75,28 +75,28 @@ vMBPortSerialEnable( BOOL bEnableRx, BOOL bEnableTx ) { ( void )tcflush( iSerialFd, TCIFLUSH ); uiRxBufferPos = 0; - bRxEnabled = TRUE; + bRxEnabled = true; } else { - bRxEnabled = FALSE; + bRxEnabled = false; } if( bEnableTx ) { - bTxEnabled = TRUE; + bTxEnabled = true; uiTxBufferPos = 0; } else { - bTxEnabled = FALSE; + bTxEnabled = false; } } -BOOL -xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) +bool +xMBPortSerialInit( uint8_t ucPort, uint32_t ulBaudRate, uint8_t ucDataBits, eMBParity eParity ) { - CHAR szDevice[16]; - BOOL bStatus = TRUE; + char szDevice[16]; + bool bStatus = true; struct termios xNewTIO; speed_t xNewSpeed; @@ -130,7 +130,7 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e xNewTIO.c_cflag |= PARENB | PARODD; break; default: - bStatus = FALSE; + bStatus = false; } switch ( ucDataBits ) { @@ -141,7 +141,7 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e xNewTIO.c_cflag |= CS7; break; default: - bStatus = FALSE; + bStatus = false; } switch ( ulBaudRate ) { @@ -161,7 +161,7 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e xNewSpeed = B115200; break; default: - bStatus = FALSE; + bStatus = false; } if( bStatus ) { @@ -182,16 +182,16 @@ xMBPortSerialInit( UCHAR ucPort, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity e } else { - vMBPortSerialEnable( FALSE, FALSE ); - bStatus = TRUE; + vMBPortSerialEnable( false, false ); + bStatus = true; } } } return bStatus; } -BOOL -xMBPortSerialSetTimeout( ULONG ulNewTimeoutMs ) +bool +xMBPortSerialSetTimeout( uint32_t ulNewTimeoutMs ) { if( ulNewTimeoutMs > 0 ) { @@ -201,7 +201,7 @@ xMBPortSerialSetTimeout( ULONG ulNewTimeoutMs ) { ulTimeoutMs = 1; } - return TRUE; + return true; } void @@ -215,10 +215,10 @@ vMBPortClose( void ) } } -BOOL -prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead ) +bool +prvbMBPortSerialRead( uint8_t * pucBuffer, uint16_t usNBytes, uint16_t * usNBytesRead ) { - BOOL bResult = TRUE; + bool bResult = true; ssize_t res; fd_set rfds; struct timeval tv; @@ -236,18 +236,18 @@ prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead { if( errno != EINTR ) { - bResult = FALSE; + bResult = false; } } else if( FD_ISSET( iSerialFd, &rfds ) ) { if( ( res = read( iSerialFd, pucBuffer, usNBytes ) ) == -1 ) { - bResult = FALSE; + bResult = false; } else { - *usNBytesRead = ( USHORT ) res; + *usNBytesRead = ( uint16_t ) res; break; } } @@ -257,12 +257,12 @@ prvbMBPortSerialRead( UCHAR * pucBuffer, USHORT usNBytes, USHORT * usNBytesRead break; } } - while( bResult == TRUE ); + while( bResult == true ); return bResult; } -BOOL -prvbMBPortSerialWrite( UCHAR * pucBuffer, USHORT usNBytes ) +bool +prvbMBPortSerialWrite( uint8_t * pucBuffer, uint16_t usNBytes ) { ssize_t res; size_t left = ( size_t ) usNBytes; @@ -282,14 +282,14 @@ prvbMBPortSerialWrite( UCHAR * pucBuffer, USHORT usNBytes ) done += res; left -= res; } - return left == 0 ? TRUE : FALSE; + return left == 0 ? true : false; } -BOOL +bool xMBPortSerialPoll( ) { - BOOL bStatus = TRUE; - USHORT usBytesRead; + bool bStatus = true; + uint16_t usBytesRead; int i; while( bRxEnabled ) @@ -315,7 +315,7 @@ xMBPortSerialPoll( ) { vMBPortLog( MB_LOG_ERROR, "SER-POLL", "read failed on serial device: %s\n", strerror( errno ) ); - bStatus = FALSE; + bStatus = false; } } if( bTxEnabled ) @@ -329,27 +329,27 @@ xMBPortSerialPoll( ) { vMBPortLog( MB_LOG_ERROR, "SER-POLL", "write failed on serial device: %s\n", strerror( errno ) ); - bStatus = FALSE; + bStatus = false; } } return bStatus; } -BOOL -xMBPortSerialPutByte( CHAR ucByte ) +bool +xMBPortSerialPutByte( int8_t ucByte ) { ASSERT( uiTxBufferPos < BUF_SIZE ); ucBuffer[uiTxBufferPos] = ucByte; uiTxBufferPos++; - return TRUE; + return true; } -BOOL -xMBPortSerialGetByte( CHAR * pucByte ) +bool +xMBPortSerialGetByte( int8_t * pucByte ) { ASSERT( uiRxBufferPos < BUF_SIZE ); *pucByte = ucBuffer[uiRxBufferPos]; uiRxBufferPos++; - return TRUE; + return true; } diff --git a/apps/modbus/nuttx/porttimer.c b/apps/modbus/nuttx/porttimer.c index 3ff2d78ae..d688c4f52 100644 --- a/apps/modbus/nuttx/porttimer.c +++ b/apps/modbus/nuttx/porttimer.c @@ -37,14 +37,14 @@ /* ----------------------- Defines ------------------------------------------*/ /* ----------------------- Static variables ---------------------------------*/ -ULONG ulTimeOut; -BOOL bTimeoutEnable; +uint32_t ulTimeOut; +bool bTimeoutEnable; static struct timeval xTimeLast; /* ----------------------- Start implementation -----------------------------*/ -BOOL -xMBPortTimersInit( USHORT usTim1Timerout50us ) +bool +xMBPortTimersInit( uint16_t usTim1Timerout50us ) { ulTimeOut = usTim1Timerout50us / 20U; if( ulTimeOut == 0 ) @@ -62,7 +62,7 @@ xMBPortTimersClose( ) void vMBPortTimerPoll( ) { - ULONG ulDeltaMS; + uint32_t ulDeltaMS; struct timeval xTimeCur; /* Timers are called from the serial layer because we have no high @@ -79,7 +79,7 @@ vMBPortTimerPoll( ) ( xTimeCur.tv_usec - xTimeLast.tv_usec ) * 1000L; if( ulDeltaMS > ulTimeOut ) { - bTimeoutEnable = FALSE; + bTimeoutEnable = false; ( void )pxMBPortCBTimerExpired( ); } } @@ -92,11 +92,11 @@ vMBPortTimersEnable( ) int res = gettimeofday( &xTimeLast, NULL ); ASSERT( res == 0 ); - bTimeoutEnable = TRUE; + bTimeoutEnable = true; } void vMBPortTimersDisable( ) { - bTimeoutEnable = FALSE; + bTimeoutEnable = false; } -- cgit v1.2.3