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 SPI clock = 48 MHz (APB clock)
180 obj->br_presc = SPI_BAUDRATEPRESCALER_256; // 188 kHz
181 } else if ((hz >= 375000) && (hz < 750000)) {
182 obj->br_presc = SPI_BAUDRATEPRESCALER_128; // 375 kHz
183 } else if ((hz >= 750000) && (hz < 1000000)) {
184 obj->br_presc = SPI_BAUDRATEPRESCALER_64; // 750 kHz
185 } else if ((hz >= 1000000) && (hz < 3000000)) {
186 obj->br_presc = SPI_BAUDRATEPRESCALER_32; // 1.5 MHz
187 } else if ((hz >= 3000000) && (hz < 6000000)) {
188 obj->br_presc = SPI_BAUDRATEPRESCALER_16; // 3 MHz
189 } else if ((hz >= 6000000) && (hz < 12000000)) {
190 obj->br_presc = SPI_BAUDRATEPRESCALER_8; // 6 MHz
191 } else if ((hz >= 12000000) && (hz < 24000000)) {
192 obj->br_presc = SPI_BAUDRATEPRESCALER_4; // 12 MHz
193 } else { // >= 24000000
194 obj->br_presc = SPI_BAUDRATEPRESCALER_2; // 24 MHz
200 static inline int ssp_readable(spi_t *obj)
203 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
204 // Check if data is received
205 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
209 static inline int ssp_writeable(spi_t *obj)
212 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
213 // Check if data is transmitted
214 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
218 static inline void ssp_write(spi_t *obj, int value)
220 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
221 while (!ssp_writeable(obj));
222 if (obj->bits == SPI_DATASIZE_8BIT) {
223 // Force 8-bit access to the data register
224 uint8_t *p_spi_dr = 0;
225 p_spi_dr = (uint8_t *) & (spi->DR);
226 *p_spi_dr = (uint8_t)value;
227 } else { // SPI_DATASIZE_16BIT
228 spi->DR = (uint16_t)value;
232 static inline int ssp_read(spi_t *obj)
234 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
235 while (!ssp_readable(obj));
236 if (obj->bits == SPI_DATASIZE_8BIT) {
237 // Force 8-bit access to the data register
238 uint8_t *p_spi_dr = 0;
239 p_spi_dr = (uint8_t *) & (spi->DR);
240 return (int)(*p_spi_dr);
246 static inline int ssp_busy(spi_t *obj)
249 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
250 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
254 int spi_master_write(spi_t *obj, int value)
256 ssp_write(obj, value);
257 return ssp_read(obj);
260 int spi_slave_receive(spi_t *obj)
262 return ((ssp_readable(obj) && !ssp_busy(obj)) ? 1 : 0);
265 int spi_slave_read(spi_t *obj)
267 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
268 while (!ssp_readable(obj));
269 if (obj->bits == SPI_DATASIZE_8BIT) {
270 // Force 8-bit access to the data register
271 uint8_t *p_spi_dr = 0;
272 p_spi_dr = (uint8_t *) & (spi->DR);
273 return (int)(*p_spi_dr);
279 void spi_slave_write(spi_t *obj, int value)
281 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
282 while (!ssp_writeable(obj));
283 if (obj->bits == SPI_DATASIZE_8BIT) {
284 // Force 8-bit access to the data register
285 uint8_t *p_spi_dr = 0;
286 p_spi_dr = (uint8_t *) & (spi->DR);
287 *p_spi_dr = (uint8_t)value;
288 } else { // SPI_DATASIZE_16BIT
289 spi->DR = (uint16_t)value;
293 int spi_busy(spi_t *obj)
295 return ssp_busy(obj);