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.

Committer:
mbed_official
Date:
Thu Aug 20 10:45:13 2015 +0100
Revision:
613:bc40b8d2aec4
Parent:
552:a1b9575155a3
Synchronized with git revision 92ca8c7b60a283b6bb60eb65b183dac1599f0ade

Full URL: https://github.com/mbedmicro/mbed/commit/92ca8c7b60a283b6bb60eb65b183dac1599f0ade/

Nordic: update application start address in GCC linker script

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 442:d916d321e60f 1 /* mbed Microcontroller Library
mbed_official 442:d916d321e60f 2 *******************************************************************************
mbed_official 613:bc40b8d2aec4 3 * Copyright (c) 2015, STMicroelectronics
mbed_official 442:d916d321e60f 4 * All rights reserved.
mbed_official 442:d916d321e60f 5 *
mbed_official 442:d916d321e60f 6 * Redistribution and use in source and binary forms, with or without
mbed_official 442:d916d321e60f 7 * modification, are permitted provided that the following conditions are met:
mbed_official 442:d916d321e60f 8 *
mbed_official 442:d916d321e60f 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 442:d916d321e60f 10 * this list of conditions and the following disclaimer.
mbed_official 442:d916d321e60f 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 442:d916d321e60f 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 442:d916d321e60f 13 * and/or other materials provided with the distribution.
mbed_official 442:d916d321e60f 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 442:d916d321e60f 15 * may be used to endorse or promote products derived from this software
mbed_official 442:d916d321e60f 16 * without specific prior written permission.
mbed_official 442:d916d321e60f 17 *
mbed_official 442:d916d321e60f 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 442:d916d321e60f 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 442:d916d321e60f 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 442:d916d321e60f 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 442:d916d321e60f 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 442:d916d321e60f 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 442:d916d321e60f 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 442:d916d321e60f 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 442:d916d321e60f 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 442:d916d321e60f 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 442:d916d321e60f 28 *******************************************************************************
mbed_official 442:d916d321e60f 29 */
mbed_official 442:d916d321e60f 30 #include "mbed_assert.h"
mbed_official 442:d916d321e60f 31 #include "spi_api.h"
mbed_official 442:d916d321e60f 32
mbed_official 442:d916d321e60f 33 #if DEVICE_SPI
mbed_official 442:d916d321e60f 34
mbed_official 442:d916d321e60f 35 #include <math.h>
mbed_official 442:d916d321e60f 36 #include "cmsis.h"
mbed_official 442:d916d321e60f 37 #include "pinmap.h"
mbed_official 442:d916d321e60f 38 #include "PeripheralPins.h"
mbed_official 613:bc40b8d2aec4 39 #include "mbed_error.h"
mbed_official 442:d916d321e60f 40
mbed_official 442:d916d321e60f 41 static SPI_HandleTypeDef SpiHandle;
mbed_official 442:d916d321e60f 42
mbed_official 442:d916d321e60f 43 static void init_spi(spi_t *obj)
mbed_official 442:d916d321e60f 44 {
mbed_official 442:d916d321e60f 45 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 46
mbed_official 442:d916d321e60f 47 __HAL_SPI_DISABLE(&SpiHandle);
mbed_official 442:d916d321e60f 48
mbed_official 442:d916d321e60f 49 SpiHandle.Init.Mode = obj->mode;
mbed_official 442:d916d321e60f 50 SpiHandle.Init.BaudRatePrescaler = obj->br_presc;
mbed_official 442:d916d321e60f 51 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
mbed_official 442:d916d321e60f 52 SpiHandle.Init.CLKPhase = obj->cpha;
mbed_official 442:d916d321e60f 53 SpiHandle.Init.CLKPolarity = obj->cpol;
mbed_official 442:d916d321e60f 54 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
mbed_official 442:d916d321e60f 55 SpiHandle.Init.CRCPolynomial = 7;
mbed_official 442:d916d321e60f 56 SpiHandle.Init.DataSize = obj->bits;
mbed_official 442:d916d321e60f 57 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
mbed_official 442:d916d321e60f 58 SpiHandle.Init.NSS = obj->nss;
mbed_official 442:d916d321e60f 59 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
mbed_official 442:d916d321e60f 60
mbed_official 613:bc40b8d2aec4 61 if (HAL_SPI_Init(&SpiHandle) != HAL_OK) {
mbed_official 613:bc40b8d2aec4 62 error("Cannot initialize SPI");
mbed_official 613:bc40b8d2aec4 63 }
mbed_official 442:d916d321e60f 64
mbed_official 442:d916d321e60f 65 __HAL_SPI_ENABLE(&SpiHandle);
mbed_official 442:d916d321e60f 66 }
mbed_official 442:d916d321e60f 67
mbed_official 442:d916d321e60f 68 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
mbed_official 442:d916d321e60f 69 {
mbed_official 442:d916d321e60f 70 // Determine the SPI to use
mbed_official 442:d916d321e60f 71 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 442:d916d321e60f 72 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 442:d916d321e60f 73 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 442:d916d321e60f 74 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 442:d916d321e60f 75
mbed_official 442:d916d321e60f 76 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 442:d916d321e60f 77 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 442:d916d321e60f 78
mbed_official 442:d916d321e60f 79 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 442:d916d321e60f 80 MBED_ASSERT(obj->spi != (SPIName)NC);
mbed_official 442:d916d321e60f 81
mbed_official 442:d916d321e60f 82 // Enable SPI clock
mbed_official 442:d916d321e60f 83 if (obj->spi == SPI_1) {
mbed_official 613:bc40b8d2aec4 84 __HAL_RCC_SPI1_CLK_ENABLE();
mbed_official 442:d916d321e60f 85 }
mbed_official 442:d916d321e60f 86
mbed_official 442:d916d321e60f 87 if (obj->spi == SPI_2) {
mbed_official 613:bc40b8d2aec4 88 __HAL_RCC_SPI2_CLK_ENABLE();
mbed_official 442:d916d321e60f 89 }
mbed_official 442:d916d321e60f 90
mbed_official 442:d916d321e60f 91 if (obj->spi == SPI_3) {
mbed_official 613:bc40b8d2aec4 92 __HAL_RCC_SPI3_CLK_ENABLE();
mbed_official 442:d916d321e60f 93 }
mbed_official 442:d916d321e60f 94
mbed_official 442:d916d321e60f 95 #if defined SPI4_BASE
mbed_official 442:d916d321e60f 96 if (obj->spi == SPI_4) {
mbed_official 613:bc40b8d2aec4 97 __HAL_RCC_SPI4_CLK_ENABLE();
mbed_official 442:d916d321e60f 98 }
mbed_official 442:d916d321e60f 99 #endif
mbed_official 442:d916d321e60f 100
mbed_official 442:d916d321e60f 101 #if defined SPI5_BASE
mbed_official 442:d916d321e60f 102 if (obj->spi == SPI_5) {
mbed_official 613:bc40b8d2aec4 103 __HAL_RCC_SPI5_CLK_ENABLE();
mbed_official 442:d916d321e60f 104 }
mbed_official 442:d916d321e60f 105 #endif
mbed_official 442:d916d321e60f 106
mbed_official 442:d916d321e60f 107 // Configure the SPI pins
mbed_official 442:d916d321e60f 108 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 442:d916d321e60f 109 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 442:d916d321e60f 110 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 442:d916d321e60f 111
mbed_official 442:d916d321e60f 112 // Save new values
mbed_official 442:d916d321e60f 113 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 442:d916d321e60f 114 obj->cpol = SPI_POLARITY_LOW;
mbed_official 442:d916d321e60f 115 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 442:d916d321e60f 116 obj->br_presc = SPI_BAUDRATEPRESCALER_256;
mbed_official 442:d916d321e60f 117
mbed_official 442:d916d321e60f 118 obj->pin_miso = miso;
mbed_official 442:d916d321e60f 119 obj->pin_mosi = mosi;
mbed_official 442:d916d321e60f 120 obj->pin_sclk = sclk;
mbed_official 442:d916d321e60f 121 obj->pin_ssel = ssel;
mbed_official 442:d916d321e60f 122
mbed_official 552:a1b9575155a3 123 if (ssel != NC) {
mbed_official 552:a1b9575155a3 124 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 552:a1b9575155a3 125 } else {
mbed_official 442:d916d321e60f 126 obj->nss = SPI_NSS_SOFT;
mbed_official 442:d916d321e60f 127 }
mbed_official 442:d916d321e60f 128
mbed_official 442:d916d321e60f 129 init_spi(obj);
mbed_official 442:d916d321e60f 130 }
mbed_official 442:d916d321e60f 131
mbed_official 442:d916d321e60f 132 void spi_free(spi_t *obj)
mbed_official 442:d916d321e60f 133 {
mbed_official 442:d916d321e60f 134 // Reset SPI and disable clock
mbed_official 442:d916d321e60f 135 if (obj->spi == SPI_1) {
mbed_official 613:bc40b8d2aec4 136 __HAL_RCC_SPI1_FORCE_RESET();
mbed_official 613:bc40b8d2aec4 137 __HAL_RCC_SPI1_RELEASE_RESET();
mbed_official 613:bc40b8d2aec4 138 __HAL_RCC_SPI1_CLK_DISABLE();
mbed_official 442:d916d321e60f 139 }
mbed_official 442:d916d321e60f 140
mbed_official 442:d916d321e60f 141 if (obj->spi == SPI_2) {
mbed_official 613:bc40b8d2aec4 142 __HAL_RCC_SPI2_FORCE_RESET();
mbed_official 613:bc40b8d2aec4 143 __HAL_RCC_SPI2_RELEASE_RESET();
mbed_official 613:bc40b8d2aec4 144 __HAL_RCC_SPI2_CLK_DISABLE();
mbed_official 442:d916d321e60f 145 }
mbed_official 442:d916d321e60f 146
mbed_official 442:d916d321e60f 147 if (obj->spi == SPI_3) {
mbed_official 613:bc40b8d2aec4 148 __HAL_RCC_SPI3_FORCE_RESET();
mbed_official 613:bc40b8d2aec4 149 __HAL_RCC_SPI3_RELEASE_RESET();
mbed_official 613:bc40b8d2aec4 150 __HAL_RCC_SPI3_CLK_DISABLE();
mbed_official 442:d916d321e60f 151 }
mbed_official 442:d916d321e60f 152
mbed_official 442:d916d321e60f 153 #if defined SPI4_BASE
mbed_official 442:d916d321e60f 154 if (obj->spi == SPI_4) {
mbed_official 613:bc40b8d2aec4 155 __HAL_RCC_SPI4_FORCE_RESET();
mbed_official 613:bc40b8d2aec4 156 __HAL_RCC_SPI4_RELEASE_RESET();
mbed_official 613:bc40b8d2aec4 157 __HAL_RCC_SPI4_CLK_DISABLE();
mbed_official 442:d916d321e60f 158 }
mbed_official 442:d916d321e60f 159 #endif
mbed_official 442:d916d321e60f 160
mbed_official 442:d916d321e60f 161 #if defined SPI5_BASE
mbed_official 442:d916d321e60f 162 if (obj->spi == SPI_5) {
mbed_official 613:bc40b8d2aec4 163 __HAL_RCC_SPI5_FORCE_RESET();
mbed_official 613:bc40b8d2aec4 164 __HAL_RCC_SPI5_RELEASE_RESET();
mbed_official 613:bc40b8d2aec4 165 __HAL_RCC_SPI5_CLK_DISABLE();
mbed_official 442:d916d321e60f 166 }
mbed_official 442:d916d321e60f 167 #endif
mbed_official 442:d916d321e60f 168
mbed_official 442:d916d321e60f 169 // Configure GPIOs
mbed_official 442:d916d321e60f 170 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 171 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 172 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 173 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 174 }
mbed_official 442:d916d321e60f 175
mbed_official 442:d916d321e60f 176 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 442:d916d321e60f 177 {
mbed_official 442:d916d321e60f 178 // Save new values
mbed_official 442:d916d321e60f 179 if (bits == 16) {
mbed_official 442:d916d321e60f 180 obj->bits = SPI_DATASIZE_16BIT;
mbed_official 442:d916d321e60f 181 } else {
mbed_official 442:d916d321e60f 182 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 442:d916d321e60f 183 }
mbed_official 442:d916d321e60f 184
mbed_official 442:d916d321e60f 185 switch (mode) {
mbed_official 442:d916d321e60f 186 case 0:
mbed_official 442:d916d321e60f 187 obj->cpol = SPI_POLARITY_LOW;
mbed_official 442:d916d321e60f 188 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 442:d916d321e60f 189 break;
mbed_official 442:d916d321e60f 190 case 1:
mbed_official 442:d916d321e60f 191 obj->cpol = SPI_POLARITY_LOW;
mbed_official 442:d916d321e60f 192 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 442:d916d321e60f 193 break;
mbed_official 442:d916d321e60f 194 case 2:
mbed_official 442:d916d321e60f 195 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 442:d916d321e60f 196 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 442:d916d321e60f 197 break;
mbed_official 442:d916d321e60f 198 default:
mbed_official 442:d916d321e60f 199 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 442:d916d321e60f 200 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 442:d916d321e60f 201 break;
mbed_official 442:d916d321e60f 202 }
mbed_official 442:d916d321e60f 203
mbed_official 552:a1b9575155a3 204 if (obj->nss != SPI_NSS_SOFT) {
mbed_official 552:a1b9575155a3 205 obj->nss = (slave) ? SPI_NSS_HARD_INPUT : SPI_NSS_HARD_OUTPUT;
mbed_official 442:d916d321e60f 206 }
mbed_official 442:d916d321e60f 207
mbed_official 552:a1b9575155a3 208 obj->mode = (slave) ? SPI_MODE_SLAVE : SPI_MODE_MASTER;
mbed_official 552:a1b9575155a3 209
mbed_official 442:d916d321e60f 210 init_spi(obj);
mbed_official 442:d916d321e60f 211 }
mbed_official 442:d916d321e60f 212
mbed_official 442:d916d321e60f 213 void spi_frequency(spi_t *obj, int hz)
mbed_official 442:d916d321e60f 214 {
mbed_official 542:6693aa6d3ba3 215 #if defined(TARGET_STM32F401RE) || defined(TARGET_STM32F401VC) || defined(TARGET_STM32F407VG)
mbed_official 442:d916d321e60f 216 // Note: The frequencies are obtained with SPI1 clock = 84 MHz (APB2 clock)
mbed_official 442:d916d321e60f 217 if (hz < 600000) {
mbed_official 442:d916d321e60f 218 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 330 kHz
mbed_official 442:d916d321e60f 219 } else if ((hz >= 600000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 220 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 656 kHz
mbed_official 442:d916d321e60f 221 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 442:d916d321e60f 222 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.3 MHz
mbed_official 442:d916d321e60f 223 } else if ((hz >= 2000000) && (hz < 5000000)) {
mbed_official 442:d916d321e60f 224 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2.6 MHz
mbed_official 442:d916d321e60f 225 } else if ((hz >= 5000000) && (hz < 10000000)) {
mbed_official 442:d916d321e60f 226 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 5.25 MHz
mbed_official 442:d916d321e60f 227 } else if ((hz >= 10000000) && (hz < 21000000)) {
mbed_official 442:d916d321e60f 228 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 10.5 MHz
mbed_official 442:d916d321e60f 229 } else if ((hz >= 21000000) && (hz < 42000000)) {
mbed_official 442:d916d321e60f 230 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 21 MHz
mbed_official 442:d916d321e60f 231 } else { // >= 42000000
mbed_official 442:d916d321e60f 232 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 42 MHz
mbed_official 442:d916d321e60f 233 }
mbed_official 442:d916d321e60f 234 #elif defined(TARGET_STM32F405RG)
mbed_official 442:d916d321e60f 235 // Note: The frequencies are obtained with SPI1 clock = 48 MHz (APB2 clock)
mbed_official 442:d916d321e60f 236 if (obj->spi == SPI_1) {
mbed_official 442:d916d321e60f 237 if (hz < 375000) {
mbed_official 442:d916d321e60f 238 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 187.5 kHz
mbed_official 442:d916d321e60f 239 } else if ((hz >= 375000) && (hz < 750000)) {
mbed_official 442:d916d321e60f 240 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz
mbed_official 442:d916d321e60f 241 } else if ((hz >= 750000) && (hz < 1500000)) {
mbed_official 442:d916d321e60f 242 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 0.75 MHz
mbed_official 442:d916d321e60f 243 } else if ((hz >= 1500000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 244 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz
mbed_official 442:d916d321e60f 245 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 246 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz
mbed_official 442:d916d321e60f 247 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 248 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz
mbed_official 442:d916d321e60f 249 } else if ((hz >= 12000000) && (hz < 24000000)) {
mbed_official 442:d916d321e60f 250 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz
mbed_official 442:d916d321e60f 251 } else { // >= 24000000
mbed_official 442:d916d321e60f 252 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
mbed_official 442:d916d321e60f 253 }
mbed_official 442:d916d321e60f 254 // Note: The frequencies are obtained with SPI2/3 clock = 48 MHz (APB1 clock)
mbed_official 442:d916d321e60f 255 } else if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 442:d916d321e60f 256 if (hz < 375000) {
mbed_official 442:d916d321e60f 257 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 187.5 kHz
mbed_official 442:d916d321e60f 258 } else if ((hz >= 375000) && (hz < 750000)) {
mbed_official 442:d916d321e60f 259 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz
mbed_official 442:d916d321e60f 260 } else if ((hz >= 750000) && (hz < 1500000)) {
mbed_official 442:d916d321e60f 261 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 0.75 MHz
mbed_official 442:d916d321e60f 262 } else if ((hz >= 1500000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 263 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz
mbed_official 442:d916d321e60f 264 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 265 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz
mbed_official 442:d916d321e60f 266 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 267 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz
mbed_official 442:d916d321e60f 268 } else if ((hz >= 12000000) && (hz < 24000000)) {
mbed_official 442:d916d321e60f 269 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz
mbed_official 442:d916d321e60f 270 } else { // >= 24000000
mbed_official 442:d916d321e60f 271 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
mbed_official 442:d916d321e60f 272 }
mbed_official 442:d916d321e60f 273 }
mbed_official 442:d916d321e60f 274 #elif defined(TARGET_STM32F411RE) || defined(TARGET_STM32F429ZI)
mbed_official 442:d916d321e60f 275 // Values depend of PCLK2: 100 MHz
mbed_official 442:d916d321e60f 276 if ((obj->spi == SPI_1) || (obj->spi == SPI_4) || (obj->spi == SPI_5)) {
mbed_official 442:d916d321e60f 277 if (hz < 700000) {
mbed_official 442:d916d321e60f 278 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 391 kHz
mbed_official 442:d916d321e60f 279 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 280 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 781 kHz
mbed_official 442:d916d321e60f 281 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 282 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.56 MHz
mbed_official 442:d916d321e60f 283 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 284 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 3.13 MHz
mbed_official 442:d916d321e60f 285 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 286 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 6.25 MHz
mbed_official 442:d916d321e60f 287 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 442:d916d321e60f 288 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 12.5 MHz
mbed_official 442:d916d321e60f 289 } else if ((hz >= 25000000) && (hz < 50000000)) {
mbed_official 442:d916d321e60f 290 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 25 MHz
mbed_official 442:d916d321e60f 291 } else { // >= 50000000
mbed_official 442:d916d321e60f 292 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 50 MHz
mbed_official 442:d916d321e60f 293 }
mbed_official 442:d916d321e60f 294 }
mbed_official 442:d916d321e60f 295 // Values depend of PCLK1: 50 MHz
mbed_official 442:d916d321e60f 296 if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 442:d916d321e60f 297 if (hz < 400000) {
mbed_official 442:d916d321e60f 298 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 195 kHz
mbed_official 442:d916d321e60f 299 } else if ((hz >= 400000) && (hz < 700000)) {
mbed_official 442:d916d321e60f 300 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 391 kHz
mbed_official 442:d916d321e60f 301 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 302 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 781 MHz
mbed_official 442:d916d321e60f 303 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 304 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.56 MHz
mbed_official 442:d916d321e60f 305 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 306 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3.13 MHz
mbed_official 442:d916d321e60f 307 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 308 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6.25 MHz
mbed_official 442:d916d321e60f 309 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 442:d916d321e60f 310 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12.5 MHz
mbed_official 442:d916d321e60f 311 } else { // >= 25000000
mbed_official 442:d916d321e60f 312 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 25 MHz
mbed_official 442:d916d321e60f 313 }
mbed_official 442:d916d321e60f 314 }
mbed_official 613:bc40b8d2aec4 315 #elif defined(TARGET_STM32F446RE)
mbed_official 613:bc40b8d2aec4 316 // Values depend of PCLK2: 90 MHz
mbed_official 613:bc40b8d2aec4 317 if ((obj->spi == SPI_1) || (obj->spi == SPI_4)) {
mbed_official 613:bc40b8d2aec4 318 if (hz < 700000) {
mbed_official 613:bc40b8d2aec4 319 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 352 kHz
mbed_official 613:bc40b8d2aec4 320 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 613:bc40b8d2aec4 321 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 703 kHz
mbed_official 613:bc40b8d2aec4 322 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 613:bc40b8d2aec4 323 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.41 MHz
mbed_official 613:bc40b8d2aec4 324 } else if ((hz >= 3000000) && (hz < 5000000)) {
mbed_official 613:bc40b8d2aec4 325 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2.81 MHz
mbed_official 613:bc40b8d2aec4 326 } else if ((hz >= 5000000) && (hz < 11000000)) {
mbed_official 613:bc40b8d2aec4 327 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 5.63 MHz
mbed_official 613:bc40b8d2aec4 328 } else if ((hz >= 11000000) && (hz < 22000000)) {
mbed_official 613:bc40b8d2aec4 329 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 11.25 MHz
mbed_official 613:bc40b8d2aec4 330 } else if ((hz >= 22000000) && (hz < 45000000)) {
mbed_official 613:bc40b8d2aec4 331 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 22.5 MHz
mbed_official 613:bc40b8d2aec4 332 } else { // >= 45000000
mbed_official 613:bc40b8d2aec4 333 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 45 MHz
mbed_official 613:bc40b8d2aec4 334 }
mbed_official 613:bc40b8d2aec4 335 }
mbed_official 613:bc40b8d2aec4 336 // Values depend of PCLK1: 45 MHz
mbed_official 613:bc40b8d2aec4 337 if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 613:bc40b8d2aec4 338 if (hz < 350000) {
mbed_official 613:bc40b8d2aec4 339 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 176 kHz
mbed_official 613:bc40b8d2aec4 340 } else if ((hz >= 350000) && (hz < 700000)) {
mbed_official 613:bc40b8d2aec4 341 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 352 kHz
mbed_official 613:bc40b8d2aec4 342 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 613:bc40b8d2aec4 343 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 703 kHz
mbed_official 613:bc40b8d2aec4 344 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 613:bc40b8d2aec4 345 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.41 MHz
mbed_official 613:bc40b8d2aec4 346 } else if ((hz >= 3000000) && (hz < 5000000)) {
mbed_official 613:bc40b8d2aec4 347 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 2.81 MHz
mbed_official 613:bc40b8d2aec4 348 } else if ((hz >= 5000000) && (hz < 11000000)) {
mbed_official 613:bc40b8d2aec4 349 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 5.63 MHz
mbed_official 613:bc40b8d2aec4 350 } else if ((hz >= 11000000) && (hz < 22000000)) {
mbed_official 613:bc40b8d2aec4 351 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 11.25 MHz
mbed_official 613:bc40b8d2aec4 352 } else { // >= 22000000
mbed_official 613:bc40b8d2aec4 353 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 22.5 MHz
mbed_official 613:bc40b8d2aec4 354 }
mbed_official 613:bc40b8d2aec4 355 }
mbed_official 442:d916d321e60f 356 #endif
mbed_official 442:d916d321e60f 357 init_spi(obj);
mbed_official 442:d916d321e60f 358 }
mbed_official 442:d916d321e60f 359
mbed_official 442:d916d321e60f 360 static inline int ssp_readable(spi_t *obj)
mbed_official 442:d916d321e60f 361 {
mbed_official 442:d916d321e60f 362 int status;
mbed_official 442:d916d321e60f 363 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 364 // Check if data is received
mbed_official 442:d916d321e60f 365 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 366 return status;
mbed_official 442:d916d321e60f 367 }
mbed_official 442:d916d321e60f 368
mbed_official 442:d916d321e60f 369 static inline int ssp_writeable(spi_t *obj)
mbed_official 442:d916d321e60f 370 {
mbed_official 442:d916d321e60f 371 int status;
mbed_official 442:d916d321e60f 372 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 373 // Check if data is transmitted
mbed_official 442:d916d321e60f 374 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 375 return status;
mbed_official 442:d916d321e60f 376 }
mbed_official 442:d916d321e60f 377
mbed_official 442:d916d321e60f 378 static inline void ssp_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 379 {
mbed_official 442:d916d321e60f 380 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 381 while (!ssp_writeable(obj));
mbed_official 442:d916d321e60f 382 spi->DR = (uint16_t)value;
mbed_official 442:d916d321e60f 383 }
mbed_official 442:d916d321e60f 384
mbed_official 442:d916d321e60f 385 static inline int ssp_read(spi_t *obj)
mbed_official 442:d916d321e60f 386 {
mbed_official 442:d916d321e60f 387 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 388 while (!ssp_readable(obj));
mbed_official 442:d916d321e60f 389 return (int)spi->DR;
mbed_official 442:d916d321e60f 390 }
mbed_official 442:d916d321e60f 391
mbed_official 442:d916d321e60f 392 static inline int ssp_busy(spi_t *obj)
mbed_official 442:d916d321e60f 393 {
mbed_official 442:d916d321e60f 394 int status;
mbed_official 442:d916d321e60f 395 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 396 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 397 return status;
mbed_official 442:d916d321e60f 398 }
mbed_official 442:d916d321e60f 399
mbed_official 442:d916d321e60f 400 int spi_master_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 401 {
mbed_official 442:d916d321e60f 402 ssp_write(obj, value);
mbed_official 442:d916d321e60f 403 return ssp_read(obj);
mbed_official 442:d916d321e60f 404 }
mbed_official 442:d916d321e60f 405
mbed_official 442:d916d321e60f 406 int spi_slave_receive(spi_t *obj)
mbed_official 442:d916d321e60f 407 {
mbed_official 442:d916d321e60f 408 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 442:d916d321e60f 409 };
mbed_official 442:d916d321e60f 410
mbed_official 442:d916d321e60f 411 int spi_slave_read(spi_t *obj)
mbed_official 442:d916d321e60f 412 {
mbed_official 442:d916d321e60f 413 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 414 while (!ssp_readable(obj));
mbed_official 442:d916d321e60f 415 return (int)spi->DR;
mbed_official 442:d916d321e60f 416 }
mbed_official 442:d916d321e60f 417
mbed_official 442:d916d321e60f 418 void spi_slave_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 419 {
mbed_official 442:d916d321e60f 420 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 421 while (!ssp_writeable(obj));
mbed_official 442:d916d321e60f 422 spi->DR = (uint16_t)value;
mbed_official 442:d916d321e60f 423 }
mbed_official 442:d916d321e60f 424
mbed_official 442:d916d321e60f 425 int spi_busy(spi_t *obj)
mbed_official 442:d916d321e60f 426 {
mbed_official 442:d916d321e60f 427 return ssp_busy(obj);
mbed_official 442:d916d321e60f 428 }
mbed_official 442:d916d321e60f 429
mbed_official 442:d916d321e60f 430 #endif