mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
237:f3da66175598
test with CLOCK_SETUP = 0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 237:f3da66175598 1 /* mbed Microcontroller Library
mbed_official 237:f3da66175598 2 *******************************************************************************
mbed_official 237:f3da66175598 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 237:f3da66175598 4 * All rights reserved.
mbed_official 237:f3da66175598 5 *
mbed_official 237:f3da66175598 6 * Redistribution and use in source and binary forms, with or without
mbed_official 237:f3da66175598 7 * modification, are permitted provided that the following conditions are met:
mbed_official 237:f3da66175598 8 *
mbed_official 237:f3da66175598 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 237:f3da66175598 10 * this list of conditions and the following disclaimer.
mbed_official 237:f3da66175598 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 237:f3da66175598 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 237:f3da66175598 13 * and/or other materials provided with the distribution.
mbed_official 237:f3da66175598 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 237:f3da66175598 15 * may be used to endorse or promote products derived from this software
mbed_official 237:f3da66175598 16 * without specific prior written permission.
mbed_official 237:f3da66175598 17 *
mbed_official 237:f3da66175598 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 237:f3da66175598 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 237:f3da66175598 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 237:f3da66175598 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 237:f3da66175598 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 237:f3da66175598 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 237:f3da66175598 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 237:f3da66175598 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 237:f3da66175598 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 237:f3da66175598 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 237:f3da66175598 28 *******************************************************************************
mbed_official 237:f3da66175598 29 */
mbed_official 237:f3da66175598 30 #include "mbed_assert.h"
mbed_official 237:f3da66175598 31 #include "spi_api.h"
mbed_official 237:f3da66175598 32
mbed_official 237:f3da66175598 33 #if DEVICE_SPI
mbed_official 237:f3da66175598 34
mbed_official 237:f3da66175598 35 #include <math.h>
mbed_official 237:f3da66175598 36 #include "cmsis.h"
mbed_official 237:f3da66175598 37 #include "pinmap.h"
mbed_official 237:f3da66175598 38
mbed_official 237:f3da66175598 39 static const PinMap PinMap_SPI_MOSI[] = {
mbed_official 237:f3da66175598 40 {PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 41 {PB_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 42 {NC, NC, 0}
mbed_official 237:f3da66175598 43 };
mbed_official 237:f3da66175598 44
mbed_official 237:f3da66175598 45 static const PinMap PinMap_SPI_MISO[] = {
mbed_official 237:f3da66175598 46 {PA_6, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 47 {PB_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 48 {NC, NC, 0}
mbed_official 237:f3da66175598 49 };
mbed_official 237:f3da66175598 50
mbed_official 237:f3da66175598 51 static const PinMap PinMap_SPI_SCLK[] = {
mbed_official 237:f3da66175598 52 {PA_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 53 {PB_3, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 54 {NC, NC, 0}
mbed_official 237:f3da66175598 55 };
mbed_official 237:f3da66175598 56
mbed_official 237:f3da66175598 57 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 237:f3da66175598 58 {PA_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 59 {PA_15, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 237:f3da66175598 60 {NC, NC, 0}
mbed_official 237:f3da66175598 61 };
mbed_official 237:f3da66175598 62
mbed_official 237:f3da66175598 63 static SPI_HandleTypeDef SpiHandle;
mbed_official 237:f3da66175598 64
mbed_official 237:f3da66175598 65 static void init_spi(spi_t *obj)
mbed_official 237:f3da66175598 66 {
mbed_official 237:f3da66175598 67 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 68
mbed_official 237:f3da66175598 69 __HAL_SPI_DISABLE(&SpiHandle);
mbed_official 237:f3da66175598 70
mbed_official 237:f3da66175598 71 SpiHandle.Init.Mode = obj->mode;
mbed_official 237:f3da66175598 72 SpiHandle.Init.BaudRatePrescaler = obj->br_presc;
mbed_official 237:f3da66175598 73 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
mbed_official 237:f3da66175598 74 SpiHandle.Init.CLKPhase = obj->cpha;
mbed_official 237:f3da66175598 75 SpiHandle.Init.CLKPolarity = obj->cpol;
mbed_official 237:f3da66175598 76 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
mbed_official 237:f3da66175598 77 SpiHandle.Init.CRCPolynomial = 7;
mbed_official 237:f3da66175598 78 SpiHandle.Init.DataSize = obj->bits;
mbed_official 237:f3da66175598 79 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
mbed_official 237:f3da66175598 80 SpiHandle.Init.NSS = obj->nss;
mbed_official 237:f3da66175598 81 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
mbed_official 237:f3da66175598 82
mbed_official 237:f3da66175598 83 HAL_SPI_Init(&SpiHandle);
mbed_official 237:f3da66175598 84
mbed_official 237:f3da66175598 85 __HAL_SPI_ENABLE(&SpiHandle);
mbed_official 237:f3da66175598 86 }
mbed_official 237:f3da66175598 87
mbed_official 237:f3da66175598 88 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
mbed_official 237:f3da66175598 89 {
mbed_official 237:f3da66175598 90 // Determine the SPI to use
mbed_official 237:f3da66175598 91 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 237:f3da66175598 92 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 237:f3da66175598 93 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 237:f3da66175598 94 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 237:f3da66175598 95
mbed_official 237:f3da66175598 96 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 237:f3da66175598 97 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 237:f3da66175598 98
mbed_official 237:f3da66175598 99 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 237:f3da66175598 100 MBED_ASSERT(obj->spi != (SPIName)NC);
mbed_official 237:f3da66175598 101
mbed_official 237:f3da66175598 102 // Enable SPI clock
mbed_official 237:f3da66175598 103 __SPI1_CLK_ENABLE();
mbed_official 237:f3da66175598 104
mbed_official 237:f3da66175598 105 // Configure the SPI pins
mbed_official 237:f3da66175598 106 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 237:f3da66175598 107 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 237:f3da66175598 108 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 237:f3da66175598 109
mbed_official 237:f3da66175598 110 // Save new values
mbed_official 237:f3da66175598 111 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 237:f3da66175598 112 obj->cpol = SPI_POLARITY_LOW;
mbed_official 237:f3da66175598 113 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 237:f3da66175598 114 obj->br_presc = SPI_BAUDRATEPRESCALER_256;
mbed_official 237:f3da66175598 115
mbed_official 237:f3da66175598 116 obj->pin_miso = miso;
mbed_official 237:f3da66175598 117 obj->pin_mosi = mosi;
mbed_official 237:f3da66175598 118 obj->pin_sclk = sclk;
mbed_official 237:f3da66175598 119 obj->pin_ssel = ssel;
mbed_official 237:f3da66175598 120
mbed_official 237:f3da66175598 121 if (ssel == NC) { // SW NSS Master mode
mbed_official 237:f3da66175598 122 obj->mode = SPI_MODE_MASTER;
mbed_official 237:f3da66175598 123 obj->nss = SPI_NSS_SOFT;
mbed_official 237:f3da66175598 124 } else { // Slave
mbed_official 237:f3da66175598 125 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 237:f3da66175598 126 obj->mode = SPI_MODE_SLAVE;
mbed_official 237:f3da66175598 127 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 237:f3da66175598 128 }
mbed_official 237:f3da66175598 129
mbed_official 237:f3da66175598 130 init_spi(obj);
mbed_official 237:f3da66175598 131 }
mbed_official 237:f3da66175598 132
mbed_official 237:f3da66175598 133 void spi_free(spi_t *obj)
mbed_official 237:f3da66175598 134 {
mbed_official 237:f3da66175598 135 // Reset SPI and disable clock
mbed_official 237:f3da66175598 136 __SPI1_FORCE_RESET();
mbed_official 237:f3da66175598 137 __SPI1_RELEASE_RESET();
mbed_official 237:f3da66175598 138 __SPI1_CLK_DISABLE();
mbed_official 237:f3da66175598 139
mbed_official 237:f3da66175598 140 // Configure GPIOs
mbed_official 237:f3da66175598 141 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 237:f3da66175598 142 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 237:f3da66175598 143 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 237:f3da66175598 144 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 237:f3da66175598 145 }
mbed_official 237:f3da66175598 146
mbed_official 237:f3da66175598 147 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 237:f3da66175598 148 {
mbed_official 237:f3da66175598 149 // Save new values
mbed_official 237:f3da66175598 150 if (bits == 16) {
mbed_official 237:f3da66175598 151 obj->bits = SPI_DATASIZE_16BIT;
mbed_official 237:f3da66175598 152 } else {
mbed_official 237:f3da66175598 153 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 237:f3da66175598 154 }
mbed_official 237:f3da66175598 155
mbed_official 237:f3da66175598 156 switch (mode) {
mbed_official 237:f3da66175598 157 case 0:
mbed_official 237:f3da66175598 158 obj->cpol = SPI_POLARITY_LOW;
mbed_official 237:f3da66175598 159 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 237:f3da66175598 160 break;
mbed_official 237:f3da66175598 161 case 1:
mbed_official 237:f3da66175598 162 obj->cpol = SPI_POLARITY_LOW;
mbed_official 237:f3da66175598 163 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 237:f3da66175598 164 break;
mbed_official 237:f3da66175598 165 case 2:
mbed_official 237:f3da66175598 166 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 237:f3da66175598 167 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 237:f3da66175598 168 break;
mbed_official 237:f3da66175598 169 default:
mbed_official 237:f3da66175598 170 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 237:f3da66175598 171 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 237:f3da66175598 172 break;
mbed_official 237:f3da66175598 173 }
mbed_official 237:f3da66175598 174
mbed_official 237:f3da66175598 175 if (slave == 0) {
mbed_official 237:f3da66175598 176 obj->mode = SPI_MODE_MASTER;
mbed_official 237:f3da66175598 177 obj->nss = SPI_NSS_SOFT;
mbed_official 237:f3da66175598 178 } else {
mbed_official 237:f3da66175598 179 obj->mode = SPI_MODE_SLAVE;
mbed_official 237:f3da66175598 180 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 237:f3da66175598 181 }
mbed_official 237:f3da66175598 182
mbed_official 237:f3da66175598 183 init_spi(obj);
mbed_official 237:f3da66175598 184 }
mbed_official 237:f3da66175598 185
mbed_official 237:f3da66175598 186 void spi_frequency(spi_t *obj, int hz)
mbed_official 237:f3da66175598 187 {
mbed_official 237:f3da66175598 188 // Values depend of APB2CLK : 64 MHz if HSI is used, 72 MHz if HSE is used
mbed_official 237:f3da66175598 189 if (hz < 500000) {
mbed_official 237:f3da66175598 190 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 250 kHz - 281 kHz
mbed_official 237:f3da66175598 191 } else if ((hz >= 500000) && (hz < 1000000)) {
mbed_official 237:f3da66175598 192 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 500 kHz - 563 kHz
mbed_official 237:f3da66175598 193 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 237:f3da66175598 194 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1 MHz - 1.13 MHz
mbed_official 237:f3da66175598 195 } else if ((hz >= 2000000) && (hz < 4000000)) {
mbed_official 237:f3da66175598 196 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2 MHz - 2.25 MHz
mbed_official 237:f3da66175598 197 } else if ((hz >= 4000000) && (hz < 8000000)) {
mbed_official 237:f3da66175598 198 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 4 MHz - 4.5 MHz
mbed_official 237:f3da66175598 199 } else if ((hz >= 8000000) && (hz < 16000000)) {
mbed_official 237:f3da66175598 200 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 8 MHz - 9 MHz
mbed_official 237:f3da66175598 201 } else if ((hz >= 16000000) && (hz < 32000000)) {
mbed_official 237:f3da66175598 202 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 16 MHz - 18 MHz
mbed_official 237:f3da66175598 203 } else { // >= 32000000
mbed_official 237:f3da66175598 204 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 32 MHz - 36 MHz
mbed_official 237:f3da66175598 205 }
mbed_official 237:f3da66175598 206
mbed_official 237:f3da66175598 207 init_spi(obj);
mbed_official 237:f3da66175598 208 }
mbed_official 237:f3da66175598 209
mbed_official 237:f3da66175598 210 static inline int ssp_readable(spi_t *obj)
mbed_official 237:f3da66175598 211 {
mbed_official 237:f3da66175598 212 int status;
mbed_official 237:f3da66175598 213 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 214 // Check if data is received
mbed_official 237:f3da66175598 215 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 237:f3da66175598 216 return status;
mbed_official 237:f3da66175598 217 }
mbed_official 237:f3da66175598 218
mbed_official 237:f3da66175598 219 static inline int ssp_writeable(spi_t *obj)
mbed_official 237:f3da66175598 220 {
mbed_official 237:f3da66175598 221 int status;
mbed_official 237:f3da66175598 222 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 223 // Check if data is transmitted
mbed_official 237:f3da66175598 224 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 237:f3da66175598 225 return status;
mbed_official 237:f3da66175598 226 }
mbed_official 237:f3da66175598 227
mbed_official 237:f3da66175598 228 static inline void ssp_write(spi_t *obj, int value)
mbed_official 237:f3da66175598 229 {
mbed_official 237:f3da66175598 230 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 231 while (!ssp_writeable(obj));
mbed_official 237:f3da66175598 232 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 237:f3da66175598 233 // Force 8-bit access to the data register
mbed_official 237:f3da66175598 234 uint8_t *p_spi_dr = 0;
mbed_official 237:f3da66175598 235 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 237:f3da66175598 236 *p_spi_dr = (uint8_t)value;
mbed_official 237:f3da66175598 237 } else { // SPI_DATASIZE_16BIT
mbed_official 237:f3da66175598 238 spi->DR = (uint16_t)value;
mbed_official 237:f3da66175598 239 }
mbed_official 237:f3da66175598 240 }
mbed_official 237:f3da66175598 241
mbed_official 237:f3da66175598 242 static inline int ssp_read(spi_t *obj)
mbed_official 237:f3da66175598 243 {
mbed_official 237:f3da66175598 244 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 245 while (!ssp_readable(obj));
mbed_official 237:f3da66175598 246 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 237:f3da66175598 247 // Force 8-bit access to the data register
mbed_official 237:f3da66175598 248 uint8_t *p_spi_dr = 0;
mbed_official 237:f3da66175598 249 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 237:f3da66175598 250 return (int)(*p_spi_dr);
mbed_official 237:f3da66175598 251 } else {
mbed_official 237:f3da66175598 252 return (int)spi->DR;
mbed_official 237:f3da66175598 253 }
mbed_official 237:f3da66175598 254 }
mbed_official 237:f3da66175598 255
mbed_official 237:f3da66175598 256 static inline int ssp_busy(spi_t *obj)
mbed_official 237:f3da66175598 257 {
mbed_official 237:f3da66175598 258 int status;
mbed_official 237:f3da66175598 259 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 260 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 237:f3da66175598 261 return status;
mbed_official 237:f3da66175598 262 }
mbed_official 237:f3da66175598 263
mbed_official 237:f3da66175598 264 int spi_master_write(spi_t *obj, int value)
mbed_official 237:f3da66175598 265 {
mbed_official 237:f3da66175598 266 ssp_write(obj, value);
mbed_official 237:f3da66175598 267 return ssp_read(obj);
mbed_official 237:f3da66175598 268 }
mbed_official 237:f3da66175598 269
mbed_official 237:f3da66175598 270 int spi_slave_receive(spi_t *obj)
mbed_official 237:f3da66175598 271 {
mbed_official 237:f3da66175598 272 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 237:f3da66175598 273 };
mbed_official 237:f3da66175598 274
mbed_official 237:f3da66175598 275 int spi_slave_read(spi_t *obj)
mbed_official 237:f3da66175598 276 {
mbed_official 237:f3da66175598 277 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 278 while (!ssp_readable(obj));
mbed_official 237:f3da66175598 279 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 237:f3da66175598 280 // Force 8-bit access to the data register
mbed_official 237:f3da66175598 281 uint8_t *p_spi_dr = 0;
mbed_official 237:f3da66175598 282 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 237:f3da66175598 283 return (int)(*p_spi_dr);
mbed_official 237:f3da66175598 284 } else {
mbed_official 237:f3da66175598 285 return (int)spi->DR;
mbed_official 237:f3da66175598 286 }
mbed_official 237:f3da66175598 287 }
mbed_official 237:f3da66175598 288
mbed_official 237:f3da66175598 289 void spi_slave_write(spi_t *obj, int value)
mbed_official 237:f3da66175598 290 {
mbed_official 237:f3da66175598 291 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 237:f3da66175598 292 while (!ssp_writeable(obj));
mbed_official 237:f3da66175598 293 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 237:f3da66175598 294 // Force 8-bit access to the data register
mbed_official 237:f3da66175598 295 uint8_t *p_spi_dr = 0;
mbed_official 237:f3da66175598 296 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 237:f3da66175598 297 *p_spi_dr = (uint8_t)value;
mbed_official 237:f3da66175598 298 } else { // SPI_DATASIZE_16BIT
mbed_official 237:f3da66175598 299 spi->DR = (uint16_t)value;
mbed_official 237:f3da66175598 300 }
mbed_official 237:f3da66175598 301 }
mbed_official 237:f3da66175598 302
mbed_official 237:f3da66175598 303 int spi_busy(spi_t *obj)
mbed_official 237:f3da66175598 304 {
mbed_official 237:f3da66175598 305 return ssp_busy(obj);
mbed_official 237:f3da66175598 306 }
mbed_official 237:f3da66175598 307
mbed_official 237:f3da66175598 308 #endif