ST Expansion SW Team / Mbed OS VL6180_MB63_1sensor_singleshot_poll

Dependencies:   X_NUCLEO_6180

Revision:
0:d96995e87b29
Child:
1:43a9bc2f05b5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 29 09:23:00 2020 +0000
@@ -0,0 +1,229 @@
+/**
+  ******************************************************************************
+  * File Name          : main.c
+  * Date               : 29/10/2020
+  * Description        : Main program body
+  ******************************************************************************
+  *
+  * COPYRIGHT(c) 2020 STMicroelectronics
+  *
+  * Redistribution and use in source and binary forms, with or without modification,
+  * are permitted provided that the following conditions are met:
+  *   1. Redistributions of source code must retain the above copyright notice,
+  *      this list of conditions and the following disclaimer.
+  *   2. Redistributions in binary form must reproduce the above copyright notice,
+  *      this list of conditions and the following disclaimer in the documentation
+  *      and/or other materials provided with the distribution.
+  *   3. Neither the name of STMicroelectronics nor the names of its contributors
+  *      may be used to endorse or promote products derived from this software
+  *      without specific prior written permission.
+  *
+  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+  *
+  ******************************************************************************
+    This program controls a VL6180 ToF sensor running on an STM32F401 card with the
+  VL6180 shield.  This program works in single shot mode where the program polls for
+  the measurement finishing. This allows for the display to be updated while the
+  measurement occurs.
+  
+  The display is very crude with no storing of the last digits, each digit is written
+  and then a wait occurs and then the next digit is written. This means that a lot of time is 
+  taken writing the display and any long period when the display is not serviced 
+  will cause the display to flicker.
+  
+  In this program access to the VL6180 api is through wrapper functions in
+  vl6180_class.cpp. It is also possible to access the api directly. E.G both the lines below
+  do the same thing. 
+  
+status = VL6180_ClearInterrupt(dev,INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING);
+status = sensor->vl6180_ClearInterrupt(dev,INTERRUPT_CLEAR_ERROR|INTERRUPT_CLEAR_RANGING);
+  */
+  
+
+/* Includes ------------------------------------------------------------------*/
+//#include "stm32xxx_hal.h"
+
+/* Private variables ---------------------------------------------------------*/
+
+#include <stdio.h>
+
+#include "mbed.h"
+#include "XNucleo6810.h"
+#include <time.h>
+
+#include "spi_interface.h"
+
+
+//VL6180_SINGLE_DEVICE_DRIVER
+
+
+#define VL6180_I2C_SDA   D14 
+#define VL6180_I2C_SCL   D15 
+
+/* USER CODE BEGIN 0 */
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "vl6180_api.h"
+#include "6180a1.h"
+
+#ifdef DEBUG
+    //TODO
+#include "diag/trace.h"
+    #define debug(msg, ...)   trace_printf(msg,__VA_ARGS__)
+    #define trace_warn(msg,...) trace_printf("W %s %d" msg "\n", __func__, __LINE__, __VA_ARGS__)
+#else
+    #define debug(msg, ...)  (void)0
+#endif
+
+#define DigitDisplay_ms     1 /* ms each digit is kept on */
+#if VL6180_HAVE_DMAX_RANGING
+#define DMaxDispTime     0 /* Set to 1000 to display Dmax during 1 sec when no target is detected */
+#else
+#define DMaxDispTime     0
+#endif
+    
+
+MyVL6180Dev_t                   devCentre;  //data  for each of the vl6180
+MyVL6180Dev_t                   devLeft;
+MyVL6180Dev_t                   devRight;
+MyVL6180Dev_t                   devBottom;
+VL6180Dev_t                     Dev = &devCentre; // the currently used vl6180
+
+volatile int IntrFired=0;
+
+VL6180 *Sensor;  
+
+static XNucleo53L1A1 *board=NULL;
+
+Serial pc(SERIAL_TX, SERIAL_RX); 
+
+vl6180_DevI2C *dev_I2C = new vl6180_DevI2C(VL6180_I2C_SDA, VL6180_I2C_SCL);
+
+#define theVL6180Dev   0x52    // what we use as "API device
+
+#define i2c_bus      (&hi2c1)
+#define def_i2c_time_out 100
+
+
+void XNUCLEO6180XA1_WaitMilliSec(int n){
+    wait_ms(n);
+}
+
+
+
+
+/**
+ * DISPLAY public
+ */
+/***************  DISPLAY PUBLIC *********************/
+/***************  DISPLAY PRIVATE *********************/
+static char DISP_CurString[10]; // the strin g to be displayed on the screen
+
+char buffer[10];
+
+
+uint32_t TimeStarted;       /* various display and mode delay starting time */
+VL6180_RangeData_t Range;  /* Range measurmeent  */
+uint16_t range;             /* range average distance */
+
+
+/* USER CODE END 0 */
+
+/* Private function prototypes -----------------------------------------------*/
+
+int main(void)
+{
+    pc.baud(115200);  // baud rate is important as printf statements take a lot of time
+    devCentre.i2c_addr = theVL6180Dev;  // set to default adress for now
+    printf("vl6180_ss_and_poll_ranging \n");
+
+    int status;
+    
+    // create instances for the sensors
+    board = XNucleo53L1A1::instance(dev_I2C, A2, D8, D2 ,D2);  
+    
+        // find the sensors we have and initialise
+    status = board->init_board();
+    if (status) {
+        printf("Failed to init board!\r\n");
+        return 0;
+    }
+    
+    //select centre sensor
+    devCentre.i2c_addr = NEW_SENSOR_CENTRE_ADDRESS;
+    Dev = &devCentre;
+    Sensor=board->sensor_centre;
+    if ( board->sensor_centre ==NULL)
+        printf("board->sensor_centre = Null\n");
+
+    /* Note that as we waited  1msec we could bypass VL6180_WaitDeviceBooted(&Dev); */
+    Sensor->vl6180_WaitDeviceBooted(Dev);
+    Sensor->vl6180_InitData(Dev);
+
+    printf("started interrupt centre %d\n",status);
+    
+    /* Enable Dmax calculation only if value is displayed (to save computation power) */
+    Sensor->vl6180_DMaxSetState(Dev, DMaxDispTime>0);
+    status = Sensor->vl6180_Prepare(Dev);
+    
+    VL6180_ClearInterrupt(Dev, INTERRUPT_CLEAR_RANGING);
+
+    /* kick off the first measurement */
+    status = Sensor->vl6180_RangeStartSingleShot(Dev);
+    
+    printf("start loop \n");
+    /* Infinite loop */
+    while (1) {
+        status = Sensor->vl6180_RangeGetMeasurementIfReady(Dev, &Range);
+        if( status == 0 ){         
+            // Application must check Range.errorStatus before accessing the other data
+            //    If Range.errorStatus is DataNotReady, application knows that it has to wait a bit before getting a new data
+            //    If Range.errorStatus is 0, application knows it is a valid distance
+            //    If Range.errorStatus is not 0, application knows that reported distance is invalid so may take some decisions depending on the errorStatus
+            if (Range.errorStatus == DataNotReady)
+                continue;
+            
+           if((Range.errorStatus == 0) && (status == 0))
+           {
+      //           printf("range = %d\n",Range.range_mm); //output result to serial port
+                 sprintf(DISP_CurString, " %d", (int)Range.range_mm);
+           } 
+           else
+           {
+                 sprintf(DISP_CurString, " %d", 0);
+           }               
+                
+            /* re-arm next measurement */ 
+           status = Sensor->vl6180_RangeStartSingleShot(Dev);   
+        }
+        else{
+            // it is an critical error
+ //           HandleError("critical error on VL6180x_RangeCheckAndGetMeasurement");
+        }
+             
+       
+       // the display is very simple and requires written to frequently so
+       // we are writing to the display when we would normally sleep.
+       // when we are not writing to the display it is blank
+       for (int i = 0 ;i < 20;i++)
+       {
+            XNUCLEO6180XA1_DisplayString(DISP_CurString, DigitDisplay_ms* 5);
+       }
+
+    }
+
+}
+
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/