![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MbedOS 5.15.7 example, using Interrupt mode with expansion board onboard sensor. Serial port set to default (9600) baud.
Dependencies: X_NUCLEO_53L1A1_mbed
main.cpp@4:b3346923eb1e, 2021-05-11 (annotated)
- Committer:
- johnAlexander
- Date:
- Tue May 11 09:37:42 2021 +0000
- Revision:
- 4:b3346923eb1e
- Parent:
- 2:46dcd0517f2a
Add support for MbedOS v6.x.; Override serial port default of 9600 baud, for printfs. Now set at 115200 baud, to match header comment.
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 | 4:b3346923eb1e | 40 | #if (MBED_VERSION > 60300) |
johnAlexander | 4:b3346923eb1e | 41 | UnbufferedSerial pc(USBTX, USBRX); |
johnAlexander | 4:b3346923eb1e | 42 | extern "C" void wait_ms(int ms); |
johnAlexander | 4:b3346923eb1e | 43 | #else |
johnAlexander | 2:46dcd0517f2a | 44 | Serial pc(SERIAL_TX, SERIAL_RX); |
johnAlexander | 4:b3346923eb1e | 45 | #endif |
johnAlexander | 0:6b7696e7df5e | 46 | |
johnAlexander | 2:46dcd0517f2a | 47 | /* flags that handle interrupt request for sensor and user blue button*/ |
johnAlexander | 2:46dcd0517f2a | 48 | volatile bool int_sensor = false; |
johnAlexander | 2:46dcd0517f2a | 49 | volatile bool int_stop = false; |
johnAlexander | 1:e5cce6b28b6f | 50 | |
johnAlexander | 1:e5cce6b28b6f | 51 | /* ISR callback function of the centre sensor */ |
johnAlexander | 2:46dcd0517f2a | 52 | void sensor_irq(void) |
johnAlexander | 1:e5cce6b28b6f | 53 | { |
johnAlexander | 2:46dcd0517f2a | 54 | int_sensor = true; |
johnAlexander | 1:e5cce6b28b6f | 55 | board->sensor_centre->disable_interrupt_measure_detection_irq(); |
johnAlexander | 1:e5cce6b28b6f | 56 | } |
johnAlexander | 1:e5cce6b28b6f | 57 | |
johnAlexander | 2:46dcd0517f2a | 58 | /* Start the sensor ranging */ |
johnAlexander | 2:46dcd0517f2a | 59 | int init_sensor() |
johnAlexander | 1:e5cce6b28b6f | 60 | { |
johnAlexander | 2:46dcd0517f2a | 61 | int status = 0; |
johnAlexander | 2:46dcd0517f2a | 62 | /* start the measure on the center sensor */ |
johnAlexander | 2:46dcd0517f2a | 63 | if (NULL != board->sensor_centre) { |
johnAlexander | 2:46dcd0517f2a | 64 | status = board->sensor_centre->stop_measurement(); |
johnAlexander | 2:46dcd0517f2a | 65 | if (status != 0) { |
johnAlexander | 2:46dcd0517f2a | 66 | return status; |
johnAlexander | 2:46dcd0517f2a | 67 | } |
johnAlexander | 1:e5cce6b28b6f | 68 | |
johnAlexander | 2:46dcd0517f2a | 69 | status = board->sensor_centre->start_measurement(&sensor_irq); |
johnAlexander | 2:46dcd0517f2a | 70 | if (status != 0) { |
johnAlexander | 2:46dcd0517f2a | 71 | return status; |
johnAlexander | 2:46dcd0517f2a | 72 | } |
johnAlexander | 2:46dcd0517f2a | 73 | } |
johnAlexander | 2:46dcd0517f2a | 74 | return status; |
johnAlexander | 1:e5cce6b28b6f | 75 | } |
johnAlexander | 1:e5cce6b28b6f | 76 | |
johnAlexander | 1:e5cce6b28b6f | 77 | /* ISR callback function of the user blue button to switch measuring sensor. */ |
johnAlexander | 2:46dcd0517f2a | 78 | void measuring_stop_irq(void) |
johnAlexander | 0:6b7696e7df5e | 79 | { |
johnAlexander | 2:46dcd0517f2a | 80 | int_stop = true; |
johnAlexander | 1:e5cce6b28b6f | 81 | } |
johnAlexander | 0:6b7696e7df5e | 82 | |
johnAlexander | 1:e5cce6b28b6f | 83 | /* |
johnAlexander | 1:e5cce6b28b6f | 84 | * Main ranging function |
johnAlexander | 1:e5cce6b28b6f | 85 | */ |
johnAlexander | 1:e5cce6b28b6f | 86 | int range_measure(vl53L1X_DevI2C *device_i2c) |
johnAlexander | 1:e5cce6b28b6f | 87 | { |
johnAlexander | 1:e5cce6b28b6f | 88 | int status = 0; |
johnAlexander | 2:46dcd0517f2a | 89 | uint16_t distance = 0; |
johnAlexander | 0:6b7696e7df5e | 90 | |
johnAlexander | 0:6b7696e7df5e | 91 | /* creates the 53L1A1 expansion board singleton obj */ |
johnAlexander | 1:e5cce6b28b6f | 92 | board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2); |
johnAlexander | 0:6b7696e7df5e | 93 | |
johnAlexander | 0:6b7696e7df5e | 94 | /* init the 53L1A1 expansion board with default values */ |
johnAlexander | 0:6b7696e7df5e | 95 | status = board->init_board(); |
johnAlexander | 0:6b7696e7df5e | 96 | if (status != 0) { |
johnAlexander | 0:6b7696e7df5e | 97 | printf("Failed to init board!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 98 | return status; |
johnAlexander | 0:6b7696e7df5e | 99 | } |
johnAlexander | 0:6b7696e7df5e | 100 | |
johnAlexander | 1:e5cce6b28b6f | 101 | /* init an array with chars to id the sensors */ |
johnAlexander | 2:46dcd0517f2a | 102 | status = init_sensor(); |
johnAlexander | 0:6b7696e7df5e | 103 | if (status != 0) { |
johnAlexander | 1:e5cce6b28b6f | 104 | printf("Failed to init sensors!\r\n"); |
johnAlexander | 0:6b7696e7df5e | 105 | return status; |
johnAlexander | 0:6b7696e7df5e | 106 | } |
johnAlexander | 0:6b7696e7df5e | 107 | |
johnAlexander | 1:e5cce6b28b6f | 108 | printf("Entering loop mode\r\n"); |
johnAlexander | 1:e5cce6b28b6f | 109 | /* Main ranging interrupt loop */ |
johnAlexander | 1:e5cce6b28b6f | 110 | while (true) { |
johnAlexander | 2:46dcd0517f2a | 111 | if (int_sensor) { |
johnAlexander | 2:46dcd0517f2a | 112 | int_sensor = false; |
johnAlexander | 2:46dcd0517f2a | 113 | status = board->sensor_centre->handle_irq(&distance); |
johnAlexander | 2:46dcd0517f2a | 114 | printf("distance: %d\r\n", distance); |
johnAlexander | 2:46dcd0517f2a | 115 | } |
johnAlexander | 2:46dcd0517f2a | 116 | |
johnAlexander | 2:46dcd0517f2a | 117 | if (int_stop) { |
johnAlexander | 2:46dcd0517f2a | 118 | printf("\r\nEnding loop mode \r\n"); |
johnAlexander | 2:46dcd0517f2a | 119 | break; |
johnAlexander | 0:6b7696e7df5e | 120 | } |
johnAlexander | 0:6b7696e7df5e | 121 | } |
johnAlexander | 2:46dcd0517f2a | 122 | |
johnAlexander | 0:6b7696e7df5e | 123 | return status; |
johnAlexander | 2:46dcd0517f2a | 124 | |
johnAlexander | 0:6b7696e7df5e | 125 | } |
johnAlexander | 1:e5cce6b28b6f | 126 | |
johnAlexander | 1:e5cce6b28b6f | 127 | /*=================================== Main ================================== |
johnAlexander | 1:e5cce6b28b6f | 128 | =============================================================================*/ |
johnAlexander | 1:e5cce6b28b6f | 129 | int main() |
johnAlexander | 1:e5cce6b28b6f | 130 | { |
johnAlexander | 1:e5cce6b28b6f | 131 | #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401 |
johnAlexander | 4:b3346923eb1e | 132 | // InterruptIn stop_button(USER_BUTTON); |
johnAlexander | 4:b3346923eb1e | 133 | // stop_button.rise(&measuring_stop_irq); |
johnAlexander | 1:e5cce6b28b6f | 134 | #endif |
johnAlexander | 4:b3346923eb1e | 135 | pc.baud(115200); // baud rate is important as printf statements take a lot of time |
johnAlexander | 4:b3346923eb1e | 136 | |
johnAlexander | 2:46dcd0517f2a | 137 | vl53L1X_DevI2C *device_i2c = new vl53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL); |
johnAlexander | 2:46dcd0517f2a | 138 | range_measure(device_i2c); // start continuous measures |
johnAlexander | 1:e5cce6b28b6f | 139 | } |
johnAlexander | 4:b3346923eb1e | 140 | |
johnAlexander | 4:b3346923eb1e | 141 | #if (MBED_VERSION > 60300) |
johnAlexander | 4:b3346923eb1e | 142 | extern "C" void wait_ms(int ms) |
johnAlexander | 4:b3346923eb1e | 143 | { |
johnAlexander | 4:b3346923eb1e | 144 | thread_sleep_for(ms); |
johnAlexander | 4:b3346923eb1e | 145 | } |
johnAlexander | 4:b3346923eb1e | 146 | #endif |