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