ST Expansion SW Team / Mbed OS VL6180_MB63_4sensors_interrupts

Dependencies:   X_NUCLEO_6180

Revision:
1:3cef7d50cab4
Parent:
0:e0163b3111e0
--- a/main.cpp	Wed Oct 28 16:21:19 2020 +0000
+++ b/main.cpp	Mon May 03 10:57:15 2021 +0000
@@ -1,11 +1,11 @@
 /**
   ******************************************************************************
   * File Name          : main.c
-  * Date               : 21/10/2014 09:21:14
+  * Date               : 21/10/2020 09:21:14
   * Description        : Main program body
   ******************************************************************************
   *
-  * COPYRIGHT(c) 2014 STMicroelectronics
+  * COPYRIGHT(c) 2020 STMicroelectronics
   *
   * Redistribution and use in source and binary forms, with or without modification,
   * are permitted provided that the following conditions are met:
@@ -56,16 +56,17 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
 
 #include "mbed.h"
 #include "XNucleo6810.h"
 #include <time.h>
 
 #include "spi_interface.h"
-
-
-/* Private variables ---------------------------------------------------------*/
-
+#include "vl6180_api.h"
+#include "6180a1.h"
 
 
 //VL6180_SINGLE_DEVICE_DRIVER
@@ -74,41 +75,33 @@
 #define I2C_SDA   D14 
 #define I2C_SCL   D15 
 
-/* USER CODE BEGIN 0 */
-#include <string.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#include "vl6180_api.h"
-#include "6180a1.h"
-
-#ifdef DEBUG
-    //TODO
-#include "diag/trace.h"
-    #define debug(msg, ...)   trace_printf(msg,__VA_ARGS__)
-    #define trace_warn(msg,...) trace_printf("W %s %d" msg "\n", __func__, __LINE__, __VA_ARGS__)
-#else
-    #define debug(msg, ...)  (void)0
-#endif
-
 #define DigitDisplay_ms     1 /* ms each digit is kept on */
 
+#define NUM_SENSORS 4
+
+// Define interrupt pins
+// The interrupt pins depend on solder blobs on the back of the shield.
+// Each interupt can have two possible pins, this allows all interrupts to be on the same gpio or different gpio
+// The first values are those an unmodified board has
+// see ST document UM2657 for more details
+PinName CentreIntPin = A3;
+PinName LeftIntPin = D13;
+PinName RightIntPin = D2;
+PinName BottomIntPin = A2;
+// alternate set
+//PinName CentreIntPin = A5;
+//PinName LeftIntPin = D8;
+//PinName RightIntPin = D4;
+//PinName BottomIntPin = A4;
 
 
-
-
+// flags that indicate an interrupt has occured
 static int int_centre_result = 0;
 static int int_left_result = 0;
 static int int_right_result = 0;
 static int int_bottom_result = 0;
 static int int_result = 0;
 
-
-// timing for debug purposes
-uint32_t  centre_polling_time_ms;
-uint32_t  centre_start_time_ms;
-uint32_t  centre_end_time_ms;
-
 void start_sensor(VL6180Dev_t  dev ,VL6180 *sensor);
 int get_sensor_data(VL6180Dev_t  dev ,VL6180 *sensor); 
 
@@ -127,36 +120,26 @@
 
 
     // constructor - Sensor is not used and can be removed
-    WaitForMeasurement(PinName pin,MyVL6180Dev_t Dev) : _interrupt(pin)          // create the InterruptIn on the pin specified to Counter
+    WaitForMeasurement(PinName pin,int i2c_address) : _interrupt(pin)          // create the InterruptIn on the pin specified to Counter
     {
          int_result = 1;
-         Devlocal = Dev;
+         i2c_addr = i2c_address;
         _interrupt.rise(callback(this, &WaitForMeasurement::got_interrupt)); // attach increment function of this counter instance
         
     }
