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:
Tue May 19 08:00:08 2015 +0100
Revision:
542:6693aa6d3ba3
Parent:
442:d916d321e60f
Child:
552:a1b9575155a3
Synchronized with git revision d2cab9778fbc4229fed9561c199f206fe1a43a98

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

Tools: Added support for testing of multiple targets of the same type (board type)

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 442:d916d321e60f 120 if (ssel == NC) { // SW NSS Master mode
mbed_official 442:d916d321e60f 121 obj->mode = SPI_MODE_MASTER;
mbed_official 442:d916d321e60f 122 obj->nss = SPI_NSS_SOFT;
mbed_official 442:d916d321e60f 123 } else { // Slave
mbed_official 442:d916d321e60f 124 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 442:d916d321e60f 125 obj->mode = SPI_MODE_SLAVE;
mbed_official 442:d916d321e60f 126 obj->nss = SPI_NSS_HARD_INPUT;
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 442:d916d321e60f 136 __SPI1_FORCE_RESET();
mbed_official 442:d916d321e60f 137 __SPI1_RELEASE_RESET();
mbed_official 442:d916d321e60f 138 __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 442:d916d321e60f 142 __SPI2_FORCE_RESET();
mbed_official 442:d916d321e60f 143 __SPI2_RELEASE_RESET();
mbed_official 442:d916d321e60f 144 __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 442:d916d321e60f 148 __SPI3_FORCE_RESET();
mbed_official 442:d916d321e60f 149 __SPI3_RELEASE_RESET();
mbed_official 442:d916d321e60f 150 __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 442:d916d321e60f 155 __SPI4_FORCE_RESET();
mbed_official 442:d916d321e60f 156 __SPI4_RELEASE_RESET();
mbed_official 442:d916d321e60f 157 __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 442:d916d321e60f 163 __SPI5_FORCE_RESET();
mbed_official 442:d916d321e60f 164 __SPI5_RELEASE_RESET();
mbed_official 442:d916d321e60f 165 __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 442:d916d321e60f 204 if (slave == 0) {
mbed_official 442:d916d321e60f 205 obj->mode = SPI_MODE_MASTER;
mbed_official 442:d916d321e60f 206 obj->nss = SPI_NSS_SOFT;
mbed_official 442:d916d321e60f 207 } else {
mbed_official 442:d916d321e60f 208 obj->mode = SPI_MODE_SLAVE;
mbed_official 442:d916d321e60f 209 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 442:d916d321e60f 210 }
mbed_official 442:d916d321e60f 211
mbed_official 442:d916d321e60f 212 init_spi(obj);
mbed_official 442:d916d321e60f 213 }
mbed_official 442:d916d321e60f 214
mbed_official 442:d916d321e60f 215 void spi_frequency(spi_t *obj, int hz)
mbed_official 442:d916d321e60f 216 {
mbed_official 542:6693aa6d3ba3 217 #if defined(TARGET_STM32F401RE) || defined(TARGET_STM32F401VC) || defined(TARGET_STM32F407VG)
mbed_official 442:d916d321e60f 218 // Note: The frequencies are obtained with SPI1 clock = 84 MHz (APB2 clock)
mbed_official 442:d916d321e60f 219 if (hz < 600000) {
mbed_official 442:d916d321e60f 220 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 330 kHz
mbed_official 442:d916d321e60f 221 } else if ((hz >= 600000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 222 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 656 kHz
mbed_official 442:d916d321e60f 223 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 442:d916d321e60f 224 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.3 MHz
mbed_official 442:d916d321e60f 225 } else if ((hz >= 2000000) && (hz < 5000000)) {
mbed_official 442:d916d321e60f 226 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2.6 MHz
mbed_official 442:d916d321e60f 227 } else if ((hz >= 5000000) && (hz < 10000000)) {
mbed_official 442:d916d321e60f 228 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 5.25 MHz
mbed_official 442:d916d321e60f 229 } else if ((hz >= 10000000) && (hz < 21000000)) {
mbed_official 442:d916d321e60f 230 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 10.5 MHz
mbed_official 442:d916d321e60f 231 } else if ((hz >= 21000000) && (hz < 42000000)) {
mbed_official 442:d916d321e60f 232 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 21 MHz
mbed_official 442:d916d321e60f 233 } else { // >= 42000000
mbed_official 442:d916d321e60f 234 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 42 MHz
mbed_official 442:d916d321e60f 235 }
mbed_official 442:d916d321e60f 236 #elif defined(TARGET_STM32F405RG)
mbed_official 442:d916d321e60f 237 // Note: The frequencies are obtained with SPI1 clock = 48 MHz (APB2 clock)
mbed_official 442:d916d321e60f 238 if (obj->spi == SPI_1) {
mbed_official 442:d916d321e60f 239 if (hz < 375000) {
mbed_official 442:d916d321e60f 240 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 187.5 kHz
mbed_official 442:d916d321e60f 241 } else if ((hz >= 375000) && (hz < 750000)) {
mbed_official 442:d916d321e60f 242 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz
mbed_official 442:d916d321e60f 243 } else if ((hz >= 750000) && (hz < 1500000)) {
mbed_official 442:d916d321e60f 244 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 0.75 MHz
mbed_official 442:d916d321e60f 245 } else if ((hz >= 1500000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 246 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz
mbed_official 442:d916d321e60f 247 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 248 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz
mbed_official 442:d916d321e60f 249 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 250 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz
mbed_official 442:d916d321e60f 251 } else if ((hz >= 12000000) && (hz < 24000000)) {
mbed_official 442:d916d321e60f 252 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz
mbed_official 442:d916d321e60f 253 } else { // >= 24000000
mbed_official 442:d916d321e60f 254 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
mbed_official 442:d916d321e60f 255 }
mbed_official 442:d916d321e60f 256 // Note: The frequencies are obtained with SPI2/3 clock = 48 MHz (APB1 clock)
mbed_official 442:d916d321e60f 257 } else if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 442:d916d321e60f 258 if (hz < 375000) {
mbed_official 442:d916d321e60f 259 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 187.5 kHz
mbed_official 442:d916d321e60f 260 } else if ((hz >= 375000) && (hz < 750000)) {
mbed_official 442:d916d321e60f 261 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz
mbed_official 442:d916d321e60f 262 } else if ((hz >= 750000) && (hz < 1500000)) {
mbed_official 442:d916d321e60f 263 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 0.75 MHz
mbed_official 442:d916d321e60f 264 } else if ((hz >= 1500000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 265 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz
mbed_official 442:d916d321e60f 266 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 267 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz
mbed_official 442:d916d321e60f 268 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 269 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz
mbed_official 442:d916d321e60f 270 } else if ((hz >= 12000000) && (hz < 24000000)) {
mbed_official 442:d916d321e60f 271 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz
mbed_official 442:d916d321e60f 272 } else { // >= 24000000
mbed_official 442:d916d321e60f 273 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
mbed_official 442:d916d321e60f 274 }
mbed_official 442:d916d321e60f 275 }
mbed_official 442:d916d321e60f 276 #elif defined(TARGET_STM32F411RE) || defined(TARGET_STM32F429ZI)
mbed_official 442:d916d321e60f 277 // Values depend of PCLK2: 100 MHz
mbed_official 442:d916d321e60f 278 if ((obj->spi == SPI_1) || (obj->spi == SPI_4) || (obj->spi == SPI_5)) {
mbed_official 442:d916d321e60f 279 if (hz < 700000) {
mbed_official 442:d916d321e60f 280 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 391 kHz
mbed_official 442:d916d321e60f 281 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 282 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 781 kHz
mbed_official 442:d916d321e60f 283 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 284 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.56 MHz
mbed_official 442:d916d321e60f 285 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 286 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 3.13 MHz
mbed_official 442:d916d321e60f 287 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 288 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 6.25 MHz
mbed_official 442:d916d321e60f 289 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 442:d916d321e60f 290 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 12.5 MHz
mbed_official 442:d916d321e60f 291 } else if ((hz >= 25000000) && (hz < 50000000)) {
mbed_official 442:d916d321e60f 292 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 25 MHz
mbed_official 442:d916d321e60f 293 } else { // >= 50000000
mbed_official 442:d916d321e60f 294 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 50 MHz
mbed_official 442:d916d321e60f 295 }
mbed_official 442:d916d321e60f 296 }
mbed_official 442:d916d321e60f 297
mbed_official 442:d916d321e60f 298 // Values depend of PCLK1: 50 MHz
mbed_official 442:d916d321e60f 299 if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 442:d916d321e60f 300 if (hz < 400000) {
mbed_official 442:d916d321e60f 301 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 195 kHz
mbed_official 442:d916d321e60f 302 } else if ((hz >= 400000) && (hz < 700000)) {
mbed_official 442:d916d321e60f 303 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 391 kHz
mbed_official 442:d916d321e60f 304 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 442:d916d321e60f 305 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 781 MHz
mbed_official 442:d916d321e60f 306 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 442:d916d321e60f 307 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.56 MHz
mbed_official 442:d916d321e60f 308 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 442:d916d321e60f 309 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3.13 MHz
mbed_official 442:d916d321e60f 310 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 442:d916d321e60f 311 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6.25 MHz
mbed_official 442:d916d321e60f 312 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 442:d916d321e60f 313 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12.5 MHz
mbed_official 442:d916d321e60f 314 } else { // >= 25000000
mbed_official 442:d916d321e60f 315 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 25 MHz
mbed_official 442:d916d321e60f 316 }
mbed_official 442:d916d321e60f 317 }
mbed_official 442:d916d321e60f 318 #endif
mbed_official 442:d916d321e60f 319 init_spi(obj);
mbed_official 442:d916d321e60f 320 }
mbed_official 442:d916d321e60f 321
mbed_official 442:d916d321e60f 322 static inline int ssp_readable(spi_t *obj)
mbed_official 442:d916d321e60f 323 {
mbed_official 442:d916d321e60f 324 int status;
mbed_official 442:d916d321e60f 325 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 326 // Check if data is received
mbed_official 442:d916d321e60f 327 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 328 return status;
mbed_official 442:d916d321e60f 329 }
mbed_official 442:d916d321e60f 330
mbed_official 442:d916d321e60f 331 static inline int ssp_writeable(spi_t *obj)
mbed_official 442:d916d321e60f 332 {
mbed_official 442:d916d321e60f 333 int status;
mbed_official 442:d916d321e60f 334 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 335 // Check if data is transmitted
mbed_official 442:d916d321e60f 336 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 337 return status;
mbed_official 442:d916d321e60f 338 }
mbed_official 442:d916d321e60f 339
mbed_official 442:d916d321e60f 340 static inline void ssp_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 341 {
mbed_official 442:d916d321e60f 342 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 343 while (!ssp_writeable(obj));
mbed_official 442:d916d321e60f 344 spi->DR = (uint16_t)value;
mbed_official 442:d916d321e60f 345 }
mbed_official 442:d916d321e60f 346
mbed_official 442:d916d321e60f 347 static inline int ssp_read(spi_t *obj)
mbed_official 442:d916d321e60f 348 {
mbed_official 442:d916d321e60f 349 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 350 while (!ssp_readable(obj));
mbed_official 442:d916d321e60f 351 return (int)spi->DR;
mbed_official 442:d916d321e60f 352 }
mbed_official 442:d916d321e60f 353
mbed_official 442:d916d321e60f 354 static inline int ssp_busy(spi_t *obj)
mbed_official 442:d916d321e60f 355 {
mbed_official 442:d916d321e60f 356 int status;
mbed_official 442:d916d321e60f 357 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 358 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 442:d916d321e60f 359 return status;
mbed_official 442:d916d321e60f 360 }
mbed_official 442:d916d321e60f 361
mbed_official 442:d916d321e60f 362 int spi_master_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 363 {
mbed_official 442:d916d321e60f 364 ssp_write(obj, value);
mbed_official 442:d916d321e60f 365 return ssp_read(obj);
mbed_official 442:d916d321e60f 366 }
mbed_official 442:d916d321e60f 367
mbed_official 442:d916d321e60f 368 int spi_slave_receive(spi_t *obj)
mbed_official 442:d916d321e60f 369 {
mbed_official 442:d916d321e60f 370 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 442:d916d321e60f 371 };
mbed_official 442:d916d321e60f 372
mbed_official 442:d916d321e60f 373 int spi_slave_read(spi_t *obj)
mbed_official 442:d916d321e60f 374 {
mbed_official 442:d916d321e60f 375 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 376 while (!ssp_readable(obj));
mbed_official 442:d916d321e60f 377 return (int)spi->DR;
mbed_official 442:d916d321e60f 378 }
mbed_official 442:d916d321e60f 379
mbed_official 442:d916d321e60f 380 void spi_slave_write(spi_t *obj, int value)
mbed_official 442:d916d321e60f 381 {
mbed_official 442:d916d321e60f 382 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 442:d916d321e60f 383 while (!ssp_writeable(obj));
mbed_official 442:d916d321e60f 384 spi->DR = (uint16_t)value;
mbed_official 442:d916d321e60f 385 }
mbed_official 442:d916d321e60f 386
mbed_official 442:d916d321e60f 387 int spi_busy(spi_t *obj)
mbed_official 442:d916d321e60f 388 {
mbed_official 442:d916d321e60f 389 return ssp_busy(obj);
mbed_official 442:d916d321e60f 390 }
mbed_official 442:d916d321e60f 391
mbed_official 442:d916d321e60f 392 #endif