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