![](/media/cache/group/logo_thTzkVQ.jpg.50x50_q85.jpg)
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@2:60deb475f831, 2016-08-18 (annotated)
- Committer:
- wclin
- Date:
- Thu Aug 18 08:24:54 2016 +0000
- Revision:
- 2:60deb475f831
- Parent:
- 0:084b94d2c1b5
- Child:
- 3:489a1d39f6ac
- Child:
- 4:02c148438101
fix buffer overflow.
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 | 0:084b94d2c1b5 | 3 | * It needs run with NuMaker NuWicam board. |
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 | 0:084b94d2c1b5 | 20 | eData_DO, |
wclin | 0:084b94d2c1b5 | 21 | eData_RGB, |
wclin | 0:084b94d2c1b5 | 22 | eData_MBResistorVar, |
wclin | 0:084b94d2c1b5 | 23 | eData_TemperatureSensor, |
wclin | 0:084b94d2c1b5 | 24 | eData_Cnt |
wclin | 0:084b94d2c1b5 | 25 | } E_DATA_TYPE; |
wclin | 0:084b94d2c1b5 | 26 | |
wclin | 0:084b94d2c1b5 | 27 | #define REG_INPUT_START 1 |
wclin | 0:084b94d2c1b5 | 28 | #define REG_INPUT_NREGS eData_Cnt |
wclin | 0:084b94d2c1b5 | 29 | #define SLAVE_ID 0x01 |
wclin | 0:084b94d2c1b5 | 30 | /* ----------------------- Static variables ---------------------------------*/ |
wclin | 0:084b94d2c1b5 | 31 | static USHORT usRegInputStart = REG_INPUT_START; |
wclin | 0:084b94d2c1b5 | 32 | static USHORT usRegInputBuf[REG_INPUT_NREGS]; |
wclin | 0:084b94d2c1b5 | 33 | |
wclin | 0:084b94d2c1b5 | 34 | DigitalOut led1(LED1); // For temperature worker. |
wclin | 0:084b94d2c1b5 | 35 | DigitalOut led2(LED2); // For Modbus worker. |
wclin | 0:084b94d2c1b5 | 36 | DigitalOut led3(LED3); // For Holder CB |
wclin | 0:084b94d2c1b5 | 37 | |
wclin | 0:084b94d2c1b5 | 38 | AnalogIn LM35(A0); |
wclin | 0:084b94d2c1b5 | 39 | |
wclin | 0:084b94d2c1b5 | 40 | #define DEF_LED_NUM 6 |
wclin | 0:084b94d2c1b5 | 41 | DigitalOut LED[DEF_LED_NUM] = { PF_9, PF_10, PC_10, PC_11, PA_10, PA_9 } ; |
wclin | 0:084b94d2c1b5 | 42 | |
wclin | 0:084b94d2c1b5 | 43 | |
wclin | 0:084b94d2c1b5 | 44 | void light_leds() |
wclin | 0:084b94d2c1b5 | 45 | { |
wclin | 0:084b94d2c1b5 | 46 | int i=0; |
wclin | 0:084b94d2c1b5 | 47 | USHORT usOutValue = ~usRegInputBuf[eData_DO]; |
wclin | 0:084b94d2c1b5 | 48 | for ( i=0; i<DEF_LED_NUM ; i++) |
wclin | 0:084b94d2c1b5 | 49 | LED[i].write( (usOutValue&(0x1<<i)) >> i ); |
wclin | 0:084b94d2c1b5 | 50 | } |
wclin | 0:084b94d2c1b5 | 51 | |
wclin | 0:084b94d2c1b5 | 52 | void get_temp(void) |
wclin | 0:084b94d2c1b5 | 53 | { |
wclin | 0:084b94d2c1b5 | 54 | float tempC, a[10], avg; |
wclin | 0:084b94d2c1b5 | 55 | int i; |
wclin | 0:084b94d2c1b5 | 56 | #define DEF_ADC_READTIMES 10 |
wclin | 0:084b94d2c1b5 | 57 | avg=0; |
wclin | 0:084b94d2c1b5 | 58 | for(i=0;i<DEF_ADC_READTIMES;i++) |
wclin | 0:084b94d2c1b5 | 59 | { |
wclin | 0:084b94d2c1b5 | 60 | a[i] = LM35.read(); |
wclin | 0:084b94d2c1b5 | 61 | Thread::wait(1); |
wclin | 0:084b94d2c1b5 | 62 | } |
wclin | 0:084b94d2c1b5 | 63 | |
wclin | 0:084b94d2c1b5 | 64 | for ( i=0; i<DEF_ADC_READTIMES; i++ ) |
wclin | 0:084b94d2c1b5 | 65 | avg += a[i]; |
wclin | 0:084b94d2c1b5 | 66 | |
wclin | 0:084b94d2c1b5 | 67 | avg /= DEF_ADC_READTIMES; |
wclin | 2:60deb475f831 | 68 | tempC=(avg*3.685503686*100); |
wclin | 0:084b94d2c1b5 | 69 | usRegInputBuf[eData_TemperatureSensor] = (USHORT)tempC; |
wclin | 2:60deb475f831 | 70 | //printf("[%s %d] %f %d\r\n", __func__, __LINE__, avg, usRegInputBuf[eData_TemperatureSensor] ); |
wclin | 0:084b94d2c1b5 | 71 | } |
wclin | 0:084b94d2c1b5 | 72 | |
wclin | 0:084b94d2c1b5 | 73 | void worker_get_temperature(void const *args) |
wclin | 0:084b94d2c1b5 | 74 | { |
wclin | 0:084b94d2c1b5 | 75 | // Poll temperature sensor per 1 second. |
wclin | 0:084b94d2c1b5 | 76 | while (true) { |
wclin | 0:084b94d2c1b5 | 77 | get_temp(); |
wclin | 0:084b94d2c1b5 | 78 | led1 = !led1; |
wclin | 0:084b94d2c1b5 | 79 | Thread::wait(1000); |
wclin | 0:084b94d2c1b5 | 80 | } |
wclin | 0:084b94d2c1b5 | 81 | } |
wclin | 0:084b94d2c1b5 | 82 | |
wclin | 0:084b94d2c1b5 | 83 | void worker_uart(void const *args) |
wclin | 0:084b94d2c1b5 | 84 | { |
wclin | 0:084b94d2c1b5 | 85 | // For UART-SERIAL Tx/Rx Service. |
wclin | 2:60deb475f831 | 86 | while (true) |
wclin | 0:084b94d2c1b5 | 87 | xMBPortSerialPolling(); |
wclin | 0:084b94d2c1b5 | 88 | } |
wclin | 0:084b94d2c1b5 | 89 | |
wclin | 0:084b94d2c1b5 | 90 | /* ----------------------- Start implementation -----------------------------*/ |
wclin | 0:084b94d2c1b5 | 91 | int |
wclin | 0:084b94d2c1b5 | 92 | main( void ) |
wclin | 0:084b94d2c1b5 | 93 | { |
wclin | 0:084b94d2c1b5 | 94 | eMBErrorCode eStatus; |
wclin | 0:084b94d2c1b5 | 95 | Thread temperature_thread(worker_get_temperature); |
wclin | 0:084b94d2c1b5 | 96 | Thread uart_thread(worker_uart); |
wclin | 0:084b94d2c1b5 | 97 | |
wclin | 0:084b94d2c1b5 | 98 | // Initialise some registers |
wclin | 0:084b94d2c1b5 | 99 | for (int i=0; i<REG_INPUT_NREGS; i++) |
wclin | 0:084b94d2c1b5 | 100 | usRegInputBuf[i] = 0x0; |
wclin | 2:60deb475f831 | 101 | |
wclin | 2:60deb475f831 | 102 | light_leds(); // Control LEDs |
wclin | 2:60deb475f831 | 103 | |
wclin | 0:084b94d2c1b5 | 104 | /* Enable the Modbus Protocol Stack. */ |
wclin | 0:084b94d2c1b5 | 105 | if ( (eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR ) |
wclin | 0:084b94d2c1b5 | 106 | goto FAIL_MB; |
wclin | 0:084b94d2c1b5 | 107 | else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR ) |
wclin | 0:084b94d2c1b5 | 108 | goto FAIL_MB_1; |
wclin | 0:084b94d2c1b5 | 109 | else { |
wclin | 0:084b94d2c1b5 | 110 | for( ;; ) |
wclin | 0:084b94d2c1b5 | 111 | { |
wclin | 0:084b94d2c1b5 | 112 | if ( eMBPoll( ) != MB_ENOERR ) break; |
wclin | 2:60deb475f831 | 113 | } |
wclin | 0:084b94d2c1b5 | 114 | } |
wclin | 2:60deb475f831 | 115 | |
wclin | 0:084b94d2c1b5 | 116 | FAIL_MB_1: |
wclin | 0:084b94d2c1b5 | 117 | eMBClose(); |
wclin | 2:60deb475f831 | 118 | |
wclin | 0:084b94d2c1b5 | 119 | FAIL_MB: |
wclin | 0:084b94d2c1b5 | 120 | for( ;; ) |
wclin | 0:084b94d2c1b5 | 121 | { |
wclin | 0:084b94d2c1b5 | 122 | led2 = !led2; |
wclin | 0:084b94d2c1b5 | 123 | Thread::wait(200); |
wclin | 2:60deb475f831 | 124 | } |
wclin | 0:084b94d2c1b5 | 125 | } |
wclin | 0:084b94d2c1b5 | 126 | |
wclin | 0:084b94d2c1b5 | 127 | |
wclin | 0:084b94d2c1b5 | 128 | |
wclin | 0:084b94d2c1b5 | 129 | |
wclin | 0:084b94d2c1b5 | 130 | |
wclin | 0:084b94d2c1b5 | 131 | |
wclin | 0:084b94d2c1b5 | 132 | |
wclin | 0:084b94d2c1b5 | 133 | |
wclin | 0:084b94d2c1b5 | 134 | |
wclin | 0:084b94d2c1b5 | 135 | |
wclin | 0:084b94d2c1b5 | 136 | |
wclin | 0:084b94d2c1b5 | 137 | |
wclin | 0:084b94d2c1b5 | 138 | |
wclin | 0:084b94d2c1b5 | 139 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 140 | eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs ) |
wclin | 0:084b94d2c1b5 | 141 | { |
wclin | 0:084b94d2c1b5 | 142 | eMBErrorCode eStatus = MB_ENOERR; |
wclin | 0:084b94d2c1b5 | 143 | int iRegIndex; |
wclin | 0:084b94d2c1b5 | 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 | else |
wclin | 0:084b94d2c1b5 | 160 | { |
wclin | 0:084b94d2c1b5 | 161 | eStatus = MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 162 | } |
wclin | 0:084b94d2c1b5 | 163 | |
wclin | 0:084b94d2c1b5 | 164 | return eStatus; |
wclin | 0:084b94d2c1b5 | 165 | } |
wclin | 0:084b94d2c1b5 | 166 | |
wclin | 0:084b94d2c1b5 | 167 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 168 | eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode ) |
wclin | 0:084b94d2c1b5 | 169 | { |
wclin | 0:084b94d2c1b5 | 170 | eMBErrorCode eStatus = MB_ENOERR; |
wclin | 0:084b94d2c1b5 | 171 | int iRegIndex; |
wclin | 2:60deb475f831 | 172 | |
wclin | 0:084b94d2c1b5 | 173 | usRegInputBuf[eData_MBInCounter]++; |
wclin | 0:084b94d2c1b5 | 174 | usRegInputBuf[eData_MBOutCounter]++; |
wclin | 0:084b94d2c1b5 | 175 | |
wclin | 0:084b94d2c1b5 | 176 | if (eMode == MB_REG_READ) |
wclin | 0:084b94d2c1b5 | 177 | { |
wclin | 0:084b94d2c1b5 | 178 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 179 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 180 | { |
wclin | 0:084b94d2c1b5 | 181 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 182 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 183 | { |
wclin | 0:084b94d2c1b5 | 184 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 185 | ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 ); |
wclin | 0:084b94d2c1b5 | 186 | *pucRegBuffer++ = |
wclin | 0:084b94d2c1b5 | 187 | ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF ); |
wclin | 0:084b94d2c1b5 | 188 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 189 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 190 | } |
wclin | 0:084b94d2c1b5 | 191 | } |
wclin | 0:084b94d2c1b5 | 192 | } |
wclin | 0:084b94d2c1b5 | 193 | |
wclin | 0:084b94d2c1b5 | 194 | if (eMode == MB_REG_WRITE) |
wclin | 0:084b94d2c1b5 | 195 | { |
wclin | 0:084b94d2c1b5 | 196 | if( ( usAddress >= REG_INPUT_START ) |
wclin | 0:084b94d2c1b5 | 197 | && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) ) |
wclin | 0:084b94d2c1b5 | 198 | { |
wclin | 0:084b94d2c1b5 | 199 | iRegIndex = ( int )( usAddress - usRegInputStart ); |
wclin | 0:084b94d2c1b5 | 200 | while( usNRegs > 0 ) |
wclin | 0:084b94d2c1b5 | 201 | { |
wclin | 0:084b94d2c1b5 | 202 | usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1)); |
wclin | 0:084b94d2c1b5 | 203 | pucRegBuffer+=2; |
wclin | 0:084b94d2c1b5 | 204 | iRegIndex++; |
wclin | 0:084b94d2c1b5 | 205 | usNRegs--; |
wclin | 0:084b94d2c1b5 | 206 | } |
wclin | 0:084b94d2c1b5 | 207 | light_leds(); // Control LEDs |
wclin | 0:084b94d2c1b5 | 208 | } |
wclin | 0:084b94d2c1b5 | 209 | } |
wclin | 0:084b94d2c1b5 | 210 | |
wclin | 0:084b94d2c1b5 | 211 | led3=!led3; |
wclin | 0:084b94d2c1b5 | 212 | |
wclin | 0:084b94d2c1b5 | 213 | return eStatus; |
wclin | 0:084b94d2c1b5 | 214 | } |
wclin | 0:084b94d2c1b5 | 215 | |
wclin | 0:084b94d2c1b5 | 216 | |
wclin | 0:084b94d2c1b5 | 217 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 218 | eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, |
wclin | 0:084b94d2c1b5 | 219 | eMBRegisterMode eMode ) |
wclin | 0:084b94d2c1b5 | 220 | { |
wclin | 0:084b94d2c1b5 | 221 | return MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 222 | } |
wclin | 0:084b94d2c1b5 | 223 | |
wclin | 0:084b94d2c1b5 | 224 | eMBErrorCode |
wclin | 0:084b94d2c1b5 | 225 | eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete ) |
wclin | 0:084b94d2c1b5 | 226 | { |
wclin | 0:084b94d2c1b5 | 227 | return MB_ENOREG; |
wclin | 0:084b94d2c1b5 | 228 | } |