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: WizFi250_AP_HelloWorld
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_STM32F3XX/spi_api.c@155:8435094ec241, 2014-04-08 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Apr 08 09:15:06 2014 +0100
- Revision:
- 155:8435094ec241
- Child:
- 227:7bd0639b8911
Synchronized with git revision d616c415fc62f490b915ff0ff39c8d24b2917c0f
Full URL: https://github.com/mbedmicro/mbed/commit/d616c415fc62f490b915ff0ff39c8d24b2917c0f/
[STM32F3XX] Initial port
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 155:8435094ec241 | 1 | /* mbed Microcontroller Library |
mbed_official | 155:8435094ec241 | 2 | ******************************************************************************* |
mbed_official | 155:8435094ec241 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 155:8435094ec241 | 4 | * All rights reserved. |
mbed_official | 155:8435094ec241 | 5 | * |
mbed_official | 155:8435094ec241 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 155:8435094ec241 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 155:8435094ec241 | 8 | * |
mbed_official | 155:8435094ec241 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 155:8435094ec241 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 155:8435094ec241 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 155:8435094ec241 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 155:8435094ec241 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 155:8435094ec241 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 155:8435094ec241 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 155:8435094ec241 | 16 | * without specific prior written permission. |
mbed_official | 155:8435094ec241 | 17 | * |
mbed_official | 155:8435094ec241 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 155:8435094ec241 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 155:8435094ec241 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 155:8435094ec241 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 155:8435094ec241 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 155:8435094ec241 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 155:8435094ec241 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 155:8435094ec241 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 155:8435094ec241 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 155:8435094ec241 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 155:8435094ec241 | 28 | ******************************************************************************* |
mbed_official | 155:8435094ec241 | 29 | */ |
mbed_official | 155:8435094ec241 | 30 | #include "spi_api.h" |
mbed_official | 155:8435094ec241 | 31 | |
mbed_official | 155:8435094ec241 | 32 | #if DEVICE_SPI |
mbed_official | 155:8435094ec241 | 33 | |
mbed_official | 155:8435094ec241 | 34 | #include <math.h> |
mbed_official | 155:8435094ec241 | 35 | #include "cmsis.h" |
mbed_official | 155:8435094ec241 | 36 | #include "pinmap.h" |
mbed_official | 155:8435094ec241 | 37 | #include "error.h" |
mbed_official | 155:8435094ec241 | 38 | |
mbed_official | 155:8435094ec241 | 39 | static const PinMap PinMap_SPI_MOSI[] = { |
mbed_official | 155:8435094ec241 | 40 | {PA_11, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 41 | {PB_5, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 42 | {PB_15, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 43 | {PC_12, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 44 | {NC, NC, 0} |
mbed_official | 155:8435094ec241 | 45 | }; |
mbed_official | 155:8435094ec241 | 46 | |
mbed_official | 155:8435094ec241 | 47 | static const PinMap PinMap_SPI_MISO[] = { |
mbed_official | 155:8435094ec241 | 48 | {PA_10, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 49 | {PB_4, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 50 | {PB_14, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 51 | {PC_11, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 52 | {NC, NC, 0} |
mbed_official | 155:8435094ec241 | 53 | }; |
mbed_official | 155:8435094ec241 | 54 | |
mbed_official | 155:8435094ec241 | 55 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 155:8435094ec241 | 56 | {PB_3, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 57 | {PB_13, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 58 | {PC_10, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 59 | {PF_1, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 60 | {NC, NC, 0} |
mbed_official | 155:8435094ec241 | 61 | }; |
mbed_official | 155:8435094ec241 | 62 | |
mbed_official | 155:8435094ec241 | 63 | static const PinMap PinMap_SPI_SSEL[] = { |
mbed_official | 155:8435094ec241 | 64 | {PA_4, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 65 | {PA_15, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_6)}, |
mbed_official | 155:8435094ec241 | 66 | {PB_12, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 67 | {PF_0, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_DOWN, GPIO_AF_5)}, |
mbed_official | 155:8435094ec241 | 68 | {NC, NC, 0} |
mbed_official | 155:8435094ec241 | 69 | }; |
mbed_official | 155:8435094ec241 | 70 | |
mbed_official | 155:8435094ec241 | 71 | static void init_spi(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 72 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 73 | SPI_InitTypeDef SPI_InitStructure; |
mbed_official | 155:8435094ec241 | 74 | |
mbed_official | 155:8435094ec241 | 75 | SPI_Cmd(spi, DISABLE); |
mbed_official | 155:8435094ec241 | 76 | |
mbed_official | 155:8435094ec241 | 77 | SPI_InitStructure.SPI_Mode = obj->mode; |
mbed_official | 155:8435094ec241 | 78 | SPI_InitStructure.SPI_NSS = obj->nss; |
mbed_official | 155:8435094ec241 | 79 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; |
mbed_official | 155:8435094ec241 | 80 | SPI_InitStructure.SPI_DataSize = obj->bits; |
mbed_official | 155:8435094ec241 | 81 | SPI_InitStructure.SPI_CPOL = obj->cpol; |
mbed_official | 155:8435094ec241 | 82 | SPI_InitStructure.SPI_CPHA = obj->cpha; |
mbed_official | 155:8435094ec241 | 83 | SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; |
mbed_official | 155:8435094ec241 | 84 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; |
mbed_official | 155:8435094ec241 | 85 | SPI_InitStructure.SPI_CRCPolynomial = 7; |
mbed_official | 155:8435094ec241 | 86 | SPI_Init(spi, &SPI_InitStructure); |
mbed_official | 155:8435094ec241 | 87 | |
mbed_official | 155:8435094ec241 | 88 | SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF); |
mbed_official | 155:8435094ec241 | 89 | |
mbed_official | 155:8435094ec241 | 90 | SPI_Cmd(spi, ENABLE); |
mbed_official | 155:8435094ec241 | 91 | } |
mbed_official | 155:8435094ec241 | 92 | |
mbed_official | 155:8435094ec241 | 93 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 155:8435094ec241 | 94 | // Determine the SPI to use |
mbed_official | 155:8435094ec241 | 95 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 155:8435094ec241 | 96 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 155:8435094ec241 | 97 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 155:8435094ec241 | 98 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 155:8435094ec241 | 99 | |
mbed_official | 155:8435094ec241 | 100 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 155:8435094ec241 | 101 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 155:8435094ec241 | 102 | |
mbed_official | 155:8435094ec241 | 103 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 155:8435094ec241 | 104 | |
mbed_official | 155:8435094ec241 | 105 | if (obj->spi == (SPIName)NC) { |
mbed_official | 155:8435094ec241 | 106 | error("SPI pinout mapping failed"); |
mbed_official | 155:8435094ec241 | 107 | } |
mbed_official | 155:8435094ec241 | 108 | |
mbed_official | 155:8435094ec241 | 109 | // Enable SPI clock |
mbed_official | 155:8435094ec241 | 110 | if (obj->spi == SPI_2) { |
mbed_official | 155:8435094ec241 | 111 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 155:8435094ec241 | 112 | } |
mbed_official | 155:8435094ec241 | 113 | if (obj->spi == SPI_3) { |
mbed_official | 155:8435094ec241 | 114 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); |
mbed_official | 155:8435094ec241 | 115 | } |
mbed_official | 155:8435094ec241 | 116 | |
mbed_official | 155:8435094ec241 | 117 | // Configure the SPI pins |
mbed_official | 155:8435094ec241 | 118 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 155:8435094ec241 | 119 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 155:8435094ec241 | 120 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 155:8435094ec241 | 121 | |
mbed_official | 155:8435094ec241 | 122 | // Save new values |
mbed_official | 155:8435094ec241 | 123 | obj->bits = SPI_DataSize_8b; |
mbed_official | 155:8435094ec241 | 124 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 155:8435094ec241 | 125 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 155:8435094ec241 | 126 | obj->br_presc = SPI_BaudRatePrescaler_256; |
mbed_official | 155:8435094ec241 | 127 | |
mbed_official | 155:8435094ec241 | 128 | if (ssel == NC) { // Master |
mbed_official | 155:8435094ec241 | 129 | obj->mode = SPI_Mode_Master; |
mbed_official | 155:8435094ec241 | 130 | obj->nss = SPI_NSS_Soft; |
mbed_official | 155:8435094ec241 | 131 | } else { // Slave |
mbed_official | 155:8435094ec241 | 132 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 155:8435094ec241 | 133 | obj->mode = SPI_Mode_Slave; |
mbed_official | 155:8435094ec241 | 134 | obj->nss = SPI_NSS_Soft; |
mbed_official | 155:8435094ec241 | 135 | } |
mbed_official | 155:8435094ec241 | 136 | |
mbed_official | 155:8435094ec241 | 137 | init_spi(obj); |
mbed_official | 155:8435094ec241 | 138 | } |
mbed_official | 155:8435094ec241 | 139 | |
mbed_official | 155:8435094ec241 | 140 | void spi_free(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 141 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 142 | SPI_I2S_DeInit(spi); |
mbed_official | 155:8435094ec241 | 143 | } |
mbed_official | 155:8435094ec241 | 144 | |
mbed_official | 155:8435094ec241 | 145 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 155:8435094ec241 | 146 | // Save new values |
mbed_official | 155:8435094ec241 | 147 | if (bits == 8) { |
mbed_official | 155:8435094ec241 | 148 | obj->bits = SPI_DataSize_8b; |
mbed_official | 155:8435094ec241 | 149 | } else { |
mbed_official | 155:8435094ec241 | 150 | obj->bits = SPI_DataSize_16b; |
mbed_official | 155:8435094ec241 | 151 | } |
mbed_official | 155:8435094ec241 | 152 | |
mbed_official | 155:8435094ec241 | 153 | switch (mode) { |
mbed_official | 155:8435094ec241 | 154 | case 0: |
mbed_official | 155:8435094ec241 | 155 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 155:8435094ec241 | 156 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 155:8435094ec241 | 157 | break; |
mbed_official | 155:8435094ec241 | 158 | case 1: |
mbed_official | 155:8435094ec241 | 159 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 155:8435094ec241 | 160 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 155:8435094ec241 | 161 | break; |
mbed_official | 155:8435094ec241 | 162 | case 2: |
mbed_official | 155:8435094ec241 | 163 | obj->cpol = SPI_CPOL_High; |
mbed_official | 155:8435094ec241 | 164 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 155:8435094ec241 | 165 | break; |
mbed_official | 155:8435094ec241 | 166 | default: |
mbed_official | 155:8435094ec241 | 167 | obj->cpol = SPI_CPOL_High; |
mbed_official | 155:8435094ec241 | 168 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 155:8435094ec241 | 169 | break; |
mbed_official | 155:8435094ec241 | 170 | } |
mbed_official | 155:8435094ec241 | 171 | |
mbed_official | 155:8435094ec241 | 172 | if (slave == 0) { |
mbed_official | 155:8435094ec241 | 173 | obj->mode = SPI_Mode_Master; |
mbed_official | 155:8435094ec241 | 174 | obj->nss = SPI_NSS_Soft; |
mbed_official | 155:8435094ec241 | 175 | } else { |
mbed_official | 155:8435094ec241 | 176 | obj->mode = SPI_Mode_Slave; |
mbed_official | 155:8435094ec241 | 177 | obj->nss = SPI_NSS_Hard; |
mbed_official | 155:8435094ec241 | 178 | } |
mbed_official | 155:8435094ec241 | 179 | |
mbed_official | 155:8435094ec241 | 180 | init_spi(obj); |
mbed_official | 155:8435094ec241 | 181 | } |
mbed_official | 155:8435094ec241 | 182 | |
mbed_official | 155:8435094ec241 | 183 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 155:8435094ec241 | 184 | // Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used |
mbed_official | 155:8435094ec241 | 185 | if (hz < 250000) { |
mbed_official | 155:8435094ec241 | 186 | obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz - 141 kHz |
mbed_official | 155:8435094ec241 | 187 | } else if ((hz >= 250000) && (hz < 500000)) { |
mbed_official | 155:8435094ec241 | 188 | obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz - 280 kHz |
mbed_official | 155:8435094ec241 | 189 | } else if ((hz >= 500000) && (hz < 1000000)) { |
mbed_official | 155:8435094ec241 | 190 | obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz - 560 kHz |
mbed_official | 155:8435094ec241 | 191 | } else if ((hz >= 1000000) && (hz < 2000000)) { |
mbed_official | 155:8435094ec241 | 192 | obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz - 1.13 MHz |
mbed_official | 155:8435094ec241 | 193 | } else if ((hz >= 2000000) && (hz < 4000000)) { |
mbed_official | 155:8435094ec241 | 194 | obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz - 2.25 MHz |
mbed_official | 155:8435094ec241 | 195 | } else if ((hz >= 4000000) && (hz < 8000000)) { |
mbed_official | 155:8435094ec241 | 196 | obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz - 4.5 MHz |
mbed_official | 155:8435094ec241 | 197 | } else if ((hz >= 8000000) && (hz < 16000000)) { |
mbed_official | 155:8435094ec241 | 198 | obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz - 9 MHz |
mbed_official | 155:8435094ec241 | 199 | } else { // >= 16000000 |
mbed_official | 155:8435094ec241 | 200 | obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz - 18 MHz |
mbed_official | 155:8435094ec241 | 201 | } |
mbed_official | 155:8435094ec241 | 202 | init_spi(obj); |
mbed_official | 155:8435094ec241 | 203 | } |
mbed_official | 155:8435094ec241 | 204 | |
mbed_official | 155:8435094ec241 | 205 | static inline int ssp_readable(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 206 | int status; |
mbed_official | 155:8435094ec241 | 207 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 208 | // Check if data is received |
mbed_official | 155:8435094ec241 | 209 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 155:8435094ec241 | 210 | return status; |
mbed_official | 155:8435094ec241 | 211 | } |
mbed_official | 155:8435094ec241 | 212 | |
mbed_official | 155:8435094ec241 | 213 | static inline int ssp_writeable(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 214 | int status; |
mbed_official | 155:8435094ec241 | 215 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 216 | // Check if data is transmitted |
mbed_official | 155:8435094ec241 | 217 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 155:8435094ec241 | 218 | return status; |
mbed_official | 155:8435094ec241 | 219 | } |
mbed_official | 155:8435094ec241 | 220 | |
mbed_official | 155:8435094ec241 | 221 | static inline void ssp_write(spi_t *obj, int value) { |
mbed_official | 155:8435094ec241 | 222 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 223 | while (!ssp_writeable(obj)); |
mbed_official | 155:8435094ec241 | 224 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 155:8435094ec241 | 225 | SPI_SendData8(spi, (uint8_t)value); |
mbed_official | 155:8435094ec241 | 226 | } else { |
mbed_official | 155:8435094ec241 | 227 | SPI_I2S_SendData16(spi, (uint16_t)value); |
mbed_official | 155:8435094ec241 | 228 | } |
mbed_official | 155:8435094ec241 | 229 | } |
mbed_official | 155:8435094ec241 | 230 | |
mbed_official | 155:8435094ec241 | 231 | static inline int ssp_read(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 232 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 233 | while (!ssp_readable(obj)); |
mbed_official | 155:8435094ec241 | 234 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 155:8435094ec241 | 235 | return (int)SPI_ReceiveData8(spi); |
mbed_official | 155:8435094ec241 | 236 | } else { |
mbed_official | 155:8435094ec241 | 237 | return (int)SPI_I2S_ReceiveData16(spi); |
mbed_official | 155:8435094ec241 | 238 | } |
mbed_official | 155:8435094ec241 | 239 | } |
mbed_official | 155:8435094ec241 | 240 | |
mbed_official | 155:8435094ec241 | 241 | static inline int ssp_busy(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 242 | int status; |
mbed_official | 155:8435094ec241 | 243 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 244 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 155:8435094ec241 | 245 | return status; |
mbed_official | 155:8435094ec241 | 246 | } |
mbed_official | 155:8435094ec241 | 247 | |
mbed_official | 155:8435094ec241 | 248 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 155:8435094ec241 | 249 | ssp_write(obj, value); |
mbed_official | 155:8435094ec241 | 250 | return ssp_read(obj); |
mbed_official | 155:8435094ec241 | 251 | } |
mbed_official | 155:8435094ec241 | 252 | |
mbed_official | 155:8435094ec241 | 253 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 254 | return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); |
mbed_official | 155:8435094ec241 | 255 | }; |
mbed_official | 155:8435094ec241 | 256 | |
mbed_official | 155:8435094ec241 | 257 | int spi_slave_read(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 258 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 259 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 155:8435094ec241 | 260 | return (int)SPI_ReceiveData8(spi); |
mbed_official | 155:8435094ec241 | 261 | } else { |
mbed_official | 155:8435094ec241 | 262 | return (int)SPI_I2S_ReceiveData16(spi); |
mbed_official | 155:8435094ec241 | 263 | } |
mbed_official | 155:8435094ec241 | 264 | } |
mbed_official | 155:8435094ec241 | 265 | |
mbed_official | 155:8435094ec241 | 266 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 155:8435094ec241 | 267 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 155:8435094ec241 | 268 | while (!ssp_writeable(obj)); |
mbed_official | 155:8435094ec241 | 269 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 155:8435094ec241 | 270 | SPI_SendData8(spi, (uint8_t)value); |
mbed_official | 155:8435094ec241 | 271 | } else { |
mbed_official | 155:8435094ec241 | 272 | SPI_I2S_SendData16(spi, (uint16_t)value); |
mbed_official | 155:8435094ec241 | 273 | } |
mbed_official | 155:8435094ec241 | 274 | } |
mbed_official | 155:8435094ec241 | 275 | |
mbed_official | 155:8435094ec241 | 276 | int spi_busy(spi_t *obj) { |
mbed_official | 155:8435094ec241 | 277 | return ssp_busy(obj); |
mbed_official | 155:8435094ec241 | 278 | } |
mbed_official | 155:8435094ec241 | 279 | |
mbed_official | 155:8435094ec241 | 280 | #endif |