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/

Revision:
0:084b94d2c1b5
Child:
2:60deb475f831
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Aug 09 08:21:49 2016 +0000
@@ -0,0 +1,239 @@
+/*
+* The program is a sample code. 
+* It needs run with NuMaker NuWicam board.
+*/
+
+/* ----------------------- 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_DO,
+  eData_RGB,
+  eData_MBResistorVar,
+  eData_TemperatureSensor,
+  eData_Cnt
+} E_DATA_TYPE;
+
+#define REG_INPUT_START 1
+#define REG_INPUT_NREGS eData_Cnt
+#define SLAVE_ID 0x01
+/* ----------------------- 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
+
+AnalogIn   LM35(A0);
+
+#define DEF_LED_NUM 6
+DigitalOut LED[DEF_LED_NUM] = { PF_9, PF_10, PC_10, PC_11, PA_10, PA_9 } ;
+
+
+void light_leds()
+{
+    int i=0;
+    USHORT usOutValue = ~usRegInputBuf[eData_DO];
+
+    //printf("[%s %d] 0x%08X\r\n", __func__, __LINE__,  usOutValue );
+    
+    for ( i=0; i<DEF_LED_NUM ; i++)
+        LED[i].write( (usOutValue&(0x1<<i)) >> i  );
+}
+
+void get_temp(void)
+{
+    float tempC, a[10], avg;
+    int i;
+    #define DEF_ADC_READTIMES   10
+    avg=0;
+    for(i=0;i<DEF_ADC_READTIMES;i++)
+    {
+        a[i] = LM35.read();
+        Thread::wait(1);
+    }
+    
+    for ( i=0; i<DEF_ADC_READTIMES; i++ )
+        avg += a[i];
+    
+    avg /= DEF_ADC_READTIMES;
+    
+    //tempC=(avg*3.685503686*100);
+    tempC=3.3*avg*100;
+    usRegInputBuf[eData_TemperatureSensor] = (USHORT)tempC;
+
+    printf("[%s %d] %d\r\n", __func__, __LINE__,  usRegInputBuf[eData_TemperatureSensor]  );
+}
+
+void worker_get_temperature(void const *args)
+{
+    // Poll temperature sensor per 1 second.
+    while (true) {
+        get_temp();
+        led1 = !led1;
+        Thread::wait(1000);
+    }
+}
+
+void worker_uart(void const *args)
+{   
+    // For UART-SERIAL Tx/Rx Service.
+    while (true) {
+        xMBPortSerialPolling();
+    }
+}
+
+/* ----------------------- Start implementation -----------------------------*/
+int
+main( void )
+{
+    eMBErrorCode    eStatus;
+    Thread temperature_thread(worker_get_temperature);
+    Thread uart_thread(worker_uart);
+    
+    // Initialise some registers
+    for (int i=0; i<REG_INPUT_NREGS; i++)
+         usRegInputBuf[i] = 0x0;
+    
+    /* Enable the Modbus Protocol Stack. */
+    if ( (eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 115200, MB_PAR_NONE )) !=  MB_ENOERR )
+        goto FAIL_MB;
+    else if ( (eStatus = eMBEnable(  ) ) != MB_ENOERR )
+        goto FAIL_MB_1;
+    else {
+        for( ;; )
+        {
+            if ( eMBPoll( ) != MB_ENOERR ) break;
+            Thread::wait(1);
+        }        
+    }    
+
+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;
+    
+    //printf("[%s %d] %d %d\r\n", __func__, __LINE__, usAddress, REG_INPUT_START);
+
+    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;
+    
+    //printf("[%s %d] Mode=%d usAddress=%d(%d) %d\r\n", __func__, __LINE__, eMode, usAddress, REG_INPUT_START, usNRegs);
+ 
+    usRegInputBuf[eData_MBInCounter]++;
+    usRegInputBuf[eData_MBOutCounter]++;
+        
+    if (eMode == MB_REG_READ)
+    {
+        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--;
+            }
+            light_leds(); // Control LEDs
+        }
+    }
+
+    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;
+}