Simple Ranging with only the Time-of-Flight (ToF) Sensor on the X-NUCLEO-53L1A1 expansion board. Does not use Satellite boards.

Dependencies:   X_NUCLEO_53L1A1

Committer:
johnAlexander
Date:
Tue Nov 03 11:36:23 2020 +0000
Revision:
7:b157ad0895f7
Parent:
3:f61dd7863e32
Use ST published board class, inc ST sensor class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 0:6b7696e7df5e 1 /*
johnAlexander 1:e5cce6b28b6f 2 * This VL53L1X Expansion board sample application performs range measurements
johnAlexander 1:e5cce6b28b6f 3 * with interrupts enabled to generate a hardware interrupt each time a new
johnAlexander 1:e5cce6b28b6f 4 * measurement is ready to be read.
johnAlexander 0:6b7696e7df5e 5 *
johnAlexander 3:f61dd7863e32 6 * Measured ranges are output on the Serial Port, running at 115200 baud.
johnAlexander 0:6b7696e7df5e 7 *
johnAlexander 3:f61dd7863e32 8 * On STM32-Nucleo boards :
johnAlexander 3:f61dd7863e32 9 * The User Blue button stops the current measurement and entire program,
johnAlexander 3:f61dd7863e32 10 * releasing all resources.
johnAlexander 0:6b7696e7df5e 11 *
johnAlexander 3:f61dd7863e32 12 * The Black Reset button is used to restart the program.
johnAlexander 1:e5cce6b28b6f 13 *
johnAlexander 2:46dcd0517f2a 14 * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
johnAlexander 3:f61dd7863e32 15 * the X-NUCELO-53L1A1 expansion board are not made/OFF.
johnAlexander 0:6b7696e7df5e 16 * These links must be made to allow interrupts from the Satellite boards
johnAlexander 0:6b7696e7df5e 17 * to be received.
johnAlexander 0:6b7696e7df5e 18 * U11 and U18 must be made/ON to allow interrupts to be received from the
johnAlexander 0:6b7696e7df5e 19 * INT_L & INT_R positions; or
johnAlexander 0:6b7696e7df5e 20 * U10 and U15 must be made/ON to allow interrupts to be received from the
johnAlexander 0:6b7696e7df5e 21 * Alternate INT_L & INT_R positions.
johnAlexander 0:6b7696e7df5e 22 * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
johnAlexander 0:6b7696e7df5e 23 * positions.
johnAlexander 0:6b7696e7df5e 24 * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
johnAlexander 0:6b7696e7df5e 25 * Alternate INT_L is on CN5 Connector pin 2 as D8.
johnAlexander 0:6b7696e7df5e 26 * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
johnAlexander 0:6b7696e7df5e 27 * Alternate INT_R is on CN9 Connector pin 5 as D2.
johnAlexander 0:6b7696e7df5e 28 * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
johnAlexander 0:6b7696e7df5e 29 */
johnAlexander 2:46dcd0517f2a 30
johnAlexander 0:6b7696e7df5e 31 #include <stdio.h>
johnAlexander 0:6b7696e7df5e 32
johnAlexander 0:6b7696e7df5e 33 #include "mbed.h"
johnAlexander 0:6b7696e7df5e 34 #include "XNucleo53L1A1.h"
johnAlexander 3:f61dd7863e32 35 #include "VL53L1X_I2C.h"
johnAlexander 0:6b7696e7df5e 36
johnAlexander 2:46dcd0517f2a 37 #define VL53L1_I2C_SDA D14
johnAlexander 2:46dcd0517f2a 38 #define VL53L1_I2C_SCL D15
johnAlexander 0:6b7696e7df5e 39
johnAlexander 3:f61dd7863e32 40 #if TARGET_STM // we are cross compiling for an STM32-Nucleo
johnAlexander 3:f61dd7863e32 41 InterruptIn stop_button(USER_BUTTON);
johnAlexander 3:f61dd7863e32 42 #endif
johnAlexander 3:f61dd7863e32 43 #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards.
johnAlexander 3:f61dd7863e32 44 InterruptIn stop_button(SW2);
johnAlexander 3:f61dd7863e32 45 #endif
johnAlexander 3:f61dd7863e32 46
johnAlexander 3:f61dd7863e32 47
johnAlexander 0:6b7696e7df5e 48 static XNucleo53L1A1 *board=NULL;
johnAlexander 0:6b7696e7df5e 49
johnAlexander 2:46dcd0517f2a 50 /* flags that handle interrupt request for sensor and user blue button*/
johnAlexander 2:46dcd0517f2a 51 volatile bool int_sensor = false;
johnAlexander 2:46dcd0517f2a 52 volatile bool int_stop = false;
johnAlexander 1:e5cce6b28b6f 53
johnAlexander 1:e5cce6b28b6f 54 /* ISR callback function of the centre sensor */
johnAlexander 2:46dcd0517f2a 55 void sensor_irq(void)
johnAlexander 1:e5cce6b28b6f 56 {
johnAlexander 2:46dcd0517f2a 57 int_sensor = true;
johnAlexander 1:e5cce6b28b6f 58 board->sensor_centre->disable_interrupt_measure_detection_irq();
johnAlexander 1:e5cce6b28b6f 59 }
johnAlexander 1:e5cce6b28b6f 60
johnAlexander 2:46dcd0517f2a 61 /* Start the sensor ranging */
johnAlexander 2:46dcd0517f2a 62 int init_sensor()
johnAlexander 1:e5cce6b28b6f 63 {
johnAlexander 2:46dcd0517f2a 64 int status = 0;
johnAlexander 2:46dcd0517f2a 65 /* start the measure on the center sensor */
johnAlexander 2:46dcd0517f2a 66 if (NULL != board->sensor_centre) {
johnAlexander 2:46dcd0517f2a 67 status = board->sensor_centre->stop_measurement();
johnAlexander 2:46dcd0517f2a 68 if (status != 0) {
johnAlexander 2:46dcd0517f2a 69 return status;
johnAlexander 2:46dcd0517f2a 70 }
johnAlexander 1:e5cce6b28b6f 71
johnAlexander 2:46dcd0517f2a 72 status = board->sensor_centre->start_measurement(&sensor_irq);
johnAlexander 2:46dcd0517f2a 73 if (status != 0) {
johnAlexander 2:46dcd0517f2a 74 return status;
johnAlexander 2:46dcd0517f2a 75 }
johnAlexander 2:46dcd0517f2a 76 }
johnAlexander 2:46dcd0517f2a 77 return status;
johnAlexander 1:e5cce6b28b6f 78 }
johnAlexander 1:e5cce6b28b6f 79
johnAlexander 1:e5cce6b28b6f 80 /* ISR callback function of the user blue button to switch measuring sensor. */
johnAlexander 2:46dcd0517f2a 81 void measuring_stop_irq(void)
johnAlexander 0:6b7696e7df5e 82 {
johnAlexander 2:46dcd0517f2a 83 int_stop = true;
johnAlexander 1:e5cce6b28b6f 84 }
johnAlexander 0:6b7696e7df5e 85
johnAlexander 1:e5cce6b28b6f 86 /*
johnAlexander 1:e5cce6b28b6f 87 * Main ranging function
johnAlexander 1:e5cce6b28b6f 88 */
johnAlexander 3:f61dd7863e32 89 int range_measure(VL53L1X_DevI2C *device_i2c)
johnAlexander 1:e5cce6b28b6f 90 {
johnAlexander 1:e5cce6b28b6f 91 int status = 0;
johnAlexander 2:46dcd0517f2a 92 uint16_t distance = 0;
johnAlexander 0:6b7696e7df5e 93
johnAlexander 0:6b7696e7df5e 94 /* creates the 53L1A1 expansion board singleton obj */
johnAlexander 1:e5cce6b28b6f 95 board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2);
johnAlexander 0:6b7696e7df5e 96
johnAlexander 0:6b7696e7df5e 97 /* init the 53L1A1 expansion board with default values */
johnAlexander 0:6b7696e7df5e 98 status = board->init_board();
johnAlexander 0:6b7696e7df5e 99 if (status != 0) {
johnAlexander 0:6b7696e7df5e 100 printf("Failed to init board!\r\n");
johnAlexander 0:6b7696e7df5e 101 return status;
johnAlexander 0:6b7696e7df5e 102 }
johnAlexander 0:6b7696e7df5e 103
johnAlexander 1:e5cce6b28b6f 104 /* init an array with chars to id the sensors */
johnAlexander 2:46dcd0517f2a 105 status = init_sensor();
johnAlexander 0:6b7696e7df5e 106 if (status != 0) {
johnAlexander 1:e5cce6b28b6f 107 printf("Failed to init sensors!\r\n");
johnAlexander 0:6b7696e7df5e 108 return status;
johnAlexander 0:6b7696e7df5e 109 }
johnAlexander 0:6b7696e7df5e 110
johnAlexander 1:e5cce6b28b6f 111 printf("Entering loop mode\r\n");
johnAlexander 1:e5cce6b28b6f 112 /* Main ranging interrupt loop */
johnAlexander 1:e5cce6b28b6f 113 while (true) {
johnAlexander 2:46dcd0517f2a 114 if (int_sensor) {
johnAlexander 2:46dcd0517f2a 115 int_sensor = false;
johnAlexander 2:46dcd0517f2a 116 status = board->sensor_centre->handle_irq(&distance);
johnAlexander 2:46dcd0517f2a 117 printf("distance: %d\r\n", distance);
johnAlexander 2:46dcd0517f2a 118 }
johnAlexander 2:46dcd0517f2a 119
johnAlexander 2:46dcd0517f2a 120 if (int_stop) {
johnAlexander 2:46dcd0517f2a 121 printf("\r\nEnding loop mode \r\n");
johnAlexander 2:46dcd0517f2a 122 break;
johnAlexander 0:6b7696e7df5e 123 }
johnAlexander 0:6b7696e7df5e 124 }
johnAlexander 2:46dcd0517f2a 125
johnAlexander 0:6b7696e7df5e 126 return status;
johnAlexander 2:46dcd0517f2a 127
johnAlexander 0:6b7696e7df5e 128 }
johnAlexander 1:e5cce6b28b6f 129
johnAlexander 1:e5cce6b28b6f 130 /*=================================== Main ==================================
johnAlexander 1:e5cce6b28b6f 131 =============================================================================*/
johnAlexander 1:e5cce6b28b6f 132 int main()
johnAlexander 1:e5cce6b28b6f 133 {
johnAlexander 2:46dcd0517f2a 134 stop_button.rise(&measuring_stop_irq);
johnAlexander 3:f61dd7863e32 135
johnAlexander 3:f61dd7863e32 136 VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
johnAlexander 2:46dcd0517f2a 137 range_measure(device_i2c); // start continuous measures
johnAlexander 1:e5cce6b28b6f 138 }