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