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:
Wed May 27 13:30:08 2015 +0100
Revision:
552:a1b9575155a3
Parent:
542:6693aa6d3ba3
Child:
613:bc40b8d2aec4
Synchronized with git revision a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207

Full URL: https://github.com/mbedmicro/mbed/commit/a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207/

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 442:d916d321e60f 3 * Copyright (c) 2014, 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 442:d916d321e60f 39
mbed_official 442:d916d321e60f 40 static SPI_HandleTypeDef SpiHandle;
mbed_official 442:d916d321e60f 41
mbed_official 442:d916d321e60f 42 static void init_spi(spi_t *obj)
mbed_official 442:d916d321e60f 43 {
mbed_official 442:d916d321e60f 44 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 45
mbed_official 442:d916d321e60f 46 __HAL_SPI_DISABLE(&SpiHandle);
mbed_official 442:d916d321e60f 47
mbed_official 442:d916d321e60f 48 SpiHandle.Init.Mode = obj->mode;
mbed_official 442:d916d321e60f 49 SpiHandle.Init.BaudRatePrescaler = obj->br_presc;
mbed_official 442:d916d321e60f 50 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
mbed_official 442:d916d321e60f 51 SpiHandle.Init.CLKPhase = obj->cpha;
mbed_official 442:d916d321e60f 52 SpiHandle.Init.CLKPolarity = obj->cpol;
mbed_official 442:d916d321e60f 53 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
mbed_official 442:d916d321e60f 54 SpiHandle.Init.CRCPolynomial = 7;
mbed_official 442:d916d321e60f 55 SpiHandle.Init.DataSize = obj->bits;
mbed_official 442:d916d321e60f 56 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
mbed_official 442:d916d321e60f 57 SpiHandle.Init.NSS = obj->nss;
mbed_official 442:d916d321e60f 58 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
mbed_official 442:d916d321e60f 59
mbed_official 442:d916d321e60f 60 HAL_SPI_Init(&SpiHandle);
mbed_official 442:d916d321e60f 61
mbed_official 442:d916d321e60f 62 __HAL_SPI_ENABLE(&SpiHandle);
mbed_official 442:d916d321e60f 63 }
mbed_official 442:d916d321e60f 64
mbed_official 442:d916d321e60f 65 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
mbed_official 442:d916d321e60f 66 {
mbed_official 442:d916d321e60f 67 // Determine the SPI to use
mbed_official 442:d916d321e60f 68 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 442:d916d321e60f 69 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 442:d916d321e60f 70 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 442:d916d321e60f 71 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 442:d916d321e60f 72
mbed_official 442:d916d321e60f 73 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 442:d916d321e60f 74 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 442:d916d321e60f 75
mbed_official 442:d916d321e60f 76 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 442:d916d321e60f 77 MBED_ASSERT(obj->spi != (SPIName)NC);
mbed_official 442:d916d321e60f 78
mbed_official 442:d916d321e60f 79 // Enable SPI clock
mbed_official 442:d916d321e60f 80 if (obj->spi == SPI_1) {
mbed_official 442:d916d321e60f 81 __SPI1_CLK_ENABLE();
mbed_official 442:d916d321e60f 82 }
mbed_official 442:d916d321e60f 83
mbed_official 442:d916d321e60f 84 if (obj->spi == SPI_2) {
mbed_official 442:d916d321e60f 85 __SPI2_CLK_ENABLE();
mbed_official 442:d916d321e60f 86 }
mbed_official 442:d916d321e60f 87
mbed_official 442:d916d321e60f 88 if (obj->spi == SPI_3) {
mbed_official 442:d916d321e60f 89 __SPI3_CLK_ENABLE();
mbed_official 442:d916d321e60f 90 }
mbed_official 442:d916d321e60f 91
mbed_official 442:d916d321e60f 92 #if defined SPI4_BASE
mbed_official 442:d916d321e60f 93 if (obj->spi == SPI_4) {
mbed_official 442:d916d321e60f 94 __SPI4_CLK_ENABLE();
mbed_official 442:d916d321e60f 95 }
mbed_official 442:d916d321e60f 96 #endif
mbed_official 442:d916d321e60f 97
mbed_official 442:d916d321e60f 98 #if defined SPI5_BASE
mbed_official 442:d916d321e60f 99 if (obj->spi == SPI_5) {
mbed_official 442:d916d321e60f 100 __SPI5_CLK_ENABLE();
mbed_official 442:d916d321e60f 101 }
mbed_official 442:d916d321e60f 102 #endif
mbed_official 442:d916d321e60f 103
mbed_official 442:d916d321e60f 104 // Configure the SPI pins
mbed_official 442:d916d321e60f 105 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 442:d916d321e60f 106 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 442:d916d321e60f 107 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 442:d916d321e60f 108
mbed_official 442:d916d321e60f 109 // Save new values
mbed_official 442:d916d321e60f 110 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 442:d916d321e60f 111 obj->cpol = SPI_POLARITY_LOW;
mbed_official 442:d916d321e60f 112 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 442:d916d321e60f 113 obj->br_presc = SPI_BAUDRATEPRESCALER_256;
mbed_official 442:d916d321e60f 114
mbed_official 442:d916d321e60f 115 obj->pin_miso = miso;
mbed_official 442:d916d321e60f 116 obj->pin_mosi = mosi;
mbed_official 442:d916d321e60f 117 obj->pin_sclk = sclk;
mbed_official 442:d916d321e60f 118 obj->pin_ssel = ssel;
mbed_official 442:d916d321e60f 119
mbed_official 552:a1b9575155a3 120 if (ssel != NC) {
mbed_official 552:a1b9575155a3 121 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 552:a1b9575155a3 122 } else {
mbed_official 442:d916d321e60f 123 obj->nss = SPI_NSS_SOFT;
mbed_official 442:d916d321e60f 124 }
mbed_official 442:d916d321e60f 125
mbed_official 442:d916d321e60f 126 init_spi(obj);
mbed_official 442:d916d321e60f 127 }
mbed_official 442:d916d321e60f 128
mbed_official 442:d916d321e60f 129 void spi_free(spi_t *obj)
mbed_official 442:d916d321e60f 130 {
mbed_official 442:d916d321e60f 131 // Reset SPI and disable clock
mbed_official 442:d916d321e60f 132 if (obj->spi == SPI_1) {
mbed_official 442:d916d321e60f 133 __SPI1_FORCE_RESET();
mbed_official 442:d916d321e60f 134 __SPI1_RELEASE_RESET();
mbed_official 442:d916d321e60f 135 __SPI1_CLK_DISABLE();
mbed_official 442:d916d321e60f 136 }
mbed_official 442:d916d321e60f 137
mbed_official 442:d916d321e60f 138 if (obj->spi == SPI_2) {
mbed_official 442:d916d321e60f 139 __SPI2_FORCE_RESET();
mbed_official 442:d916d321e60f 140 __SPI2_RELEASE_RESET();
mbed_official 442:d916d321e60f 141 __SPI2_CLK_DISABLE();
mbed_official 442:d916d321e60f 142 }
mbed_official 442:d916d321e60f 143
mbed_official 442:d916d321e60f 144 if (obj->spi == SPI_3) {
mbed_official 442:d916d321e60f 145 __SPI3_FORCE_RESET();
mbed_official 442:d916d321e60f 146 __SPI3_RELEASE_RESET();
mbed_official 442:d916d321e60f 147 __SPI3_CLK_DISABLE();
mbed_official 442:d916d321e60f 148 }
mbed_official 442:d916d321e60f 149
mbed_official 442:d916d321e60f 150 #if defined SPI4_BASE
mbed_official 442:d916d321e60f 151 if (obj->spi == SPI_4) {
mbed_official 442:d916d321e60f 152 __SPI4_FORCE_RESET();
mbed_official 442:d916d321e60f 153 __SPI4_RELEASE_RESET();
mbed_official 442:d916d321e60f 154 __SPI4_CLK_DISABLE();
mbed_official 442:d916d321e60f 155 }
mbed_official 442:d916d321e60f 156 #endif
mbed_official 442:d916d321e60f 157
mbed_official 442:d916d321e60f 158 #if defined SPI5_BASE
mbed_official 442:d916d321e60f 159 if (obj->spi == SPI_5) {
mbed_official 442:d916d321e60f 160 __SPI5_FORCE_RESET();
mbed_official 442:d916d321e60f 161 __SPI5_RELEASE_RESET();
mbed_official 442:d916d321e60f 162 __SPI5_CLK_DISABLE();
mbed_official 442:d916d321e60f 163 }
mbed_official 442:d916d321e60f 164 #endif
mbed_official 442:d916d321e60f 165
mbed_official 442:d916d321e60f 166 // Configure GPIOs
mbed_official 442:d916d321e60f 167 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 168 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 169 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 170 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 442:d916d321e60f 171 }
mbed_official 442:d916d321e60f 172
mbed_official 442:d916d321e60f 173 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 442:d916d321e60f 174 {
mbed_official 442:d916d321e60f 175 // Save new values
mbed_official 442:d916d321e60f 176 if (bits == 16) {
mbed_official 442:d916d321e60f 177 obj->bits = SPI_DATASIZE_16BIT;
mbed_official 442:d916d321e60f 178 } else {
mbed_official 442:d916d321e60f 179 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 442:d916d321e60f 180 }
mbed_official 442:d916d321e60f 181
mbed_official 442:d916d321e60f 182 switch (mode) {
mbed_official 442:d916d321e60f 183 case 0:
mbed_official 442:d916d321e60f 184 obj->cpol = SPI_POLARITY_LOW;
mbed_official 442:d916d321e60f 185 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 442:d916d321e60f 186 break;
mbed_official 442:d916d321e60f 187 case 1:
mbed_official 442:d916d321e60f 188 obj->cpol = SPI_POLARITY_LOW;
mbed_official 442:d916d321e60f 189 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 442:d916d321e60f 190 break;
mbed_official 442:d916d321e60f 191 case 2:
mbed_official 442:d916d321e60f 192 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 442:d916d321e60f 193 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 442:d916d321e60f 194 break;
mbed_official 442:d916d321e60f 195 default:
mbed_official 442:d916d321e60f 196 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 442:d916d321e60f 197 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 442:d916d321e60f 198 break;
mbed_official 442:d916d321e60f 199 }
mbed_official 442:d916d321e60f 200
mbed_official 552:a1b9575155a3 201 if (obj->nss != SPI_NSS_SOFT) {
mbed_official 552:a1b9575155a3 202 obj->nss = (slave) ? SPI_NSS_HARD_INPUT : SPI_NSS_HARD_OUTPUT;
mbed_official 442:d916d321e60f 203 }
mbed_official 442:d916d321e60f 204
mbed_official 552:a1b9575155a3 205 obj->mode = (slave) ? SPI_MODE_SLAVE : SPI_MODE_MASTER;
mbed_official 552:a1b9575155a3 206
mbed_official 442:d916d321e60f 207 init_spi(obj);
mbed_official 442:d916d321e60f 208 }
mbed_official 442:d916d321e60f 209
mbed_official 442:d916d321e60f 210 void spi_frequency(spi_t *obj, int hz)
mbed_official 442:d916d321e60f 211 {
mbed_official 542:6693aa6d3ba3 212 #if defined(TARGET_STM32F401RE) || defined(TARGET_STM32F401VC) || defined(TARGET_STM32F407VG)
mbed_official 442:d916d321e60f 213 // Note: The frequencies are obtained with SPI1 clock = 84 MHz (APB2 clock)
mbed_official 442:d916d321e60f 214 if (hz < 600000) {
mbed_official 442:d916d321e60f 215 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 330 kHz
mbed_official 442:d916d321e60f 216 } else if ((hz >= 600000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 217 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 656 kHz
mbed_official 442:d916d321e60f 218 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 442:d916d321e60f 219 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.3 MHz
mbed_official 442:d916d321e60f 220 } else if ((hz >= 2000000) && (hz < 5000000)) {
mbed_official 442:d916d321e60f 221 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2.6 MHz
mbed_official 442:d916d321e60f 222 } else if ((hz >= 5000000) && (hz < 10000000)) {
mbed_official 442:d916d321e60f 223 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 5.25 MHz
mbed_official 442:d916d321e60f 224 } else if ((hz >= 10000000) && (hz < 21000000)) {
mbed_official 442:d916d321e60f 225 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 10.5 MHz
mbed_official 442:d916d321e60f 226 } else if ((hz >= 21000000) && (hz < 42000000)) {
mbed_official 442:d916d321e60f 227 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 21 MHz
mbed_official 442:d916d321e60f 228 } else { // >= 42000000
mbed_official 442:d916d321e60f 229 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 42 MHz
mbed_official 442:d916d321e60f 230 }
mbed_official 442:d916d321e60f 231 #elif defined(TARGET_STM32F405RG)
mbed_official 442:d916d321e60f 232 // Note: The frequencies are obtained with SPI1 clock = 48 MHz (APB2 clock)
mbed_official 442:d916d321e60f 233 if (obj->spi == SPI_1) {
mbed_official 442:d916d321e60f 234 if (hz < 375000) {
mbed_official 442:d916d321e60f 235 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 187.5 kHz
mbed_official 442:d916d321e60f 236 } else if ((hz >= 375000) && (hz < 750000)) {
mbed_official 442:d916d321e60f 237 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz
mbed_official 442:d916d321e60f 238 } else if ((hz >= 750000) && (hz < 1500000)) {
mbed_official 442:d916d321e60f 239 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 0.75 MHz
mbed_official 442:d916d321e60f 240 } else if ((hz >= 1500000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 241 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz
mbed_official 442:d916d321e60f 242 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 243 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz
mbed_official 442:d916d321e60f 244 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 245 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz
mbed_official 442:d916d321e60f 246 } else if ((hz >= 12000000) && (hz < 24000000)) {
mbed_official 442:d916d321e60f 247 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz
mbed_official 442:d916d321e60f 248 } else { // >= 24000000
mbed_official 442:d916d321e60f 249 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
mbed_official 442:d916d321e60f 250 }
mbed_official 442:d916d321e60f 251 // Note: The frequencies are obtained with SPI2/3 clock = 48 MHz (APB1 clock)
mbed_official 442:d916d321e60f 252 } else if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 442:d916d321e60f 253 if (hz < 375000) {
mbed_official 442:d916d321e60f 254 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 187.5 kHz
mbed_official 442:d916d321e60f 255 } else if ((hz >= 375000) && (hz < 750000)) {
mbed_official 442:d916d321e60f 256 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz
mbed_official 442:d916d321e60f 257 } else if ((hz >= 750000) && (hz < 1500000)) {
mbed_official 442:d916d321e60f 258 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 0.75 MHz
mbed_official 442:d916d321e60f 259 } else if ((hz >= 1500000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 260 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz
mbed_official 442:d916d321e60f 261 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 262 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz
mbed_official 442:d916d321e60f 263 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 264 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz
mbed_official 442:d916d321e60f 265 } else if ((hz >= 12000000) && (hz < 24000000)) {
mbed_official 442:d916d321e60f 266 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz
mbed_official 442:d916d321e60f 267 } else { // >= 24000000
mbed_official 442:d916d321e60f 268 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
mbed_official 442:d916d321e60f 269 }
mbed_official 442:d916d321e60f 270 }
mbed_official 442:d916d321e60f 271 #elif defined(TARGET_STM32F411RE) || defined(TARGET_STM32F429ZI)
mbed_official 442:d916d321e60f 272 // Values depend of PCLK2: 100 MHz
mbed_official 442:d916d321e60f 273 if ((obj->spi == SPI_1) || (obj->spi == SPI_4) || (obj->spi == SPI_5)) {
mbed_official 442:d916d321e60f 274 if (hz < 700000) {
mbed_official 442:d916d321e60f 275 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 391 kHz
mbed_official 442:d916d321e60f 276 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 277 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 781 kHz
mbed_official 442:d916d321e60f 278 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 279 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.56 MHz
mbed_official 442:d916d321e60f 280 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 281 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 3.13 MHz
mbed_official 442:d916d321e60f 282 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 283 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 6.25 MHz
mbed_official 442:d916d321e60f 284 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 442:d916d321e60f 285 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 12.5 MHz
mbed_official 442:d916d321e60f 286 } else if ((hz >= 25000000) && (hz < 50000000)) {
mbed_official 442:d916d321e60f 287 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 25 MHz
mbed_official 442:d916d321e60f 288 } else { // >= 50000000
mbed_official 442:d916d321e60f 289 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 50 MHz
mbed_official 442:d916d321e60f 290 }
mbed_official 442:d916d321e60f 291 }
mbed_official 442:d916d321e60f 292
mbed_official 442:d916d321e60f 293 // Values depend of PCLK1: 50 MHz
mbed_official 442:d916d321e60f 294 if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 442:d916d321e60f 295 if (hz < 400000) {
mbed_official 442:d916d321e60f 296 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 195 kHz
mbed_official 442:d916d321e60f 297 } else if ((hz >= 400000) && (hz < 700000)) {
mbed_official 442:d916d321e60f 298 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 391 kHz
mbed_official 442:d916d321e60f 299 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 300 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 781 MHz
mbed_official 442:d916d321e60f 301 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 302 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.56 MHz
mbed_official 442:d916d321e60f 303 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 304 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3.13 MHz
mbed_official 442:d916d321e60f 305 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 306 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6.25 MHz
mbed_official 442:d916d321e60f 307 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 442:d916d321e60f 308 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12.5 MHz
mbed_official 442:d916d321e60f 309 } else { // >= 25000000
mbed_official 442:d916d321e60f 310 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 25 MHz
mbed_official 442:d916d321e60f 311 }
mbed_official 442:d916d321e60f 312 }
mbed_official 442:d916d321e60f 313 #endif
mbed_official 442:d916d321e60f 314 init_spi(obj);
mbed_official 442:d916d321e60f 315 }
mbed_official 442:d916d321e60f 316
mbed_official 442:d916d321e60f 317 static inline int ssp_readable(spi_t *obj)
mbed_official 442:d916d321e60f 318 {
mbed_official 442:d916d321e60f 319 int status;
mbed_official 442:d916d321e60f 320 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 321 // Check if data is received
mbed_official 442:d916d321e60f 322 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 323 return status;
mbed_official 442:d916d321e60f 324 }
mbed_official 442:d916d321e60f 325
mbed_official 442:d916d321e60f 326 static inline int ssp_writeable(spi_t *obj)
mbed_official 442:d916d321e60f 327 {
mbed_official 442:d916d321e60f 328 int status;
mbed_official 442:d916d321e60f 329 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 330 // Check if data is transmitted
mbed_official 442:d916d321e60f 331 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 332 return status;
mbed_official 442:d916d321e60f 333 }
mbed_official 442:d916d321e60f 334
mbed_official 442:d916d321e60f 335 static inline void ssp_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 336 {
mbed_official 442:d916d321e60f 337 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 338 while (!ssp_writeable(obj));
mbed_official 442:d916d321e60f 339 spi->DR = (uint16_t)value;
mbed_official 442:d916d321e60f 340 }
mbed_official 442:d916d321e60f 341
mbed_official 442:d916d321e60f 342 static inline int ssp_read(spi_t *obj)
mbed_official 442:d916d321e60f 343 {
mbed_official 442:d916d321e60f 344 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 345 while (!ssp_readable(obj));
mbed_official 442:d916d321e60f 346 return (int)spi->DR;
mbed_official 442:d916d321e60f 347 }
mbed_official 442:d916d321e60f 348
mbed_official 442:d916d321e60f 349 static inline int ssp_busy(spi_t *obj)
mbed_official 442:d916d321e60f 350 {
mbed_official 442:d916d321e60f 351 int status;
mbed_official 442:d916d321e60f 352 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 353 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 354 return status;
mbed_official 442:d916d321e60f 355 }
mbed_official 442:d916d321e60f 356
mbed_official 442:d916d321e60f 357 int spi_master_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 358 {
mbed_official 442:d916d321e60f 359 ssp_write(obj, value);
mbed_official 442:d916d321e60f 360 return ssp_read(obj);
mbed_official 442:d916d321e60f 361 }
mbed_official 442:d916d321e60f 362
mbed_official 442:d916d321e60f 363 int spi_slave_receive(spi_t *obj)
mbed_official 442:d916d321e60f 364 {
mbed_official 442:d916d321e60f 365 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 442:d916d321e60f 366 };
mbed_official 442:d916d321e60f 367
mbed_official 442:d916d321e60f 368 int spi_slave_read(spi_t *obj)
mbed_official 442:d916d321e60f 369 {
mbed_official 442:d916d321e60f 370 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 371 while (!ssp_readable(obj));
mbed_official 442:d916d321e60f 372 return (int)spi->DR;
mbed_official 442:d916d321e60f 373 }
mbed_official 442:d916d321e60f 374
mbed_official 442:d916d321e60f 375 void spi_slave_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 376 {
mbed_official 442:d916d321e60f 377 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 378 while (!ssp_writeable(obj));
mbed_official 442:d916d321e60f 379 spi->DR = (uint16_t)value;
mbed_official 442:d916d321e60f 380 }
mbed_official 442:d916d321e60f 381
mbed_official 442:d916d321e60f 382 int spi_busy(spi_t *obj)
mbed_official 442:d916d321e60f 383 {
mbed_official 442:d916d321e60f 384 return ssp_busy(obj);
mbed_official 442:d916d321e60f 385 }
mbed_official 442:d916d321e60f 386
mbed_official 442:d916d321e60f 387 #endif