mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Jun 17 11:30:08 2014 +0100
Revision:
233:1bbc1451db33
Parent:
227:7bd0639b8911
Synchronized with git revision f60bb902c9fd07cbc8e2726c4264aa4f710e2541

Full URL: https://github.com/mbedmicro/mbed/commit/f60bb902c9fd07cbc8e2726c4264aa4f710e2541/

kds exporter

Who changed what in which revision?

UserRevisionLine numberNew 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 84:f54042cbc282 40 {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 171:3d240fda1f07 41 {PB_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 171:3d240fda1f07 42 {PB_15, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
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 84:f54042cbc282 47 {PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 171:3d240fda1f07 48 {PB_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 171:3d240fda1f07 49 {PB_14, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
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 84:f54042cbc282 54 {PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 171:3d240fda1f07 55 {PB_3, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 171:3d240fda1f07 56 {PB_13, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 84:f54042cbc282 57 {NC, NC, 0}
mbed_official 84:f54042cbc282 58 };
mbed_official 84:f54042cbc282 59
mbed_official 84:f54042cbc282 60 // Only used in Slave mode
mbed_official 84:f54042cbc282 61 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 233:1bbc1451db33 62 {PA_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0)},
mbed_official 233:1bbc1451db33 63 {PA_15, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0)},
mbed_official 233:1bbc1451db33 64 {PB_12, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0)},
mbed_official 84:f54042cbc282 65 {NC, NC, 0}
mbed_official 84:f54042cbc282 66 };
mbed_official 84:f54042cbc282 67
mbed_official 84:f54042cbc282 68 static void init_spi(spi_t *obj) {
mbed_official 84:f54042cbc282 69 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 70 SPI_InitTypeDef SPI_InitStructure;
mbed_official 84:f54042cbc282 71
mbed_official 84:f54042cbc282 72 SPI_Cmd(spi, DISABLE);
mbed_official 84:f54042cbc282 73
mbed_official 84:f54042cbc282 74 SPI_InitStructure.SPI_Mode = obj->mode;
mbed_official 216:577900467c9e 75 SPI_InitStructure.SPI_NSS = obj->nss;
mbed_official 216:577900467c9e 76 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
mbed_official 84:f54042cbc282 77 SPI_InitStructure.SPI_DataSize = obj->bits;
mbed_official 84:f54042cbc282 78 SPI_InitStructure.SPI_CPOL = obj->cpol;
mbed_official 216:577900467c9e 79 SPI_InitStructure.SPI_CPHA = obj->cpha;
mbed_official 84:f54042cbc282 80 SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
mbed_official 84:f54042cbc282 81 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
mbed_official 84:f54042cbc282 82 SPI_InitStructure.SPI_CRCPolynomial = 7;
mbed_official 84:f54042cbc282 83 SPI_Init(spi, &SPI_InitStructure);
mbed_official 84:f54042cbc282 84
mbed_official 84:f54042cbc282 85 SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF);
mbed_official 84:f54042cbc282 86
mbed_official 84:f54042cbc282 87 SPI_Cmd(spi, ENABLE);
mbed_official 84:f54042cbc282 88 }
mbed_official 84:f54042cbc282 89
mbed_official 84:f54042cbc282 90 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
mbed_official 84:f54042cbc282 91 // Determine the SPI to use
mbed_official 84:f54042cbc282 92 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 84:f54042cbc282 93 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 84:f54042cbc282 94 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 84:f54042cbc282 95 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 216:577900467c9e 96
mbed_official 84:f54042cbc282 97 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 84:f54042cbc282 98 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 216:577900467c9e 99
mbed_official 84:f54042cbc282 100 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 227:7bd0639b8911 101 MBED_ASSERT(obj->spi != (SPIName)NC);
mbed_official 216:577900467c9e 102
mbed_official 84:f54042cbc282 103 // Enable SPI clock
mbed_official 84:f54042cbc282 104 if (obj->spi == SPI_1) {
mbed_official 216:577900467c9e 105 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
mbed_official 84:f54042cbc282 106 }
mbed_official 84:f54042cbc282 107 if (obj->spi == SPI_2) {
mbed_official 216:577900467c9e 108 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
mbed_official 84:f54042cbc282 109 }
mbed_official 216:577900467c9e 110
mbed_official 84:f54042cbc282 111 // Configure the SPI pins
mbed_official 84:f54042cbc282 112 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 84:f54042cbc282 113 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 84:f54042cbc282 114 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 216:577900467c9e 115
mbed_official 84:f54042cbc282 116 // Save new values
mbed_official 84:f54042cbc282 117 obj->bits = SPI_DataSize_8b;
mbed_official 84:f54042cbc282 118 obj->cpol = SPI_CPOL_Low;
mbed_official 84:f54042cbc282 119 obj->cpha = SPI_CPHA_1Edge;
mbed_official 84:f54042cbc282 120 obj->br_presc = SPI_BaudRatePrescaler_8; // 1 MHz
mbed_official 216:577900467c9e 121
mbed_official 216:577900467c9e 122 obj->pin_miso = miso;
mbed_official 216:577900467c9e 123 obj->pin_mosi = mosi;
mbed_official 216:577900467c9e 124 obj->pin_sclk = sclk;
mbed_official 216:577900467c9e 125 obj->pin_ssel = ssel;
mbed_official 216:577900467c9e 126
mbed_official 84:f54042cbc282 127 if (ssel == NC) { // Master
mbed_official 84:f54042cbc282 128 obj->mode = SPI_Mode_Master;
mbed_official 84:f54042cbc282 129 obj->nss = SPI_NSS_Soft;
mbed_official 216:577900467c9e 130 } else { // Slave
mbed_official 84:f54042cbc282 131 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 84:f54042cbc282 132 obj->mode = SPI_Mode_Slave;
mbed_official 171:3d240fda1f07 133 obj->nss = SPI_NSS_Hard;
mbed_official 84:f54042cbc282 134 }
mbed_official 84:f54042cbc282 135
mbed_official 84:f54042cbc282 136 init_spi(obj);
mbed_official 84:f54042cbc282 137 }
mbed_official 84:f54042cbc282 138
mbed_official 84:f54042cbc282 139 void spi_free(spi_t *obj) {
mbed_official 216:577900467c9e 140 // Reset SPI and disable clock
mbed_official 216:577900467c9e 141 if (obj->spi == SPI_1) {
mbed_official 216:577900467c9e 142 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE);
mbed_official 216:577900467c9e 143 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
mbed_official 216:577900467c9e 144 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE);
mbed_official 216:577900467c9e 145 }
mbed_official 216:577900467c9e 146
mbed_official 216:577900467c9e 147 if (obj->spi == SPI_2) {
mbed_official 216:577900467c9e 148 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
mbed_official 216:577900467c9e 149 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE);
mbed_official 216:577900467c9e 150 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, DISABLE);
mbed_official 216:577900467c9e 151 }
mbed_official 216:577900467c9e 152
mbed_official 216:577900467c9e 153 // Configure GPIOs
mbed_official 216:577900467c9e 154 pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
mbed_official 216:577900467c9e 155 pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
mbed_official 216:577900467c9e 156 pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
mbed_official 216:577900467c9e 157 pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
mbed_official 84:f54042cbc282 158 }
mbed_official 84:f54042cbc282 159
mbed_official 216:577900467c9e 160 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 84:f54042cbc282 161 // Save new values
mbed_official 233:1bbc1451db33 162 if (bits == 16) {
mbed_official 233:1bbc1451db33 163 obj->bits = SPI_DataSize_16b;
mbed_official 233:1bbc1451db33 164 } else {
mbed_official 84:f54042cbc282 165 obj->bits = SPI_DataSize_8b;
mbed_official 84:f54042cbc282 166 }
mbed_official 216:577900467c9e 167
mbed_official 84:f54042cbc282 168 switch (mode) {
mbed_official 84:f54042cbc282 169 case 0:
mbed_official 216:577900467c9e 170 obj->cpol = SPI_CPOL_Low;
mbed_official 216:577900467c9e 171 obj->cpha = SPI_CPHA_1Edge;
mbed_official 216:577900467c9e 172 break;
mbed_official 84:f54042cbc282 173 case 1:
mbed_official 216:577900467c9e 174 obj->cpol = SPI_CPOL_Low;
mbed_official 216:577900467c9e 175 obj->cpha = SPI_CPHA_2Edge;
mbed_official 216:577900467c9e 176 break;
mbed_official 84:f54042cbc282 177 case 2:
mbed_official 216:577900467c9e 178 obj->cpol = SPI_CPOL_High;
mbed_official 216:577900467c9e 179 obj->cpha = SPI_CPHA_1Edge;
mbed_official 216:577900467c9e 180 break;
mbed_official 84:f54042cbc282 181 default:
mbed_official 216:577900467c9e 182 obj->cpol = SPI_CPOL_High;
mbed_official 216:577900467c9e 183 obj->cpha = SPI_CPHA_2Edge;
mbed_official 216:577900467c9e 184 break;
mbed_official 84:f54042cbc282 185 }
mbed_official 216:577900467c9e 186
mbed_official 84:f54042cbc282 187 if (slave == 0) {
mbed_official 84:f54042cbc282 188 obj->mode = SPI_Mode_Master;
mbed_official 84:f54042cbc282 189 obj->nss = SPI_NSS_Soft;
mbed_official 216:577900467c9e 190 } else {
mbed_official 84:f54042cbc282 191 obj->mode = SPI_Mode_Slave;
mbed_official 216:577900467c9e 192 obj->nss = SPI_NSS_Hard;
mbed_official 84:f54042cbc282 193 }
mbed_official 216:577900467c9e 194
mbed_official 84:f54042cbc282 195 init_spi(obj);
mbed_official 84:f54042cbc282 196 }
mbed_official 84:f54042cbc282 197
mbed_official 84:f54042cbc282 198 void spi_frequency(spi_t *obj, int hz) {
mbed_official 166:cb4253f91ada 199 // Note: The frequencies are obtained with SPI clock = 48 MHz (APB1 & APB2 clocks)
mbed_official 166:cb4253f91ada 200 if (hz < 300000) {
mbed_official 166:cb4253f91ada 201 obj->br_presc = SPI_BaudRatePrescaler_256; // 188 kHz
mbed_official 216:577900467c9e 202 } else if ((hz >= 300000) && (hz < 700000)) {
mbed_official 166:cb4253f91ada 203 obj->br_presc = SPI_BaudRatePrescaler_128; // 375 kHz
mbed_official 216:577900467c9e 204 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 166:cb4253f91ada 205 obj->br_presc = SPI_BaudRatePrescaler_64; // 750 kHz
mbed_official 216:577900467c9e 206 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 166:cb4253f91ada 207 obj->br_presc = SPI_BaudRatePrescaler_32; // 1.5 MHz
mbed_official 216:577900467c9e 208 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 166:cb4253f91ada 209 obj->br_presc = SPI_BaudRatePrescaler_16; // 3 MHz
mbed_official 216:577900467c9e 210 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 166:cb4253f91ada 211 obj->br_presc = SPI_BaudRatePrescaler_8; // 6 MHz
mbed_official 216:577900467c9e 212 } else if ((hz >= 12000000) && (hz < 24000000)) {
mbed_official 166:cb4253f91ada 213 obj->br_presc = SPI_BaudRatePrescaler_4; // 12 MHz
mbed_official 216:577900467c9e 214 } else { // >= 24000000
mbed_official 166:cb4253f91ada 215 obj->br_presc = SPI_BaudRatePrescaler_2; // 24 MHz
mbed_official 166:cb4253f91ada 216 }
mbed_official 84:f54042cbc282 217 init_spi(obj);
mbed_official 84:f54042cbc282 218 }
mbed_official 84:f54042cbc282 219
mbed_official 84:f54042cbc282 220 static inline int ssp_readable(spi_t *obj) {
mbed_official 84:f54042cbc282 221 int status;
mbed_official 84:f54042cbc282 222 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 223 // Check if data is received
mbed_official 84:f54042cbc282 224 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 216:577900467c9e 225 return status;
mbed_official 84:f54042cbc282 226 }
mbed_official 84:f54042cbc282 227
mbed_official 84:f54042cbc282 228 static inline int ssp_writeable(spi_t *obj) {
mbed_official 84:f54042cbc282 229 int status;
mbed_official 84:f54042cbc282 230 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 231 // Check if data is transmitted
mbed_official 84:f54042cbc282 232 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 84:f54042cbc282 233 return status;
mbed_official 84:f54042cbc282 234 }
mbed_official 84:f54042cbc282 235
mbed_official 84:f54042cbc282 236 static inline void ssp_write(spi_t *obj, int value) {
mbed_official 216:577900467c9e 237 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 238 while (!ssp_writeable(obj));
mbed_official 166:cb4253f91ada 239 if (obj->bits == SPI_DataSize_8b) {
mbed_official 166:cb4253f91ada 240 SPI_SendData8(spi, (uint8_t)value);
mbed_official 216:577900467c9e 241 } else { // 16-bit
mbed_official 166:cb4253f91ada 242 SPI_I2S_SendData16(spi, (uint16_t)value);
mbed_official 166:cb4253f91ada 243 }
mbed_official 84:f54042cbc282 244 }
mbed_official 84:f54042cbc282 245
mbed_official 84:f54042cbc282 246 static inline int ssp_read(spi_t *obj) {
mbed_official 216:577900467c9e 247 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 248 while (!ssp_readable(obj));
mbed_official 166:cb4253f91ada 249 if (obj->bits == SPI_DataSize_8b) {
mbed_official 166:cb4253f91ada 250 return (int)SPI_ReceiveData8(spi);
mbed_official 216:577900467c9e 251 } else { // 16-bit
mbed_official 166:cb4253f91ada 252 return (int)SPI_I2S_ReceiveData16(spi);
mbed_official 166:cb4253f91ada 253 }
mbed_official 84:f54042cbc282 254 }
mbed_official 84:f54042cbc282 255
mbed_official 84:f54042cbc282 256 static inline int ssp_busy(spi_t *obj) {
mbed_official 84:f54042cbc282 257 int status;
mbed_official 84:f54042cbc282 258 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 259 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 84:f54042cbc282 260 return status;
mbed_official 84:f54042cbc282 261 }
mbed_official 84:f54042cbc282 262
mbed_official 84:f54042cbc282 263 int spi_master_write(spi_t *obj, int value) {
mbed_official 84:f54042cbc282 264 ssp_write(obj, value);
mbed_official 84:f54042cbc282 265 return ssp_read(obj);
mbed_official 84:f54042cbc282 266 }
mbed_official 84:f54042cbc282 267
mbed_official 84:f54042cbc282 268 int spi_slave_receive(spi_t *obj) {
mbed_official 233:1bbc1451db33 269 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 84:f54042cbc282 270 };
mbed_official 84:f54042cbc282 271
mbed_official 84:f54042cbc282 272 int spi_slave_read(spi_t *obj) {
mbed_official 84:f54042cbc282 273 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 166:cb4253f91ada 274 if (obj->bits == SPI_DataSize_8b) {
mbed_official 166:cb4253f91ada 275 return (int)SPI_ReceiveData8(spi);
mbed_official 216:577900467c9e 276 } else { // 16-bit
mbed_official 166:cb4253f91ada 277 return (int)SPI_I2S_ReceiveData16(spi);
mbed_official 166:cb4253f91ada 278 }
mbed_official 84:f54042cbc282 279 }
mbed_official 84:f54042cbc282 280
mbed_official 84:f54042cbc282 281 void spi_slave_write(spi_t *obj, int value) {
mbed_official 216:577900467c9e 282 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 216:577900467c9e 283 while (!ssp_writeable(obj));
mbed_official 166:cb4253f91ada 284 if (obj->bits == SPI_DataSize_8b) {
mbed_official 166:cb4253f91ada 285 SPI_SendData8(spi, (uint8_t)value);
mbed_official 216:577900467c9e 286 } else { // 16-bit
mbed_official 166:cb4253f91ada 287 SPI_I2S_SendData16(spi, (uint16_t)value);
mbed_official 166:cb4253f91ada 288 }
mbed_official 84:f54042cbc282 289 }
mbed_official 84:f54042cbc282 290
mbed_official 84:f54042cbc282 291 int spi_busy(spi_t *obj) {
mbed_official 84:f54042cbc282 292 return ssp_busy(obj);
mbed_official 84:f54042cbc282 293 }
mbed_official 84:f54042cbc282 294
mbed_official 84:f54042cbc282 295 #endif