mbed library with additional peripherals for ST F401 board

Fork of mbed-src by mbed official

This mbed LIB has additional peripherals for ST F401 board

  • UART2 : PA_3 rx, PA_2 tx
  • UART3 : PC_7 rx, PC_6 tx
  • I2C2 : PB_3 SDA, PB_10 SCL
  • I2C3 : PB_4 SDA, PA_8 SCL
Committer:
mbed_official
Date:
Mon Feb 10 19:00:06 2014 +0000
Revision:
88:81f18c97d490
Parent:
70:c1fbde68b492
Synchronized with git revision 82b58af42c386d68aa6f12d8cfcae87f0f448b73

Full URL: https://github.com/mbedmicro/mbed/commit/82b58af42c386d68aa6f12d8cfcae87f0f448b73/

[NUCLEO_F401RE] Corrections in uvision exporter, GPIOs setting and SPI.

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 52:a51c77007319 60 {NC, NC, 0}
mbed_official 52:a51c77007319 61 };
mbed_official 52:a51c77007319 62
mbed_official 56:99eb381a3269 63 static void init_spi(spi_t *obj) {
mbed_official 56:99eb381a3269 64 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 56:99eb381a3269 65 SPI_InitTypeDef SPI_InitStructure;
mbed_official 56:99eb381a3269 66
mbed_official 56:99eb381a3269 67 SPI_Cmd(spi, DISABLE);
mbed_official 52:a51c77007319 68
mbed_official 56:99eb381a3269 69 SPI_InitStructure.SPI_Mode = obj->mode;
mbed_official 56:99eb381a3269 70 SPI_InitStructure.SPI_NSS = obj->nss;
mbed_official 56:99eb381a3269 71 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
mbed_official 56:99eb381a3269 72 SPI_InitStructure.SPI_DataSize = obj->bits;
mbed_official 56:99eb381a3269 73 SPI_InitStructure.SPI_CPOL = obj->cpol;
mbed_official 56:99eb381a3269 74 SPI_InitStructure.SPI_CPHA = obj->cpha;
mbed_official 56:99eb381a3269 75 SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
mbed_official 56:99eb381a3269 76 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
mbed_official 56:99eb381a3269 77 SPI_InitStructure.SPI_CRCPolynomial = 7;
mbed_official 56:99eb381a3269 78 SPI_Init(spi, &SPI_InitStructure);
mbed_official 56:99eb381a3269 79
mbed_official 56:99eb381a3269 80 SPI_Cmd(spi, ENABLE);
mbed_official 56:99eb381a3269 81 }
mbed_official 56:99eb381a3269 82
mbed_official 56:99eb381a3269 83 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
mbed_official 52:a51c77007319 84 // Determine the SPI to use
mbed_official 52:a51c77007319 85 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
mbed_official 52:a51c77007319 86 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
mbed_official 52:a51c77007319 87 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
mbed_official 52:a51c77007319 88 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
mbed_official 52:a51c77007319 89
mbed_official 52:a51c77007319 90 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
mbed_official 52:a51c77007319 91 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
mbed_official 52:a51c77007319 92
mbed_official 52:a51c77007319 93 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
mbed_official 52:a51c77007319 94
mbed_official 52:a51c77007319 95 if (obj->spi == (SPIName)NC) {
mbed_official 52:a51c77007319 96 error("SPI pinout mapping failed");
mbed_official 52:a51c77007319 97 }
mbed_official 52:a51c77007319 98
mbed_official 52:a51c77007319 99 // Enable SPI clock
mbed_official 52:a51c77007319 100 if (obj->spi == SPI_1) {
mbed_official 52:a51c77007319 101 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
mbed_official 52:a51c77007319 102 }
mbed_official 52:a51c77007319 103
mbed_official 52:a51c77007319 104 // Configure the SPI pins
mbed_official 52:a51c77007319 105 pinmap_pinout(mosi, PinMap_SPI_MOSI);
mbed_official 52:a51c77007319 106 pinmap_pinout(miso, PinMap_SPI_MISO);
mbed_official 52:a51c77007319 107 pinmap_pinout(sclk, PinMap_SPI_SCLK);
mbed_official 52:a51c77007319 108
mbed_official 52:a51c77007319 109 // Save new values
mbed_official 52:a51c77007319 110 obj->bits = SPI_DataSize_8b;
mbed_official 52:a51c77007319 111 obj->cpol = SPI_CPOL_Low;
mbed_official 52:a51c77007319 112 obj->cpha = SPI_CPHA_1Edge;
mbed_official 88:81f18c97d490 113 obj->br_presc = SPI_BaudRatePrescaler_256; // 1MHz
mbed_official 52:a51c77007319 114
mbed_official 52:a51c77007319 115 if (ssel == NC) { // Master
mbed_official 52:a51c77007319 116 obj->mode = SPI_Mode_Master;
mbed_official 52:a51c77007319 117 obj->nss = SPI_NSS_Soft;
mbed_official 52:a51c77007319 118 }
mbed_official 52:a51c77007319 119 else { // Slave
mbed_official 52:a51c77007319 120 pinmap_pinout(ssel, PinMap_SPI_SSEL);
mbed_official 52:a51c77007319 121 obj->mode = SPI_Mode_Slave;
mbed_official 56:99eb381a3269 122 obj->nss = SPI_NSS_Soft;
mbed_official 52:a51c77007319 123 }
mbed_official 52:a51c77007319 124
mbed_official 56:99eb381a3269 125 init_spi(obj);
mbed_official 52:a51c77007319 126 }
mbed_official 52:a51c77007319 127
mbed_official 52:a51c77007319 128 void spi_free(spi_t *obj) {
mbed_official 52:a51c77007319 129 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 130 SPI_I2S_DeInit(spi);
mbed_official 52:a51c77007319 131 }
mbed_official 52:a51c77007319 132
mbed_official 56:99eb381a3269 133 void spi_format(spi_t *obj, int bits, int mode, int slave) {
mbed_official 52:a51c77007319 134 // Save new values
mbed_official 52:a51c77007319 135 if (bits == 8) {
mbed_official 52:a51c77007319 136 obj->bits = SPI_DataSize_8b;
mbed_official 52:a51c77007319 137 }
mbed_official 52:a51c77007319 138 else {
mbed_official 52:a51c77007319 139 obj->bits = SPI_DataSize_16b;
mbed_official 52:a51c77007319 140 }
mbed_official 52:a51c77007319 141
mbed_official 52:a51c77007319 142 switch (mode) {
mbed_official 52:a51c77007319 143 case 0:
mbed_official 52:a51c77007319 144 obj->cpol = SPI_CPOL_Low;
mbed_official 52:a51c77007319 145 obj->cpha = SPI_CPHA_1Edge;
mbed_official 52:a51c77007319 146 break;
mbed_official 52:a51c77007319 147 case 1:
mbed_official 52:a51c77007319 148 obj->cpol = SPI_CPOL_Low;
mbed_official 52:a51c77007319 149 obj->cpha = SPI_CPHA_2Edge;
mbed_official 52:a51c77007319 150 break;
mbed_official 52:a51c77007319 151 case 2:
mbed_official 52:a51c77007319 152 obj->cpol = SPI_CPOL_High;
mbed_official 52:a51c77007319 153 obj->cpha = SPI_CPHA_1Edge;
mbed_official 52:a51c77007319 154 break;
mbed_official 52:a51c77007319 155 default:
mbed_official 52:a51c77007319 156 obj->cpol = SPI_CPOL_High;
mbed_official 52:a51c77007319 157 obj->cpha = SPI_CPHA_2Edge;
mbed_official 52:a51c77007319 158 break;
mbed_official 52:a51c77007319 159 }
mbed_official 52:a51c77007319 160
mbed_official 52:a51c77007319 161 if (slave == 0) {
mbed_official 52:a51c77007319 162 obj->mode = SPI_Mode_Master;
mbed_official 52:a51c77007319 163 obj->nss = SPI_NSS_Soft;
mbed_official 52:a51c77007319 164 }
mbed_official 52:a51c77007319 165 else {
mbed_official 52:a51c77007319 166 obj->mode = SPI_Mode_Slave;
mbed_official 52:a51c77007319 167 obj->nss = SPI_NSS_Hard;
mbed_official 52:a51c77007319 168 }
mbed_official 52:a51c77007319 169
mbed_official 56:99eb381a3269 170 init_spi(obj);
mbed_official 52:a51c77007319 171 }
mbed_official 52:a51c77007319 172
mbed_official 52:a51c77007319 173 void spi_frequency(spi_t *obj, int hz) {
mbed_official 52:a51c77007319 174 // Choose the baud rate divisor (between 2 and 256)
mbed_official 88:81f18c97d490 175 uint32_t divisor = SystemCoreClock / hz;
mbed_official 52:a51c77007319 176
mbed_official 52:a51c77007319 177 // Find the nearest power-of-2
mbed_official 52:a51c77007319 178 divisor = (divisor > 0 ? divisor-1 : 0);
mbed_official 52:a51c77007319 179 divisor |= divisor >> 1;
mbed_official 52:a51c77007319 180 divisor |= divisor >> 2;
mbed_official 52:a51c77007319 181 divisor |= divisor >> 4;
mbed_official 52:a51c77007319 182 divisor |= divisor >> 8;
mbed_official 52:a51c77007319 183 divisor |= divisor >> 16;
mbed_official 52:a51c77007319 184 divisor++;
mbed_official 52:a51c77007319 185
mbed_official 52:a51c77007319 186 uint32_t baud_rate = __builtin_ffs(divisor) - 2;
mbed_official 52:a51c77007319 187
mbed_official 52:a51c77007319 188 // Save new value
mbed_official 52:a51c77007319 189 obj->br_presc = ((baud_rate > 7) ? (7 << 3) : (baud_rate << 3));
mbed_official 52:a51c77007319 190
mbed_official 56:99eb381a3269 191 init_spi(obj);
mbed_official 52:a51c77007319 192 }
mbed_official 52:a51c77007319 193
mbed_official 52:a51c77007319 194 static inline int ssp_readable(spi_t *obj) {
mbed_official 52:a51c77007319 195 int status;
mbed_official 52:a51c77007319 196 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 197 // Check if data is received
mbed_official 52:a51c77007319 198 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_RXNE) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 199 return status;
mbed_official 52:a51c77007319 200 }
mbed_official 52:a51c77007319 201
mbed_official 52:a51c77007319 202 static inline int ssp_writeable(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 transmitted
mbed_official 52:a51c77007319 206 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_TXE) != 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 void ssp_write(spi_t *obj, int value) {
mbed_official 52:a51c77007319 211 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 212 while (!ssp_writeable(obj));
mbed_official 52:a51c77007319 213 SPI_I2S_SendData(spi, (uint16_t)value);
mbed_official 52:a51c77007319 214 }
mbed_official 52:a51c77007319 215
mbed_official 52:a51c77007319 216 static inline int ssp_read(spi_t *obj) {
mbed_official 52:a51c77007319 217 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 218 while (!ssp_readable(obj));
mbed_official 52:a51c77007319 219 return (int)SPI_I2S_ReceiveData(spi);
mbed_official 52:a51c77007319 220 }
mbed_official 52:a51c77007319 221
mbed_official 52:a51c77007319 222 static inline int ssp_busy(spi_t *obj) {
mbed_official 52:a51c77007319 223 int status;
mbed_official 52:a51c77007319 224 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 225 status = ((SPI_I2S_GetFlagStatus(spi, SPI_I2S_FLAG_BSY) != RESET) ? 1 : 0);
mbed_official 52:a51c77007319 226 return status;
mbed_official 52:a51c77007319 227 }
mbed_official 52:a51c77007319 228
mbed_official 52:a51c77007319 229 int spi_master_write(spi_t *obj, int value) {
mbed_official 52:a51c77007319 230 ssp_write(obj, value);
mbed_official 52:a51c77007319 231 return ssp_read(obj);
mbed_official 52:a51c77007319 232 }
mbed_official 52:a51c77007319 233
mbed_official 52:a51c77007319 234 int spi_slave_receive(spi_t *obj) {
mbed_official 52:a51c77007319 235 return (ssp_readable(obj) && !ssp_busy(obj)) ? (1) : (0);
mbed_official 52:a51c77007319 236 };
mbed_official 52:a51c77007319 237
mbed_official 52:a51c77007319 238 int spi_slave_read(spi_t *obj) {
mbed_official 52:a51c77007319 239 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 240 return (int)SPI_I2S_ReceiveData(spi);
mbed_official 52:a51c77007319 241 }
mbed_official 52:a51c77007319 242
mbed_official 52:a51c77007319 243 void spi_slave_write(spi_t *obj, int value) {
mbed_official 52:a51c77007319 244 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
mbed_official 52:a51c77007319 245 while (!ssp_writeable(obj));
mbed_official 52:a51c77007319 246 SPI_I2S_SendData(spi, (uint16_t)value);
mbed_official 52:a51c77007319 247 }
mbed_official 52:a51c77007319 248
mbed_official 52:a51c77007319 249 int spi_busy(spi_t *obj) {
mbed_official 52:a51c77007319 250 return ssp_busy(obj);
mbed_official 52:a51c77007319 251 }
mbed_official 52:a51c77007319 252
mbed_official 52:a51c77007319 253 #endif