Sample application using interrupts, and range_continuous_interrupts mode, to receive range data from the on-board and satellite sensors. Results are displayed on the on-board, 4 digit display and on the COM port.

Dependencies:   mbed X_NUCLEO_53L0A1

Fork of Display_53L0A1_Interrupts by ST

* NOTE : Hard-links U11 and U18, on the underside of the X-NUCELO-53L0A1 expansion board, must be made/ON to allow interrupts to be received from the satellite boards, on INT_L and INT_R, or U10 and U15 to receive interrupts from the alternate locations. *

Committer:
johnAlexander
Date:
Tue Jun 20 08:14:49 2017 +0000
Revision:
4:44629a30d6f4
Parent:
3:12cb106044f9
Child:
5:906aa7aede10
Refactored example code and updated comment section to mention expansion board hardware configuration needs.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 0:cd8e818d4288 1 #include "mbed.h"
johnAlexander 0:cd8e818d4288 2 #include "x_nucleo_53l0a1.h"
johnAlexander 2:bdf097d93eca 3 #include <string.h>
johnAlexander 2:bdf097d93eca 4 #include <stdlib.h>
johnAlexander 2:bdf097d93eca 5 #include <stdio.h>
johnAlexander 2:bdf097d93eca 6 #include <assert.h>
johnAlexander 2:bdf097d93eca 7
johnAlexander 3:12cb106044f9 8 /*
johnAlexander 3:12cb106044f9 9 * This VL53L0X Expansion board sample application performs range measurements using
johnAlexander 3:12cb106044f9 10 * range_continuous_interrupt mode to generate a hardware interrupt each time a new
johnAlexander 3:12cb106044f9 11 * measurement is ready to be read.
johnAlexander 4:44629a30d6f4 12 * The application supports the centre, on-board, sensor and up two satellite boards.
johnAlexander 3:12cb106044f9 13 *
johnAlexander 4:44629a30d6f4 14 * The measured range data is displayed on the on-board 4-digit LED display.
johnAlexander 3:12cb106044f9 15 *
johnAlexander 3:12cb106044f9 16 * The User Blue button switches between the currently selected sensor to display range
johnAlexander 3:12cb106044f9 17 * results from.
johnAlexander 3:12cb106044f9 18 *
johnAlexander 3:12cb106044f9 19 * The Black Reset button is used to restart the program.
johnAlexander 4:44629a30d6f4 20 *
johnAlexander 4:44629a30d6f4 21 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
johnAlexander 4:44629a30d6f4 22 * the X-NUCELO-53L0A1 expansion board are not made/OFF.
johnAlexander 4:44629a30d6f4 23 * These links must be made to allow interrupts from the Satellite boards
johnAlexander 4:44629a30d6f4 24 * to be received.
johnAlexander 4:44629a30d6f4 25 * U11 and U18 must be made/ON to allow interrupts to be received from the
johnAlexander 4:44629a30d6f4 26 * INT_L & INT_R positions; or
johnAlexander 4:44629a30d6f4 27 * U10 and U15 must be made/ON to allow interrupts to be received from the
johnAlexander 4:44629a30d6f4 28 * Alternate INT_L & INT_R positions.
johnAlexander 4:44629a30d6f4 29 * The X_NUCLEO_53L0A1 firmware library defaults to use the INT_L/INT_R
johnAlexander 4:44629a30d6f4 30 * positions.
johnAlexander 4:44629a30d6f4 31 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D8.
johnAlexander 4:44629a30d6f4 32 * Alternate INT_L is on CN5 Connector pin 2 as D9.
johnAlexander 4:44629a30d6f4 33 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D2.
johnAlexander 4:44629a30d6f4 34 * Alternate INT_R is on CN9 Connector pin 5 as D4.
johnAlexander 4:44629a30d6f4 35 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L0A1/
johnAlexander 4:44629a30d6f4 36 *
johnAlexander 3:12cb106044f9 37 */
johnAlexander 2:bdf097d93eca 38
johnAlexander 0:cd8e818d4288 39 #define VL53L0_I2C_SDA D14
johnAlexander 0:cd8e818d4288 40 #define VL53L0_I2C_SCL D15
johnAlexander 0:cd8e818d4288 41
johnAlexander 4:44629a30d6f4 42 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-64s
johnAlexander 4:44629a30d6f4 43 InterruptIn stop_button (USER_BUTTON);
johnAlexander 4:44629a30d6f4 44 #endif
johnAlexander 3:12cb106044f9 45
johnAlexander 2:bdf097d93eca 46 static X_NUCLEO_53L0A1 *board=NULL;
johnAlexander 2:bdf097d93eca 47 VL53L0X_RangingMeasurementData_t data_sensor;
johnAlexander 0:cd8e818d4288 48 OperatingMode operating_mode;
johnAlexander 4:44629a30d6f4 49
johnAlexander 4:44629a30d6f4 50 /* current displayed sensor change IRQ */
johnAlexander 4:44629a30d6f4 51 volatile bool switchChanged = false;
johnAlexander 4:44629a30d6f4 52
johnAlexander 3:12cb106044f9 53 /* interrupt requests */
johnAlexander 4:44629a30d6f4 54 volatile bool centerSensor = false;
johnAlexander 4:44629a30d6f4 55 volatile bool leftSensor = false;
johnAlexander 4:44629a30d6f4 56 volatile bool rightSensor = false;
johnAlexander 3:12cb106044f9 57
johnAlexander 3:12cb106044f9 58 /* Current sensor number*/
johnAlexander 3:12cb106044f9 59 volatile int currentSensor = 0;
johnAlexander 3:12cb106044f9 60 /* Installed sensors count */
johnAlexander 3:12cb106044f9 61 int sensorCnt = 0;
johnAlexander 3:12cb106044f9 62
johnAlexander 4:44629a30d6f4 63 /* installed sensors prefixes */
johnAlexander 4:44629a30d6f4 64 char installedSensors[3];
johnAlexander 0:cd8e818d4288 65
johnAlexander 3:12cb106044f9 66 /* ISR callback function of the sensor_centre */
johnAlexander 2:bdf097d93eca 67 void SensorCenterIRQ(void)
johnAlexander 0:cd8e818d4288 68 {
johnAlexander 4:44629a30d6f4 69 centerSensor = true;
johnAlexander 2:bdf097d93eca 70 board->sensor_centre->DisableInterruptMeasureDetectionIRQ();
johnAlexander 2:bdf097d93eca 71 }
johnAlexander 2:bdf097d93eca 72
johnAlexander 3:12cb106044f9 73 void SensorLeftIRQ(void)
johnAlexander 3:12cb106044f9 74 {
johnAlexander 4:44629a30d6f4 75 leftSensor = true;
johnAlexander 3:12cb106044f9 76 board->sensor_left->DisableInterruptMeasureDetectionIRQ();
johnAlexander 3:12cb106044f9 77 }
johnAlexander 3:12cb106044f9 78
johnAlexander 3:12cb106044f9 79 void SensorRightIRQ(void)
johnAlexander 4:44629a30d6f4 80 {
johnAlexander 4:44629a30d6f4 81 rightSensor = true;
johnAlexander 3:12cb106044f9 82 board->sensor_right->DisableInterruptMeasureDetectionIRQ();
johnAlexander 3:12cb106044f9 83 }
johnAlexander 3:12cb106044f9 84
johnAlexander 2:bdf097d93eca 85 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 3:12cb106044f9 86 void SwitchMeasuringSensorIRQ(void)
johnAlexander 2:bdf097d93eca 87 {
johnAlexander 4:44629a30d6f4 88 stop_button.disable_irq();
johnAlexander 4:44629a30d6f4 89 switchChanged = true;
johnAlexander 0:cd8e818d4288 90 }
johnAlexander 0:cd8e818d4288 91
johnAlexander 4:44629a30d6f4 92
johnAlexander 0:cd8e818d4288 93 /* On board 4 digit local display refresh */
johnAlexander 4:44629a30d6f4 94 void RefreshDisplay(const VL53L0X_RangingMeasurementData_t &data, char prefix)
johnAlexander 4:44629a30d6f4 95 {
johnAlexander 4:44629a30d6f4 96 static char str[5];
johnAlexander 4:44629a30d6f4 97 if (data_sensor.RangeStatus == 0) // we have a valid range.
johnAlexander 4:44629a30d6f4 98 {
johnAlexander 4:44629a30d6f4 99 sprintf(str, "%c%3d", prefix ,data.RangeMilliMeter);
johnAlexander 4:44629a30d6f4 100 board->display->DisplayString(str);
johnAlexander 4:44629a30d6f4 101 }
johnAlexander 4:44629a30d6f4 102 else
johnAlexander 2:bdf097d93eca 103 {
johnAlexander 4:44629a30d6f4 104 sprintf(str, "%c%s", prefix, "---");
johnAlexander 4:44629a30d6f4 105 board->display->DisplayString(str);
johnAlexander 4:44629a30d6f4 106 }
johnAlexander 4:44629a30d6f4 107 }
johnAlexander 4:44629a30d6f4 108
johnAlexander 4:44629a30d6f4 109 inline void MeasureSensors(OperatingMode op_mode)
johnAlexander 4:44629a30d6f4 110 {
johnAlexander 4:44629a30d6f4 111 bool current = false;
johnAlexander 4:44629a30d6f4 112 if (centerSensor)
johnAlexander 4:44629a30d6f4 113 {
johnAlexander 4:44629a30d6f4 114 centerSensor = false;
johnAlexander 4:44629a30d6f4 115 board->sensor_centre->HandleIRQ(op_mode, &data_sensor);
johnAlexander 4:44629a30d6f4 116 current = (currentSensor == 0);
johnAlexander 4:44629a30d6f4 117 if (current)
johnAlexander 3:12cb106044f9 118 {
johnAlexander 4:44629a30d6f4 119 RefreshDisplay(data_sensor, 'C');
johnAlexander 3:12cb106044f9 120 }
johnAlexander 2:bdf097d93eca 121 }
johnAlexander 4:44629a30d6f4 122 if (leftSensor)
johnAlexander 4:44629a30d6f4 123 {
johnAlexander 4:44629a30d6f4 124 leftSensor = false;
johnAlexander 4:44629a30d6f4 125 board->sensor_left->HandleIRQ(op_mode, &data_sensor);
johnAlexander 4:44629a30d6f4 126 current = (installedSensors[currentSensor] == 'L');
johnAlexander 4:44629a30d6f4 127 if (current)
johnAlexander 4:44629a30d6f4 128 {
johnAlexander 4:44629a30d6f4 129 RefreshDisplay(data_sensor, 'L');
johnAlexander 4:44629a30d6f4 130 }
johnAlexander 4:44629a30d6f4 131 }
johnAlexander 4:44629a30d6f4 132 if (rightSensor)
johnAlexander 4:44629a30d6f4 133 {
johnAlexander 4:44629a30d6f4 134 rightSensor = false;
johnAlexander 4:44629a30d6f4 135 board->sensor_right->HandleIRQ(op_mode, &data_sensor);
johnAlexander 4:44629a30d6f4 136 current = (installedSensors[currentSensor] == 'R');
johnAlexander 4:44629a30d6f4 137 if (current)
johnAlexander 4:44629a30d6f4 138 {
johnAlexander 4:44629a30d6f4 139 RefreshDisplay(data_sensor, 'R');
johnAlexander 4:44629a30d6f4 140 }
johnAlexander 4:44629a30d6f4 141 }
johnAlexander 3:12cb106044f9 142 }
johnAlexander 3:12cb106044f9 143
johnAlexander 3:12cb106044f9 144 int InitSensorsArray()
johnAlexander 3:12cb106044f9 145 {
johnAlexander 3:12cb106044f9 146 int status = 1;
johnAlexander 3:12cb106044f9 147 sensorCnt = 0;
johnAlexander 3:12cb106044f9 148 /* start the measure on the center sensor */
johnAlexander 3:12cb106044f9 149 if (NULL != board->sensor_centre)
johnAlexander 3:12cb106044f9 150 {
johnAlexander 4:44629a30d6f4 151 installedSensors[sensorCnt] = 'C';
johnAlexander 3:12cb106044f9 152 status=board->sensor_centre->StopMeasurement(operating_mode);
johnAlexander 3:12cb106044f9 153 status=board->sensor_centre->StartMeasurement(operating_mode, &SensorCenterIRQ);
johnAlexander 3:12cb106044f9 154 ++sensorCnt;
johnAlexander 3:12cb106044f9 155 }
johnAlexander 3:12cb106044f9 156 /* start the measure on the left sensor */
johnAlexander 3:12cb106044f9 157 if (NULL != board->sensor_left)
johnAlexander 2:bdf097d93eca 158 {
johnAlexander 4:44629a30d6f4 159 installedSensors[sensorCnt] = 'L';
johnAlexander 3:12cb106044f9 160 status=board->sensor_left->StopMeasurement(operating_mode);
johnAlexander 3:12cb106044f9 161 status=board->sensor_left->StartMeasurement(operating_mode, &SensorLeftIRQ);
johnAlexander 3:12cb106044f9 162 ++sensorCnt;
johnAlexander 3:12cb106044f9 163 }
johnAlexander 3:12cb106044f9 164 /* start the measure on the right sensor */
johnAlexander 3:12cb106044f9 165 if (NULL != board->sensor_right)
johnAlexander 3:12cb106044f9 166 {
johnAlexander 4:44629a30d6f4 167 installedSensors[sensorCnt] = 'R';
johnAlexander 3:12cb106044f9 168 status=board->sensor_right->StopMeasurement(operating_mode);
johnAlexander 3:12cb106044f9 169 status=board->sensor_right->StartMeasurement(operating_mode, &SensorRightIRQ);
johnAlexander 3:12cb106044f9 170 ++sensorCnt;
johnAlexander 3:12cb106044f9 171 }
johnAlexander 3:12cb106044f9 172 currentSensor = 0;
johnAlexander 3:12cb106044f9 173 return status;
johnAlexander 0:cd8e818d4288 174 }
johnAlexander 0:cd8e818d4288 175
johnAlexander 4:44629a30d6f4 176
johnAlexander 2:bdf097d93eca 177 void RangeMeasure(DevI2C *device_i2c) {
johnAlexander 2:bdf097d93eca 178 int status;
johnAlexander 1:c1c10e0c32b2 179
johnAlexander 0:cd8e818d4288 180 /* creates the 53L0A1 expansion board singleton obj */
johnAlexander 0:cd8e818d4288 181 board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D8, D2);
johnAlexander 4:44629a30d6f4 182 //board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D9, D4); // Alternate INT_L/INT_R settings.
johnAlexander 0:cd8e818d4288 183
johnAlexander 0:cd8e818d4288 184 board->display->DisplayString("53L0");
johnAlexander 2:bdf097d93eca 185
johnAlexander 2:bdf097d93eca 186 operating_mode=range_continuous_interrupt;
johnAlexander 2:bdf097d93eca 187
johnAlexander 0:cd8e818d4288 188 /* init the 53L0A1 expansion board with default values */
johnAlexander 2:bdf097d93eca 189 status=board->InitBoard();
johnAlexander 3:12cb106044f9 190
johnAlexander 2:bdf097d93eca 191 if(status)
johnAlexander 2:bdf097d93eca 192 {
johnAlexander 2:bdf097d93eca 193 printf("Failed to init board!\n\r");
johnAlexander 2:bdf097d93eca 194 }
johnAlexander 3:12cb106044f9 195 else
johnAlexander 3:12cb106044f9 196 {
johnAlexander 3:12cb106044f9 197 status = InitSensorsArray();
johnAlexander 3:12cb106044f9 198 }
johnAlexander 2:bdf097d93eca 199
johnAlexander 2:bdf097d93eca 200 if(!status)
johnAlexander 0:cd8e818d4288 201 {
johnAlexander 3:12cb106044f9 202 printf ("\r\nEntering loop mode\r\n");
johnAlexander 2:bdf097d93eca 203 while (true)
johnAlexander 2:bdf097d93eca 204 {
johnAlexander 4:44629a30d6f4 205 MeasureSensors(operating_mode);
johnAlexander 4:44629a30d6f4 206 if (switchChanged)
johnAlexander 4:44629a30d6f4 207 {
johnAlexander 4:44629a30d6f4 208 ++currentSensor;
johnAlexander 4:44629a30d6f4 209 if (currentSensor == sensorCnt)
johnAlexander 4:44629a30d6f4 210 currentSensor = 0;
johnAlexander 4:44629a30d6f4 211
johnAlexander 4:44629a30d6f4 212 printf("Sensor changed to %c\r\n",installedSensors[currentSensor]);
johnAlexander 4:44629a30d6f4 213 switchChanged = false;
johnAlexander 4:44629a30d6f4 214 stop_button.enable_irq();
johnAlexander 4:44629a30d6f4 215 }
johnAlexander 2:bdf097d93eca 216 }
johnAlexander 0:cd8e818d4288 217 }
johnAlexander 0:cd8e818d4288 218 delete board;
johnAlexander 2:bdf097d93eca 219 }
johnAlexander 2:bdf097d93eca 220
johnAlexander 2:bdf097d93eca 221 /*=================================== Main ==================================
johnAlexander 3:12cb106044f9 222 Press the blue user button to switch the displayed sensor.
johnAlexander 2:bdf097d93eca 223 =============================================================================*/
johnAlexander 2:bdf097d93eca 224 int main()
johnAlexander 2:bdf097d93eca 225 {
johnAlexander 2:bdf097d93eca 226 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401
johnAlexander 3:12cb106044f9 227 stop_button.rise (&SwitchMeasuringSensorIRQ);
johnAlexander 4:44629a30d6f4 228 stop_button.enable_irq();
johnAlexander 2:bdf097d93eca 229 #endif
johnAlexander 2:bdf097d93eca 230 DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
johnAlexander 2:bdf097d93eca 231 RangeMeasure(device_i2c); // start continuous measures
johnAlexander 0:cd8e818d4288 232 }
johnAlexander 2:bdf097d93eca 233
johnAlexander 2:bdf097d93eca 234
johnAlexander 4:44629a30d6f4 235