
Complete interrupt based test application for the STMicrolectronics' X-NUCLEO-6180XA1 Proximity and ambient light sensor expansion board.
Dependencies: X_NUCLEO_6180XA1 mbed
Fork of HelloWorld_6180XA1_AppExample by
Complete interrupt based test application for the STMicrolectronics' X-NUCLEO-6180XA1 Proximity and ambient light sensor expansion board.
The application reads the on board red switch and reports the measured ALS or range accordingly.
A demonstration of resources de-allocation and system restart is also provided by means of Blue Button press.
main.cpp@4:84dfc00ae7b3, 2017-03-13 (annotated)
- Committer:
- Davidroid
- Date:
- Mon Mar 13 19:14:32 2017 +0000
- Revision:
- 4:84dfc00ae7b3
- Parent:
- 3:d3719ebf51c4
Aligned to ARM coding style.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 4:84dfc00ae7b3 | 1 | /* |
Davidroid | 4:84dfc00ae7b3 | 2 | This VL6180X Expansion board test application performs a range measurement |
Davidroid | 4:84dfc00ae7b3 | 3 | and an als measurement in interrupt mode on the onboard embedded top sensor. |
Davidroid | 4:84dfc00ae7b3 | 4 | The board red slider selects on the flight the measurement type as ALS or |
Davidroid | 4:84dfc00ae7b3 | 5 | RANGE; the measured data is diplayed on the on bord 4digits display. |
Davidroid | 4:84dfc00ae7b3 | 6 | |
Davidroid | 4:84dfc00ae7b3 | 7 | User Blue button allows to stop current measurement and the entire program |
Davidroid | 4:84dfc00ae7b3 | 8 | releasing all the resources. |
Davidroid | 4:84dfc00ae7b3 | 9 | Reset button is used to restart the program. |
Davidroid | 4:84dfc00ae7b3 | 10 | |
Davidroid | 4:84dfc00ae7b3 | 11 | Polling operating modes don`t require callback function that handles IRQ |
Davidroid | 4:84dfc00ae7b3 | 12 | callbacks. IRQ functions are used only for measures that require interrupts. |
Davidroid | 4:84dfc00ae7b3 | 13 | |
Davidroid | 4:84dfc00ae7b3 | 14 | Notes: |
Davidroid | 4:84dfc00ae7b3 | 15 | + get_measurement() is asynchronous! It returns NOT_READY if the measurement |
Davidroid | 4:84dfc00ae7b3 | 16 | value is not ready to be read from the corresponding register. So you need |
Davidroid | 4:84dfc00ae7b3 | 17 | to wait for the result to be ready.\ |
Davidroid | 4:84dfc00ae7b3 | 18 | */ |
Davidroid | 4:84dfc00ae7b3 | 19 | |
Davidroid | 4:84dfc00ae7b3 | 20 | |
Davidroid | 4:84dfc00ae7b3 | 21 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 4:84dfc00ae7b3 | 22 | |
mapellil | 0:b706d6b7c1d3 | 23 | #include "mbed.h" |
Davidroid | 4:84dfc00ae7b3 | 24 | #include "XNucleo6180XA1.h" |
mapellil | 0:b706d6b7c1d3 | 25 | #include <string.h> |
mapellil | 0:b706d6b7c1d3 | 26 | #include <stdlib.h> |
mapellil | 0:b706d6b7c1d3 | 27 | #include <stdio.h> |
mapellil | 0:b706d6b7c1d3 | 28 | #include <assert.h> |
mapellil | 0:b706d6b7c1d3 | 29 | |
mapellil | 0:b706d6b7c1d3 | 30 | |
Davidroid | 4:84dfc00ae7b3 | 31 | /* Definitions ---------------------------------------------------------------*/ |
mapellil | 0:b706d6b7c1d3 | 32 | |
mapellil | 0:b706d6b7c1d3 | 33 | #define VL6180X_I2C_SDA D14 |
mapellil | 0:b706d6b7c1d3 | 34 | #define VL6180X_I2C_SCL D15 |
mapellil | 0:b706d6b7c1d3 | 35 | |
mapellil | 0:b706d6b7c1d3 | 36 | #define RANGE 0 |
mapellil | 0:b706d6b7c1d3 | 37 | #define ALS 1 |
mapellil | 0:b706d6b7c1d3 | 38 | |
Davidroid | 3:d3719ebf51c4 | 39 | #define DELAY 2000 // 2Sec |
Davidroid | 3:d3719ebf51c4 | 40 | |
Davidroid | 3:d3719ebf51c4 | 41 | |
Davidroid | 4:84dfc00ae7b3 | 42 | /* Types ---------------------------------------------------------------------*/ |
Davidroid | 4:84dfc00ae7b3 | 43 | |
Davidroid | 4:84dfc00ae7b3 | 44 | /* Operating mode */ |
Davidroid | 4:84dfc00ae7b3 | 45 | operating_mode_t operating_mode, prev_operating_mode; |
Davidroid | 4:84dfc00ae7b3 | 46 | enum op_mode_int_poll_t { |
Davidroid | 4:84dfc00ae7b3 | 47 | PollMeasure, |
Davidroid | 4:84dfc00ae7b3 | 48 | IntMeasure |
Davidroid | 4:84dfc00ae7b3 | 49 | }; |
Davidroid | 4:84dfc00ae7b3 | 50 | |
Davidroid | 4:84dfc00ae7b3 | 51 | |
Davidroid | 4:84dfc00ae7b3 | 52 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 4:84dfc00ae7b3 | 53 | |
Davidroid | 3:d3719ebf51c4 | 54 | /* Expansion board */ |
Davidroid | 4:84dfc00ae7b3 | 55 | static XNucleo6180XA1 *board = NULL; |
Davidroid | 3:d3719ebf51c4 | 56 | |
Davidroid | 3:d3719ebf51c4 | 57 | /* Measure data */ |
Davidroid | 4:84dfc00ae7b3 | 58 | measure_data_t data_sensor_top; |
Davidroid | 3:d3719ebf51c4 | 59 | |
Davidroid | 3:d3719ebf51c4 | 60 | /* Flags that handle interrupt request */ |
Davidroid | 3:d3719ebf51c4 | 61 | bool int_sensor_top = false, int_stop_measure = false; |
mapellil | 0:b706d6b7c1d3 | 62 | |
Davidroid | 4:84dfc00ae7b3 | 63 | |
Davidroid | 4:84dfc00ae7b3 | 64 | /* Functions -----------------------------------------------------------------*/ |
Davidroid | 4:84dfc00ae7b3 | 65 | |
mapellil | 0:b706d6b7c1d3 | 66 | /* ISR callback function of the sensor_top */ |
Davidroid | 4:84dfc00ae7b3 | 67 | void sensor_top_irq(void) |
mapellil | 0:b706d6b7c1d3 | 68 | { |
Davidroid | 3:d3719ebf51c4 | 69 | int_sensor_top = true; |
Davidroid | 4:84dfc00ae7b3 | 70 | board->sensor_top->disable_interrupt_measure_detection_irq(); |
Davidroid | 3:d3719ebf51c4 | 71 | } |
mapellil | 0:b706d6b7c1d3 | 72 | |
mapellil | 0:b706d6b7c1d3 | 73 | /* ISR callback function of the user blue button to stop program */ |
Davidroid | 4:84dfc00ae7b3 | 74 | void stop_measure_irq(void) |
mapellil | 0:b706d6b7c1d3 | 75 | { |
Davidroid | 3:d3719ebf51c4 | 76 | int_stop_measure = true; |
mapellil | 0:b706d6b7c1d3 | 77 | } |
mapellil | 0:b706d6b7c1d3 | 78 | |
mapellil | 0:b706d6b7c1d3 | 79 | /* On board 4 digit local display refresh */ |
Davidroid | 4:84dfc00ae7b3 | 80 | void display_refresh(operating_mode_t op_mode) |
Davidroid | 3:d3719ebf51c4 | 81 | { |
Davidroid | 3:d3719ebf51c4 | 82 | char str[5]; |
Davidroid | 3:d3719ebf51c4 | 83 | |
Davidroid | 3:d3719ebf51c4 | 84 | if (op_mode==range_continuous_interrupt || op_mode==range_continuous_polling) { |
Davidroid | 3:d3719ebf51c4 | 85 | if (data_sensor_top.range_mm!=0xFFFFFFFF) { |
Davidroid | 3:d3719ebf51c4 | 86 | sprintf(str,"%d",data_sensor_top.range_mm); |
Davidroid | 3:d3719ebf51c4 | 87 | } else { |
Davidroid | 3:d3719ebf51c4 | 88 | sprintf(str,"%s","----"); |
Davidroid | 3:d3719ebf51c4 | 89 | } |
Davidroid | 3:d3719ebf51c4 | 90 | } else if (op_mode==als_continuous_interrupt || op_mode==als_continuous_polling) { |
Davidroid | 3:d3719ebf51c4 | 91 | if (data_sensor_top.lux!=0xFFFFFFFF) { |
Davidroid | 3:d3719ebf51c4 | 92 | sprintf(str,"%d",data_sensor_top.lux); |
Davidroid | 3:d3719ebf51c4 | 93 | } else { |
Davidroid | 3:d3719ebf51c4 | 94 | sprintf(str,"%s","----"); |
Davidroid | 3:d3719ebf51c4 | 95 | } |
Davidroid | 3:d3719ebf51c4 | 96 | } |
Davidroid | 4:84dfc00ae7b3 | 97 | board->display->display_string(str, strlen(str)); |
mapellil | 0:b706d6b7c1d3 | 98 | } |
mapellil | 0:b706d6b7c1d3 | 99 | |
mapellil | 0:b706d6b7c1d3 | 100 | /* On board red slider position check */ |
Davidroid | 4:84dfc00ae7b3 | 101 | operating_mode_t check_slider(enum op_mode_int_poll_t op_mode) |
Davidroid | 3:d3719ebf51c4 | 102 | { |
Davidroid | 4:84dfc00ae7b3 | 103 | operating_mode_t ret; |
Davidroid | 4:84dfc00ae7b3 | 104 | int measure = board->rd_switch(); |
mapellil | 0:b706d6b7c1d3 | 105 | |
Davidroid | 4:84dfc00ae7b3 | 106 | switch (op_mode) { |
Davidroid | 3:d3719ebf51c4 | 107 | case PollMeasure: |
Davidroid | 3:d3719ebf51c4 | 108 | if (measure==RANGE) { |
Davidroid | 3:d3719ebf51c4 | 109 | ret = range_continuous_polling; |
Davidroid | 3:d3719ebf51c4 | 110 | } else if (measure==ALS) { |
Davidroid | 3:d3719ebf51c4 | 111 | ret = als_continuous_polling; |
Davidroid | 3:d3719ebf51c4 | 112 | } |
Davidroid | 3:d3719ebf51c4 | 113 | break; |
Davidroid | 3:d3719ebf51c4 | 114 | case IntMeasure: |
Davidroid | 3:d3719ebf51c4 | 115 | if (measure==RANGE) { |
Davidroid | 3:d3719ebf51c4 | 116 | ret = range_continuous_interrupt; |
Davidroid | 3:d3719ebf51c4 | 117 | } else if (measure==ALS) { |
Davidroid | 3:d3719ebf51c4 | 118 | ret = als_continuous_interrupt; |
Davidroid | 3:d3719ebf51c4 | 119 | } |
Davidroid | 3:d3719ebf51c4 | 120 | break; |
Davidroid | 3:d3719ebf51c4 | 121 | } |
Davidroid | 3:d3719ebf51c4 | 122 | return ret; |
mapellil | 0:b706d6b7c1d3 | 123 | } |
mapellil | 0:b706d6b7c1d3 | 124 | |
Davidroid | 4:84dfc00ae7b3 | 125 | /* Print on USB Serial the started operating_mode_t */ |
Davidroid | 4:84dfc00ae7b3 | 126 | void print_start_message(operating_mode_t op_mode) |
mapellil | 0:b706d6b7c1d3 | 127 | { |
Davidroid | 3:d3719ebf51c4 | 128 | if (op_mode==range_continuous_interrupt) { |
Davidroid | 3:d3719ebf51c4 | 129 | printf("\nStarted range continuous interrupt measure\n\r"); |
Davidroid | 3:d3719ebf51c4 | 130 | } else if (prev_operating_mode==als_continuous_interrupt) { |
Davidroid | 3:d3719ebf51c4 | 131 | printf("\nStarted als continuous interrupt measure\n\r"); |
Davidroid | 3:d3719ebf51c4 | 132 | } |
mapellil | 0:b706d6b7c1d3 | 133 | } |
mapellil | 0:b706d6b7c1d3 | 134 | |
Davidroid | 4:84dfc00ae7b3 | 135 | /* Print on USB Serial the stopped operating_mode_t */ |
Davidroid | 4:84dfc00ae7b3 | 136 | void print_stop_message(operating_mode_t op_mode) |
mapellil | 0:b706d6b7c1d3 | 137 | { |
Davidroid | 3:d3719ebf51c4 | 138 | if (op_mode==range_continuous_interrupt) { |
Davidroid | 3:d3719ebf51c4 | 139 | printf("Stopped range continuous interrupt measure\n\r"); |
Davidroid | 3:d3719ebf51c4 | 140 | } else if (prev_operating_mode==als_continuous_interrupt) { |
Davidroid | 3:d3719ebf51c4 | 141 | printf("Stopped als continuous interrupt measure\n\r"); |
Davidroid | 3:d3719ebf51c4 | 142 | } |
mapellil | 0:b706d6b7c1d3 | 143 | } |
mapellil | 0:b706d6b7c1d3 | 144 | |
mapellil | 0:b706d6b7c1d3 | 145 | /* Print on board 4 Digit display the indicated message <= 4 char */ |
Davidroid | 4:84dfc00ae7b3 | 146 | void display_msg(const char * msg) |
mapellil | 0:b706d6b7c1d3 | 147 | { |
Davidroid | 3:d3719ebf51c4 | 148 | Timer timer; |
Davidroid | 3:d3719ebf51c4 | 149 | char str[5]; |
Davidroid | 3:d3719ebf51c4 | 150 | |
Davidroid | 3:d3719ebf51c4 | 151 | timer.start(); |
Davidroid | 3:d3719ebf51c4 | 152 | for (int i=0; i<DELAY; i=timer.read_ms()) |
Davidroid | 3:d3719ebf51c4 | 153 | { |
Davidroid | 3:d3719ebf51c4 | 154 | sprintf(str,"%s",msg); |
Davidroid | 4:84dfc00ae7b3 | 155 | board->display->display_string(str, strlen(str)); |
Davidroid | 3:d3719ebf51c4 | 156 | } |
Davidroid | 3:d3719ebf51c4 | 157 | timer.stop(); |
mapellil | 0:b706d6b7c1d3 | 158 | } |
mapellil | 0:b706d6b7c1d3 | 159 | |
Davidroid | 3:d3719ebf51c4 | 160 | /* Handle continuous ALS or Range measurement. */ |
Davidroid | 4:84dfc00ae7b3 | 161 | void int_continous_als_or_range_measure (DevI2C *device_i2c) { |
Davidroid | 3:d3719ebf51c4 | 162 | int status; |
Davidroid | 3:d3719ebf51c4 | 163 | |
Davidroid | 3:d3719ebf51c4 | 164 | /* Creates the 6180XA1 expansion board singleton obj */ |
Davidroid | 3:d3719ebf51c4 | 165 | #ifdef TARGET_STM32F429 |
Davidroid | 4:84dfc00ae7b3 | 166 | board = XNucleo6180XA1::instance(device_i2c, A5, A2, D13, D2); |
Davidroid | 3:d3719ebf51c4 | 167 | #else |
Davidroid | 4:84dfc00ae7b3 | 168 | board = XNucleo6180XA1::instance(device_i2c, A3, A2, D13, D2); |
Davidroid | 3:d3719ebf51c4 | 169 | #endif |
Davidroid | 4:84dfc00ae7b3 | 170 | display_msg("INT"); |
Davidroid | 3:d3719ebf51c4 | 171 | |
Davidroid | 3:d3719ebf51c4 | 172 | /* Init the 6180XA1 expansion board with default values */ |
Davidroid | 4:84dfc00ae7b3 | 173 | status = board->init_board(); |
Davidroid | 3:d3719ebf51c4 | 174 | if (status) { |
Davidroid | 3:d3719ebf51c4 | 175 | printf("Failed to init board!\n\r"); |
Davidroid | 3:d3719ebf51c4 | 176 | } |
Davidroid | 3:d3719ebf51c4 | 177 | |
Davidroid | 3:d3719ebf51c4 | 178 | /* Check the red slider position for ALS/Range measure */ |
Davidroid | 4:84dfc00ae7b3 | 179 | operating_mode=check_slider(IntMeasure); |
Davidroid | 3:d3719ebf51c4 | 180 | |
Davidroid | 3:d3719ebf51c4 | 181 | /* Start the measure on sensor top */ |
Davidroid | 4:84dfc00ae7b3 | 182 | status = board->sensor_top->start_measurement(operating_mode, sensor_top_irq, NULL, NULL); |
Davidroid | 3:d3719ebf51c4 | 183 | if (!status) { |
Davidroid | 3:d3719ebf51c4 | 184 | prev_operating_mode=operating_mode; |
Davidroid | 4:84dfc00ae7b3 | 185 | print_start_message(operating_mode); |
Davidroid | 3:d3719ebf51c4 | 186 | while (true) { |
Davidroid | 3:d3719ebf51c4 | 187 | if (int_sensor_top) { /* 6180 isr was triggered */ |
Davidroid | 3:d3719ebf51c4 | 188 | int_sensor_top = false; |
Davidroid | 4:84dfc00ae7b3 | 189 | status = board->sensor_top->handle_irq(operating_mode, &data_sensor_top); /* handle the isr and read the meaure */ |
Davidroid | 4:84dfc00ae7b3 | 190 | display_refresh(operating_mode); |
Davidroid | 3:d3719ebf51c4 | 191 | } |
Davidroid | 3:d3719ebf51c4 | 192 | if (int_stop_measure) { /* Blue Button isr was triggered */ |
Davidroid | 4:84dfc00ae7b3 | 193 | status = board->sensor_top->stop_measurement(prev_operating_mode); /* stop the measure and exit */ |
Davidroid | 3:d3719ebf51c4 | 194 | if (!status) { |
Davidroid | 4:84dfc00ae7b3 | 195 | print_stop_message(prev_operating_mode); |
Davidroid | 3:d3719ebf51c4 | 196 | } |
Davidroid | 3:d3719ebf51c4 | 197 | int_stop_measure = false; |
Davidroid | 3:d3719ebf51c4 | 198 | printf("\nProgram stopped!\n\n\r"); |
Davidroid | 3:d3719ebf51c4 | 199 | break; |
Davidroid | 3:d3719ebf51c4 | 200 | } |
Davidroid | 4:84dfc00ae7b3 | 201 | operating_mode = check_slider(IntMeasure); /* check if red slider was moved */ |
Davidroid | 3:d3719ebf51c4 | 202 | if (operating_mode!=prev_operating_mode) { |
Davidroid | 4:84dfc00ae7b3 | 203 | display_refresh(prev_operating_mode); |
Davidroid | 4:84dfc00ae7b3 | 204 | status = board->sensor_top->stop_measurement(prev_operating_mode); /* stop the running measure */ |
Davidroid | 3:d3719ebf51c4 | 205 | if (!status) { |
Davidroid | 4:84dfc00ae7b3 | 206 | print_stop_message(prev_operating_mode); |
Davidroid | 3:d3719ebf51c4 | 207 | } |
Davidroid | 3:d3719ebf51c4 | 208 | prev_operating_mode = operating_mode; |
Davidroid | 4:84dfc00ae7b3 | 209 | status = board->sensor_top->start_measurement(operating_mode, sensor_top_irq, NULL, NULL); /* start the new measure */ |
Davidroid | 3:d3719ebf51c4 | 210 | if (!status) { |
Davidroid | 4:84dfc00ae7b3 | 211 | print_start_message(operating_mode); |
Davidroid | 3:d3719ebf51c4 | 212 | } |
Davidroid | 3:d3719ebf51c4 | 213 | } else { |
Davidroid | 4:84dfc00ae7b3 | 214 | display_refresh(operating_mode); |
Davidroid | 3:d3719ebf51c4 | 215 | } |
Davidroid | 3:d3719ebf51c4 | 216 | } |
Davidroid | 3:d3719ebf51c4 | 217 | } |
Davidroid | 4:84dfc00ae7b3 | 218 | display_msg("BYE"); |
Davidroid | 3:d3719ebf51c4 | 219 | } |
mapellil | 0:b706d6b7c1d3 | 220 | |
mapellil | 0:b706d6b7c1d3 | 221 | /*=================================== Main ================================== |
mapellil | 0:b706d6b7c1d3 | 222 | Move the VL6180X Expansion board red slider to switch between ALS or Range |
mapellil | 0:b706d6b7c1d3 | 223 | measures. |
Davidroid | 4:84dfc00ae7b3 | 224 | Press the blue user button to stop the measurements in progress. |
mapellil | 0:b706d6b7c1d3 | 225 | =============================================================================*/ |
mapellil | 0:b706d6b7c1d3 | 226 | int main() |
mapellil | 0:b706d6b7c1d3 | 227 | { |
Davidroid | 3:d3719ebf51c4 | 228 | #if USER_BUTTON==PC_13 // Cross compiling for Nucleo-F401 |
Davidroid | 3:d3719ebf51c4 | 229 | InterruptIn stop_button (USER_BUTTON); |
Davidroid | 4:84dfc00ae7b3 | 230 | stop_button.rise (&stop_measure_irq); |
mapellil | 0:b706d6b7c1d3 | 231 | #endif |
Davidroid | 3:d3719ebf51c4 | 232 | DevI2C *device_i2c = new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL); |
Davidroid | 3:d3719ebf51c4 | 233 | |
Davidroid | 3:d3719ebf51c4 | 234 | /* Start continous measures Interrupt based */ |
Davidroid | 4:84dfc00ae7b3 | 235 | int_continous_als_or_range_measure (device_i2c); |
mapellil | 0:b706d6b7c1d3 | 236 | } |