mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Jun 26 09:45:08 2014 +0100
Revision:
240:9a7c54113eaf
Parent:
227:7bd0639b8911
Synchronized with git revision 288cce7281f1f4bd5ab515fff36cdf6090bc4480

Full URL: https://github.com/mbedmicro/mbed/commit/288cce7281f1f4bd5ab515fff36cdf6090bc4480/

[DISCO-F051R8] Updated with F030R8 recent changes

Who changed what in which revision?

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