Mbed for VNG board

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Nov 25 07:15:09 2014 +0000
Revision:
414:4ec4c5b614b0
Parent:
404:cbc7dfcb0ce9
Synchronized with git revision fbc74e874ac089c6cb05add312fb5422be628886

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

Targets: NUCLEOs - Add PeripheralPins files for all nucleo targets

Who changed what in which revision?

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