mbed library sources

Fork of mbed-src by mbed official

Committer:
moirans2
Date:
Wed Jan 14 20:53:08 2015 +0000
Revision:
445:9a3ffe6cfa19
Parent:
414:4ec4c5b614b0
internal clock stm32L051

Who changed what in which revision?

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