mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F072RB/spi_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 239:8cadf13dff33
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 219:993c9b0acbcc | 1 | /* mbed Microcontroller Library |
mbed_official | 219:993c9b0acbcc | 2 | ******************************************************************************* |
mbed_official | 219:993c9b0acbcc | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 219:993c9b0acbcc | 4 | * All rights reserved. |
mbed_official | 219:993c9b0acbcc | 5 | * |
mbed_official | 219:993c9b0acbcc | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 219:993c9b0acbcc | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 219:993c9b0acbcc | 8 | * |
mbed_official | 219:993c9b0acbcc | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 219:993c9b0acbcc | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 219:993c9b0acbcc | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 219:993c9b0acbcc | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 219:993c9b0acbcc | 13 | * and/or other materials provided with the distribution. |
mbed_official | 219:993c9b0acbcc | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 219:993c9b0acbcc | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 219:993c9b0acbcc | 16 | * without specific prior written permission. |
mbed_official | 219:993c9b0acbcc | 17 | * |
mbed_official | 219:993c9b0acbcc | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 219:993c9b0acbcc | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 219:993c9b0acbcc | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 219:993c9b0acbcc | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 219:993c9b0acbcc | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 219:993c9b0acbcc | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 219:993c9b0acbcc | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 219:993c9b0acbcc | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 219:993c9b0acbcc | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 219:993c9b0acbcc | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 219:993c9b0acbcc | 28 | ******************************************************************************* |
mbed_official | 219:993c9b0acbcc | 29 | */ |
mbed_official | 239:8cadf13dff33 | 30 | #include "mbed_assert.h" |
mbed_official | 219:993c9b0acbcc | 31 | #include "spi_api.h" |
mbed_official | 219:993c9b0acbcc | 32 | |
mbed_official | 219:993c9b0acbcc | 33 | #if DEVICE_SPI |
mbed_official | 219:993c9b0acbcc | 34 | |
mbed_official | 219:993c9b0acbcc | 35 | #include <math.h> |
mbed_official | 219:993c9b0acbcc | 36 | #include "cmsis.h" |
mbed_official | 219:993c9b0acbcc | 37 | #include "pinmap.h" |
mbed_official | 219:993c9b0acbcc | 38 | |
mbed_official | 219:993c9b0acbcc | 39 | static const PinMap PinMap_SPI_MOSI[] = { |
mbed_official | 219:993c9b0acbcc | 40 | {PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 41 | {PB_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 42 | {PB_15, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 43 | {PC_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 44 | {NC, NC, 0} |
mbed_official | 219:993c9b0acbcc | 45 | }; |
mbed_official | 219:993c9b0acbcc | 46 | |
mbed_official | 219:993c9b0acbcc | 47 | static const PinMap PinMap_SPI_MISO[] = { |
mbed_official | 219:993c9b0acbcc | 48 | {PA_6, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 49 | {PB_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 50 | {PB_14, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 51 | {PC_2, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 52 | {NC, NC, 0} |
mbed_official | 219:993c9b0acbcc | 53 | }; |
mbed_official | 219:993c9b0acbcc | 54 | |
mbed_official | 219:993c9b0acbcc | 55 | static const PinMap PinMap_SPI_SCLK[] = { |
mbed_official | 219:993c9b0acbcc | 56 | {PA_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 57 | {PB_3, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 58 | {PB_10, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 59 | {PB_13, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 60 | {NC, NC, 0} |
mbed_official | 219:993c9b0acbcc | 61 | }; |
mbed_official | 219:993c9b0acbcc | 62 | |
mbed_official | 219:993c9b0acbcc | 63 | static const PinMap PinMap_SPI_SSEL[] = { |
mbed_official | 219:993c9b0acbcc | 64 | {PA_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 65 | {PA_15, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI1)}, |
mbed_official | 219:993c9b0acbcc | 66 | {PB_9, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 67 | {PB_12, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF0_SPI2)}, |
mbed_official | 219:993c9b0acbcc | 68 | {NC, NC, 0} |
mbed_official | 219:993c9b0acbcc | 69 | }; |
mbed_official | 219:993c9b0acbcc | 70 | |
mbed_official | 219:993c9b0acbcc | 71 | static SPI_HandleTypeDef SpiHandle; |
mbed_official | 219:993c9b0acbcc | 72 | |
mbed_official | 219:993c9b0acbcc | 73 | static void init_spi(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 74 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 75 | |
mbed_official | 219:993c9b0acbcc | 76 | __HAL_SPI_DISABLE(&SpiHandle); |
mbed_official | 219:993c9b0acbcc | 77 | |
mbed_official | 219:993c9b0acbcc | 78 | SpiHandle.Init.Mode = obj->mode; |
mbed_official | 219:993c9b0acbcc | 79 | SpiHandle.Init.BaudRatePrescaler = obj->br_presc; |
mbed_official | 219:993c9b0acbcc | 80 | SpiHandle.Init.Direction = SPI_DIRECTION_2LINES; |
mbed_official | 219:993c9b0acbcc | 81 | SpiHandle.Init.CLKPhase = obj->cpha; |
mbed_official | 219:993c9b0acbcc | 82 | SpiHandle.Init.CLKPolarity = obj->cpol; |
mbed_official | 219:993c9b0acbcc | 83 | SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; |
mbed_official | 219:993c9b0acbcc | 84 | SpiHandle.Init.CRCPolynomial = 7; |
mbed_official | 219:993c9b0acbcc | 85 | SpiHandle.Init.DataSize = obj->bits; |
mbed_official | 219:993c9b0acbcc | 86 | SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB; |
mbed_official | 219:993c9b0acbcc | 87 | SpiHandle.Init.NSS = obj->nss; |
mbed_official | 219:993c9b0acbcc | 88 | SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED; |
mbed_official | 219:993c9b0acbcc | 89 | |
mbed_official | 219:993c9b0acbcc | 90 | HAL_SPI_Init(&SpiHandle); |
mbed_official | 219:993c9b0acbcc | 91 | |
mbed_official | 219:993c9b0acbcc | 92 | __HAL_SPI_ENABLE(&SpiHandle); |
mbed_official | 219:993c9b0acbcc | 93 | } |
mbed_official | 219:993c9b0acbcc | 94 | |
mbed_official | 219:993c9b0acbcc | 95 | void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) { |
mbed_official | 219:993c9b0acbcc | 96 | // Determine the SPI to use |
mbed_official | 219:993c9b0acbcc | 97 | SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI); |
mbed_official | 219:993c9b0acbcc | 98 | SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO); |
mbed_official | 219:993c9b0acbcc | 99 | SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK); |
mbed_official | 219:993c9b0acbcc | 100 | SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL); |
mbed_official | 219:993c9b0acbcc | 101 | |
mbed_official | 219:993c9b0acbcc | 102 | SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso); |
mbed_official | 219:993c9b0acbcc | 103 | SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel); |
mbed_official | 219:993c9b0acbcc | 104 | |
mbed_official | 219:993c9b0acbcc | 105 | obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl); |
mbed_official | 239:8cadf13dff33 | 106 | MBED_ASSERT(obj->spi != (SPIName)NC); |
mbed_official | 219:993c9b0acbcc | 107 | |
mbed_official | 219:993c9b0acbcc | 108 | // Enable SPI clock |
mbed_official | 219:993c9b0acbcc | 109 | if (obj->spi == SPI_1) { |
mbed_official | 219:993c9b0acbcc | 110 | __SPI1_CLK_ENABLE(); |
mbed_official | 219:993c9b0acbcc | 111 | } |
mbed_official | 219:993c9b0acbcc | 112 | if (obj->spi == SPI_2) { |
mbed_official | 219:993c9b0acbcc | 113 | __SPI2_CLK_ENABLE(); |
mbed_official | 219:993c9b0acbcc | 114 | } |
mbed_official | 219:993c9b0acbcc | 115 | |
mbed_official | 219:993c9b0acbcc | 116 | // Configure the SPI pins |
mbed_official | 219:993c9b0acbcc | 117 | pinmap_pinout(mosi, PinMap_SPI_MOSI); |
mbed_official | 219:993c9b0acbcc | 118 | pinmap_pinout(miso, PinMap_SPI_MISO); |
mbed_official | 219:993c9b0acbcc | 119 | pinmap_pinout(sclk, PinMap_SPI_SCLK); |
mbed_official | 219:993c9b0acbcc | 120 | |
mbed_official | 219:993c9b0acbcc | 121 | // Save new values |
mbed_official | 219:993c9b0acbcc | 122 | obj->bits = SPI_DATASIZE_8BIT; |
mbed_official | 219:993c9b0acbcc | 123 | obj->cpol = SPI_POLARITY_LOW; |
mbed_official | 219:993c9b0acbcc | 124 | obj->cpha = SPI_PHASE_1EDGE; |
mbed_official | 219:993c9b0acbcc | 125 | obj->br_presc = SPI_BAUDRATEPRESCALER_256; |
mbed_official | 219:993c9b0acbcc | 126 | |
mbed_official | 219:993c9b0acbcc | 127 | obj->pin_miso = miso; |
mbed_official | 219:993c9b0acbcc | 128 | obj->pin_mosi = mosi; |
mbed_official | 219:993c9b0acbcc | 129 | obj->pin_sclk = sclk; |
mbed_official | 219:993c9b0acbcc | 130 | obj->pin_ssel = ssel; |
mbed_official | 219:993c9b0acbcc | 131 | |
mbed_official | 219:993c9b0acbcc | 132 | if (ssel == NC) { // SW NSS Master mode |
mbed_official | 219:993c9b0acbcc | 133 | obj->mode = SPI_MODE_MASTER; |
mbed_official | 219:993c9b0acbcc | 134 | obj->nss = SPI_NSS_SOFT; |
mbed_official | 219:993c9b0acbcc | 135 | } else { // Slave |
mbed_official | 219:993c9b0acbcc | 136 | pinmap_pinout(ssel, PinMap_SPI_SSEL); |
mbed_official | 219:993c9b0acbcc | 137 | obj->mode = SPI_MODE_SLAVE; |
mbed_official | 219:993c9b0acbcc | 138 | obj->nss = SPI_NSS_HARD_INPUT; |
mbed_official | 219:993c9b0acbcc | 139 | } |
mbed_official | 219:993c9b0acbcc | 140 | |
mbed_official | 219:993c9b0acbcc | 141 | init_spi(obj); |
mbed_official | 219:993c9b0acbcc | 142 | } |
mbed_official | 219:993c9b0acbcc | 143 | |
mbed_official | 219:993c9b0acbcc | 144 | void spi_free(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 145 | // Reset SPI and disable clock |
mbed_official | 219:993c9b0acbcc | 146 | if (obj->spi == SPI_1) { |
mbed_official | 219:993c9b0acbcc | 147 | __SPI1_FORCE_RESET(); |
mbed_official | 219:993c9b0acbcc | 148 | __SPI1_RELEASE_RESET(); |
mbed_official | 219:993c9b0acbcc | 149 | __SPI1_CLK_DISABLE(); |
mbed_official | 219:993c9b0acbcc | 150 | } |
mbed_official | 219:993c9b0acbcc | 151 | |
mbed_official | 219:993c9b0acbcc | 152 | if (obj->spi == SPI_2) { |
mbed_official | 219:993c9b0acbcc | 153 | __SPI2_FORCE_RESET(); |
mbed_official | 219:993c9b0acbcc | 154 | __SPI2_RELEASE_RESET(); |
mbed_official | 219:993c9b0acbcc | 155 | __SPI2_CLK_DISABLE(); |
mbed_official | 219:993c9b0acbcc | 156 | } |
mbed_official | 219:993c9b0acbcc | 157 | |
mbed_official | 219:993c9b0acbcc | 158 | // Configure GPIO |
mbed_official | 219:993c9b0acbcc | 159 | pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 219:993c9b0acbcc | 160 | pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 219:993c9b0acbcc | 161 | pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 219:993c9b0acbcc | 162 | pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); |
mbed_official | 219:993c9b0acbcc | 163 | } |
mbed_official | 219:993c9b0acbcc | 164 | |
mbed_official | 219:993c9b0acbcc | 165 | void spi_format(spi_t *obj, int bits, int mode, int slave) { |
mbed_official | 219:993c9b0acbcc | 166 | // Save new values |
mbed_official | 219:993c9b0acbcc | 167 | if (bits == 16) { |
mbed_official | 219:993c9b0acbcc | 168 | obj->bits = SPI_DATASIZE_16BIT; |
mbed_official | 219:993c9b0acbcc | 169 | } else { |
mbed_official | 219:993c9b0acbcc | 170 | obj->bits = SPI_DATASIZE_8BIT; |
mbed_official | 219:993c9b0acbcc | 171 | } |
mbed_official | 219:993c9b0acbcc | 172 | |
mbed_official | 219:993c9b0acbcc | 173 | switch (mode) { |
mbed_official | 219:993c9b0acbcc | 174 | case 0: |
mbed_official | 219:993c9b0acbcc | 175 | obj->cpol = SPI_POLARITY_LOW; |
mbed_official | 219:993c9b0acbcc | 176 | obj->cpha = SPI_PHASE_1EDGE; |
mbed_official | 219:993c9b0acbcc | 177 | break; |
mbed_official | 219:993c9b0acbcc | 178 | case 1: |
mbed_official | 219:993c9b0acbcc | 179 | obj->cpol = SPI_POLARITY_LOW; |
mbed_official | 219:993c9b0acbcc | 180 | obj->cpha = SPI_PHASE_2EDGE; |
mbed_official | 219:993c9b0acbcc | 181 | break; |
mbed_official | 219:993c9b0acbcc | 182 | case 2: |
mbed_official | 219:993c9b0acbcc | 183 | obj->cpol = SPI_POLARITY_HIGH; |
mbed_official | 219:993c9b0acbcc | 184 | obj->cpha = SPI_PHASE_1EDGE; |
mbed_official | 219:993c9b0acbcc | 185 | break; |
mbed_official | 219:993c9b0acbcc | 186 | default: |
mbed_official | 219:993c9b0acbcc | 187 | obj->cpol = SPI_POLARITY_HIGH; |
mbed_official | 219:993c9b0acbcc | 188 | obj->cpha = SPI_PHASE_2EDGE; |
mbed_official | 219:993c9b0acbcc | 189 | break; |
mbed_official | 219:993c9b0acbcc | 190 | } |
mbed_official | 219:993c9b0acbcc | 191 | |
mbed_official | 219:993c9b0acbcc | 192 | if (slave == 0) { |
mbed_official | 219:993c9b0acbcc | 193 | obj->mode = SPI_MODE_MASTER; |
mbed_official | 219:993c9b0acbcc | 194 | obj->nss = SPI_NSS_SOFT; |
mbed_official | 219:993c9b0acbcc | 195 | } else { |
mbed_official | 219:993c9b0acbcc | 196 | obj->mode = SPI_MODE_SLAVE; |
mbed_official | 219:993c9b0acbcc | 197 | obj->nss = SPI_NSS_HARD_INPUT; |
mbed_official | 219:993c9b0acbcc | 198 | } |
mbed_official | 219:993c9b0acbcc | 199 | |
mbed_official | 219:993c9b0acbcc | 200 | init_spi(obj); |
mbed_official | 219:993c9b0acbcc | 201 | } |
mbed_official | 219:993c9b0acbcc | 202 | |
mbed_official | 219:993c9b0acbcc | 203 | void spi_frequency(spi_t *obj, int hz) { |
mbed_official | 219:993c9b0acbcc | 204 | // Note: The frequencies are obtained with SPI clock = 48 MHz (APB clock) |
mbed_official | 219:993c9b0acbcc | 205 | if (hz < 375000) { |
mbed_official | 219:993c9b0acbcc | 206 | obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 188 kHz |
mbed_official | 219:993c9b0acbcc | 207 | } else if ((hz >= 375000) && (hz < 750000)) { |
mbed_official | 219:993c9b0acbcc | 208 | obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz |
mbed_official | 219:993c9b0acbcc | 209 | } else if ((hz >= 750000) && (hz < 1000000)) { |
mbed_official | 219:993c9b0acbcc | 210 | obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 750 kHz |
mbed_official | 219:993c9b0acbcc | 211 | } else if ((hz >= 1000000) && (hz < 3000000)) { |
mbed_official | 219:993c9b0acbcc | 212 | obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz |
mbed_official | 219:993c9b0acbcc | 213 | } else if ((hz >= 3000000) && (hz < 6000000)) { |
mbed_official | 219:993c9b0acbcc | 214 | obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz |
mbed_official | 219:993c9b0acbcc | 215 | } else if ((hz >= 6000000) && (hz < 12000000)) { |
mbed_official | 219:993c9b0acbcc | 216 | obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz |
mbed_official | 219:993c9b0acbcc | 217 | } else if ((hz >= 12000000) && (hz < 24000000)) { |
mbed_official | 219:993c9b0acbcc | 218 | obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz |
mbed_official | 219:993c9b0acbcc | 219 | } else { // >= 24000000 |
mbed_official | 219:993c9b0acbcc | 220 | obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz |
mbed_official | 219:993c9b0acbcc | 221 | } |
mbed_official | 219:993c9b0acbcc | 222 | init_spi(obj); |
mbed_official | 219:993c9b0acbcc | 223 | } |
mbed_official | 219:993c9b0acbcc | 224 | |
mbed_official | 219:993c9b0acbcc | 225 | static inline int ssp_readable(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 226 | int status; |
mbed_official | 219:993c9b0acbcc | 227 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 228 | // Check if data is received |
mbed_official | 219:993c9b0acbcc | 229 | status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0); |
mbed_official | 219:993c9b0acbcc | 230 | return status; |
mbed_official | 219:993c9b0acbcc | 231 | } |
mbed_official | 219:993c9b0acbcc | 232 | |
mbed_official | 219:993c9b0acbcc | 233 | static inline int ssp_writeable(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 234 | int status; |
mbed_official | 219:993c9b0acbcc | 235 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 236 | // Check if data is transmitted |
mbed_official | 219:993c9b0acbcc | 237 | status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0); |
mbed_official | 219:993c9b0acbcc | 238 | return status; |
mbed_official | 219:993c9b0acbcc | 239 | } |
mbed_official | 219:993c9b0acbcc | 240 | |
mbed_official | 219:993c9b0acbcc | 241 | static inline void ssp_write(spi_t *obj, int value) { |
mbed_official | 219:993c9b0acbcc | 242 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 243 | while (!ssp_writeable(obj)); |
mbed_official | 219:993c9b0acbcc | 244 | |
mbed_official | 219:993c9b0acbcc | 245 | if (obj->bits <= SPI_DATASIZE_8BIT) { |
mbed_official | 219:993c9b0acbcc | 246 | // force 8-bit access the data register due to SPI data buffer in this device |
mbed_official | 219:993c9b0acbcc | 247 | uint8_t *p_spi_dr = 0; |
mbed_official | 219:993c9b0acbcc | 248 | p_spi_dr = (uint8_t *) & (spi->DR); |
mbed_official | 219:993c9b0acbcc | 249 | *p_spi_dr = (uint8_t)value; |
mbed_official | 219:993c9b0acbcc | 250 | } else { |
mbed_official | 219:993c9b0acbcc | 251 | spi->DR = (uint16_t)value; |
mbed_official | 219:993c9b0acbcc | 252 | } |
mbed_official | 219:993c9b0acbcc | 253 | } |
mbed_official | 219:993c9b0acbcc | 254 | |
mbed_official | 219:993c9b0acbcc | 255 | static inline int ssp_read(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 256 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 257 | while (!ssp_readable(obj)); |
mbed_official | 219:993c9b0acbcc | 258 | |
mbed_official | 219:993c9b0acbcc | 259 | if (obj->bits <= SPI_DATASIZE_8BIT) { |
mbed_official | 219:993c9b0acbcc | 260 | // force 8-bit access the data register due to SPI data buffer in this device |
mbed_official | 219:993c9b0acbcc | 261 | uint8_t *p_spi_dr = 0; |
mbed_official | 219:993c9b0acbcc | 262 | p_spi_dr = (uint8_t *) & (spi->DR); |
mbed_official | 219:993c9b0acbcc | 263 | return (int)(*p_spi_dr); |
mbed_official | 219:993c9b0acbcc | 264 | } else { |
mbed_official | 219:993c9b0acbcc | 265 | return (int)spi->DR; |
mbed_official | 219:993c9b0acbcc | 266 | } |
mbed_official | 219:993c9b0acbcc | 267 | } |
mbed_official | 219:993c9b0acbcc | 268 | |
mbed_official | 219:993c9b0acbcc | 269 | static inline int ssp_busy(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 270 | int status; |
mbed_official | 219:993c9b0acbcc | 271 | SpiHandle.Instance = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 272 | status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0); |
mbed_official | 219:993c9b0acbcc | 273 | return status; |
mbed_official | 219:993c9b0acbcc | 274 | } |
mbed_official | 219:993c9b0acbcc | 275 | |
mbed_official | 219:993c9b0acbcc | 276 | int spi_master_write(spi_t *obj, int value) { |
mbed_official | 219:993c9b0acbcc | 277 | ssp_write(obj, value); |
mbed_official | 219:993c9b0acbcc | 278 | return ssp_read(obj); |
mbed_official | 219:993c9b0acbcc | 279 | } |
mbed_official | 219:993c9b0acbcc | 280 | |
mbed_official | 219:993c9b0acbcc | 281 | int spi_slave_receive(spi_t *obj) { |
mbed_official | 233:1bbc1451db33 | 282 | return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0); |
mbed_official | 219:993c9b0acbcc | 283 | }; |
mbed_official | 219:993c9b0acbcc | 284 | |
mbed_official | 219:993c9b0acbcc | 285 | int spi_slave_read(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 286 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 287 | while (!ssp_readable(obj)); |
mbed_official | 219:993c9b0acbcc | 288 | return (int)spi->DR; |
mbed_official | 219:993c9b0acbcc | 289 | } |
mbed_official | 219:993c9b0acbcc | 290 | |
mbed_official | 219:993c9b0acbcc | 291 | void spi_slave_write(spi_t *obj, int value) { |
mbed_official | 219:993c9b0acbcc | 292 | SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi); |
mbed_official | 219:993c9b0acbcc | 293 | while (!ssp_writeable(obj)); |
mbed_official | 219:993c9b0acbcc | 294 | spi->DR = (uint16_t)value; |
mbed_official | 219:993c9b0acbcc | 295 | } |
mbed_official | 219:993c9b0acbcc | 296 | |
mbed_official | 219:993c9b0acbcc | 297 | int spi_busy(spi_t *obj) { |
mbed_official | 219:993c9b0acbcc | 298 | return ssp_busy(obj); |
mbed_official | 219:993c9b0acbcc | 299 | } |
mbed_official | 219:993c9b0acbcc | 300 | |
mbed_official | 219:993c9b0acbcc | 301 | #endif |