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 2018/05/23 01:15:10 UTC

[GitHub] vrahane closed pull request #1108: Fix/nrf52 i2c hang

vrahane closed pull request #1108: Fix/nrf52 i2c hang
URL: https://github.com/apache/mynewt-core/pull/1108
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_i2c.c b/hw/mcu/nordic/nrf52xxx/src/hal_i2c.c
index 9104ad4275..8ac5eea358 100644
--- a/hw/mcu/nordic/nrf52xxx/src/hal_i2c.c
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_i2c.c
@@ -290,12 +290,14 @@ hal_i2c_master_write(uint8_t i2c_num, struct hal_i2c_master_data *pdata,
     int rc = -1;
     int i;
     uint32_t start;
+    int retry_once = 1;
 
     NRF52_HAL_I2C_RESOLVE(i2c_num, i2c);
     regs = i2c->nhi_regs;
 
     regs->ADDRESS = pdata->address;
 
+retry:
     regs->EVENTS_ERROR = 0;
     regs->EVENTS_STOPPED = 0;
     regs->EVENTS_SUSPENDED = 0;
@@ -311,7 +313,19 @@ hal_i2c_master_write(uint8_t i2c_num, struct hal_i2c_master_data *pdata,
         while (!regs->EVENTS_TXDSENT && !regs->EVENTS_ERROR) {
             if (os_time_get() - start > timo) {
                 regs->TASKS_STOP = 1;
-                goto err;
+                if (retry_once) {
+                   /* 
+                    * Some I2C slave peripherals cause a glitch on the bus when
+                    * they reset which puts the TWI in an unresponsive state.
+                    * Disabling and re-enabling the TWI returns it to normal operation.
+                    */
+                    retry_once = 0;
+                    regs->ENABLE = TWI_ENABLE_ENABLE_Disabled;
+                    regs->ENABLE = TWI_ENABLE_ENABLE_Enabled;
+                    goto retry;
+                } else {
+                    goto err;
+                }
             }
         }
         if (regs->EVENTS_ERROR) {
@@ -350,10 +364,12 @@ hal_i2c_master_read(uint8_t i2c_num, struct hal_i2c_master_data *pdata,
     int rc = -1;
     int i;
     uint32_t start;
+    int retry_once = 1;
 
     NRF52_HAL_I2C_RESOLVE(i2c_num, i2c);
     regs = i2c->nhi_regs;
 
+retry:
     start = os_time_get();
 
     if (regs->EVENTS_RXDREADY) {
@@ -379,12 +395,23 @@ hal_i2c_master_read(uint8_t i2c_num, struct hal_i2c_master_data *pdata,
 
     for (i = 0; i < pdata->len; i++) {
         regs->TASKS_RESUME = 1;
-
         while (!regs->EVENTS_RXDREADY && !regs->EVENTS_ERROR) {
             if (os_time_get() - start > timo) {
                 regs->SHORTS = TWI_SHORTS_BB_STOP_Msk;
                 regs->TASKS_STOP = 1;
-                goto err;
+                if (retry_once) {
+                   /* 
+                    * Some I2C slave peripherals cause a glitch on the bus when
+                    * they reset which puts the TWI in an unresponsive state.
+                    * Disabling and re-enabling the TWI returns it to normal operation.
+                    */
+                    retry_once = 0;
+                    regs->ENABLE = TWI_ENABLE_ENABLE_Disabled;
+                    regs->ENABLE = TWI_ENABLE_ENABLE_Enabled;
+                    goto retry;
+                } else {
+                    goto err;
+                }
             }
         }
         if (regs->EVENTS_ERROR) {


 

----------------------------------------------------------------
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