-    
-    void process_right_interrupt()
-    {
-          printf("processing right interrupt\n");
-    }
 
   // function is called every time an interupt is seen. A flag is raised which allows the main routine to service the interupt.
     void got_interrupt()
     {
-    
-        _count++;
-
-        if (Devlocal.i2c_addr == NEW_SENSOR_CENTRE_ADDRESS)
+        if (i2c_addr == NEW_SENSOR_CENTRE_ADDRESS)
             int_centre_result = 1;  //flag to main that interrupt happened
-        if (Devlocal.i2c_addr == NEW_SENSOR_LEFT_ADDRESS)
+        if (i2c_addr == NEW_SENSOR_LEFT_ADDRESS)
             int_left_result = 1;   //flag to main that interrupt happened
-        if (Devlocal.i2c_addr == NEW_SENSOR_RIGHT_ADDRESS)
-        {
+        if (i2c_addr == NEW_SENSOR_RIGHT_ADDRESS)
             int_right_result = 1;  //flag to main that interrupt happened
-        }
-        if (Devlocal.i2c_addr == NEW_SENSOR_BOTTOM_ADDRESS)
+        if (i2c_addr == NEW_SENSOR_BOTTOM_ADDRESS)
             int_bottom_result = 1;   //flag to main that interrupt happened
-        }
+    }
     
     //destructor
     ~WaitForMeasurement()
@@ -166,9 +149,8 @@
 
 private:
     InterruptIn _interrupt;
-    volatile int _count;
-    MyVL6180Dev_t Devlocal;
     int status;
+    int i2c_addr;
     
 };
 
@@ -187,106 +169,28 @@
 
 static XNucleo53L1A1 *board=NULL;
 
+// MBed V6.4 has renamed wait_ms and UnbufferedSerial replaces Serial
+#if (MBED_VERSION  > 60300) 
+UnbufferedSerial  pc(SERIAL_TX, SERIAL_RX); 
+extern "C" void wait_ms(int ms);
+#else
 Serial pc(SERIAL_TX, SERIAL_RX); 
+#endif
 
 vl6180_DevI2C *dev_I2C = new vl6180_DevI2C(I2C_SDA, I2C_SCL);
 
-//void WaitMilliSec(int ms);
 
-/**
- * VL6180x CubeMX F401 i2c porting implementation
- */
-
-#define theVL6180Dev   0x52    // what we use as "API device
+#define theVL6180Dev   0x52    //the address of a sensor on power up
 
 #define i2c_bus      (&hi2c1)
 #define def_i2c_time_out 100
 
-
-void XNUCLEO6180XA1_WaitMilliSec(int n){
-    wait_ms(n);
-}
-
-
-
-
-/**
- * DISPLAY public
- */
-/***************  DISPLAY PUBLIC *********************/
-/***************  DISPLAY PRIVATE *********************/
-static char DISP_CurString[10];
-
-/**
- * call in the main loop
- * when running under debugger it enable doing direct vl6180x reg access
- * typcilay breaking at entrance
- * change  the the local index/data and cmd variable to do what needed
- * reg_cmd -1 wr byte -2wr word -3 wr dword
- * reg_cmd 1 rd byte 2 rd word 3 rd dword
- * step to last statement before return and read variable to get rd result exit
- */
- 
-void debug_stuff(void) {
-    int reg_cmd = 0;
-    static uint32_t reg_data;
-    static uint16_t reg_index;
+static char DISP_CurString[10]; // used to store what is to be displayed on the display
 
-    if (reg_cmd) {
-        switch (reg_cmd) {
-        case -1:
-            VL6180_WrByte(Dev, reg_index, reg_data);
-            debug("Wr B 0x%X = %d", reg_index, (int)reg_data);
-            break;
-        case -2:
-            VL6180_WrWord(Dev, reg_index, reg_data);
-            debug("Wr W 0x%X = %d", reg_index,(int) reg_data);
-            break;
-
-        case -3:
-            VL6180_WrDWord(Dev, reg_index, reg_data);
-            debug("WrDW 0x%X = %d", reg_index, (int)reg_data);
-            break;
-
-        case 1:
-            reg_data=0;
-            VL6180_RdByte(Dev, reg_index, (uint8_t*)&reg_data);
-            debug("RD B 0x%X = %d", reg_index, (int)reg_data);
-            break;
-        case 2:
-            reg_data=0;
-            VL6180_RdWord(Dev, reg_index, (uint16_t*)&reg_data);
-            debug("RD W 0x%X = %d", reg_index, (int)reg_data);
-            break;
-
-        case 3:
-            VL6180_RdDWord(Dev, reg_index, &reg_data);
-            debug("RD DW 0x%X = %d", reg_index, (int)reg_data);
-            break;
-        default:
-            debug("Invalid command %d", reg_cmd);
-            /* nothing to do*/
-            ;
-        }
-    }
-}
-
-
-volatile int VL6180_IsrFired=0;
-
-
-char buffer[10];
-
-
-uint32_t TimeStarted;       /* various display and mode delay starting time */
 VL6180_RangeData_t Range;  /* Range measurmeent  */
 uint16_t range;             /* range average distance */
 
 
