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:
Thu Mar 27 09:45:07 2014 +0000
Revision:
139:e3413eddde57
Parent:
88:81f18c97d490
Child:
174:8bb9f3a33240
Synchronized with git revision 3d49a491d4dd16466354746d3c329428840f5a03

Full URL: https://github.com/mbedmicro/mbed/commit/3d49a491d4dd16466354746d3c329428840f5a03/

Fixed readNB() bug

Who changed what in which revision?

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