mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Revision:
139:e3413eddde57
Parent:
129:0182c99221bc
Child:
174:8bb9f3a33240
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/spi_api.c	Tue Mar 25 17:45:07 2014 +0000
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/spi_api.c	Thu Mar 27 09:45:07 2014 +0000
@@ -191,30 +191,58 @@
 }
 
 void spi_frequency(spi_t *obj, int hz) {
-    // Note: The frequencies are obtained with SPI clock = 32 MHz (APB1 & APB2 clocks)
-    if (hz < 250000) {
-        obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz
-    }
-    else if ((hz >= 250000) && (hz < 500000)) {
-        obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz
-    }
-    else if ((hz >= 500000) && (hz < 1000000)) {
-        obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz
+    // Values depend of PCLK1 and PCLK2: 32 MHz if HSI is used, 24 MHz if HSE is used
+    if (SystemCoreClock == 32000000) { // HSI
+        if (hz < 250000) {
+            obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz
+        }
+        else if ((hz >= 250000) && (hz < 500000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz
+        }
+        else if ((hz >= 500000) && (hz < 1000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz
+        }
+        else if ((hz >= 1000000) && (hz < 2000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz
+        }
+        else if ((hz >= 2000000) && (hz < 4000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz
+        }
+        else if ((hz >= 4000000) && (hz < 8000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz
+        }
+        else if ((hz >= 8000000) && (hz < 16000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz
+        }
+        else { // >= 16000000
+            obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz
+        }
     }
-    else if ((hz >= 1000000) && (hz < 2000000)) {
-        obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz
-    }
-    else if ((hz >= 2000000) && (hz < 4000000)) {
-        obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz
-    }
-    else if ((hz >= 4000000) && (hz < 8000000)) {
-        obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz
-    }
-    else if ((hz >= 8000000) && (hz < 16000000)) {
-        obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz
-    }
-    else { // >= 16000000
-        obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz
+    else { // 24 MHz - HSE
+        if (hz < 180000) {
+            obj->br_presc = SPI_BaudRatePrescaler_256; // 94 kHz
+        }
+        else if ((hz >= 180000) && (hz < 350000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_128; // 188 kHz
+        }
+        else if ((hz >= 350000) && (hz < 750000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_64; // 375 kHz
+        }        
+        else if ((hz >= 750000) && (hz < 1000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_32; // 750 kHz
+        }
+        else if ((hz >= 1000000) && (hz < 3000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_16; // 1.5 MHz
+        }
+        else if ((hz >= 3000000) && (hz < 6000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_8; // 3 MHz
+        }
+        else if ((hz >= 6000000) && (hz < 12000000)) {
+            obj->br_presc = SPI_BaudRatePrescaler_4; // 6 MHz
+        }
+        else { // >= 12000000
+            obj->br_presc = SPI_BaudRatePrescaler_2; // 12 MHz
+        }      
     }
     init_spi(obj);
 }