commit 849b383a300fdd7113a54b1d8142348677804196
Author: galberding <galberding@techfak.uni-bielefeld.de>
Date:   Mon Jun 15 11:45:49 2020 +0200

    Add tests for vl53l1x

diff --git a/modules/NUCLEO-L476RG/module.h b/modules/NUCLEO-L476RG/module.h
index 86dbcfa..df2ca53 100644
--- a/modules/NUCLEO-L476RG/module.h
+++ b/modules/NUCLEO-L476RG/module.h
@@ -60,7 +60,7 @@ extern SerialConfig moduleHalProgIfConfig;
  */
 #define MODULE_HAL_RTC                          RTCD1
 
-#if (BOARD_MPU6050_CONNECTED == true) || defined(__DOXYGEN__)
+#if (BOARD_MPU6050_CONNECTED == true || BOARD_VL53L1X_CONNECTED == true) || defined(__DOXYGEN__)
 
 /**
  * @brief   I2C driver to access multiplexer, proximity sensors 5 to 8, power monitors for VSYS4.2, VIO 5.0 and VDD, EEPROM, touch sensor, and fuel gauge (front battery).
@@ -72,7 +72,7 @@ extern SerialConfig moduleHalProgIfConfig;
  */
 extern I2CConfig moduleHalI2c3Config;
 
-#endif /* (BOARD_MPU6050_CONNECTED == true) */
+#endif /* (BOARD_MPU6050_CONNECTED == true || BOARD_VL53L1X_CONNECTED == true) */
 
 /** @} */
 
@@ -92,6 +92,8 @@ extern ROMCONST apalControlGpio_t moduleGpioLed;
  * @brief   User button input signal.
  */
 extern ROMCONST apalControlGpio_t moduleGpioUserButton;
+extern ROMCONST apalControlGpio_t moduleGpioVl53l1xINT;
+extern ROMCONST apalControlGpio_t moduleGpioVl53l1xXSHUT;
 
 /** @} */
 
@@ -102,6 +104,8 @@ extern ROMCONST apalControlGpio_t moduleGpioUserButton;
  */
 /*===========================================================================*/
 
+#include <math.h>
+
 /**
  * @brief   Event flag to be set on a USER_BUTTON interrupt.
  */
@@ -121,8 +125,19 @@ extern ROMCONST char* moduleShellPrompt;
   /* user button */                                                           \
   palSetLineCallback(moduleGpioUserButton.gpio->line, aosSysGetStdGpioCallback(), &moduleGpioUserButton.gpio->line);  \
   palEnableLineEvent(moduleGpioUserButton.gpio->line, APAL2CH_EDGE(moduleGpioUserButton.meta.edge));                  \
+  MODULE_INIT_INTERRUPTS_VL53L1X();                                           \
 }
 
+
+#if (BOARD_VL53L1X_CONNECTED == true)
+  #define MODULE_INIT_INTERRUPTS_VL53L1X() {                                  \
+    palSetLineCallback(moduleGpioVl53l1xINT.gpio->line, aosSysGetStdGpioCallback(), &moduleGpioVl53l1xINT.gpio->line);  \
+    palEnableLineEvent(moduleGpioVl53l1xINT.gpio->line, APAL2CH_EDGE(moduleGpioVl53l1xINT.meta.edge));                  \
+  }
+#else /* (BOARD_VL53L1X_CONNECTED == true) */
+  #define MODULE_INIT_INTERRUPTS_VL53L1X() {}
+#endif /* (BOARD_VL53L1X_CONNECTED == true) */
+
 /**
  * @brief   Test initialization hook.
  */
@@ -131,6 +146,7 @@ extern ROMCONST char* moduleShellPrompt;
   aosShellAddCommand(&aos.shell, &moduleTestLedShellCmd);                     \
   aosShellAddCommand(&aos.shell, &moduleTestButtonShellCmd);                  \
   MODULE_INIT_TEST_MPU6050();                                                 \
+  MODULE_INIT_TEST_VL53L1X();                                                 \
   aosShellAddCommand(&aos.shell, &moduleTestAllShellCmd);                     \
 }
 #if (BOARD_MPU6050_CONNECTED == true)
@@ -141,6 +157,15 @@ extern ROMCONST char* moduleShellPrompt;
   #define MODULE_INIT_TEST_MPU6050() {}
 #endif /* (BOARD_MPU6050_CONNECTED == true) */
 
+#if (BOARD_VL53L1X_CONNECTED == true)
+  #define MODULE_INIT_TEST_VL53L1X() {                                        \
+    aosShellAddCommand(&aos.shell, &moduleTestVL53L1XShellCmd);               \
+    aosShellAddCommand(&aos.shell, &moduleTestVL53L1XInitShellCmd);               \
+  }
+#else /* (BOARD_VL53L1X_CONNECTED == true) */
+  #define MODULE_INIT_TEST_VL53L1X() {}
+#endif /* (BOARD_VL53L1X_CONNECTED == true) */
+
 /**
  * @brief   Periphery communication interfaces initialization hook.
  */
@@ -149,6 +174,7 @@ extern ROMCONST char* moduleShellPrompt;
   sdStart(&MODULE_HAL_PROGIF, &moduleHalProgIfConfig);                        \
   /* MPU6050 demo */                                                          \
   MODULE_INIT_PERIPHERY_IF_MPU6050();                                         \
+  MODULE_INIT_PERIPHERY_IF_VL53L1X();                                         \
 }
 #if (BOARD_MPU6050_CONNECTED == true)
   #define MODULE_INIT_PERIPHERY_IF_MPU6050() {                                \
