mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Oct 13 08:00:07 2014 +0100
Revision:
344:ed24f9926218
Parent:
340:28d1f895c6fe
Synchronized with git revision 342790b0c3be997d6ed3744962729c4d9c6a9a39

Full URL: https://github.com/mbedmicro/mbed/commit/342790b0c3be997d6ed3744962729c4d9c6a9a39/

HAL: Disco_F429ZI - extensions of interrupt vectors, pin names and pin functions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 340:28d1f895c6fe 1 /* mbed Microcontroller Library
mbed_official 340:28d1f895c6fe 2 *******************************************************************************
mbed_official 340:28d1f895c6fe 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 340:28d1f895c6fe 4 * All rights reserved.
mbed_official 340:28d1f895c6fe 5 *
mbed_official 340:28d1f895c6fe 6 * Redistribution and use in source and binary forms, with or without
mbed_official 340:28d1f895c6fe 7 * modification, are permitted provided that the following conditions are met:
mbed_official 340:28d1f895c6fe 8 *
mbed_official 340:28d1f895c6fe 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 340:28d1f895c6fe 10 * this list of conditions and the following disclaimer.
mbed_official 340:28d1f895c6fe 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 340:28d1f895c6fe 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 340:28d1f895c6fe 13 * and/or other materials provided with the distribution.
mbed_official 340:28d1f895c6fe 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 340:28d1f895c6fe 15 * may be used to endorse or promote products derived from this software
mbed_official 340:28d1f895c6fe 16 * without specific prior written permission.
mbed_official 340:28d1f895c6fe 17 *
mbed_official 340:28d1f895c6fe 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 340:28d1f895c6fe 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 340:28d1f895c6fe 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 340:28d1f895c6fe 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 340:28d1f895c6fe 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 340:28d1f895c6fe 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 340:28d1f895c6fe 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 340:28d1f895c6fe 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 340:28d1f895c6fe 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 340:28d1f895c6fe 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 340:28d1f895c6fe 28 *******************************************************************************
mbed_official 340:28d1f895c6fe 29 */
mbed_official 340:28d1f895c6fe 30 #include "mbed_assert.h"
mbed_official 340:28d1f895c6fe 31 #include "spi_api.h"
mbed_official 340:28d1f895c6fe 32
mbed_official 340:28d1f895c6fe 33 #if DEVICE_SPI
mbed_official 340:28d1f895c6fe 34
mbed_official 340:28d1f895c6fe 35 #include <math.h>
mbed_official 340:28d1f895c6fe 36 #include "cmsis.h"
mbed_official 340:28d1f895c6fe 37 #include "pinmap.h"
mbed_official 340:28d1f895c6fe 38
mbed_official 340:28d1f895c6fe 39 static const PinMap PinMap_SPI_MOSI[] = {
mbed_official 340:28d1f895c6fe 40 {PA_7, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 340:28d1f895c6fe 41 {PB_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 340:28d1f895c6fe 42 {PB_15, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 43 {PC_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 44 {PC_12, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 344:ed24f9926218 45 // {PD_6, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI3)}, // error in datasheet?
mbed_official 340:28d1f895c6fe 46 {PE_6, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 47 {PE_14, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 48 {PF_9, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 340:28d1f895c6fe 49 {PF_11, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 340:28d1f895c6fe 50 {PG_14, SPI_6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI6)},
mbed_official 344:ed24f9926218 51 {PI_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 52 {NC, NC, 0}
mbed_official 340:28d1f895c6fe 53 };
mbed_official 340:28d1f895c6fe 54
mbed_official 340:28d1f895c6fe 55 static const PinMap PinMap_SPI_MISO[] = {
mbed_official 340:28d1f895c6fe 56 {PA_6, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 340:28d1f895c6fe 57 {PB_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 340:28d1f895c6fe 58 {PB_14, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 59 {PC_2, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 60 {PC_11, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 340:28d1f895c6fe 61 {PE_5, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 62 {PE_13, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 63 {PF_8, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 340:28d1f895c6fe 64 {PG_12, SPI_6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI6)},
mbed_official 344:ed24f9926218 65 {PH_7, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 344:ed24f9926218 66 {PH_5, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 344:ed24f9926218 67 {PI_2, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 68 {NC, NC, 0}
mbed_official 340:28d1f895c6fe 69 };
mbed_official 340:28d1f895c6fe 70
mbed_official 340:28d1f895c6fe 71 static const PinMap PinMap_SPI_SCLK[] = {
mbed_official 340:28d1f895c6fe 72 {PA_5, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 340:28d1f895c6fe 73 {PB_3, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI1)},
mbed_official 340:28d1f895c6fe 74 {PB_10, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 75 {PB_13, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 344:ed24f9926218 76 {PC_10, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF6_SPI3)},
mbed_official 340:28d1f895c6fe 77 {PD_3, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 78 {PE_2, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 79 {PE_12, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 80 {PF_7, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 340:28d1f895c6fe 81 {PG_13, SPI_6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI6)},
mbed_official 344:ed24f9926218 82 {PH_6, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 344:ed24f9926218 83 {PI_1, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 84 {NC, NC, 0}
mbed_official 340:28d1f895c6fe 85 };
mbed_official 340:28d1f895c6fe 86
mbed_official 340:28d1f895c6fe 87 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 340:28d1f895c6fe 88 {PA_4, SPI_1, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF5_SPI1)},
mbed_official 340:28d1f895c6fe 89 {PA_15, SPI_3, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF6_SPI3)},
mbed_official 340:28d1f895c6fe 90 {PB_9, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 91 {PB_12, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_NOPULL, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 92 {PE_4, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 93 {PE_11, SPI_4, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI4)},
mbed_official 340:28d1f895c6fe 94 {PF_6, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 340:28d1f895c6fe 95 {PG_8, SPI_6, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI6)},
mbed_official 344:ed24f9926218 96 {PH_5, SPI_5, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI5)},
mbed_official 344:ed24f9926218 97 {PI_0, SPI_2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF5_SPI2)},
mbed_official 340:28d1f895c6fe 98 {NC, NC, 0}
mbed_official 340:28d1f895c6fe 99 };
mbed_official 340:28d1f895c6fe 100
mbed_official 340:28d1f895c6fe 101 static SPI_HandleTypeDef SpiHandle;
mbed_official 340:28d1f895c6fe 102
mbed_official 340:28d1f895c6fe 103 static void init_spi(spi_t *obj)
mbed_official 340:28d1f895c6fe 104 {
mbed_official 340:28d1f895c6fe 105 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 106
mbed_official 340:28d1f895c6fe 107 __HAL_SPI_DISABLE(&SpiHandle);
mbed_official 340:28d1f895c6fe 108
mbed_official 340:28d1f895c6fe 109 SpiHandle.Init.Mode = obj->mode;
mbed_official 340:28d1f895c6fe 110 SpiHandle.Init.BaudRatePrescaler = obj->br_presc;
mbed_official 340:28d1f895c6fe 111 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
mbed_official 340:28d1f895c6fe 112 SpiHandle.Init.CLKPhase = obj->cpha;
mbed_official 340:28d1f895c6fe 113 SpiHandle.Init.CLKPolarity = obj->cpol;
mbed_official 340:28d1f895c6fe 114 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
mbed_official 340:28d1f895c6fe 115 SpiHandle.Init.CRCPolynomial = 7;
mbed_official 340:28d1f895c6fe 116 SpiHandle.Init.DataSize = obj->bits;
mbed_official 340:28d1f895c6fe 117 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
mbed_official 340:28d1f895c6fe 118 SpiHandle.Init.NSS = obj->nss;
mbed_official 340:28d1f895c6fe 119 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
mbed_official 340:28d1f895c6fe 120
mbed_official 340:28d1f895c6fe 121 HAL_SPI_Init(&SpiHandle);
mbed_official 340:28d1f895c6fe 122
mbed_official 340:28d1f895c6fe 123 __HAL_SPI_ENABLE(&SpiHandle);
mbed_official 340:28d1f895c6fe 124 }
mbed_official 340:28d1f895c6fe 125
mbed_official 340:28d1f895c6fe 126 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
mbed_official 340:28d1f895c6fe 127 {
mbed_official 340:28d1f895c6fe 128 // Determine the SPI to use
mbed_official 340:28d1f895c6fe 129 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 340:28d1f895c6fe 130 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 340:28d1f895c6fe 131 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 340:28d1f895c6fe 132 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 340:28d1f895c6fe 133
mbed_official 340:28d1f895c6fe 134 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 340:28d1f895c6fe 135 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 340:28d1f895c6fe 136
mbed_official 340:28d1f895c6fe 137 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 340:28d1f895c6fe 138 MBED_ASSERT(obj->spi != (SPIName)NC);
mbed_official 340:28d1f895c6fe 139
mbed_official 340:28d1f895c6fe 140 // Enable SPI clock
mbed_official 340:28d1f895c6fe 141 if (obj->spi == SPI_1) {
mbed_official 340:28d1f895c6fe 142 __SPI1_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 143 }
mbed_official 340:28d1f895c6fe 144 if (obj->spi == SPI_2) {
mbed_official 340:28d1f895c6fe 145 __SPI2_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 146 }
mbed_official 340:28d1f895c6fe 147 if (obj->spi == SPI_3) {
mbed_official 340:28d1f895c6fe 148 __SPI3_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 149 }
mbed_official 340:28d1f895c6fe 150 if (obj->spi == SPI_4) {
mbed_official 340:28d1f895c6fe 151 __SPI4_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 152 }
mbed_official 340:28d1f895c6fe 153 if (obj->spi == SPI_5) {
mbed_official 340:28d1f895c6fe 154 __SPI5_CLK_ENABLE();
mbed_official 340:28d1f895c6fe 155 }
mbed_official 340:28d1f895c6fe 156
mbed_official 340:28d1f895c6fe 157 // Configure the SPI pins
mbed_official 340:28d1f895c6fe 158 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 340:28d1f895c6fe 159 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 340:28d1f895c6fe 160 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 340:28d1f895c6fe 161
mbed_official 340:28d1f895c6fe 162 // Save new values
mbed_official 340:28d1f895c6fe 163 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 340:28d1f895c6fe 164 obj->cpol = SPI_POLARITY_LOW;
mbed_official 340:28d1f895c6fe 165 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 340:28d1f895c6fe 166 obj->br_presc = SPI_BAUDRATEPRESCALER_256;
mbed_official 340:28d1f895c6fe 167
mbed_official 340:28d1f895c6fe 168 obj->pin_miso = miso;
mbed_official 340:28d1f895c6fe 169 obj->pin_mosi = mosi;
mbed_official 340:28d1f895c6fe 170 obj->pin_sclk = sclk;
mbed_official 340:28d1f895c6fe 171 obj->pin_ssel = ssel;
mbed_official 340:28d1f895c6fe 172
mbed_official 340:28d1f895c6fe 173 if (ssel == NC) { // SW NSS Master mode
mbed_official 340:28d1f895c6fe 174 obj->mode = SPI_MODE_MASTER;
mbed_official 340:28d1f895c6fe 175 obj->nss = SPI_NSS_SOFT;
mbed_official 340:28d1f895c6fe 176 } else { // Slave
mbed_official 340:28d1f895c6fe 177 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 340:28d1f895c6fe 178 obj->mode = SPI_MODE_SLAVE;
mbed_official 340:28d1f895c6fe 179 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 340:28d1f895c6fe 180 }
mbed_official 340:28d1f895c6fe 181
mbed_official 340:28d1f895c6fe 182 init_spi(obj);
mbed_official 340:28d1f895c6fe 183 }
mbed_official 340:28d1f895c6fe 184
mbed_official 340:28d1f895c6fe 185 void spi_free(spi_t *obj)
mbed_official 340:28d1f895c6fe 186 {
mbed_official 340:28d1f895c6fe 187 // Reset SPI and disable clock
mbed_official 340:28d1f895c6fe 188 if (obj->spi == SPI_1) {
mbed_official 340:28d1f895c6fe 189 __SPI1_FORCE_RESET();
mbed_official 340:28d1f895c6fe 190 __SPI1_RELEASE_RESET();
mbed_official 340:28d1f895c6fe 191 __SPI1_CLK_DISABLE();
mbed_official 340:28d1f895c6fe 192 }
mbed_official 340:28d1f895c6fe 193 if (obj->spi == SPI_2) {
mbed_official 340:28d1f895c6fe 194 __SPI2_FORCE_RESET();
mbed_official 340:28d1f895c6fe 195 __SPI2_RELEASE_RESET();
mbed_official 340:28d1f895c6fe 196 __SPI2_CLK_DISABLE();
mbed_official 340:28d1f895c6fe 197 }
mbed_official 340:28d1f895c6fe 198 if (obj->spi == SPI_3) {
mbed_official 340:28d1f895c6fe 199 __SPI3_FORCE_RESET();
mbed_official 340:28d1f895c6fe 200 __SPI3_RELEASE_RESET();
mbed_official 340:28d1f895c6fe 201 __SPI3_CLK_DISABLE();
mbed_official 340:28d1f895c6fe 202 }
mbed_official 340:28d1f895c6fe 203 if (obj->spi == SPI_4) {
mbed_official 340:28d1f895c6fe 204 __SPI4_FORCE_RESET();
mbed_official 340:28d1f895c6fe 205 __SPI4_RELEASE_RESET();
mbed_official 340:28d1f895c6fe 206 __SPI4_CLK_DISABLE();
mbed_official 340:28d1f895c6fe 207 }
mbed_official 340:28d1f895c6fe 208 if (obj->spi == SPI_5) {
mbed_official 340:28d1f895c6fe 209 __SPI5_FORCE_RESET();
mbed_official 340:28d1f895c6fe 210 __SPI5_RELEASE_RESET();
mbed_official 340:28d1f895c6fe 211 __SPI5_CLK_DISABLE();
mbed_official 340:28d1f895c6fe 212 }
mbed_official 340:28d1f895c6fe 213
mbed_official 340:28d1f895c6fe 214 // Configure GPIOs
mbed_official 340:28d1f895c6fe 215 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 340:28d1f895c6fe 216 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 340:28d1f895c6fe 217 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 340:28d1f895c6fe 218 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
mbed_official 340:28d1f895c6fe 219 }
mbed_official 340:28d1f895c6fe 220
mbed_official 340:28d1f895c6fe 221 void spi_format(spi_t *obj, int bits, int mode, int slave)
mbed_official 340:28d1f895c6fe 222 {
mbed_official 340:28d1f895c6fe 223 // Save new values
mbed_official 340:28d1f895c6fe 224 if (bits == 16) {
mbed_official 340:28d1f895c6fe 225 obj->bits = SPI_DATASIZE_16BIT;
mbed_official 340:28d1f895c6fe 226 } else {
mbed_official 340:28d1f895c6fe 227 obj->bits = SPI_DATASIZE_8BIT;
mbed_official 340:28d1f895c6fe 228 }
mbed_official 340:28d1f895c6fe 229
mbed_official 340:28d1f895c6fe 230 switch (mode) {
mbed_official 340:28d1f895c6fe 231 case 0:
mbed_official 340:28d1f895c6fe 232 obj->cpol = SPI_POLARITY_LOW;
mbed_official 340:28d1f895c6fe 233 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 340:28d1f895c6fe 234 break;
mbed_official 340:28d1f895c6fe 235 case 1:
mbed_official 340:28d1f895c6fe 236 obj->cpol = SPI_POLARITY_LOW;
mbed_official 340:28d1f895c6fe 237 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 340:28d1f895c6fe 238 break;
mbed_official 340:28d1f895c6fe 239 case 2:
mbed_official 340:28d1f895c6fe 240 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 340:28d1f895c6fe 241 obj->cpha = SPI_PHASE_1EDGE;
mbed_official 340:28d1f895c6fe 242 break;
mbed_official 340:28d1f895c6fe 243 default:
mbed_official 340:28d1f895c6fe 244 obj->cpol = SPI_POLARITY_HIGH;
mbed_official 340:28d1f895c6fe 245 obj->cpha = SPI_PHASE_2EDGE;
mbed_official 340:28d1f895c6fe 246 break;
mbed_official 340:28d1f895c6fe 247 }
mbed_official 340:28d1f895c6fe 248
mbed_official 340:28d1f895c6fe 249 if (slave == 0) {
mbed_official 340:28d1f895c6fe 250 obj->mode = SPI_MODE_MASTER;
mbed_official 340:28d1f895c6fe 251 obj->nss = SPI_NSS_SOFT;
mbed_official 340:28d1f895c6fe 252 } else {
mbed_official 340:28d1f895c6fe 253 obj->mode = SPI_MODE_SLAVE;
mbed_official 340:28d1f895c6fe 254 obj->nss = SPI_NSS_HARD_INPUT;
mbed_official 340:28d1f895c6fe 255 }
mbed_official 340:28d1f895c6fe 256
mbed_official 340:28d1f895c6fe 257 init_spi(obj);
mbed_official 340:28d1f895c6fe 258 }
mbed_official 340:28d1f895c6fe 259
mbed_official 340:28d1f895c6fe 260 void spi_frequency(spi_t *obj, int hz)
mbed_official 340:28d1f895c6fe 261 {
mbed_official 340:28d1f895c6fe 262 // Values depend of PCLK2: 100 MHz
mbed_official 340:28d1f895c6fe 263 if ((obj->spi == SPI_1) || (obj->spi == SPI_4) || (obj->spi == SPI_5)) {
mbed_official 340:28d1f895c6fe 264 if (hz < 700000) {
mbed_official 340:28d1f895c6fe 265 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 391 kHz
mbed_official 340:28d1f895c6fe 266 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 340:28d1f895c6fe 267 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 781 kHz
mbed_official 340:28d1f895c6fe 268 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 340:28d1f895c6fe 269 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 1.56 MHz
mbed_official 340:28d1f895c6fe 270 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 340:28d1f895c6fe 271 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 3.13 MHz
mbed_official 340:28d1f895c6fe 272 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 340:28d1f895c6fe 273 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 6.25 MHz
mbed_official 340:28d1f895c6fe 274 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 340:28d1f895c6fe 275 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 12.5 MHz
mbed_official 340:28d1f895c6fe 276 } else if ((hz >= 25000000) && (hz < 50000000)) {
mbed_official 340:28d1f895c6fe 277 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 25 MHz
mbed_official 340:28d1f895c6fe 278 } else { // >= 50000000
mbed_official 340:28d1f895c6fe 279 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 50 MHz
mbed_official 340:28d1f895c6fe 280 }
mbed_official 340:28d1f895c6fe 281 }
mbed_official 340:28d1f895c6fe 282
mbed_official 340:28d1f895c6fe 283 // Values depend of PCLK1: 50 MHz
mbed_official 340:28d1f895c6fe 284 if ((obj->spi == SPI_2) || (obj->spi == SPI_3)) {
mbed_official 340:28d1f895c6fe 285 if (hz < 400000) {
mbed_official 340:28d1f895c6fe 286 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 195 kHz
mbed_official 340:28d1f895c6fe 287 } else if ((hz >= 400000) && (hz < 700000)) {
mbed_official 340:28d1f895c6fe 288 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 391 kHz
mbed_official 340:28d1f895c6fe 289 } else if ((hz >= 700000) && (hz < 1000000)) {
mbed_official 340:28d1f895c6fe 290 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 781 MHz
mbed_official 340:28d1f895c6fe 291 } else if ((hz >= 1000000) && (hz < 3000000)) {
mbed_official 340:28d1f895c6fe 292 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.56 MHz
mbed_official 340:28d1f895c6fe 293 } else if ((hz >= 3000000) && (hz < 6000000)) {
mbed_official 340:28d1f895c6fe 294 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3.13 MHz
mbed_official 340:28d1f895c6fe 295 } else if ((hz >= 6000000) && (hz < 12000000)) {
mbed_official 340:28d1f895c6fe 296 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6.25 MHz
mbed_official 340:28d1f895c6fe 297 } else if ((hz >= 12000000) && (hz < 25000000)) {
mbed_official 340:28d1f895c6fe 298 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12.5 MHz
mbed_official 340:28d1f895c6fe 299 } else { // >= 25000000
mbed_official 340:28d1f895c6fe 300 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 25 MHz
mbed_official 340:28d1f895c6fe 301 }
mbed_official 340:28d1f895c6fe 302 }
mbed_official 340:28d1f895c6fe 303
mbed_official 340:28d1f895c6fe 304 init_spi(obj);
mbed_official 340:28d1f895c6fe 305 }
mbed_official 340:28d1f895c6fe 306
mbed_official 340:28d1f895c6fe 307 static inline int ssp_readable(spi_t *obj)
mbed_official 340:28d1f895c6fe 308 {
mbed_official 340:28d1f895c6fe 309 int status;
mbed_official 340:28d1f895c6fe 310 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 311 // Check if data is received
mbed_official 340:28d1f895c6fe 312 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 340:28d1f895c6fe 313 return status;
mbed_official 340:28d1f895c6fe 314 }
mbed_official 340:28d1f895c6fe 315
mbed_official 340:28d1f895c6fe 316 static inline int ssp_writeable(spi_t *obj)
mbed_official 340:28d1f895c6fe 317 {
mbed_official 340:28d1f895c6fe 318 int status;
mbed_official 340:28d1f895c6fe 319 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 320 // Check if data is transmitted
mbed_official 340:28d1f895c6fe 321 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 340:28d1f895c6fe 322 return status;
mbed_official 340:28d1f895c6fe 323 }
mbed_official 340:28d1f895c6fe 324
mbed_official 340:28d1f895c6fe 325 static inline void ssp_write(spi_t *obj, int value)
mbed_official 340:28d1f895c6fe 326 {
mbed_official 340:28d1f895c6fe 327 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 328 while (!ssp_writeable(obj));
mbed_official 340:28d1f895c6fe 329 spi->DR = (uint16_t)value;
mbed_official 340:28d1f895c6fe 330 }
mbed_official 340:28d1f895c6fe 331
mbed_official 340:28d1f895c6fe 332 static inline int ssp_read(spi_t *obj)
mbed_official 340:28d1f895c6fe 333 {
mbed_official 340:28d1f895c6fe 334 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 335 while (!ssp_readable(obj));
mbed_official 340:28d1f895c6fe 336 return (int)spi->DR;
mbed_official 340:28d1f895c6fe 337 }
mbed_official 340:28d1f895c6fe 338
mbed_official 340:28d1f895c6fe 339 static inline int ssp_busy(spi_t *obj)
mbed_official 340:28d1f895c6fe 340 {
mbed_official 340:28d1f895c6fe 341 int status;
mbed_official 340:28d1f895c6fe 342 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 343 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 340:28d1f895c6fe 344 return status;
mbed_official 340:28d1f895c6fe 345 }
mbed_official 340:28d1f895c6fe 346
mbed_official 340:28d1f895c6fe 347 int spi_master_write(spi_t *obj, int value)
mbed_official 340:28d1f895c6fe 348 {
mbed_official 340:28d1f895c6fe 349 ssp_write(obj, value);
mbed_official 340:28d1f895c6fe 350 return ssp_read(obj);
mbed_official 340:28d1f895c6fe 351 }
mbed_official 340:28d1f895c6fe 352
mbed_official 340:28d1f895c6fe 353 int spi_slave_receive(spi_t *obj)
mbed_official 340:28d1f895c6fe 354 {
mbed_official 340:28d1f895c6fe 355 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
mbed_official 340:28d1f895c6fe 356 };
mbed_official 340:28d1f895c6fe 357
mbed_official 340:28d1f895c6fe 358 int spi_slave_read(spi_t *obj)
mbed_official 340:28d1f895c6fe 359 {
mbed_official 340:28d1f895c6fe 360 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 361 while (!ssp_readable(obj));
mbed_official 340:28d1f895c6fe 362 return (int)spi->DR;
mbed_official 340:28d1f895c6fe 363 }
mbed_official 340:28d1f895c6fe 364
mbed_official 340:28d1f895c6fe 365 void spi_slave_write(spi_t *obj, int value)
mbed_official 340:28d1f895c6fe 366 {
mbed_official 340:28d1f895c6fe 367 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 340:28d1f895c6fe 368 while (!ssp_writeable(obj));
mbed_official 340:28d1f895c6fe 369 spi->DR = (uint16_t)value;
mbed_official 340:28d1f895c6fe 370 }
mbed_official 340:28d1f895c6fe 371
mbed_official 340:28d1f895c6fe 372 int spi_busy(spi_t *obj)
mbed_official 340:28d1f895c6fe 373 {
mbed_official 340:28d1f895c6fe 374 return ssp_busy(obj);
mbed_official 340:28d1f895c6fe 375 }
mbed_official 340:28d1f895c6fe 376
mbed_official 340:28d1f895c6fe 377 #endif