NuMaker NuWicam sample on NUC472
Dependencies: Modbus nvt_rs485
NuWicam for NuMaker-PFM-NUC472 board. Please refer document before executing it. More details, please refer https://github.com/OpenNuvoton/NuMaker_NuWicam_Samples/
main.cpp@3:489a1d39f6ac, 2016-09-20 (annotated)
- Committer:
- wclin
- Date:
- Tue Sep 20 06:18:35 2016 +0000
- Revision:
- 3:489a1d39f6ac
- Parent:
- 2:60deb475f831
- Child:
- 5:a1dd75c39f6a
For NuMaker-PFM-NUC472
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wclin | 0:084b94d2c1b5 | 1 | /* |
wclin | 0:084b94d2c1b5 | 2 | * The program is a sample code. |
wclin | 3:489a1d39f6ac | 3 | * It needs run with some NuMaker-PFM-NUC472 boards. |
wclin | 0:084b94d2c1b5 | 4 | */ |
wclin | 0:084b94d2c1b5 | 5 | |
wclin | 0:084b94d2c1b5 | 6 | /* ----------------------- System includes --------------------------------*/ |
wclin | 0:084b94d2c1b5 | 7 | #include "mbed.h" |
wclin | 0:084b94d2c1b5 | 8 | #include "rtos.h" |
wclin | 0:084b94d2c1b5 | 9 | /*----------------------- Modbus includes ----------------------------------*/ |
wclin | 0:084b94d2c1b5 | 10 | #include "mb.h" |
wclin | 0:084b94d2c1b5 | 11 | #include "mbport.h" |
wclin | 0:084b94d2c1b5 | 12 | |
wclin | 0:084b94d2c1b5 | 13 | /* ----------------------- Defines ------------------------------------------*/ |
wclin | 0:084b94d2c1b5 | 14 | // Sharing buffer index |
wclin | 0:084b94d2c1b5 | 15 | enum { |
wclin | 0:084b94d2c1b5 | 16 | eData_MBInCounter, |
wclin | 0:084b94d2c1b5 | 17 | eData_MBOutCounter, |
wclin | 0:084b94d2c1b5 | 18 | eData_MBError, |
wclin | 0:084b94d2c1b5 | 19 | eData_DI, |
wclin | 3:489a1d39f6ac | 20 | eData_DATA, |
wclin | 0:084b94d2c1b5 | 21 | eData_Cnt |
wclin | 0:084b94d2c1b5 | 22 | } E_DATA_TYPE; |
wclin | 0:084b94d2c1b5 | 23 | |
wclin | 0:084b94d2c1b5 | 24 | #define REG_INPUT_START 1 |
wclin | 0:084b94d2c1b5 | 25 | #define REG_INPUT_NREGS eData_Cnt |
wclin | 0:084b94d2c1b5 | 26 | /* ----------------------- Static variables ---------------------------------*/ |
wclin | 0:084b94d2c1b5 | 27 | static USHORT usRegInputStart = REG_INPUT_START; |
wclin | 0:084b94d2c1b5 | 28 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; |
wclin | 0:084b94d2c1b5 | 29 | |
wclin | 0:084b94d2c1b5 | 30 | DigitalOut led1(LED1); // For temperature worker. |
wclin | 0:084b94d2c1b5 | 31 | DigitalOut led2(LED2); // For Modbus worker. |
wclin | 0:084b94d2c1b5 | 32 | DigitalOut led3(LED3); // For Holder CB |
wclin | 0:084b94d2c1b5 | 33 | |
wclin | 3:489a1d39f6ac | 34 | #define DEF_PIN_NUM 6 |
wclin | 3:489a1d39f6ac | 35 | DigitalIn DipSwitch[DEF_PIN_NUM] = { PG_1, PG_2, PF_9, PF_10, PC_10, PC_11 } ; |
wclin | 0:084b94d2c1b5 | 36 | |
wclin | 3:489a1d39f6ac | 37 | unsigned short GetValueOnDipSwitch() |
wclin | 0:084b94d2c1b5 | 38 | { |
wclin | 0:084b94d2c1b5 | 39 | int i=0; |
wclin | 3:489a1d39f6ac | 40 | unsigned short usDipValue = 0x0; |
wclin | 3:489a1d39f6ac | 41 | for ( i=0; i<DEF_PIN_NUM ; i++) |
wclin | 3:489a1d39f6ac | 42 | usDipValue |= DipSwitch[i].read() << i; |
wclin | 3:489a1d39f6ac | 43 | usDipValue = (~usDipValue) & 0x003F; |
wclin | 3:489a1d39f6ac | 44 | return usDipValue; |
wclin | 0:084b94d2c1b5 | 45 | } |
wclin | 0:084b94d2c1b5 | 46 | |
wclin | 0:084b94d2c1b5 | 47 | void worker_uart(void const *args) |
wclin | 0:084b94d2c1b5 | 48 | { |
wclin | 3:489a1d39f6ac | 49 | int counter=0; |
wclin | 0:084b94d2c1b5 | 50 | // For UART-SERIAL Tx/Rx Service. |
wclin | 2:60deb475f831 | 51 | while (true) |
wclin | 3:489a1d39f6ac | 52 | { |
wclin | 3:489a1d39f6ac | 53 | //xMBPortSerialPolling(); |
wclin | 3:489a1d39f6ac | 54 | if ( counter > 10000 ) |
wclin | 3:489a1d39f6ac | 55 | { |
wclin | 3:489a1d39f6ac | 56 | led2 = !led2; |
wclin | 3:489a1d39f6ac | 57 | counter=0; |
wclin | 3:489a1d39f6ac | 58 | } |
wclin | 3:489a1d39f6ac | 59 | counter++; |
wclin | 3:489a1d39f6ac | 60 | } |
wclin | 0:084b94d2c1b5 | 61 | } |
wclin | 0:084b94d2c1b5 | 62 | |
wclin | 0:084b94d2c1b5 | 63 | /* ----------------------- Start implementation -----------------------------*/ |
wclin | 0:084b94d2c1b5 | 64 | int |
wclin | 0:084b94d2c1b5 | 65 | main( void ) |
wclin | 0:084b94d2c1b5 | 66 | { |
wclin | 0:084b94d2c1b5 | 67 | eMBErrorCode eStatus; |
wclin | 3:489a1d39f6ac | 68 | //Thread uart_thread(worker_uart); |
wclin | 3:489a1d39f6ac | 69 | unsigned short usSlaveID=GetValueOnDipSwitch(); |
wclin | 0:084b94d2c1b5 | 70 | |
wclin | 0:084b94d2c1b5 | 71 | // Initialise some registers |
wclin | 0:084b94d2c1b5 | 72 | for (int i=0; i<REG_INPUT_NREGS; i++) |
wclin | 0:084b94d2c1b5 | 73 | usRegInputBuf[i] = 0x0; |
wclin | 3:489a1d39f6ac | 74 | |
wclin | 3:489a1d39f6ac | 75 | printf("We will set modbus slave ID-%d(0x%x) for the device.\r\n", usSlaveID, usSlaveID ); |
wclin | 2:60deb475f831 | 76 | |
wclin | 0:084b94d2c1b5 | 77 | /* Enable the Modbus Protocol Stack. */ |
wclin | 3:489a1d39f6ac | 78 | if ( (eStatus = eMBInit( MB_RTU, usSlaveID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR ) |
wclin | 0:084b94d2c1b5 | 79 | goto FAIL_MB; |
wclin | 0:084b94d2c1b5 | 80 | else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR ) |
wclin | 0:084b94d2c1b5 | 81 | goto FAIL_MB_1; |
wclin | 0:084b94d2c1b5 | 82 | else { |
wclin | 0:084b94d2c1b5 | 83 | for( ;; ) |
wclin | 0:084b94d2c1b5 | 84 | { |
wclin | 3:489a1d39f6ac | 85 | xMBPortSerialPolling(); |
wclin | 0:084b94d2c1b5 | 86 | if ( eMBPoll( ) != MB_ENOERR ) break; |
wclin | 2:60deb475f831 | 87 | } |
wclin | 0:084b94d2c1b5 | 88 | } |
wclin | 2:60deb475f831 | 89 | |
wclin | 0:084b94d2c1b5 | 90 | FAIL_MB_1: |
wclin | 0:084b94d2c1b5 | 91 | eMBClose(); |
wclin | 2:60deb475f831 | 92 | |
wclin | 0:084b94d2c1b5 | 93 | FAIL_MB: |
wclin | 0:084b94d2c1b5 | 94 | for( ;; ) |
wclin | 0:084b94d2c1b5 | 95 | { |
wclin | 0:084b94d2c1b5 | 96 | led2 = !led2; |
wclin | 0:084b94d2c1b5 | 97 | Thread::wait(200); |
wclin | 2:60deb475f831 | 98 | } |
wclin | 0:084b94d2c1b5 | 99 | } |
wclin | 0:084b94d2c1b5 | 100 | |
wclin | 0:084b94d2c1b5 | 101 | |
wclin | 0:084b94d2c1b5 | 102 | |
wclin | 0:084b94d2c1b5 | 103 | |
wclin | 0:084b94d2c1b5 | 104 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 105 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
wclin | 0:084b94d2c1b5 | 106 | { |
wclin | 0:084b94d2c1b5 | 107 | eMBErrorCode eStatus = MB_ENOERR; |
wclin | 0:084b94d2c1b5 | 108 | int iRegIndex; |
wclin | 0:084b94d2c1b5 | 109 | |
wclin | 0:084b94d2c1b5 | 110 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 111 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 112 | { |
wclin | 0:084b94d2c1b5 | 113 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 114 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 115 | { |
wclin | 0:084b94d2c1b5 | 116 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 117 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
wclin | 0:084b94d2c1b5 | 118 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 119 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
wclin | 0:084b94d2c1b5 | 120 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 121 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 122 | } |
wclin | 0:084b94d2c1b5 | 123 | } |
wclin | 0:084b94d2c1b5 | 124 | else |
wclin | 0:084b94d2c1b5 | 125 | { |
wclin | 0:084b94d2c1b5 | 126 | eStatus = MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 127 | } |
wclin | 0:084b94d2c1b5 | 128 | |
wclin | 0:084b94d2c1b5 | 129 | return eStatus; |
wclin | 0:084b94d2c1b5 | 130 | } |
wclin | 0:084b94d2c1b5 | 131 | |
wclin | 0:084b94d2c1b5 | 132 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 133 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) |
wclin | 0:084b94d2c1b5 | 134 | { |
wclin | 0:084b94d2c1b5 | 135 | eMBErrorCode eStatus = MB_ENOERR; |
wclin | 0:084b94d2c1b5 | 136 | int iRegIndex; |
wclin | 2:60deb475f831 | 137 | |
wclin | 0:084b94d2c1b5 | 138 | usRegInputBuf[eData_MBInCounter]++; |
wclin | 0:084b94d2c1b5 | 139 | usRegInputBuf[eData_MBOutCounter]++; |
wclin | 0:084b94d2c1b5 | 140 | |
wclin | 0:084b94d2c1b5 | 141 | if (eMode == MB_REG_READ) |
wclin | 0:084b94d2c1b5 | 142 | { |
wclin | 3:489a1d39f6ac | 143 | usRegInputBuf[eData_DI] = GetValueOnDipSwitch(); |
wclin | 3:489a1d39f6ac | 144 | |
wclin | 0:084b94d2c1b5 | 145 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 146 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 147 | { |
wclin | 0:084b94d2c1b5 | 148 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 149 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 150 | { |
wclin | 0:084b94d2c1b5 | 151 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 152 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
wclin | 0:084b94d2c1b5 | 153 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 154 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
wclin | 0:084b94d2c1b5 | 155 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 156 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 157 | } |
wclin | 0:084b94d2c1b5 | 158 | } |
wclin | 0:084b94d2c1b5 | 159 | } |
wclin | 0:084b94d2c1b5 | 160 | |
wclin | 0:084b94d2c1b5 | 161 | if (eMode == MB_REG_WRITE) |
wclin | 0:084b94d2c1b5 | 162 | { |
wclin | 0:084b94d2c1b5 | 163 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 164 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 165 | { |
wclin | 0:084b94d2c1b5 | 166 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 167 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 168 | { |
wclin | 0:084b94d2c1b5 | 169 | usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
wclin | 0:084b94d2c1b5 | 170 | pucRegBuffer+=2; |
wclin | 0:084b94d2c1b5 | 171 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 172 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 173 | } |
wclin | 0:084b94d2c1b5 | 174 | } |
wclin | 0:084b94d2c1b5 | 175 | } |
wclin | 0:084b94d2c1b5 | 176 | |
wclin | 0:084b94d2c1b5 | 177 | led3=!led3; |
wclin | 0:084b94d2c1b5 | 178 | |
wclin | 0:084b94d2c1b5 | 179 | return eStatus; |
wclin | 0:084b94d2c1b5 | 180 | } |
wclin | 0:084b94d2c1b5 | 181 | |
wclin | 0:084b94d2c1b5 | 182 | |
wclin | 0:084b94d2c1b5 | 183 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 184 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
wclin | 0:084b94d2c1b5 | 185 | eMBRegisterMode eMode ) |
wclin | 0:084b94d2c1b5 | 186 | { |
wclin | 0:084b94d2c1b5 | 187 | return MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 188 | } |
wclin | 0:084b94d2c1b5 | 189 | |
wclin | 0:084b94d2c1b5 | 190 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 191 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
wclin | 0:084b94d2c1b5 | 192 | { |
wclin | 0:084b94d2c1b5 | 193 | return MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 194 | } |