enlarge functional simple ranging/interrupt example, to cater for multi-zone.

Dependencies:   X_NUCLEO_53L1A2

Revision:
2:25bcfa4b1aca
Parent:
1:c67af60ec906
Child:
3:09f23aad108a
--- a/main.cpp	Mon Nov 09 17:23:54 2020 +0000
+++ b/main.cpp	Wed May 12 14:30:46 2021 +0000
@@ -1,21 +1,42 @@
 /*
- *  This VL53L1X Expansion board test application performs range measurements
- *  using the onboard embedded centre sensor and two satelites, in multizone, interrupt mode.
- *  Measured ranges are ouput on the Serial Port, running at 115200 baud.
+ * This VL53L1CB Expansion board test application performs range measurements
+ * using the onboard embedded sensor and two satellites, in interrupt mode.
+ * Measured ranges are ouput on the Serial Port, running at 115200 baud.
+ *
+ * This is designed to work with MBed v2.x, & MBedOS v5.x / v6.x.
+ *
+ * The Reset button can be used to restart the program.
+ *
+ * *** NOTE :
+ *     Default Mbed build system settings disable printf() floating-point support.
+ *     Offline builds can enable this, again.
+ *     https://github.com/ARMmbed/mbed-os/blob/master/platform/source/minimal-printf/README.md
+ *     .\mbed-os\platform\mbed_lib.json
  *
- *
- * This is designed to work with MBed V2 , MBed V5 and MBed V6.
+ * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
+ *            the X-NUCELO-53L1A2 expansion board are not made/OFF.
+ *            These links must be made to allow interrupts from the Satellite boards
+ *            to be received.
+ *            U11 and U18 must be made/ON to allow interrupts to be received from the
+ *            INT_L & INT_R positions; or
+ *            U10 and U15 must be made/ON to allow interrupts to be received from the
+ *            Alternate INT_L & INT_R positions.
+ *            The X_NUCLEO_53L1A2 library defaults to use the INT_L/INT_R positions.
+ *            INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8.
+ *            Alternate INT_L is on CN5 Connector pin 2 as D9.
+ *            INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2.
+ *            Alternate INT_R is on CN9 Connector pin 5 as D4.
+ *            The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A2/
  *
- *
- *  The Reset button can be used to restart the program.
  */
  
 #include <stdio.h>
+#include <time.h>
 
 #include "mbed.h"
+
 #include "XNucleo53L1A2.h"
 #include "ToF_I2C.h"
-#include <time.h>
 
 
 // define the i2c comms pins
@@ -29,201 +50,177 @@
 // the satellite pins depend on solder blobs on the back of the shield.
 // they may not exist or may be one of two sets.
 // the centre pin always exists
-PinName LeftIntPin = D9;
-PinName RightIntPin = D4;
+//PinName LeftIntPin = D8;
+PinName RightIntPin = D2;
 // alternate set
-//PinName LeftIntPin = D8;
-//PinName RightIntPin = D2;
+PinName LeftIntPin = D9;
+//PinName RightIntPin = D4;
 
 
 static XNucleo53L1A2 *board=NULL;
 
 #if (MBED_VERSION  > 60300) 
-UnbufferedSerial  pc(SERIAL_TX, SERIAL_RX); 
+UnbufferedSerial  pc(USBTX, USBRX); 
 extern "C" void wait_ms(int ms);
 #else
 Serial pc(SERIAL_TX, SERIAL_RX); 
 #endif
 
-// flags to indicate an interrupt has happened
-static int int_centre_result = 0;
-static int int_left_result = 0;
-static int int_right_result = 0;
-
-// flags to indicate an interrupt has cleared
-static int int_centre_dropped = 0;
-static int int_left_dropped = 0;
-static int int_right_dropped = 0;
-
-// timing for debug purposes
-uint32_t  centre_polling_time_ms;
-uint32_t  centre_start_time_ms;
-uint32_t  centre_end_time_ms;
+#if TARGET_STM  // we are cross compiling for an STM32-Nucleo
+    InterruptIn stop_button(BUTTON1);
+#endif
+#if TARGET_Freescale // we are cross-compiling for NXP FRDM boards.
+    InterruptIn stop_button(SW2);
+#endif
 
 void process_interrupt( VL53L1 * sensor,VL53L1_DEV dev );
 void print_results( int devSpiNumber, VL53L1_MultiRangingData_t *pMultiRangingData );
 
 
+VL53L1_Dev_t devCentre;
+VL53L1_Dev_t devLeft;
+VL53L1_Dev_t devRight;
+VL53L1_DEV Dev = &devCentre;
 
