mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Revision:
160:d5399cc887bb
Parent:
150:02e0a0aed4ec
Child:
165:e614a9f1c9e2
--- 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	Tue Mar 14 16:40:56 2017 +0000
@@ -90,22 +90,22 @@
 void SPI1_TWI1_IRQHandler(void);
 void SPIM2_SPIS2_SPI2_IRQHandler(void);
 
-static const peripheral_handler_desc_t spi_hanlder_desc[SPI_COUNT] = {
+static const peripheral_handler_desc_t spi_handler_desc[SPI_COUNT] = {
 #if SPI0_ENABLED
     {
-        SPIS0_IRQ,
+        SPI0_IRQ,
         (uint32_t) SPI0_TWI0_IRQHandler
     },
 #endif
 #if SPI1_ENABLED
     {
-        SPIS1_IRQ,
+        SPI1_IRQ,
         (uint32_t) SPI1_TWI1_IRQHandler
     },
 #endif
 #if SPI2_ENABLED
     {
-        SPIS2_IRQ,
+        SPI2_IRQ,
         (uint32_t) SPIM2_SPIS2_SPI2_IRQHandler
     },
 #endif    
@@ -254,12 +254,29 @@
               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) {
-         
-            NVIC_SetVector(spi_hanlder_desc[i].IRQn, spi_hanlder_desc[i].vector);
-            
             p_spi_info->sck_pin   = (uint8_t)sclk;
             p_spi_info->mosi_pin  = (mosi != NC) ?
                 (uint8_t)mosi : NRF_DRV_SPI_PIN_NOT_USED;
@@ -270,6 +287,8 @@
             p_spi_info->spi_mode  = (uint8_t)NRF_DRV_SPI_MODE_0;
             p_spi_info->frequency = NRF_DRV_SPI_FREQ_1M;
 
+            NVIC_SetVector(spi_handler_desc[i].IRQn, spi_handler_desc[i].vector);
+
             // By default each SPI instance is initialized to work as a master.
             // Should the slave mode be used, the instance will be reconfigured
             // appropriately in 'spi_format'.