Lancaster University / mbed-src

Fork of mbed-src by mbed official

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:52:54 2016 +0100
Revision:
641:be9b2017785a
Parent:
552:a1b9575155a3
Synchronized with git rev 1fb8ab4c
Author: James Devine
mbed-classic: BUGFIX for timer when using wait_ms from interrupt context

Previously if a user used wait[_ms,_us] in interrupt context the device would
hang indefinitely. This was due to incrementing overflowCount from
interrupt context only.

This meant that if a user used wait[_ms,_us] in an ISR with
the same or greater interrupt priority, it would result in an infinite
loop as the overflowCount variable would never be incremented, and
wait[_ms,_us] would never return.

This patch simply applies a better solution for the race condition
mentioned in the previous commit. It instead disables the timer1
interrupt and increments the overflowCount variable, preventing
the race condition whilst supporting wait[_ms,_us] in interrupt
context.

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 552:a1b9575155a3 103 if (ssel != NC) {
mbed_official 552:a1b9575155a3 104 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 552:a1b9575155a3 105 } else {
mbed_official 489:119543c9f674 106 obj->nss = SPI_NSS_SOFT;
mbed_official 52:a51c77007319 107 }
mbed_official 52:a51c77007319 108
mbed_official 56:99eb381a3269 109 init_spi(obj);
mbed_official 52:a51c77007319 110 }
mbed_official 52:a51c77007319 111
mbed_official 402:09075a3b15e3 112 void spi_free(spi_t *obj)
mbed_official 402:09075a3b15e3 113 {
mbed_official 208:4557f4bb2dd5 114 // Reset SPI and disable clock
mbed_official 208:4557f4bb2dd5 115 if (obj->spi == SPI_1) {
mbed_official 489:119543c9f674 116 __SPI1_FORCE_RESET();
mbed_official 489:119543c9f674 117 __SPI1_RELEASE_RESET();
mbed_official 489:119543c9f674 118 __SPI1_CLK_DISABLE();
mbed_official 208:4557f4bb2dd5 119 }
mbed_official 208:4557f4bb2dd5 120
mbed_official 208:4557f4bb2dd5 121 if (obj->spi == SPI_2) {
mbed_official 489:119543c9f674 122 __SPI2_FORCE_RESET();
mbed_official 489:119543c9f674 123 __SPI2_RELEASE_RESET();
mbed_official 489:119543c9f674 124 __SPI2_CLK_DISABLE();
mbed_official 208:4557f4bb2dd5 125 }
mbed_official 208:4557f4bb2dd5 126
mbed_official 208:4557f4bb2dd5 127 // Configure GPIOs
mbed_official 489:119543c9f674 128 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 489:119543c9f674 129 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 489:119543c9f674 130 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 489:119543c9f674 131 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 52:a51c77007319 132 }
mbed_official 52:a51c77007319 133
mbed_official 402:09075a3b15e3 134 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 402:09075a3b15e3 135 {
mbed_official 52:a51c77007319 136 // Save new values
mbed_official 233:1bbc1451db33 137 if (bits == 16) {
mbed_official 489:119543c9f674 138 obj->bits = SPI_DATASIZE_16BIT;
mbed_official 233:1bbc1451db33 139 } else {
mbed_official 489:119543c9f674 140 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 52:a51c77007319 141 }
mbed_official 174:8bb9f3a33240 142
mbed_official 52:a51c77007319 143 switch (mode) {
mbed_official 52:a51c77007319 144 case 0:
mbed_official 489:119543c9f674 145 obj->cpol = SPI_POLARITY_LOW;
mbed_official 489:119543c9f674 146 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 174:8bb9f3a33240 147 break;
mbed_official 52:a51c77007319 148 case 1:
mbed_official 489:119543c9f674 149 obj->cpol = SPI_POLARITY_LOW;
mbed_official 489:119543c9f674 150 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 174:8bb9f3a33240 151 break;
mbed_official 52:a51c77007319 152 case 2:
mbed_official 489:119543c9f674 153 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 489:119543c9f674 154 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 174:8bb9f3a33240 155 break;
mbed_official 52:a51c77007319 156 default:
mbed_official 489:119543c9f674 157 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 489:119543c9f674 158 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 174:8bb9f3a33240 159 break;
mbed_official 52:a51c77007319 160 }
mbed_official 174:8bb9f3a33240 161
mbed_official 552:a1b9575155a3 162 if (obj->nss != SPI_NSS_SOFT) {
mbed_official 552:a1b9575155a3 163 obj->nss = (slave) ? SPI_NSS_HARD_INPUT : SPI_NSS_HARD_OUTPUT;
mbed_official 52:a51c77007319 164 }
mbed_official 174:8bb9f3a33240 165
mbed_official 552:a1b9575155a3 166 obj->mode = (slave) ? SPI_MODE_SLAVE : SPI_MODE_MASTER;
mbed_official 552:a1b9575155a3 167
mbed_official 56:99eb381a3269 168 init_spi(obj);
mbed_official 52:a51c77007319 169 }
mbed_official 52:a51c77007319 170
mbed_official 402:09075a3b15e3 171 void spi_frequency(spi_t *obj, int hz)
mbed_official 402:09075a3b15e3 172 {
mbed_official 139:e3413eddde57 173 if (obj->spi == SPI_1) {
mbed_official 139:e3413eddde57 174 // Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used
mbed_official 139:e3413eddde57 175 if (hz < 500000) {
mbed_official 489:119543c9f674 176 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 250 kHz - 281 kHz
mbed_official 174:8bb9f3a33240 177 } else if ((hz >= 500000) && (hz < 1000000)) {
mbed_official 489:119543c9f674 178 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 500 kHz - 563 kHz
mbed_official 174:8bb9f3a33240 179 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 489:119543c9f674 180 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1 MHz - 1.13 MHz
mbed_official 174:8bb9f3a33240 181 } else if ((hz >= 2000000) && (hz < 4000000)) {
mbed_official 489:119543c9f674 182 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 2 MHz - 2.25 MHz
mbed_official 174:8bb9f3a33240 183 } else if ((hz >= 4000000) && (hz < 8000000)) {
mbed_official 489:119543c9f674 184 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 4 MHz - 4.5 MHz
mbed_official 174:8bb9f3a33240 185 } else if ((hz >= 8000000) && (hz < 16000000)) {
mbed_official 489:119543c9f674 186 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 8 MHz - 9 MHz
mbed_official 174:8bb9f3a33240 187 } else if ((hz >= 16000000) && (hz < 32000000)) {
mbed_official 489:119543c9f674 188 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 16 MHz - 18 MHz
mbed_official 174:8bb9f3a33240 189 } else { // >= 32000000
mbed_official 489:119543c9f674 190 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 32 MHz - 36 MHz
mbed_official 139:e3413eddde57 191 }
mbed_official 139:e3413eddde57 192 }
mbed_official 174:8bb9f3a33240 193
mbed_official 139:e3413eddde57 194 if (obj->spi == SPI_2) {
mbed_official 139:e3413eddde57 195 // Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used
mbed_official 139:e3413eddde57 196 if (hz < 250000) {
mbed_official 489:119543c9f674 197 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 125 kHz - 141 kHz
mbed_official 174:8bb9f3a33240 198 } else if ((hz >= 250000) && (hz < 500000)) {
mbed_official 489:119543c9f674 199 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 250 kHz - 281 kHz
mbed_official 174:8bb9f3a33240 200 } else if ((hz >= 500000) && (hz < 1000000)) {
mbed_official 489:119543c9f674 201 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 500 kHz - 563 kHz
mbed_official 174:8bb9f3a33240 202 } else if ((hz >= 1000000) && (hz < 2000000)) {
mbed_official 489:119543c9f674 203 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1 MHz - 1.13 MHz
mbed_official 174:8bb9f3a33240 204 } else if ((hz >= 2000000) && (hz < 4000000)) {
mbed_official 489:119543c9f674 205 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 2 MHz - 2.25 MHz
mbed_official 174:8bb9f3a33240 206 } else if ((hz >= 4000000) && (hz < 8000000)) {
mbed_official 489:119543c9f674 207 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 4 MHz - 4.5 MHz
mbed_official 174:8bb9f3a33240 208 } else if ((hz >= 8000000) && (hz < 16000000)) {
mbed_official 489:119543c9f674 209 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 8 MHz - 9 MHz
mbed_official 174:8bb9f3a33240 210 } else { // >= 16000000
mbed_official 489:119543c9f674 211 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 16 MHz - 18 MHz
mbed_official 139:e3413eddde57 212 }
mbed_official 139:e3413eddde57 213 }
mbed_official 52:a51c77007319 214
mbed_official 56:99eb381a3269 215 init_spi(obj);
mbed_official 52:a51c77007319 216 }
mbed_official 52:a51c77007319 217
mbed_official 402:09075a3b15e3 218 static inline int ssp_readable(spi_t *obj)
mbed_official 402:09075a3b15e3 219 {
mbed_official 52:a51c77007319 220 int status;
mbed_official 489:119543c9f674 221 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 222 // Check if data is received
mbed_official 489:119543c9f674 223 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 174:8bb9f3a33240 224 return status;
mbed_official 52:a51c77007319 225 }
mbed_official 52:a51c77007319 226
mbed_official 402:09075a3b15e3 227 static inline int ssp_writeable(spi_t *obj)
mbed_official 402:09075a3b15e3 228 {
mbed_official 52:a51c77007319 229 int status;
mbed_official 489:119543c9f674 230 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 231 // Check if data is transmitted
mbed_official 489:119543c9f674 232 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 233 return status;
mbed_official 52:a51c77007319 234 }
mbed_official 52:a51c77007319 235
mbed_official 402:09075a3b15e3 236 static inline void ssp_write(spi_t *obj, int value)
mbed_official 402:09075a3b15e3 237 {
mbed_official 174:8bb9f3a33240 238 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 239 while (!ssp_writeable(obj));
mbed_official 489:119543c9f674 240 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 241 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 242 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 243 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 244 *p_spi_dr = (uint8_t)value;
mbed_official 489:119543c9f674 245 } else { // SPI_DATASIZE_16BIT
mbed_official 489:119543c9f674 246 spi->DR = (uint16_t)value;
mbed_official 489:119543c9f674 247 }
mbed_official 52:a51c77007319 248 }
mbed_official 52:a51c77007319 249
mbed_official 402:09075a3b15e3 250 static inline int ssp_read(spi_t *obj)
mbed_official 402:09075a3b15e3 251 {
mbed_official 174:8bb9f3a33240 252 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 253 while (!ssp_readable(obj));
mbed_official 489:119543c9f674 254 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 255 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 256 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 257 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 258 return (int)(*p_spi_dr);
mbed_official 489:119543c9f674 259 } else {
mbed_official 489:119543c9f674 260 return (int)spi->DR;
mbed_official 489:119543c9f674 261 }
mbed_official 52:a51c77007319 262 }
mbed_official 52:a51c77007319 263
mbed_official 402:09075a3b15e3 264 static inline int ssp_busy(spi_t *obj)
mbed_official 402:09075a3b15e3 265 {
mbed_official 52:a51c77007319 266 int status;
mbed_official 489:119543c9f674 267 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 489:119543c9f674 268 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 269 return status;
mbed_official 52:a51c77007319 270 }
mbed_official 52:a51c77007319 271
mbed_official 402:09075a3b15e3 272 int spi_master_write(spi_t *obj, int value)
mbed_official 402:09075a3b15e3 273 {
mbed_official 52:a51c77007319 274 ssp_write(obj, value);
mbed_official 52:a51c77007319 275 return ssp_read(obj);
mbed_official 52:a51c77007319 276 }
mbed_official 52:a51c77007319 277
mbed_official 402:09075a3b15e3 278 int spi_slave_receive(spi_t *obj)
mbed_official 402:09075a3b15e3 279 {
mbed_official 233:1bbc1451db33 280 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 52:a51c77007319 281 };
mbed_official 52:a51c77007319 282
mbed_official 402:09075a3b15e3 283 int spi_slave_read(spi_t *obj)
mbed_official 402:09075a3b15e3 284 {
mbed_official 52:a51c77007319 285 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 489:119543c9f674 286 while (!ssp_readable(obj));
mbed_official 489:119543c9f674 287 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 288 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 289 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 290 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 291 return (int)(*p_spi_dr);
mbed_official 489:119543c9f674 292 } else {
mbed_official 489:119543c9f674 293 return (int)spi->DR;
mbed_official 489:119543c9f674 294 }
mbed_official 52:a51c77007319 295 }
mbed_official 52:a51c77007319 296
mbed_official 402:09075a3b15e3 297 void spi_slave_write(spi_t *obj, int value)
mbed_official 402:09075a3b15e3 298 {
mbed_official 174:8bb9f3a33240 299 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 174:8bb9f3a33240 300 while (!ssp_writeable(obj));
mbed_official 489:119543c9f674 301 if (obj->bits == SPI_DATASIZE_8BIT) {
mbed_official 489:119543c9f674 302 // Force 8-bit access to the data register
mbed_official 489:119543c9f674 303 uint8_t *p_spi_dr = 0;
mbed_official 489:119543c9f674 304 p_spi_dr = (uint8_t *) & (spi->DR);
mbed_official 489:119543c9f674 305 *p_spi_dr = (uint8_t)value;
mbed_official 489:119543c9f674 306 } else { // SPI_DATASIZE_16BIT
mbed_official 489:119543c9f674 307 spi->DR = (uint16_t)value;
mbed_official 489:119543c9f674 308 }
mbed_official 52:a51c77007319 309 }
mbed_official 52:a51c77007319 310
mbed_official 402:09075a3b15e3 311 int spi_busy(spi_t *obj)
mbed_official 402:09075a3b15e3 312 {
mbed_official 52:a51c77007319 313 return ssp_busy(obj);
mbed_official 52:a51c77007319 314 }
mbed_official 52:a51c77007319 315
mbed_official 52:a51c77007319 316 #endif