-// this class services the interrupts from the ToF sensors.
-// There is a limited amount you can do in an interrupt routine; printfs,mutexes break them among other things.
-// We keep things simple by only raising a flag so all the real work is done outside the interrupt.
-// This is designed around MBED V2 which doesn't have the RTOS features that would make this work nicely e.g. semaphores/queues.
-class WaitForMeasurement {
-public:
+/* Installed sensors count */
+int sensorCnt = 0;
+
+/* installed sensors prefixes */
+char installedSensors[3];
+
+/* interrupt requests */
+volatile bool centerSensor = false;
+volatile bool leftSensor = false;
+volatile bool rightSensor = false;
+volatile bool int_measuring_stop = false;
+
+/* Current sensor number*/
+volatile int currentSensor = 0;
 
+/* current displayed sensor change IRQ */
+volatile bool switchChanged = false;
 
-WaitForMeasurement(): _interrupt(A1)
+/* ISR callback function of the centre sensor */
+void sensor_centre_irq(void)
 {
+    centerSensor = true;
+    board->sensor_centre->disable_interrupt_measure_detection_irq();
+}
+
+/* ISR callback function of the left sensor */
+void sensor_left_irq(void)
+{
+    leftSensor = true;
+    board->sensor_left->disable_interrupt_measure_detection_irq();
 }
 
+/* ISR callback function of the right sensor */
+void sensor_right_irq(void)
+{
+    rightSensor = true;
+    board->sensor_right->disable_interrupt_measure_detection_irq();
+}
 
-    // constructor - Sensor is not used and can be removed
-    WaitForMeasurement(PinName pin,VL53L1_DEV Dev) : _interrupt(pin)          // create the InterruptIn on the pin specified to Counter
-    {
-         Devlocal = Dev;
-         pinlocal = pin;
-         
-         #include "mbed.h"
- 
-         printf("WaitForMeasurement %d \n",Dev->i2c_slave_address);
-        _interrupt.rise(callback(this, &WaitForMeasurement::got_interrupt)); // if interrupt happens read data
-        _interrupt.fall(callback(this, &WaitForMeasurement::interruptdropped)); // if interupt clears, start next reading
-        
-    }
-    
-      // function is called every time an interupt is cleared. causes 
-    void interruptdropped()
-    {
+/* ISR callback function of the user blue button to switch measuring sensor. */
+void switch_measuring_sensor_irq(void)
+{
+    stop_button.disable_irq();
+    switchChanged = true;
+}
+
+/*
+ * This function calls the interrupt handler for each sensor
+ * and outputs the range
+ */
+inline void measure_sensors()
+{
+    int status = 0;
+    bool current = false;
+    uint16_t distance = 0;
+
+    /* Handle the interrupt and output the range from the centre sensor */
+    if (centerSensor) {
+        centerSensor = false;
+//        board->sensor_centre->handle_irq(&distance);
+        status = board->sensor_centre->VL53L1_GetDistance(&distance);
+        status = board->sensor_centre->VL53L1_ClearInterrupt();
+        board->sensor_centre->enable_interrupt_measure_detection_irq();
         
-        if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS)
-                int_centre_dropped = 1;  //flag to main that interrupt cleared. A flag is raised which allows the main routine to service interupt.
-        if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS)
-                int_left_dropped = 1;   //flag to main that interrupt cleared
-        if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS)
-                int_right_dropped = 1;  //flag to main that interrupt cleared
-        
-    }
-
-  // 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()
-    {
-        DigitalIn intp(pinlocal);
-        _count++;
-
-        if (Devlocal->i2c_slave_address == NEW_SENSOR_CENTRE_ADDRESS)
-                int_centre_result = 1;  //flag to main that interrupt happened
-        if (Devlocal->i2c_slave_address == NEW_SENSOR_LEFT_ADDRESS)
-                int_left_result = 1;   //flag to main that interrupt happened7
-        if (Devlocal->i2c_slave_address == NEW_SENSOR_RIGHT_ADDRESS)
-                int_right_result = 1;  //flag to main that interrupt happened
-        
-    }
-
-    
-    //destructor
-    ~WaitForMeasurement()
-    {
-        printf("WaitForMeasurement destruction \n");
+        current = (currentSensor == 0);
+        if (current) {
+            printf("Centre: %d\r\n", distance);
+        }
     }
 
-private:
-    InterruptIn _interrupt;
-    PinName pinlocal;
-    volatile int _count;
-    VL53L1_DEV Devlocal;
-    int status;
-    
-};
-
-
+    /* Handle the interrupt and output the range from the left sensor */
+    if (leftSensor) {
+        leftSensor = false;
+//        board->sensor_left->handle_irq(&distance);
+        status = board->sensor_left->VL53L1_GetDistance(&distance);
+        status = board->sensor_left->VL53L1_ClearInterrupt();
+        board->sensor_left->enable_interrupt_measure_detection_irq();
+        
+        current = (installedSensors[currentSensor] == 'L');
+        if (current) {
+            printf("Left: %d\r\n", distance);
+        }
+    }
 
