mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Mon Feb 03 09:30:05 2014 +0000
Revision:
84:f54042cbc282
Synchronized with git revision bbbd8699601c42149ccf0c37bc42bb6856ccc4c6

Full URL: https://github.com/mbedmicro/mbed/commit/bbbd8699601c42149ccf0c37bc42bb6856ccc4c6/

[NUCLEO_L152RE/F030_R8] SPI, I2C, Ticker, PWM updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 84:f54042cbc282 1 /* mbed Microcontroller Library
mbed_official 84:f54042cbc282 2 *******************************************************************************
mbed_official 84:f54042cbc282 3 * Copyright (c) 2014, STMicroelectronics
mbed_official 84:f54042cbc282 4 * All rights reserved.
mbed_official 84:f54042cbc282 5 *
mbed_official 84:f54042cbc282 6 * Redistribution and use in source and binary forms, with or without
mbed_official 84:f54042cbc282 7 * modification, are permitted provided that the following conditions are met:
mbed_official 84:f54042cbc282 8 *
mbed_official 84:f54042cbc282 9 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 84:f54042cbc282 10 * this list of conditions and the following disclaimer.
mbed_official 84:f54042cbc282 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 84:f54042cbc282 12 * this list of conditions and the following disclaimer in the documentation
mbed_official 84:f54042cbc282 13 * and/or other materials provided with the distribution.
mbed_official 84:f54042cbc282 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 84:f54042cbc282 15 * may be used to endorse or promote products derived from this software
mbed_official 84:f54042cbc282 16 * without specific prior written permission.
mbed_official 84:f54042cbc282 17 *
mbed_official 84:f54042cbc282 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 84:f54042cbc282 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 84:f54042cbc282 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 84:f54042cbc282 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 84:f54042cbc282 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 84:f54042cbc282 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 84:f54042cbc282 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 84:f54042cbc282 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 84:f54042cbc282 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 84:f54042cbc282 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 84:f54042cbc282 28 *******************************************************************************
mbed_official 84:f54042cbc282 29 */
mbed_official 84:f54042cbc282 30 #include "spi_api.h"
mbed_official 84:f54042cbc282 31
mbed_official 84:f54042cbc282 32 #if DEVICE_SPI
mbed_official 84:f54042cbc282 33
mbed_official 84:f54042cbc282 34 #include <math.h>
mbed_official 84:f54042cbc282 35 #include "cmsis.h"
mbed_official 84:f54042cbc282 36 #include "pinmap.h"
mbed_official 84:f54042cbc282 37 #include "error.h"
mbed_official 84:f54042cbc282 38
mbed_official 84:f54042cbc282 39 static const PinMap PinMap_SPI_MOSI[] = {
mbed_official 84:f54042cbc282 40 {PA_7, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 84:f54042cbc282 41 {NC, NC, 0}
mbed_official 84:f54042cbc282 42 };
mbed_official 84:f54042cbc282 43
mbed_official 84:f54042cbc282 44 static const PinMap PinMap_SPI_MISO[] = {
mbed_official 84:f54042cbc282 45 {PA_6, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 84:f54042cbc282 46 {NC, NC, 0}
mbed_official 84:f54042cbc282 47 };
mbed_official 84:f54042cbc282 48
mbed_official 84:f54042cbc282 49 static const PinMap PinMap_SPI_SCLK[] = {
mbed_official 84:f54042cbc282 50 {PA_5, SPI_1, STM_PIN_DATA(GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_NOPULL, GPIO_AF_0)},
mbed_official 84:f54042cbc282 51 {NC, NC, 0}
mbed_official 84:f54042cbc282 52 };
mbed_official 84:f54042cbc282 53
mbed_official 84:f54042cbc282 54 // Only used in Slave mode
mbed_official 84:f54042cbc282 55 static const PinMap PinMap_SPI_SSEL[] = {
mbed_official 84:f54042cbc282 56 {PB_6, SPI_1, STM_PIN_DATA(GPIO_Mode_IN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0)}, // Generic IO, not real H/W NSS pin
mbed_official 84:f54042cbc282 57 {NC, NC, 0}
mbed_official 84:f54042cbc282 58 };
mbed_official 84:f54042cbc282 59
mbed_official 84:f54042cbc282 60 static void init_spi(spi_t *obj) {
mbed_official 84:f54042cbc282 61 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 62 SPI_InitTypeDef SPI_InitStructure;
mbed_official 84:f54042cbc282 63
mbed_official 84:f54042cbc282 64 SPI_Cmd(spi, DISABLE);
mbed_official 84:f54042cbc282 65
mbed_official 84:f54042cbc282 66 SPI_InitStructure.SPI_Mode = obj->mode;
mbed_official 84:f54042cbc282 67 SPI_InitStructure.SPI_NSS = obj->nss;
mbed_official 84:f54042cbc282 68 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
mbed_official 84:f54042cbc282 69 SPI_InitStructure.SPI_DataSize = obj->bits;
mbed_official 84:f54042cbc282 70 SPI_InitStructure.SPI_CPOL = obj->cpol;
mbed_official 84:f54042cbc282 71 SPI_InitStructure.SPI_CPHA = obj->cpha;
mbed_official 84:f54042cbc282 72 SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
mbed_official 84:f54042cbc282 73 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
mbed_official 84:f54042cbc282 74 SPI_InitStructure.SPI_CRCPolynomial = 7;
mbed_official 84:f54042cbc282 75 SPI_Init(spi, &SPI_InitStructure);
mbed_official 84:f54042cbc282 76
mbed_official 84:f54042cbc282 77 SPI_RxFIFOThresholdConfig(spi, SPI_RxFIFOThreshold_QF);
mbed_official 84:f54042cbc282 78
mbed_official 84:f54042cbc282 79 SPI_Cmd(spi, ENABLE);
mbed_official 84:f54042cbc282 80 }
mbed_official 84:f54042cbc282 81
mbed_official 84:f54042cbc282 82 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
mbed_official 84:f54042cbc282 83 // Determine the SPI to use
mbed_official 84:f54042cbc282 84 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 84:f54042cbc282 85 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 84:f54042cbc282 86 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 84:f54042cbc282 87 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 84:f54042cbc282 88
mbed_official 84:f54042cbc282 89 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 84:f54042cbc282 90 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 84:f54042cbc282 91
mbed_official 84:f54042cbc282 92 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 84:f54042cbc282 93
mbed_official 84:f54042cbc282 94 if (obj->spi == (SPIName)NC) {
mbed_official 84:f54042cbc282 95 error("SPI pinout mapping failed");
mbed_official 84:f54042cbc282 96 }
mbed_official 84:f54042cbc282 97
mbed_official 84:f54042cbc282 98 // Enable SPI clock
mbed_official 84:f54042cbc282 99 if (obj->spi == SPI_1) {
mbed_official 84:f54042cbc282 100 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
mbed_official 84:f54042cbc282 101 }
mbed_official 84:f54042cbc282 102 if (obj->spi == SPI_2) {
mbed_official 84:f54042cbc282 103 RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
mbed_official 84:f54042cbc282 104 }
mbed_official 84:f54042cbc282 105
mbed_official 84:f54042cbc282 106 // Configure the SPI pins
mbed_official 84:f54042cbc282 107 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 84:f54042cbc282 108 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 84:f54042cbc282 109 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 84:f54042cbc282 110
mbed_official 84:f54042cbc282 111 // Save new values
mbed_official 84:f54042cbc282 112 obj->bits = SPI_DataSize_8b;
mbed_official 84:f54042cbc282 113 obj->cpol = SPI_CPOL_Low;
mbed_official 84:f54042cbc282 114 obj->cpha = SPI_CPHA_1Edge;
mbed_official 84:f54042cbc282 115 obj->br_presc = SPI_BaudRatePrescaler_8; // 1 MHz
mbed_official 84:f54042cbc282 116
mbed_official 84:f54042cbc282 117 if (ssel == NC) { // Master
mbed_official 84:f54042cbc282 118 obj->mode = SPI_Mode_Master;
mbed_official 84:f54042cbc282 119 obj->nss = SPI_NSS_Soft;
mbed_official 84:f54042cbc282 120 }
mbed_official 84:f54042cbc282 121 else { // Slave
mbed_official 84:f54042cbc282 122 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 84:f54042cbc282 123 obj->mode = SPI_Mode_Slave;
mbed_official 84:f54042cbc282 124 obj->nss = SPI_NSS_Soft;
mbed_official 84:f54042cbc282 125 }
mbed_official 84:f54042cbc282 126
mbed_official 84:f54042cbc282 127 init_spi(obj);
mbed_official 84:f54042cbc282 128 }
mbed_official 84:f54042cbc282 129
mbed_official 84:f54042cbc282 130 void spi_free(spi_t *obj) {
mbed_official 84:f54042cbc282 131 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 132 SPI_I2S_DeInit(spi);
mbed_official 84:f54042cbc282 133 }
mbed_official 84:f54042cbc282 134
mbed_official 84:f54042cbc282 135 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 84:f54042cbc282 136 // Save new values
mbed_official 84:f54042cbc282 137 if (bits == 8) {
mbed_official 84:f54042cbc282 138 obj->bits = SPI_DataSize_8b;
mbed_official 84:f54042cbc282 139 }
mbed_official 84:f54042cbc282 140 else {
mbed_official 84:f54042cbc282 141 obj->bits = SPI_DataSize_16b;
mbed_official 84:f54042cbc282 142 }
mbed_official 84:f54042cbc282 143
mbed_official 84:f54042cbc282 144 switch (mode) {
mbed_official 84:f54042cbc282 145 case 0:
mbed_official 84:f54042cbc282 146 obj->cpol = SPI_CPOL_Low;
mbed_official 84:f54042cbc282 147 obj->cpha = SPI_CPHA_1Edge;
mbed_official 84:f54042cbc282 148 break;
mbed_official 84:f54042cbc282 149 case 1:
mbed_official 84:f54042cbc282 150 obj->cpol = SPI_CPOL_Low;
mbed_official 84:f54042cbc282 151 obj->cpha = SPI_CPHA_2Edge;
mbed_official 84:f54042cbc282 152 break;
mbed_official 84:f54042cbc282 153 case 2:
mbed_official 84:f54042cbc282 154 obj->cpol = SPI_CPOL_High;
mbed_official 84:f54042cbc282 155 obj->cpha = SPI_CPHA_1Edge;
mbed_official 84:f54042cbc282 156 break;
mbed_official 84:f54042cbc282 157 default:
mbed_official 84:f54042cbc282 158 obj->cpol = SPI_CPOL_High;
mbed_official 84:f54042cbc282 159 obj->cpha = SPI_CPHA_2Edge;
mbed_official 84:f54042cbc282 160 break;
mbed_official 84:f54042cbc282 161 }
mbed_official 84:f54042cbc282 162
mbed_official 84:f54042cbc282 163 if (slave == 0) {
mbed_official 84:f54042cbc282 164 obj->mode = SPI_Mode_Master;
mbed_official 84:f54042cbc282 165 obj->nss = SPI_NSS_Soft;
mbed_official 84:f54042cbc282 166 }
mbed_official 84:f54042cbc282 167 else {
mbed_official 84:f54042cbc282 168 obj->mode = SPI_Mode_Slave;
mbed_official 84:f54042cbc282 169 obj->nss = SPI_NSS_Hard;
mbed_official 84:f54042cbc282 170 }
mbed_official 84:f54042cbc282 171
mbed_official 84:f54042cbc282 172 init_spi(obj);
mbed_official 84:f54042cbc282 173 }
mbed_official 84:f54042cbc282 174
mbed_official 84:f54042cbc282 175 void spi_frequency(spi_t *obj, int hz) {
mbed_official 84:f54042cbc282 176 // Get SPI clock frequency
mbed_official 84:f54042cbc282 177 uint32_t PCLK = SystemCoreClock;
mbed_official 84:f54042cbc282 178
mbed_official 84:f54042cbc282 179 // Choose the baud rate divisor (between 2 and 256)
mbed_official 84:f54042cbc282 180 uint32_t divisor = PCLK / hz;
mbed_official 84:f54042cbc282 181
mbed_official 84:f54042cbc282 182 // Find the nearest power-of-2
mbed_official 84:f54042cbc282 183 divisor = (divisor > 0 ? divisor-1 : 0);
mbed_official 84:f54042cbc282 184 divisor |= divisor >> 1;
mbed_official 84:f54042cbc282 185 divisor |= divisor >> 2;
mbed_official 84:f54042cbc282 186 divisor |= divisor >> 4;
mbed_official 84:f54042cbc282 187 divisor |= divisor >> 8;
mbed_official 84:f54042cbc282 188 divisor |= divisor >> 16;
mbed_official 84:f54042cbc282 189 divisor++;
mbed_official 84:f54042cbc282 190
mbed_official 84:f54042cbc282 191 uint32_t baud_rate = __builtin_ffs(divisor) - 2;
mbed_official 84:f54042cbc282 192
mbed_official 84:f54042cbc282 193 // Save new value
mbed_official 84:f54042cbc282 194 obj->br_presc = ((baud_rate > 7) ? (7 << 3) : (baud_rate << 3));
mbed_official 84:f54042cbc282 195
mbed_official 84:f54042cbc282 196 init_spi(obj);
mbed_official 84:f54042cbc282 197 }
mbed_official 84:f54042cbc282 198
mbed_official 84:f54042cbc282 199 static inline int ssp_readable(spi_t *obj) {
mbed_official 84:f54042cbc282 200 int status;
mbed_official 84:f54042cbc282 201 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 202 // Check if data is received
mbed_official 84:f54042cbc282 203 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 84:f54042cbc282 204 return status;
mbed_official 84:f54042cbc282 205 }
mbed_official 84:f54042cbc282 206
mbed_official 84:f54042cbc282 207 static inline int ssp_writeable(spi_t *obj) {
mbed_official 84:f54042cbc282 208 int status;
mbed_official 84:f54042cbc282 209 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 210 // Check if data is transmitted
mbed_official 84:f54042cbc282 211 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != RESET) ? 1 : 0);
mbed_official 84:f54042cbc282 212 return status;
mbed_official 84:f54042cbc282 213 }
mbed_official 84:f54042cbc282 214
mbed_official 84:f54042cbc282 215 static inline void ssp_write(spi_t *obj, int value) {
mbed_official 84:f54042cbc282 216 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 217 while (!ssp_writeable(obj));
mbed_official 84:f54042cbc282 218 SPI_SendData8(spi, (uint8_t)value);
mbed_official 84:f54042cbc282 219 }
mbed_official 84:f54042cbc282 220
mbed_official 84:f54042cbc282 221 static inline int ssp_read(spi_t *obj) {
mbed_official 84:f54042cbc282 222 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 223 while (!ssp_readable(obj));
mbed_official 84:f54042cbc282 224 return (int)SPI_ReceiveData8(spi);
mbed_official 84:f54042cbc282 225 }
mbed_official 84:f54042cbc282 226
mbed_official 84:f54042cbc282 227 static inline int ssp_busy(spi_t *obj) {
mbed_official 84:f54042cbc282 228 int status;
mbed_official 84:f54042cbc282 229 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 230 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 84:f54042cbc282 231 return status;
mbed_official 84:f54042cbc282 232 }
mbed_official 84:f54042cbc282 233
mbed_official 84:f54042cbc282 234 int spi_master_write(spi_t *obj, int value) {
mbed_official 84:f54042cbc282 235 ssp_write(obj, value);
mbed_official 84:f54042cbc282 236 return ssp_read(obj);
mbed_official 84:f54042cbc282 237 }
mbed_official 84:f54042cbc282 238
mbed_official 84:f54042cbc282 239 int spi_slave_receive(spi_t *obj) {
mbed_official 84:f54042cbc282 240 return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
mbed_official 84:f54042cbc282 241 };
mbed_official 84:f54042cbc282 242
mbed_official 84:f54042cbc282 243 int spi_slave_read(spi_t *obj) {
mbed_official 84:f54042cbc282 244 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 245 return (int)SPI_ReceiveData8(spi);
mbed_official 84:f54042cbc282 246 }
mbed_official 84:f54042cbc282 247
mbed_official 84:f54042cbc282 248 void spi_slave_write(spi_t *obj, int value) {
mbed_official 84:f54042cbc282 249 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 84:f54042cbc282 250 while (!ssp_writeable(obj));
mbed_official 84:f54042cbc282 251 SPI_SendData8(spi, (uint8_t)value);
mbed_official 84:f54042cbc282 252 }
mbed_official 84:f54042cbc282 253
mbed_official 84:f54042cbc282 254 int spi_busy(spi_t *obj) {
mbed_official 84:f54042cbc282 255 return ssp_busy(obj);
mbed_official 84:f54042cbc282 256 }
mbed_official 84:f54042cbc282 257
mbed_official 84:f54042cbc282 258 #endif