mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F051R8/spi_api.c@227:7bd0639b8911, 2014-06-11 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 11 16:00:09 2014 +0100
- Revision:
- 227:7bd0639b8911
- Parent:
- 130:1dec54e4aec3
- Child:
- 240:9a7c54113eaf
Synchronized with git revision d58d532ebc0e0a96f4fffb8edefc082b71b964af
Full URL: https://github.com/mbedmicro/mbed/commit/d58d532ebc0e0a96f4fffb8edefc082b71b964af/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 130:1dec54e4aec3 | 1 | /* mbed Microcontroller Library |
mbed_official | 130:1dec54e4aec3 | 2 | ******************************************************************************* |
mbed_official | 130:1dec54e4aec3 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 130:1dec54e4aec3 | 4 | * All rights reserved. |
mbed_official | 130:1dec54e4aec3 | 5 | * |
mbed_official | 130:1dec54e4aec3 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 130:1dec54e4aec3 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 130:1dec54e4aec3 | 8 | * |
mbed_official | 130:1dec54e4aec3 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 130:1dec54e4aec3 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 130:1dec54e4aec3 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 130:1dec54e4aec3 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 130:1dec54e4aec3 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 130:1dec54e4aec3 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 130:1dec54e4aec3 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 130:1dec54e4aec3 | 16 | * without specific prior written permission. |
mbed_official | 130:1dec54e4aec3 | 17 | * |
mbed_official | 130:1dec54e4aec3 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 130:1dec54e4aec3 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 130:1dec54e4aec3 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 130:1dec54e4aec3 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 130:1dec54e4aec3 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 130:1dec54e4aec3 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 130:1dec54e4aec3 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 130:1dec54e4aec3 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 130:1dec54e4aec3 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 130:1dec54e4aec3 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 130:1dec54e4aec3 | 28 | ******************************************************************************* |
mbed_official | 130:1dec54e4aec3 | 29 | */ |
mbed_official | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 130:1dec54e4aec3 | 31 | #include "spi_api.h" |
mbed_official | 130:1dec54e4aec3 | 32 | |
mbed_official | 130:1dec54e4aec3 | 33 | #if DEVICE_SPI |
mbed_official | 130:1dec54e4aec3 | 34 | |
mbed_official | 130:1dec54e4aec3 | 35 | #include <math.h> |
mbed_official | 130:1dec54e4aec3 | 36 | #include "cmsis.h" |
mbed_official | 130:1dec54e4aec3 | 37 | #include "pinmap.h" |
mbed_official | 130:1dec54e4aec3 | 38 | |
mbed_official | 130:1dec54e4aec3 | 39 | static const PinMap PinMap_SPI_MOSI[] = { |
mbed_official | 130:1dec54e4aec3 | 40 | {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, |
mbed_official | 130:1dec54e4aec3 | 41 | {NC, NC, 0} |
mbed_official | 130:1dec54e4aec3 | 42 | }; |
mbed_official | 130:1dec54e4aec3 | 43 | |
mbed_official | 130:1dec54e4aec3 | 44 | static const PinMap PinMap_SPI_MISO[] = { |
mbed_official | 130:1dec54e4aec3 | 45 | {PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, |
mbed_official | 130:1dec54e4aec3 | 46 | {NC, NC, 0} |
mbed_official | 130:1dec54e4aec3 | 47 | }; |
mbed_official | 130:1dec54e4aec3 | 48 | |
mbed_official | 130:1dec54e4aec3 | 49 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 130:1dec54e4aec3 | 50 | {PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)}, |
mbed_official | 130:1dec54e4aec3 | 51 | {NC, NC, 0} |
mbed_official | 130:1dec54e4aec3 | 52 | }; |
mbed_official | 130:1dec54e4aec3 | 53 | |
mbed_official | 130:1dec54e4aec3 | 54 | // Only used in Slave mode |
mbed_official | 130:1dec54e4aec3 | 55 | static const PinMap PinMap_SPI_SSEL[] = { |
mbed_official | 130:1dec54e4aec3 | 56 | {PB_6, SPI_1, STM_PIN_DATA(GPIO_Mode_IN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0)}, // Generic IO, not real H/W NSS pin |
mbed_official | 130:1dec54e4aec3 | 57 | {NC, NC, 0} |
mbed_official | 130:1dec54e4aec3 | 58 | }; |
mbed_official | 130:1dec54e4aec3 | 59 | |
mbed_official | 130:1dec54e4aec3 | 60 | static void init_spi(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 61 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 62 | SPI_InitTypeDef SPI_InitStructure; |
mbed_official | 130:1dec54e4aec3 | 63 | |
mbed_official | 130:1dec54e4aec3 | 64 | SPI_Cmd(spi, DISABLE); |
mbed_official | 130:1dec54e4aec3 | 65 | |
mbed_official | 130:1dec54e4aec3 | 66 | SPI_InitStructure.SPI_Mode = obj->mode; |
mbed_official | 227:7bd0639b8911 | 67 | SPI_InitStructure.SPI_NSS = obj->nss; |
mbed_official | 227:7bd0639b8911 | 68 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; |
mbed_official | 130:1dec54e4aec3 | 69 | SPI_InitStructure.SPI_DataSize = obj->bits; |
mbed_official | 130:1dec54e4aec3 | 70 | SPI_InitStructure.SPI_CPOL = obj->cpol; |
mbed_official | 227:7bd0639b8911 | 71 | SPI_InitStructure.SPI_CPHA = obj->cpha; |
mbed_official | 130:1dec54e4aec3 | 72 | SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; |
mbed_official | 130:1dec54e4aec3 | 73 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; |
mbed_official | 130:1dec54e4aec3 | 74 | SPI_InitStructure.SPI_CRCPolynomial = 7; |
mbed_official | 130:1dec54e4aec3 | 75 | SPI_Init(spi, &SPI_InitStructure); |
mbed_official | 130:1dec54e4aec3 | 76 | |
mbed_official | 130:1dec54e4aec3 | 77 | SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF); |
mbed_official | 130:1dec54e4aec3 | 78 | |
mbed_official | 130:1dec54e4aec3 | 79 | SPI_Cmd(spi, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 80 | } |
mbed_official | 130:1dec54e4aec3 | 81 | |
mbed_official | 130:1dec54e4aec3 | 82 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 130:1dec54e4aec3 | 83 | // Determine the SPI to use |
mbed_official | 130:1dec54e4aec3 | 84 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 130:1dec54e4aec3 | 85 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 130:1dec54e4aec3 | 86 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 130:1dec54e4aec3 | 87 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 130:1dec54e4aec3 | 88 | |
mbed_official | 130:1dec54e4aec3 | 89 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 130:1dec54e4aec3 | 90 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 130:1dec54e4aec3 | 91 | |
mbed_official | 130:1dec54e4aec3 | 92 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 227:7bd0639b8911 | 93 | MBED_ASSERT(obj->spi != (SPIName)NC); |
mbed_official | 130:1dec54e4aec3 | 94 | |
mbed_official | 130:1dec54e4aec3 | 95 | // Enable SPI clock |
mbed_official | 130:1dec54e4aec3 | 96 | if (obj->spi == SPI_1) { |
mbed_official | 227:7bd0639b8911 | 97 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 98 | } |
mbed_official | 130:1dec54e4aec3 | 99 | if (obj->spi == SPI_2) { |
mbed_official | 227:7bd0639b8911 | 100 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 130:1dec54e4aec3 | 101 | } |
mbed_official | 130:1dec54e4aec3 | 102 | |
mbed_official | 130:1dec54e4aec3 | 103 | // Configure the SPI pins |
mbed_official | 130:1dec54e4aec3 | 104 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 130:1dec54e4aec3 | 105 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 130:1dec54e4aec3 | 106 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 130:1dec54e4aec3 | 107 | |
mbed_official | 130:1dec54e4aec3 | 108 | // Save new values |
mbed_official | 130:1dec54e4aec3 | 109 | obj->bits = SPI_DataSize_8b; |
mbed_official | 130:1dec54e4aec3 | 110 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 130:1dec54e4aec3 | 111 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 130:1dec54e4aec3 | 112 | obj->br_presc = SPI_BaudRatePrescaler_8; // 1 MHz |
mbed_official | 130:1dec54e4aec3 | 113 | |
mbed_official | 130:1dec54e4aec3 | 114 | if (ssel == NC) { // Master |
mbed_official | 130:1dec54e4aec3 | 115 | obj->mode = SPI_Mode_Master; |
mbed_official | 130:1dec54e4aec3 | 116 | obj->nss = SPI_NSS_Soft; |
mbed_official | 130:1dec54e4aec3 | 117 | } |
mbed_official | 130:1dec54e4aec3 | 118 | else { // Slave |
mbed_official | 130:1dec54e4aec3 | 119 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 130:1dec54e4aec3 | 120 | obj->mode = SPI_Mode_Slave; |
mbed_official | 130:1dec54e4aec3 | 121 | obj->nss = SPI_NSS_Soft; |
mbed_official | 130:1dec54e4aec3 | 122 | } |
mbed_official | 130:1dec54e4aec3 | 123 | |
mbed_official | 130:1dec54e4aec3 | 124 | init_spi(obj); |
mbed_official | 130:1dec54e4aec3 | 125 | } |
mbed_official | 130:1dec54e4aec3 | 126 | |
mbed_official | 130:1dec54e4aec3 | 127 | void spi_free(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 128 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 129 | SPI_I2S_DeInit(spi); |
mbed_official | 130:1dec54e4aec3 | 130 | } |
mbed_official | 130:1dec54e4aec3 | 131 | |
mbed_official | 227:7bd0639b8911 | 132 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 130:1dec54e4aec3 | 133 | // Save new values |
mbed_official | 130:1dec54e4aec3 | 134 | if (bits == 8) { |
mbed_official | 130:1dec54e4aec3 | 135 | obj->bits = SPI_DataSize_8b; |
mbed_official | 130:1dec54e4aec3 | 136 | } |
mbed_official | 130:1dec54e4aec3 | 137 | else { |
mbed_official | 130:1dec54e4aec3 | 138 | obj->bits = SPI_DataSize_16b; |
mbed_official | 130:1dec54e4aec3 | 139 | } |
mbed_official | 130:1dec54e4aec3 | 140 | |
mbed_official | 130:1dec54e4aec3 | 141 | switch (mode) { |
mbed_official | 130:1dec54e4aec3 | 142 | case 0: |
mbed_official | 130:1dec54e4aec3 | 143 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 130:1dec54e4aec3 | 144 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 130:1dec54e4aec3 | 145 | break; |
mbed_official | 130:1dec54e4aec3 | 146 | case 1: |
mbed_official | 130:1dec54e4aec3 | 147 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 130:1dec54e4aec3 | 148 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 130:1dec54e4aec3 | 149 | break; |
mbed_official | 130:1dec54e4aec3 | 150 | case 2: |
mbed_official | 130:1dec54e4aec3 | 151 | obj->cpol = SPI_CPOL_High; |
mbed_official | 227:7bd0639b8911 | 152 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 130:1dec54e4aec3 | 153 | break; |
mbed_official | 130:1dec54e4aec3 | 154 | default: |
mbed_official | 130:1dec54e4aec3 | 155 | obj->cpol = SPI_CPOL_High; |
mbed_official | 227:7bd0639b8911 | 156 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 130:1dec54e4aec3 | 157 | break; |
mbed_official | 130:1dec54e4aec3 | 158 | } |
mbed_official | 130:1dec54e4aec3 | 159 | |
mbed_official | 130:1dec54e4aec3 | 160 | if (slave == 0) { |
mbed_official | 130:1dec54e4aec3 | 161 | obj->mode = SPI_Mode_Master; |
mbed_official | 130:1dec54e4aec3 | 162 | obj->nss = SPI_NSS_Soft; |
mbed_official | 130:1dec54e4aec3 | 163 | } |
mbed_official | 130:1dec54e4aec3 | 164 | else { |
mbed_official | 130:1dec54e4aec3 | 165 | obj->mode = SPI_Mode_Slave; |
mbed_official | 227:7bd0639b8911 | 166 | obj->nss = SPI_NSS_Hard; |
mbed_official | 130:1dec54e4aec3 | 167 | } |
mbed_official | 130:1dec54e4aec3 | 168 | |
mbed_official | 130:1dec54e4aec3 | 169 | init_spi(obj); |
mbed_official | 130:1dec54e4aec3 | 170 | } |
mbed_official | 130:1dec54e4aec3 | 171 | |
mbed_official | 130:1dec54e4aec3 | 172 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 130:1dec54e4aec3 | 173 | // Get SPI clock frequency |
mbed_official | 130:1dec54e4aec3 | 174 | uint32_t PCLK = SystemCoreClock; |
mbed_official | 130:1dec54e4aec3 | 175 | |
mbed_official | 130:1dec54e4aec3 | 176 | // Choose the baud rate divisor (between 2 and 256) |
mbed_official | 130:1dec54e4aec3 | 177 | uint32_t divisor = PCLK / hz; |
mbed_official | 130:1dec54e4aec3 | 178 | |
mbed_official | 130:1dec54e4aec3 | 179 | // Find the nearest power-of-2 |
mbed_official | 130:1dec54e4aec3 | 180 | divisor = (divisor > 0 ? divisor-1 : 0); |
mbed_official | 130:1dec54e4aec3 | 181 | divisor |= divisor >> 1; |
mbed_official | 130:1dec54e4aec3 | 182 | divisor |= divisor >> 2; |
mbed_official | 130:1dec54e4aec3 | 183 | divisor |= divisor >> 4; |
mbed_official | 130:1dec54e4aec3 | 184 | divisor |= divisor >> 8; |
mbed_official | 130:1dec54e4aec3 | 185 | divisor |= divisor >> 16; |
mbed_official | 130:1dec54e4aec3 | 186 | divisor++; |
mbed_official | 130:1dec54e4aec3 | 187 | |
mbed_official | 130:1dec54e4aec3 | 188 | uint32_t baud_rate = __builtin_ffs(divisor) - 2; |
mbed_official | 130:1dec54e4aec3 | 189 | |
mbed_official | 130:1dec54e4aec3 | 190 | // Save new value |
mbed_official | 130:1dec54e4aec3 | 191 | obj->br_presc = ((baud_rate > 7) ? (7 << 3) : (baud_rate << 3)); |
mbed_official | 130:1dec54e4aec3 | 192 | |
mbed_official | 130:1dec54e4aec3 | 193 | init_spi(obj); |
mbed_official | 130:1dec54e4aec3 | 194 | } |
mbed_official | 130:1dec54e4aec3 | 195 | |
mbed_official | 130:1dec54e4aec3 | 196 | static inline int ssp_readable(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 197 | int status; |
mbed_official | 130:1dec54e4aec3 | 198 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 199 | // Check if data is received |
mbed_official | 130:1dec54e4aec3 | 200 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 227:7bd0639b8911 | 201 | return status; |
mbed_official | 130:1dec54e4aec3 | 202 | } |
mbed_official | 130:1dec54e4aec3 | 203 | |
mbed_official | 130:1dec54e4aec3 | 204 | static inline int ssp_writeable(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 205 | int status; |
mbed_official | 130:1dec54e4aec3 | 206 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 207 | // Check if data is transmitted |
mbed_official | 130:1dec54e4aec3 | 208 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 130:1dec54e4aec3 | 209 | return status; |
mbed_official | 130:1dec54e4aec3 | 210 | } |
mbed_official | 130:1dec54e4aec3 | 211 | |
mbed_official | 130:1dec54e4aec3 | 212 | static inline void ssp_write(spi_t *obj, int value) { |
mbed_official | 227:7bd0639b8911 | 213 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 214 | while (!ssp_writeable(obj)); |
mbed_official | 130:1dec54e4aec3 | 215 | if(obj->bits == SPI_DataSize_8b) // 8 bit mode |
mbed_official | 130:1dec54e4aec3 | 216 | SPI_SendData8(spi, (uint8_t)value); |
mbed_official | 130:1dec54e4aec3 | 217 | else |
mbed_official | 130:1dec54e4aec3 | 218 | SPI_I2S_SendData16(spi, (uint16_t)value); |
mbed_official | 130:1dec54e4aec3 | 219 | } |
mbed_official | 130:1dec54e4aec3 | 220 | |
mbed_official | 130:1dec54e4aec3 | 221 | static inline int ssp_read(spi_t *obj) { |
mbed_official | 227:7bd0639b8911 | 222 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 223 | while (!ssp_readable(obj)); |
mbed_official | 130:1dec54e4aec3 | 224 | if(obj->bits == SPI_DataSize_8b) // 8 bit mode |
mbed_official | 130:1dec54e4aec3 | 225 | return (int)SPI_ReceiveData8(spi); |
mbed_official | 130:1dec54e4aec3 | 226 | else // 16 bit mode |
mbed_official | 227:7bd0639b8911 | 227 | return (int)SPI_I2S_ReceiveData16(spi); |
mbed_official | 130:1dec54e4aec3 | 228 | } |
mbed_official | 130:1dec54e4aec3 | 229 | |
mbed_official | 130:1dec54e4aec3 | 230 | static inline int ssp_busy(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 231 | int status; |
mbed_official | 130:1dec54e4aec3 | 232 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 233 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 130:1dec54e4aec3 | 234 | return status; |
mbed_official | 130:1dec54e4aec3 | 235 | } |
mbed_official | 130:1dec54e4aec3 | 236 | |
mbed_official | 130:1dec54e4aec3 | 237 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 130:1dec54e4aec3 | 238 | ssp_write(obj, value); |
mbed_official | 130:1dec54e4aec3 | 239 | return ssp_read(obj); |
mbed_official | 130:1dec54e4aec3 | 240 | } |
mbed_official | 130:1dec54e4aec3 | 241 | |
mbed_official | 130:1dec54e4aec3 | 242 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 243 | return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); |
mbed_official | 130:1dec54e4aec3 | 244 | }; |
mbed_official | 130:1dec54e4aec3 | 245 | |
mbed_official | 130:1dec54e4aec3 | 246 | int spi_slave_read(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 247 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 130:1dec54e4aec3 | 248 | if(obj->bits == SPI_DataSize_8b) // 8 bit mode |
mbed_official | 130:1dec54e4aec3 | 249 | return (int)SPI_ReceiveData8(spi); |
mbed_official | 227:7bd0639b8911 | 250 | else |
mbed_official | 227:7bd0639b8911 | 251 | return (int)SPI_I2S_ReceiveData16(spi); |
mbed_official | 130:1dec54e4aec3 | 252 | } |
mbed_official | 130:1dec54e4aec3 | 253 | |
mbed_official | 130:1dec54e4aec3 | 254 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 227:7bd0639b8911 | 255 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 227:7bd0639b8911 | 256 | while (!ssp_writeable(obj)); |
mbed_official | 130:1dec54e4aec3 | 257 | if(obj->bits == SPI_DataSize_8b) // 8 bit mode |
mbed_official | 130:1dec54e4aec3 | 258 | SPI_SendData8(spi, (uint8_t)value); |
mbed_official | 227:7bd0639b8911 | 259 | else |
mbed_official | 130:1dec54e4aec3 | 260 | SPI_I2S_SendData16(spi, (uint16_t)value); |
mbed_official | 130:1dec54e4aec3 | 261 | } |
mbed_official | 130:1dec54e4aec3 | 262 | |
mbed_official | 130:1dec54e4aec3 | 263 | int spi_busy(spi_t *obj) { |
mbed_official | 130:1dec54e4aec3 | 264 | return ssp_busy(obj); |
mbed_official | 130:1dec54e4aec3 | 265 | } |
mbed_official | 130:1dec54e4aec3 | 266 | |
mbed_official | 130:1dec54e4aec3 | 267 | #endif |