![](/media/cache/group/mbed_group_aewAOUI.png.50x50_q85.png)
Simple Ranging Example, using Expansion Board Sensor, driven via Interrupts, on Mbed OS 5.x.
Dependencies: X_NUCLEO_53L1A1_mbed
main.cpp@2:46dcd0517f2a, 2019-05-17 (annotated)
- Committer:
- johnAlexander
- Date:
- Fri May 17 12:29:28 2019 +0000
- Revision:
- 2:46dcd0517f2a
- Parent:
- 1:e5cce6b28b6f
Simple Ranging Example, using Expansion Board Sensor, driven via Interrupts, on Mbed OS 5.x.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:46dcd0517f2a | 6 | * Measured ranges are ouput on the Serial Port, running at 115200 baud. |
johnAlexander | 0:6b7696e7df5e | 7 | * |
johnAlexander | 2:46dcd0517f2a | 8 | * The User Blue button stops the current measurement and entire program, |
johnAlexander | 2:46dcd0517f2a | 9 | * releasing all resources. |
johnAlexander | 0:6b7696e7df5e | 10 | * |
johnAlexander | 2:46dcd0517f2a | 11 | * The Black Reset button is used to restart the program. |
johnAlexander | 1:e5cce6b28b6f | 12 | * |
johnAlexander | 2:46dcd0517f2a | 13 | * *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of |
johnAlexander | 0:6b7696e7df5e | 14 | * the X-NUCELO-53L0A1 expansion board are not made/OFF. |
johnAlexander | 0:6b7696e7df5e | 15 | * These links must be made to allow interrupts from the Satellite boards |
johnAlexander | 0:6b7696e7df5e | 16 | * to be received. |
johnAlexander | 0:6b7696e7df5e | 17 | * U11 and U18 must be made/ON to allow interrupts to be received from the |
johnAlexander | 0:6b7696e7df5e | 18 | * INT_L & INT_R positions; or |
johnAlexander | 0:6b7696e7df5e | 19 | * U10 and U15 must be made/ON to allow interrupts to be received from the |
johnAlexander | 0:6b7696e7df5e | 20 | * Alternate INT_L & INT_R positions. |
johnAlexander | 0:6b7696e7df5e | 21 | * The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R |
johnAlexander | 0:6b7696e7df5e | 22 | * positions. |
johnAlexander | 0:6b7696e7df5e | 23 | * INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9. |
johnAlexander | 0:6b7696e7df5e | 24 | * Alternate INT_L is on CN5 Connector pin 2 as D8. |
johnAlexander | 0:6b7696e7df5e | 25 | * INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4. |
johnAlexander | 0:6b7696e7df5e | 26 | * Alternate INT_R is on CN9 Connector pin 5 as D2. |
johnAlexander | 0:6b7696e7df5e | 27 | * The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/ |
johnAlexander | 0:6b7696e7df5e | 28 | */ |
johnAlexander | 2:46dcd0517f2a | 29 | |
johnAlexander | 0:6b7696e7df5e | 30 | #include <stdio.h> |
johnAlexander | 0:6b7696e7df5e | 31 | |
johnAlexander | 0:6b7696e7df5e | 32 | #include "mbed.h" |
johnAlexander | 0:6b7696e7df5e | 33 | #include "XNucleo53L1A1.h" |
johnAlexander | 0:6b7696e7df5e | 34 | #include "vl53L1x_I2c.h" |
johnAlexander | 0:6b7696e7df5e | 35 | |
johnAlexander | 2:46dcd0517f2a | 36 | #define VL53L1_I2C_SDA D14 |
johnAlexander | 2:46dcd0517f2a | 37 | #define VL53L1_I2C_SCL D15 |
johnAlexander | 0:6b7696e7df5e | 38 | |
johnAlexander | 0:6b7696e7df5e | 39 | static XNucleo53L1A1 *board=NULL; |
johnAlexander | 2:46dcd0517f2a | 40 | Serial pc(SERIAL_TX, SERIAL_RX); |
johnAlexander | 0:6b7696e7df5e | 41 | |
johnAlexander | 2:46dcd0517f2a | 42 | /* flags that handle interrupt request for sensor and user blue button*/ |
johnAlexander | 2:46dcd0517f2a | 43 | volatile bool int_sensor = false; |
johnAlexander | 2:46dcd0517f2a | 44 | volatile bool int_stop = false; |
johnAlexander | 1:e5cce6b28b6f | 45 | |
johnAlexander | 1:e5cce6b28b6f | 46 | /* ISR callback function of the centre sensor */ |
johnAlexander | 2:46dcd0517f2a | 47 | void sensor_irq(void) |
johnAlexander | 1:e5cce6b28b6f | 48 | { |
johnAlexander | 2:46dcd0517f2a | 49 | int_sensor = true; |
johnAlexander | 1:e5cce6b28b6f | 50 | board->sensor_centre->disable_interrupt_measure_detection_irq(); |
johnAlexander | 1:e5cce6b28b6f | 51 | } |
johnAlexander | 1:e5cce6b28b6f | 52 | |
johnAlexander | 2:46dcd0517f2a | 53 | /* Start the sensor ranging */ |
johnAlexander | 2:46dcd0517f2a | 54 | int init_sensor() |
johnAlexander | 1:e5cce6b28b6f | 55 | { |
johnAlexander | 2:46dcd0517f2a | 56 | int status = 0; |
johnAlexander | 2:46dcd0517f2a | 57 | /* start the measure on the center sensor */ |
johnAlexander | 2:46dcd0517f2a | 58 | if (NULL != board->sensor_centre) { |
johnAlexander | 2:46dcd0517f2a | 59 | status = board->sensor_centre->stop_measurement(); |
johnAlexander | 2:46dcd0517f2a | 60 | if (status != 0) { |
johnAlexander | 2:46dcd0517f2a | 61 | return status; |
johnAlexander | 2:46dcd0517f2a | 62 | } |
johnAlexander | 1:e5cce6b28b6f | 63 | |
johnAlexander | 2:46dcd0517f2a | 64 | status = board->sensor_centre->start_measurement(&sensor_irq); |
johnAlexander | 2:46dcd0517f2a | 65 | if (status != 0) { |
johnAlexander | 2:46dcd0517f2a | 66 | return status; |
johnAlexander | 2:46dcd0517f2a | 67 | } |
johnAlexander | 2:46dcd0517f2a | 68 | } |
johnAlexander | 2:46dcd0517f2a | 69 | return status; |
johnAlexander | 1:e5cce6b28b6f | 70 | } |
johnAlexander | 1:e5cce6b28b6f | 71 | |
johnAlexander | 1:e5cce6b28b6f | 72 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
johnAlexander | 2:46dcd0517f2a | 73 | void measuring_stop_irq(void) |
johnAlexander | 0:6b7696e7df5e | 74 | { |
johnAlexander | 2:46dcd0517f2a | 75 | int_stop = true; |
johnAlexander | 1:e5cce6b28b6f | 76 | } |
johnAlexander | 0:6b7696e7df5e | 77 | |
johnAlexander | 1:e5cce6b28b6f | 78 | /* |
johnAlexander | 1:e5cce6b28b6f | 79 | * Main ranging function |
johnAlexander | 1:e5cce6b28b6f | 80 | */ |
johnAlexander | 1:e5cce6b28b6f | 81 | int range_measure(vl53L1X_DevI2C *device_i2c) |
johnAlexander | 1:e5cce6b28b6f | 82 | { |
johnAlexander | 1:e5cce6b28b6f | 83 | int status = 0; |
johnAlexander | 2:46dcd0517f2a | 84 | uint16_t distance = 0; |
johnAlexander | 0:6b7696e7df5e | 85 | |
johnAlexander | 0:6b7696e7df5e | 86 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 1:e5cce6b28b6f | 87 | board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2); |
johnAlexander | 0:6b7696e7df5e | 88 | |
johnAlexander | 0:6b7696e7df5e | 89 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 0:6b7696e7df5e | 90 | status = board->init_board(); |
johnAlexander | 0:6b7696e7df5e | 91 | if (status != 0) { |
johnAlexander | 0:6b7696e7df5e | 92 | printf("Failed to init board!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 93 | return status; |
johnAlexander | 0:6b7696e7df5e | 94 | } |
johnAlexander | 0:6b7696e7df5e | 95 | |
johnAlexander | 1:e5cce6b28b6f | 96 | /* init an array with chars to id the sensors */ |
johnAlexander | 2:46dcd0517f2a | 97 | status = init_sensor(); |
johnAlexander | 0:6b7696e7df5e | 98 | if (status != 0) { |
johnAlexander | 1:e5cce6b28b6f | 99 | printf("Failed to init sensors!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 100 | return status; |
johnAlexander | 0:6b7696e7df5e | 101 | } |
johnAlexander | 0:6b7696e7df5e | 102 | |
johnAlexander | 1:e5cce6b28b6f | 103 | printf("Entering loop mode\r\n"); |
johnAlexander | 1:e5cce6b28b6f | 104 | /* Main ranging interrupt loop */ |
johnAlexander | 1:e5cce6b28b6f | 105 | while (true) { |
johnAlexander | 2:46dcd0517f2a | 106 | if (int_sensor) { |
johnAlexander | 2:46dcd0517f2a | 107 | int_sensor = false; |
johnAlexander | 2:46dcd0517f2a | 108 | status = board->sensor_centre->handle_irq(&distance); |
johnAlexander | 2:46dcd0517f2a | 109 | printf("distance: %d\r\n", distance); |
johnAlexander | 2:46dcd0517f2a | 110 | } |
johnAlexander | 2:46dcd0517f2a | 111 | |
johnAlexander | 2:46dcd0517f2a | 112 | if (int_stop) { |
johnAlexander | 2:46dcd0517f2a | 113 | printf("\r\nEnding loop mode \r\n"); |
johnAlexander | 2:46dcd0517f2a | 114 | break; |
johnAlexander | 0:6b7696e7df5e | 115 | } |
johnAlexander | 0:6b7696e7df5e | 116 | } |
johnAlexander | 2:46dcd0517f2a | 117 | |
johnAlexander | 0:6b7696e7df5e | 118 | return status; |
johnAlexander | 2:46dcd0517f2a | 119 | |
johnAlexander | 0:6b7696e7df5e | 120 | } |
johnAlexander | 1:e5cce6b28b6f | 121 | |
johnAlexander | 1:e5cce6b28b6f | 122 | /*=================================== Main ================================== |
johnAlexander | 1:e5cce6b28b6f | 123 | =============================================================================*/ |
johnAlexander | 1:e5cce6b28b6f | 124 | int main() |
johnAlexander | 1:e5cce6b28b6f | 125 | { |
johnAlexander | 1:e5cce6b28b6f | 126 | #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 |
johnAlexander | 2:46dcd0517f2a | 127 | InterruptIn stop_button(USER_BUTTON); |
johnAlexander | 2:46dcd0517f2a | 128 | stop_button.rise(&measuring_stop_irq); |
johnAlexander | 1:e5cce6b28b6f | 129 | #endif |
johnAlexander | 2:46dcd0517f2a | 130 | vl53L1X_DevI2C *device_i2c = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
johnAlexander | 2:46dcd0517f2a | 131 | range_measure(device_i2c); // start continuous measures |
johnAlexander | 1:e5cce6b28b6f | 132 | } |