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/

Committer:
shliu1
Date:
Thu Dec 22 14:50:10 2016 +0800
Revision:
9:a2e3c8955dda
Parent:
5:a1dd75c39f6a
Child:
11:9d35473bab9c
Support M453, target is numaker_pfm_m453

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wclin 0:084b94d2c1b5 1 /*
wclin 0:084b94d2c1b5 2 * The program is a sample code.
wclin 5:a1dd75c39f6a 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 5:a1dd75c39f6a 20 eData_DO,
wclin 5:a1dd75c39f6a 21 eData_RGB,
wclin 5:a1dd75c39f6a 22 eData_MBResistorVar,
wclin 5:a1dd75c39f6a 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 5:a1dd75c39f6a 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 5:a1dd75c39f6a 38 AnalogIn LM35(A0);
wclin 0:084b94d2c1b5 39
wclin 5:a1dd75c39f6a 40 #define DEF_LED_NUM 6
shliu1 9:a2e3c8955dda 41 #if defined(TARGET_NUMAKER_PFM_NUC472)
wclin 5:a1dd75c39f6a 42 DigitalOut LED[DEF_LED_NUM] = { PF_9, PF_10, PC_10, PC_11, PA_10, PA_9 } ;
shliu1 9:a2e3c8955dda 43 #elif defined(TARGET_NUMAKER_PFM_M453)
shliu1 9:a2e3c8955dda 44 DigitalOut LED[DEF_LED_NUM] = { PC_6, PC_7, PC_11, PC_12, PC_13, PC_14 } ;
shliu1 9:a2e3c8955dda 45 #endif
wclin 5:a1dd75c39f6a 46
wclin 5:a1dd75c39f6a 47 void light_leds()
wclin 0:084b94d2c1b5 48 {
wclin 0:084b94d2c1b5 49 int i=0;
wclin 5:a1dd75c39f6a 50 USHORT usOutValue = ~usRegInputBuf[eData_DO];
wclin 5:a1dd75c39f6a 51 for ( i=0; i<DEF_LED_NUM ; i++)
wclin 5:a1dd75c39f6a 52 LED[i].write( (usOutValue&(0x1<<i)) >> i );
wclin 0:084b94d2c1b5 53 }
wclin 0:084b94d2c1b5 54
wclin 5:a1dd75c39f6a 55 void get_temp(void)
wclin 5:a1dd75c39f6a 56 {
wclin 5:a1dd75c39f6a 57 float tempC, a[10], avg;
wclin 5:a1dd75c39f6a 58 int i;
wclin 5:a1dd75c39f6a 59 #define DEF_ADC_READTIMES 10
wclin 5:a1dd75c39f6a 60 avg=0;
wclin 5:a1dd75c39f6a 61 for(i=0;i<DEF_ADC_READTIMES;i++)
wclin 5:a1dd75c39f6a 62 {
wclin 5:a1dd75c39f6a 63 a[i] = LM35.read();
wclin 5:a1dd75c39f6a 64 Thread::wait(1);
wclin 5:a1dd75c39f6a 65 }
wclin 5:a1dd75c39f6a 66
wclin 5:a1dd75c39f6a 67 for ( i=0; i<DEF_ADC_READTIMES; i++ )
wclin 5:a1dd75c39f6a 68 avg += a[i];
wclin 5:a1dd75c39f6a 69
wclin 5:a1dd75c39f6a 70 avg /= DEF_ADC_READTIMES;
wclin 5:a1dd75c39f6a 71 tempC=(avg*3.685503686*100);
wclin 5:a1dd75c39f6a 72 usRegInputBuf[eData_TemperatureSensor] = (USHORT)tempC;
wclin 5:a1dd75c39f6a 73 //printf("[%s %d] %f %d\r\n", __func__, __LINE__, avg, usRegInputBuf[eData_TemperatureSensor] );
wclin 5:a1dd75c39f6a 74 }
wclin 5:a1dd75c39f6a 75
wclin 5:a1dd75c39f6a 76 void worker_get_temperature(void const *args)
wclin 5:a1dd75c39f6a 77 {
wclin 5:a1dd75c39f6a 78 // Poll temperature sensor per 1 second.
wclin 5:a1dd75c39f6a 79 while (true) {
wclin 5:a1dd75c39f6a 80 get_temp();
wclin 5:a1dd75c39f6a 81 led1 = !led1;
wclin 5:a1dd75c39f6a 82 Thread::wait(1000);
wclin 5:a1dd75c39f6a 83 }
wclin 5:a1dd75c39f6a 84 }
wclin 5:a1dd75c39f6a 85
wclin 5:a1dd75c39f6a 86 #if 0
wclin 0:084b94d2c1b5 87 void worker_uart(void const *args)
wclin 0:084b94d2c1b5 88 {
wclin 0:084b94d2c1b5 89 // For UART-SERIAL Tx/Rx Service.
wclin 2:60deb475f831 90 while (true)
wclin 5:a1dd75c39f6a 91 xMBPortSerialPolling();
wclin 0:084b94d2c1b5 92 }
wclin 5:a1dd75c39f6a 93 #endif
wclin 0:084b94d2c1b5 94
wclin 0:084b94d2c1b5 95 /* ----------------------- Start implementation -----------------------------*/
wclin 0:084b94d2c1b5 96 int
wclin 0:084b94d2c1b5 97 main( void )
wclin 0:084b94d2c1b5 98 {
wclin 0:084b94d2c1b5 99 eMBErrorCode eStatus;
wclin 5:a1dd75c39f6a 100 Thread temperature_thread(worker_get_temperature);
wclin 3:489a1d39f6ac 101 //Thread uart_thread(worker_uart);
wclin 0:084b94d2c1b5 102
wclin 0:084b94d2c1b5 103 // Initialise some registers
wclin 0:084b94d2c1b5 104 for (int i=0; i<REG_INPUT_NREGS; i++)
wclin 0:084b94d2c1b5 105 usRegInputBuf[i] = 0x0;
wclin 2:60deb475f831 106
wclin 5:a1dd75c39f6a 107 light_leds(); // Control LEDs
wclin 5:a1dd75c39f6a 108
wclin 0:084b94d2c1b5 109 /* Enable the Modbus Protocol Stack. */
wclin 5:a1dd75c39f6a 110 if ( (eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE )) != MB_ENOERR )
wclin 0:084b94d2c1b5 111 goto FAIL_MB;
wclin 0:084b94d2c1b5 112 else if ( (eStatus = eMBEnable( ) ) != MB_ENOERR )
wclin 0:084b94d2c1b5 113 goto FAIL_MB_1;
wclin 0:084b94d2c1b5 114 else {
wclin 0:084b94d2c1b5 115 for( ;; )
wclin 0:084b94d2c1b5 116 {
wclin 3:489a1d39f6ac 117 xMBPortSerialPolling();
wclin 0:084b94d2c1b5 118 if ( eMBPoll( ) != MB_ENOERR ) break;
wclin 2:60deb475f831 119 }
wclin 0:084b94d2c1b5 120 }
wclin 2:60deb475f831 121
wclin 0:084b94d2c1b5 122 FAIL_MB_1:
wclin 0:084b94d2c1b5 123 eMBClose();
wclin 2:60deb475f831 124
wclin 0:084b94d2c1b5 125 FAIL_MB:
wclin 0:084b94d2c1b5 126 for( ;; )
wclin 0:084b94d2c1b5 127 {
wclin 0:084b94d2c1b5 128 led2 = !led2;
wclin 0:084b94d2c1b5 129 Thread::wait(200);
wclin 2:60deb475f831 130 }
wclin 0:084b94d2c1b5 131 }
wclin 0:084b94d2c1b5 132
wclin 0:084b94d2c1b5 133
wclin 0:084b94d2c1b5 134
wclin 0:084b94d2c1b5 135
wclin 5:a1dd75c39f6a 136
wclin 5:a1dd75c39f6a 137
wclin 5:a1dd75c39f6a 138
wclin 5:a1dd75c39f6a 139
wclin 5:a1dd75c39f6a 140
wclin 5:a1dd75c39f6a 141
wclin 5:a1dd75c39f6a 142
wclin 5:a1dd75c39f6a 143
wclin 5:a1dd75c39f6a 144
wclin 0:084b94d2c1b5 145 eMBErrorCode
wclin 0:084b94d2c1b5 146 eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
wclin 0:084b94d2c1b5 147 {
wclin 0:084b94d2c1b5 148 eMBErrorCode eStatus = MB_ENOERR;
wclin 0:084b94d2c1b5 149 int iRegIndex;
wclin 0:084b94d2c1b5 150
wclin 0:084b94d2c1b5 151 if( ( usAddress >= REG_INPUT_START )
wclin 0:084b94d2c1b5 152 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:084b94d2c1b5 153 {
wclin 0:084b94d2c1b5 154 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:084b94d2c1b5 155 while( usNRegs > 0 )
wclin 0:084b94d2c1b5 156 {
wclin 0:084b94d2c1b5 157 *pucRegBuffer++ =
wclin 0:084b94d2c1b5 158 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
wclin 0:084b94d2c1b5 159 *pucRegBuffer++ =
wclin 0:084b94d2c1b5 160 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
wclin 0:084b94d2c1b5 161 iRegIndex++;
wclin 0:084b94d2c1b5 162 usNRegs--;
wclin 0:084b94d2c1b5 163 }
wclin 0:084b94d2c1b5 164 }
wclin 0:084b94d2c1b5 165 else
wclin 0:084b94d2c1b5 166 {
wclin 0:084b94d2c1b5 167 eStatus = MB_ENOREG;
wclin 0:084b94d2c1b5 168 }
wclin 0:084b94d2c1b5 169
wclin 0:084b94d2c1b5 170 return eStatus;
wclin 0:084b94d2c1b5 171 }
wclin 0:084b94d2c1b5 172
wclin 0:084b94d2c1b5 173 eMBErrorCode
wclin 0:084b94d2c1b5 174 eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
wclin 0:084b94d2c1b5 175 {
wclin 0:084b94d2c1b5 176 eMBErrorCode eStatus = MB_ENOERR;
wclin 0:084b94d2c1b5 177 int iRegIndex;
wclin 2:60deb475f831 178
wclin 0:084b94d2c1b5 179 usRegInputBuf[eData_MBInCounter]++;
wclin 0:084b94d2c1b5 180 usRegInputBuf[eData_MBOutCounter]++;
wclin 0:084b94d2c1b5 181
wclin 0:084b94d2c1b5 182 if (eMode == MB_REG_READ)
wclin 0:084b94d2c1b5 183 {
wclin 0:084b94d2c1b5 184 if( ( usAddress >= REG_INPUT_START )
wclin 0:084b94d2c1b5 185 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:084b94d2c1b5 186 {
wclin 0:084b94d2c1b5 187 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:084b94d2c1b5 188 while( usNRegs > 0 )
wclin 0:084b94d2c1b5 189 {
wclin 0:084b94d2c1b5 190 *pucRegBuffer++ =
wclin 0:084b94d2c1b5 191 ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
wclin 0:084b94d2c1b5 192 *pucRegBuffer++ =
wclin 0:084b94d2c1b5 193 ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
wclin 0:084b94d2c1b5 194 iRegIndex++;
wclin 0:084b94d2c1b5 195 usNRegs--;
wclin 0:084b94d2c1b5 196 }
wclin 0:084b94d2c1b5 197 }
wclin 0:084b94d2c1b5 198 }
wclin 0:084b94d2c1b5 199
wclin 0:084b94d2c1b5 200 if (eMode == MB_REG_WRITE)
wclin 0:084b94d2c1b5 201 {
wclin 0:084b94d2c1b5 202 if( ( usAddress >= REG_INPUT_START )
wclin 0:084b94d2c1b5 203 && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
wclin 0:084b94d2c1b5 204 {
wclin 0:084b94d2c1b5 205 iRegIndex = ( int )( usAddress - usRegInputStart );
wclin 0:084b94d2c1b5 206 while( usNRegs > 0 )
wclin 0:084b94d2c1b5 207 {
wclin 0:084b94d2c1b5 208 usRegInputBuf[iRegIndex] = ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1));
wclin 0:084b94d2c1b5 209 pucRegBuffer+=2;
wclin 0:084b94d2c1b5 210 iRegIndex++;
wclin 0:084b94d2c1b5 211 usNRegs--;
wclin 0:084b94d2c1b5 212 }
wclin 5:a1dd75c39f6a 213 light_leds(); // Control LEDs
wclin 0:084b94d2c1b5 214 }
wclin 0:084b94d2c1b5 215 }
wclin 0:084b94d2c1b5 216
wclin 0:084b94d2c1b5 217 led3=!led3;
wclin 0:084b94d2c1b5 218
wclin 0:084b94d2c1b5 219 return eStatus;
wclin 0:084b94d2c1b5 220 }
wclin 0:084b94d2c1b5 221
wclin 0:084b94d2c1b5 222
wclin 0:084b94d2c1b5 223 eMBErrorCode
wclin 0:084b94d2c1b5 224 eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
wclin 0:084b94d2c1b5 225 eMBRegisterMode eMode )
wclin 0:084b94d2c1b5 226 {
wclin 0:084b94d2c1b5 227 return MB_ENOREG;
wclin 0:084b94d2c1b5 228 }
wclin 0:084b94d2c1b5 229
wclin 0:084b94d2c1b5 230 eMBErrorCode
wclin 0:084b94d2c1b5 231 eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
wclin 0:084b94d2c1b5 232 {
wclin 0:084b94d2c1b5 233 return MB_ENOREG;
wclin 0:084b94d2c1b5 234 }