]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32L0/spi_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32L0 / spi_api.c
1 /* mbed Microcontroller Library
2 *******************************************************************************
3 * Copyright (c) 2014, STMicroelectronics
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
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.
17 *
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 *******************************************************************************
29 */
30 #include "mbed_assert.h"
31 #include "spi_api.h"
32
33 #if DEVICE_SPI
34
35 #include <math.h>
36 #include "cmsis.h"
37 #include "pinmap.h"
38 #include "PeripheralPins.h"
39
40 static SPI_HandleTypeDef SpiHandle;
41
42 static void init_spi(spi_t *obj)
43 {
44 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
45
46 __HAL_SPI_DISABLE(&SpiHandle);
47
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;
59
60 HAL_SPI_Init(&SpiHandle);
61
62 __HAL_SPI_ENABLE(&SpiHandle);
63 }
64
65 void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
66 {
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);
72
73 SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
74 SPIName spi_cntl = (SPIName)pinmap_merge(spi_sclk, spi_ssel);
75
76 obj->spi = (SPIName)pinmap_merge(spi_data, spi_cntl);
77 MBED_ASSERT(obj->spi != (SPIName)NC);
78
79 // Enable SPI clock
80 if (obj->spi == SPI_1) {
81 __SPI1_CLK_ENABLE();
82 }
83 if (obj->spi == SPI_2) {
84 __SPI2_CLK_ENABLE();
85 }
86
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);
91
92 // Save new values
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;
97
98 obj->pin_miso = miso;
99 obj->pin_mosi = mosi;
100 obj->pin_sclk = sclk;
101 obj->pin_ssel = ssel;
102
103 if (ssel == NC) { // SW NSS Master mode
104 obj->mode = SPI_MODE_MASTER;
105 obj->nss = SPI_NSS_SOFT;
106 } else { // Slave
107 pinmap_pinout(ssel, PinMap_SPI_SSEL);
108 obj->mode = SPI_MODE_SLAVE;
109 obj->nss = SPI_NSS_HARD_INPUT;
110 }
111
112 init_spi(obj);
113 }
114
115 void spi_free(spi_t *obj)
116 {
117 // Reset SPI and disable clock
118 if (obj->spi == SPI_1) {
119 __SPI1_FORCE_RESET();
120 __SPI1_RELEASE_RESET();
121 __SPI1_CLK_DISABLE();
122 }
123
124 if (obj->spi == SPI_2) {
125 __SPI2_FORCE_RESET();
126 __SPI2_RELEASE_RESET();
127 __SPI2_CLK_DISABLE();
128 }
129
130 // Configure GPIO
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));
135 }
136
137 void spi_format(spi_t *obj, int bits, int mode, int slave)
138 {
139 // Save new values
140 if (bits == 16) {
141 obj->bits = SPI_DATASIZE_16BIT;
142 } else {
143 obj->bits = SPI_DATASIZE_8BIT;
144 }
145
146 switch (mode) {
147 case 0:
148 obj->cpol = SPI_POLARITY_LOW;
149 obj->cpha = SPI_PHASE_1EDGE;
150 break;
151 case 1:
152 obj->cpol = SPI_POLARITY_LOW;
153 obj->cpha = SPI_PHASE_2EDGE;
154 break;
155 case 2:
156 obj->cpol = SPI_POLARITY_HIGH;
157 obj->cpha = SPI_PHASE_1EDGE;
158 break;
159 default:
160 obj->cpol = SPI_POLARITY_HIGH;
161 obj->cpha = SPI_PHASE_2EDGE;
162 break;
163 }
164
165 if (slave == 0) {
166 obj->mode = SPI_MODE_MASTER;
167 obj->nss = SPI_NSS_SOFT;
168 } else {
169 obj->mode = SPI_MODE_SLAVE;
170 obj->nss = SPI_NSS_HARD_INPUT;
171 }
172
173 init_spi(obj);
174 }
175
176 void spi_frequency(spi_t *obj, int hz)
177 {
178 // Note: The frequencies are obtained with SPI1 clock = 32 MHz (APB2 clock)
179 if (hz < 250000) {
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
195 }
196 init_spi(obj);
197 }
198
199 static inline int ssp_readable(spi_t *obj)
200 {
201 int status;
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);
205 return status;
206 }
207
208 static inline int ssp_writeable(spi_t *obj)
209 {
210 int status;
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);
214 return status;
215 }
216
217 static inline void ssp_write(spi_t *obj, int value)
218 {
219 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
220 while (!ssp_writeable(obj));
221 spi->DR = (uint16_t)value;
222 }
223
224 static inline int ssp_read(spi_t *obj)
225 {
226 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
227 while (!ssp_readable(obj));
228 return (int)spi->DR;
229 }
230
231 static inline int ssp_busy(spi_t *obj)
232 {
233 int status;
234 SpiHandle.Instance = (SPI_TypeDef *)(obj->spi);
235 status = ((__HAL_SPI_GET_FLAG(&SpiHandle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
236 return status;
237 }
238
239 int spi_master_write(spi_t *obj, int value)
240 {
241 ssp_write(obj, value);
242 return ssp_read(obj);
243 }
244
245 int spi_slave_receive(spi_t *obj)
246 {
247 return (ssp_readable(obj) ? 1 : 0);
248 };
249
250 int spi_slave_read(spi_t *obj)
251 {
252 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
253 while (!ssp_readable(obj));
254 return (int)spi->DR;
255 }
256
257 void spi_slave_write(spi_t *obj, int value)
258 {
259 SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
260 while (!ssp_writeable(obj));
261 spi->DR = (uint16_t)value;
262 }
263
264 int spi_busy(spi_t *obj)
265 {
266 return ssp_busy(obj);
267 }
268
269 #endif
Imprint / Impressum