Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Freedman_v2 Nucleo_i2c_OLED_BME280_copy
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_STM32L1/spi_api.c@552:a1b9575155a3, 2015-05-27 (annotated)
- Committer:
- mbed_official
- Date:
- Wed May 27 13:30:08 2015 +0100
- Revision:
- 552:a1b9575155a3
- Parent:
- 496:543871686697
Synchronized with git revision a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207
Full URL: https://github.com/mbedmicro/mbed/commit/a74a8f14fd8c4bf3dc09980c4bf9498ebcf4c207/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 76:aeb1df146756 | 1 | /* mbed Microcontroller Library |
mbed_official | 76:aeb1df146756 | 2 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 76:aeb1df146756 | 4 | * All rights reserved. |
mbed_official | 76:aeb1df146756 | 5 | * |
mbed_official | 76:aeb1df146756 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 76:aeb1df146756 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 76:aeb1df146756 | 8 | * |
mbed_official | 76:aeb1df146756 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 76:aeb1df146756 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 76:aeb1df146756 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 76:aeb1df146756 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 76:aeb1df146756 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 76:aeb1df146756 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 76:aeb1df146756 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 76:aeb1df146756 | 16 | * without specific prior written permission. |
mbed_official | 76:aeb1df146756 | 17 | * |
mbed_official | 76:aeb1df146756 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 76:aeb1df146756 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 76:aeb1df146756 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 76:aeb1df146756 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 76:aeb1df146756 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 76:aeb1df146756 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 76:aeb1df146756 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 76:aeb1df146756 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 76:aeb1df146756 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 76:aeb1df146756 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 76:aeb1df146756 | 28 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 29 | */ |
mbed_official | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 76:aeb1df146756 | 31 | #include "spi_api.h" |
mbed_official | 76:aeb1df146756 | 32 | |
mbed_official | 76:aeb1df146756 | 33 | #if DEVICE_SPI |
mbed_official | 76:aeb1df146756 | 34 | |
mbed_official | 76:aeb1df146756 | 35 | #include <math.h> |
mbed_official | 76:aeb1df146756 | 36 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 37 | #include "pinmap.h" |
mbed_official | 414:4ec4c5b614b0 | 38 | #include "PeripheralPins.h" |
mbed_official | 76:aeb1df146756 | 39 | |
mbed_official | 354:e67efb2aab0e | 40 | static SPI_HandleTypeDef SpiHandle; |
mbed_official | 76:aeb1df146756 | 41 | |
mbed_official | 354:e67efb2aab0e | 42 | static void init_spi(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 43 | { |
mbed_official | 354:e67efb2aab0e | 44 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 354:e67efb2aab0e | 45 | |
mbed_official | 354:e67efb2aab0e | 46 | __HAL_SPI_DISABLE(&SpiHandle); |
mbed_official | 76:aeb1df146756 | 47 | |
mbed_official | 354:e67efb2aab0e | 48 | SpiHandle.Init.Mode = obj->mode; |
mbed_official | 354:e67efb2aab0e | 49 | SpiHandle.Init.BaudRatePrescaler = obj->br_presc; |
mbed_official | 354:e67efb2aab0e | 50 | SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; |
mbed_official | 354:e67efb2aab0e | 51 | SpiHandle.Init.CLKPhase = obj->cpha; |
mbed_official | 354:e67efb2aab0e | 52 | SpiHandle.Init.CLKPolarity = obj->cpol; |
mbed_official | 354:e67efb2aab0e | 53 | SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; |
mbed_official | 354:e67efb2aab0e | 54 | SpiHandle.Init.CRCPolynomial = 7; |
mbed_official | 354:e67efb2aab0e | 55 | SpiHandle.Init.DataSize = obj->bits; |
mbed_official | 354:e67efb2aab0e | 56 | SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; |
mbed_official | 354:e67efb2aab0e | 57 | SpiHandle.Init.NSS = obj->nss; |
mbed_official | 354:e67efb2aab0e | 58 | SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; |
mbed_official | 76:aeb1df146756 | 59 | |
mbed_official | 354:e67efb2aab0e | 60 | HAL_SPI_Init(&SpiHandle); |
mbed_official | 354:e67efb2aab0e | 61 | |
mbed_official | 354:e67efb2aab0e | 62 | __HAL_SPI_ENABLE(&SpiHandle); |
mbed_official | 76:aeb1df146756 | 63 | } |
mbed_official | 76:aeb1df146756 | 64 | |
mbed_official | 354:e67efb2aab0e | 65 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) |
mbed_official | 354:e67efb2aab0e | 66 | { |
mbed_official | 76:aeb1df146756 | 67 | // Determine the SPI to use |
mbed_official | 76:aeb1df146756 | 68 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 76:aeb1df146756 | 69 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 76:aeb1df146756 | 70 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 76:aeb1df146756 | 71 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 174:8bb9f3a33240 | 72 | |
mbed_official | 76:aeb1df146756 | 73 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 76:aeb1df146756 | 74 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 174:8bb9f3a33240 | 75 | |
mbed_official | 76:aeb1df146756 | 76 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 227:7bd0639b8911 | 77 | MBED_ASSERT(obj->spi != (SPIName)NC); |
mbed_official | 174:8bb9f3a33240 | 78 | |
mbed_official | 76:aeb1df146756 | 79 | // Enable SPI clock |
mbed_official | 76:aeb1df146756 | 80 | if (obj->spi == SPI_1) { |
mbed_official | 354:e67efb2aab0e | 81 | __SPI1_CLK_ENABLE(); |
mbed_official | 76:aeb1df146756 | 82 | } |
mbed_official | 76:aeb1df146756 | 83 | if (obj->spi == SPI_2) { |
mbed_official | 354:e67efb2aab0e | 84 | __SPI2_CLK_ENABLE(); |
mbed_official | 76:aeb1df146756 | 85 | } |
mbed_official | 118:b44c45162f28 | 86 | if (obj->spi == SPI_3) { |
mbed_official | 354:e67efb2aab0e | 87 | __SPI3_CLK_ENABLE(); |
mbed_official | 118:b44c45162f28 | 88 | } |
mbed_official | 174:8bb9f3a33240 | 89 | |
mbed_official | 76:aeb1df146756 | 90 | // Configure the SPI pins |
mbed_official | 76:aeb1df146756 | 91 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 76:aeb1df146756 | 92 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 76:aeb1df146756 | 93 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 174:8bb9f3a33240 | 94 | |
mbed_official | 76:aeb1df146756 | 95 | // Save new values |
mbed_official | 354:e67efb2aab0e | 96 | obj->bits = SPI_DATASIZE_8BIT; |
mbed_official | 354:e67efb2aab0e | 97 | obj->cpol = SPI_POLARITY_LOW; |
mbed_official | 354:e67efb2aab0e | 98 | obj->cpha = SPI_PHASE_1EDGE; |
mbed_official | 354:e67efb2aab0e | 99 | obj->br_presc = SPI_BAUDRATEPRESCALER_256; |
mbed_official | 174:8bb9f3a33240 | 100 | |
mbed_official | 215:83cf97a28428 | 101 | obj->pin_miso = miso; |
mbed_official | 215:83cf97a28428 | 102 | obj->pin_mosi = mosi; |
mbed_official | 215:83cf97a28428 | 103 | obj->pin_sclk = sclk; |
mbed_official | 215:83cf97a28428 | 104 | obj->pin_ssel = ssel; |
mbed_official | 215:83cf97a28428 | 105 | |
mbed_official | 552:a1b9575155a3 | 106 | if (ssel != NC) { |
mbed_official | 552:a1b9575155a3 | 107 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 552:a1b9575155a3 | 108 | } else { |
mbed_official | 354:e67efb2aab0e | 109 | obj->nss = SPI_NSS_SOFT; |
mbed_official | 76:aeb1df146756 | 110 | } |
mbed_official | 76:aeb1df146756 | 111 | |
mbed_official | 76:aeb1df146756 | 112 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 113 | } |
mbed_official | 76:aeb1df146756 | 114 | |
mbed_official | 354:e67efb2aab0e | 115 | void spi_free(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 116 | { |
mbed_official | 215:83cf97a28428 | 117 | // Reset SPI and disable clock |
mbed_official | 215:83cf97a28428 | 118 | if (obj->spi == SPI_1) { |
mbed_official | 354:e67efb2aab0e | 119 | __SPI1_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 120 | __SPI1_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 121 | __SPI1_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 122 | } |
mbed_official | 215:83cf97a28428 | 123 | |
mbed_official | 215:83cf97a28428 | 124 | if (obj->spi == SPI_2) { |
mbed_official | 354:e67efb2aab0e | 125 | __SPI2_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 126 | __SPI2_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 127 | __SPI2_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 128 | } |
mbed_official | 215:83cf97a28428 | 129 | |
mbed_official | 215:83cf97a28428 | 130 | if (obj->spi == SPI_3) { |
mbed_official | 354:e67efb2aab0e | 131 | __SPI3_FORCE_RESET(); |
mbed_official | 354:e67efb2aab0e | 132 | __SPI3_RELEASE_RESET(); |
mbed_official | 354:e67efb2aab0e | 133 | __SPI3_CLK_DISABLE(); |
mbed_official | 215:83cf97a28428 | 134 | } |
mbed_official | 215:83cf97a28428 | 135 | |
mbed_official | 354:e67efb2aab0e | 136 | // Configure GPIO |
mbed_official | 354:e67efb2aab0e | 137 | pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 354:e67efb2aab0e | 138 | pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 354:e67efb2aab0e | 139 | pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 354:e67efb2aab0e | 140 | pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 76:aeb1df146756 | 141 | } |
mbed_official | 76:aeb1df146756 | 142 | |
mbed_official | 354:e67efb2aab0e | 143 | void spi_format(spi_t *obj, int bits, int mode, int slave) |
mbed_official | 354:e67efb2aab0e | 144 | { |
mbed_official | 76:aeb1df146756 | 145 | // Save new values |
mbed_official | 233:1bbc1451db33 | 146 | if (bits == 16) { |
mbed_official | 354:e67efb2aab0e | 147 | obj->bits = SPI_DATASIZE_16BIT; |
mbed_official | 233:1bbc1451db33 | 148 | } else { |
mbed_official | 354:e67efb2aab0e | 149 | obj->bits = SPI_DATASIZE_8BIT; |
mbed_official | 76:aeb1df146756 | 150 | } |
mbed_official | 174:8bb9f3a33240 | 151 | |
mbed_official | 76:aeb1df146756 | 152 | switch (mode) { |
mbed_official | 76:aeb1df146756 | 153 | case 0: |
mbed_official | 354:e67efb2aab0e | 154 | obj->cpol = SPI_POLARITY_LOW; |
mbed_official | 354:e67efb2aab0e | 155 | obj->cpha = SPI_PHASE_1EDGE; |
mbed_official | 174:8bb9f3a33240 | 156 | break; |
mbed_official | 76:aeb1df146756 | 157 | case 1: |
mbed_official | 354:e67efb2aab0e | 158 | obj->cpol = SPI_POLARITY_LOW; |
mbed_official | 354:e67efb2aab0e | 159 | obj->cpha = SPI_PHASE_2EDGE; |
mbed_official | 174:8bb9f3a33240 | 160 | break; |
mbed_official | 76:aeb1df146756 | 161 | case 2: |
mbed_official | 354:e67efb2aab0e | 162 | obj->cpol = SPI_POLARITY_HIGH; |
mbed_official | 354:e67efb2aab0e | 163 | obj->cpha = SPI_PHASE_1EDGE; |
mbed_official | 174:8bb9f3a33240 | 164 | break; |
mbed_official | 76:aeb1df146756 | 165 | default: |
mbed_official | 354:e67efb2aab0e | 166 | obj->cpol = SPI_POLARITY_HIGH; |
mbed_official | 354:e67efb2aab0e | 167 | obj->cpha = SPI_PHASE_2EDGE; |
mbed_official | 174:8bb9f3a33240 | 168 | break; |
mbed_official | 76:aeb1df146756 | 169 | } |
mbed_official | 174:8bb9f3a33240 | 170 | |
mbed_official | 552:a1b9575155a3 | 171 | if (obj->nss != SPI_NSS_SOFT) { |
mbed_official | 552:a1b9575155a3 | 172 | obj->nss = (slave) ? SPI_NSS_HARD_INPUT : SPI_NSS_HARD_OUTPUT; |
mbed_official | 76:aeb1df146756 | 173 | } |
mbed_official | 174:8bb9f3a33240 | 174 | |
mbed_official | 552:a1b9575155a3 | 175 | obj->mode = (slave) ? SPI_MODE_SLAVE : SPI_MODE_MASTER; |
mbed_official | 552:a1b9575155a3 | 176 | |
mbed_official | 76:aeb1df146756 | 177 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 178 | } |
mbed_official | 76:aeb1df146756 | 179 | |
mbed_official | 354:e67efb2aab0e | 180 | void spi_frequency(spi_t *obj, int hz) |
mbed_official | 354:e67efb2aab0e | 181 | { |
mbed_official | 139:e3413eddde57 | 182 | // Values depend of PCLK1 and PCLK2: 32 MHz if HSI is used, 24 MHz if HSE is used |
mbed_official | 139:e3413eddde57 | 183 | if (SystemCoreClock == 32000000) { // HSI |
mbed_official | 139:e3413eddde57 | 184 | if (hz < 250000) { |
mbed_official | 354:e67efb2aab0e | 185 | obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 125 kHz |
mbed_official | 174:8bb9f3a33240 | 186 | } else if ((hz >= 250000) && (hz < 500000)) { |
mbed_official | 354:e67efb2aab0e | 187 | obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 250 kHz |
mbed_official | 174:8bb9f3a33240 | 188 | } else if ((hz >= 500000) && (hz < 1000000)) { |
mbed_official | 354:e67efb2aab0e | 189 | obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 500 kHz |
mbed_official | 174:8bb9f3a33240 | 190 | } else if ((hz >= 1000000) && (hz < 2000000)) { |
mbed_official | 354:e67efb2aab0e | 191 | obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1 MHz |
mbed_official | 174:8bb9f3a33240 | 192 | } else if ((hz >= 2000000) && (hz < 4000000)) { |
mbed_official | 354:e67efb2aab0e | 193 | obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 2 MHz |
mbed_official | 174:8bb9f3a33240 | 194 | } else if ((hz >= 4000000) && (hz < 8000000)) { |
mbed_official | 354:e67efb2aab0e | 195 | obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 4 MHz |
mbed_official | 174:8bb9f3a33240 | 196 | } else if ((hz >= 8000000) && (hz < 16000000)) { |
mbed_official | 354:e67efb2aab0e | 197 | obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 8 MHz |
mbed_official | 174:8bb9f3a33240 | 198 | } else { // >= 16000000 |
mbed_official | 354:e67efb2aab0e | 199 | obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 16 MHz |
mbed_official | 139:e3413eddde57 | 200 | } |
mbed_official | 174:8bb9f3a33240 | 201 | } else { // 24 MHz - HSE |
mbed_official | 139:e3413eddde57 | 202 | if (hz < 180000) { |
mbed_official | 354:e67efb2aab0e | 203 | obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 94 kHz |
mbed_official | 174:8bb9f3a33240 | 204 | } else if ((hz >= 180000) && (hz < 350000)) { |
mbed_official | 354:e67efb2aab0e | 205 | obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 188 kHz |
mbed_official | 174:8bb9f3a33240 | 206 | } else if ((hz >= 350000) && (hz < 750000)) { |
mbed_official | 354:e67efb2aab0e | 207 | obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 375 kHz |
mbed_official | 174:8bb9f3a33240 | 208 | } else if ((hz >= 750000) && (hz < 1000000)) { |
mbed_official | 354:e67efb2aab0e | 209 | obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 750 kHz |
mbed_official | 174:8bb9f3a33240 | 210 | } else if ((hz >= 1000000) && (hz < 3000000)) { |
mbed_official | 354:e67efb2aab0e | 211 | obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 1.5 MHz |
mbed_official | 174:8bb9f3a33240 | 212 | } else if ((hz >= 3000000) && (hz < 6000000)) { |
mbed_official | 354:e67efb2aab0e | 213 | obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 3 MHz |
mbed_official | 174:8bb9f3a33240 | 214 | } else if ((hz >= 6000000) && (hz < 12000000)) { |
mbed_official | 354:e67efb2aab0e | 215 | obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 6 MHz |
mbed_official | 174:8bb9f3a33240 | 216 | } else { // >= 12000000 |
mbed_official | 354:e67efb2aab0e | 217 | obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 12 MHz |
mbed_official | 139:e3413eddde57 | 218 | } |
mbed_official | 129:0182c99221bc | 219 | } |
mbed_official | 76:aeb1df146756 | 220 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 221 | } |
mbed_official | 76:aeb1df146756 | 222 | |
mbed_official | 354:e67efb2aab0e | 223 | static inline int ssp_readable(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 224 | { |
mbed_official | 76:aeb1df146756 | 225 | int status; |
mbed_official | 354:e67efb2aab0e | 226 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 227 | // Check if data is received |
mbed_official | 354:e67efb2aab0e | 228 | status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 174:8bb9f3a33240 | 229 | return status; |
mbed_official | 76:aeb1df146756 | 230 | } |
mbed_official | 76:aeb1df146756 | 231 | |
mbed_official | 354:e67efb2aab0e | 232 | static inline int ssp_writeable(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 233 | { |
mbed_official | 76:aeb1df146756 | 234 | int status; |
mbed_official | 354:e67efb2aab0e | 235 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 236 | // Check if data is transmitted |
mbed_official | 354:e67efb2aab0e | 237 | status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 238 | return status; |
mbed_official | 76:aeb1df146756 | 239 | } |
mbed_official | 76:aeb1df146756 | 240 | |
mbed_official | 354:e67efb2aab0e | 241 | static inline void ssp_write(spi_t *obj, int value) |
mbed_official | 354:e67efb2aab0e | 242 | { |
mbed_official | 174:8bb9f3a33240 | 243 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 244 | while (!ssp_writeable(obj)); |
mbed_official | 354:e67efb2aab0e | 245 | spi->DR = (uint16_t)value; |
mbed_official | 76:aeb1df146756 | 246 | } |
mbed_official | 76:aeb1df146756 | 247 | |
mbed_official | 354:e67efb2aab0e | 248 | static inline int ssp_read(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 249 | { |
mbed_official | 174:8bb9f3a33240 | 250 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 251 | while (!ssp_readable(obj)); |
mbed_official | 354:e67efb2aab0e | 252 | return (int)spi->DR; |
mbed_official | 76:aeb1df146756 | 253 | } |
mbed_official | 76:aeb1df146756 | 254 | |
mbed_official | 354:e67efb2aab0e | 255 | static inline int ssp_busy(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 256 | { |
mbed_official | 76:aeb1df146756 | 257 | int status; |
mbed_official | 354:e67efb2aab0e | 258 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 354:e67efb2aab0e | 259 | status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 260 | return status; |
mbed_official | 76:aeb1df146756 | 261 | } |
mbed_official | 76:aeb1df146756 | 262 | |
mbed_official | 354:e67efb2aab0e | 263 | int spi_master_write(spi_t *obj, int value) |
mbed_official | 354:e67efb2aab0e | 264 | { |
mbed_official | 76:aeb1df146756 | 265 | ssp_write(obj, value); |
mbed_official | 76:aeb1df146756 | 266 | return ssp_read(obj); |
mbed_official | 76:aeb1df146756 | 267 | } |
mbed_official | 76:aeb1df146756 | 268 | |
mbed_official | 354:e67efb2aab0e | 269 | int spi_slave_receive(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 270 | { |
mbed_official | 233:1bbc1451db33 | 271 | return (ssp_readable(obj) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 272 | }; |
mbed_official | 76:aeb1df146756 | 273 | |
mbed_official | 354:e67efb2aab0e | 274 | int spi_slave_read(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 275 | { |
mbed_official | 76:aeb1df146756 | 276 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 354:e67efb2aab0e | 277 | while (!ssp_readable(obj)); |
mbed_official | 354:e67efb2aab0e | 278 | return (int)spi->DR; |
mbed_official | 76:aeb1df146756 | 279 | } |
mbed_official | 76:aeb1df146756 | 280 | |
mbed_official | 354:e67efb2aab0e | 281 | void spi_slave_write(spi_t *obj, int value) |
mbed_official | 354:e67efb2aab0e | 282 | { |
mbed_official | 174:8bb9f3a33240 | 283 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 174:8bb9f3a33240 | 284 | while (!ssp_writeable(obj)); |
mbed_official | 354:e67efb2aab0e | 285 | spi->DR = (uint16_t)value; |
mbed_official | 76:aeb1df146756 | 286 | } |
mbed_official | 76:aeb1df146756 | 287 | |
mbed_official | 354:e67efb2aab0e | 288 | int spi_busy(spi_t *obj) |
mbed_official | 354:e67efb2aab0e | 289 | { |
mbed_official | 76:aeb1df146756 | 290 | return ssp_busy(obj); |
mbed_official | 76:aeb1df146756 | 291 | } |
mbed_official | 76:aeb1df146756 | 292 | |
mbed_official | 76:aeb1df146756 | 293 | #endif |