Complete interrupt based test application for the STMicrolectronics' X-NUCLEO-6180XA1 Proximity and ambient light sensor expansion board.

Dependencies:   X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1_AppExample by ST Expansion SW Team

Complete interrupt based test application for the STMicrolectronics' X-NUCLEO-6180XA1 Proximity and ambient light sensor expansion board.
The application reads the on board red switch and reports the measured ALS or range accordingly.
A demonstration of resources de-allocation and system restart is also provided by means of Blue Button press.

Files at this revision

API Documentation at this revision

Comitter:
Davidroid
Date:
Fri Mar 03 15:11:39 2017 +0000
Parent:
2:e8c6345e519d
Child:
4:84dfc00ae7b3
Commit message:
+ Added SW setting for F429ZI MCU board. This requires a HW patch, i.e. to solder Pin 6 of CN8 Arduino connector (A5).; + Some modifications and indentation corrected to fit mbed coding style.

Changed in this revision

X_NUCLEO_6180XA1.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- a/X_NUCLEO_6180XA1.lib	Wed Nov 30 08:34:36 2016 +0000
+++ b/X_NUCLEO_6180XA1.lib	Fri Mar 03 15:11:39 2017 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/teams/ST/code/X_NUCLEO_6180XA1/#17f3bb228b13
+http://developer.mbed.org/teams/ST/code/X_NUCLEO_6180XA1/#a37f407230ca
--- a/main.cpp	Wed Nov 30 08:34:36 2016 +0000
+++ b/main.cpp	Fri Mar 03 15:11:39 2017 +0000
@@ -14,11 +14,12 @@
    Reset button is used to restart the program. */
 
 /* Polling operating modes don`t require callback function that handles IRQ 
-   Callback IRQ functions are used only for measure that require interrupt */
+   Callback IRQ functions are used only for measure that require interrupt. */
 
 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value 
    is not ready to be read from the corresponding register. So you need to wait
-   for the result to be ready */
+   for the result to be ready. */
+
 
 #define VL6180X_I2C_SDA   D14 
 #define VL6180X_I2C_SCL   D15 
@@ -26,168 +27,176 @@
 #define RANGE   0
 #define ALS     1
 
-static X_NUCLEO_6180XA1 *board=NULL;
+#define DELAY 2000  // 2Sec
+
+
+/* Expansion board */
+static X_NUCLEO_6180XA1 *board = NULL;
+
+/* Measure data */
 MeasureData_t data_sensor_top;
+
+/* Operating mode */
 OperatingMode operating_mode, prev_operating_mode;
+enum OpModeIntPoll_t{ PollMeasure, IntMeasure };
     
-/* flags that handle interrupt request */
-bool int_sensor_top=false, int_stop_measure=false;  
+/* Flags that handle interrupt request */
+bool int_sensor_top = false, int_stop_measure = false;  
 
 /* ISR callback function of the sensor_top */
 void SensorTopIRQ(void)
 {
-   int_sensor_top=true;
-   board->sensor_top->DisableInterruptMeasureDetectionIRQ();
-}   
+    int_sensor_top = true;
+    board->sensor_top->DisableInterruptMeasureDetectionIRQ();
+}
 
 /* ISR callback function of the user blue button to stop program */
 void StopMeasureIRQ(void)
 {
-   int_stop_measure=true;
+    int_stop_measure = true;
 }
 
 /* On board 4 digit local display refresh */
 void DisplayRefresh(OperatingMode op_mode)
