helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

Revision:
169:e3b6fe271b81
Parent:
165:e614a9f1c9e2
Child:
177:447f873cad2f
--- a/targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c	Thu Jul 06 15:42:05 2017 +0100
+++ b/targets/TARGET_NORDIC/TARGET_NRF5/i2c_api.c	Wed Jul 19 17:31:21 2017 +0100
@@ -290,7 +290,9 @@
 
 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
 {
+    ret_code_t ret;
     int i;
+
     for (i = 0; i < TWI_COUNT; ++i) {
         if (m_twi_info[i].initialized &&
             m_twi_info[i].pselsda == (uint32_t)sda &&
@@ -304,6 +306,13 @@
 
     for (i = 0; i < TWI_COUNT; ++i) {
         if (!m_twi_info[i].initialized) {
+            ret = nrf_drv_common_per_res_acquire(m_twi_instances[i],
+                    m_twi_irq_handlers[i]);
+
+            if (ret != NRF_SUCCESS) {
+                continue; /* the hw resource is busy - test another one */
+            }
+
             TWI_IDX(obj) = i;
 
             twi_info_t *twi_info = TWI_INFO(obj);
@@ -324,8 +333,6 @@
             i2c_reset(obj);
 
 #if DEVICE_I2C_ASYNCH
-            nrf_drv_common_per_res_acquire(m_twi_instances[i],
-                m_twi_irq_handlers[i]);
             NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector);
             nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY);
 #endif