mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

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:
Tue Jan 07 11:00:05 2014 +0000
Revision:
70:c1fbde68b492
Parent:
56:99eb381a3269
Child:
88:81f18c97d490
Synchronized with git revision 3f438a307904431f2782db3c8fa49946b9fc1d85

Full URL: https://github.com/mbedmicro/mbed/commit/3f438a307904431f2782db3c8fa49946b9fc1d85/

[NUCLEO_F103RB] license text changed + sleep hal updated

Who changed what in which revision?

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