mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/spi_api.c@166:cb4253f91ada, 2014-04-22 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Apr 22 16:00:06 2014 +0100
- Revision:
- 166:cb4253f91ada
- Parent:
- 116:471443864d4b
- Child:
- 171:3d240fda1f07
Synchronized with git revision a519f94f35c3993310499623be5c28f74b80485c
Full URL: https://github.com/mbedmicro/mbed/commit/a519f94f35c3993310499623be5c28f74b80485c/
[NUCLEO_F030R8] Many improvements added
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 | 84:f54042cbc282 | 30 | #include "spi_api.h" |
mbed_official | 84:f54042cbc282 | 31 | |
mbed_official | 84:f54042cbc282 | 32 | #if DEVICE_SPI |
mbed_official | 84:f54042cbc282 | 33 | |
mbed_official | 84:f54042cbc282 | 34 | #include <math.h> |
mbed_official | 84:f54042cbc282 | 35 | #include "cmsis.h" |
mbed_official | 84:f54042cbc282 | 36 | #include "pinmap.h" |
mbed_official | 84:f54042cbc282 | 37 | #include "error.h" |
mbed_official | 84:f54042cbc282 | 38 | |
mbed_official | 84:f54042cbc282 | 39 | static const PinMap PinMap_SPI_MOSI[] = { |
mbed_official | 84:f54042cbc282 | 40 | {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, |
mbed_official | 84:f54042cbc282 | 41 | {NC, NC, 0} |
mbed_official | 84:f54042cbc282 | 42 | }; |
mbed_official | 84:f54042cbc282 | 43 | |
mbed_official | 84:f54042cbc282 | 44 | static const PinMap PinMap_SPI_MISO[] = { |
mbed_official | 84:f54042cbc282 | 45 | {PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, |
mbed_official | 84:f54042cbc282 | 46 | {NC, NC, 0} |
mbed_official | 84:f54042cbc282 | 47 | }; |
mbed_official | 84:f54042cbc282 | 48 | |
mbed_official | 84:f54042cbc282 | 49 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 84:f54042cbc282 | 50 | {PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, |
mbed_official | 84:f54042cbc282 | 51 | {NC, NC, 0} |
mbed_official | 84:f54042cbc282 | 52 | }; |
mbed_official | 84:f54042cbc282 | 53 | |
mbed_official | 84:f54042cbc282 | 54 | // Only used in Slave mode |
mbed_official | 84:f54042cbc282 | 55 | static const PinMap PinMap_SPI_SSEL[] = { |
mbed_official | 84:f54042cbc282 | 56 | {PB_6, SPI_1, STM_PIN_DATA(GPIO_Mode_IN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0)}, // Generic IO, not real H/W NSS pin |
mbed_official | 84:f54042cbc282 | 57 | {NC, NC, 0} |
mbed_official | 84:f54042cbc282 | 58 | }; |
mbed_official | 84:f54042cbc282 | 59 | |
mbed_official | 84:f54042cbc282 | 60 | static void init_spi(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 61 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 84:f54042cbc282 | 62 | SPI_InitTypeDef SPI_InitStructure; |
mbed_official | 84:f54042cbc282 | 63 | |
mbed_official | 84:f54042cbc282 | 64 | SPI_Cmd(spi, DISABLE); |
mbed_official | 84:f54042cbc282 | 65 | |
mbed_official | 84:f54042cbc282 | 66 | SPI_InitStructure.SPI_Mode = obj->mode; |
mbed_official | 84:f54042cbc282 | 67 | SPI_InitStructure.SPI_NSS = obj->nss; |
mbed_official | 84:f54042cbc282 | 68 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; |
mbed_official | 84:f54042cbc282 | 69 | SPI_InitStructure.SPI_DataSize = obj->bits; |
mbed_official | 84:f54042cbc282 | 70 | SPI_InitStructure.SPI_CPOL = obj->cpol; |
mbed_official | 84:f54042cbc282 | 71 | SPI_InitStructure.SPI_CPHA = obj->cpha; |
mbed_official | 84:f54042cbc282 | 72 | SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; |
mbed_official | 84:f54042cbc282 | 73 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; |
mbed_official | 84:f54042cbc282 | 74 | SPI_InitStructure.SPI_CRCPolynomial = 7; |
mbed_official | 84:f54042cbc282 | 75 | SPI_Init(spi, &SPI_InitStructure); |
mbed_official | 84:f54042cbc282 | 76 | |
mbed_official | 84:f54042cbc282 | 77 | SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF); |
mbed_official | 84:f54042cbc282 | 78 | |
mbed_official | 84:f54042cbc282 | 79 | SPI_Cmd(spi, ENABLE); |
mbed_official | 84:f54042cbc282 | 80 | } |
mbed_official | 84:f54042cbc282 | 81 | |
mbed_official | 84:f54042cbc282 | 82 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 84:f54042cbc282 | 83 | // Determine the SPI to use |
mbed_official | 84:f54042cbc282 | 84 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 84:f54042cbc282 | 85 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 84:f54042cbc282 | 86 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 84:f54042cbc282 | 87 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 84:f54042cbc282 | 88 | |
mbed_official | 84:f54042cbc282 | 89 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 84:f54042cbc282 | 90 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 84:f54042cbc282 | 91 | |
mbed_official | 84:f54042cbc282 | 92 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 84:f54042cbc282 | 93 | |
mbed_official | 84:f54042cbc282 | 94 | if (obj->spi == (SPIName)NC) { |
mbed_official | 84:f54042cbc282 | 95 | error("SPI pinout mapping failed"); |
mbed_official | 84:f54042cbc282 | 96 | } |
mbed_official | 84:f54042cbc282 | 97 | |
mbed_official | 84:f54042cbc282 | 98 | // Enable SPI clock |
mbed_official | 84:f54042cbc282 | 99 | if (obj->spi == SPI_1) { |
mbed_official | 84:f54042cbc282 | 100 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); |
mbed_official | 84:f54042cbc282 | 101 | } |
mbed_official | 84:f54042cbc282 | 102 | if (obj->spi == SPI_2) { |
mbed_official | 84:f54042cbc282 | 103 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 84:f54042cbc282 | 104 | } |
mbed_official | 84:f54042cbc282 | 105 | |
mbed_official | 84:f54042cbc282 | 106 | // Configure the SPI pins |
mbed_official | 84:f54042cbc282 | 107 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 84:f54042cbc282 | 108 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 84:f54042cbc282 | 109 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 84:f54042cbc282 | 110 | |
mbed_official | 84:f54042cbc282 | 111 | // Save new values |
mbed_official | 84:f54042cbc282 | 112 | obj->bits = SPI_DataSize_8b; |
mbed_official | 84:f54042cbc282 | 113 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 84:f54042cbc282 | 114 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 84:f54042cbc282 | 115 | obj->br_presc = SPI_BaudRatePrescaler_8; // 1 MHz |
mbed_official | 84:f54042cbc282 | 116 | |
mbed_official | 84:f54042cbc282 | 117 | if (ssel == NC) { // Master |
mbed_official | 84:f54042cbc282 | 118 | obj->mode = SPI_Mode_Master; |
mbed_official | 84:f54042cbc282 | 119 | obj->nss = SPI_NSS_Soft; |
mbed_official | 84:f54042cbc282 | 120 | } |
mbed_official | 84:f54042cbc282 | 121 | else { // Slave |
mbed_official | 84:f54042cbc282 | 122 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 84:f54042cbc282 | 123 | obj->mode = SPI_Mode_Slave; |
mbed_official | 84:f54042cbc282 | 124 | obj->nss = SPI_NSS_Soft; |
mbed_official | 84:f54042cbc282 | 125 | } |
mbed_official | 84:f54042cbc282 | 126 | |
mbed_official | 84:f54042cbc282 | 127 | init_spi(obj); |
mbed_official | 84:f54042cbc282 | 128 | } |
mbed_official | 84:f54042cbc282 | 129 | |
mbed_official | 84:f54042cbc282 | 130 | void spi_free(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 131 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 84:f54042cbc282 | 132 | SPI_I2S_DeInit(spi); |
mbed_official | 84:f54042cbc282 | 133 | } |
mbed_official | 84:f54042cbc282 | 134 | |
mbed_official | 84:f54042cbc282 | 135 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 84:f54042cbc282 | 136 | // Save new values |
mbed_official | 84:f54042cbc282 | 137 | if (bits == 8) { |
mbed_official | 84:f54042cbc282 | 138 | obj->bits = SPI_DataSize_8b; |
mbed_official | 84:f54042cbc282 | 139 | } |
mbed_official | 84:f54042cbc282 | 140 | else { |
mbed_official | 84:f54042cbc282 | 141 | obj->bits = SPI_DataSize_16b; |
mbed_official | 84:f54042cbc282 | 142 | } |
mbed_official | 84:f54042cbc282 | 143 | |
mbed_official | 84:f54042cbc282 | 144 | switch (mode) { |
mbed_official | 84:f54042cbc282 | 145 | case 0: |
mbed_official | 84:f54042cbc282 | 146 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 84:f54042cbc282 | 147 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 84:f54042cbc282 | 148 | break; |
mbed_official | 84:f54042cbc282 | 149 | case 1: |
mbed_official | 84:f54042cbc282 | 150 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 84:f54042cbc282 | 151 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 84:f54042cbc282 | 152 | break; |
mbed_official | 84:f54042cbc282 | 153 | case 2: |
mbed_official | 84:f54042cbc282 | 154 | obj->cpol = SPI_CPOL_High; |
mbed_official | 84:f54042cbc282 | 155 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 84:f54042cbc282 | 156 | break; |
mbed_official | 84:f54042cbc282 | 157 | default: |
mbed_official | 84:f54042cbc282 | 158 | obj->cpol = SPI_CPOL_High; |
mbed_official | 84:f54042cbc282 | 159 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 84:f54042cbc282 | 160 | break; |
mbed_official | 84:f54042cbc282 | 161 | } |
mbed_official | 84:f54042cbc282 | 162 | |
mbed_official | 84:f54042cbc282 | 163 | if (slave == 0) { |
mbed_official | 84:f54042cbc282 | 164 | obj->mode = SPI_Mode_Master; |
mbed_official | 84:f54042cbc282 | 165 | obj->nss = SPI_NSS_Soft; |
mbed_official | 84:f54042cbc282 | 166 | } |
mbed_official | 84:f54042cbc282 | 167 | else { |
mbed_official | 84:f54042cbc282 | 168 | obj->mode = SPI_Mode_Slave; |
mbed_official | 84:f54042cbc282 | 169 | obj->nss = SPI_NSS_Hard; |
mbed_official | 84:f54042cbc282 | 170 | } |
mbed_official | 84:f54042cbc282 | 171 | |
mbed_official | 84:f54042cbc282 | 172 | init_spi(obj); |
mbed_official | 84:f54042cbc282 | 173 | } |
mbed_official | 84:f54042cbc282 | 174 | |
mbed_official | 84:f54042cbc282 | 175 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 166:cb4253f91ada | 176 | // Note: The frequencies are obtained with SPI clock = 48 MHz (APB1 & APB2 clocks) |
mbed_official | 166:cb4253f91ada | 177 | if (hz < 300000) { |
mbed_official | 166:cb4253f91ada | 178 | obj->br_presc = SPI_BaudRatePrescaler_256; // 188 kHz |
mbed_official | 166:cb4253f91ada | 179 | } |
mbed_official | 166:cb4253f91ada | 180 | else if ((hz >= 300000) && (hz < 700000)) { |
mbed_official | 166:cb4253f91ada | 181 | obj->br_presc = SPI_BaudRatePrescaler_128; // 375 kHz |
mbed_official | 166:cb4253f91ada | 182 | } |
mbed_official | 166:cb4253f91ada | 183 | else if ((hz >= 700000) && (hz < 1000000)) { |
mbed_official | 166:cb4253f91ada | 184 | obj->br_presc = SPI_BaudRatePrescaler_64; // 750 kHz |
mbed_official | 166:cb4253f91ada | 185 | } |
mbed_official | 166:cb4253f91ada | 186 | else if ((hz >= 1000000) && (hz < 3000000)) { |
mbed_official | 166:cb4253f91ada | 187 | obj->br_presc = SPI_BaudRatePrescaler_32; // 1.5 MHz |
mbed_official | 166:cb4253f91ada | 188 | } |
mbed_official | 166:cb4253f91ada | 189 | else if ((hz >= 3000000) && (hz < 6000000)) { |
mbed_official | 166:cb4253f91ada | 190 | obj->br_presc = SPI_BaudRatePrescaler_16; // 3 MHz |
mbed_official | 166:cb4253f91ada | 191 | } |
mbed_official | 166:cb4253f91ada | 192 | else if ((hz >= 6000000) && (hz < 12000000)) { |
mbed_official | 166:cb4253f91ada | 193 | obj->br_presc = SPI_BaudRatePrescaler_8; // 6 MHz |
mbed_official | 166:cb4253f91ada | 194 | } |
mbed_official | 166:cb4253f91ada | 195 | else if ((hz >= 12000000) && (hz < 24000000)) { |
mbed_official | 166:cb4253f91ada | 196 | obj->br_presc = SPI_BaudRatePrescaler_4; // 12 MHz |
mbed_official | 166:cb4253f91ada | 197 | } |
mbed_official | 166:cb4253f91ada | 198 | else { // >= 24000000 |
mbed_official | 166:cb4253f91ada | 199 | obj->br_presc = SPI_BaudRatePrescaler_2; // 24 MHz |
mbed_official | 166:cb4253f91ada | 200 | } |
mbed_official | 84:f54042cbc282 | 201 | init_spi(obj); |
mbed_official | 84:f54042cbc282 | 202 | } |
mbed_official | 84:f54042cbc282 | 203 | |
mbed_official | 84:f54042cbc282 | 204 | static inline int ssp_readable(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 205 | int status; |
mbed_official | 84:f54042cbc282 | 206 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 84:f54042cbc282 | 207 | // Check if data is received |
mbed_official | 84:f54042cbc282 | 208 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 84:f54042cbc282 | 209 | return status; |
mbed_official | 84:f54042cbc282 | 210 | } |
mbed_official | 84:f54042cbc282 | 211 | |
mbed_official | 84:f54042cbc282 | 212 | static inline int ssp_writeable(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 213 | int status; |
mbed_official | 84:f54042cbc282 | 214 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 84:f54042cbc282 | 215 | // Check if data is transmitted |
mbed_official | 84:f54042cbc282 | 216 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 84:f54042cbc282 | 217 | return status; |
mbed_official | 84:f54042cbc282 | 218 | } |
mbed_official | 84:f54042cbc282 | 219 | |
mbed_official | 84:f54042cbc282 | 220 | static inline void ssp_write(spi_t *obj, int value) { |
mbed_official | 84:f54042cbc282 | 221 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 84:f54042cbc282 | 222 | while (!ssp_writeable(obj)); |
mbed_official | 166:cb4253f91ada | 223 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 166:cb4253f91ada | 224 | SPI_SendData8(spi, (uint8_t)value); |
mbed_official | 166:cb4253f91ada | 225 | } |
mbed_official | 166:cb4253f91ada | 226 | else { // 16-bit |
mbed_official | 166:cb4253f91ada | 227 | SPI_I2S_SendData16(spi, (uint16_t)value); |
mbed_official | 166:cb4253f91ada | 228 | } |
mbed_official | 84:f54042cbc282 | 229 | } |
mbed_official | 84:f54042cbc282 | 230 | |
mbed_official | 84:f54042cbc282 | 231 | static inline int ssp_read(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 232 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 84:f54042cbc282 | 233 | while (!ssp_readable(obj)); |
mbed_official | 166:cb4253f91ada | 234 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 166:cb4253f91ada | 235 | return (int)SPI_ReceiveData8(spi); |
mbed_official | 166:cb4253f91ada | 236 | } |
mbed_official | 166:cb4253f91ada | 237 | else { // 16-bit |
mbed_official | 166:cb4253f91ada | 238 | return (int)SPI_I2S_ReceiveData16(spi); |
mbed_official | 166:cb4253f91ada | 239 | } |
mbed_official | 84:f54042cbc282 | 240 | } |
mbed_official | 84:f54042cbc282 | 241 | |
mbed_official | 84:f54042cbc282 | 242 | static inline int ssp_busy(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 243 | int status; |
mbed_official | 84:f54042cbc282 | 244 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 84:f54042cbc282 | 245 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 84:f54042cbc282 | 246 | return status; |
mbed_official | 84:f54042cbc282 | 247 | } |
mbed_official | 84:f54042cbc282 | 248 | |
mbed_official | 84:f54042cbc282 | 249 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 84:f54042cbc282 | 250 | ssp_write(obj, value); |
mbed_official | 84:f54042cbc282 | 251 | return ssp_read(obj); |
mbed_official | 84:f54042cbc282 | 252 | } |
mbed_official | 84:f54042cbc282 | 253 | |
mbed_official | 84:f54042cbc282 | 254 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 255 | return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); |
mbed_official | 84:f54042cbc282 | 256 | }; |
mbed_official | 84:f54042cbc282 | 257 | |
mbed_official | 84:f54042cbc282 | 258 | int spi_slave_read(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 259 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 166:cb4253f91ada | 260 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 166:cb4253f91ada | 261 | return (int)SPI_ReceiveData8(spi); |
mbed_official | 166:cb4253f91ada | 262 | } |
mbed_official | 166:cb4253f91ada | 263 | else { // 16-bit |
mbed_official | 166:cb4253f91ada | 264 | return (int)SPI_I2S_ReceiveData16(spi); |
mbed_official | 166:cb4253f91ada | 265 | } |
mbed_official | 84:f54042cbc282 | 266 | } |
mbed_official | 84:f54042cbc282 | 267 | |
mbed_official | 84:f54042cbc282 | 268 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 84:f54042cbc282 | 269 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 166:cb4253f91ada | 270 | while (!ssp_writeable(obj)); |
mbed_official | 166:cb4253f91ada | 271 | if (obj->bits == SPI_DataSize_8b) { |
mbed_official | 166:cb4253f91ada | 272 | SPI_SendData8(spi, (uint8_t)value); |
mbed_official | 166:cb4253f91ada | 273 | } |
mbed_official | 166:cb4253f91ada | 274 | else { // 16-bit |
mbed_official | 166:cb4253f91ada | 275 | SPI_I2S_SendData16(spi, (uint16_t)value); |
mbed_official | 166:cb4253f91ada | 276 | } |
mbed_official | 84:f54042cbc282 | 277 | } |
mbed_official | 84:f54042cbc282 | 278 | |
mbed_official | 84:f54042cbc282 | 279 | int spi_busy(spi_t *obj) { |
mbed_official | 84:f54042cbc282 | 280 | return ssp_busy(obj); |
mbed_official | 84:f54042cbc282 | 281 | } |
mbed_official | 84:f54042cbc282 | 282 | |
mbed_official | 84:f54042cbc282 | 283 | #endif |