TM STM32F4xx Libraries  v1.0.0
Libraries for STM32F4xx devices from Tilen Majerle
tm_stm32f4_spi.h
1 
29 #ifndef TM_SPI_H
30 #define TM_SPI_H 200
31 
32 /* C++ detection */
33 #ifdef __cplusplus
34 extern C {
35 #endif
36 
147 #include "stm32f4xx.h"
148 #include "stm32f4xx_rcc.h"
149 #include "stm32f4xx_gpio.h"
150 #include "stm32f4xx_spi.h"
151 #include "defines.h"
152 #include "attributes.h"
153 #include "tm_stm32f4_gpio.h"
154 
164 typedef enum {
169 } TM_SPI_Mode_t;
170 
174 typedef enum {
180 
184 typedef enum {
188 
202 #define USE_SPI1
203 #define USE_SPI2
204 #define USE_SPI3
205 #ifdef SPI4
206 #define USE_SPI4
207 #else
208 #warning "SPI4 undefined. Please update library with STD drivers from ST.com"
209 #endif
210 #ifdef SPI5
211 #define USE_SPI5
212 #else
213 #warning "SPI5 undefined. Please update library with STD drivers from ST.com"
214 #endif
215 #ifdef SPI6
216 #define USE_SPI6
217 #else
218 #warning "SPI6 undefined. Please update library with STD drivers from ST.com"
219 #endif
220 
221 //----- SPI1 options start -------
222 //Options can be overwriten in defines.h file
223 #ifndef TM_SPI1_PRESCALER
224 #define TM_SPI1_PRESCALER SPI_BaudRatePrescaler_32
225 #endif
226 //Specify datasize
227 #ifndef TM_SPI1_DATASIZE
228 #define TM_SPI1_DATASIZE SPI_DataSize_8b
229 #endif
230 //Specify which bit is first
231 #ifndef TM_SPI1_FIRSTBIT
232 #define TM_SPI1_FIRSTBIT SPI_FirstBit_MSB
233 #endif
234 //Mode, master or slave
235 #ifndef TM_SPI1_MASTERSLAVE
236 #define TM_SPI1_MASTERSLAVE SPI_Mode_Master
237 #endif
238 //Specify mode of operation, clock polarity and clock phase
239 #ifndef TM_SPI1_MODE
240 #define TM_SPI1_MODE TM_SPI_Mode_0
241 #endif
242 //----- SPI1 options end -------
243 
244 //----- SPI2 options start -------
245 //Options can be overwriten in defines.h file
246 #ifndef TM_SPI2_PRESCALER
247 #define TM_SPI2_PRESCALER SPI_BaudRatePrescaler_32
248 #endif
249 //Specify datasize
250 #ifndef TM_SPI2_DATASIZE
251 #define TM_SPI2_DATASIZE SPI_DataSize_8b
252 #endif
253 //Specify which bit is first
254 #ifndef TM_SPI2_FIRSTBIT
255 #define TM_SPI2_FIRSTBIT SPI_FirstBit_MSB
256 #endif
257 //Mode, master or slave
258 #ifndef TM_SPI2_MASTERSLAVE
259 #define TM_SPI2_MASTERSLAVE SPI_Mode_Master
260 #endif
261 //Specify mode of operation, clock polarity and clock phase
262 #ifndef TM_SPI2_MODE
263 #define TM_SPI2_MODE TM_SPI_Mode_0
264 #endif
265 //----- SPI2 options end -------
266 
267 //----- SPI3 options start -------
268 //Options can be overwriten in defines.h file
269 #ifndef TM_SPI3_PRESCALER
270 #define TM_SPI3_PRESCALER SPI_BaudRatePrescaler_32
271 #endif
272 //Specify datasize
273 #ifndef TM_SPI3_DATASIZE
274 #define TM_SPI3_DATASIZE SPI_DataSize_8b
275 #endif
276 //Specify which bit is first
277 #ifndef TM_SPI3_FIRSTBIT
278 #define TM_SPI3_FIRSTBIT SPI_FirstBit_MSB
279 #endif
280 //Mode, master or slave
281 #ifndef TM_SPI3_MASTERSLAVE
282 #define TM_SPI3_MASTERSLAVE SPI_Mode_Master
283 #endif
284 //Specify mode of operation, clock polarity and clock phase
285 #ifndef TM_SPI3_MODE
286 #define TM_SPI3_MODE TM_SPI_Mode_0
287 #endif
288 //----- SPI3 options end -------
289 
290 //----- SPI4 options start -------
291 //Options can be overwriten in defines.h file
292 #ifndef TM_SPI4_PRESCALER
293 #define TM_SPI4_PRESCALER SPI_BaudRatePrescaler_32
294 #endif
295 //Specify datasize
296 #ifndef TM_SPI4_DATASIZE
297 #define TM_SPI4_DATASIZE SPI_DataSize_8b
298 #endif
299 //Specify which bit is first
300 #ifndef TM_SPI4_FIRSTBIT
301 #define TM_SPI4_FIRSTBIT SPI_FirstBit_MSB
302 #endif
303 //Mode, master or slave
304 #ifndef TM_SPI4_MASTERSLAVE
305 #define TM_SPI4_MASTERSLAVE SPI_Mode_Master
306 #endif
307 //Specify mode of operation, clock polarity and clock phase
308 #ifndef TM_SPI4_MODE
309 #define TM_SPI4_MODE TM_SPI_Mode_0
310 #endif
311 //----- SPI4 options end -------
312 
313 //----- SPI5 options start -------
314 //Options can be overwriten in defines.h file
315 #ifndef TM_SPI5_PRESCALER
316 #define TM_SPI5_PRESCALER SPI_BaudRatePrescaler_32
317 #endif
318 //Specify datasize
319 #ifndef TM_SPI5_DATASIZE
320 #define TM_SPI5_DATASIZE SPI_DataSize_8b
321 #endif
322 //Specify which bit is first
323 #ifndef TM_SPI5_FIRSTBIT
324 #define TM_SPI5_FIRSTBIT SPI_FirstBit_MSB
325 #endif
326 //Mode, master or slave
327 #ifndef TM_SPI5_MASTERSLAVE
328 #define TM_SPI5_MASTERSLAVE SPI_Mode_Master
329 #endif
330 //Specify mode of operation, clock polarity and clock phase
331 #ifndef TM_SPI5_MODE
332 #define TM_SPI5_MODE TM_SPI_Mode_0
333 #endif
334 //----- SPI5 options end -------
335 
336 //----- SPI6 options start -------
337 //Options can be overwriten in defines.h file
338 #ifndef TM_SPI6_PRESCALER
339 #define TM_SPI6_PRESCALER SPI_BaudRatePrescaler_32
340 #endif
341 //Specify datasize
342 #ifndef TM_SPI6_DATASIZE
343 #define TM_SPI6_DATASIZE SPI_DataSize_8b
344 #endif
345 //Specify which bit is first
346 #ifndef TM_SPI6_FIRSTBIT
347 #define TM_SPI6_FIRSTBIT SPI_FirstBit_MSB
348 #endif
349 //Mode, master or slave
350 #ifndef TM_SPI6_MASTERSLAVE
351 #define TM_SPI6_MASTERSLAVE SPI_Mode_Master
352 #endif
353 //Specify mode of operation, clock polarity and clock phase
354 #ifndef TM_SPI6_MODE
355 #define TM_SPI6_MODE TM_SPI_Mode_0
356 #endif
357 //----- SPI6 options end -------
358 
362 #define SPI_IS_BUSY(SPIx) (((SPIx)->SR & (SPI_SR_TXE | SPI_SR_RXNE)) == 0 || ((SPIx)->SR & SPI_SR_BSY))
363 
367 #define SPI_WAIT(SPIx) while (SPI_IS_BUSY(SPIx))
368 
372 #define SPI_CHECK_ENABLED(SPIx) if (!((SPIx)->CR1 & SPI_CR1_SPE)) {return;}
373 
377 #define SPI_CHECK_ENABLED_RESP(SPIx, val) if (!((SPIx)->CR1 & SPI_CR1_SPE)) {return (val);}
378 
395 void TM_SPI_Init(SPI_TypeDef* SPIx, TM_SPI_PinsPack_t pinspack);
396 
404 void TM_SPI_InitWithMode(SPI_TypeDef* SPIx, TM_SPI_PinsPack_t pinspack, TM_SPI_Mode_t SPI_Mode);
405 
420 void TM_SPI_InitFull(SPI_TypeDef* SPIx, TM_SPI_PinsPack_t pinspack, uint16_t SPI_BaudRatePrescaler, TM_SPI_Mode_t SPI_Mode_t, uint16_t SPI_Mode, uint16_t SPI_FirstBit);
421 
437 uint16_t TM_SPI_GetPrescalerFromMaxFrequency(SPI_TypeDef* SPIx, uint32_t MAX_SPI_Frequency);
438 
446 TM_SPI_DataSize_t TM_SPI_SetDataSize(SPI_TypeDef* SPIx, TM_SPI_DataSize_t DataSize);
447 
454 static __INLINE uint8_t TM_SPI_Send(SPI_TypeDef* SPIx, uint8_t data) {
455  /* Check if SPI is enabled */
456  SPI_CHECK_ENABLED_RESP(SPIx, 0);
457 
458  /* Wait for previous transmissions to complete if DMA TX enabled for SPI */
459  SPI_WAIT(SPIx);
460 
461  /* Fill output buffer with data */
462  SPIx->DR = data;
463 
464  /* Wait for transmission to complete */
465  SPI_WAIT(SPIx);
466 
467  /* Return data from buffer */
468  return SPIx->DR;
469 }
470 
479 void TM_SPI_SendMulti(SPI_TypeDef* SPIx, uint8_t* dataOut, uint8_t* dataIn, uint32_t count);
480 
488 void TM_SPI_WriteMulti(SPI_TypeDef* SPIx, uint8_t* dataOut, uint32_t count);
489 
499 void TM_SPI_ReadMulti(SPI_TypeDef* SPIx, uint8_t *dataIn, uint8_t dummy, uint32_t count);
500 
508 static __INLINE uint16_t TM_SPI_Send16(SPI_TypeDef* SPIx, uint8_t data) {
509  /* Check if SPI is enabled */
510  SPI_CHECK_ENABLED_RESP(SPIx, 0);
511 
512  /* Wait for previous transmissions to complete if DMA TX enabled for SPI */
513  SPI_WAIT(SPIx);
514 
515  /* Fill output buffer with data */
516  SPIx->DR = data;
517 
518  /* Wait for transmission to complete */
519  SPI_WAIT(SPIx);
520 
521  /* Return data from buffer */
522  return SPIx->DR;
523 }
524 
534 void TM_SPI_SendMulti16(SPI_TypeDef* SPIx, uint16_t* dataOut, uint16_t* dataIn, uint32_t count);
535 
544 void TM_SPI_WriteMulti16(SPI_TypeDef* SPIx, uint16_t* dataOut, uint32_t count);
545 
555 void TM_SPI_ReadMulti16(SPI_TypeDef* SPIx, uint16_t* dataIn, uint16_t dummy, uint32_t count);
556 
569 void TM_SPI_InitCustomPinsCallback(SPI_TypeDef* SPIx, uint16_t AlternateFunction);
570 
583 /* C++ detection */
584 #ifdef __cplusplus
585 }
586 #endif
587 
588 #endif
589 
TM_SPI_DataSize_t
Daza size enumeration.
Definition: tm_stm32f4_spi.h:184
Definition: tm_stm32f4_spi.h:186
Definition: tm_stm32f4_spi.h:166
Definition: tm_stm32f4_spi.h:168
Definition: tm_stm32f4_spi.h:175
TM_SPI_Mode_t
SPI modes selection.
Definition: tm_stm32f4_spi.h:164
void TM_SPI_InitWithMode(SPI_TypeDef *SPIx, TM_SPI_PinsPack_t pinspack, TM_SPI_Mode_t SPI_Mode)
Initializes SPIx peripheral with custom pinspack and SPI mode and default other settings.
Definition: tm_stm32f4_spi.h:165
static __INLINE uint8_t TM_SPI_Send(SPI_TypeDef *SPIx, uint8_t data)
Sends single byte over SPI.
Definition: tm_stm32f4_spi.h:454
void TM_SPI_InitFull(SPI_TypeDef *SPIx, TM_SPI_PinsPack_t pinspack, uint16_t SPI_BaudRatePrescaler, TM_SPI_Mode_t SPI_Mode_t, uint16_t SPI_Mode, uint16_t SPI_FirstBit)
Initializes SPIx peripheral with custom settings.
Definition: tm_stm32f4_spi.h:185
Definition: tm_stm32f4_spi.h:178
Definition: tm_stm32f4_spi.h:176
#define SPI_WAIT(SPIx)
SPI wait till end.
Definition: tm_stm32f4_spi.h:367
TM_SPI_DataSize_t TM_SPI_SetDataSize(SPI_TypeDef *SPIx, TM_SPI_DataSize_t DataSize)
Sets data size for SPI at runtime.
static __INLINE uint16_t TM_SPI_Send16(SPI_TypeDef *SPIx, uint8_t data)
Sends single byte over SPI.
Definition: tm_stm32f4_spi.h:508
void TM_SPI_WriteMulti(SPI_TypeDef *SPIx, uint8_t *dataOut, uint32_t count)
Writes multiple bytes over SPI.
void TM_SPI_SendMulti16(SPI_TypeDef *SPIx, uint16_t *dataOut, uint16_t *dataIn, uint32_t count)
Sends and receives multiple bytes over SPIx in 16-bit SPI mode.
uint16_t TM_SPI_GetPrescalerFromMaxFrequency(SPI_TypeDef *SPIx, uint32_t MAX_SPI_Frequency)
Calculates bits for SPI prescaler register to get minimal prescaler value for SPI peripheral...
void TM_SPI_SendMulti(SPI_TypeDef *SPIx, uint8_t *dataOut, uint8_t *dataIn, uint32_t count)
Sends and receives multiple bytes over SPIx.
void TM_SPI_Init(SPI_TypeDef *SPIx, TM_SPI_PinsPack_t pinspack)
Initializes SPIx peripheral with custom pinspack and default other settings.
void TM_SPI_ReadMulti(SPI_TypeDef *SPIx, uint8_t *dataIn, uint8_t dummy, uint32_t count)
Receives multiple data bytes over SPI.
void TM_SPI_WriteMulti16(SPI_TypeDef *SPIx, uint16_t *dataOut, uint32_t count)
Writes multiple data via SPI in 16-bit SPI mode.
void TM_SPI_InitCustomPinsCallback(SPI_TypeDef *SPIx, uint16_t AlternateFunction)
Init custom SPI pins for your SPIx. This is callback function and will be called from my library if n...
Definition: tm_stm32f4_spi.h:177
void TM_SPI_ReadMulti16(SPI_TypeDef *SPIx, uint16_t *dataIn, uint16_t dummy, uint32_t count)
Receives multiple data bytes over SPI in 16-bit SPI mode.
#define SPI_CHECK_ENABLED_RESP(SPIx, val)
Checks if SPI is enabled and returns value from function if not.
Definition: tm_stm32f4_spi.h:377
Definition: tm_stm32f4_spi.h:167
TM_SPI_PinsPack_t
USART PinsPack enumeration to select pins combination for USART.
Definition: tm_stm32f4_spi.h:174