-{   
-   char str[5];
-   
-   if(op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
-   {
-      if(data_sensor_top.range_mm!=0xFFFFFFFF)
-      {
-         sprintf(str,"%d",data_sensor_top.range_mm);
-      }
-      else
-      {
-         sprintf(str,"%s","----");
-      }
-   }
-   else if(op_mode==als_continuous_interrupt || op_mode==als_continuous_polling)
-   {
-      if(data_sensor_top.lux!=0xFFFFFFFF)
-      {
-         sprintf(str,"%d",data_sensor_top.lux);
-      }
-      else
-      {
-         sprintf(str,"%s","----");
-      }
-   }
-   board->display->DisplayString(str, strlen(str));       
+{
+    char str[5];
+
+    if (op_mode==range_continuous_interrupt || op_mode==range_continuous_polling) {
+        if (data_sensor_top.range_mm!=0xFFFFFFFF) {
+            sprintf(str,"%d",data_sensor_top.range_mm);
+        } else {
+            sprintf(str,"%s","----");
+        }
+    } else if (op_mode==als_continuous_interrupt || op_mode==als_continuous_polling) {
+        if (data_sensor_top.lux!=0xFFFFFFFF) {
+            sprintf(str,"%d",data_sensor_top.lux);
+        } else {
+            sprintf(str,"%s","----");
+        }
+    }
+    board->display->DisplayString(str, strlen(str));       
 }
 
 /* On board red slider position check */
-enum OpModeIntPoll_t{ PollMeasure, IntMeasure };
-
-OperatingMode CheckSlider(enum OpModeIntPoll_t OpMode) {
-    
-OperatingMode ret;
-int measure= board->RdSwitch();
+OperatingMode CheckSlider(enum OpModeIntPoll_t OpMode)
+{
+    OperatingMode ret;
+    int measure = board->RdSwitch();
 
-   switch (OpMode) {
-    case PollMeasure:
-      if(measure==RANGE)
-        ret = range_continuous_polling;
-      else if(measure==ALS)
-        ret = als_continuous_polling;           
-    break;
-    
-    case IntMeasure:
-      if(measure==RANGE)
-        ret = range_continuous_interrupt;
-      else if(measure==ALS)
-        ret = als_continuous_interrupt;     
-    break;
-   }
-     return ret;      
+    switch (OpMode) {
+        case PollMeasure:
+            if (measure==RANGE) {
+                ret = range_continuous_polling;
+            } else if (measure==ALS) {
+                ret = als_continuous_polling;
+            }
+            break;
+        case IntMeasure:
+            if (measure==RANGE) {
+                ret = range_continuous_interrupt;
+            } else if (measure==ALS) {
+                ret = als_continuous_interrupt;
+            }
+            break;
+    }
+    return ret;      
 }
 
 /* Print on USB Serial the started OperatingMode */
 void PrintStartMessage(OperatingMode op_mode)
 {
-   if(op_mode==range_continuous_interrupt)
-      printf("\nStarted range continuous interrupt measure\n\r");
-   else if(prev_operating_mode==als_continuous_interrupt)
-      printf("\nStarted als continuous interrupt measure\n\r");
+    if (op_mode==range_continuous_interrupt) {
+        printf("\nStarted range continuous interrupt measure\n\r");
+    } else if (prev_operating_mode==als_continuous_interrupt) {
+        printf("\nStarted als continuous interrupt measure\n\r");
+    }
 }
 
 /* Print on USB Serial the stopped OperatingMode */
 void PrintStopMessage(OperatingMode op_mode)
 {
-   if(op_mode==range_continuous_interrupt)
-      printf("Stopped range continuous interrupt measure\n\r");
-   else if(prev_operating_mode==als_continuous_interrupt)
-      printf("Stopped als continuous interrupt measure\n\r");
+    if (op_mode==range_continuous_interrupt) {
+        printf("Stopped range continuous interrupt measure\n\r");
+    } else if (prev_operating_mode==als_continuous_interrupt) {
+        printf("Stopped als continuous interrupt measure\n\r");
+    }
 }
 
 /* Print on board 4 Digit display the indicated message <= 4 char */
-#define DELAY 2000  // 2Sec
 void DisplayMsg(const char * msg)
 {
-   Timer timer;
-   char str[5];
-   
-   timer.start();
-   for(int i=0; i<DELAY; i=timer.read_ms())
-   {
-      sprintf(str,"%s",msg);
-      board->display->DisplayString(str, strlen(str));
-   }
-   timer.stop();
+    Timer timer;
+    char str[5];
+
+    timer.start();
+    for (int i=0; i<DELAY; i=timer.read_ms())
+    {
+        sprintf(str,"%s",msg);
+        board->display->DisplayString(str, strlen(str));
+    }
+    timer.stop();
 }
 
-
+/* Handle continuous ALS or Range measurement. */
 void IntContinousALSorRangeMeasure (DevI2C *device_i2c) {
-   int status;   
-   /* creates the 6180XA1 expansion board singleton obj */
-   board=X_NUCLEO_6180XA1::Instance(device_i2c, A3, A2, D13, D2);
-   DisplayMsg  ("INT");
-   /* init the 6180XA1 expansion board with default values */
-   status=board->InitBoard();
-   if(status)
-      printf("Failed to init board!\n\r");   
-   /* check the red slider position for ALS/Range measure */
-   operating_mode=CheckSlider(IntMeasure);   
-   /* start the measure on sensor top */
-   status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
-   if(!status)
-   {
-      prev_operating_mode=operating_mode;
-      PrintStartMessage(operating_mode);
-      while(1)
-      {
-         if(int_sensor_top)  /* 6180 isr was triggered */
-         {
-            int_sensor_top=false;
-            status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); /* handle the isr and read the meaure */
-            DisplayRefresh(operating_mode);
-         }
-         if(int_stop_measure) /* Blue Button isr was triggered */
-         {
-            status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the measure and exit */
-            if(!status)
-               PrintStopMessage(prev_operating_mode);
-            int_stop_measure = false;
-            printf("\nProgram stopped!\n\n\r");
-            break;
-         }
-         operating_mode=CheckSlider(IntMeasure); /* check if red slider was moved */
-         if(operating_mode!=prev_operating_mode)
-         {
-            DisplayRefresh(prev_operating_mode);
-            status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the running measure */
-            if(!status)
-               PrintStopMessage(prev_operating_mode);
-            prev_operating_mode=operating_mode;
-            status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); /* start the new measure */
-            if(!status)
-               PrintStartMessage(operating_mode);
-         } else
-            DisplayRefresh(operating_mode);           
-      }
-   }
-   DisplayMsg("BYE");
-}    
+    int status;
+
+    /* Creates the 6180XA1 expansion board singleton obj */
+#ifdef TARGET_STM32F429
+    board = X_NUCLEO_6180XA1::Instance(device_i2c, A5, A2, D13, D2);
+#else
+    board = X_NUCLEO_6180XA1::Instance(device_i2c, A3, A2, D13, D2);
+#endif
+    DisplayMsg("INT");
+    
+    /* Init the 6180XA1 expansion board with default values */
+    status = board->InitBoard();
+    if (status) {
+        printf("Failed to init board!\n\r");
+    }
+
+    /* Check the red slider position for ALS/Range measure */
+    operating_mode=CheckSlider(IntMeasure);
+
+    /* Start the measure on sensor top */
+    status = board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
+    if (!status) {
+        prev_operating_mode=operating_mode;
+        PrintStartMessage(operating_mode);
+        while (true) {
+            if (int_sensor_top) { /* 6180 isr was triggered */
+                int_sensor_top = false;
+                status = board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); /* handle the isr and read the meaure */
+                DisplayRefresh(operating_mode);
+            }
+            if (int_stop_measure) { /* Blue Button isr was triggered */
+                status = board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the measure and exit */
+                if (!status) {
+                    PrintStopMessage(prev_operating_mode);
+                }
+                int_stop_measure = false;
+                printf("\nProgram stopped!\n\n\r");
+                break;
+            }
+            operating_mode = CheckSlider(IntMeasure); /* check if red slider was moved */
+            if (operating_mode!=prev_operating_mode) {
+                DisplayRefresh(prev_operating_mode);
+                status = board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the running measure */
+                if (!status) {
+                    PrintStopMessage(prev_operating_mode);
+                }
+                prev_operating_mode = operating_mode;
+                status = board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); /* start the new measure */
+                if (!status) {
+                    PrintStartMessage(operating_mode);
+                }
+            } else {
+                DisplayRefresh(operating_mode);
+            }
+        }
+    }
+    DisplayMsg("BYE");
+}
 
 /*=================================== Main ==================================
  Move the VL6180X Expansion board red slider to switch between ALS or Range
@@ -196,12 +205,13 @@
 =============================================================================*/
 int main()
 {   
-#if USER_BUTTON==PC_13  // we are cross compiling for Nucleo-f401 
-   InterruptIn stop_button (USER_BUTTON);
-   stop_button.rise (&StopMeasureIRQ);  
+#if USER_BUTTON==PC_13  // Cross compiling for Nucleo-F401
+    InterruptIn stop_button (USER_BUTTON);
+    stop_button.rise (&StopMeasureIRQ);  
 #endif   
-   DevI2C *device_i2c =new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);     
-        
-   IntContinousALSorRangeMeasure (device_i2c);  // start continous measures Interrupt based
+    DevI2C *device_i2c = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);     
+
+    /* Start continous measures Interrupt based */
+    IntContinousALSorRangeMeasure (device_i2c);
 }
 
--- a/mbed.bld	Wed Nov 30 08:34:36 2016 +0000
+++ b/mbed.bld	Fri Mar 03 15:11:39 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/2e9cc70d1897
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/e1686b8d5b90
\ No newline at end of file