1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *******************************************************************************
30 #include "mbed_assert.h"
38 #include "PeripheralPins.h"
40 static SPI_HandleTypeDef SpiHandle;
42 static void init_spi(spi_t *obj)
44 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
46 __HAL_SPI_DISABLE(&SpiHandle);
48 SpiHandle.Init.Mode = obj->mode;
49 SpiHandle.Init.BaudRatePrescaler = obj->br_presc;
50 SpiHandle.Init.Direction = SPI_DIRECTION_2LINES;
51 SpiHandle.Init.CLKPhase = obj->cpha;
52 SpiHandle.Init.CLKPolarity = obj->cpol;
53 SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
54 SpiHandle.Init.CRCPolynomial = 7;
55 SpiHandle.Init.DataSize = obj->bits;
56 SpiHandle.Init.FirstBit = SPI_FIRSTBIT_MSB;
57 SpiHandle.Init.NSS = obj->nss;
58 SpiHandle.Init.TIMode = SPI_TIMODE_DISABLED;
60 HAL_SPI_Init(&SpiHandle);
62 __HAL_SPI_ENABLE(&SpiHandle);
65 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
67 // Determine the SPI to use
68 SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
69 SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
70 SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
71 SPIName spi_ssel = (SPIName)pinmap_peripheral(ssel, PinMap_SPI_SSEL);
73 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
74 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
76 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
77 MBED_ASSERT(obj->spi != (SPIName)NC);
80 if (obj->spi == SPI_1) {
83 if (obj->spi == SPI_2) {
87 // Configure the SPI pins
88 pinmap_pinout(mosi, PinMap_SPI_MOSI);
89 pinmap_pinout(miso, PinMap_SPI_MISO);
90 pinmap_pinout(sclk, PinMap_SPI_SCLK);
93 obj->bits = SPI_DATASIZE_8BIT;
94 obj->cpol = SPI_POLARITY_LOW;
95 obj->cpha = SPI_PHASE_1EDGE;
96 obj->br_presc = SPI_BAUDRATEPRESCALER_256;
100 obj->pin_sclk = sclk;
101 obj->pin_ssel = ssel;
103 if (ssel == NC) { // SW NSS Master mode
104 obj->mode = SPI_MODE_MASTER;
105 obj->nss = SPI_NSS_SOFT;
107 pinmap_pinout(ssel, PinMap_SPI_SSEL);
108 obj->mode = SPI_MODE_SLAVE;
109 obj->nss = SPI_NSS_HARD_INPUT;
115 void spi_free(spi_t *obj)
117 // Reset SPI and disable clock
118 if (obj->spi == SPI_1) {
119 __SPI1_FORCE_RESET();
120 __SPI1_RELEASE_RESET();
121 __SPI1_CLK_DISABLE();
124 if (obj->spi == SPI_2) {
125 __SPI2_FORCE_RESET();
126 __SPI2_RELEASE_RESET();
127 __SPI2_CLK_DISABLE();
131 pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
132 pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
133 pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
134 pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
137 void spi_format(spi_t *obj, int bits, int mode, int slave)
141 obj->bits = SPI_DATASIZE_16BIT;
143 obj->bits = SPI_DATASIZE_8BIT;
148 obj->cpol = SPI_POLARITY_LOW;
149 obj->cpha = SPI_PHASE_1EDGE;
152 obj->cpol = SPI_POLARITY_LOW;
153 obj->cpha = SPI_PHASE_2EDGE;
156 obj->cpol = SPI_POLARITY_HIGH;
157 obj->cpha = SPI_PHASE_1EDGE;
160 obj->cpol = SPI_POLARITY_HIGH;
161 obj->cpha = SPI_PHASE_2EDGE;
166 obj->mode = SPI_MODE_MASTER;
167 obj->nss = SPI_NSS_SOFT;
169 obj->mode = SPI_MODE_SLAVE;
170 obj->nss = SPI_NSS_HARD_INPUT;
176 void spi_frequency(spi_t *obj, int hz)
178 // Note: The frequencies are obtained with SPI1 clock = 32 MHz (APB2 clock)
180 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 125 kHz
181 } else if ((hz >= 250000) && (hz < 500000)) {
182 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 250 kHz
183 } else if ((hz >= 500000) && (hz < 1000000)) {
184 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 500 kHz
185 } else if ((hz >= 1000000) && (hz < 2000000)) {
186 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1 MHz
187 } else if ((hz >= 2000000) && (hz < 4000000)) {
188 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 2 MHz
189 } else if ((hz >= 4000000) && (hz < 8000000)) {
190 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 4 MHz
191 } else if ((hz >= 8000000) && (hz < 16000000)) {
192 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 8 MHz
193 } else { // >= 16000000
194 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 16 MHz
199 static inline int ssp_readable(spi_t *obj)
202 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
203 // Check if data is received
204 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
208 static inline int ssp_writeable(spi_t *obj)
211 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
212 // Check if data is transmitted
213 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
217 static inline void ssp_write(spi_t *obj, int value)
219 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
220 while (!ssp_writeable(obj));
221 spi->DR = (uint16_t)value;
224 static inline int ssp_read(spi_t *obj)
226 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
227 while (!ssp_readable(obj));
231 static inline int ssp_busy(spi_t *obj)
234 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
235 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
239 int spi_master_write(spi_t *obj, int value)
241 ssp_write(obj, value);
242 return ssp_read(obj);
245 int spi_slave_receive(spi_t *obj)
247 return (ssp_readable(obj) ? 1 : 0);
250 int spi_slave_read(spi_t *obj)
252 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
253 while (!ssp_readable(obj));
257 void spi_slave_write(spi_t *obj, int value)
259 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
260 while (!ssp_writeable(obj));
261 spi->DR = (uint16_t)value;
264 int spi_busy(spi_t *obj)
266 return ssp_busy(obj);