Mbed for VNG board
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c@414:4ec4c5b614b0, 2014-11-25 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 25 07:15:09 2014 +0000
- Revision:
- 414:4ec4c5b614b0
- Parent:
- 402:09075a3b15e3
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?
User | Revision | Line number | New 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 | 402:09075a3b15e3 | 40 | static void init_spi(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 41 | { |
mbed_official | 56:99eb381a3269 | 42 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 56:99eb381a3269 | 43 | SPI_InitTypeDef SPI_InitStructure; |
mbed_official | 56:99eb381a3269 | 44 | |
mbed_official | 56:99eb381a3269 | 45 | SPI_Cmd(spi, DISABLE); |
mbed_official | 52:a51c77007319 | 46 | |
mbed_official | 139:e3413eddde57 | 47 | SPI_InitStructure.SPI_Mode = obj->mode; |
mbed_official | 139:e3413eddde57 | 48 | SPI_InitStructure.SPI_NSS = obj->nss; |
mbed_official | 174:8bb9f3a33240 | 49 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; |
mbed_official | 139:e3413eddde57 | 50 | SPI_InitStructure.SPI_DataSize = obj->bits; |
mbed_official | 139:e3413eddde57 | 51 | SPI_InitStructure.SPI_CPOL = obj->cpol; |
mbed_official | 139:e3413eddde57 | 52 | SPI_InitStructure.SPI_CPHA = obj->cpha; |
mbed_official | 56:99eb381a3269 | 53 | SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; |
mbed_official | 139:e3413eddde57 | 54 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; |
mbed_official | 139:e3413eddde57 | 55 | SPI_InitStructure.SPI_CRCPolynomial = 7; |
mbed_official | 56:99eb381a3269 | 56 | SPI_Init(spi, &SPI_InitStructure); |
mbed_official | 56:99eb381a3269 | 57 | |
mbed_official | 56:99eb381a3269 | 58 | SPI_Cmd(spi, ENABLE); |
mbed_official | 56:99eb381a3269 | 59 | } |
mbed_official | 56:99eb381a3269 | 60 | |
mbed_official | 402:09075a3b15e3 | 61 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) |
mbed_official | 402:09075a3b15e3 | 62 | { |
mbed_official | 52:a51c77007319 | 63 | // Determine the SPI to use |
mbed_official | 52:a51c77007319 | 64 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 52:a51c77007319 | 65 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 52:a51c77007319 | 66 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 52:a51c77007319 | 67 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 174:8bb9f3a33240 | 68 | |
mbed_official | 52:a51c77007319 | 69 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 52:a51c77007319 | 70 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 174:8bb9f3a33240 | 71 | |
mbed_official | 52:a51c77007319 | 72 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 227:7bd0639b8911 | 73 | MBED_ASSERT(obj->spi != (SPIName)NC); |
mbed_official | 174:8bb9f3a33240 | 74 | |
mbed_official | 52:a51c77007319 | 75 | // Enable SPI clock |
mbed_official | 52:a51c77007319 | 76 | if (obj->spi == SPI_1) { |
mbed_official | 174:8bb9f3a33240 | 77 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); |
mbed_official | 52:a51c77007319 | 78 | } |
mbed_official | 139:e3413eddde57 | 79 | if (obj->spi == SPI_2) { |
mbed_official | 139:e3413eddde57 | 80 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 139:e3413eddde57 | 81 | } |
mbed_official | 139:e3413eddde57 | 82 | |
mbed_official | 52:a51c77007319 | 83 | // Configure the SPI pins |
mbed_official | 52:a51c77007319 | 84 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 52:a51c77007319 | 85 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 52:a51c77007319 | 86 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 174:8bb9f3a33240 | 87 | |
mbed_official | 52:a51c77007319 | 88 | // Save new values |
mbed_official | 52:a51c77007319 | 89 | obj->bits = SPI_DataSize_8b; |
mbed_official | 52:a51c77007319 | 90 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 52:a51c77007319 | 91 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 139:e3413eddde57 | 92 | obj->br_presc = SPI_BaudRatePrescaler_256; |
mbed_official | 174:8bb9f3a33240 | 93 | |
mbed_official | 208:4557f4bb2dd5 | 94 | obj->pin_miso = miso; |
mbed_official | 208:4557f4bb2dd5 | 95 | obj->pin_mosi = mosi; |
mbed_official | 208:4557f4bb2dd5 | 96 | obj->pin_sclk = sclk; |
mbed_official | 208:4557f4bb2dd5 | 97 | obj->pin_ssel = ssel; |
mbed_official | 208:4557f4bb2dd5 | 98 | |
mbed_official | 52:a51c77007319 | 99 | if (ssel == NC) { // Master |
mbed_official | 52:a51c77007319 | 100 | obj->mode = SPI_Mode_Master; |
mbed_official | 52:a51c77007319 | 101 | obj->nss = SPI_NSS_Soft; |
mbed_official | 174:8bb9f3a33240 | 102 | } else { // Slave |
mbed_official | 52:a51c77007319 | 103 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 52:a51c77007319 | 104 | obj->mode = SPI_Mode_Slave; |
mbed_official | 181:a4cbdfbbd2f4 | 105 | obj->nss = SPI_NSS_Hard; |
mbed_official | 52:a51c77007319 | 106 | } |
mbed_official | 52:a51c77007319 | 107 | |
mbed_official | 56:99eb381a3269 | 108 | init_spi(obj); |
mbed_official | 52:a51c77007319 | 109 | } |
mbed_official | 52:a51c77007319 | 110 | |
mbed_official | 402:09075a3b15e3 | 111 | void spi_free(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 112 | { |
mbed_official | 208:4557f4bb2dd5 | 113 | // Reset SPI and disable clock |
mbed_official | 208:4557f4bb2dd5 | 114 | if (obj->spi == SPI_1) { |
mbed_official | 208:4557f4bb2dd5 | 115 | RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); |
mbed_official | 208:4557f4bb2dd5 | 116 | RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE); |
mbed_official | 208:4557f4bb2dd5 | 117 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE); |
mbed_official | 208:4557f4bb2dd5 | 118 | } |
mbed_official | 208:4557f4bb2dd5 | 119 | |
mbed_official | 208:4557f4bb2dd5 | 120 | if (obj->spi == SPI_2) { |
mbed_official | 208:4557f4bb2dd5 | 121 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 208:4557f4bb2dd5 | 122 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE); |
mbed_official | 208:4557f4bb2dd5 | 123 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, DISABLE); |
mbed_official | 208:4557f4bb2dd5 | 124 | } |
mbed_official | 208:4557f4bb2dd5 | 125 | |
mbed_official | 208:4557f4bb2dd5 | 126 | // Configure GPIOs |
mbed_official | 208:4557f4bb2dd5 | 127 | pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); |
mbed_official | 208:4557f4bb2dd5 | 128 | pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); |
mbed_official | 208:4557f4bb2dd5 | 129 | pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); |
mbed_official | 208:4557f4bb2dd5 | 130 | pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)); |
mbed_official | 52:a51c77007319 | 131 | } |
mbed_official | 52:a51c77007319 | 132 | |
mbed_official | 402:09075a3b15e3 | 133 | void spi_format(spi_t *obj, int bits, int mode, int slave) |
mbed_official | 402:09075a3b15e3 | 134 | { |
mbed_official | 52:a51c77007319 | 135 | // Save new values |
mbed_official | 233:1bbc1451db33 | 136 | if (bits == 16) { |
mbed_official | 233:1bbc1451db33 | 137 | obj->bits = SPI_DataSize_16b; |
mbed_official | 233:1bbc1451db33 | 138 | } else { |
mbed_official | 52:a51c77007319 | 139 | obj->bits = SPI_DataSize_8b; |
mbed_official | 52:a51c77007319 | 140 | } |
mbed_official | 174:8bb9f3a33240 | 141 | |
mbed_official | 52:a51c77007319 | 142 | switch (mode) { |
mbed_official | 52:a51c77007319 | 143 | case 0: |
mbed_official | 174:8bb9f3a33240 | 144 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 174:8bb9f3a33240 | 145 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 174:8bb9f3a33240 | 146 | break; |
mbed_official | 52:a51c77007319 | 147 | case 1: |
mbed_official | 174:8bb9f3a33240 | 148 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 174:8bb9f3a33240 | 149 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 174:8bb9f3a33240 | 150 | break; |
mbed_official | 52:a51c77007319 | 151 | case 2: |
mbed_official | 174:8bb9f3a33240 | 152 | obj->cpol = SPI_CPOL_High; |
mbed_official | 174:8bb9f3a33240 | 153 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 174:8bb9f3a33240 | 154 | break; |
mbed_official | 52:a51c77007319 | 155 | default: |
mbed_official | 174:8bb9f3a33240 | 156 | obj->cpol = SPI_CPOL_High; |
mbed_official | 174:8bb9f3a33240 | 157 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 174:8bb9f3a33240 | 158 | break; |
mbed_official | 52:a51c77007319 | 159 | } |
mbed_official | 174:8bb9f3a33240 | 160 | |
mbed_official | 52:a51c77007319 | 161 | if (slave == 0) { |
mbed_official | 52:a51c77007319 | 162 | obj->mode = SPI_Mode_Master; |
mbed_official | 52:a51c77007319 | 163 | obj->nss = SPI_NSS_Soft; |
mbed_official | 174:8bb9f3a33240 | 164 | } else { |
mbed_official | 52:a51c77007319 | 165 | obj->mode = SPI_Mode_Slave; |
mbed_official | 174:8bb9f3a33240 | 166 | obj->nss = SPI_NSS_Hard; |
mbed_official | 52:a51c77007319 | 167 | } |
mbed_official | 174:8bb9f3a33240 | 168 | |
mbed_official | 56:99eb381a3269 | 169 | init_spi(obj); |
mbed_official | 52:a51c77007319 | 170 | } |
mbed_official | 52:a51c77007319 | 171 | |
mbed_official | 402:09075a3b15e3 | 172 | void spi_frequency(spi_t *obj, int hz) |
mbed_official | 402:09075a3b15e3 | 173 | { |
mbed_official | 139:e3413eddde57 | 174 | if (obj->spi == SPI_1) { |
mbed_official | 139:e3413eddde57 | 175 | // Values depend of PCLK2: 64 MHz if HSI is used, 72 MHz if HSE is used |
mbed_official | 139:e3413eddde57 | 176 | if (hz < 500000) { |
mbed_official | 139:e3413eddde57 | 177 | obj->br_presc = SPI_BaudRatePrescaler_256; // 250 kHz - 281 kHz |
mbed_official | 174:8bb9f3a33240 | 178 | } else if ((hz >= 500000) && (hz < 1000000)) { |
mbed_official | 139:e3413eddde57 | 179 | obj->br_presc = SPI_BaudRatePrescaler_128; // 500 kHz - 563 kHz |
mbed_official | 174:8bb9f3a33240 | 180 | } else if ((hz >= 1000000) && (hz < 2000000)) { |
mbed_official | 139:e3413eddde57 | 181 | obj->br_presc = SPI_BaudRatePrescaler_64; // 1 MHz - 1.13 MHz |
mbed_official | 174:8bb9f3a33240 | 182 | } else if ((hz >= 2000000) && (hz < 4000000)) { |
mbed_official | 139:e3413eddde57 | 183 | obj->br_presc = SPI_BaudRatePrescaler_32; // 2 MHz - 2.25 MHz |
mbed_official | 174:8bb9f3a33240 | 184 | } else if ((hz >= 4000000) && (hz < 8000000)) { |
mbed_official | 139:e3413eddde57 | 185 | obj->br_presc = SPI_BaudRatePrescaler_16; // 4 MHz - 4.5 MHz |
mbed_official | 174:8bb9f3a33240 | 186 | } else if ((hz >= 8000000) && (hz < 16000000)) { |
mbed_official | 139:e3413eddde57 | 187 | obj->br_presc = SPI_BaudRatePrescaler_8; // 8 MHz - 9 MHz |
mbed_official | 174:8bb9f3a33240 | 188 | } else if ((hz >= 16000000) && (hz < 32000000)) { |
mbed_official | 139:e3413eddde57 | 189 | obj->br_presc = SPI_BaudRatePrescaler_4; // 16 MHz - 18 MHz |
mbed_official | 174:8bb9f3a33240 | 190 | } else { // >= 32000000 |
mbed_official | 139:e3413eddde57 | 191 | obj->br_presc = SPI_BaudRatePrescaler_2; // 32 MHz - 36 MHz |
mbed_official | 139:e3413eddde57 | 192 | } |
mbed_official | 139:e3413eddde57 | 193 | } |
mbed_official | 174:8bb9f3a33240 | 194 | |
mbed_official | 139:e3413eddde57 | 195 | if (obj->spi == SPI_2) { |
mbed_official | 139:e3413eddde57 | 196 | // Values depend of PCLK1: 32 MHz if HSI is used, 36 MHz if HSE is used |
mbed_official | 139:e3413eddde57 | 197 | if (hz < 250000) { |
mbed_official | 139:e3413eddde57 | 198 | obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz - 141 kHz |
mbed_official | 174:8bb9f3a33240 | 199 | } else if ((hz >= 250000) && (hz < 500000)) { |
mbed_official | 139:e3413eddde57 | 200 | obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz - 281 kHz |
mbed_official | 174:8bb9f3a33240 | 201 | } else if ((hz >= 500000) && (hz < 1000000)) { |
mbed_official | 139:e3413eddde57 | 202 | obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz - 563 kHz |
mbed_official | 174:8bb9f3a33240 | 203 | } else if ((hz >= 1000000) && (hz < 2000000)) { |
mbed_official | 139:e3413eddde57 | 204 | obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz - 1.13 MHz |
mbed_official | 174:8bb9f3a33240 | 205 | } else if ((hz >= 2000000) && (hz < 4000000)) { |
mbed_official | 139:e3413eddde57 | 206 | obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz - 2.25 MHz |
mbed_official | 174:8bb9f3a33240 | 207 | } else if ((hz >= 4000000) && (hz < 8000000)) { |
mbed_official | 139:e3413eddde57 | 208 | obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz - 4.5 MHz |
mbed_official | 174:8bb9f3a33240 | 209 | } else if ((hz >= 8000000) && (hz < 16000000)) { |
mbed_official | 139:e3413eddde57 | 210 | obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz - 9 MHz |
mbed_official | 174:8bb9f3a33240 | 211 | } else { // >= 16000000 |
mbed_official | 139:e3413eddde57 | 212 | obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz - 18 MHz |
mbed_official | 139:e3413eddde57 | 213 | } |
mbed_official | 139:e3413eddde57 | 214 | } |
mbed_official | 52:a51c77007319 | 215 | |
mbed_official | 56:99eb381a3269 | 216 | init_spi(obj); |
mbed_official | 52:a51c77007319 | 217 | } |
mbed_official | 52:a51c77007319 | 218 | |
mbed_official | 402:09075a3b15e3 | 219 | static inline int ssp_readable(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 220 | { |
mbed_official | 52:a51c77007319 | 221 | int status; |
mbed_official | 52:a51c77007319 | 222 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 52:a51c77007319 | 223 | // Check if data is received |
mbed_official | 52:a51c77007319 | 224 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 174:8bb9f3a33240 | 225 | return status; |
mbed_official | 52:a51c77007319 | 226 | } |
mbed_official | 52:a51c77007319 | 227 | |
mbed_official | 402:09075a3b15e3 | 228 | static inline int ssp_writeable(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 229 | { |
mbed_official | 52:a51c77007319 | 230 | int status; |
mbed_official | 52:a51c77007319 | 231 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 52:a51c77007319 | 232 | // Check if data is transmitted |
mbed_official | 52:a51c77007319 | 233 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 52:a51c77007319 | 234 | return status; |
mbed_official | 52:a51c77007319 | 235 | } |
mbed_official | 52:a51c77007319 | 236 | |
mbed_official | 402:09075a3b15e3 | 237 | static inline void ssp_write(spi_t *obj, int value) |
mbed_official | 402:09075a3b15e3 | 238 | { |
mbed_official | 174:8bb9f3a33240 | 239 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 52:a51c77007319 | 240 | while (!ssp_writeable(obj)); |
mbed_official | 52:a51c77007319 | 241 | SPI_I2S_SendData(spi, (uint16_t)value); |
mbed_official | 52:a51c77007319 | 242 | } |
mbed_official | 52:a51c77007319 | 243 | |
mbed_official | 402:09075a3b15e3 | 244 | static inline int ssp_read(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 245 | { |
mbed_official | 174:8bb9f3a33240 | 246 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 52:a51c77007319 | 247 | while (!ssp_readable(obj)); |
mbed_official | 52:a51c77007319 | 248 | return (int)SPI_I2S_ReceiveData(spi); |
mbed_official | 52:a51c77007319 | 249 | } |
mbed_official | 52:a51c77007319 | 250 | |
mbed_official | 402:09075a3b15e3 | 251 | static inline int ssp_busy(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 252 | { |
mbed_official | 52:a51c77007319 | 253 | int status; |
mbed_official | 52:a51c77007319 | 254 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 52:a51c77007319 | 255 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 52:a51c77007319 | 256 | return status; |
mbed_official | 52:a51c77007319 | 257 | } |
mbed_official | 52:a51c77007319 | 258 | |
mbed_official | 402:09075a3b15e3 | 259 | int spi_master_write(spi_t *obj, int value) |
mbed_official | 402:09075a3b15e3 | 260 | { |
mbed_official | 52:a51c77007319 | 261 | ssp_write(obj, value); |
mbed_official | 52:a51c77007319 | 262 | return ssp_read(obj); |
mbed_official | 52:a51c77007319 | 263 | } |
mbed_official | 52:a51c77007319 | 264 | |
mbed_official | 402:09075a3b15e3 | 265 | int spi_slave_receive(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 266 | { |
mbed_official | 233:1bbc1451db33 | 267 | return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0); |
mbed_official | 52:a51c77007319 | 268 | }; |
mbed_official | 52:a51c77007319 | 269 | |
mbed_official | 402:09075a3b15e3 | 270 | int spi_slave_read(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 271 | { |
mbed_official | 52:a51c77007319 | 272 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 52:a51c77007319 | 273 | return (int)SPI_I2S_ReceiveData(spi); |
mbed_official | 52:a51c77007319 | 274 | } |
mbed_official | 52:a51c77007319 | 275 | |
mbed_official | 402:09075a3b15e3 | 276 | void spi_slave_write(spi_t *obj, int value) |
mbed_official | 402:09075a3b15e3 | 277 | { |
mbed_official | 174:8bb9f3a33240 | 278 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 174:8bb9f3a33240 | 279 | while (!ssp_writeable(obj)); |
mbed_official | 52:a51c77007319 | 280 | SPI_I2S_SendData(spi, (uint16_t)value); |
mbed_official | 52:a51c77007319 | 281 | } |
mbed_official | 52:a51c77007319 | 282 | |
mbed_official | 402:09075a3b15e3 | 283 | int spi_busy(spi_t *obj) |
mbed_official | 402:09075a3b15e3 | 284 | { |
mbed_official | 52:a51c77007319 | 285 | return ssp_busy(obj); |
mbed_official | 52:a51c77007319 | 286 | } |
mbed_official | 52:a51c77007319 | 287 | |
mbed_official | 52:a51c77007319 | 288 | #endif |