mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_L152RE/spi_api.c@118:b44c45162f28, 2014-03-14 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Mar 14 10:00:07 2014 +0000
- Revision:
- 118:b44c45162f28
- Parent:
- 84:f54042cbc282
- Child:
- 129:0182c99221bc
Synchronized with git revision 7d88efe969a293d8fee496c7c1f902f359c40af8
Full URL: https://github.com/mbedmicro/mbed/commit/7d88efe969a293d8fee496c7c1f902f359c40af8/
Add additional peripherie for ST Nucleo L152 board
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 | 80:66393a7b209d | 40 | {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 80:66393a7b209d | 41 | {PA_12, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, // REMAP |
mbed_official | 118:b44c45162f28 | 42 | {PB_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, // REMAP2 |
mbed_official | 118:b44c45162f28 | 43 | {PB_15, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 118:b44c45162f28 | 44 | {PB_5, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 118:b44c45162f28 | 45 | {PC_12, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, // REMAP |
mbed_official | 118:b44c45162f28 | 46 | |
mbed_official | 76:aeb1df146756 | 47 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 48 | }; |
mbed_official | 76:aeb1df146756 | 49 | |
mbed_official | 76:aeb1df146756 | 50 | static const PinMap PinMap_SPI_MISO[] = { |
mbed_official | 80:66393a7b209d | 51 | {PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 80:66393a7b209d | 52 | {PA_11, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, // REMAP |
mbed_official | 118:b44c45162f28 | 53 | {PB_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, // REMAP2 |
mbed_official | 118:b44c45162f28 | 54 | {PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 118:b44c45162f28 | 55 | {PB_14, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 118:b44c45162f28 | 56 | {PB_4 , SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 118:b44c45162f28 | 57 | {PC_11, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 76:aeb1df146756 | 58 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 59 | }; |
mbed_official | 76:aeb1df146756 | 60 | |
mbed_official | 76:aeb1df146756 | 61 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 80:66393a7b209d | 62 | {PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 80:66393a7b209d | 63 | {PB_3, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, // REMAP |
mbed_official | 118:b44c45162f28 | 64 | {PB_13, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 118:b44c45162f28 | 65 | {PB_3, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 118:b44c45162f28 | 66 | {PC_10, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 76:aeb1df146756 | 67 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 68 | }; |
mbed_official | 76:aeb1df146756 | 69 | |
mbed_official | 76:aeb1df146756 | 70 | // Only used in Slave mode |
mbed_official | 76:aeb1df146756 | 71 | static const PinMap PinMap_SPI_SSEL[] = { |
mbed_official | 80:66393a7b209d | 72 | {PA_4, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, |
mbed_official | 118:b44c45162f28 | 73 | {PA_15, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI1)}, // REMAP |
mbed_official | 118:b44c45162f28 | 74 | {PB_12, SPI_2, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI2)}, |
mbed_official | 118:b44c45162f28 | 75 | {PA_4 , SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 118:b44c45162f28 | 76 | {PA_15, SPI_3, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_AF_SPI3)}, |
mbed_official | 76:aeb1df146756 | 77 | {NC, NC, 0} |
mbed_official | 76:aeb1df146756 | 78 | }; |
mbed_official | 76:aeb1df146756 | 79 | |
mbed_official | 76:aeb1df146756 | 80 | static void init_spi(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 81 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 82 | SPI_InitTypeDef SPI_InitStructure; |
mbed_official | 76:aeb1df146756 | 83 | |
mbed_official | 76:aeb1df146756 | 84 | SPI_Cmd(spi, DISABLE); |
mbed_official | 76:aeb1df146756 | 85 | |
mbed_official | 76:aeb1df146756 | 86 | SPI_InitStructure.SPI_Mode = obj->mode; |
mbed_official | 76:aeb1df146756 | 87 | SPI_InitStructure.SPI_NSS = obj->nss; |
mbed_official | 76:aeb1df146756 | 88 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; |
mbed_official | 76:aeb1df146756 | 89 | SPI_InitStructure.SPI_DataSize = obj->bits; |
mbed_official | 76:aeb1df146756 | 90 | SPI_InitStructure.SPI_CPOL = obj->cpol; |
mbed_official | 76:aeb1df146756 | 91 | SPI_InitStructure.SPI_CPHA = obj->cpha; |
mbed_official | 76:aeb1df146756 | 92 | SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc; |
mbed_official | 76:aeb1df146756 | 93 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; |
mbed_official | 76:aeb1df146756 | 94 | SPI_InitStructure.SPI_CRCPolynomial = 7; |
mbed_official | 76:aeb1df146756 | 95 | SPI_Init(spi, &SPI_InitStructure); |
mbed_official | 76:aeb1df146756 | 96 | |
mbed_official | 76:aeb1df146756 | 97 | SPI_Cmd(spi, ENABLE); |
mbed_official | 76:aeb1df146756 | 98 | } |
mbed_official | 76:aeb1df146756 | 99 | |
mbed_official | 76:aeb1df146756 | 100 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 76:aeb1df146756 | 101 | // Determine the SPI to use |
mbed_official | 76:aeb1df146756 | 102 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 76:aeb1df146756 | 103 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 76:aeb1df146756 | 104 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 76:aeb1df146756 | 105 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 76:aeb1df146756 | 106 | |
mbed_official | 76:aeb1df146756 | 107 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 76:aeb1df146756 | 108 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 76:aeb1df146756 | 109 | |
mbed_official | 76:aeb1df146756 | 110 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 76:aeb1df146756 | 111 | |
mbed_official | 76:aeb1df146756 | 112 | if (obj->spi == (SPIName)NC) { |
mbed_official | 76:aeb1df146756 | 113 | error("SPI pinout mapping failed"); |
mbed_official | 76:aeb1df146756 | 114 | } |
mbed_official | 76:aeb1df146756 | 115 | |
mbed_official | 76:aeb1df146756 | 116 | // Enable SPI clock |
mbed_official | 76:aeb1df146756 | 117 | if (obj->spi == SPI_1) { |
mbed_official | 76:aeb1df146756 | 118 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); |
mbed_official | 76:aeb1df146756 | 119 | } |
mbed_official | 76:aeb1df146756 | 120 | if (obj->spi == SPI_2) { |
mbed_official | 76:aeb1df146756 | 121 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); |
mbed_official | 76:aeb1df146756 | 122 | } |
mbed_official | 118:b44c45162f28 | 123 | if (obj->spi == SPI_3) { |
mbed_official | 118:b44c45162f28 | 124 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); |
mbed_official | 118:b44c45162f28 | 125 | } |
mbed_official | 76:aeb1df146756 | 126 | |
mbed_official | 76:aeb1df146756 | 127 | // Configure the SPI pins |
mbed_official | 76:aeb1df146756 | 128 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 76:aeb1df146756 | 129 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 76:aeb1df146756 | 130 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 76:aeb1df146756 | 131 | |
mbed_official | 76:aeb1df146756 | 132 | // Save new values |
mbed_official | 76:aeb1df146756 | 133 | obj->bits = SPI_DataSize_8b; |
mbed_official | 76:aeb1df146756 | 134 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 76:aeb1df146756 | 135 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 84:f54042cbc282 | 136 | obj->br_presc = SPI_BaudRatePrescaler_16; // 1 MHz |
mbed_official | 76:aeb1df146756 | 137 | |
mbed_official | 76:aeb1df146756 | 138 | if (ssel == NC) { // Master |
mbed_official | 76:aeb1df146756 | 139 | obj->mode = SPI_Mode_Master; |
mbed_official | 76:aeb1df146756 | 140 | obj->nss = SPI_NSS_Soft; |
mbed_official | 76:aeb1df146756 | 141 | } |
mbed_official | 76:aeb1df146756 | 142 | else { // Slave |
mbed_official | 76:aeb1df146756 | 143 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 76:aeb1df146756 | 144 | obj->mode = SPI_Mode_Slave; |
mbed_official | 76:aeb1df146756 | 145 | obj->nss = SPI_NSS_Soft; |
mbed_official | 76:aeb1df146756 | 146 | } |
mbed_official | 76:aeb1df146756 | 147 | |
mbed_official | 76:aeb1df146756 | 148 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 149 | } |
mbed_official | 76:aeb1df146756 | 150 | |
mbed_official | 76:aeb1df146756 | 151 | void spi_free(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 152 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 153 | SPI_I2S_DeInit(spi); |
mbed_official | 76:aeb1df146756 | 154 | } |
mbed_official | 76:aeb1df146756 | 155 | |
mbed_official | 76:aeb1df146756 | 156 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 76:aeb1df146756 | 157 | // Save new values |
mbed_official | 76:aeb1df146756 | 158 | if (bits == 8) { |
mbed_official | 76:aeb1df146756 | 159 | obj->bits = SPI_DataSize_8b; |
mbed_official | 76:aeb1df146756 | 160 | } |
mbed_official | 76:aeb1df146756 | 161 | else { |
mbed_official | 76:aeb1df146756 | 162 | obj->bits = SPI_DataSize_16b; |
mbed_official | 76:aeb1df146756 | 163 | } |
mbed_official | 76:aeb1df146756 | 164 | |
mbed_official | 76:aeb1df146756 | 165 | switch (mode) { |
mbed_official | 76:aeb1df146756 | 166 | case 0: |
mbed_official | 76:aeb1df146756 | 167 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 76:aeb1df146756 | 168 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 76:aeb1df146756 | 169 | break; |
mbed_official | 76:aeb1df146756 | 170 | case 1: |
mbed_official | 76:aeb1df146756 | 171 | obj->cpol = SPI_CPOL_Low; |
mbed_official | 76:aeb1df146756 | 172 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 76:aeb1df146756 | 173 | break; |
mbed_official | 76:aeb1df146756 | 174 | case 2: |
mbed_official | 76:aeb1df146756 | 175 | obj->cpol = SPI_CPOL_High; |
mbed_official | 76:aeb1df146756 | 176 | obj->cpha = SPI_CPHA_1Edge; |
mbed_official | 76:aeb1df146756 | 177 | break; |
mbed_official | 76:aeb1df146756 | 178 | default: |
mbed_official | 76:aeb1df146756 | 179 | obj->cpol = SPI_CPOL_High; |
mbed_official | 76:aeb1df146756 | 180 | obj->cpha = SPI_CPHA_2Edge; |
mbed_official | 76:aeb1df146756 | 181 | break; |
mbed_official | 76:aeb1df146756 | 182 | } |
mbed_official | 76:aeb1df146756 | 183 | |
mbed_official | 76:aeb1df146756 | 184 | if (slave == 0) { |
mbed_official | 76:aeb1df146756 | 185 | obj->mode = SPI_Mode_Master; |
mbed_official | 76:aeb1df146756 | 186 | obj->nss = SPI_NSS_Soft; |
mbed_official | 76:aeb1df146756 | 187 | } |
mbed_official | 76:aeb1df146756 | 188 | else { |
mbed_official | 76:aeb1df146756 | 189 | obj->mode = SPI_Mode_Slave; |
mbed_official | 76:aeb1df146756 | 190 | obj->nss = SPI_NSS_Hard; |
mbed_official | 76:aeb1df146756 | 191 | } |
mbed_official | 76:aeb1df146756 | 192 | |
mbed_official | 76:aeb1df146756 | 193 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 194 | } |
mbed_official | 76:aeb1df146756 | 195 | |
mbed_official | 76:aeb1df146756 | 196 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 76:aeb1df146756 | 197 | // Get SPI clock frequency |
mbed_official | 84:f54042cbc282 | 198 | uint32_t PCLK = SystemCoreClock; |
mbed_official | 76:aeb1df146756 | 199 | |
mbed_official | 76:aeb1df146756 | 200 | // Choose the baud rate divisor (between 2 and 256) |
mbed_official | 76:aeb1df146756 | 201 | uint32_t divisor = PCLK / hz; |
mbed_official | 76:aeb1df146756 | 202 | |
mbed_official | 76:aeb1df146756 | 203 | // Find the nearest power-of-2 |
mbed_official | 76:aeb1df146756 | 204 | divisor = (divisor > 0 ? divisor-1 : 0); |
mbed_official | 76:aeb1df146756 | 205 | divisor |= divisor >> 1; |
mbed_official | 76:aeb1df146756 | 206 | divisor |= divisor >> 2; |
mbed_official | 76:aeb1df146756 | 207 | divisor |= divisor >> 4; |
mbed_official | 76:aeb1df146756 | 208 | divisor |= divisor >> 8; |
mbed_official | 76:aeb1df146756 | 209 | divisor |= divisor >> 16; |
mbed_official | 76:aeb1df146756 | 210 | divisor++; |
mbed_official | 76:aeb1df146756 | 211 | |
mbed_official | 76:aeb1df146756 | 212 | uint32_t baud_rate = __builtin_ffs(divisor) - 2; |
mbed_official | 76:aeb1df146756 | 213 | |
mbed_official | 76:aeb1df146756 | 214 | // Save new value |
mbed_official | 76:aeb1df146756 | 215 | obj->br_presc = ((baud_rate > 7) ? (7 << 3) : (baud_rate << 3)); |
mbed_official | 76:aeb1df146756 | 216 | |
mbed_official | 76:aeb1df146756 | 217 | init_spi(obj); |
mbed_official | 76:aeb1df146756 | 218 | } |
mbed_official | 76:aeb1df146756 | 219 | |
mbed_official | 76:aeb1df146756 | 220 | static inline int ssp_readable(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 221 | int status; |
mbed_official | 76:aeb1df146756 | 222 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 223 | // Check if data is received |
mbed_official | 76:aeb1df146756 | 224 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 225 | return status; |
mbed_official | 76:aeb1df146756 | 226 | } |
mbed_official | 76:aeb1df146756 | 227 | |
mbed_official | 76:aeb1df146756 | 228 | static inline int ssp_writeable(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 229 | int status; |
mbed_official | 76:aeb1df146756 | 230 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 231 | // Check if data is transmitted |
mbed_official | 76:aeb1df146756 | 232 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 233 | return status; |
mbed_official | 76:aeb1df146756 | 234 | } |
mbed_official | 76:aeb1df146756 | 235 | |
mbed_official | 76:aeb1df146756 | 236 | static inline void ssp_write(spi_t *obj, int value) { |
mbed_official | 76:aeb1df146756 | 237 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 238 | while (!ssp_writeable(obj)); |
mbed_official | 76:aeb1df146756 | 239 | SPI_I2S_SendData(spi, (uint16_t)value); |
mbed_official | 76:aeb1df146756 | 240 | } |
mbed_official | 76:aeb1df146756 | 241 | |
mbed_official | 76:aeb1df146756 | 242 | static inline int ssp_read(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 243 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 244 | while (!ssp_readable(obj)); |
mbed_official | 76:aeb1df146756 | 245 | return (int)SPI_I2S_ReceiveData(spi); |
mbed_official | 76:aeb1df146756 | 246 | } |
mbed_official | 76:aeb1df146756 | 247 | |
mbed_official | 76:aeb1df146756 | 248 | static inline int ssp_busy(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 249 | int status; |
mbed_official | 76:aeb1df146756 | 250 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 251 | status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 76:aeb1df146756 | 252 | return status; |
mbed_official | 76:aeb1df146756 | 253 | } |
mbed_official | 76:aeb1df146756 | 254 | |
mbed_official | 76:aeb1df146756 | 255 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 76:aeb1df146756 | 256 | ssp_write(obj, value); |
mbed_official | 76:aeb1df146756 | 257 | return ssp_read(obj); |
mbed_official | 76:aeb1df146756 | 258 | } |
mbed_official | 76:aeb1df146756 | 259 | |
mbed_official | 76:aeb1df146756 | 260 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 261 | return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0); |
mbed_official | 76:aeb1df146756 | 262 | }; |
mbed_official | 76:aeb1df146756 | 263 | |
mbed_official | 76:aeb1df146756 | 264 | int spi_slave_read(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 265 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 266 | return (int)SPI_I2S_ReceiveData(spi); |
mbed_official | 76:aeb1df146756 | 267 | } |
mbed_official | 76:aeb1df146756 | 268 | |
mbed_official | 76:aeb1df146756 | 269 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 76:aeb1df146756 | 270 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 76:aeb1df146756 | 271 | while (!ssp_writeable(obj)); |
mbed_official | 76:aeb1df146756 | 272 | SPI_I2S_SendData(spi, (uint16_t)value); |
mbed_official | 76:aeb1df146756 | 273 | } |
mbed_official | 76:aeb1df146756 | 274 | |
mbed_official | 76:aeb1df146756 | 275 | int spi_busy(spi_t *obj) { |
mbed_official | 76:aeb1df146756 | 276 | return ssp_busy(obj); |
mbed_official | 76:aeb1df146756 | 277 | } |
mbed_official | 76:aeb1df146756 | 278 | |
mbed_official | 76:aeb1df146756 | 279 | #endif |