mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/spi_api.c@215:83cf97a28428, 2014-05-27 (annotated)
- Committer:
- mbed_official
- Date:
- Tue May 27 08:30:06 2014 +0100
- Revision:
- 215:83cf97a28428
- Parent:
- 190:bde2479eef9e
- Child:
- 227:7bd0639b8911
Synchronized with git revision a8b7f5df2cb5d7a76fda549a15b91fdaba6e2986
Full URL: https://github.com/mbedmicro/mbed/commit/a8b7f5df2cb5d7a76fda549a15b91fdaba6e2986/
[KLXX] Added RPC + RTOS support (KL05Z)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 76:aeb1df146756 | 1 | /* mbed Microcontroller Library |
mbed_official | 76:aeb1df146756 | 2 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 76:aeb1df146756 | 4 | * All rights reserved. |
mbed_official | 76:aeb1df146756 | 5 | * |
mbed_official | 76:aeb1df146756 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 76:aeb1df146756 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 76:aeb1df146756 | 8 | * |
mbed_official | 76:aeb1df146756 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 76:aeb1df146756 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 76:aeb1df146756 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 76:aeb1df146756 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 76:aeb1df146756 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 76:aeb1df146756 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 76:aeb1df146756 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 76:aeb1df146756 | 16 | * without specific prior written permission. |
mbed_official | 76:aeb1df146756 | 17 | * |
mbed_official | 76:aeb1df146756 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 76:aeb1df146756 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 76:aeb1df146756 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 76:aeb1df146756 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 76:aeb1df146756 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 76:aeb1df146756 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 76:aeb1df146756 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 76:aeb1df146756 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 76:aeb1df146756 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 76:aeb1df146756 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 76:aeb1df146756 | 28 | ******************************************************************************* |
mbed_official | 76:aeb1df146756 | 29 | */ |
mbed_official | 76:aeb1df146756 | 30 | #include "spi_api.h" |
mbed_official | 76:aeb1df146756 | 31 | |
mbed_official | 76:aeb1df146756 | 32 | #if DEVICE_SPI |
mbed_official | 76:aeb1df146756 | 33 | |
mbed_official | 76:aeb1df146756 | 34 | #include <math.h> |
mbed_official | 76:aeb1df146756 | 35 | #include "cmsis.h" |
mbed_official | 76:aeb1df146756 | 36 | #include "pinmap.h" |
mbed_official | 76:aeb1df146756 | 37 | #include "error.h" |
mbed_official | 76:aeb1df146756 | 38 | |
mbed_official | 76:aeb1df146756 | 39 | static const PinMap PinMap_SPI_MOSI[] = { |
mbed_official | 174:8bb9f3a33240 | 40 | {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 129:0182c99221bc | 41 | {PA_12, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 129:0182c99221bc | 42 | {PB_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 174:8bb9f3a33240 | 43 | // {PB_5, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 129:0182c99221bc | 44 | {PB_15, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 129:0182c99221bc | 45 | {PC_12, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 76:aeb1df146756 | 46 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 47 | }; |
mbed_official | 76:aeb1df146756 | 48 | |
mbed_official | 76:aeb1df146756 | 49 | static const PinMap PinMap_SPI_MISO[] = { |
mbed_official | 174:8bb9f3a33240 | 50 | {PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 129:0182c99221bc | 51 | {PA_11, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 129:0182c99221bc | 52 | {PB_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 174:8bb9f3a33240 | 53 | // {PB_4, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 118:b44c45162f28 | 54 | {PB_14, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 118:b44c45162f28 | 55 | {PC_11, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 76:aeb1df146756 | 56 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 57 | }; |
mbed_official | 76:aeb1df146756 | 58 | |
mbed_official | 76:aeb1df146756 | 59 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 174:8bb9f3a33240 | 60 | {PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 129:0182c99221bc | 61 | {PB_3, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 174:8bb9f3a33240 | 62 | // {PB_3, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 118:b44c45162f28 | 63 | {PB_13, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 118:b44c45162f28 | 64 | {PC_10, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 76:aeb1df146756 | 65 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 66 | }; |
mbed_official | 76:aeb1df146756 | 67 | |
mbed_official | 76:aeb1df146756 | 68 | static const PinMap PinMap_SPI_SSEL[] = { |
mbed_official | 129:0182c99221bc | 69 | {PA_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 174:8bb9f3a33240 | 70 | // {PA_4, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 129:0182c99221bc | 71 | {PA_15, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 174:8bb9f3a33240 | 72 | // {PA_15, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 118:b44c45162f28 | 73 | {PB_12, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 76:aeb1df146756 | 74 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 75 | }; |
mbed_official | 76:aeb1df146756 | 76 | |
mbed_official | 76:aeb1df146756 | 77 | static void init_spi(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 78 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 79 | SPI_InitTypeDef SPI_InitStructure; |
mbed_official | 76:aeb1df146756 | 80 | |
mbed_official | 76:aeb1df146756 | 81 | SPI_Cmd(spi, DISABLE); |
mbed_official | 76:aeb1df146756 | 82 | |
mbed_official | 129:0182c99221bc | 83 | SPI_InitStructure.SPI_Mode = obj->mode; |
mbed_official | 174:8bb9f3a33240 | 84 | SPI_InitStructure.SPI_NSS = obj->nss; |
mbed_official | 174:8bb9f3a33240 | 85 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; |
mbed_official | 129:0182c99221bc | 86 | SPI_InitStructure.SPI_DataSize = obj->bits; |
mbed_official | 129:0182c99221bc | 87 | SPI_InitStructure.SPI_CPOL = obj->cpol; |
mbed_official | 174:8bb9f3a33240 | 88 | SPI_InitStructure.SPI_CPHA = obj->cpha; |
mbed_official | 76:aeb1df146756 | 89 | SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; |
mbed_official | 129:0182c99221bc | 90 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; |
mbed_official | 129:0182c99221bc | 91 | SPI_InitStructure.SPI_CRCPolynomial = 7; |
mbed_official | 76:aeb1df146756 | 92 | SPI_Init(spi, &SPI_InitStructure); |
mbed_official | 76:aeb1df146756 | 93 | |
mbed_official | 76:aeb1df146756 | 94 | SPI_Cmd(spi, ENABLE); |
mbed_official | 76:aeb1df146756 | 95 | } |
mbed_official | 76:aeb1df146756 | 96 | |
mbed_official | 76:aeb1df146756 | 97 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 76:aeb1df146756 | 98 | // Determine the SPI to use |
mbed_official | 76:aeb1df146756 | 99 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 76:aeb1df146756 | 100 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 76:aeb1df146756 | 101 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 76:aeb1df146756 | 102 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 174:8bb9f3a33240 | 103 | |
mbed_official | 76:aeb1df146756 | 104 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 76:aeb1df146756 | 105 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 174:8bb9f3a33240 | 106 | |
mbed_official | 76:aeb1df146756 | 107 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 174:8bb9f3a33240 | 108 | |
mbed_official | 76:aeb1df146756 | 109 | if (obj->spi == (SPIName)NC) { |
mbed_official | 76:aeb1df146756 | 110 | error("SPI pinout mapping failed"); |
mbed_official | 76:aeb1df146756 | 111 | } |
mbed_official | 174:8bb9f3a33240 | 112 | |
mbed_official | 76:aeb1df146756 | 113 | // Enable SPI clock |
mbed_official | 76:aeb1df146756 | 114 | if (obj->spi == SPI_1) { |
mbed_official | 174:8bb9f3a33240 | 115 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); |
mbed_official | 76:aeb1df146756 | 116 | } |
mbed_official | 76:aeb1df146756 | 117 | if (obj->spi == SPI_2) { |
mbed_official | 174:8bb9f3a33240 | 118 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 76:aeb1df146756 | 119 | } |
mbed_official | 118:b44c45162f28 | 120 | if (obj->spi == SPI_3) { |
mbed_official | 174:8bb9f3a33240 | 121 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); |
mbed_official | 118:b44c45162f28 | 122 | } |
mbed_official | 174:8bb9f3a33240 | 123 | |
mbed_official | 76:aeb1df146756 | 124 | // Configure the SPI pins |
mbed_official | 76:aeb1df146756 | 125 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 76:aeb1df146756 | 126 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 76:aeb1df146756 | 127 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 174:8bb9f3a33240 | 128 | |
mbed_official | 76:aeb1df146756 | 129 | // Save new values |
mbed_official | 76:aeb1df146756 | 130 | obj->bits = SPI_DataSize_8b; |
mbed_official | 76:aeb1df146756 | 131 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 76:aeb1df146756 | 132 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 129:0182c99221bc | 133 | obj->br_presc = SPI_BaudRatePrescaler_256; |
mbed_official | 174:8bb9f3a33240 | 134 | |
mbed_official | 215:83cf97a28428 | 135 | obj->pin_miso = miso; |
mbed_official | 215:83cf97a28428 | 136 | obj->pin_mosi = mosi; |
mbed_official | 215:83cf97a28428 | 137 | obj->pin_sclk = sclk; |
mbed_official | 215:83cf97a28428 | 138 | obj->pin_ssel = ssel; |
mbed_official | 215:83cf97a28428 | 139 | |
mbed_official | 76:aeb1df146756 | 140 | if (ssel == NC) { // Master |
mbed_official | 76:aeb1df146756 | 141 | obj->mode = SPI_Mode_Master; |
mbed_official | 76:aeb1df146756 | 142 | obj->nss = SPI_NSS_Soft; |
mbed_official | 174:8bb9f3a33240 | 143 | } else { // Slave |
mbed_official | 76:aeb1df146756 | 144 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 76:aeb1df146756 | 145 | obj->mode = SPI_Mode_Slave; |
mbed_official | 190:bde2479eef9e | 146 | obj->nss = SPI_NSS_Hard; |
mbed_official | 76:aeb1df146756 | 147 | } |
mbed_official | 76:aeb1df146756 | 148 | |
mbed_official | 76:aeb1df146756 | 149 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 150 | } |
mbed_official | 76:aeb1df146756 | 151 | |
mbed_official | 76:aeb1df146756 | 152 | void spi_free(spi_t *obj) { |
mbed_official | 215:83cf97a28428 | 153 | // Reset SPI and disable clock |
mbed_official | 215:83cf97a28428 | 154 | if (obj->spi == SPI_1) { |
mbed_official | 215:83cf97a28428 | 155 | RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, ENABLE); |
mbed_official | 215:83cf97a28428 | 156 | RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE); |
mbed_official | 215:83cf97a28428 | 157 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE); |
mbed_official | 215:83cf97a28428 | 158 | } |
mbed_official | 215:83cf97a28428 | 159 | |
mbed_official | 215:83cf97a28428 | 160 | if (obj->spi == SPI_2) { |
mbed_official | 215:83cf97a28428 | 161 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 215:83cf97a28428 | 162 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, DISABLE); |
mbed_official | 215:83cf97a28428 | 163 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, DISABLE); |
mbed_official | 215:83cf97a28428 | 164 | } |
mbed_official | 215:83cf97a28428 | 165 | |
mbed_official | 215:83cf97a28428 | 166 | if (obj->spi == SPI_3) { |
mbed_official | 215:83cf97a28428 | 167 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, ENABLE); |
mbed_official | 215:83cf97a28428 | 168 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI3, DISABLE); |
mbed_official | 215:83cf97a28428 | 169 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, DISABLE); |
mbed_official | 215:83cf97a28428 | 170 | } |
mbed_official | 215:83cf97a28428 | 171 | |
mbed_official | 215:83cf97a28428 | 172 | // Configure GPIOs |
mbed_official | 215:83cf97a28428 | 173 | pin_function(obj->pin_miso, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); |
mbed_official | 215:83cf97a28428 | 174 | pin_function(obj->pin_mosi, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); |
mbed_official | 215:83cf97a28428 | 175 | pin_function(obj->pin_sclk, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); |
mbed_official | 215:83cf97a28428 | 176 | pin_function(obj->pin_ssel, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF)); |
mbed_official | 76:aeb1df146756 | 177 | } |
mbed_official | 76:aeb1df146756 | 178 | |
mbed_official | 174:8bb9f3a33240 | 179 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 76:aeb1df146756 | 180 | // Save new values |
mbed_official | 76:aeb1df146756 | 181 | if (bits == 8) { |
mbed_official | 76:aeb1df146756 | 182 | obj->bits = SPI_DataSize_8b; |
mbed_official | 174:8bb9f3a33240 | 183 | } else { |
mbed_official | 76:aeb1df146756 | 184 | obj->bits = SPI_DataSize_16b; |
mbed_official | 76:aeb1df146756 | 185 | } |
mbed_official | 174:8bb9f3a33240 | 186 | |
mbed_official | 76:aeb1df146756 | 187 | switch (mode) { |
mbed_official | 76:aeb1df146756 | 188 | case 0: |
mbed_official | 174:8bb9f3a33240 | 189 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 174:8bb9f3a33240 | 190 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 174:8bb9f3a33240 | 191 | break; |
mbed_official | 76:aeb1df146756 | 192 | case 1: |
mbed_official | 174:8bb9f3a33240 | 193 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 174:8bb9f3a33240 | 194 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 174:8bb9f3a33240 | 195 | break; |
mbed_official | 76:aeb1df146756 | 196 | case 2: |
mbed_official | 174:8bb9f3a33240 | 197 | obj->cpol = SPI_CPOL_High; |
mbed_official | 174:8bb9f3a33240 | 198 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 174:8bb9f3a33240 | 199 | break; |
mbed_official | 76:aeb1df146756 | 200 | default: |
mbed_official | 174:8bb9f3a33240 | 201 | obj->cpol = SPI_CPOL_High; |
mbed_official | 174:8bb9f3a33240 | 202 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 174:8bb9f3a33240 | 203 | break; |
mbed_official | 76:aeb1df146756 | 204 | } |
mbed_official | 174:8bb9f3a33240 | 205 | |
mbed_official | 76:aeb1df146756 | 206 | if (slave == 0) { |
mbed_official | 76:aeb1df146756 | 207 | obj->mode = SPI_Mode_Master; |
mbed_official | 76:aeb1df146756 | 208 | obj->nss = SPI_NSS_Soft; |
mbed_official | 174:8bb9f3a33240 | 209 | } else { |
mbed_official | 76:aeb1df146756 | 210 | obj->mode = SPI_Mode_Slave; |
mbed_official | 174:8bb9f3a33240 | 211 | obj->nss = SPI_NSS_Hard; |
mbed_official | 76:aeb1df146756 | 212 | } |
mbed_official | 174:8bb9f3a33240 | 213 | |
mbed_official | 76:aeb1df146756 | 214 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 215 | } |
mbed_official | 76:aeb1df146756 | 216 | |
mbed_official | 76:aeb1df146756 | 217 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 139:e3413eddde57 | 218 | // Values depend of PCLK1 and PCLK2: 32 MHz if HSI is used, 24 MHz if HSE is used |
mbed_official | 139:e3413eddde57 | 219 | if (SystemCoreClock == 32000000) { // HSI |
mbed_official | 139:e3413eddde57 | 220 | if (hz < 250000) { |
mbed_official | 139:e3413eddde57 | 221 | obj->br_presc = SPI_BaudRatePrescaler_256; // 125 kHz |
mbed_official | 174:8bb9f3a33240 | 222 | } else if ((hz >= 250000) && (hz < 500000)) { |
mbed_official | 139:e3413eddde57 | 223 | obj->br_presc = SPI_BaudRatePrescaler_128; // 250 kHz |
mbed_official | 174:8bb9f3a33240 | 224 | } else if ((hz >= 500000) && (hz < 1000000)) { |
mbed_official | 139:e3413eddde57 | 225 | obj->br_presc = SPI_BaudRatePrescaler_64; // 500 kHz |
mbed_official | 174:8bb9f3a33240 | 226 | } else if ((hz >= 1000000) && (hz < 2000000)) { |
mbed_official | 139:e3413eddde57 | 227 | obj->br_presc = SPI_BaudRatePrescaler_32; // 1 MHz |
mbed_official | 174:8bb9f3a33240 | 228 | } else if ((hz >= 2000000) && (hz < 4000000)) { |
mbed_official | 139:e3413eddde57 | 229 | obj->br_presc = SPI_BaudRatePrescaler_16; // 2 MHz |
mbed_official | 174:8bb9f3a33240 | 230 | } else if ((hz >= 4000000) && (hz < 8000000)) { |
mbed_official | 139:e3413eddde57 | 231 | obj->br_presc = SPI_BaudRatePrescaler_8; // 4 MHz |
mbed_official | 174:8bb9f3a33240 | 232 | } else if ((hz >= 8000000) && (hz < 16000000)) { |
mbed_official | 139:e3413eddde57 | 233 | obj->br_presc = SPI_BaudRatePrescaler_4; // 8 MHz |
mbed_official | 174:8bb9f3a33240 | 234 | } else { // >= 16000000 |
mbed_official | 139:e3413eddde57 | 235 | obj->br_presc = SPI_BaudRatePrescaler_2; // 16 MHz |
mbed_official | 139:e3413eddde57 | 236 | } |
mbed_official | 174:8bb9f3a33240 | 237 | } else { // 24 MHz - HSE |
mbed_official | 139:e3413eddde57 | 238 | if (hz < 180000) { |
mbed_official | 139:e3413eddde57 | 239 | obj->br_presc = SPI_BaudRatePrescaler_256; // 94 kHz |
mbed_official | 174:8bb9f3a33240 | 240 | } else if ((hz >= 180000) && (hz < 350000)) { |
mbed_official | 139:e3413eddde57 | 241 | obj->br_presc = SPI_BaudRatePrescaler_128; // 188 kHz |
mbed_official | 174:8bb9f3a33240 | 242 | } else if ((hz >= 350000) && (hz < 750000)) { |
mbed_official | 139:e3413eddde57 | 243 | obj->br_presc = SPI_BaudRatePrescaler_64; // 375 kHz |
mbed_official | 174:8bb9f3a33240 | 244 | } else if ((hz >= 750000) && (hz < 1000000)) { |
mbed_official | 139:e3413eddde57 | 245 | obj->br_presc = SPI_BaudRatePrescaler_32; // 750 kHz |
mbed_official | 174:8bb9f3a33240 | 246 | } else if ((hz >= 1000000) && (hz < 3000000)) { |
mbed_official | 174:8bb9f3a33240 | 247 | obj->br_presc = SPI_BaudRatePrescaler_16; // 1.5 MHz |
mbed_official | 174:8bb9f3a33240 | 248 | } else if ((hz >= 3000000) && (hz < 6000000)) { |
mbed_official | 174:8bb9f3a33240 | 249 | obj->br_presc = SPI_BaudRatePrescaler_8; // 3 MHz |
mbed_official | 174:8bb9f3a33240 | 250 | } else if ((hz >= 6000000) && (hz < 12000000)) { |
mbed_official | 174:8bb9f3a33240 | 251 | obj->br_presc = SPI_BaudRatePrescaler_4; // 6 MHz |
mbed_official | 174:8bb9f3a33240 | 252 | } else { // >= 12000000 |
mbed_official | 174:8bb9f3a33240 | 253 | obj->br_presc = SPI_BaudRatePrescaler_2; // 12 MHz |
mbed_official | 139:e3413eddde57 | 254 | } |
mbed_official | 129:0182c99221bc | 255 | } |
mbed_official | 76:aeb1df146756 | 256 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 257 | } |
mbed_official | 76:aeb1df146756 | 258 | |
mbed_official | 76:aeb1df146756 | 259 | static inline int ssp_readable(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 260 | int status; |
mbed_official | 76:aeb1df146756 | 261 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 262 | // Check if data is received |
mbed_official | 76:aeb1df146756 | 263 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 174:8bb9f3a33240 | 264 | return status; |
mbed_official | 76:aeb1df146756 | 265 | } |
mbed_official | 76:aeb1df146756 | 266 | |
mbed_official | 76:aeb1df146756 | 267 | static inline int ssp_writeable(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 268 | int status; |
mbed_official | 76:aeb1df146756 | 269 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 270 | // Check if data is transmitted |
mbed_official | 76:aeb1df146756 | 271 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 272 | return status; |
mbed_official | 76:aeb1df146756 | 273 | } |
mbed_official | 76:aeb1df146756 | 274 | |
mbed_official | 76:aeb1df146756 | 275 | static inline void ssp_write(spi_t *obj, int value) { |
mbed_official | 174:8bb9f3a33240 | 276 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 277 | while (!ssp_writeable(obj)); |
mbed_official | 76:aeb1df146756 | 278 | SPI_I2S_SendData(spi, (uint16_t)value); |
mbed_official | 76:aeb1df146756 | 279 | } |
mbed_official | 76:aeb1df146756 | 280 | |
mbed_official | 76:aeb1df146756 | 281 | static inline int ssp_read(spi_t *obj) { |
mbed_official | 174:8bb9f3a33240 | 282 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 283 | while (!ssp_readable(obj)); |
mbed_official | 76:aeb1df146756 | 284 | return (int)SPI_I2S_ReceiveData(spi); |
mbed_official | 76:aeb1df146756 | 285 | } |
mbed_official | 76:aeb1df146756 | 286 | |
mbed_official | 76:aeb1df146756 | 287 | static inline int ssp_busy(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 288 | int status; |
mbed_official | 76:aeb1df146756 | 289 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 290 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 291 | return status; |
mbed_official | 76:aeb1df146756 | 292 | } |
mbed_official | 76:aeb1df146756 | 293 | |
mbed_official | 76:aeb1df146756 | 294 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 76:aeb1df146756 | 295 | ssp_write(obj, value); |
mbed_official | 76:aeb1df146756 | 296 | return ssp_read(obj); |
mbed_official | 76:aeb1df146756 | 297 | } |
mbed_official | 76:aeb1df146756 | 298 | |
mbed_official | 76:aeb1df146756 | 299 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 190:bde2479eef9e | 300 | //return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); // initial code |
mbed_official | 190:bde2479eef9e | 301 | return (ssp_readable(obj)) ? (1) : (0); // works better like this |
mbed_official | 76:aeb1df146756 | 302 | }; |
mbed_official | 76:aeb1df146756 | 303 | |
mbed_official | 76:aeb1df146756 | 304 | int spi_slave_read(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 305 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 306 | return (int)SPI_I2S_ReceiveData(spi); |
mbed_official | 76:aeb1df146756 | 307 | } |
mbed_official | 76:aeb1df146756 | 308 | |
mbed_official | 76:aeb1df146756 | 309 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 174:8bb9f3a33240 | 310 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 174:8bb9f3a33240 | 311 | while (!ssp_writeable(obj)); |
mbed_official | 76:aeb1df146756 | 312 | SPI_I2S_SendData(spi, (uint16_t)value); |
mbed_official | 76:aeb1df146756 | 313 | } |
mbed_official | 76:aeb1df146756 | 314 | |
mbed_official | 76:aeb1df146756 | 315 | int spi_busy(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 316 | return ssp_busy(obj); |
mbed_official | 76:aeb1df146756 | 317 | } |
mbed_official | 76:aeb1df146756 | 318 | |
mbed_official | 76:aeb1df146756 | 319 | #endif |