You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2019/01/09 11:46:23 UTC

[GitHub] kasjer commented on a change in pull request #1575: hw/bus: Add I2C bus driver for nRF52xxx TWIM (DMA)

kasjer commented on a change in pull request #1575: hw/bus: Add I2C bus driver for nRF52xxx TWIM (DMA)
URL: https://github.com/apache/mynewt-core/pull/1575#discussion_r246342800
 
 

 ##########
 File path: hw/bus/drivers/i2c_nrf52_twim/src/i2c_nrf52_twim.c
 ##########
 @@ -0,0 +1,659 @@
+/*
+ * 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 <assert.h>
+#include "defs/error.h"
+#include "hal/hal_gpio.h"
+#include "bus/bus.h"
+#include "bus/bus_debug.h"
+#include "bus/bus_driver.h"
+#include "bus/drivers/i2c_common.h"
+#include "mcu/nrf52_hal.h"
+#include "nrfx.h"
+
+#define TWIM_GPIO_PIN_CNF \
+    ((GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |  \
+     (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |      \
+     (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) |      \
+     (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |   \
+     (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos))
+
+#if MYNEWT_VAL(I2C_NRF52_TWIM_STAT)
+STATS_SECT_START(twim_stats_section)
+    STATS_SECT_ENTRY(sda_lo_err)        /* SDA pulled low on r/w */
+    STATS_SECT_ENTRY(sda_lo_err_nrecov) /* SDA pulled low on r/w (not recovered)*/
+    STATS_SECT_ENTRY(scl_hi_err)        /* SCL unresponsive */
+    STATS_SECT_ENTRY(scl_hi_err_nrecov) /* SCL unresponsive (not recovered)*/
+STATS_SECT_END
+
+STATS_NAME_START(twim_stats_section)
+    STATS_NAME(twim_stats_section, sda_lo_err)
+    STATS_NAME(twim_stats_section, sda_lo_err_nrecov)
+    STATS_NAME(twim_stats_section, scl_hi_err)
+    STATS_NAME(twim_stats_section, scl_hi_err_nrecov)
+STATS_NAME_END(twim_stats_section)
+#endif
+
+struct twim {
+    NRF_TWIM_Type *nrf_twim;
+    int irqn;
+    void (* isr)(void);
+};
+
+static void twim0_irq_handler(void);
+static void twim1_irq_handler(void);
+
+static const struct twim twims[TWIM_COUNT] = {
+    {
+        .nrf_twim = NRF_TWIM0,
+        .irqn = SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn,
+        .isr = twim0_irq_handler,
+    },
+    {
+        .nrf_twim = NRF_TWIM1,
+        .irqn = SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1_IRQn,
+        .isr = twim1_irq_handler,
+    },
+};
+
+struct twim_dev_data {
+    struct os_sem sem;
+    uint32_t errorsrc;
+    bool suspended;
+
+#if MYNEWT_VAL(I2C_NRF52_TWIM_STAT)
+    STATS_SECT_DECL(twim_stats_section) stats;
+#endif
+};
+
+static struct bus_i2c_dev *twim_devs[TWIM_COUNT];
+static struct twim_dev_data twim_devs_data[TWIM_COUNT];
+
+static void
+twim_irq_handler(struct bus_i2c_dev *dev)
+{
+    NRF_TWIM_Type *nrf_twim = twims[dev->cfg.i2c_num].nrf_twim;
+    struct twim_dev_data *dd = &twim_devs_data[dev->cfg.i2c_num];
+
+    nrf_twim->INTEN = 0;
+
+    if (nrf_twim->EVENTS_STOPPED) {
+        nrf_twim->EVENTS_STOPPED = 0;
+    }
+
+    if (nrf_twim->EVENTS_SUSPENDED) {
+        nrf_twim->EVENTS_SUSPENDED = 0;
+    }
+
+    if (nrf_twim->EVENTS_ERROR) {
+        nrf_twim->EVENTS_ERROR = 0;
+    }
+
+    dd->errorsrc = nrf_twim->ERRORSRC;
+    nrf_twim->ERRORSRC = dd->errorsrc;
+
+    os_sem_release(&twim_devs_data[dev->cfg.i2c_num].sem);
 
 Review comment:
   dd could be used instead of array access

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services