mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Mar 19 10:15:22 2014 +0000
Revision:
125:23cc3068a9e4
Child:
135:067cc8ba23da
Synchronized with git revision ace35dfba3748c7cdc102eb38ec6b9e1067c3252

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

[NUCLEO_F302R8] Add cmsis and hal files + change F401RE clock to 84MHz

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 125:23cc3068a9e4 30 #include "spi_api.h"
mbed_official 125:23cc3068a9e4 31
mbed_official 125:23cc3068a9e4 32 #if DEVICE_SPI
mbed_official 125:23cc3068a9e4 33
mbed_official 125:23cc3068a9e4 34 #include <math.h>
mbed_official 125:23cc3068a9e4 35 #include "cmsis.h"
mbed_official 125:23cc3068a9e4 36 #include "pinmap.h"
mbed_official 125:23cc3068a9e4 37 #include "error.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 125:23cc3068a9e4 77 SPI_InitStructure.SPI_Mode = obj->mode;
mbed_official 125:23cc3068a9e4 78 SPI_InitStructure.SPI_NSS = obj->nss;
mbed_official 125:23cc3068a9e4 79 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
mbed_official 125:23cc3068a9e4 80 SPI_InitStructure.SPI_DataSize = obj->bits;
mbed_official 125:23cc3068a9e4 81 SPI_InitStructure.SPI_CPOL = obj->cpol;
mbed_official 125:23cc3068a9e4 82 SPI_InitStructure.SPI_CPHA = obj->cpha;
mbed_official 125:23cc3068a9e4 83 SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
mbed_official 125:23cc3068a9e4 84 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
mbed_official 125:23cc3068a9e4 85 SPI_InitStructure.SPI_CRCPolynomial = 7;
mbed_official 125:23cc3068a9e4 86 SPI_Init(spi, &SPI_InitStructure);
mbed_official 125:23cc3068a9e4 87
mbed_official 125:23cc3068a9e4 88 SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF);
mbed_official 125:23cc3068a9e4 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 125:23cc3068a9e4 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 125:23cc3068a9e4 102
mbed_official 125:23cc3068a9e4 103 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 125:23cc3068a9e4 104
mbed_official 125:23cc3068a9e4 105 if (obj->spi == (SPIName)NC) {
mbed_official 125:23cc3068a9e4 106 error("SPI pinout mapping failed");
mbed_official 125:23cc3068a9e4 107 }
mbed_official 125:23cc3068a9e4 108
mbed_official 125:23cc3068a9e4 109 // Enable SPI clock
mbed_official 125:23cc3068a9e4 110 if (obj->spi == SPI_2) {
mbed_official 125:23cc3068a9e4 111 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
mbed_official 125:23cc3068a9e4 112 }
mbed_official 125:23cc3068a9e4 113 if (obj->spi == SPI_3) {
mbed_official 125:23cc3068a9e4 114 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
mbed_official 125:23cc3068a9e4 115 }
mbed_official 125:23cc3068a9e4 116
mbed_official 125:23cc3068a9e4 117 // Configure the SPI pins
mbed_official 125:23cc3068a9e4 118 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 125:23cc3068a9e4 119 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 125:23cc3068a9e4 120 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 125:23cc3068a9e4 121
mbed_official 125:23cc3068a9e4 122 // Save new values
mbed_official 125:23cc3068a9e4 123 obj->bits = SPI_DataSize_8b;
mbed_official 125:23cc3068a9e4 124 obj->cpol = SPI_CPOL_Low;
mbed_official 125:23cc3068a9e4 125 obj->cpha = SPI_CPHA_1Edge;
mbed_official 125:23cc3068a9e4 126 obj->br_presc = SPI_BaudRatePrescaler_256;
mbed_official 125:23cc3068a9e4 127
mbed_official 125:23cc3068a9e4 128 if (ssel == NC) { // Master
mbed_official 125:23cc3068a9e4 129 obj->mode = SPI_Mode_Master;
mbed_official 125:23cc3068a9e4 130 obj->nss = SPI_NSS_Soft;
mbed_official 125:23cc3068a9e4 131 }
mbed_official 125:23cc3068a9e4 132 else { // Slave
mbed_official 125:23cc3068a9e4 133 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 125:23cc3068a9e4 134 obj->mode = SPI_Mode_Slave;
mbed_official 125:23cc3068a9e4 135 obj->nss = SPI_NSS_Soft;
mbed_official 125:23cc3068a9e4 136 }
mbed_official 125:23cc3068a9e4 137
mbed_official 125:23cc3068a9e4 138 init_spi(obj);
mbed_official 125:23cc3068a9e4 139 }
mbed_official 125:23cc3068a9e4 140
mbed_official 125:23cc3068a9e4 141 void spi_free(spi_t *obj) {
mbed_official 125:23cc3068a9e4 142 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 143 SPI_I2S_DeInit(spi);
mbed_official 125:23cc3068a9e4 144 }
mbed_official 125:23cc3068a9e4 145
mbed_official 125:23cc3068a9e4 146 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 125:23cc3068a9e4 147 // Save new values
mbed_official 125:23cc3068a9e4 148 if (bits == 8) {
mbed_official 125:23cc3068a9e4 149 obj->bits = SPI_DataSize_8b;
mbed_official 125:23cc3068a9e4 150 }
mbed_official 125:23cc3068a9e4 151 else {
mbed_official 125:23cc3068a9e4 152 obj->bits = SPI_DataSize_16b;
mbed_official 125:23cc3068a9e4 153 }
mbed_official 125:23cc3068a9e4 154
mbed_official 125:23cc3068a9e4 155 switch (mode) {
mbed_official 125:23cc3068a9e4 156 case 0:
mbed_official 125:23cc3068a9e4 157 obj->cpol = SPI_CPOL_Low;
mbed_official 125:23cc3068a9e4 158 obj->cpha = SPI_CPHA_1Edge;
mbed_official 125:23cc3068a9e4 159 break;
mbed_official 125:23cc3068a9e4 160 case 1:
mbed_official 125:23cc3068a9e4 161 obj->cpol = SPI_CPOL_Low;
mbed_official 125:23cc3068a9e4 162 obj->cpha = SPI_CPHA_2Edge;
mbed_official 125:23cc3068a9e4 163 break;
mbed_official 125:23cc3068a9e4 164 case 2:
mbed_official 125:23cc3068a9e4 165 obj->cpol = SPI_CPOL_High;
mbed_official 125:23cc3068a9e4 166 obj->cpha = SPI_CPHA_1Edge;
mbed_official 125:23cc3068a9e4 167 break;
mbed_official 125:23cc3068a9e4 168 default:
mbed_official 125:23cc3068a9e4 169 obj->cpol = SPI_CPOL_High;
mbed_official 125:23cc3068a9e4 170 obj->cpha = SPI_CPHA_2Edge;
mbed_official 125:23cc3068a9e4 171 break;
mbed_official 125:23cc3068a9e4 172 }
mbed_official 125:23cc3068a9e4 173
mbed_official 125:23cc3068a9e4 174 if (slave == 0) {
mbed_official 125:23cc3068a9e4 175 obj->mode = SPI_Mode_Master;
mbed_official 125:23cc3068a9e4 176 obj->nss = SPI_NSS_Soft;
mbed_official 125:23cc3068a9e4 177 }
mbed_official 125:23cc3068a9e4 178 else {
mbed_official 125:23cc3068a9e4 179 obj->mode = SPI_Mode_Slave;
mbed_official 125:23cc3068a9e4 180 obj->nss = SPI_NSS_Hard;
mbed_official 125:23cc3068a9e4 181 }
mbed_official 125:23cc3068a9e4 182
mbed_official 125:23cc3068a9e4 183 init_spi(obj);
mbed_official 125:23cc3068a9e4 184 }
mbed_official 125:23cc3068a9e4 185
mbed_official 125:23cc3068a9e4 186 void spi_frequency(spi_t *obj, int hz) {
mbed_official 125:23cc3068a9e4 187 // Note: The frequencies are obtained with SPI2 clock = 32 MHz (APB1 clock)
mbed_official 125:23cc3068a9e4 188 if (hz < 250000) {
mbed_official 125:23cc3068a9e4 189 obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz
mbed_official 125:23cc3068a9e4 190 }
mbed_official 125:23cc3068a9e4 191 else if ((hz >= 250000) && (hz < 500000)) {
mbed_official 125:23cc3068a9e4 192 obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz
mbed_official 125:23cc3068a9e4 193 }
mbed_official 125:23cc3068a9e4 194 else if ((hz >= 500000) && (hz < 1000000)) {
mbed_official 125:23cc3068a9e4 195 obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz
mbed_official 125:23cc3068a9e4 196 }
mbed_official 125:23cc3068a9e4 197 else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 125:23cc3068a9e4 198 obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz
mbed_official 125:23cc3068a9e4 199 }
mbed_official 125:23cc3068a9e4 200 else if ((hz >= 2000000) && (hz < 4000000)) {
mbed_official 125:23cc3068a9e4 201 obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz
mbed_official 125:23cc3068a9e4 202 }
mbed_official 125:23cc3068a9e4 203 else if ((hz >= 4000000) && (hz < 8000000)) {
mbed_official 125:23cc3068a9e4 204 obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz
mbed_official 125:23cc3068a9e4 205 }
mbed_official 125:23cc3068a9e4 206 else if ((hz >= 8000000) && (hz < 16000000)) {
mbed_official 125:23cc3068a9e4 207 obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz
mbed_official 125:23cc3068a9e4 208 }
mbed_official 125:23cc3068a9e4 209 else { // >= 16000000
mbed_official 125:23cc3068a9e4 210 obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz
mbed_official 125:23cc3068a9e4 211 }
mbed_official 125:23cc3068a9e4 212 init_spi(obj);
mbed_official 125:23cc3068a9e4 213 }
mbed_official 125:23cc3068a9e4 214
mbed_official 125:23cc3068a9e4 215 static inline int ssp_readable(spi_t *obj) {
mbed_official 125:23cc3068a9e4 216 int status;
mbed_official 125:23cc3068a9e4 217 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 218 // Check if data is received
mbed_official 125:23cc3068a9e4 219 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 125:23cc3068a9e4 220 return status;
mbed_official 125:23cc3068a9e4 221 }
mbed_official 125:23cc3068a9e4 222
mbed_official 125:23cc3068a9e4 223 static inline int ssp_writeable(spi_t *obj) {
mbed_official 125:23cc3068a9e4 224 int status;
mbed_official 125:23cc3068a9e4 225 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 226 // Check if data is transmitted
mbed_official 125:23cc3068a9e4 227 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 125:23cc3068a9e4 228 return status;
mbed_official 125:23cc3068a9e4 229 }
mbed_official 125:23cc3068a9e4 230
mbed_official 125:23cc3068a9e4 231 static inline void ssp_write(spi_t *obj, int value) {
mbed_official 125:23cc3068a9e4 232 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 233 while (!ssp_writeable(obj));
mbed_official 125:23cc3068a9e4 234 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 235 SPI_SendData8(spi, (uint8_t)value);
mbed_official 125:23cc3068a9e4 236 }
mbed_official 125:23cc3068a9e4 237 else {
mbed_official 125:23cc3068a9e4 238 SPI_I2S_SendData16(spi, (uint16_t)value);
mbed_official 125:23cc3068a9e4 239 }
mbed_official 125:23cc3068a9e4 240 }
mbed_official 125:23cc3068a9e4 241
mbed_official 125:23cc3068a9e4 242 static inline int ssp_read(spi_t *obj) {
mbed_official 125:23cc3068a9e4 243 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 244 while (!ssp_readable(obj));
mbed_official 125:23cc3068a9e4 245 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 246 return (int)SPI_ReceiveData8(spi);
mbed_official 125:23cc3068a9e4 247 }
mbed_official 125:23cc3068a9e4 248 else {
mbed_official 125:23cc3068a9e4 249 return (int)SPI_I2S_ReceiveData16(spi);
mbed_official 125:23cc3068a9e4 250 }
mbed_official 125:23cc3068a9e4 251 }
mbed_official 125:23cc3068a9e4 252
mbed_official 125:23cc3068a9e4 253 static inline int ssp_busy(spi_t *obj) {
mbed_official 125:23cc3068a9e4 254 int status;
mbed_official 125:23cc3068a9e4 255 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 256 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 125:23cc3068a9e4 257 return status;
mbed_official 125:23cc3068a9e4 258 }
mbed_official 125:23cc3068a9e4 259
mbed_official 125:23cc3068a9e4 260 int spi_master_write(spi_t *obj, int value) {
mbed_official 125:23cc3068a9e4 261 ssp_write(obj, value);
mbed_official 125:23cc3068a9e4 262 return ssp_read(obj);
mbed_official 125:23cc3068a9e4 263 }
mbed_official 125:23cc3068a9e4 264
mbed_official 125:23cc3068a9e4 265 int spi_slave_receive(spi_t *obj) {
mbed_official 125:23cc3068a9e4 266 return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
mbed_official 125:23cc3068a9e4 267 };
mbed_official 125:23cc3068a9e4 268
mbed_official 125:23cc3068a9e4 269 int spi_slave_read(spi_t *obj) {
mbed_official 125:23cc3068a9e4 270 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 271 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 272 return (int)SPI_ReceiveData8(spi);
mbed_official 125:23cc3068a9e4 273 }
mbed_official 125:23cc3068a9e4 274 else {
mbed_official 125:23cc3068a9e4 275 return (int)SPI_I2S_ReceiveData16(spi);
mbed_official 125:23cc3068a9e4 276 }
mbed_official 125:23cc3068a9e4 277 }
mbed_official 125:23cc3068a9e4 278
mbed_official 125:23cc3068a9e4 279 void spi_slave_write(spi_t *obj, int value) {
mbed_official 125:23cc3068a9e4 280 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 281 while (!ssp_writeable(obj));
mbed_official 125:23cc3068a9e4 282 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 283 SPI_SendData8(spi, (uint8_t)value);
mbed_official 125:23cc3068a9e4 284 }
mbed_official 125:23cc3068a9e4 285 else {
mbed_official 125:23cc3068a9e4 286 SPI_I2S_SendData16(spi, (uint16_t)value);
mbed_official 125:23cc3068a9e4 287 }
mbed_official 125:23cc3068a9e4 288 }
mbed_official 125:23cc3068a9e4 289
mbed_official 125:23cc3068a9e4 290 int spi_busy(spi_t *obj) {
mbed_official 125:23cc3068a9e4 291 return ssp_busy(obj);
mbed_official 125:23cc3068a9e4 292 }
mbed_official 125:23cc3068a9e4 293
mbed_official 125:23cc3068a9e4 294 #endif