Support M487 board

Dependencies:   Modbus nvt_rs485

Files at this revision

API Documentation at this revision

Comitter:
wclin
Date:
Fri Oct 06 02:28:18 2017 +0000
Commit message:
1. Support M487 board.; 2. Disable get value from Dip switch, if def-rs485-port value is 1 in mbed_app.json file.

Changed in this revision

Modbus.lib Show annotated file Show diff for this revision Revisions of this file
README.md Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
nvt_rs485.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Modbus.lib	Fri Oct 06 02:28:18 2017 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Nuvoton/code/Modbus/#456375d72644
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/README.md	Fri Oct 06 02:28:18 2017 +0000
@@ -0,0 +1,20 @@
+# Getting started sample with modbus-RS485 on mbed OS
+
+
+### Import NuMaker-mbed-modbus-sample from on-line IDE
+1. Please choose Nuvoton NuMaker-PFM-XXX as your target platform.
+2. Please press the left-up icon "New", then choose "NuMaker Modbus-RS485" from the template list.
+3. Your NuMaker-mbed-modbus-sample program is existed.
+
+#### Now compile
+Please press compile icon.
+
+#### Burn Code & Execute
+1. Connect the board NuMaker-PFM-XXX with your PC by USB cable, then there will be one "mbed" disk.
+2. Copy the built binary file into "mbed" disk on you PC.
+3. Press device's reset button to execute, and you could get the below log by uart console.
+   And LED1, LED2 and LED3 will flash.
+-------------------------------------------------------------------------------
+We will set modbus slave ID-61(0x3d) for the device.
+
+--------------------------------------------------------------------------
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Oct 06 02:28:18 2017 +0000
@@ -0,0 +1,213 @@
+/*
+* The program is a sample code. 
+* It needs run with some NuMaker-PFM-NUC472 boards.
+*
+* Please remeber to modify global definition to enable RS485 port on board.
+* Modify '//#define DEF_RS485_PORT 1' to '#define DEF_RS485_PORT 1'
+*/
+
+/* ----------------------- System includes --------------------------------*/
+#include "mbed.h"
+#include "rtos.h"
+/*----------------------- Modbus includes ----------------------------------*/
+#include "mb.h"
+#include "mbport.h"
+
+/* ----------------------- Defines ------------------------------------------*/
+// Sharing buffer index
+enum {
+  eData_MBInCounter,
+  eData_MBOutCounter,
+  eData_MBError,  
+  eData_DI,
+  eData_DATA,   
+  eData_Cnt
+} E_DATA_TYPE;
+
+#define REG_INPUT_START 1
+#define REG_INPUT_NREGS eData_Cnt
+/* ----------------------- Static variables ---------------------------------*/
+static USHORT   usRegInputStart = REG_INPUT_START;
+static USHORT   usRegInputBuf[REG_INPUT_NREGS];
+
+DigitalOut led1(LED1);  // For temperature worker.
+DigitalOut led2(LED2);  // For Modbus worker.
+DigitalOut led3(LED3);  // For Holder CB
+
+#if defined(MBED_CONF_APP_DEF_RS485_PORT) && (MBED_CONF_APP_DEF_RS485_PORT)// mbed serial port
+
+#define DEF_PIN_NUM 6
+
+#if defined(TARGET_NUMAKER_PFM_NUC472)
+    DigitalIn DipSwitch[DEF_PIN_NUM] = { PG_1, PG_2, PF_9, PF_10, PC_10, PC_11 } ;
+#elif defined(TARGET_NUMAKER_PFM_M453)
+    DigitalIn DipSwitch[DEF_PIN_NUM] = { PD_6, PD_1, PC_6, PC_7, PC_11, PC_12 };
+#elif defined(TARGET_NUMAKER_PFM_M487)
+    DigitalIn DipSwitch[DEF_PIN_NUM] = { PH_9, PH_8, PB_9, PF_11, PG_4, PC_11 };
+#endif
+
+unsigned short GetValueOnDipSwitch()
+{
+    int i=0;
+    unsigned short usDipValue = 0x0;
+    for ( i=0; i<DEF_PIN_NUM ; i++)
+        usDipValue |= DipSwitch[i].read() << i;
+    usDipValue = (~usDipValue) & 0x003F;
+    return usDipValue;
+}
+#else
+unsigned short GetValueOnDipSwitch()
+{
+    //always 0x02
+    return 0x02;
+}
+#endif
+
+void worker_uart(void const *args)
+{   
+    int counter=0;
+    // For UART-SERIAL Tx/Rx Service.
+    while (true)
+    {
+        //xMBPortSerialPolling();
+        if ( counter > 10000 )
+        {
+            led2 = !led2;
+            counter=0;
+        }    
+        counter++;
+    }
+}
+
+/* ----------------------- Start implementation -----------------------------*/
+int
+main( void )
+{
+    eMBErrorCode    eStatus;
+    //Thread uart_thread(worker_uart);
+    unsigned short usSlaveID=GetValueOnDipSwitch();
+    
+    // Initialise some registers
+    for (int i=0; i<REG_INPUT_NREGS; i++)
+         usRegInputBuf[i] = 0x0;
+    
+    printf("We will set modbus slave ID-%d(0x%x) for the device.\r\n", usSlaveID, usSlaveID );
+
+    /* Enable the Modbus Protocol Stack. */
+    if ( (eStatus = eMBInit( MB_RTU, usSlaveID, 0, 115200, MB_PAR_NONE )) !=  MB_ENOERR )
+        goto FAIL_MB;
+    else if ( (eStatus = eMBEnable(  ) ) != MB_ENOERR )
+        goto FAIL_MB_1;
+    else {
+        for( ;; )
+        {
+            xMBPortSerialPolling();
+            if ( eMBPoll( ) != MB_ENOERR ) break;
+        }       
+    }    
+    
+FAIL_MB_1:
+    eMBClose();    
+    
+FAIL_MB:
+    for( ;; )
+    {
+        led2 = !led2;
+        Thread::wait(200);
+    }
+}
+
+
+
+
+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;
+     
+    usRegInputBuf[eData_MBInCounter]++;
+    usRegInputBuf[eData_MBOutCounter]++;
+        
+    if (eMode == MB_REG_READ)
+    {
+        usRegInputBuf[eData_DI] = GetValueOnDipSwitch();
+
+        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--;
+            }
+        }
+    }
+
+    if (eMode == MB_REG_WRITE)
+    {
+        if( ( usAddress >= REG_INPUT_START )
+            && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
+        {
+            iRegIndex = ( int )( usAddress - usRegInputStart );
+            while( usNRegs > 0 )
+            {
+                usRegInputBuf[iRegIndex] =  ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1));
+                pucRegBuffer+=2;
+                iRegIndex++;
+                usNRegs--;
+            }
+        }
+    }
+
+    led3=!led3;
+        
+    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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Fri Oct 06 02:28:18 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#cc7556a92fb9320f4bebb190c6e1315af116c50c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Fri Oct 06 02:28:18 2017 +0000
@@ -0,0 +1,5 @@
+{
+    "config": {
+        "def-rs485-port": "1"
+    }
+}  
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nvt_rs485.lib	Fri Oct 06 02:28:18 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/wclin/code/nvt_rs485/#4c9d65e9275c