ST / Mbed OS HelloWorld_53L1A1

Dependencies:   X_NUCLEO_53L1A1

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * This VL53L1X Expansion board sample application performs range measurements
00003  * with interrupts enabled to generate a hardware interrupt each time a new
00004  * measurement is ready to be read.
00005  *
00006  * Measured ranges are output on the Serial Port, running at 115200 baud.
00007  *
00008  * On STM32-Nucleo boards :
00009  *     The User Blue button stops the current measurement and entire program,
00010  *     releasing all resources.
00011  *
00012  *     The Black Reset button is used to restart the program.
00013  *
00014  *  *** NOTE : By default hardlinks U10, U11, U15 & U18, on the underside of
00015  *            the X-NUCELO-53L1A1 expansion board are not made/OFF.
00016  *            These links must be made to allow interrupts from the Satellite boards
00017  *            to be received.
00018  *            U11 and U18 must be made/ON to allow interrupts to be received from the
00019  *            INT_L & INT_R positions; or
00020  *            U10 and U15 must be made/ON to allow interrupts to be received from the
00021  *            Alternate INT_L & INT_R positions.
00022  *            The X_NUCLEO_53L1A1 firmware library defaults to use the INT_L/INT_R
00023  *            positions.
00024  *            INT_L is available on expansion board Arduino Connector CN5, pin 1 as D9.
00025  *            Alternate INT_L is on CN5 Connector pin 2 as D8.
00026  *            INT_R is available on expansion board Arduino Connector CN9, pin 3 as D4.
00027  *            Alternate INT_R is on CN9 Connector pin 5 as D2.
00028  *            The pinouts are shown here : https://developer.mbed.org/components/X-NUCLEO-53L1A1/
00029  */
00030 
00031 #include <stdio.h>
00032 
00033 #include "mbed.h"
00034 #include "XNucleo53L1A1.h"
00035 #include "VL53L1X_I2C.h"
00036 
00037 #define VL53L1_I2C_SDA   D14
00038 #define VL53L1_I2C_SCL   D15
00039 
00040 #if TARGET_STM  // we are cross compiling for an STM32-Nucleo
00041     InterruptIn stop_button(USER_BUTTON);
00042 #endif
00043 #if TARGET_Freescale // we are cross-compiling for NXP FRDM boards.
00044     InterruptIn stop_button(SW2);
00045 #endif
00046 
00047 
00048 static XNucleo53L1A1 *board=NULL;
00049 
00050 /* flags that handle interrupt request for sensor and user blue button*/
00051 volatile bool int_sensor = false;
00052 volatile bool int_stop = false;
00053 
00054 /* ISR callback function of the centre sensor */
00055 void sensor_irq(void)
00056 {
00057     int_sensor = true;
00058     board->sensor_centre->disable_interrupt_measure_detection_irq();
00059 }
00060 
00061 /* Start the sensor ranging */
00062 int init_sensor()
00063 {
00064     int status = 0;
00065     /* start the measure on the center sensor */
00066     if (NULL != board->sensor_centre) {
00067         status = board->sensor_centre->stop_measurement();
00068         if (status != 0) {
00069                 return status;
00070         }
00071 
00072         status = board->sensor_centre->start_measurement(&sensor_irq);
00073         if (status != 0) {
00074             return status;
00075         }
00076     }
00077     return status;
00078 }
00079 
00080 /* ISR callback function of the user blue button to switch measuring sensor. */
00081 void measuring_stop_irq(void)
00082 {
00083     int_stop = true;
00084 }
00085 
00086 /*
00087  * Main ranging function
00088  */
00089 int range_measure(VL53L1X_DevI2C *device_i2c)
00090 {
00091     int status = 0;
00092     uint16_t distance = 0;
00093 
00094     /* creates the 53L1A1 expansion board singleton obj */
00095     board = XNucleo53L1A1::instance(device_i2c, A2, D9, D2);
00096 
00097     /* init the 53L1A1 expansion board with default values */
00098     status = board->init_board();
00099     if (status != 0) {
00100         printf("Failed to init board!\r\n");
00101         return status;
00102     }
00103 
00104     /* init an array with chars to id the sensors */
00105     status = init_sensor();
00106     if (status != 0) {
00107         printf("Failed to init sensors!\r\n");
00108         return status;
00109     }
00110 
00111     printf("Entering loop mode\r\n");
00112     /* Main ranging interrupt loop */
00113     while (true) {
00114         if (int_sensor) {
00115             int_sensor = false;
00116             status = board->sensor_centre->handle_irq(&distance);
00117             printf("distance: %d\r\n", distance);
00118         }
00119 
00120         if (int_stop) {
00121             printf("\r\nEnding loop mode \r\n");
00122             break;
00123         }
00124     }
00125 
00126     return status;
00127 
00128 }
00129 
00130 /*=================================== Main ==================================
00131 =============================================================================*/
00132 int main()
00133 {
00134     stop_button.rise(&measuring_stop_irq);
00135 
00136     VL53L1X_DevI2C *device_i2c = new VL53L1X_DevI2C(VL53L1_I2C_SDA, VL53L1_I2C_SCL);
00137     range_measure(device_i2c);  // start continuous measures
00138 }