-
Notifications
You must be signed in to change notification settings - Fork 3k
/
stm_spi_api.c
1515 lines (1347 loc) · 46.3 KB
/
stm_spi_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* mbed Microcontroller Library
*******************************************************************************
* Copyright (c) 2015, STMicroelectronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************
*/
#include "mbed_assert.h"
#include "mbed_error.h"
#include "mbed_debug.h"
#include "mbed_critical.h"
#include "mbed_wait_api.h"
#include "spi_api.h"
#if DEVICE_SPI
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "cmsis.h"
#include "pinmap.h"
#include "PeripheralPins.h"
#include "spi_device.h"
#if DEVICE_SPI_ASYNCH
#define SPI_INST(obj) ((SPI_TypeDef *)(obj->spi.spi))
#else
#define SPI_INST(obj) ((SPI_TypeDef *)(obj->spi))
#endif
#if DEVICE_SPI_ASYNCH
#define SPI_S(obj) (( struct spi_s *)(&(obj->spi)))
#else
#define SPI_S(obj) (( struct spi_s *)(obj))
#endif
#ifndef DEBUG_STDIO
# define DEBUG_STDIO 0
#endif
#if DEBUG_STDIO
# include <stdio.h>
# define DEBUG_PRINTF(...) do { printf(__VA_ARGS__); } while(0)
#else
# define DEBUG_PRINTF(...) {}
#endif
/* Consider 10ms as the default timeout for sending/receving 1 byte */
#define TIMEOUT_1_BYTE 10
#if defined(SPI_FLAG_FRLVL) // STM32F0 STM32F3 STM32F7 STM32L4
extern HAL_StatusTypeDef HAL_SPIEx_FlushRxFifo(SPI_HandleTypeDef *hspi);
#endif
#if defined(SPI_DATASIZE_17BIT) || defined(SPI_DATASIZE_18BIT) || defined(SPI_DATASIZE_19BIT) || defined(SPI_DATASIZE_20BIT) || \
defined(SPI_DATASIZE_21BIT) || defined(SPI_DATASIZE_22BIT) || defined(SPI_DATASIZE_23BIT) || defined(SPI_DATASIZE_24BIT) || \
defined(SPI_DATASIZE_25BIT) || defined(SPI_DATASIZE_26BIT) || defined(SPI_DATASIZE_27BIT) || defined(SPI_DATASIZE_28BIT) || \
defined(SPI_DATASIZE_29BIT) || defined(SPI_DATASIZE_30BIT) || defined(SPI_DATASIZE_31BIT) || defined(SPI_DATASIZE_32BIT)
#define HAS_32BIT_SPI_TRANSFERS 1
#endif // SPI_DATASIZE_X
/**
* Flush RX FIFO/input register of SPI interface and clear overrun flag.
*/
static inline void spi_flush_rx(spi_t *obj)
{
#if defined(SPI_FLAG_FRLVL)
HAL_SPIEx_FlushRxFifo(&(SPI_S(obj)->handle));
#endif
LL_SPI_ClearFlag_OVR(SPI_INST(obj));
}
void spi_get_capabilities(PinName ssel, bool slave, spi_capabilities_t *cap)
{
if (slave) {
cap->minimum_frequency = 200000; // 200 kHz
cap->maximum_frequency = 2000000; // 2 MHz
cap->word_length = 0x00008080; // 8 and 16 bit symbols
cap->support_slave_mode = false; // to be determined later based on ssel
cap->hw_cs_handle = false; // irrelevant in slave mode
cap->slave_delay_between_symbols_ns = 2500; // 2.5 us
cap->clk_modes = 0x0f; // all clock modes
cap->tx_rx_buffers_equal_length = false; // rx/tx buffers can have different sizes
#if DEVICE_SPI_ASYNCH
cap->async_mode = true;
#else
cap->async_mode = false;
#endif
} else {
cap->minimum_frequency = 200000; // 200 kHz
cap->maximum_frequency = 2000000; // 2 MHz
cap->word_length = STM32_SPI_CAPABILITY_WORD_LENGTH; // Defined in spi_device.h
cap->support_slave_mode = false; // to be determined later based on ssel
cap->hw_cs_handle = false; // to be determined later based on ssel
cap->slave_delay_between_symbols_ns = 0; // irrelevant in master mode
cap->clk_modes = 0x0f; // all clock modes
cap->tx_rx_buffers_equal_length = false; // rx/tx buffers can have different sizes
#if DEVICE_SPI_ASYNCH
cap->async_mode = true;
#else
cap->async_mode = false;
#endif
}
// check if given ssel pin is in the cs pinmap
const PinMap *cs_pins = spi_master_cs_pinmap();
while (cs_pins->pin != NC) {
if (cs_pins->pin == ssel) {
#if DEVICE_SPISLAVE
cap->support_slave_mode = true;
#endif
cap->hw_cs_handle = true;
break;
}
cs_pins++;
}
}
void init_spi(spi_t *obj)
{
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
__HAL_SPI_DISABLE(handle);
DEBUG_PRINTF("init_spi: instance=0x%8X\r\n", (int)handle->Instance);
if (HAL_SPI_Init(handle) != HAL_OK) {
error("Cannot initialize SPI");
}
/* In some cases after SPI object re-creation SPI overrun flag may not
* be cleared, so clear RX data explicitly to prevent any transmissions errors */
spi_flush_rx(obj);
/* In case of standard 4 wires SPI,PI can be kept enabled all time
* and SCK will only be generated during the write operations. But in case
* of 3 wires, it should be only enabled during rd/wr unitary operations,
* which is handled inside STM32 HAL layer.
*/
if (handle->Init.Direction == SPI_DIRECTION_2LINES) {
__HAL_SPI_ENABLE(handle);
}
}
SPIName spi_get_peripheral_name(PinName mosi, PinName miso, PinName sclk)
{
SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
SPIName spi_sclk = (SPIName)pinmap_peripheral(sclk, PinMap_SPI_SCLK);
SPIName spi_per;
// MISO or MOSI may be not connected
if (miso == NC) {
spi_per = (SPIName)pinmap_merge(spi_mosi, spi_sclk);
} else if (mosi == NC) {
spi_per = (SPIName)pinmap_merge(spi_miso, spi_sclk);
} else {
SPIName spi_data = (SPIName)pinmap_merge(spi_mosi, spi_miso);
spi_per = (SPIName)pinmap_merge(spi_data, spi_sclk);
}
return spi_per;
}
#if STATIC_PINMAP_READY
#define SPI_INIT_DIRECT spi_init_direct
void spi_init_direct(spi_t *obj, const spi_pinmap_t *pinmap)
#else
#define SPI_INIT_DIRECT _spi_init_direct
static void _spi_init_direct(spi_t *obj, const spi_pinmap_t *pinmap)
#endif
{
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
spiobj->spi = (SPIName)pinmap->peripheral;
MBED_ASSERT(spiobj->spi != (SPIName)NC);
#if defined(SPI_IP_VERSION_V2)
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
#endif /* SPI_IP_VERSION_V2 */
#if defined SPI1_BASE
// Enable SPI clock
if (spiobj->spi == SPI_1) {
#if defined(SPI_IP_VERSION_V2)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SPI1;
#if defined (RCC_SPI123CLKSOURCE_PLL)
PeriphClkInit.Spi123ClockSelection = RCC_SPI123CLKSOURCE_PLL;
#else
PeriphClkInit.Spi1ClockSelection = RCC_SPI1CLKSOURCE_SYSCLK;
#endif
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
error("HAL_RCCEx_PeriphCLKConfig\n");
}
#endif /* SPI_IP_VERSION_V2 */
__HAL_RCC_SPI1_FORCE_RESET();
__HAL_RCC_SPI1_RELEASE_RESET();
__HAL_RCC_SPI1_CLK_ENABLE();
spiobj->spiIRQ = SPI1_IRQn;
}
#endif
#if defined SPI2_BASE
if (spiobj->spi == SPI_2) {
#if defined(SPI_IP_VERSION_V2)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SPI2;
#if defined (RCC_SPI123CLKSOURCE_PLL)
PeriphClkInit.Spi123ClockSelection = RCC_SPI123CLKSOURCE_PLL;
#else
PeriphClkInit.Spi2ClockSelection = RCC_SPI2CLKSOURCE_SYSCLK;
#endif
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
error("HAL_RCCEx_PeriphCLKConfig\n");
}
#endif /* SPI_IP_VERSION_V2 */
__HAL_RCC_SPI2_FORCE_RESET();
__HAL_RCC_SPI2_RELEASE_RESET();
__HAL_RCC_SPI2_CLK_ENABLE();
spiobj->spiIRQ = SPI2_IRQn;
}
#endif
#if defined SPI3_BASE
if (spiobj->spi == SPI_3) {
#if defined(SPI_IP_VERSION_V2)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SPI3;
#if defined (RCC_SPI123CLKSOURCE_PLL)
PeriphClkInit.Spi123ClockSelection = RCC_SPI123CLKSOURCE_PLL;
#else
PeriphClkInit.Spi3ClockSelection = RCC_SPI3CLKSOURCE_SYSCLK;
#endif
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
error("HAL_RCCEx_PeriphCLKConfig\n");
}
#endif /* SPI_IP_VERSION_V2 */
__HAL_RCC_SPI3_FORCE_RESET();
__HAL_RCC_SPI3_RELEASE_RESET();
__HAL_RCC_SPI3_CLK_ENABLE();
spiobj->spiIRQ = SPI3_IRQn;
}
#endif
#if defined SPI4_BASE
if (spiobj->spi == SPI_4) {
#if defined(SPI_IP_VERSION_V2)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SPI4;
PeriphClkInit.Spi45ClockSelection = RCC_SPI45CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
error("HAL_RCCEx_PeriphCLKConfig\n");
}
#endif /* SPI_IP_VERSION_V2 */
__HAL_RCC_SPI4_FORCE_RESET();
__HAL_RCC_SPI4_RELEASE_RESET();
__HAL_RCC_SPI4_CLK_ENABLE();
spiobj->spiIRQ = SPI4_IRQn;
}
#endif
#if defined SPI5_BASE
if (spiobj->spi == SPI_5) {
#if defined(SPI_IP_VERSION_V2)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SPI5;
PeriphClkInit.Spi45ClockSelection = RCC_SPI45CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
error("HAL_RCCEx_PeriphCLKConfig\n");
}
#endif /* SPI_IP_VERSION_V2 */
__HAL_RCC_SPI5_FORCE_RESET();
__HAL_RCC_SPI5_RELEASE_RESET();
__HAL_RCC_SPI5_CLK_ENABLE();
spiobj->spiIRQ = SPI5_IRQn;
}
#endif
#if defined SPI6_BASE
if (spiobj->spi == SPI_6) {
#if defined(SPI_IP_VERSION_V2)
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SPI6;
PeriphClkInit.Spi6ClockSelection = RCC_SPI6CLKSOURCE_PCLK4;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
error("HAL_RCCEx_PeriphCLKConfig\n");
}
#endif /* SPI_IP_VERSION_V2 */
__HAL_RCC_SPI6_FORCE_RESET();
__HAL_RCC_SPI6_RELEASE_RESET();
__HAL_RCC_SPI6_CLK_ENABLE();
spiobj->spiIRQ = SPI6_IRQn;
}
#endif
// Configure the SPI pins
pin_function(pinmap->mosi_pin, pinmap->mosi_function);
pin_mode(pinmap->mosi_pin, PullDown); // Pull Down is set for output line
pin_function(pinmap->miso_pin, pinmap->miso_function);
pin_mode(pinmap->miso_pin, PullNone);
pin_function(pinmap->sclk_pin, pinmap->sclk_function);
pin_mode(pinmap->sclk_pin, PullNone);
spiobj->pin_miso = pinmap->miso_pin;
spiobj->pin_mosi = pinmap->mosi_pin;
spiobj->pin_sclk = pinmap->sclk_pin;
spiobj->pin_ssel = pinmap->ssel_pin;
if (pinmap->ssel_pin != NC) {
pin_function(pinmap->ssel_pin, pinmap->ssel_function);
pin_mode(pinmap->ssel_pin, PullNone);
handle->Init.NSS = SPI_NSS_HARD_OUTPUT;
#if defined(SPI_NSS_PULSE_ENABLE)
handle->Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
#endif
} else {
handle->Init.NSS = SPI_NSS_SOFT;
#if defined(SPI_NSS_PULSE_DISABLE)
handle->Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
#endif
}
/* Fill default value */
handle->Instance = SPI_INST(obj);
handle->Init.Mode = SPI_MODE_MASTER;
handle->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
if (pinmap->miso_pin != NC) {
handle->Init.Direction = SPI_DIRECTION_2LINES;
} else {
handle->Init.Direction = SPI_DIRECTION_1LINE;
}
handle->Init.CLKPhase = SPI_PHASE_1EDGE;
handle->Init.CLKPolarity = SPI_POLARITY_LOW;
handle->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
handle->Init.CRCPolynomial = 7;
#if defined(SPI_CRC_LENGTH_DATASIZE)
handle->Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
#endif
handle->Init.DataSize = SPI_DATASIZE_8BIT;
handle->Init.FirstBit = SPI_FIRSTBIT_MSB;
handle->Init.TIMode = SPI_TIMODE_DISABLE;
#if defined (SPI_IP_VERSION_V2)
handle->Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
handle->Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
handle->Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
handle->Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
handle->Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
handle->Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
handle->Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
handle->Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
handle->Init.IOSwap = SPI_IO_SWAP_DISABLE;
#if defined(SPI_RDY_MASTER_MANAGEMENT_INTERNALLY)
handle->Init.ReadyMasterManagement = SPI_RDY_MASTER_MANAGEMENT_INTERNALLY;
handle->Init.ReadyPolarity = SPI_RDY_POLARITY_HIGH;
#endif
#endif /* SPI_IP_VERSION_V2 */
/*
* According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
* or PULLUP the SCK pin according the polarity used.
*/
pin_mode(spiobj->pin_sclk, (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? PullDown : PullUp);
init_spi(obj);
}
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel)
{
// determine the SPI to use
uint32_t spi_mosi = pinmap_peripheral(mosi, PinMap_SPI_MOSI);
uint32_t spi_miso = pinmap_peripheral(miso, PinMap_SPI_MISO);
uint32_t spi_sclk = pinmap_peripheral(sclk, PinMap_SPI_SCLK);
uint32_t spi_ssel = pinmap_peripheral(ssel, PinMap_SPI_SSEL);
uint32_t spi_data = pinmap_merge(spi_mosi, spi_miso);
uint32_t spi_cntl = pinmap_merge(spi_sclk, spi_ssel);
int peripheral = (int)pinmap_merge(spi_data, spi_cntl);
// pin out the spi pins
int mosi_function = (int)pinmap_find_function(mosi, PinMap_SPI_MOSI);
int miso_function = (int)pinmap_find_function(miso, PinMap_SPI_MISO);
int sclk_function = (int)pinmap_find_function(sclk, PinMap_SPI_SCLK);
int ssel_function = (int)pinmap_find_function(ssel, PinMap_SPI_SSEL);
const spi_pinmap_t explicit_spi_pinmap = {peripheral, mosi, mosi_function, miso, miso_function, sclk, sclk_function, ssel, ssel_function};
SPI_INIT_DIRECT(obj, &explicit_spi_pinmap);
}
void spi_free(spi_t *obj)
{
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
DEBUG_PRINTF("spi_free\r\n");
__HAL_SPI_DISABLE(handle);
HAL_SPI_DeInit(handle);
#if defined(DUAL_CORE) && (TARGET_STM32H7)
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
}
#endif /* DUAL_CORE */
#if defined SPI1_BASE
// Reset SPI and disable clock
if (spiobj->spi == SPI_1) {
__HAL_RCC_SPI1_FORCE_RESET();
__HAL_RCC_SPI1_RELEASE_RESET();
__HAL_RCC_SPI1_CLK_DISABLE();
}
#endif
#if defined SPI2_BASE
if (spiobj->spi == SPI_2) {
__HAL_RCC_SPI2_FORCE_RESET();
__HAL_RCC_SPI2_RELEASE_RESET();
__HAL_RCC_SPI2_CLK_DISABLE();
}
#endif
#if defined SPI3_BASE
if (spiobj->spi == SPI_3) {
__HAL_RCC_SPI3_FORCE_RESET();
__HAL_RCC_SPI3_RELEASE_RESET();
__HAL_RCC_SPI3_CLK_DISABLE();
}
#endif
#if defined SPI4_BASE
if (spiobj->spi == SPI_4) {
__HAL_RCC_SPI4_FORCE_RESET();
__HAL_RCC_SPI4_RELEASE_RESET();
__HAL_RCC_SPI4_CLK_DISABLE();
}
#endif
#if defined SPI5_BASE
if (spiobj->spi == SPI_5) {
__HAL_RCC_SPI5_FORCE_RESET();
__HAL_RCC_SPI5_RELEASE_RESET();
__HAL_RCC_SPI5_CLK_DISABLE();
}
#endif
#if defined SPI6_BASE
if (spiobj->spi == SPI_6) {
__HAL_RCC_SPI6_FORCE_RESET();
__HAL_RCC_SPI6_RELEASE_RESET();
__HAL_RCC_SPI6_CLK_DISABLE();
}
#endif
#if defined(DUAL_CORE) && (TARGET_STM32H7)
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
#endif /* DUAL_CORE */
// Configure GPIOs back to reset value
pin_function(spiobj->pin_miso, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
pin_function(spiobj->pin_mosi, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
pin_function(spiobj->pin_sclk, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
if (handle->Init.NSS != SPI_NSS_SOFT) {
pin_function(spiobj->pin_ssel, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0));
}
}
void spi_format(spi_t *obj, int bits, int mode, int slave)
{
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
PinMode pull = PullNone;
DEBUG_PRINTF("spi_format, bits:%d, mode:%d, slave?:%d\r\n", bits, mode, slave);
// Save new values
uint32_t DataSize;
switch (bits) {
#if defined(SPI_DATASIZE_4BIT)
case 4:
DataSize = SPI_DATASIZE_4BIT;
break;
#endif
#if defined(SPI_DATASIZE_5BIT)
case 5:
DataSize = SPI_DATASIZE_5BIT;
break;
#endif
#if defined(SPI_DATASIZE_6BIT)
case 6:
DataSize = SPI_DATASIZE_6BIT;
break;
#endif
#if defined(SPI_DATASIZE_7BIT)
case 7:
DataSize = SPI_DATASIZE_7BIT;
break;
#endif
#if defined(SPI_DATASIZE_9BIT)
case 9:
DataSize = SPI_DATASIZE_9BIT;
break;
#endif
#if defined(SPI_DATASIZE_10BIT)
case 10:
DataSize = SPI_DATASIZE_10BIT;
break;
#endif
#if defined(SPI_DATASIZE_11BIT)
case 11:
DataSize = SPI_DATASIZE_11BIT;
break;
#endif
#if defined(SPI_DATASIZE_12BIT)
case 12:
DataSize = SPI_DATASIZE_12BIT;
break;
#endif
#if defined(SPI_DATASIZE_13BIT)
case 13:
DataSize = SPI_DATASIZE_13BIT;
break;
#endif
#if defined(SPI_DATASIZE_14BIT)
case 14:
DataSize = SPI_DATASIZE_14BIT;
break;
#endif
#if defined(SPI_DATASIZE_15BIT)
case 15:
DataSize = SPI_DATASIZE_15BIT;
break;
#endif
#if defined(SPI_DATASIZE_17BIT)
case 17:
DataSize = SPI_DATASIZE_17BIT;
break;
#endif
#if defined(SPI_DATASIZE_18BIT)
case 18:
DataSize = SPI_DATASIZE_18BIT;
break;
#endif
#if defined(SPI_DATASIZE_19BIT)
case 19:
DataSize = SPI_DATASIZE_19BIT;
break;
#endif
#if defined(SPI_DATASIZE_20BIT)
case 20:
DataSize = SPI_DATASIZE_20BIT;
break;
#endif
#if defined(SPI_DATASIZE_21BIT)
case 21:
DataSize = SPI_DATASIZE_21BIT;
break;
#endif
#if defined(SPI_DATASIZE_22BIT)
case 22:
DataSize = SPI_DATASIZE_22BIT;
break;
#endif
#if defined(SPI_DATASIZE_23BIT)
case 23:
DataSize = SPI_DATASIZE_23BIT;
break;
#endif
#if defined(SPI_DATASIZE_24BIT)
case 24:
DataSize = SPI_DATASIZE_24BIT;
break;
#endif
#if defined(SPI_DATASIZE_25BIT)
case 25:
DataSize = SPI_DATASIZE_25BIT;
break;
#endif
#if defined(SPI_DATASIZE_26BIT)
case 26:
DataSize = SPI_DATASIZE_26BIT;
break;
#endif
#if defined(SPI_DATASIZE_27BIT)
case 27:
DataSize = SPI_DATASIZE_27BIT;
break;
#endif
#if defined(SPI_DATASIZE_28BIT)
case 28:
DataSize = SPI_DATASIZE_28BIT;
break;
#endif
#if defined(SPI_DATASIZE_29BIT)
case 29:
DataSize = SPI_DATASIZE_29BIT;
break;
#endif
#if defined(SPI_DATASIZE_30BIT)
case 30:
DataSize = SPI_DATASIZE_30BIT;
break;
#endif
#if defined(SPI_DATASIZE_31BIT)
case 31:
DataSize = SPI_DATASIZE_31BIT;
break;
#endif
#if defined(SPI_DATASIZE_32BIT)
case 32:
DataSize = SPI_DATASIZE_32BIT;
break;
#endif
case 16:
DataSize = SPI_DATASIZE_16BIT;
break;
// 8 bits is the default for anything not found before
default:
DataSize = SPI_DATASIZE_8BIT;
break;
}
handle->Init.DataSize = DataSize;
switch (mode) {
case 0:
handle->Init.CLKPolarity = SPI_POLARITY_LOW;
handle->Init.CLKPhase = SPI_PHASE_1EDGE;
break;
case 1:
handle->Init.CLKPolarity = SPI_POLARITY_LOW;
handle->Init.CLKPhase = SPI_PHASE_2EDGE;
break;
case 2:
handle->Init.CLKPolarity = SPI_POLARITY_HIGH;
handle->Init.CLKPhase = SPI_PHASE_1EDGE;
break;
default:
handle->Init.CLKPolarity = SPI_POLARITY_HIGH;
handle->Init.CLKPhase = SPI_PHASE_2EDGE;
break;
}
if (handle->Init.NSS != SPI_NSS_SOFT) {
handle->Init.NSS = (slave) ? SPI_NSS_HARD_INPUT : SPI_NSS_HARD_OUTPUT;
}
if (slave) {
handle->Init.Mode = SPI_MODE_SLAVE;
if (handle->Init.Direction == SPI_DIRECTION_1LINE) {
/* SPI slave implemtation in MBED does not support the 3 wires SPI.
* (e.g. when MISO is not connected). So we're forcing slave in
* 2LINES mode. As MISO is not connected, slave will only read
* from master, and cannot write to it. Inform user.
*/
debug("3 wires SPI slave not supported - slave will only read\r\n");
handle->Init.Direction = SPI_DIRECTION_2LINES;
}
pin_mode(spiobj->pin_mosi, PullNone);
pin_mode(spiobj->pin_miso, PullDown); // Pull Down is set for output line
}
/*
* According the STM32 Datasheet for SPI peripheral we need to PULLDOWN
* or PULLUP the SCK pin according the polarity used.
*/
pull = (handle->Init.CLKPolarity == SPI_POLARITY_LOW) ? PullDown : PullUp;
pin_mode(spiobj->pin_sclk, pull);
init_spi(obj);
}
/*
* Only the IP clock input is family dependant so it computed
* separately in spi_get_clock_freq
*/
extern int spi_get_clock_freq(spi_t *obj);
static const uint32_t baudrate_prescaler_table[] = {SPI_BAUDRATEPRESCALER_2,
SPI_BAUDRATEPRESCALER_4,
SPI_BAUDRATEPRESCALER_8,
SPI_BAUDRATEPRESCALER_16,
SPI_BAUDRATEPRESCALER_32,
SPI_BAUDRATEPRESCALER_64,
SPI_BAUDRATEPRESCALER_128,
SPI_BAUDRATEPRESCALER_256
};
/**
* Convert SPI_BAUDRATEPRESCALER_<X> constant into numeric prescaler rank.
*/
static uint8_t spi_get_baudrate_prescaler_rank(uint32_t value)
{
switch (value) {
case SPI_BAUDRATEPRESCALER_2:
return 0;
case SPI_BAUDRATEPRESCALER_4:
return 1;
case SPI_BAUDRATEPRESCALER_8:
return 2;
case SPI_BAUDRATEPRESCALER_16:
return 3;
case SPI_BAUDRATEPRESCALER_32:
return 4;
case SPI_BAUDRATEPRESCALER_64:
return 5;
case SPI_BAUDRATEPRESCALER_128:
return 6;
case SPI_BAUDRATEPRESCALER_256:
return 7;
default:
return 0xFF;
}
}
/**
* Get actual SPI baudrate.
*
* It may differ from a value that is passed to the ::spi_frequency function.
*/
int spi_get_baudrate(spi_t *obj)
{
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
int freq = spi_get_clock_freq(obj);
uint8_t baudrate_rank = spi_get_baudrate_prescaler_rank(handle->Init.BaudRatePrescaler);
MBED_ASSERT(baudrate_rank != 0xFF);
return freq >> (baudrate_rank + 1);
}
void spi_frequency(spi_t *obj, int hz)
{
struct spi_s *spiobj = SPI_S(obj);
int spi_hz = 0;
uint8_t prescaler_rank = 0;
uint8_t last_index = (sizeof(baudrate_prescaler_table) / sizeof(baudrate_prescaler_table[0])) - 1;
SPI_HandleTypeDef *handle = &(spiobj->handle);
/* Calculate the spi clock for prescaler_rank 0: SPI_BAUDRATEPRESCALER_2 */
spi_hz = spi_get_clock_freq(obj) / 2;
/* Define pre-scaler in order to get highest available frequency below requested frequency */
while ((spi_hz > hz) && (prescaler_rank < last_index)) {
spi_hz = spi_hz / 2;
prescaler_rank++;
}
/* Use the best fit pre-scaler */
handle->Init.BaudRatePrescaler = baudrate_prescaler_table[prescaler_rank];
/* In case maximum pre-scaler still gives too high freq, raise an error */
if (spi_hz > hz) {
DEBUG_PRINTF("WARNING: lowest SPI freq (%d) higher than requested (%d)\r\n", spi_hz, hz);
}
DEBUG_PRINTF("spi_frequency, request:%d, select:%d\r\n", hz, spi_hz);
init_spi(obj);
}
static inline int ssp_readable(spi_t *obj)
{
int status;
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
// Check if data is received
#if defined(SPI_IP_VERSION_V2)
status = ((__HAL_SPI_GET_FLAG(handle, SPI_FLAG_RXP) != RESET) ? 1 : 0);
#else /* SPI_IP_VERSION_V2 */
status = ((__HAL_SPI_GET_FLAG(handle, SPI_FLAG_RXNE) != RESET) ? 1 : 0);
#endif /* SPI_IP_VERSION_V2 */
return status;
}
static inline int ssp_writeable(spi_t *obj)
{
int status;
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
// Check if data is transmitted
#if defined(SPI_IP_VERSION_V2)
status = ((__HAL_SPI_GET_FLAG(handle, SPI_FLAG_TXP) != RESET) ? 1 : 0);
#else /* SPI_IP_VERSION_V2 */
status = ((__HAL_SPI_GET_FLAG(handle, SPI_FLAG_TXE) != RESET) ? 1 : 0);
#endif /* SPI_IP_VERSION_V2 */
return status;
}
static inline int ssp_busy(spi_t *obj)
{
int status;
struct spi_s *spiobj = SPI_S(obj);
SPI_HandleTypeDef *handle = &(spiobj->handle);
#if defined(SPI_IP_VERSION_V2)
status = ((__HAL_SPI_GET_FLAG(handle, SPI_FLAG_RXWNE) != RESET) ? 1 : 0);
#else /* SPI_IP_VERSION_V2 */
status = ((__HAL_SPI_GET_FLAG(handle, SPI_FLAG_BSY) != RESET) ? 1 : 0);
#endif /* SPI_IP_VERSION_V2 */
return status;
}
static inline int datasize_to_transfer_bitshift(uint32_t DataSize)
{
switch (DataSize) {
#if defined(SPI_DATASIZE_4BIT)
case SPI_DATASIZE_4BIT:
#endif
#if defined(SPI_DATASIZE_5BIT)
case SPI_DATASIZE_5BIT:
#endif
#if defined(SPI_DATASIZE_6BIT)
case SPI_DATASIZE_6BIT:
#endif
#if defined(SPI_DATASIZE_7BIT)
case SPI_DATASIZE_7BIT:
#endif
case SPI_DATASIZE_8BIT:
return 0;
#if defined(SPI_DATASIZE_9BIT)
case SPI_DATASIZE_9BIT:
#endif
#if defined(SPI_DATASIZE_10BIT)
case SPI_DATASIZE_10BIT:
#endif
#if defined(SPI_DATASIZE_11BIT)
case SPI_DATASIZE_11BIT:
#endif
#if defined(SPI_DATASIZE_12BIT)
case SPI_DATASIZE_12BIT:
#endif
#if defined(SPI_DATASIZE_13BIT)
case SPI_DATASIZE_13BIT:
#endif
#if defined(SPI_DATASIZE_14BIT)
case SPI_DATASIZE_14BIT:
#endif
#if defined(SPI_DATASIZE_15BIT)
case SPI_DATASIZE_15BIT:
#endif
case SPI_DATASIZE_16BIT:
return 1;
#if defined(SPI_DATASIZE_17BIT)
case SPI_DATASIZE_17BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_18BIT)
case SPI_DATASIZE_18BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_19BIT)
case SPI_DATASIZE_19BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_20BIT)
case SPI_DATASIZE_20BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_21BIT)
case SPI_DATASIZE_21BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_22BIT)
case SPI_DATASIZE_22BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_23BIT)
case SPI_DATASIZE_23BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_24BIT)
case SPI_DATASIZE_24BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_25BIT)
case SPI_DATASIZE_25BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_26BIT)
case SPI_DATASIZE_26BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_27BIT)
case SPI_DATASIZE_27BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_28BIT)
case SPI_DATASIZE_28BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_29BIT)
case SPI_DATASIZE_29BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_30BIT)
case SPI_DATASIZE_30BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_31BIT)
case SPI_DATASIZE_31BIT:
return 2;
#endif
#if defined(SPI_DATASIZE_32BIT)
case SPI_DATASIZE_32BIT:
return 2;
#endif
// This point should never be reached, so return a negative value for assertion checking
default:
return -1;
}
}
/**
* Check if SPI master interface is writable.
*
* @param obj
* @return 0 - SPI isn't writable, non-zero - SPI is writable
*/
static inline int msp_writable(spi_t *obj)
{
#if defined(SPI_IP_VERSION_V2)
return (int)LL_SPI_IsActiveFlag_TXP(SPI_INST(obj));
#else /* SPI_IP_VERSION_V2 */
return (int)LL_SPI_IsActiveFlag_TXE(SPI_INST(obj));
#endif /* SPI_IP_VERSION_V2 */
}
/**
* Check if SPI master interface is readable.
*
* @param obj
* @return 0 - SPI isn't readable, non-zero - SPI is readable
*/
static inline int msp_readable(spi_t *obj)
{
#if defined(SPI_IP_VERSION_V2)
return (int)LL_SPI_IsActiveFlag_RXP(SPI_INST(obj));
#else /* SPI_IP_VERSION_V2 */
return (int)LL_SPI_IsActiveFlag_RXNE(SPI_INST(obj));
#endif /* SPI_IP_VERSION_V2 */
}
/**
* Wait till SPI master interface is writable.
*/
static inline void msp_wait_writable(spi_t *obj)
{
while (!msp_writable(obj));
}
/**
* Wait till SPI master interface is readable.
*/
static inline void msp_wait_readable(spi_t *obj)
{
while (!msp_readable(obj));
}
/**
* Check if SPI master interface is busy.
*
* @param obj
* @return 0 - SPI isn't busy, non-zero - SPI is busy
*/
static inline int msp_busy(spi_t *obj)
{
#if defined(SPI_IP_VERSION_V2)
return !(int)LL_SPI_IsActiveFlag_TXC(SPI_INST(obj));
#else /* SPI_IP_VERSION_V2 */
return (int)LL_SPI_IsActiveFlag_BSY(SPI_INST(obj));
#endif /* SPI_IP_VERSION_V2 */