You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ut...@apache.org on 2020/01/31 09:41:55 UTC

[mynewt-core] 02/02: bsp: stm32f3discovery: enable LSM303DLHC sensor

This is an automated email from the ASF dual-hosted git repository.

utzig pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit d4847736ce895999c94600476b3280b1bfb65c4f
Author: Fabio Utzig <ut...@apache.org>
AuthorDate: Thu Jan 30 11:13:50 2020 -0300

    bsp: stm32f3discovery: enable LSM303DLHC sensor
    
    Enable the onboard LSM303DLHC sensor; this changes the pinout of I2C_0,
    but the new pins can still be accessed on the same connector.
    
    Signed-off-by: Fabio Utzig <ut...@apache.org>
---
 hw/bsp/stm32f3discovery/pkg.yml       |  6 +++
 hw/bsp/stm32f3discovery/src/hal_bsp.c | 70 ++++++++++++++++++++++++++++++++++-
 hw/bsp/stm32f3discovery/syscfg.yml    | 14 ++++++-
 3 files changed, 87 insertions(+), 3 deletions(-)

diff --git a/hw/bsp/stm32f3discovery/pkg.yml b/hw/bsp/stm32f3discovery/pkg.yml
index 3edf273..ed0f9f9 100644
--- a/hw/bsp/stm32f3discovery/pkg.yml
+++ b/hw/bsp/stm32f3discovery/pkg.yml
@@ -41,3 +41,9 @@ pkg.deps.UART_0:
 
 pkg.deps.UART_1:
     - "@apache-mynewt-core/hw/drivers/uart/uart_hal"
+
+pkg.deps.LSM303DLHC_ONB:
+    - "@apache-mynewt-core/hw/drivers/sensors/lsm303dlhc"
+
+pkg.init:
+    config_lsm303dlhc_sensor: 'MYNEWT_VAL(STM32F3DISCOVERY_SYSINIT_STAGE)'
diff --git a/hw/bsp/stm32f3discovery/src/hal_bsp.c b/hw/bsp/stm32f3discovery/src/hal_bsp.c
index 3471a36..f63bab2 100644
--- a/hw/bsp/stm32f3discovery/src/hal_bsp.c
+++ b/hw/bsp/stm32f3discovery/src/hal_bsp.c
@@ -50,6 +50,19 @@
 #include "mcu/mcu.h"
 #include "os/os_cputime.h"
 
+#if MYNEWT_VAL(LSM303DLHC_ONB)
+#include "lsm303dlhc/lsm303dlhc.h"
+static struct lsm303dlhc lsm303dlhc;
+#endif
+
+#if MYNEWT_VAL(LSM303DLHC_ONB)
+static struct sensor_itf i2c_0_itf = {
+    .si_type = SENSOR_ITF_I2C,
+    .si_num = 0,
+    .si_addr = 0, /* sensor config function sets the addresses */
+};
+#endif
+
 #if MYNEWT_VAL(UART_0)
 static struct uart_dev hal_uart[1];
 
@@ -100,8 +113,8 @@ static struct stm32_hal_i2c_cfg i2c_cfg0 = {
     .hic_i2c = I2C1,
     .hic_rcc_reg = &RCC->APB1ENR,
     .hic_rcc_dev = RCC_APB1ENR_I2C1EN,
-    .hic_pin_sda = MCU_GPIO_PORTB(9),
-    .hic_pin_scl = MCU_GPIO_PORTB(8),
+    .hic_pin_sda = MCU_GPIO_PORTB(7),
+    .hic_pin_scl = MCU_GPIO_PORTB(6),
     .hic_pin_af = GPIO_AF4_I2C1,
     .hic_10bit = 0,
     .hic_timingr = 0x10420F13,    /* 100KHz at 8MHz of SysCoreClock */
@@ -155,6 +168,57 @@ hal_bsp_get_nvic_priority(int irq_num, uint32_t pri)
     return pri;
 }
 
