t

Fork of mbed-dev by mbed official

Revision:
178:c26431f84b0d
Parent:
150:02e0a0aed4ec
--- a/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c	Tue Feb 28 17:13:35 2017 +0000
+++ b/targets/TARGET_NORDIC/TARGET_NRF5/spi_api.c	Thu Nov 09 22:14:37 2017 +0000
@@ -254,6 +254,28 @@
               PinName mosi, PinName miso, PinName sclk, PinName ssel)
 {
     int i;
+    
+    
+    // This block is only a workaround that allows to create SPI object several
+    // times, what would be otherwise impossible in the current implementation
+    // of mbed driver that does not call spi_free() from SPI destructor.
+    // Once this mbed's imperfection is corrected, this block should be removed.
+    for (i = 0; i < SPI_COUNT; ++i) {
+        spi_info_t *p_spi_info = &m_spi_info[i];
+        if (p_spi_info->initialized &&
+            p_spi_info->mosi_pin == (uint8_t)mosi &&
+            p_spi_info->miso_pin == (uint8_t)miso &&
+            p_spi_info->sck_pin  == (uint8_t)sclk &&
+            p_spi_info->ss_pin   == (uint8_t)ssel) {
+            // Reuse the already allocated SPI instance (instead of allocating
+            // a new one), if it appears to be initialized with exactly the same
+            // pin assignments.
+            SPI_IDX(obj) = i;
+            return;
+        }
+    }
+
+    
     for (i = 0; i < SPI_COUNT; ++i) {
         spi_info_t *p_spi_info = &m_spi_info[i];
         if (!p_spi_info->initialized) {