-VL53L1_Dev_t                   devCentre;
-VL53L1_Dev_t                   devLeft;
-VL53L1_Dev_t                   devRight;
-VL53L1_DEV                     Dev = &devCentre;
+    /* Handle the interrupt and output the range from the right sensor */
+    if (rightSensor) {
+        rightSensor = false;
+//        board->sensor_right->handle_irq(&distance);
+        status = board->sensor_right->VL53L1_GetDistance(&distance);
+        status = board->sensor_right->VL53L1_ClearInterrupt();
+        board->sensor_right->enable_interrupt_measure_detection_irq();
+        
+        current = (installedSensors[currentSensor] == 'R');
+        if (current) {
+            printf("Right: %d\r\n", distance);
+        }
+    }    
+}
 
- 
-/*=================================== Main ==================================
-=============================================================================*/
-int main()
-{   
-    int status;
-    VL53L1 * Sensor;
-    uint16_t wordData;
+/*
+ * Add to an array a character that represents the sensor and start ranging
+ */
+int init_sensors_array()
+{
+    int status = 0;
+    VL53L1 *Sensor;
     uint8_t ToFSensor = 1; // 0=Left, 1=Center(default), 2=Right
 
-    
-    WaitForMeasurement* int2;
-    WaitForMeasurement* int1;
-    WaitForMeasurement* int3;
-
-    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
-
-    printf("main_interrupt_multizone \r\n");
-
-// create i2c interface
-    ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);
+    sensorCnt = 0;
     
-    dev_I2C->frequency(400000); //also needs doing in spi_interface.c
-    
-    /* creates the 53L1A2 expansion board singleton obj */
-    board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, RightIntPin, D2);
-    
-    printf("board created!\r\n");
-
-    /* init the 53L1A1 expansion board with default values */
-    status = board->init_board();
-    if (status) {
-        printf("Failed to init board!\r\n");
-        return 0;
+    if (board->sensor_centre != NULL) {
+        Dev = &devCentre;
+        Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
+        Sensor = board->sensor_centre;
+        printf("configuring centre channel \n");
     }
-       
-        
-    printf("board initiated! - %d\r\n", status);
-  // create the sensor controller classes for each sensor and initialise the sensors                                              
-    for (ToFSensor=0;ToFSensor< NUM_SENSORS;ToFSensor++){
-        switch(ToFSensor){
-            case 0:
-                if (board->sensor_centre== NULL ) continue;  // don't create if sensor not detected
-                Dev=&devCentre;
-                Sensor=board->sensor_centre;
-                Dev->i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
-                printf("configuring centre channel \n");
-                break;
-            case 1:
-                if (board->sensor_left== NULL ) continue;  // don't create if sensor not detected
-                Dev=&devLeft; 
-                Sensor=board->sensor_left;
-                Dev->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS;
-                printf("configuring left channel \n");
-                break;
-            case 2:
-                if (board->sensor_right== NULL ) continue;  // don't create if sensor not detected
-                Dev=&devRight;  
-                Sensor=board->sensor_right;
-                Dev->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS;
-                printf("configuring right channel \n");
-                break;      
-            default:
-               printf(" error in switch, invalid ToF sensor \n");
-        }  //end of switch
-        
-// configure the sensors
-        Dev->comms_speed_khz = 400;
-
-        Dev->comms_type = 1;
-
-/* Device Initialization and setting */  
+    
+    if (board->sensor_left != NULL) {
+        Dev = &devLeft; 
+        Dev->i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS;
+        Sensor = board->sensor_left;
+        printf("configuring left channel \n");
+    }
+    
+    if (board->sensor_right != NULL) {
+        Dev = &devRight;  
+        Dev->i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS;
+        Sensor = board->sensor_right;
+        printf("configuring right channel \n");
+    }
+/*
+// Device Initialization and setting 
         status = Sensor->vl53L1_DataInit();
         status = Sensor->vl53L1_StaticInit();
         status = Sensor->vl53L1_SetPresetMode( VL53L1_PRESETMODE_MULTIZONES_SCANNING);
-
-
    
    //configure the regions of interest for each sensor
         VL53L1_RoiConfig_t roiConfig;
-        roiConfig.NumberOfRoi =3;
+        roiConfig.NumberOfRoi = 3;
         roiConfig.UserRois[0].TopLeftX = 0;
         roiConfig.UserRois[0].TopLeftY = 9; 
         roiConfig.UserRois[0].BotRightX = 4;
@@ -241,36 +238,93 @@
         
         status = Sensor->vl53L1_SetDistanceMode( VL53L1_DISTANCEMODE_LONG);
  //       status = Sensor->VL53L1_SetMeasurementTimingBudgetMicroSeconds( 100 * 500); // error -21 is because of this. Don't know why
+*/ 
+  
+    
+    /* start the measure on the center sensor */
+    if (NULL != board->sensor_centre) {
+        installedSensors[sensorCnt] = 'C';
+        status = board->sensor_centre->stop_measurement();
+        if (status != 0) {
+            return status;
+        }
+        status = board->sensor_centre->start_measurement(&sensor_centre_irq);
+        if (status != 0) {
+            return status;
+        }
+        ++sensorCnt;
+    }
+    /* start the measure on the left sensor */
+    if (NULL != board->sensor_left) {
+        installedSensors[sensorCnt] = 'L';
+        status = board->sensor_left->stop_measurement();
+        if (status != 0) {
+            return status;
+        }
+        status = board->sensor_left->start_measurement(&sensor_left_irq);
+        if (status != 0) {
+            return status;
+        }
+        ++sensorCnt;
+    }
+    /* start the measure on the right sensor */
+    if (NULL != board->sensor_right) {
+        installedSensors[sensorCnt] = 'R';
+        status = board->sensor_right->stop_measurement();
+        if (status != 0) {
+            return status;
+        }
+        status = board->sensor_right->start_measurement(&sensor_right_irq);
+        if (status != 0) {
+            return status;
+        }
+        ++sensorCnt;
+    }
+    currentSensor = 0;
+    return status;
+}
+
+
+ 
+/*=================================== Main ==================================
+=============================================================================*/
+int main()
+{   
+    int status;
+    
+    stop_button.rise(&switch_measuring_sensor_irq);
+    stop_button.enable_irq();
+    
+//    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
+    
+    printf("mbed version : %d \r\n", MBED_VERSION);
+
+// create i2c interface
+    ToF_DevI2C *dev_I2C = new ToF_DevI2C(I2C_SDA, I2C_SCL);    
+    /* creates the 53L1A2 expansion board singleton obj */
+    board = XNucleo53L1A2::instance(dev_I2C, CentreIntPin, LeftIntPin, RightIntPin);
+    
+    printf("board created!\r\n");
+
+    /* init the 53L1A1 expansion board with default values */
+    status = board->init_board();
+    if (status) {
+        printf("Failed to init board!\r\n");
+        return status;
     }
         
-    // create interrupt handlers for the sensors that exist
-    if (board->sensor_centre!= NULL )
-    {
-        printf("starting interrupt centre\n");
-        devCentre.i2c_slave_address = NEW_SENSOR_CENTRE_ADDRESS;
-        int1 =  new WaitForMeasurement(CentreIntPin,&devCentre);  // create a interrupt class for this interrupt pin
-        status = board->sensor_centre->vl53L1_StartMeasurement(); //start the sensor measuring
-    }
+    printf("board initiated! - %d\r\n", status);
     
-    if (board->sensor_left!= NULL )
-    {
-        printf("starting interrupt left\n");
-        devLeft.i2c_slave_address = NEW_SENSOR_LEFT_ADDRESS;
-        int2 = new WaitForMeasurement(LeftIntPin,&devLeft); // create a interrupt class for this interrupt pin
-        status = board->sensor_left->vl53L1_StartMeasurement(); //start the sensor measuring
+    /* init an array with chars to id the sensors */
+    status = init_sensors_array();
+    if (status != 0) {
+        printf("Failed to init sensors!\r\n");
+        return status;
     }
 
-    if (board->sensor_right!= NULL )
-    {
-        printf("starting interrupt right\n");
-        devRight.i2c_slave_address = NEW_SENSOR_RIGHT_ADDRESS;
-        int3 = new WaitForMeasurement(RightIntPin,&devRight); // create a interrupt class for this interrupt pin
-        status = board->sensor_right->vl53L1_StartMeasurement();//start the sensor measuring
-    }
-        
-        
+    printf("loop forever\n");    
     
-
+/*
        // loop waiting for interrupts to happen. This is signaled by int_centre_result,int_left_result or int_right_result
        // being non zero. When the interrupts clear this is signaled by int_centre_dropped,int_left_dropped and int_right_dropped. 
        // These are set back to zero when processing is completed
@@ -342,6 +396,19 @@
         wait_ms( 1 * 5);
                               
     }
+*/
+    /* Main ranging interrupt loop */
+    while (true) {
+        measure_sensors();
+        if (switchChanged) {
+            ++currentSensor;
+            if (currentSensor == sensorCnt)
+                currentSensor = 0;
+            printf("Sensor changed to %c\r\n", installedSensors[currentSensor]);
+            switchChanged = false;
+            stop_button.enable_irq();
+        }
+    }  
 }