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 ST Expansion SW Team

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.

Committer:
mapellil
Date:
Mon Sep 12 11:49:48 2016 +0000
Revision:
0:b706d6b7c1d3
Child:
3:d3719ebf51c4
First commit of 6180XA1 complex application example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mapellil 0:b706d6b7c1d3 1 #include "mbed.h"
mapellil 0:b706d6b7c1d3 2 #include "x_nucleo_6180xa1.h"
mapellil 0:b706d6b7c1d3 3 #include <string.h>
mapellil 0:b706d6b7c1d3 4 #include <stdlib.h>
mapellil 0:b706d6b7c1d3 5 #include <stdio.h>
mapellil 0:b706d6b7c1d3 6 #include <assert.h>
mapellil 0:b706d6b7c1d3 7
mapellil 0:b706d6b7c1d3 8 /* This VL6180X Expansion board test application performs a range measurement and an als measurement in interrupt mode
mapellil 0:b706d6b7c1d3 9 on the onboard embedded top sensor.
mapellil 0:b706d6b7c1d3 10 The board red slider select on the flight the measurement type as ALS or RANGE; the measured data is diplayed on the
mapellil 0:b706d6b7c1d3 11 on bord 4digits display.
mapellil 0:b706d6b7c1d3 12
mapellil 0:b706d6b7c1d3 13 User Blue button allows to stop current measurement and the entire program releasing all the resources.
mapellil 0:b706d6b7c1d3 14 Reset button is used to restart the program. */
mapellil 0:b706d6b7c1d3 15
mapellil 0:b706d6b7c1d3 16 /* Polling operating modes don`t require callback function that handles IRQ
mapellil 0:b706d6b7c1d3 17 Callback IRQ functions are used only for measure that require interrupt */
mapellil 0:b706d6b7c1d3 18
mapellil 0:b706d6b7c1d3 19 /* GetMeasurement is asynchronous! It returns NOT_READY if the measurement value
mapellil 0:b706d6b7c1d3 20 is not ready to be read from the corresponding register. So you need to wait
mapellil 0:b706d6b7c1d3 21 for the result to be ready */
mapellil 0:b706d6b7c1d3 22
mapellil 0:b706d6b7c1d3 23 #define VL6180X_I2C_SDA D14
mapellil 0:b706d6b7c1d3 24 #define VL6180X_I2C_SCL D15
mapellil 0:b706d6b7c1d3 25
mapellil 0:b706d6b7c1d3 26 #define RANGE 0
mapellil 0:b706d6b7c1d3 27 #define ALS 1
mapellil 0:b706d6b7c1d3 28
mapellil 0:b706d6b7c1d3 29 static X_NUCLEO_6180XA1 *board=NULL;
mapellil 0:b706d6b7c1d3 30 MeasureData_t data_sensor_top;
mapellil 0:b706d6b7c1d3 31 OperatingMode operating_mode, prev_operating_mode;
mapellil 0:b706d6b7c1d3 32
mapellil 0:b706d6b7c1d3 33 /* flags that handle interrupt request */
mapellil 0:b706d6b7c1d3 34 bool int_sensor_top=false, int_stop_measure=false;
mapellil 0:b706d6b7c1d3 35
mapellil 0:b706d6b7c1d3 36 /* ISR callback function of the sensor_top */
mapellil 0:b706d6b7c1d3 37 void SensorTopIRQ(void)
mapellil 0:b706d6b7c1d3 38 {
mapellil 0:b706d6b7c1d3 39 int_sensor_top=true;
mapellil 0:b706d6b7c1d3 40 board->sensor_top->DisableInterruptMeasureDetectionIRQ();
mapellil 0:b706d6b7c1d3 41 }
mapellil 0:b706d6b7c1d3 42
mapellil 0:b706d6b7c1d3 43 /* ISR callback function of the user blue button to stop program */
mapellil 0:b706d6b7c1d3 44 void StopMeasureIRQ(void)
mapellil 0:b706d6b7c1d3 45 {
mapellil 0:b706d6b7c1d3 46 int_stop_measure=true;
mapellil 0:b706d6b7c1d3 47 }
mapellil 0:b706d6b7c1d3 48
mapellil 0:b706d6b7c1d3 49 /* On board 4 digit local display refresh */
mapellil 0:b706d6b7c1d3 50 void DisplayRefresh(OperatingMode op_mode)
mapellil 0:b706d6b7c1d3 51 {
mapellil 0:b706d6b7c1d3 52 char str[5];
mapellil 0:b706d6b7c1d3 53
mapellil 0:b706d6b7c1d3 54 if(op_mode==range_continuous_interrupt || op_mode==range_continuous_polling)
mapellil 0:b706d6b7c1d3 55 {
mapellil 0:b706d6b7c1d3 56 if(data_sensor_top.range_mm!=0xFFFFFFFF)
mapellil 0:b706d6b7c1d3 57 {
mapellil 0:b706d6b7c1d3 58 sprintf(str,"%d",data_sensor_top.range_mm);
mapellil 0:b706d6b7c1d3 59 }
mapellil 0:b706d6b7c1d3 60 else
mapellil 0:b706d6b7c1d3 61 {
mapellil 0:b706d6b7c1d3 62 sprintf(str,"%s","----");
mapellil 0:b706d6b7c1d3 63 }
mapellil 0:b706d6b7c1d3 64 }
mapellil 0:b706d6b7c1d3 65 else if(op_mode==als_continuous_interrupt || op_mode==als_continuous_polling)
mapellil 0:b706d6b7c1d3 66 {
mapellil 0:b706d6b7c1d3 67 if(data_sensor_top.lux!=0xFFFFFFFF)
mapellil 0:b706d6b7c1d3 68 {
mapellil 0:b706d6b7c1d3 69 sprintf(str,"%d",data_sensor_top.lux);
mapellil 0:b706d6b7c1d3 70 }
mapellil 0:b706d6b7c1d3 71 else
mapellil 0:b706d6b7c1d3 72 {
mapellil 0:b706d6b7c1d3 73 sprintf(str,"%s","----");
mapellil 0:b706d6b7c1d3 74 }
mapellil 0:b706d6b7c1d3 75 }
mapellil 0:b706d6b7c1d3 76 board->display->DisplayString(str, strlen(str));
mapellil 0:b706d6b7c1d3 77 }
mapellil 0:b706d6b7c1d3 78
mapellil 0:b706d6b7c1d3 79 /* On board red slider position check */
mapellil 0:b706d6b7c1d3 80 enum OpModeIntPoll_t{ PollMeasure, IntMeasure };
mapellil 0:b706d6b7c1d3 81
mapellil 0:b706d6b7c1d3 82 OperatingMode CheckSlider(enum OpModeIntPoll_t OpMode) {
mapellil 0:b706d6b7c1d3 83
mapellil 0:b706d6b7c1d3 84 OperatingMode ret;
mapellil 0:b706d6b7c1d3 85 int measure= board->RdSwitch();
mapellil 0:b706d6b7c1d3 86
mapellil 0:b706d6b7c1d3 87 switch (OpMode) {
mapellil 0:b706d6b7c1d3 88 case PollMeasure:
mapellil 0:b706d6b7c1d3 89 if(measure==RANGE)
mapellil 0:b706d6b7c1d3 90 ret = range_continuous_polling;
mapellil 0:b706d6b7c1d3 91 else if(measure==ALS)
mapellil 0:b706d6b7c1d3 92 ret = als_continuous_polling;
mapellil 0:b706d6b7c1d3 93 break;
mapellil 0:b706d6b7c1d3 94
mapellil 0:b706d6b7c1d3 95 case IntMeasure:
mapellil 0:b706d6b7c1d3 96 if(measure==RANGE)
mapellil 0:b706d6b7c1d3 97 ret = range_continuous_interrupt;
mapellil 0:b706d6b7c1d3 98 else if(measure==ALS)
mapellil 0:b706d6b7c1d3 99 ret = als_continuous_interrupt;
mapellil 0:b706d6b7c1d3 100 break;
mapellil 0:b706d6b7c1d3 101 }
mapellil 0:b706d6b7c1d3 102 return ret;
mapellil 0:b706d6b7c1d3 103 }
mapellil 0:b706d6b7c1d3 104
mapellil 0:b706d6b7c1d3 105 /* Print on USB Serial the started OperatingMode */
mapellil 0:b706d6b7c1d3 106 void PrintStartMessage(OperatingMode op_mode)
mapellil 0:b706d6b7c1d3 107 {
mapellil 0:b706d6b7c1d3 108 if(op_mode==range_continuous_interrupt)
mapellil 0:b706d6b7c1d3 109 printf("\nStarted range continuous interrupt measure\n\r");
mapellil 0:b706d6b7c1d3 110 else if(prev_operating_mode==als_continuous_interrupt)
mapellil 0:b706d6b7c1d3 111 printf("\nStarted als continuous interrupt measure\n\r");
mapellil 0:b706d6b7c1d3 112 }
mapellil 0:b706d6b7c1d3 113
mapellil 0:b706d6b7c1d3 114 /* Print on USB Serial the stopped OperatingMode */
mapellil 0:b706d6b7c1d3 115 void PrintStopMessage(OperatingMode op_mode)
mapellil 0:b706d6b7c1d3 116 {
mapellil 0:b706d6b7c1d3 117 if(op_mode==range_continuous_interrupt)
mapellil 0:b706d6b7c1d3 118 printf("Stopped range continuous interrupt measure\n\r");
mapellil 0:b706d6b7c1d3 119 else if(prev_operating_mode==als_continuous_interrupt)
mapellil 0:b706d6b7c1d3 120 printf("Stopped als continuous interrupt measure\n\r");
mapellil 0:b706d6b7c1d3 121 }
mapellil 0:b706d6b7c1d3 122
mapellil 0:b706d6b7c1d3 123 /* Print on board 4 Digit display the indicated message <= 4 char */
mapellil 0:b706d6b7c1d3 124 #define DELAY 2000 // 2Sec
mapellil 0:b706d6b7c1d3 125 void DisplayMsg(const char * msg)
mapellil 0:b706d6b7c1d3 126 {
mapellil 0:b706d6b7c1d3 127 Timer timer;
mapellil 0:b706d6b7c1d3 128 char str[5];
mapellil 0:b706d6b7c1d3 129
mapellil 0:b706d6b7c1d3 130 timer.start();
mapellil 0:b706d6b7c1d3 131 for(int i=0; i<DELAY; i=timer.read_ms())
mapellil 0:b706d6b7c1d3 132 {
mapellil 0:b706d6b7c1d3 133 sprintf(str,"%s",msg);
mapellil 0:b706d6b7c1d3 134 board->display->DisplayString(str, strlen(str));
mapellil 0:b706d6b7c1d3 135 }
mapellil 0:b706d6b7c1d3 136 timer.stop();
mapellil 0:b706d6b7c1d3 137 }
mapellil 0:b706d6b7c1d3 138
mapellil 0:b706d6b7c1d3 139
mapellil 0:b706d6b7c1d3 140 void IntContinousALSorRangeMeasure (DevI2C *device_i2c) {
mapellil 0:b706d6b7c1d3 141 int status;
mapellil 0:b706d6b7c1d3 142 /* creates the 6180XA1 expansion board singleton obj */
mapellil 0:b706d6b7c1d3 143 board=X_NUCLEO_6180XA1::Instance(device_i2c, A3, A2, D13, D2);
mapellil 0:b706d6b7c1d3 144 DisplayMsg ("INT");
mapellil 0:b706d6b7c1d3 145 /* init the 6180XA1 expansion board with default values */
mapellil 0:b706d6b7c1d3 146 status=board->InitBoard();
mapellil 0:b706d6b7c1d3 147 if(status)
mapellil 0:b706d6b7c1d3 148 printf("Failed to init board!\n\r");
mapellil 0:b706d6b7c1d3 149 /* check the red slider position for ALS/Range measure */
mapellil 0:b706d6b7c1d3 150 operating_mode=CheckSlider(IntMeasure);
mapellil 0:b706d6b7c1d3 151 /* start the measure on sensor top */
mapellil 0:b706d6b7c1d3 152 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL);
mapellil 0:b706d6b7c1d3 153 if(!status)
mapellil 0:b706d6b7c1d3 154 {
mapellil 0:b706d6b7c1d3 155 prev_operating_mode=operating_mode;
mapellil 0:b706d6b7c1d3 156 PrintStartMessage(operating_mode);
mapellil 0:b706d6b7c1d3 157 while(1)
mapellil 0:b706d6b7c1d3 158 {
mapellil 0:b706d6b7c1d3 159 if(int_sensor_top) /* 6180 isr was triggered */
mapellil 0:b706d6b7c1d3 160 {
mapellil 0:b706d6b7c1d3 161 int_sensor_top=false;
mapellil 0:b706d6b7c1d3 162 status=board->sensor_top->HandleIRQ(operating_mode, &data_sensor_top); /* handle the isr and read the meaure */
mapellil 0:b706d6b7c1d3 163 DisplayRefresh(operating_mode);
mapellil 0:b706d6b7c1d3 164 }
mapellil 0:b706d6b7c1d3 165 if(int_stop_measure) /* Blue Button isr was triggered */
mapellil 0:b706d6b7c1d3 166 {
mapellil 0:b706d6b7c1d3 167 status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the measure and exit */
mapellil 0:b706d6b7c1d3 168 if(!status)
mapellil 0:b706d6b7c1d3 169 PrintStopMessage(prev_operating_mode);
mapellil 0:b706d6b7c1d3 170 int_stop_measure = false;
mapellil 0:b706d6b7c1d3 171 printf("\nProgram stopped!\n\n\r");
mapellil 0:b706d6b7c1d3 172 break;
mapellil 0:b706d6b7c1d3 173 }
mapellil 0:b706d6b7c1d3 174 operating_mode=CheckSlider(IntMeasure); /* check if red slider was moved */
mapellil 0:b706d6b7c1d3 175 if(operating_mode!=prev_operating_mode)
mapellil 0:b706d6b7c1d3 176 {
mapellil 0:b706d6b7c1d3 177 DisplayRefresh(prev_operating_mode);
mapellil 0:b706d6b7c1d3 178 status=board->sensor_top->StopMeasurement(prev_operating_mode); /* stop the running measure */
mapellil 0:b706d6b7c1d3 179 if(!status)
mapellil 0:b706d6b7c1d3 180 PrintStopMessage(prev_operating_mode);
mapellil 0:b706d6b7c1d3 181 prev_operating_mode=operating_mode;
mapellil 0:b706d6b7c1d3 182 status=board->sensor_top->StartMeasurement(operating_mode, SensorTopIRQ, NULL, NULL); /* start the new measure */
mapellil 0:b706d6b7c1d3 183 if(!status)
mapellil 0:b706d6b7c1d3 184 PrintStartMessage(operating_mode);
mapellil 0:b706d6b7c1d3 185 } else
mapellil 0:b706d6b7c1d3 186 DisplayRefresh(operating_mode);
mapellil 0:b706d6b7c1d3 187 }
mapellil 0:b706d6b7c1d3 188 }
mapellil 0:b706d6b7c1d3 189 DisplayMsg("BYE");
mapellil 0:b706d6b7c1d3 190 }
mapellil 0:b706d6b7c1d3 191
mapellil 0:b706d6b7c1d3 192 /*=================================== Main ==================================
mapellil 0:b706d6b7c1d3 193 Move the VL6180X Expansion board red slider to switch between ALS or Range
mapellil 0:b706d6b7c1d3 194 measures.
mapellil 0:b706d6b7c1d3 195 Press the blue user button to stop the measurements in progress
mapellil 0:b706d6b7c1d3 196 =============================================================================*/
mapellil 0:b706d6b7c1d3 197 int main()
mapellil 0:b706d6b7c1d3 198 {
mapellil 0:b706d6b7c1d3 199 #if USER_BUTTON==PC_13 // we are cross compiling for Nucleo-f401
mapellil 0:b706d6b7c1d3 200 InterruptIn stop_button (USER_BUTTON);
mapellil 0:b706d6b7c1d3 201 stop_button.rise (&StopMeasureIRQ);
mapellil 0:b706d6b7c1d3 202 #endif
mapellil 0:b706d6b7c1d3 203 DevI2C *device_i2c =new DevI2C(VL6180X_I2C_SDA, VL6180X_I2C_SCL);
mapellil 0:b706d6b7c1d3 204
mapellil 0:b706d6b7c1d3 205 IntContinousALSorRangeMeasure (device_i2c); // start continous measures Interrupt based
mapellil 0:b706d6b7c1d3 206 }
mapellil 0:b706d6b7c1d3 207