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 2018/04/18 21:56:34 UTC

[mynewt-core] 11/13: Add i2c support for stm32f7

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 da66d08859b45e6a62e27e70a6955c8b5a15cae8
Author: Fabio Utzig <ut...@apache.org>
AuthorDate: Fri Mar 23 09:49:54 2018 -0300

    Add i2c support for stm32f7
    
    - Added i2c config for nucleo f767
---
 hw/bsp/nucleo-f401re/src/hal_bsp.c                 |   2 +-
 hw/bsp/stm32f767-nucleo/src/hal_bsp.c              |  28 +++-
 hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h       |   5 +
 .../stm32f7xx/include/mcu/stm32f7xx_mynewt_hal.h   |   4 +-
 hw/mcu/stm/stm32f7xx/src/hal_i2c.c                 | 177 ---------------------
 hw/mcu/stm/stm32f7xx/syscfg.yml                    |   3 +
 6 files changed, 38 insertions(+), 181 deletions(-)

diff --git a/hw/bsp/nucleo-f401re/src/hal_bsp.c b/hw/bsp/nucleo-f401re/src/hal_bsp.c
index 65eaf42..05fb6ce 100644
--- a/hw/bsp/nucleo-f401re/src/hal_bsp.c
+++ b/hw/bsp/nucleo-f401re/src/hal_bsp.c
@@ -68,7 +68,7 @@ static const struct hal_bsp_mem_dump dump_cfg[] = {
 
 #if MYNEWT_VAL(I2C_0)
 /*
- * The PB8 and PB9 pins are connected through jumpers in the board to
+ * NOTE: The PB8 and PB9 pins are connected through jumpers in the board to
  * both AIN and I2C pins. To enable I2C functionality SB51/SB56 need to
  * be removed (they are the default connections) and SB46/SB52 need to
  * be added.
diff --git a/hw/bsp/stm32f767-nucleo/src/hal_bsp.c b/hw/bsp/stm32f767-nucleo/src/hal_bsp.c
index f79ec4d..838aaa0 100644
--- a/hw/bsp/stm32f767-nucleo/src/hal_bsp.c
+++ b/hw/bsp/stm32f767-nucleo/src/hal_bsp.c
@@ -27,6 +27,7 @@
 
 #include <hal/hal_bsp.h>
 #include <hal/hal_gpio.h>
+#include "hal/hal_i2c.h"
 #include <hal/hal_flash_int.h>
 #include <hal/hal_system.h>
 #include <hal/hal_timer.h>
@@ -39,6 +40,7 @@
 #include <stm32f7xx_hal_gpio_ex.h>
 #include "mcu/stm32_hal.h"
 #include <mcu/stm32f7_bsp.h>
+#include <mcu/stm32f7xx_mynewt_hal.h>
 
 #if MYNEWT_VAL(ETH_0)
 #include <stm32_eth/stm32_eth.h>
@@ -71,7 +73,26 @@ struct stm32_hal_spi_cfg spi0_cfg = {
     .miso_pin = MCU_GPIO_PORTA(6),           /* D12 on CN7 */
     .mosi_pin = MCU_GPIO_PORTA(7),           /* D11 on CN7 */
     .ss_pin   = MCU_GPIO_PORTD(14),          /* D10 on CN7 */
-    .irq_prio = 2
+    .irq_prio = 2,
+};
+#endif
+
+#if MYNEWT_VAL(I2C_0)
+/*
+ * The PB8 and PB9 pins are connected through jumpers in the board to
+ * both ADC_IN and I2C pins. To enable I2C functionality SB147/SB157 need
+ * to be removed (they are the default connections) and SB138/SB143 need
+ * to be shorted.
+ */
+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),    /* D14 on CN7 */
+    .hic_pin_scl = MCU_GPIO_PORTB(8),    /* D15 on CN7 */
+    .hic_pin_af = GPIO_AF4_I2C1,
+    .hic_10bit = 0,
+    .hic_timingr = 0x30420F13,    /* 100KHz at 16MHz of SysCoreClock */
 };
 #endif
 
@@ -170,6 +191,11 @@ hal_bsp_init(void)
     assert(rc == 0);
 #endif
 
+#if MYNEWT_VAL(I2C_0)
+    rc = hal_i2c_init(0, &i2c_cfg0);
+    assert(rc == 0);
+#endif
+
 #if MYNEWT_VAL(TIMER_0)
     hal_timer_init(0, TIM1);
 #endif
