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