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