-/* USER CODE END 0 */
-
-/* Private function prototypes -----------------------------------------------*/
-
 int main(void)
 {
     
@@ -296,18 +200,16 @@
     WaitForMeasurement* int4; // the interrupt handler
 
     pc.baud(115200);  // baud rate is important as printf statements take a lot of time
-
+    
+    printf("Interrupt 4 sensors mbed = %d \r\n",MBED_VERSION);
 
       //create I2C channel
     vl6180_DevI2C *dev_I2C = new vl6180_DevI2C(I2C_SDA, I2C_SCL);
-    
-    dev_I2C->frequency(400000); //also needs doing in spi_interface.c
-    devCentre.i2c_addr = theVL6180Dev;  // set to default adress for now
 
     int status;
     
     // create instances for the sensors
-    board = XNucleo53L1A1::instance(dev_I2C, A3, D13, D2 ,A2);      
+    board = XNucleo53L1A1::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin ,BottomIntPin);      
         // find the sensors we have and initialise
     status = board->init_board();
     if (status) {
@@ -319,41 +221,34 @@
     //select centre sensor
     if (board->sensor_centre != NULL ) {
         devCentre.i2c_addr = NEW_SENSOR_CENTRE_ADDRESS;
-        Dev = &devCentre;
-        Sensor=board->sensor_centre;
-        int1 =  new WaitForMeasurement(A3,devCentre);  // create a interrupt class for this interrupt pin
-        start_sensor(Dev ,Sensor);
+        int1 =  new WaitForMeasurement(CentreIntPin,NEW_SENSOR_CENTRE_ADDRESS);  // create a interrupt class for this interrupt pin
+        start_sensor(&devCentre ,board->sensor_centre);
     }
     
     if (board->sensor_left != NULL ) {
         devLeft.i2c_addr = NEW_SENSOR_LEFT_ADDRESS;
-        Dev = &devLeft;
-        Sensor=board->sensor_left;
-        int2 =  new WaitForMeasurement(D13,devLeft);  // create a interrupt class for this interrupt pin
-        start_sensor(Dev ,Sensor);
+        int2 =  new WaitForMeasurement(LeftIntPin,NEW_SENSOR_LEFT_ADDRESS);  // create a interrupt class for this interrupt pin
+        start_sensor(&devLeft ,board->sensor_left);
     }
     
     if (board->sensor_right != NULL ) {    
         devRight.i2c_addr = NEW_SENSOR_RIGHT_ADDRESS;
-        Dev = &devRight;
-        Sensor=board->sensor_right;
-        int2 =  new WaitForMeasurement(D2,devRight);  // create a interrupt class for this interrupt pin
-        start_sensor(Dev ,Sensor);
+        int2 =  new WaitForMeasurement(RightIntPin,NEW_SENSOR_RIGHT_ADDRESS);  // create a interrupt class for this interrupt pin
+        start_sensor(&devRight ,board->sensor_right);
     }
     
-    if (board->sensor_bottom != NULL ) {    
+    if (board->sensor_bottom != NULL ) {   
         devBottom.i2c_addr = NEW_SENSOR_BOTTOM_ADDRESS;
-        Dev = &devBottom;
-        Sensor=board->sensor_bottom;
-        int2 =  new WaitForMeasurement(A2,devBottom);  // create a interrupt class for this interrupt pin
-        start_sensor(Dev ,Sensor);
+        int2 =  new WaitForMeasurement(BottomIntPin,NEW_SENSOR_BOTTOM_ADDRESS);  // create a interrupt class for this interrupt pin
+        start_sensor(&devBottom ,board->sensor_bottom);
     }
 
 
     
     /* Infinite loop */
     while (1) {
-               
+           
+           // process interrupts    
         if ( int_centre_result == 1 )
         {
             int_centre_result = 0;  //clear interrupt flag
@@ -409,12 +304,11 @@
            }
        }
 
-//        debug_stuff();
     }
-
 }
 
 
+// start a sensor
 void start_sensor(VL6180Dev_t  dev ,VL6180 *sensor)
 {
     
@@ -471,8 +365,7 @@
             return result;
             }
         
-        // only display the centre sensors values
-
+        // only display the centre sensors values. All sensors values and printed out
        if((Range.errorStatus == 0) && (status == 0))
        {
             // only display the centre sensors values
@@ -506,4 +399,13 @@
 }
 
 
+ // wait_ms was removed in MBed V6.4          
+#if (MBED_VERSION  > 60300) 
+void wait_ms(int ms)
+ {
+    thread_sleep_for(ms);
+ }
+#endif
+
+
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/