Fawwaz Nadzmy / mbed-src

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Thu Mar 12 14:30:49 2015 +0000
Revision:
489:119543c9f674
Parent:
414:4ec4c5b614b0
Synchronized with git revision 051854181516992fb498d51f9ee6e70cbad9e083

Full URL: https://github.com/mbedmicro/mbed/commit/051854181516992fb498d51f9ee6e70cbad9e083/

Fix ksdk mcu HAL - stopbit

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 227:7bd0639b8911 30 #include "mbed_assert.h"
mbed_official 52:a51c77007319 31 #include "spi_api.h"
mbed_official 52:a51c77007319 32
mbed_official 52:a51c77007319 33 #if DEVICE_SPI
mbed_official 52:a51c77007319 34
mbed_official 52:a51c77007319 35 #include <math.h>
mbed_official 52:a51c77007319 36 #include "cmsis.h"
mbed_official 52:a51c77007319 37 #include "pinmap.h"
mbed_official 414:4ec4c5b614b0 38 #include "PeripheralPins.h"
mbed_official 52:a51c77007319 39
mbed_official 489:119543c9f674 40 static SPI_HandleTypeDef SpiHandle;
mbed_official 489:119543c9f674 41
mbed_official 402:09075a3b15e3 42 static void init_spi(spi_t *obj)
mbed_official 402:09075a3b15e3 43 {
mbed_official 489:119543c9f674 44 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 56:99eb381a3269 45
mbed_official 489:119543c9f674 46 __HAL_SPI_DISABLE(&SpiHandle);
mbed_official 52:a51c77007319 47
mbed_official 489:119543c9f674 48 SpiHandle.Init.Mode = obj->mode;
mbed_official 489:119543c9f674 49 SpiHandle.Init.BaudRatePrescaler = obj->br_presc;
mbed_official 489:119543c9f674 50 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
mbed_official 489:119543c9f674 51 SpiHandle.Init.CLKPhase = obj->cpha;
mbed_official 489:119543c9f674 52 SpiHandle.Init.CLKPolarity = obj->cpol;
mbed_official 489:119543c9f674 53 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
mbed_official 489:119543c9f674 54 SpiHandle.Init.CRCPolynomial = 7;
mbed_official 489:119543c9f674 55 SpiHandle.Init.DataSize = obj->bits;
mbed_official 489:119543c9f674 56 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
mbed_official 489:119543c9f674 57 SpiHandle.Init.NSS = obj->nss;
mbed_official 489:119543c9f674 58 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
mbed_official 56:99eb381a3269 59
mbed_official 489:119543c9f674 60 HAL_SPI_Init(&SpiHandle);
mbed_official 489:119543c9f674 61
mbed_official 489:119543c9f674 62 __HAL_SPI_ENABLE(&SpiHandle);
mbed_official 56:99eb381a3269 63 }
mbed_official 56:99eb381a3269 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 52:a51c77007319 67 // Determine the SPI to use
mbed_official 52:a51c77007319 68 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 52:a51c77007319 69 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 52:a51c77007319 70 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 52:a51c77007319 71 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 174:8bb9f3a33240 72
mbed_official 52:a51c77007319 73 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 52:a51c77007319 74 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 174:8bb9f3a33240 75
mbed_official 52:a51c77007319 76 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 227:7bd0639b8911 77 MBED_ASSERT(obj->spi != (SPIName)NC);
mbed_official 174:8bb9f3a33240 78
mbed_official 52:a51c77007319 79 // Enable SPI clock
mbed_official 52:a51c77007319 80 if (obj->spi == SPI_1) {
mbed_official 489:119543c9f674 81 __SPI1_CLK_ENABLE();
mbed_official 52:a51c77007319 82 }
mbed_official 139:e3413eddde57 83 if (obj->spi == SPI_2) {
mbed_official 489:119543c9f674 84 __SPI2_CLK_ENABLE();
mbed_official 139:e3413eddde57 85 }
mbed_official 139:e3413eddde57 86
mbed_official 52:a51c77007319 87 // Configure the SPI pins
mbed_official 52:a51c77007319 88 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 52:a51c77007319 89 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 52:a51c77007319 90 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 174:8bb9f3a33240 91
mbed_official 52:a51c77007319 92 // Save new values
mbed_official 489:119543c9f674 93 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 489:119543c9f674 94 obj->cpol = SPI_POLARITY_LOW;
mbed_official 489:119543c9f674 95 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 489:119543c9f674 96 obj->br_presc = SPI_BAUDRATEPRESCALER_256;
mbed_official 174:8bb9f3a33240 97
mbed_official 208:4557f4bb2dd5 98 obj->pin_miso = miso;
mbed_official 208:4557f4bb2dd5 99 obj->pin_mosi = mosi;
mbed_official 208:4557f4bb2dd5 100 obj->pin_sclk = sclk;
mbed_official 208:4557f4bb2dd5 101 obj->pin_ssel = ssel;
mbed_official 208:4557f4bb2dd5 102
mbed_official 489:119543c9f674 103 if (ssel == NC) { // SW NSS Master mode
mbed_official 489:119543c9f674 104 obj->mode = SPI_MODE_MASTER;
mbed_official 489:119543c9f674 105 obj->nss = SPI_NSS_SOFT;
mbed_official 174:8bb9f3a33240 106 } else { // Slave
mbed_official 52:a51c77007319 107 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 489:119543c9f674 108 obj->mode = SPI_MODE_SLAVE;
mbed_official 489:119543c9f674 109 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 52:a51c77007319 110 }
mbed_official 52:a51c77007319 111
mbed_official 56:99eb381a3269 112 init_spi(obj);
mbed_official 52:a51c77007319 113 }
mbed_official 52:a51c77007319 114
mbed_official 402:09075a3b15e3 115 void spi_free(spi_t *obj)
mbed_official 402:09075a3b15e3 116 {
mbed_official 208:4557f4bb2dd5 117 // Reset SPI and disable clock
mbed_official 208:4557f4bb2dd5 118 if (obj->spi == SPI_1) {
mbed_official 489:119543c9f674 119 __SPI1_FORCE_RESET();
mbed_official 489:119543c9f674 120 __SPI1_RELEASE_RESET();
mbed_official 489:119543c9f674 121 __SPI1_CLK_DISABLE();
mbed_official 208:4557f4bb2dd5 122 }
mbed_official 208:4557f4bb2dd5 123
mbed_official 208:4557f4bb2dd5 124 if (obj->spi == SPI_2) {
mbed_official 489:119543c9f674 125 __SPI2_FORCE_RESET();
mbed_official 489:119543c9f674 126 __SPI2_RELEASE_RESET();
mbed_official 489:119543c9f674 127 __SPI2_CLK_DISABLE();
mbed_official 208:4557f4bb2dd5 128 }
mbed_official 208:4557f4bb2dd5 129
mbed_official 208:4557f4bb2dd5 130 // Configure GPIOs
mbed_official 489:119543c9f674 131 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 489:119543c9f674 132 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 489:119543c9f674 133 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 489:119543c9f674 134 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 52:a51c77007319 135 }
mbed_official 52:a51c77007319 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 52:a51c77007319 139 // Save new values
mbed_official 233:1bbc1451db33 140 if (bits == 16) {
mbed_official 489:119543c9f674 141 obj->bits = SPI_DATASIZE_16BIT;
mbed_official 233:1bbc1451db33 142 } else {
mbed_official 489:119543c9f674 143 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 52:a51c77007319 144 }
mbed_official 174:8bb9f3a33240 145
mbed_official 52:a51c77007319 146 switch (mode) {
mbed_official 52:a51c77007319 147 case 0:
mbed_official 489:119543c9f674 148 obj->cpol = SPI_POLARITY_LOW;
mbed_official 489:119543c9f674 149 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 174:8bb9f3a33240 150 break;
mbed_official 52:a51c77007319 151 case 1:
mbed_official 489:119543c9f674 152 obj->cpol = SPI_POLARITY_LOW;
mbed_official 489:119543c9f674 153 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 174:8bb9f3a33240 154 break;
mbed_official 52:a51c77007319 155 case 2:
mbed_official 489:119543c9f674 156 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 489:119543c9f674 157 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 174:8bb9f3a33240 158 break;
mbed_official 52:a51c77007319 159 default:
mbed_official 489:119543c9f674 160 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 489:119543c9f674 161 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 174:8bb9f3a33240 162 break;
mbed_official 52:a51c77007319 163 }
mbed_official 174:8bb9f3a33240 164
mbed_official 52:a51c77007319 165 if (slave == 0) {
mbed_official 489:119543c9f674 166 obj->mode = SPI_MODE_MASTER;
mbed_official 489:119543c9f674 167 obj->nss = SPI_NSS_SOFT;
mbed_official 174:8bb9f3a33240 168 } else {
mbed_official 489:119543c9f674 169 obj->mode = SPI_MODE_SLAVE;
mbed_official 489:119543c9f674 170 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 52:a51c77007319 171 }
mbed_official 174:8bb9f3a33240 172
mbed_official 56:99eb381a3269 173 init_spi(obj);
mbed_official 52:a51c77007319 174 }
mbed_official 52:a51c77007319 175
mbed_official 402:09075a3b15e3 176 void spi_frequency(spi_t *obj, int hz)
mbed_official 402:09075a3b15e3 177 {
mbed_official 139:e3413eddde57 178 if (obj->spi == SPI_1) {
mbed_official 139:e3413eddde57 179 // Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used
mbed_official 139:e3413eddde57 180 if (hz < 500000) {
mbed_official 489:119543c9f674 181 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 250 kHz - 281 kHz
mbed_official 174:8bb9f3a33240 182 } else if ((hz >= 500000) && (hz < 1000000)) {
mbed_official 489:119543c9f674 183 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 500 kHz - 563 kHz
mbed_official 174:8bb9f3a33240 184 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 489:119543c9f674 185 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1 MHz - 1.13 MHz
mbed_official 174:8bb9f3a33240 186 } else if ((hz >= 2000000) && (hz < 4000000)) {
mbed_official 489:119543c9f674 187 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2 MHz - 2.25 MHz
mbed_official 174:8bb9f3a33240 188 } else if ((hz >= 4000000) && (hz < 8000000)) {
mbed_official 489:119543c9f674 189 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 4 MHz - 4.5 MHz
mbed_official 174:8bb9f3a33240 190 } else if ((hz >= 8000000) && (hz < 16000000)) {
mbed_official 489:119543c9f674 191 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 8 MHz - 9 MHz
mbed_official 174:8bb9f3a33240 192 } else if ((hz >= 16000000) && (hz < 32000000)) {
mbed_official 489:119543c9f674 193 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 16 MHz - 18 MHz
mbed_official 174:8bb9f3a33240 194 } else { // >= 32000000
mbed_official 489:119543c9f674 195 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 32 MHz - 36 MHz
mbed_official 139:e3413eddde57 196 }
mbed_official 139:e3413eddde57 197 }
mbed_official 174:8bb9f3a33240 198
mbed_official 139:e3413eddde57 199 if (obj->spi == SPI_2) {
mbed_official 139:e3413eddde57 200 // Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used
mbed_official 139:e3413eddde57 201 if (hz < 250000) {
mbed_official 489:119543c9f674 202 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 125 kHz - 141 kHz
mbed_official 174:8bb9f3a33240 203 } else if ((hz >= 250000) && (hz < 500000)) {
mbed_official 489:119543c9f674 204 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 250 kHz - 281 kHz
mbed_official 174:8bb9f3a33240 205 } else if ((hz >= 500000) && (hz < 1000000)) {
mbed_official 489:119543c9f674 206 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 500 kHz - 563 kHz
mbed_official 174:8bb9f3a33240 207 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 489:119543c9f674 208 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1 MHz - 1.13 MHz
mbed_official 174:8bb9f3a33240 209 } else if ((hz >= 2000000) && (hz < 4000000)) {
mbed_official 489:119543c9f674 210 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 2 MHz - 2.25 MHz
mbed_official 174:8bb9f3a33240 211 } else if ((hz >= 4000000) && (hz < 8000000)) {
mbed_official 489:119543c9f674 212 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 4 MHz - 4.5 MHz
mbed_official 174:8bb9f3a33240 213 } else if ((hz >= 8000000) && (hz < 16000000)) {
mbed_official 489:119543c9f674 214 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 8 MHz - 9 MHz
mbed_official 174:8bb9f3a33240 215 } else { // >= 16000000
mbed_official 489:119543c9f674 216 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 16 MHz - 18 MHz
mbed_official 139:e3413eddde57 217 }
mbed_official 139:e3413eddde57 218 }
mbed_official 52:a51c77007319 219
mbed_official 56:99eb381a3269 220 init_spi(obj);
mbed_official 52:a51c77007319 221 }
mbed_official 52:a51c77007319 222
mbed_official 402:09075a3b15e3 223 static inline int ssp_readable(spi_t *obj)
mbed_official 402:09075a3b15e3 224 {
mbed_official 52:a51c77007319 225 int status;
mbed_official 489:119543c9f674 226 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 227 // Check if data is received
mbed_official 489:119543c9f674 228 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 174:8bb9f3a33240 229 return status;
mbed_official 52:a51c77007319 230 }
mbed_official 52:a51c77007319 231
mbed_official 402:09075a3b15e3 232 static inline int ssp_writeable(spi_t *obj)
mbed_official 402:09075a3b15e3 233 {
mbed_official 52:a51c77007319 234 int status;
mbed_official 489:119543c9f674 235 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 236 // Check if data is transmitted
mbed_official 489:119543c9f674 237 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 238 return status;
mbed_official 52:a51c77007319 239 }
mbed_official 52:a51c77007319 240
mbed_official 402:09075a3b15e3 241 static inline void ssp_write(spi_t *obj, int value)
mbed_official 402:09075a3b15e3 242 {
mbed_official 174:8bb9f3a33240 243 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 244 while (!ssp_writeable(obj));
mbed_official 489:119543c9f674 245 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 246 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 247 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 248 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 249 *p_spi_dr = (uint8_t)value;
mbed_official 489:119543c9f674 250 } else { // SPI_DATASIZE_16BIT
mbed_official 489:119543c9f674 251 spi->DR = (uint16_t)value;
mbed_official 489:119543c9f674 252 }
mbed_official 52:a51c77007319 253 }
mbed_official 52:a51c77007319 254
mbed_official 402:09075a3b15e3 255 static inline int ssp_read(spi_t *obj)
mbed_official 402:09075a3b15e3 256 {
mbed_official 174:8bb9f3a33240 257 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 258 while (!ssp_readable(obj));
mbed_official 489:119543c9f674 259 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 260 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 261 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 262 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 263 return (int)(*p_spi_dr);
mbed_official 489:119543c9f674 264 } else {
mbed_official 489:119543c9f674 265 return (int)spi->DR;
mbed_official 489:119543c9f674 266 }
mbed_official 52:a51c77007319 267 }
mbed_official 52:a51c77007319 268
mbed_official 402:09075a3b15e3 269 static inline int ssp_busy(spi_t *obj)
mbed_official 402:09075a3b15e3 270 {
mbed_official 52:a51c77007319 271 int status;
mbed_official 489:119543c9f674 272 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 489:119543c9f674 273 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 274 return status;
mbed_official 52:a51c77007319 275 }
mbed_official 52:a51c77007319 276
mbed_official 402:09075a3b15e3 277 int spi_master_write(spi_t *obj, int value)
mbed_official 402:09075a3b15e3 278 {
mbed_official 52:a51c77007319 279 ssp_write(obj, value);
mbed_official 52:a51c77007319 280 return ssp_read(obj);
mbed_official 52:a51c77007319 281 }
mbed_official 52:a51c77007319 282
mbed_official 402:09075a3b15e3 283 int spi_slave_receive(spi_t *obj)
mbed_official 402:09075a3b15e3 284 {
mbed_official 233:1bbc1451db33 285 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 52:a51c77007319 286 };
mbed_official 52:a51c77007319 287
mbed_official 402:09075a3b15e3 288 int spi_slave_read(spi_t *obj)
mbed_official 402:09075a3b15e3 289 {
mbed_official 52:a51c77007319 290 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 489:119543c9f674 291 while (!ssp_readable(obj));
mbed_official 489:119543c9f674 292 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 293 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 294 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 295 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 296 return (int)(*p_spi_dr);
mbed_official 489:119543c9f674 297 } else {
mbed_official 489:119543c9f674 298 return (int)spi->DR;
mbed_official 489:119543c9f674 299 }
mbed_official 52:a51c77007319 300 }
mbed_official 52:a51c77007319 301
mbed_official 402:09075a3b15e3 302 void spi_slave_write(spi_t *obj, int value)
mbed_official 402:09075a3b15e3 303 {
mbed_official 174:8bb9f3a33240 304 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 174:8bb9f3a33240 305 while (!ssp_writeable(obj));
mbed_official 489:119543c9f674 306 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 307 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 308 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 309 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 310 *p_spi_dr = (uint8_t)value;
mbed_official 489:119543c9f674 311 } else { // SPI_DATASIZE_16BIT
mbed_official 489:119543c9f674 312 spi->DR = (uint16_t)value;
mbed_official 489:119543c9f674 313 }
mbed_official 52:a51c77007319 314 }
mbed_official 52:a51c77007319 315
mbed_official 402:09075a3b15e3 316 int spi_busy(spi_t *obj)
mbed_official 402:09075a3b15e3 317 {
mbed_official 52:a51c77007319 318 return ssp_busy(obj);
mbed_official 52:a51c77007319 319 }
mbed_official 52:a51c77007319 320
mbed_official 52:a51c77007319 321 #endif