mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Mar 25 10:15:07 2014 +0000
Revision:
135:067cc8ba23da
Parent:
125:23cc3068a9e4
Child:
198:322bca33c29c
Synchronized with git revision 11bc6fdd038cd58284639ec6ff158b57e4b4bf68

Full URL: https://github.com/mbedmicro/mbed/commit/11bc6fdd038cd58284639ec6ff158b57e4b4bf68/

[NUCLEO_F302R8] Improvements in clock configuration, spi default pins, code formatting

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 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 135:067cc8ba23da 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 135:067cc8ba23da 108
mbed_official 125:23cc3068a9e4 109 // Enable SPI clock
mbed_official 125:23cc3068a9e4 110 if (obj->spi == SPI_2) {
mbed_official 135:067cc8ba23da 111 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
mbed_official 125:23cc3068a9e4 112 }
mbed_official 125:23cc3068a9e4 113 if (obj->spi == SPI_3) {
mbed_official 135:067cc8ba23da 114 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
mbed_official 125:23cc3068a9e4 115 }
mbed_official 135:067cc8ba23da 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 135:067cc8ba23da 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 135:067cc8ba23da 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 135:067cc8ba23da 131 } else { // Slave
mbed_official 125:23cc3068a9e4 132 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 125:23cc3068a9e4 133 obj->mode = SPI_Mode_Slave;
mbed_official 125:23cc3068a9e4 134 obj->nss = SPI_NSS_Soft;
mbed_official 125:23cc3068a9e4 135 }
mbed_official 125:23cc3068a9e4 136
mbed_official 125:23cc3068a9e4 137 init_spi(obj);
mbed_official 125:23cc3068a9e4 138 }
mbed_official 125:23cc3068a9e4 139
mbed_official 125:23cc3068a9e4 140 void spi_free(spi_t *obj) {
mbed_official 125:23cc3068a9e4 141 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 142 SPI_I2S_DeInit(spi);
mbed_official 125:23cc3068a9e4 143 }
mbed_official 125:23cc3068a9e4 144
mbed_official 135:067cc8ba23da 145 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 125:23cc3068a9e4 146 // Save new values
mbed_official 125:23cc3068a9e4 147 if (bits == 8) {
mbed_official 125:23cc3068a9e4 148 obj->bits = SPI_DataSize_8b;
mbed_official 135:067cc8ba23da 149 } else {
mbed_official 125:23cc3068a9e4 150 obj->bits = SPI_DataSize_16b;
mbed_official 125:23cc3068a9e4 151 }
mbed_official 135:067cc8ba23da 152
mbed_official 125:23cc3068a9e4 153 switch (mode) {
mbed_official 125:23cc3068a9e4 154 case 0:
mbed_official 135:067cc8ba23da 155 obj->cpol = SPI_CPOL_Low;
mbed_official 135:067cc8ba23da 156 obj->cpha = SPI_CPHA_1Edge;
mbed_official 135:067cc8ba23da 157 break;
mbed_official 125:23cc3068a9e4 158 case 1:
mbed_official 135:067cc8ba23da 159 obj->cpol = SPI_CPOL_Low;
mbed_official 135:067cc8ba23da 160 obj->cpha = SPI_CPHA_2Edge;
mbed_official 135:067cc8ba23da 161 break;
mbed_official 125:23cc3068a9e4 162 case 2:
mbed_official 135:067cc8ba23da 163 obj->cpol = SPI_CPOL_High;
mbed_official 135:067cc8ba23da 164 obj->cpha = SPI_CPHA_1Edge;
mbed_official 135:067cc8ba23da 165 break;
mbed_official 125:23cc3068a9e4 166 default:
mbed_official 135:067cc8ba23da 167 obj->cpol = SPI_CPOL_High;
mbed_official 135:067cc8ba23da 168 obj->cpha = SPI_CPHA_2Edge;
mbed_official 135:067cc8ba23da 169 break;
mbed_official 125:23cc3068a9e4 170 }
mbed_official 135:067cc8ba23da 171
mbed_official 125:23cc3068a9e4 172 if (slave == 0) {
mbed_official 125:23cc3068a9e4 173 obj->mode = SPI_Mode_Master;
mbed_official 125:23cc3068a9e4 174 obj->nss = SPI_NSS_Soft;
mbed_official 135:067cc8ba23da 175 } else {
mbed_official 125:23cc3068a9e4 176 obj->mode = SPI_Mode_Slave;
mbed_official 135:067cc8ba23da 177 obj->nss = SPI_NSS_Hard;
mbed_official 125:23cc3068a9e4 178 }
mbed_official 135:067cc8ba23da 179
mbed_official 125:23cc3068a9e4 180 init_spi(obj);
mbed_official 125:23cc3068a9e4 181 }
mbed_official 125:23cc3068a9e4 182
mbed_official 125:23cc3068a9e4 183 void spi_frequency(spi_t *obj, int hz) {
mbed_official 135:067cc8ba23da 184 // Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used
mbed_official 125:23cc3068a9e4 185 if (hz < 250000) {
mbed_official 135:067cc8ba23da 186 obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz - 141 kHz
mbed_official 135:067cc8ba23da 187 } else if ((hz >= 250000) && (hz < 500000)) {
mbed_official 135:067cc8ba23da 188 obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz - 280 kHz
mbed_official 135:067cc8ba23da 189 } else if ((hz >= 500000) && (hz < 1000000)) {
mbed_official 135:067cc8ba23da 190 obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz - 560 kHz
mbed_official 135:067cc8ba23da 191 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 135:067cc8ba23da 192 obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz - 1.13 MHz
mbed_official 135:067cc8ba23da 193 } else if ((hz >= 2000000) && (hz < 4000000)) {
mbed_official 135:067cc8ba23da 194 obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz - 2.25 MHz
mbed_official 135:067cc8ba23da 195 } else if ((hz >= 4000000) && (hz < 8000000)) {
mbed_official 135:067cc8ba23da 196 obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz - 4.5 MHz
mbed_official 135:067cc8ba23da 197 } else if ((hz >= 8000000) && (hz < 16000000)) {
mbed_official 135:067cc8ba23da 198 obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz - 9 MHz
mbed_official 135:067cc8ba23da 199 } else { // >= 16000000
mbed_official 135:067cc8ba23da 200 obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz - 18 MHz
mbed_official 125:23cc3068a9e4 201 }
mbed_official 125:23cc3068a9e4 202 init_spi(obj);
mbed_official 125:23cc3068a9e4 203 }
mbed_official 125:23cc3068a9e4 204
mbed_official 125:23cc3068a9e4 205 static inline int ssp_readable(spi_t *obj) {
mbed_official 125:23cc3068a9e4 206 int status;
mbed_official 125:23cc3068a9e4 207 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 208 // Check if data is received
mbed_official 125:23cc3068a9e4 209 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 135:067cc8ba23da 210 return status;
mbed_official 125:23cc3068a9e4 211 }
mbed_official 125:23cc3068a9e4 212
mbed_official 125:23cc3068a9e4 213 static inline int ssp_writeable(spi_t *obj) {
mbed_official 125:23cc3068a9e4 214 int status;
mbed_official 125:23cc3068a9e4 215 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 216 // Check if data is transmitted
mbed_official 125:23cc3068a9e4 217 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 125:23cc3068a9e4 218 return status;
mbed_official 125:23cc3068a9e4 219 }
mbed_official 125:23cc3068a9e4 220
mbed_official 125:23cc3068a9e4 221 static inline void ssp_write(spi_t *obj, int value) {
mbed_official 135:067cc8ba23da 222 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 223 while (!ssp_writeable(obj));
mbed_official 125:23cc3068a9e4 224 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 225 SPI_SendData8(spi, (uint8_t)value);
mbed_official 135:067cc8ba23da 226 } else {
mbed_official 125:23cc3068a9e4 227 SPI_I2S_SendData16(spi, (uint16_t)value);
mbed_official 125:23cc3068a9e4 228 }
mbed_official 125:23cc3068a9e4 229 }
mbed_official 125:23cc3068a9e4 230
mbed_official 125:23cc3068a9e4 231 static inline int ssp_read(spi_t *obj) {
mbed_official 135:067cc8ba23da 232 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 233 while (!ssp_readable(obj));
mbed_official 125:23cc3068a9e4 234 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 235 return (int)SPI_ReceiveData8(spi);
mbed_official 135:067cc8ba23da 236 } else {
mbed_official 125:23cc3068a9e4 237 return (int)SPI_I2S_ReceiveData16(spi);
mbed_official 125:23cc3068a9e4 238 }
mbed_official 125:23cc3068a9e4 239 }
mbed_official 125:23cc3068a9e4 240
mbed_official 125:23cc3068a9e4 241 static inline int ssp_busy(spi_t *obj) {
mbed_official 125:23cc3068a9e4 242 int status;
mbed_official 125:23cc3068a9e4 243 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 244 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 125:23cc3068a9e4 245 return status;
mbed_official 125:23cc3068a9e4 246 }
mbed_official 125:23cc3068a9e4 247
mbed_official 125:23cc3068a9e4 248 int spi_master_write(spi_t *obj, int value) {
mbed_official 125:23cc3068a9e4 249 ssp_write(obj, value);
mbed_official 125:23cc3068a9e4 250 return ssp_read(obj);
mbed_official 125:23cc3068a9e4 251 }
mbed_official 125:23cc3068a9e4 252
mbed_official 125:23cc3068a9e4 253 int spi_slave_receive(spi_t *obj) {
mbed_official 125:23cc3068a9e4 254 return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
mbed_official 125:23cc3068a9e4 255 };
mbed_official 125:23cc3068a9e4 256
mbed_official 125:23cc3068a9e4 257 int spi_slave_read(spi_t *obj) {
mbed_official 125:23cc3068a9e4 258 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 125:23cc3068a9e4 259 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 260 return (int)SPI_ReceiveData8(spi);
mbed_official 135:067cc8ba23da 261 } else {
mbed_official 125:23cc3068a9e4 262 return (int)SPI_I2S_ReceiveData16(spi);
mbed_official 125:23cc3068a9e4 263 }
mbed_official 125:23cc3068a9e4 264 }
mbed_official 125:23cc3068a9e4 265
mbed_official 125:23cc3068a9e4 266 void spi_slave_write(spi_t *obj, int value) {
mbed_official 135:067cc8ba23da 267 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 135:067cc8ba23da 268 while (!ssp_writeable(obj));
mbed_official 125:23cc3068a9e4 269 if (obj->bits == SPI_DataSize_8b) {
mbed_official 125:23cc3068a9e4 270 SPI_SendData8(spi, (uint8_t)value);
mbed_official 135:067cc8ba23da 271 } else {
mbed_official 125:23cc3068a9e4 272 SPI_I2S_SendData16(spi, (uint16_t)value);
mbed_official 125:23cc3068a9e4 273 }
mbed_official 125:23cc3068a9e4 274 }
mbed_official 125:23cc3068a9e4 275
mbed_official 125:23cc3068a9e4 276 int spi_busy(spi_t *obj) {
mbed_official 125:23cc3068a9e4 277 return ssp_busy(obj);
mbed_official 125:23cc3068a9e4 278 }
mbed_official 125:23cc3068a9e4 279
mbed_official 125:23cc3068a9e4 280 #endif