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:
Thu Jun 15 13:52:54 2017 +0000
Revision:
3:12cb106044f9
Parent:
2:bdf097d93eca
Child:
4:44629a30d6f4
Sample application using interrupts, and range_continuous_interrupt mode, to receive range data from the on-board and satellite sensors. Results are displayed on the on-board, LED Display and the COM port.

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 3:12cb106044f9 12 * The application supports the centre, on-board, sensor and up two satellites.
johnAlexander 3:12cb106044f9 13 *
johnAlexander 3:12cb106044f9 14 * *** NOTE : Hard-links U11 and U18, on the underside of the X-NUCELO-53L0A1
johnAlexander 3:12cb106044f9 15 * expansion board must be made/ON to allow interrupts to be received
johnAlexander 3:12cb106044f9 16 * from the satellite boards. ***
johnAlexander 3:12cb106044f9 17 *
johnAlexander 3:12cb106044f9 18 * The measured range data is displayed on the on-board 4-digit LED display, and sent
johnAlexander 3:12cb106044f9 19 * to the COM port.
johnAlexander 3:12cb106044f9 20 *
johnAlexander 3:12cb106044f9 21 * The User Blue button switches between the currently selected sensor to display range
johnAlexander 3:12cb106044f9 22 * results from.
johnAlexander 3:12cb106044f9 23 *
johnAlexander 3:12cb106044f9 24 * The Black Reset button is used to restart the program.
johnAlexander 3:12cb106044f9 25 */
johnAlexander 2:bdf097d93eca 26
johnAlexander 0:cd8e818d4288 27
johnAlexander 0:cd8e818d4288 28 #define VL53L0_I2C_SDA D14
johnAlexander 0:cd8e818d4288 29 #define VL53L0_I2C_SCL D15
johnAlexander 0:cd8e818d4288 30
johnAlexander 3:12cb106044f9 31 #define CENTER_BIT 0
johnAlexander 3:12cb106044f9 32 #define LEFT_BIT 1
johnAlexander 3:12cb106044f9 33 #define RIGHT_BIT 2
johnAlexander 3:12cb106044f9 34
johnAlexander 3:12cb106044f9 35
johnAlexander 2:bdf097d93eca 36 static X_NUCLEO_53L0A1 *board=NULL;
johnAlexander 2:bdf097d93eca 37 VL53L0X_RangingMeasurementData_t data_sensor;
johnAlexander 0:cd8e818d4288 38 OperatingMode operating_mode;
johnAlexander 0:cd8e818d4288 39
johnAlexander 3:12cb106044f9 40 /* interrupt requests */
johnAlexander 3:12cb106044f9 41 volatile int upadtedSensors = 0;
johnAlexander 3:12cb106044f9 42
johnAlexander 3:12cb106044f9 43 /* Current sensor number*/
johnAlexander 3:12cb106044f9 44 volatile int currentSensor = 0;
johnAlexander 3:12cb106044f9 45 /* Installed sensors count */
johnAlexander 3:12cb106044f9 46 int sensorCnt = 0;
johnAlexander 3:12cb106044f9 47
johnAlexander 3:12cb106044f9 48 struct Sensor
johnAlexander 3:12cb106044f9 49 {
johnAlexander 3:12cb106044f9 50 char prefix;
johnAlexander 3:12cb106044f9 51 int sensorBit;
johnAlexander 3:12cb106044f9 52 VL53L0X *sensorPtr;
johnAlexander 3:12cb106044f9 53 } installedSensors[3];
johnAlexander 2:bdf097d93eca 54
johnAlexander 0:cd8e818d4288 55
johnAlexander 3:12cb106044f9 56 /* ISR callback function of the sensor_centre */
johnAlexander 2:bdf097d93eca 57 void SensorCenterIRQ(void)
johnAlexander 0:cd8e818d4288 58 {
johnAlexander 3:12cb106044f9 59 upadtedSensors |= (1 << CENTER_BIT);
johnAlexander 2:bdf097d93eca 60 board->sensor_centre->DisableInterruptMeasureDetectionIRQ();
johnAlexander 2:bdf097d93eca 61 }
johnAlexander 2:bdf097d93eca 62
johnAlexander 3:12cb106044f9 63 void SensorLeftIRQ(void)
johnAlexander 3:12cb106044f9 64 {
johnAlexander 3:12cb106044f9 65 upadtedSensors |= (1 << LEFT_BIT);
johnAlexander 3:12cb106044f9 66 board->sensor_left->DisableInterruptMeasureDetectionIRQ();
johnAlexander 3:12cb106044f9 67 }
johnAlexander 3:12cb106044f9 68
johnAlexander 3:12cb106044f9 69 void SensorRightIRQ(void)
johnAlexander 3:12cb106044f9 70 {
johnAlexander 3:12cb106044f9 71 upadtedSensors |= (1 << RIGHT_BIT);
johnAlexander 3:12cb106044f9 72 board->sensor_right->DisableInterruptMeasureDetectionIRQ();
johnAlexander 3:12cb106044f9 73 }
johnAlexander 3:12cb106044f9 74
johnAlexander 2:bdf097d93eca 75 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 3:12cb106044f9 76 void SwitchMeasuringSensorIRQ(void)
johnAlexander 2:bdf097d93eca 77 {
johnAlexander 3:12cb106044f9 78 ++currentSensor;
johnAlexander 3:12cb106044f9 79 if (currentSensor == sensorCnt)
johnAlexander 3:12cb106044f9 80 currentSensor = 0;
johnAlexander 3:12cb106044f9 81 printf("Sensor changed to %c\r\n",installedSensors[currentSensor].prefix);
johnAlexander 0:cd8e818d4288 82 }
johnAlexander 0:cd8e818d4288 83
johnAlexander 0:cd8e818d4288 84 /* On board 4 digit local display refresh */
johnAlexander 3:12cb106044f9 85 void DisplayRefresh(OperatingMode op_mode)
johnAlexander 0:cd8e818d4288 86 {
johnAlexander 3:12cb106044f9 87 int status;
johnAlexander 2:bdf097d93eca 88 char str[4];
johnAlexander 3:12cb106044f9 89 Sensor *current;
johnAlexander 3:12cb106044f9 90 for (int t=0; t < sensorCnt; t++)
johnAlexander 2:bdf097d93eca 91 {
johnAlexander 3:12cb106044f9 92 current = &installedSensors[t];
johnAlexander 3:12cb106044f9 93 if (upadtedSensors & current->sensorBit)
johnAlexander 3:12cb106044f9 94 {
johnAlexander 3:12cb106044f9 95 status = current->sensorPtr->HandleIRQ(op_mode, &data_sensor);
johnAlexander 3:12cb106044f9 96 upadtedSensors &= ~(current->sensorBit) ;
johnAlexander 3:12cb106044f9 97 if (!status)
johnAlexander 3:12cb106044f9 98 {
johnAlexander 3:12cb106044f9 99 if (data_sensor.RangeStatus == 0) // we have a valid range.
johnAlexander 3:12cb106044f9 100 {
johnAlexander 3:12cb106044f9 101 printf("%c %4d; ", current->prefix,data_sensor.RangeMilliMeter);
johnAlexander 3:12cb106044f9 102 if (currentSensor == t)
johnAlexander 3:12cb106044f9 103 {
johnAlexander 3:12cb106044f9 104 sprintf(str,"%c%3d", current->prefix ,data_sensor.RangeMilliMeter);
johnAlexander 3:12cb106044f9 105 }
johnAlexander 3:12cb106044f9 106 }
johnAlexander 3:12cb106044f9 107 else
johnAlexander 3:12cb106044f9 108 {
johnAlexander 3:12cb106044f9 109 if (currentSensor == t)
johnAlexander 3:12cb106044f9 110 {
johnAlexander 3:12cb106044f9 111 sprintf(str,"%c%s", current->prefix, "---");
johnAlexander 3:12cb106044f9 112 }
johnAlexander 3:12cb106044f9 113 }
johnAlexander 3:12cb106044f9 114 }
johnAlexander 3:12cb106044f9 115 }
johnAlexander 2:bdf097d93eca 116 }
johnAlexander 3:12cb106044f9 117 board->display->DisplayString(str);
johnAlexander 3:12cb106044f9 118 }
johnAlexander 3:12cb106044f9 119
johnAlexander 3:12cb106044f9 120 int InitSensorsArray()
johnAlexander 3:12cb106044f9 121 {
johnAlexander 3:12cb106044f9 122 int status = 1;
johnAlexander 3:12cb106044f9 123 sensorCnt = 0;
johnAlexander 3:12cb106044f9 124 /* start the measure on the center sensor */
johnAlexander 3:12cb106044f9 125 if (NULL != board->sensor_centre)
johnAlexander 3:12cb106044f9 126 {
johnAlexander 3:12cb106044f9 127 installedSensors[sensorCnt].prefix = 'C';
johnAlexander 3:12cb106044f9 128 installedSensors[sensorCnt].sensorBit |= (1 << CENTER_BIT);
johnAlexander 3:12cb106044f9 129 installedSensors[sensorCnt].sensorPtr = board->sensor_centre;
johnAlexander 3:12cb106044f9 130 status=board->sensor_centre->StopMeasurement(operating_mode);
johnAlexander 3:12cb106044f9 131 status=board->sensor_centre->StartMeasurement(operating_mode, &SensorCenterIRQ);
johnAlexander 3:12cb106044f9 132 ++sensorCnt;
johnAlexander 3:12cb106044f9 133 }
johnAlexander 3:12cb106044f9 134 /* start the measure on the left sensor */
johnAlexander 3:12cb106044f9 135 if (NULL != board->sensor_left)
johnAlexander 2:bdf097d93eca 136 {
johnAlexander 3:12cb106044f9 137 installedSensors[sensorCnt].prefix = 'L';
johnAlexander 3:12cb106044f9 138 installedSensors[sensorCnt].sensorBit |= (1 << LEFT_BIT);
johnAlexander 3:12cb106044f9 139 installedSensors[sensorCnt].sensorPtr = board->sensor_left;
johnAlexander 3:12cb106044f9 140 status=board->sensor_left->StopMeasurement(operating_mode);
johnAlexander 3:12cb106044f9 141 status=board->sensor_left->StartMeasurement(operating_mode, &SensorLeftIRQ);
johnAlexander 3:12cb106044f9 142 ++sensorCnt;
johnAlexander 3:12cb106044f9 143 }
johnAlexander 3:12cb106044f9 144 /* start the measure on the right sensor */
johnAlexander 3:12cb106044f9 145 if (NULL != board->sensor_right)
johnAlexander 3:12cb106044f9 146 {
johnAlexander 3:12cb106044f9 147 installedSensors[sensorCnt].prefix = 'R';
johnAlexander 3:12cb106044f9 148 installedSensors[sensorCnt].sensorBit |= (1 << RIGHT_BIT);
johnAlexander 3:12cb106044f9 149 installedSensors[sensorCnt].sensorPtr = board->sensor_right;
johnAlexander 3:12cb106044f9 150 status=board->sensor_right->StopMeasurement(operating_mode);
johnAlexander 3:12cb106044f9 151 status=board->sensor_right->StartMeasurement(operating_mode, &SensorRightIRQ);
johnAlexander 3:12cb106044f9 152 ++sensorCnt;
johnAlexander 3:12cb106044f9 153 }
johnAlexander 3:12cb106044f9 154 currentSensor = 0;
johnAlexander 3:12cb106044f9 155 return status;
johnAlexander 0:cd8e818d4288 156 }
johnAlexander 0:cd8e818d4288 157
johnAlexander 2:bdf097d93eca 158 void RangeMeasure(DevI2C *device_i2c) {
johnAlexander 2:bdf097d93eca 159 int status;
johnAlexander 1:c1c10e0c32b2 160
johnAlexander 0:cd8e818d4288 161 /* creates the 53L0A1 expansion board singleton obj */
johnAlexander 0:cd8e818d4288 162 board=X_NUCLEO_53L0A1::Instance(device_i2c, A2, D8, D2);
johnAlexander 0:cd8e818d4288 163
johnAlexander 0:cd8e818d4288 164 board->display->DisplayString("53L0");
johnAlexander 2:bdf097d93eca 165
johnAlexander 2:bdf097d93eca 166 operating_mode=range_continuous_interrupt;
johnAlexander 2:bdf097d93eca 167
johnAlexander 0:cd8e818d4288 168 /* init the 53L0A1 expansion board with default values */
johnAlexander 2:bdf097d93eca 169 status=board->InitBoard();
johnAlexander 3:12cb106044f9 170
johnAlexander 2:bdf097d93eca 171 if(status)
johnAlexander 2:bdf097d93eca 172 {
johnAlexander 2:bdf097d93eca 173 printf("Failed to init board!\n\r");
johnAlexander 2:bdf097d93eca 174 }
johnAlexander 3:12cb106044f9 175 else
johnAlexander 3:12cb106044f9 176 {
johnAlexander 3:12cb106044f9 177 status = InitSensorsArray();
johnAlexander 3:12cb106044f9 178 }
johnAlexander 2:bdf097d93eca 179
johnAlexander 2:bdf097d93eca 180 if(!status)
johnAlexander 0:cd8e818d4288 181 {
johnAlexander 3:12cb106044f9 182 printf ("\r\nEntering loop mode\r\n");
johnAlexander 2:bdf097d93eca 183 while (true)
johnAlexander 2:bdf097d93eca 184 {
johnAlexander 3:12cb106044f9 185 DisplayRefresh(operating_mode);
johnAlexander 2:bdf097d93eca 186 }
johnAlexander 0:cd8e818d4288 187 }
johnAlexander 0:cd8e818d4288 188 delete board;
johnAlexander 2:bdf097d93eca 189 }
johnAlexander 2:bdf097d93eca 190
johnAlexander 2:bdf097d93eca 191 /*=================================== Main ==================================
johnAlexander 3:12cb106044f9 192 Press the blue user button to switch the displayed sensor.
johnAlexander 2:bdf097d93eca 193 =============================================================================*/
johnAlexander 2:bdf097d93eca 194 int main()
johnAlexander 2:bdf097d93eca 195 {
johnAlexander 2:bdf097d93eca 196 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401
johnAlexander 2:bdf097d93eca 197 InterruptIn stop_button (USER_BUTTON);
johnAlexander 3:12cb106044f9 198 stop_button.rise (&SwitchMeasuringSensorIRQ);
johnAlexander 2:bdf097d93eca 199 #endif
johnAlexander 2:bdf097d93eca 200 DevI2C *device_i2c =new DevI2C(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
johnAlexander 2:bdf097d93eca 201 RangeMeasure(device_i2c); // start continuous measures
johnAlexander 0:cd8e818d4288 202 }
johnAlexander 2:bdf097d93eca 203
johnAlexander 2:bdf097d93eca 204