@@ -179,6 +205,43 @@ extern ROMCONST char* moduleShellPrompt;
   #define MODULE_INIT_PERIPHERY_IF_MPU6050() {}
 #endif /* (BOARD_MPU6050_CONNECTED == true) */
 
+#if (BOARD_VL53L1X_CONNECTED == true)
+  #define MODULE_INIT_PERIPHERY_IF_VL53L1X() {                                \
+    /* maximum I2C frequency is 1MHz for this MCU */                          \
+    uint32_t i2c3_freq = 1000000;                                             \
+    /* find minimum amon all devices connected to this bus */                 \
+    i2c3_freq = (VL53L1X_LLD_I2C_MAXFREQUENCY < i2c3_freq) ? VL53L1X_LLD_I2C_MAXFREQUENCY : i2c3_freq;  \
+    /* calculate PRESC (prescaler):                                           \
+     *   target is 1/(I2CXCLK * (PRESC + 1)) = 125ns                          \
+     */                                                                       \
+    moduleHalI2c3Config.timingr = ((uint8_t)((0.000000125f * STM32_I2C3CLK) - 1)) << I2C_TIMINGR_PRESC_Pos; \
+    /* SCL shall be low half of the time. */                                  \
+    moduleHalI2c3Config.timingr |= ((uint8_t)((1.f / i2c3_freq / 2 / 0.000000125f) - 1)) << I2C_TIMINGR_SCLL_Pos; \
+    /* SCL shall be high half the time of low or slightly longer. */          \
+    moduleHalI2c3Config.timingr |= (uint8_t)(ceilf(((moduleHalI2c3Config.timingr & I2C_TIMINGR_SCLL_Msk) >> I2C_TIMINGR_SCLL_Pos) / 2.f)) << I2C_TIMINGR_SCLH_Pos;  \
+    /* SDA shall be delayed 1/10 of SCL low, or shorter */                    \
+    moduleHalI2c3Config.timingr |= (uint8_t)(((moduleHalI2c3Config.timingr & I2C_TIMINGR_SCLL_Msk) >> I2C_TIMINGR_SCLL_Pos) * 0.1f) << I2C_TIMINGR_SDADEL_Pos;  \
+    /* SCL shall be delyed twice as long as SDA, but longer than 0. */        \
+    moduleHalI2c3Config.timingr |= ((((moduleHalI2c3Config.timingr & I2C_TIMINGR_SDADEL_Msk) >> I2C_TIMINGR_SDADEL_Pos) * 2) + 1) << I2C_TIMINGR_SCLDEL_Pos;  \
+    /* setup the GPIO signals */                                              \
+    moduleHalI2c3Config.timingr = 0x00702991; \
+    chSysLock();                                \
+    PAL_PORT(moduleLldVl53l1x.xshut->gpio->line)->PUPDR &= ~(3U << (PAL_PAD(moduleLldVl53l1x.xshut->gpio->line) * 2U)); \
+    PAL_PORT(moduleLldVl53l1x.xshut->gpio->line)->PUPDR |= PIN_PUPDR_PULLUP(PAL_PAD(moduleLldVl53l1x.xshut->gpio->line)); \
+    /* palSetLineMode(moduleLldVl53l1x.xshut->gpio->line, PAL_MODE_OUTPUT_OPENDRAIN); \*/ \
+      palSetLineMode(moduleLldVl53l1x.xshut->gpio->line, PAL_MODE_OUTPUT_PUSHPULL); \
+palSetLineMode(moduleLldVl53l1x.gpio1->gpio->line, PAL_MODE_INPUT_PULLUP); \
+    palSetLineMode(LINE_ARD_A4, PAL_MODE_ALTERNATE(4));                       \
+    palSetLineMode(LINE_ARD_A5, PAL_MODE_ALTERNATE(4));                       \
+    chSysUnlock();                                                            \
+    apalControlGpioSet(moduleLldVl53l1x.xshut, APAL_GPIO_OFF);                \
+    /* now we can start the I2C driver */                                     \
+    i2cStart(&MODULE_HAL_I2C3, &moduleHalI2c3Config);                         \
+  }
+#else /* (BOARD_VL53L1X_CONNECTED == true) */
+  #define MODULE_INIT_PERIPHERY_IF_VL53L1X() {}
+#endif /* (BOARD_VL53L1X_CONNECTED == true) */
+
 /**
  * @brief   Periphery communication interface deinitialization hook.
  */
@@ -226,6 +289,14 @@ extern ROMCONST char* moduleShellPrompt;
  */
 extern LEDDriver moduleLldLed;
 
+#if (BOARD_VL53L1X_CONNECTED == true) || defined(__DOXYGEN__)
+
+  #include <alld_VL53L1X.h>
+
+  extern VL53L1XDriver moduleLldVl53l1x;
+
+#endif /* (BOARD_VL53L1X_CONNECTED == true) */
+
 /**
  * @brief   Button driver.
  */
@@ -258,6 +329,12 @@ extern MPU6050Driver moduleLldMpu6050;
 extern aos_shellcommand_t moduleTestLedShellCmd;
 
 /**
+ * @brief   VL53L1X test command.
+ */
+extern aos_shellcommand_t moduleTestVL53L1XShellCmd;
+extern aos_shellcommand_t moduleTestVL53L1XInitShellCmd;
+
+/**
  * @brief   User button test command.
  */
 extern aos_shellcommand_t moduleTestButtonShellCmd;
