Support M487 board

Dependencies:   Modbus nvt_rs485

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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wclin 0:69a9b661c0ca 1 /*
wclin 0:69a9b661c0ca 2 * The program is a sample code.
wclin 0:69a9b661c0ca 3 * It needs run with some NuMaker-PFM-NUC472 boards.
wclin 0:69a9b661c0ca 4 *
wclin 0:69a9b661c0ca 5 * Please remeber to modify global definition to enable RS485 port on board.
wclin 0:69a9b661c0ca 6 * Modify '//#define DEF_RS485_PORT 1' to '#define DEF_RS485_PORT 1'
wclin 0:69a9b661c0ca 7 */
wclin 0:69a9b661c0ca 8
wclin 0:69a9b661c0ca 9 /* ----------------------- System includes --------------------------------*/
wclin 0:69a9b661c0ca 10 #include "mbed.h"
wclin 0:69a9b661c0ca 11 #include "rtos.h"
wclin 0:69a9b661c0ca 12 /*----------------------- Modbus includes ----------------------------------*/
wclin 0:69a9b661c0ca 13 #include "mb.h"
wclin 0:69a9b661c0ca 14 #include "mbport.h"
wclin 0:69a9b661c0ca 15
wclin 0:69a9b661c0ca 16 /* ----------------------- Defines ------------------------------------------*/
wclin 0:69a9b661c0ca 17 // Sharing buffer index
wclin 0:69a9b661c0ca 18 enum {
wclin 0:69a9b661c0ca 19 eData_MBInCounter,
wclin 0:69a9b661c0ca 20 eData_MBOutCounter,
wclin 0:69a9b661c0ca 21 eData_MBError,
wclin 0:69a9b661c0ca 22 eData_DI,
wclin 0:69a9b661c0ca 23 eData_DATA,
wclin 0:69a9b661c0ca 24 eData_Cnt
wclin 0:69a9b661c0ca 25 } E_DATA_TYPE;
wclin 0:69a9b661c0ca 26
wclin 0:69a9b661c0ca 27 #define REG_INPUT_START 1
wclin 0:69a9b661c0ca 28 #define REG_INPUT_NREGS eData_Cnt
wclin 0:69a9b661c0ca 29 /* ----------------------- Static variables ---------------------------------*/
wclin 0:69a9b661c0ca 30 static USHORT usRegInputStart = REG_INPUT_START;
wclin 0:69a9b661c0ca 31 static USHORT usRegInputBuf[REG_INPUT_NREGS];
wclin 0:69a9b661c0ca 32
wclin 0:69a9b661c0ca 33 DigitalOut led1(LED1); // For temperature worker.
wclin 0:69a9b661c0ca 34 DigitalOut led2(LED2); // For Modbus worker.
wclin 0:69a9b661c0ca 35 DigitalOut led3(LED3); // For Holder CB
wclin 0:69a9b661c0ca 36
wclin 0:69a9b661c0ca 37 #if defined(MBED_CONF_APP_DEF_RS485_PORT) && (MBED_CONF_APP_DEF_RS485_PORT)// mbed serial port
wclin 0:69a9b661c0ca 38
wclin 0:69a9b661c0ca 39 #define DEF_PIN_NUM 6
wclin 0:69a9b661c0ca 40
wclin 0:69a9b661c0ca 41 #if defined(TARGET_NUMAKER_PFM_NUC472)
wclin 0:69a9b661c0ca 42 DigitalIn DipSwitch[DEF_PIN_NUM] = { PG_1, PG_2, PF_9, PF_10, PC_10, PC_11 } ;
wclin 0:69a9b661c0ca 43 #elif defined(TARGET_NUMAKER_PFM_M453)
wclin 0:69a9b661c0ca 44 DigitalIn DipSwitch[DEF_PIN_NUM] = { PD_6, PD_1, PC_6, PC_7, PC_11, PC_12 };
wclin 0:69a9b661c0ca 45 #elif defined(TARGET_NUMAKER_PFM_M487)
wclin 0:69a9b661c0ca 46 DigitalIn DipSwitch[DEF_PIN_NUM] = { PH_9, PH_8, PB_9, PF_11, PG_4, PC_11 };
wclin 0:69a9b661c0ca 47 #endif
wclin 0:69a9b661c0ca 48
wclin 0:69a9b661c0ca 49 unsigned short GetValueOnDipSwitch()
wclin 0:69a9b661c0ca 50 {
wclin 0:69a9b661c0ca 51 int i=0;
wclin 0:69a9b661c0ca 52 unsigned short usDipValue = 0x0;
wclin 0:69a9b661c0ca 53 for ( i=0; i<DEF_PIN_NUM ; i++)
wclin 0:69a9b661c0ca 54 usDipValue |= DipSwitch[i].read() << i;
wclin 0:69a9b661c0ca 55 usDipValue = (~usDipValue) & 0x003F;
wclin 0:69a9b661c0ca 56 return usDipValue;
wclin 0:69a9b661c0ca 57 }
wclin 0:69a9b661c0ca 58 #else
wclin 0:69a9b661c0ca 59 unsigned short GetValueOnDipSwitch()
wclin 0:69a9b661c0ca 60 {
wclin 0:69a9b661c0ca 61 //always 0x02
wclin 0:69a9b661c0ca 62 return 0x02;
wclin 0:69a9b661c0ca 63 }
wclin 0:69a9b661c0ca 64 #endif
wclin 0:69a9b661c0ca 65
wclin 0:69a9b661c0ca 66 void worker_uart(void const *args)
wclin 0:69a9b661c0ca 67 {
wclin 0:69a9b661c0ca 68 int counter=0;
wclin 0:69a9b661c0ca 69 // For UART-SERIAL Tx/Rx Service.
wclin 0:69a9b661c0ca 70 while (true)
wclin 0:69a9b661c0ca 71 {
wclin 0:69a9b661c0ca 72 //xMBPortSerialPolling();
wclin 0:69a9b661c0ca 73 if ( counter > 10000 )
wclin 0:69a9b661c0ca 74 {
wclin 0:69a9b661c0ca 75 led2 = !led2;
wclin 0:69a9b661c0ca 76 counter=0;
wclin 0:69a9b661c0ca 77 }
wclin 0:69a9b661c0ca 78 counter++;
wclin 0:69a9b661c0ca 79 }
wclin 0:69a9b661c0ca 80 }
wclin 0:69a9b661c0ca 81
wclin 0:69a9b661c0ca 82 /* ----------------------- Start implementation -----------------------------*/
wclin 0:69a9b661c0ca 83 int
wclin 0:69a9b661c0ca 84 main( void )
wclin 0:69a9b661c0ca 85 {
wclin 0:69a9b661c0ca 86 eMBErrorCode eStatus;
wclin 0:69a9b661c0ca 87 //Thread uart_thread(worker_uart);
wclin 0:69a9b661c0ca 88 unsigned short usSlaveID=GetValueOnDipSwitch();
wclin 0:69a9b661c0ca 89
wclin 0:69a9b661c0ca 90 // Initialise some registers
wclin 0:69a9b661c0ca 91 for (int i=0; i<REG_INPUT_NREGS; i++)
wclin 0:69a9b661c0ca 92 usRegInputBuf[i] = 0x0;
wclin 0:69a9b661c0ca 93
wclin 0:69a9b661c0ca 94 printf("We will set modbus slave ID-%d(0x%x) for the device.\r\n", usSlaveID, usSlaveID );
wclin 0:69a9b661c0ca 95
wclin 0:69a9b661c0ca 96 /* Enable the Modbus Protocol Stack. */
wclin 0:69a9b661c0ca 97 if ( (eStatus = eMBInit( MB_RTU, usSlaveID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR )
wclin 0:69a9b661c0ca 98 goto FAIL_MB;
wclin 0:69a9b661c0ca 99 else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR )
wclin 0:69a9b661c0ca 100 goto FAIL_MB_1;
wclin 0:69a9b661c0ca 101 else {
wclin 0:69a9b661c0ca 102 for( ;; )
wclin 0:69a9b661c0ca 103 {
wclin 0:69a9b661c0ca 104 xMBPortSerialPolling();
wclin 0:69a9b661c0ca 105 if ( eMBPoll( ) != MB_ENOERR ) break;
wclin 0:69a9b661c0ca 106 }
wclin 0:69a9b661c0ca 107 }
wclin 0:69a9b661c0ca 108
wclin 0:69a9b661c0ca 109 FAIL_MB_1:
wclin 0:69a9b661c0ca 110 eMBClose();
wclin 0:69a9b661c0ca 111
wclin 0:69a9b661c0ca 112 FAIL_MB:
wclin 0:69a9b661c0ca 113 for( ;; )
wclin 0:69a9b661c0ca 114 {
wclin 0:69a9b661c0ca 115 led2 = !led2;
wclin 0:69a9b661c0ca 116 Thread::wait(200);
wclin 0:69a9b661c0ca 117 }
wclin 0:69a9b661c0ca 118 }
wclin 0:69a9b661c0ca 119
wclin 0:69a9b661c0ca 120
wclin 0:69a9b661c0ca 121
wclin 0:69a9b661c0ca 122
wclin 0:69a9b661c0ca 123 eMBErrorCode
wclin 0:69a9b661c0ca 124 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
wclin 0:69a9b661c0ca 125 {
wclin 0:69a9b661c0ca 126 eMBErrorCode eStatus = MB_ENOERR;
wclin 0:69a9b661c0ca 127 int iRegIndex;
wclin 0:69a9b661c0ca 128
wclin 0:69a9b661c0ca 129 if( ( usAddress >= REG_INPUT_START )
wclin 0:69a9b661c0ca 130 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:69a9b661c0ca 131 {
wclin 0:69a9b661c0ca 132 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:69a9b661c0ca 133 while( usNRegs > 0 )
wclin 0:69a9b661c0ca 134 {
wclin 0:69a9b661c0ca 135 *pucRegBuffer++ =
wclin 0:69a9b661c0ca 136 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
wclin 0:69a9b661c0ca 137 *pucRegBuffer++ =
wclin 0:69a9b661c0ca 138 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
wclin 0:69a9b661c0ca 139 iRegIndex++;
wclin 0:69a9b661c0ca 140 usNRegs--;
wclin 0:69a9b661c0ca 141 }
wclin 0:69a9b661c0ca 142 }
wclin 0:69a9b661c0ca 143 else
wclin 0:69a9b661c0ca 144 {
wclin 0:69a9b661c0ca 145 eStatus = MB_ENOREG;
wclin 0:69a9b661c0ca 146 }
wclin 0:69a9b661c0ca 147
wclin 0:69a9b661c0ca 148 return eStatus;
wclin 0:69a9b661c0ca 149 }
wclin 0:69a9b661c0ca 150
wclin 0:69a9b661c0ca 151 eMBErrorCode
wclin 0:69a9b661c0ca 152 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
wclin 0:69a9b661c0ca 153 {
wclin 0:69a9b661c0ca 154 eMBErrorCode eStatus = MB_ENOERR;
wclin 0:69a9b661c0ca 155 int iRegIndex;
wclin 0:69a9b661c0ca 156
wclin 0:69a9b661c0ca 157 usRegInputBuf[eData_MBInCounter]++;
wclin 0:69a9b661c0ca 158 usRegInputBuf[eData_MBOutCounter]++;
wclin 0:69a9b661c0ca 159
wclin 0:69a9b661c0ca 160 if (eMode == MB_REG_READ)
wclin 0:69a9b661c0ca 161 {
wclin 0:69a9b661c0ca 162 usRegInputBuf[eData_DI] = GetValueOnDipSwitch();
wclin 0:69a9b661c0ca 163
wclin 0:69a9b661c0ca 164 if( ( usAddress >= REG_INPUT_START )
wclin 0:69a9b661c0ca 165 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:69a9b661c0ca 166 {
wclin 0:69a9b661c0ca 167 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:69a9b661c0ca 168 while( usNRegs > 0 )
wclin 0:69a9b661c0ca 169 {
wclin 0:69a9b661c0ca 170 *pucRegBuffer++ =
wclin 0:69a9b661c0ca 171 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
wclin 0:69a9b661c0ca 172 *pucRegBuffer++ =
wclin 0:69a9b661c0ca 173 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
wclin 0:69a9b661c0ca 174 iRegIndex++;
wclin 0:69a9b661c0ca 175 usNRegs--;
wclin 0:69a9b661c0ca 176 }
wclin 0:69a9b661c0ca 177 }
wclin 0:69a9b661c0ca 178 }
wclin 0:69a9b661c0ca 179
wclin 0:69a9b661c0ca 180 if (eMode == MB_REG_WRITE)
wclin 0:69a9b661c0ca 181 {
wclin 0:69a9b661c0ca 182 if( ( usAddress >= REG_INPUT_START )
wclin 0:69a9b661c0ca 183 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:69a9b661c0ca 184 {
wclin 0:69a9b661c0ca 185 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:69a9b661c0ca 186 while( usNRegs > 0 )
wclin 0:69a9b661c0ca 187 {
wclin 0:69a9b661c0ca 188 usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1));
wclin 0:69a9b661c0ca 189 pucRegBuffer+=2;
wclin 0:69a9b661c0ca 190 iRegIndex++;
wclin 0:69a9b661c0ca 191 usNRegs--;
wclin 0:69a9b661c0ca 192 }
wclin 0:69a9b661c0ca 193 }
wclin 0:69a9b661c0ca 194 }
wclin 0:69a9b661c0ca 195
wclin 0:69a9b661c0ca 196 led3=!led3;
wclin 0:69a9b661c0ca 197
wclin 0:69a9b661c0ca 198 return eStatus;
wclin 0:69a9b661c0ca 199 }
wclin 0:69a9b661c0ca 200
wclin 0:69a9b661c0ca 201
wclin 0:69a9b661c0ca 202 eMBErrorCode
wclin 0:69a9b661c0ca 203 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
wclin 0:69a9b661c0ca 204 eMBRegisterMode eMode )
wclin 0:69a9b661c0ca 205 {
wclin 0:69a9b661c0ca 206 return MB_ENOREG;
wclin 0:69a9b661c0ca 207 }
wclin 0:69a9b661c0ca 208
wclin 0:69a9b661c0ca 209 eMBErrorCode
wclin 0:69a9b661c0ca 210 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
wclin 0:69a9b661c0ca 211 {
wclin 0:69a9b661c0ca 212 return MB_ENOREG;
wclin 0:69a9b661c0ca 213 }