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 Jul 29 19:00:07 2014 +0100
Revision:
268:402bcc0c870b
Parent:
242:7074e42da0b2
Synchronized with git revision 490d1a6606b3138f165c5edf2f2370ca616587c0

Full URL: https://github.com/mbedmicro/mbed/commit/490d1a6606b3138f165c5edf2f2370ca616587c0/

[LPC1114] Sleep fix + some device.h settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 133:d4dda5c437f0 1 /* mbed Microcontroller Library
mbed_official 133:d4dda5c437f0 2 *******************************************************************************
mbed_official 133:d4dda5c437f0 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 133:d4dda5c437f0 4 * All rights reserved.
mbed_official 133:d4dda5c437f0 5 *
mbed_official 133:d4dda5c437f0 6 * Redistribution and use in source and binary forms, with or without
mbed_official 133:d4dda5c437f0 7 * modification, are permitted provided that the following conditions are met:
mbed_official 133:d4dda5c437f0 8 *
mbed_official 133:d4dda5c437f0 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 133:d4dda5c437f0 10 * this list of conditions and the following disclaimer.
mbed_official 133:d4dda5c437f0 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 133:d4dda5c437f0 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 133:d4dda5c437f0 13 * and/or other materials provided with the distribution.
mbed_official 133:d4dda5c437f0 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 133:d4dda5c437f0 15 * may be used to endorse or promote products derived from this software
mbed_official 133:d4dda5c437f0 16 * without specific prior written permission.
mbed_official 133:d4dda5c437f0 17 *
mbed_official 133:d4dda5c437f0 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 133:d4dda5c437f0 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 133:d4dda5c437f0 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 133:d4dda5c437f0 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 133:d4dda5c437f0 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 133:d4dda5c437f0 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 133:d4dda5c437f0 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 133:d4dda5c437f0 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 133:d4dda5c437f0 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 133:d4dda5c437f0 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 133:d4dda5c437f0 28 *******************************************************************************
mbed_official 133:d4dda5c437f0 29 */
mbed_official 242:7074e42da0b2 30 #include "mbed_assert.h"
mbed_official 133:d4dda5c437f0 31 #include "spi_api.h"
mbed_official 133:d4dda5c437f0 32
mbed_official 133:d4dda5c437f0 33 #if DEVICE_SPI
mbed_official 133:d4dda5c437f0 34
mbed_official 133:d4dda5c437f0 35 #include <math.h>
mbed_official 133:d4dda5c437f0 36 #include "cmsis.h"
mbed_official 133:d4dda5c437f0 37 #include "pinmap.h"
mbed_official 133:d4dda5c437f0 38
mbed_official 133:d4dda5c437f0 39 static const PinMap PinMap_SPI_MOSI[] = {
mbed_official 133:d4dda5c437f0 40 {PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 133:d4dda5c437f0 41 {PB_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 242:7074e42da0b2 42 // {PB_5, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 133:d4dda5c437f0 43 {PB_15, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 133:d4dda5c437f0 44 {PC_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 133:d4dda5c437f0 45 {PC_12, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 133:d4dda5c437f0 46 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 47 };
mbed_official 133:d4dda5c437f0 48
mbed_official 133:d4dda5c437f0 49 static const PinMap PinMap_SPI_MISO[] = {
mbed_official 133:d4dda5c437f0 50 {PA_6, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 133:d4dda5c437f0 51 {PB_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 242:7074e42da0b2 52 // {PB_4, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 133:d4dda5c437f0 53 {PB_14, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 133:d4dda5c437f0 54 {PC_2, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 133:d4dda5c437f0 55 {PC_11, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 133:d4dda5c437f0 56 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 57 };
mbed_official 133:d4dda5c437f0 58
mbed_official 133:d4dda5c437f0 59 static const PinMap PinMap_SPI_SCLK[] = {
mbed_official 133:d4dda5c437f0 60 {PA_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 133:d4dda5c437f0 61 {PB_3, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 242:7074e42da0b2 62 // {PB_3, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 133:d4dda5c437f0 63 {PB_10, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 133:d4dda5c437f0 64 {PB_13, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 133:d4dda5c437f0 65 {PC_10, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 133:d4dda5c437f0 66 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 67 };
mbed_official 133:d4dda5c437f0 68
mbed_official 133:d4dda5c437f0 69 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 242:7074e42da0b2 70 {PA_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF5_SPI1)},
mbed_official 242:7074e42da0b2 71 // {PA_4, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF6_SPI3)},
mbed_official 242:7074e42da0b2 72 {PA_15, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF5_SPI1)},
mbed_official 242:7074e42da0b2 73 // {PA_15, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF6_SPI3)},
mbed_official 242:7074e42da0b2 74 {PB_9, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF5_SPI2)},
mbed_official 242:7074e42da0b2 75 {PB_12, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF5_SPI2)},
mbed_official 133:d4dda5c437f0 76 {NC, NC, 0}
mbed_official 133:d4dda5c437f0 77 };
mbed_official 133:d4dda5c437f0 78
mbed_official 133:d4dda5c437f0 79 static SPI_HandleTypeDef SpiHandle;
mbed_official 133:d4dda5c437f0 80
mbed_official 133:d4dda5c437f0 81 static void init_spi(spi_t *obj) {
mbed_official 133:d4dda5c437f0 82 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 133:d4dda5c437f0 83
mbed_official 133:d4dda5c437f0 84 __HAL_SPI_DISABLE(&SpiHandle);
mbed_official 133:d4dda5c437f0 85
mbed_official 133:d4dda5c437f0 86 SpiHandle.Init.Mode = obj->mode;
mbed_official 133:d4dda5c437f0 87 SpiHandle.Init.BaudRatePrescaler = obj->br_presc;
mbed_official 133:d4dda5c437f0 88 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
mbed_official 133:d4dda5c437f0 89 SpiHandle.Init.CLKPhase = obj->cpha;
mbed_official 133:d4dda5c437f0 90 SpiHandle.Init.CLKPolarity = obj->cpol;
mbed_official 133:d4dda5c437f0 91 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
mbed_official 133:d4dda5c437f0 92 SpiHandle.Init.CRCPolynomial = 7;
mbed_official 133:d4dda5c437f0 93 SpiHandle.Init.DataSize = obj->bits;
mbed_official 133:d4dda5c437f0 94 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
mbed_official 133:d4dda5c437f0 95 SpiHandle.Init.NSS = obj->nss;
mbed_official 133:d4dda5c437f0 96 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
mbed_official 133:d4dda5c437f0 97
mbed_official 133:d4dda5c437f0 98 HAL_SPI_Init(&SpiHandle);
mbed_official 133:d4dda5c437f0 99
mbed_official 133:d4dda5c437f0 100 __HAL_SPI_ENABLE(&SpiHandle);
mbed_official 133:d4dda5c437f0 101 }
mbed_official 133:d4dda5c437f0 102
mbed_official 133:d4dda5c437f0 103 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
mbed_official 133:d4dda5c437f0 104 // Determine the SPI to use
mbed_official 133:d4dda5c437f0 105 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 133:d4dda5c437f0 106 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 133:d4dda5c437f0 107 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 133:d4dda5c437f0 108 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 133:d4dda5c437f0 109
mbed_official 133:d4dda5c437f0 110 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 133:d4dda5c437f0 111 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 133:d4dda5c437f0 112
mbed_official 133:d4dda5c437f0 113 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 227:7bd0639b8911 114 MBED_ASSERT(obj->spi != (SPIName)NC);
mbed_official 133:d4dda5c437f0 115
mbed_official 133:d4dda5c437f0 116 // Enable SPI clock
mbed_official 133:d4dda5c437f0 117 if (obj->spi == SPI_1) {
mbed_official 133:d4dda5c437f0 118 __SPI1_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 119 }
mbed_official 133:d4dda5c437f0 120 if (obj->spi == SPI_2) {
mbed_official 133:d4dda5c437f0 121 __SPI2_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 122 }
mbed_official 133:d4dda5c437f0 123 if (obj->spi == SPI_3) {
mbed_official 133:d4dda5c437f0 124 __SPI3_CLK_ENABLE();
mbed_official 133:d4dda5c437f0 125 }
mbed_official 133:d4dda5c437f0 126
mbed_official 133:d4dda5c437f0 127 // Configure the SPI pins
mbed_official 133:d4dda5c437f0 128 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 133:d4dda5c437f0 129 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 133:d4dda5c437f0 130 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 133:d4dda5c437f0 131
mbed_official 133:d4dda5c437f0 132 // Save new values
mbed_official 133:d4dda5c437f0 133 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 133:d4dda5c437f0 134 obj->cpol = SPI_POLARITY_LOW;
mbed_official 133:d4dda5c437f0 135 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 133:d4dda5c437f0 136 obj->br_presc = SPI_BAUDRATEPRESCALER_256;
mbed_official 133:d4dda5c437f0 137
mbed_official 242:7074e42da0b2 138 obj->pin_miso = miso;
mbed_official 242:7074e42da0b2 139 obj->pin_mosi = mosi;
mbed_official 242:7074e42da0b2 140 obj->pin_sclk = sclk;
mbed_official 242:7074e42da0b2 141 obj->pin_ssel = ssel;
mbed_official 242:7074e42da0b2 142
mbed_official 133:d4dda5c437f0 143 if (ssel == NC) { // SW NSS Master mode
mbed_official 133:d4dda5c437f0 144 obj->mode = SPI_MODE_MASTER;
mbed_official 133:d4dda5c437f0 145 obj->nss = SPI_NSS_SOFT;
mbed_official 242:7074e42da0b2 146 } else { // Slave
mbed_official 133:d4dda5c437f0 147 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 133:d4dda5c437f0 148 obj->mode = SPI_MODE_SLAVE;
mbed_official 133:d4dda5c437f0 149 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 133:d4dda5c437f0 150 }
mbed_official 133:d4dda5c437f0 151
mbed_official 133:d4dda5c437f0 152 init_spi(obj);
mbed_official 133:d4dda5c437f0 153 }
mbed_official 133:d4dda5c437f0 154
mbed_official 133:d4dda5c437f0 155 void spi_free(spi_t *obj) {
mbed_official 242:7074e42da0b2 156 // Reset SPI and disable clock
mbed_official 242:7074e42da0b2 157 if (obj->spi == SPI_1) {
mbed_official 242:7074e42da0b2 158 __SPI1_FORCE_RESET();
mbed_official 242:7074e42da0b2 159 __SPI1_RELEASE_RESET();
mbed_official 242:7074e42da0b2 160 __SPI1_CLK_DISABLE();
mbed_official 242:7074e42da0b2 161 }
mbed_official 242:7074e42da0b2 162
mbed_official 242:7074e42da0b2 163 if (obj->spi == SPI_2) {
mbed_official 242:7074e42da0b2 164 __SPI2_FORCE_RESET();
mbed_official 242:7074e42da0b2 165 __SPI2_RELEASE_RESET();
mbed_official 242:7074e42da0b2 166 __SPI2_CLK_DISABLE();
mbed_official 242:7074e42da0b2 167 }
mbed_official 242:7074e42da0b2 168
mbed_official 242:7074e42da0b2 169 if (obj->spi == SPI_3) {
mbed_official 242:7074e42da0b2 170 __SPI3_FORCE_RESET();
mbed_official 242:7074e42da0b2 171 __SPI3_RELEASE_RESET();
mbed_official 242:7074e42da0b2 172 __SPI3_CLK_DISABLE();
mbed_official 242:7074e42da0b2 173 }
mbed_official 242:7074e42da0b2 174
mbed_official 242:7074e42da0b2 175 // Configure GPIOs
mbed_official 242:7074e42da0b2 176 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 242:7074e42da0b2 177 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 242:7074e42da0b2 178 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 242:7074e42da0b2 179 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 133:d4dda5c437f0 180 }
mbed_official 133:d4dda5c437f0 181
mbed_official 227:7bd0639b8911 182 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 133:d4dda5c437f0 183 // Save new values
mbed_official 242:7074e42da0b2 184 if (bits == 16) {
mbed_official 242:7074e42da0b2 185 obj->bits = SPI_DATASIZE_16BIT;
mbed_official 242:7074e42da0b2 186 } else {
mbed_official 133:d4dda5c437f0 187 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 133:d4dda5c437f0 188 }
mbed_official 133:d4dda5c437f0 189
mbed_official 133:d4dda5c437f0 190 switch (mode) {
mbed_official 133:d4dda5c437f0 191 case 0:
mbed_official 133:d4dda5c437f0 192 obj->cpol = SPI_POLARITY_LOW;
mbed_official 133:d4dda5c437f0 193 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 133:d4dda5c437f0 194 break;
mbed_official 133:d4dda5c437f0 195 case 1:
mbed_official 133:d4dda5c437f0 196 obj->cpol = SPI_POLARITY_LOW;
mbed_official 133:d4dda5c437f0 197 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 133:d4dda5c437f0 198 break;
mbed_official 133:d4dda5c437f0 199 case 2:
mbed_official 133:d4dda5c437f0 200 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 227:7bd0639b8911 201 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 133:d4dda5c437f0 202 break;
mbed_official 133:d4dda5c437f0 203 default:
mbed_official 133:d4dda5c437f0 204 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 227:7bd0639b8911 205 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 133:d4dda5c437f0 206 break;
mbed_official 133:d4dda5c437f0 207 }
mbed_official 133:d4dda5c437f0 208
mbed_official 133:d4dda5c437f0 209 if (slave == 0) {
mbed_official 133:d4dda5c437f0 210 obj->mode = SPI_MODE_MASTER;
mbed_official 133:d4dda5c437f0 211 obj->nss = SPI_NSS_SOFT;
mbed_official 242:7074e42da0b2 212 } else {
mbed_official 133:d4dda5c437f0 213 obj->mode = SPI_MODE_SLAVE;
mbed_official 227:7bd0639b8911 214 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 133:d4dda5c437f0 215 }
mbed_official 133:d4dda5c437f0 216
mbed_official 133:d4dda5c437f0 217 init_spi(obj);
mbed_official 133:d4dda5c437f0 218 }
mbed_official 133:d4dda5c437f0 219
mbed_official 133:d4dda5c437f0 220 void spi_frequency(spi_t *obj, int hz) {
mbed_official 133:d4dda5c437f0 221 // Note: The frequencies are obtained with SPI1 clock = 84 MHz (APB2 clock)
mbed_official 133:d4dda5c437f0 222 if (hz < 600000) {
mbed_official 133:d4dda5c437f0 223 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 330 kHz
mbed_official 242:7074e42da0b2 224 } else if ((hz >= 600000) && (hz < 1000000)) {
mbed_official 133:d4dda5c437f0 225 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 656 kHz
mbed_official 242:7074e42da0b2 226 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 133:d4dda5c437f0 227 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.3 MHz
mbed_official 242:7074e42da0b2 228 } else if ((hz >= 2000000) && (hz < 5000000)) {
mbed_official 133:d4dda5c437f0 229 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2.6 MHz
mbed_official 242:7074e42da0b2 230 } else if ((hz >= 5000000) && (hz < 10000000)) {
mbed_official 133:d4dda5c437f0 231 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 5.25 MHz
mbed_official 242:7074e42da0b2 232 } else if ((hz >= 10000000) && (hz < 21000000)) {
mbed_official 133:d4dda5c437f0 233 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 10.5 MHz
mbed_official 242:7074e42da0b2 234 } else if ((hz >= 21000000) && (hz < 42000000)) {
mbed_official 133:d4dda5c437f0 235 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 21 MHz
mbed_official 242:7074e42da0b2 236 } else { // >= 42000000
mbed_official 133:d4dda5c437f0 237 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 42 MHz
mbed_official 133:d4dda5c437f0 238 }
mbed_official 133:d4dda5c437f0 239 init_spi(obj);
mbed_official 133:d4dda5c437f0 240 }
mbed_official 133:d4dda5c437f0 241
mbed_official 133:d4dda5c437f0 242 static inline int ssp_readable(spi_t *obj) {
mbed_official 133:d4dda5c437f0 243 int status;
mbed_official 133:d4dda5c437f0 244 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 133:d4dda5c437f0 245 // Check if data is received
mbed_official 133:d4dda5c437f0 246 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 227:7bd0639b8911 247 return status;
mbed_official 133:d4dda5c437f0 248 }
mbed_official 133:d4dda5c437f0 249
mbed_official 133:d4dda5c437f0 250 static inline int ssp_writeable(spi_t *obj) {
mbed_official 133:d4dda5c437f0 251 int status;
mbed_official 133:d4dda5c437f0 252 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 133:d4dda5c437f0 253 // Check if data is transmitted
mbed_official 133:d4dda5c437f0 254 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 133:d4dda5c437f0 255 return status;
mbed_official 133:d4dda5c437f0 256 }
mbed_official 133:d4dda5c437f0 257
mbed_official 133:d4dda5c437f0 258 static inline void ssp_write(spi_t *obj, int value) {
mbed_official 133:d4dda5c437f0 259 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 133:d4dda5c437f0 260 while (!ssp_writeable(obj));
mbed_official 133:d4dda5c437f0 261 spi->DR = (uint16_t)value;
mbed_official 133:d4dda5c437f0 262 }
mbed_official 133:d4dda5c437f0 263
mbed_official 133:d4dda5c437f0 264 static inline int ssp_read(spi_t *obj) {
mbed_official 133:d4dda5c437f0 265 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 133:d4dda5c437f0 266 while (!ssp_readable(obj));
mbed_official 133:d4dda5c437f0 267 return (int)spi->DR;
mbed_official 133:d4dda5c437f0 268 }
mbed_official 133:d4dda5c437f0 269
mbed_official 133:d4dda5c437f0 270 static inline int ssp_busy(spi_t *obj) {
mbed_official 133:d4dda5c437f0 271 int status;
mbed_official 133:d4dda5c437f0 272 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 133:d4dda5c437f0 273 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 133:d4dda5c437f0 274 return status;
mbed_official 133:d4dda5c437f0 275 }
mbed_official 133:d4dda5c437f0 276
mbed_official 133:d4dda5c437f0 277 int spi_master_write(spi_t *obj, int value) {
mbed_official 133:d4dda5c437f0 278 ssp_write(obj, value);
mbed_official 133:d4dda5c437f0 279 return ssp_read(obj);
mbed_official 133:d4dda5c437f0 280 }
mbed_official 133:d4dda5c437f0 281
mbed_official 133:d4dda5c437f0 282 int spi_slave_receive(spi_t *obj) {
mbed_official 242:7074e42da0b2 283 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 133:d4dda5c437f0 284 };
mbed_official 133:d4dda5c437f0 285
mbed_official 133:d4dda5c437f0 286 int spi_slave_read(spi_t *obj) {
mbed_official 133:d4dda5c437f0 287 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 242:7074e42da0b2 288 while (!ssp_readable(obj));
mbed_official 133:d4dda5c437f0 289 return (int)spi->DR;
mbed_official 133:d4dda5c437f0 290 }
mbed_official 133:d4dda5c437f0 291
mbed_official 133:d4dda5c437f0 292 void spi_slave_write(spi_t *obj, int value) {
mbed_official 133:d4dda5c437f0 293 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 133:d4dda5c437f0 294 while (!ssp_writeable(obj));
mbed_official 133:d4dda5c437f0 295 spi->DR = (uint16_t)value;
mbed_official 133:d4dda5c437f0 296 }
mbed_official 133:d4dda5c437f0 297
mbed_official 133:d4dda5c437f0 298 int spi_busy(spi_t *obj) {
mbed_official 133:d4dda5c437f0 299 return ssp_busy(obj);
mbed_official 133:d4dda5c437f0 300 }
mbed_official 133:d4dda5c437f0 301
mbed_official 133:d4dda5c437f0 302 #endif