diff --git a/hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h b/hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h
index 1e77343..758c476 100644
--- a/hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h
+++ b/hw/mcu/stm/stm32f7xx/include/mcu/stm32_hal.h
@@ -62,6 +62,11 @@ struct stm32_hal_spi_cfg {
     int irq_prio;
 };
 
+/* hal_i2c */
+#include "stm32f7xx_hal_i2c.h"
+#include "mcu/stm32f7xx_mynewt_hal.h"
+#include "mcu/stm32f7_bsp.h"
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/hw/mcu/stm/stm32f7xx/include/mcu/stm32f7xx_mynewt_hal.h b/hw/mcu/stm/stm32f7xx/include/mcu/stm32f7xx_mynewt_hal.h
index 01a3092..0cd7721 100644
--- a/hw/mcu/stm/stm32f7xx/include/mcu/stm32f7xx_mynewt_hal.h
+++ b/hw/mcu/stm/stm32f7xx/include/mcu/stm32f7xx_mynewt_hal.h
@@ -48,7 +48,7 @@ extern "C" {
 int hal_gpio_init_stm(int pin, GPIO_InitTypeDef *cfg);
 int hal_gpio_deinit_stm(int pin, GPIO_InitTypeDef *cfg);
 
-struct stm32f7_hal_i2c_cfg {
+struct stm32_hal_i2c_cfg {
     I2C_TypeDef *hic_i2c;
     volatile uint32_t *hic_rcc_reg;      /* RCC register to modify */
     uint32_t hic_rcc_dev;                /* RCC device ID */
@@ -56,7 +56,7 @@ struct stm32f7_hal_i2c_cfg {
     uint8_t hic_pin_scl;
     uint8_t hic_pin_af;
     uint8_t hic_10bit;
-    uint32_t hic_speed;
+    uint32_t hic_timingr;               /* TIMINGR register */
 };
 
 #ifdef __cplusplus
diff --git a/hw/mcu/stm/stm32f7xx/src/hal_i2c.c b/hw/mcu/stm/stm32f7xx/src/hal_i2c.c
deleted file mode 100644
index 1eef619..0000000
--- a/hw/mcu/stm/stm32f7xx/src/hal_i2c.c
+++ /dev/null
@@ -1,177 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *  http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-#include <string.h>
-#include <stdlib.h>
-#include <assert.h>
-
-#include "os/mynewt.h"
-
-#include <hal/hal_i2c.h>
-#include <hal/hal_gpio.h>
-
-#include "stm32f7xx.h"
-#include "stm32f7xx_hal_dma.h"
-#include "stm32f7xx_hal_i2c.h"
-#include "stm32f7xx_hal_gpio.h"
-#include "stm32f7xx_hal_rcc.h"
-#include "mcu/stm32f7xx_mynewt_hal.h"
-#include "mcu/stm32f7_bsp.h"
-
-#define HAL_I2C_MAX_DEVS	3
-
-#define I2C_ADDRESS 		0xae
-
-struct stm32f7_hal_i2c {
-    I2C_HandleTypeDef hid_handle;
-};
-
-#if MYNEWT_VAL(I2C_0)
-static struct stm32f7_hal_i2c i2c0;
-#endif
-#if MYNEWT_VAL(I2C_1)
-static struct stm32f7_hal_i2c i2c1;
-#endif
-#if MYNEWT_VAL(I2C_2)
-static struct stm32f7_hal_i2c i2c2;
-#endif
-
-static struct stm32f7_hal_i2c *hal_i2c_devs[HAL_I2C_MAX_DEVS] = {
-#if MYNEWT_VAL(I2C_0)
-    &i2c0,
-#else
-    NULL,
-#endif
-#if MYNEWT_VAL(I2C_1)
-    &i2c1,
-#else
-    NULL,
-#endif
-#if MYNEWT_VAL(I2C_2)
-    &i2c2,
-#else
-    NULL,
-#endif
-};
-
-int
-hal_i2c_init(uint8_t i2c_num, void *usercfg)
-{
-    struct stm32f7_hal_i2c_cfg *cfg = (struct stm32f7_hal_i2c_cfg *)usercfg;
-    struct stm32f7_hal_i2c *dev;
-    I2C_InitTypeDef *init;
-    int rc;
-
-    if (i2c_num >= HAL_I2C_MAX_DEVS || !(dev = hal_i2c_devs[i2c_num])) {
-        return -1;
-    }
-
-    init = &dev->hid_handle.Init;
-    dev->hid_handle.Instance = cfg->hic_i2c;
-    //init->ClockSpeed = cfg->hic_speed;
-    init->Timing = cfg->hic_speed;
-    if (cfg->hic_10bit) {
-        init->AddressingMode = I2C_ADDRESSINGMODE_10BIT;
-    } else {
-        init->AddressingMode = I2C_ADDRESSINGMODE_7BIT;
-    }
-    init->OwnAddress1 = I2C_ADDRESS;
-    init->OwnAddress2 = 0xFE;
-
-    /*
-     * Configure GPIO pins for I2C.
-     * Enable clock routing for I2C.
-     */
-    rc = hal_gpio_init_af(cfg->hic_pin_sda, cfg->hic_pin_af, HAL_GPIO_PULL_UP,
-                          1);
-    if (rc) {
-        goto err;
-    }
-    rc = hal_gpio_init_af(cfg->hic_pin_scl, cfg->hic_pin_af, HAL_GPIO_PULL_UP,
-                          1);
-    if (rc) {
-        goto err;
-    }
-    *cfg->hic_rcc_reg |= cfg->hic_rcc_dev;
-    rc = HAL_I2C_Init(&dev->hid_handle);
-    if (rc) {
-        goto err;
-    }
-
-    return 0;
-err:
-    *cfg->hic_rcc_reg &= ~cfg->hic_rcc_dev;
-    return rc;
-}
-
-int
-hal_i2c_master_write(uint8_t i2c_num, struct hal_i2c_master_data *data,
-  uint32_t timo, uint8_t last_op)
-{
-    struct stm32f7_hal_i2c *dev;
-
-    if (i2c_num >= HAL_I2C_MAX_DEVS || !(dev = hal_i2c_devs[i2c_num])) {
-        return -1;
-    }
-
-    if (!last_op) {
-        //FIXME: NoStop
-        //return HAL_I2C_Master_Transmit_NoStop(&dev->hid_handle,
-        return HAL_I2C_Master_Transmit(&dev->hid_handle,
-          data->address << 1, data->buffer, data->len, timo);
-    } else {
-        return HAL_I2C_Master_Transmit(&dev->hid_handle,
-          data->address << 1, data->buffer, data->len, timo);
-    }
-}
-
-int
-hal_i2c_master_read(uint8_t i2c_num, struct hal_i2c_master_data *pdata,
-  uint32_t timo, uint8_t last_op)
-{
-    struct stm32f7_hal_i2c *dev;
-
-    if (i2c_num >= HAL_I2C_MAX_DEVS || !(dev = hal_i2c_devs[i2c_num])) {
-        return -1;
-    }
-
-    if (!last_op) {
-        //FIXME: NoStop
-        //return HAL_I2C_Master_Receive_NoStop(&dev->hid_handle,
-        return HAL_I2C_Master_Receive(&dev->hid_handle,
-          pdata->address << 1, pdata->buffer, pdata->len, timo);
-    } else {
-        return HAL_I2C_Master_Receive(&dev->hid_handle, pdata->address << 1,
-          pdata->buffer, pdata->len, timo);
-    }
-}
-
-int
-hal_i2c_master_probe(uint8_t i2c_num, uint8_t address, uint32_t timo)
-{
-    struct stm32f7_hal_i2c *dev;
-    int rc;
-
-    if (i2c_num >= HAL_I2C_MAX_DEVS || !(dev = hal_i2c_devs[i2c_num])) {
-        return -1;
-    }
-
-    rc = HAL_I2C_IsDeviceReady(&dev->hid_handle, address, 1, timo);
-    return rc;
-}
diff --git a/hw/mcu/stm/stm32f7xx/syscfg.yml b/hw/mcu/stm/stm32f7xx/syscfg.yml
index 08606f5..0a39551 100644
--- a/hw/mcu/stm/stm32f7xx/syscfg.yml
+++ b/hw/mcu/stm/stm32f7xx/syscfg.yml
@@ -41,3 +41,6 @@ syscfg.defs:
         value:  0
         restrictions:
             - "!SPI_0_MASTER"
+    I2C_0:
+        description: 'I2C (TWI) interface 0'
+        value:  0

-- 
To stop receiving notification emails like this one, please contact
utzig@apache.org.