+/**
+ * LSM303DLHC sensor default configuration
+ *
+ * @return 0 on success, non-zero on failure
+ */
+void
+config_lsm303dlhc_sensor(void)
+{
+#if MYNEWT_VAL(LSM303DLHC_ONB)
+    int rc;
+    struct os_dev *dev;
+    struct lsm303dlhc_cfg lsmcfg;
+
+    dev = (struct os_dev *) os_dev_open("lsm303dlhc_0", OS_TIMEOUT_NEVER, NULL);
+    assert(dev != NULL);
+
+    /* read once per sec.  API should take this value in ms. */
+    lsmcfg.accel_rate = LSM303DLHC_ACCEL_RATE_1;
+    lsmcfg.accel_range = LSM303DLHC_ACCEL_RANGE_2;
+    /* Device I2C addr for accelerometer */
+    lsmcfg.acc_addr = LSM303DLHC_ADDR_ACCEL;
+    /* Device I2C addr for magnetometer */
+    lsmcfg.mag_addr = LSM303DLHC_ADDR_MAG;
+    /* Set default mag gain to +/-1.3 gauss */
+    lsmcfg.mag_gain = LSM303DLHC_MAG_GAIN_1_3;
+    /* Set default mag sample rate to 15Hz */
+    lsmcfg.mag_rate = LSM303DLHC_MAG_RATE_15;
+
+    lsmcfg.mask = SENSOR_TYPE_ACCELEROMETER |
+                  SENSOR_TYPE_MAGNETIC_FIELD;
+
+    rc = lsm303dlhc_config((struct lsm303dlhc *)dev, &lsmcfg);
+    assert(rc == 0);
+
+    os_dev_close(dev);
+#endif
+}
+
+static void
+sensor_dev_create(void)
+{
+    int rc;
+    (void)rc;
+
+#if MYNEWT_VAL(LSM303DLHC_ONB)
+    rc = os_dev_create((struct os_dev *)&lsm303dlhc, "lsm303dlhc_0",
+                       OS_DEV_INIT_PRIMARY, 0, lsm303dlhc_init,
+                       (void *)&i2c_0_itf);
+    assert(rc == 0);
+#endif
+}
 
 void
 hal_bsp_init(void)
@@ -208,6 +272,8 @@ hal_bsp_init(void)
     assert(rc == 0);
 #endif
 
+    sensor_dev_create();
+
 #if (MYNEWT_VAL(OS_CPUTIME_TIMER_NUM) >= 0)
     rc = os_cputime_init(MYNEWT_VAL(OS_CPUTIME_FREQ));
     assert(rc == 0);
diff --git a/hw/bsp/stm32f3discovery/syscfg.yml b/hw/bsp/stm32f3discovery/syscfg.yml
index 5c2ab97..fb7f62f 100644
--- a/hw/bsp/stm32f3discovery/syscfg.yml
+++ b/hw/bsp/stm32f3discovery/syscfg.yml
@@ -19,7 +19,7 @@
 
 syscfg.defs:
     STM32_FLASH_SIZE_KB:
-        description: 'Total flash size in KB.'
+        description: 'Total flash size in KB'
         value: 256
 
     UART_0:
@@ -40,6 +40,15 @@ syscfg.defs:
         description: 'Support for timer 2'
         value: 0
 
+    LSM303DLHC_ONB:
+        description: 'Onboard lsm303dlhc sensor'
+        value:  0
+
+    STM32F3DISCOVERY_SYSINIT_STAGE:
+        description: >
+            Sysinit stage for the STM32F3-Discovery BSP
+        value: 400
+
 syscfg.vals:
     REBOOT_LOG_FLASH_AREA: FLASH_AREA_REBOOT_LOG
     CONFIG_FCB_FLASH_AREA: FLASH_AREA_NFFS
@@ -59,3 +68,6 @@ syscfg.vals:
     STM32_CLOCK_APB2_DIVIDER: 'RCC_HCLK_DIV1'
     STM32_FLASH_LATENCY: 'FLASH_LATENCY_2'
     STM32_FLASH_PREFETCH_ENABLE: 1
+
+syscfg.restrictions:
+    - "!LSM303DLHC_ONB || I2C_0"