ST Expansion SW Team / Mbed OS 53L1A1_Interrupts_MbedOS_v6x

Dependencies:   X_NUCLEO_53L1A1_mbed

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