commit db6bd9f855ec7e35156e021297f6dfa9c1c1be8f
Author: Thomas Schöpping <tschoepp@cit-ec.uni-bielefeld.de>
Date:   Tue Jun 23 16:58:50 2020 +0200

    AMiRo-LLD updated and MPU-6050 test reworked (for NUCLEO-L476RG module).

diff --git a/modules/NUCLEO-L476RG/module.h b/modules/NUCLEO-L476RG/module.h
index 52e891b..e53d27d 100644
--- a/modules/NUCLEO-L476RG/module.h
+++ b/modules/NUCLEO-L476RG/module.h
@@ -64,19 +64,15 @@ extern SerialConfig moduleHalProgIfConfig;
  */
 #define MODULE_HAL_RTC                          RTCD1
 
-#if (BOARD_MPU6050_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).
+ * @brief   Default I2C interface for the NUCLEO I/O.
  */
-#define MODULE_HAL_I2C3                         I2CD3
+#define MODULE_HAL_I2C                          I2CD1
 
 /**
- * @brief   Configuration for the I2C driver #3.
+ * @brief   Configuration for the I2C driver #1.
  */
-extern I2CConfig moduleHalI2c3Config;
-
-#endif /* (BOARD_MPU6050_CONNECTED == true) */
+extern I2CConfig moduleHalI2cConfig;
 
 /** @} */
 
@@ -151,42 +147,32 @@ extern ROMCONST char* moduleShellPrompt;
 #define MODULE_INIT_PERIPHERY_IF() {                                          \
   /* serial driver */                                                         \
   sdStart(&MODULE_HAL_PROGIF, &moduleHalProgIfConfig);                        \
-  /* MPU6050 demo */                                                          \
-  MODULE_INIT_PERIPHERY_IF_MPU6050();                                         \
+  /* I2C */                                                                   \
+  uint32_t i2c_freq = 1000000; /* maximum I2C frequency supported for this MCU */ \
+  MODULE_INIT_PERIPHERY_IF_MPU6050(i2c_freq);                                 \
+  if (i2c_freq == 100000) /* standard-mode @ 100 kHz */ {                     \
+    moduleHalI2cConfig.timingr = 0x10909CEC; /* obtained via STM32CubeMX with STM32_I2CxCLK = 80 MHz */ \
+  } else if (i2c_freq == 400000) /* fast-mode @ 400 kHz */ {                  \
+    moduleHalI2cConfig.timingr = 0x00702991; /* obtained via STM32CubeMX with STM32_I2CxCLK = 80 MHz */ \
+  } else if (i2c_freq == 1000000) /* fast-mode-plus @ 10000 kHz */ {          \
+    moduleHalI2cConfig.timingr = 0x00300F33; /* obtained via STM32CubeMX with STM32_I2CxCLK = 80 MHz */ \
+  } else {                                                                    \
+    aosDbgAssertMsg(false, "I2C frequency not supported");                    \
+  }                                                                           \
+  i2cStart(&MODULE_HAL_I2C, &moduleHalI2cConfig);                             \
 }
 #if (BOARD_MPU6050_CONNECTED == true)
-  #define MODULE_INIT_PERIPHERY_IF_MPU6050() {                                \
-    /* maximum I2C frequency is 1MHz for this MCU */                          \
-    uint32_t i2c3_freq = 1000000;                                             \
-    /* find minimum amon all devices connected to this bus */                 \
-    i2c3_freq = (MPU6050_LLD_I2C_MAXFREQUENCY < i2c3_freq) ? MPU6050_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;  \
-    /* now we can start the I2C driver */                                     \
-    chSysLock();                                                              \
-    palSetLineMode(LINE_ARD_A4, PAL_MODE_ALTERNATE(4));                       \
-    palSetLineMode(LINE_ARD_A5, PAL_MODE_ALTERNATE(4));                       \
-    chSysUnlock();                                                            \
-    i2cStart(&MODULE_HAL_I2C3, &moduleHalI2c3Config);                         \
-  }
-#else /* (BOARD_MPU6050_CONNECTED == true) */
-  #define MODULE_INIT_PERIPHERY_IF_MPU6050() {}
-#endif /* (BOARD_MPU6050_CONNECTED == true) */
+  #define MODULE_INIT_PERIPHERY_IF_MPU6050(freq) {freq = (MPU6050_LLD_I2C_MAXFREQUENCY < freq) ? MPU6050_LLD_I2C_MAXFREQUENCY : freq;}
+#else
+  #define MODULE_INIT_PERIPHERY_IF_MPU6050(freq) {}
+#endif
 
 /**
  * @brief   Periphery communication interface deinitialization hook.
  */
 #define MODULE_SHUTDOWN_PERIPHERY_IF() {                                      \
+  /* I2C */                                                                   \
+  i2cStop(&MODULE_HAL_I2C);                                                   \
   /* don't stop the serial driver so messages can still be printed */         \
 }
 
