forked from arturo182/MIMXRT10xx_SDK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfsl_lpspi.c
2099 lines (1795 loc) · 69.1 KB
/
fsl_lpspi.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
/*
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "fsl_lpspi.h"
/*******************************************************************************
* Definitions
******************************************************************************/
/* Component ID definition, used by tools. */
#ifndef FSL_COMPONENT_ID
#define FSL_COMPONENT_ID "platform.drivers.lpspi"
#endif
/*!
* @brief Default watermark values.
*
* The default watermarks are set to zero.
*/
enum _lpspi_default_watermarks
{
kLpspiDefaultTxWatermark = 0,
kLpspiDefaultRxWatermark = 0,
};
/*! @brief Typedef for master interrupt handler. */
typedef void (*lpspi_master_isr_t)(LPSPI_Type *base, lpspi_master_handle_t *handle);
/*! @brief Typedef for slave interrupt handler. */
typedef void (*lpspi_slave_isr_t)(LPSPI_Type *base, lpspi_slave_handle_t *handle);
/*******************************************************************************
* Prototypes
******************************************************************************/
/*!
* @brief Get instance number for LPSPI module.
*
* @param base LPSPI peripheral base address.
* @return Return the value of LPSPI instance.
*/
uint32_t LPSPI_GetInstance(LPSPI_Type *base);
/*!
* @brief Configures the LPSPI peripheral chip select polarity.
*
* This function takes in the desired peripheral chip select (Pcs) and it's corresponding desired polarity and
* configures the Pcs signal to operate with the desired characteristic.
*
* @param base LPSPI peripheral address.
* @param pcs The particular peripheral chip select (parameter value is of type lpspi_which_pcs_t) for which we wish to
* apply the active high or active low characteristic.
* @param activeLowOrHigh The setting for either "active high, inactive low (0)" or "active low, inactive high(1)" of
* type lpspi_pcs_polarity_config_t.
*/
static void LPSPI_SetOnePcsPolarity(LPSPI_Type *base,
lpspi_which_pcs_t pcs,
lpspi_pcs_polarity_config_t activeLowOrHigh);
/*!
* @brief Combine the write data for 1 byte to 4 bytes.
* This is not a public API.
*/
static uint32_t LPSPI_CombineWriteData(uint8_t *txData, uint32_t bytesEachWrite, bool isByteSwap);
/*!
* @brief Separate the read data for 1 byte to 4 bytes.
* This is not a public API.
*/
static void LPSPI_SeparateReadData(uint8_t *rxData, uint32_t readData, uint32_t bytesEachRead, bool isByteSwap);
/*!
* @brief Master fill up the TX FIFO with data.
* This is not a public API.
*/
static void LPSPI_MasterTransferFillUpTxFifo(LPSPI_Type *base, lpspi_master_handle_t *handle);
/*!
* @brief Master finish up a transfer.
* It would call back if there is callback function and set the state to idle.
* This is not a public API.
*/
static void LPSPI_MasterTransferComplete(LPSPI_Type *base, lpspi_master_handle_t *handle);
/*!
* @brief Slave fill up the TX FIFO with data.
* This is not a public API.
*/
static void LPSPI_SlaveTransferFillUpTxFifo(LPSPI_Type *base, lpspi_slave_handle_t *handle);
/*!
* @brief Slave finish up a transfer.
* It would call back if there is callback function and set the state to idle.
* This is not a public API.
*/
static void LPSPI_SlaveTransferComplete(LPSPI_Type *base, lpspi_slave_handle_t *handle);
/*!
* @brief LPSPI common interrupt handler.
*
* @param handle pointer to s_lpspiHandle which stores the transfer state.
*/
static void LPSPI_CommonIRQHandler(LPSPI_Type *base, void *param);
/*******************************************************************************
* Variables
******************************************************************************/
/* Defines constant value arrays for the baud rate pre-scalar and scalar divider values.*/
static const uint8_t s_baudratePrescaler[] = {1, 2, 4, 8, 16, 32, 64, 128};
/*! @brief Pointers to lpspi bases for each instance. */
static LPSPI_Type *const s_lpspiBases[] = LPSPI_BASE_PTRS;
/*! @brief Pointers to lpspi IRQ number for each instance. */
static const IRQn_Type s_lpspiIRQ[] = LPSPI_IRQS;
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
/*! @brief Pointers to lpspi clocks for each instance. */
static const clock_ip_name_t s_lpspiClocks[] = LPSPI_CLOCKS;
#if defined(LPSPI_PERIPH_CLOCKS)
static const clock_ip_name_t s_LpspiPeriphClocks[] = LPSPI_PERIPH_CLOCKS;
#endif
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
/*! @brief Pointers to lpspi handles for each instance. */
static void *s_lpspiHandle[ARRAY_SIZE(s_lpspiBases)] = {NULL};
/*! @brief Pointer to master IRQ handler for each instance. */
static lpspi_master_isr_t s_lpspiMasterIsr;
/*! @brief Pointer to slave IRQ handler for each instance. */
static lpspi_slave_isr_t s_lpspiSlaveIsr;
/* @brief Dummy data for each instance. This data is used when user's tx buffer is NULL*/
volatile uint8_t g_lpspiDummyData[ARRAY_SIZE(s_lpspiBases)] = {0};
/**********************************************************************************************************************
* Code
*********************************************************************************************************************/
uint32_t LPSPI_GetInstance(LPSPI_Type *base)
{
uint8_t instance = 0;
/* Find the instance index from base address mappings. */
for (instance = 0; instance < ARRAY_SIZE(s_lpspiBases); instance++)
{
if (s_lpspiBases[instance] == base)
{
break;
}
}
assert(instance < ARRAY_SIZE(s_lpspiBases));
return instance;
}
/*!
* brief Set up the dummy data.
*
* param base LPSPI peripheral address.
* param dummyData Data to be transferred when tx buffer is NULL.
* Note:
* This API has no effect when LPSPI in slave interrupt mode, because driver
* will set the TXMSK bit to 1 if txData is NULL, no data is loaded from transmit
* FIFO and output pin is tristated.
*/
void LPSPI_SetDummyData(LPSPI_Type *base, uint8_t dummyData)
{
uint32_t instance = LPSPI_GetInstance(base);
g_lpspiDummyData[instance] = dummyData;
}
/*!
* brief Initializes the LPSPI master.
*
* param base LPSPI peripheral address.
* param masterConfig Pointer to structure lpspi_master_config_t.
* param srcClock_Hz Module source input clock in Hertz
*/
void LPSPI_MasterInit(LPSPI_Type *base, const lpspi_master_config_t *masterConfig, uint32_t srcClock_Hz)
{
assert(masterConfig);
uint32_t tcrPrescaleValue = 0;
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
uint32_t instance = LPSPI_GetInstance(base);
/* Enable LPSPI clock */
CLOCK_EnableClock(s_lpspiClocks[instance]);
#if defined(LPSPI_PERIPH_CLOCKS)
CLOCK_EnableClock(s_LpspiPeriphClocks[instance]);
#endif
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
/* Set LPSPI to master */
LPSPI_SetMasterSlaveMode(base, kLPSPI_Master);
/* Set specific PCS to active high or low */
LPSPI_SetOnePcsPolarity(base, masterConfig->whichPcs, masterConfig->pcsActiveHighOrLow);
/* Set Configuration Register 1 related setting.*/
base->CFGR1 = (base->CFGR1 & ~(LPSPI_CFGR1_OUTCFG_MASK | LPSPI_CFGR1_PINCFG_MASK | LPSPI_CFGR1_NOSTALL_MASK)) |
LPSPI_CFGR1_OUTCFG(masterConfig->dataOutConfig) | LPSPI_CFGR1_PINCFG(masterConfig->pinCfg) |
LPSPI_CFGR1_NOSTALL(0);
/* Set baudrate and delay times*/
LPSPI_MasterSetBaudRate(base, masterConfig->baudRate, srcClock_Hz, &tcrPrescaleValue);
/* Set default watermarks */
LPSPI_SetFifoWatermarks(base, kLpspiDefaultTxWatermark, kLpspiDefaultRxWatermark);
/* Set Transmit Command Register*/
base->TCR = LPSPI_TCR_CPOL(masterConfig->cpol) | LPSPI_TCR_CPHA(masterConfig->cpha) |
LPSPI_TCR_LSBF(masterConfig->direction) | LPSPI_TCR_FRAMESZ(masterConfig->bitsPerFrame - 1) |
LPSPI_TCR_PRESCALE(tcrPrescaleValue) | LPSPI_TCR_PCS(masterConfig->whichPcs);
LPSPI_Enable(base, true);
LPSPI_MasterSetDelayTimes(base, masterConfig->pcsToSckDelayInNanoSec, kLPSPI_PcsToSck, srcClock_Hz);
LPSPI_MasterSetDelayTimes(base, masterConfig->lastSckToPcsDelayInNanoSec, kLPSPI_LastSckToPcs, srcClock_Hz);
LPSPI_MasterSetDelayTimes(base, masterConfig->betweenTransferDelayInNanoSec, kLPSPI_BetweenTransfer, srcClock_Hz);
LPSPI_SetDummyData(base, LPSPI_DUMMY_DATA);
}
/*!
* brief Sets the lpspi_master_config_t structure to default values.
*
* This API initializes the configuration structure for LPSPI_MasterInit().
* The initialized structure can remain unchanged in LPSPI_MasterInit(), or can be modified
* before calling the LPSPI_MasterInit().
* Example:
* code
* lpspi_master_config_t masterConfig;
* LPSPI_MasterGetDefaultConfig(&masterConfig);
* endcode
* param masterConfig pointer to lpspi_master_config_t structure
*/
void LPSPI_MasterGetDefaultConfig(lpspi_master_config_t *masterConfig)
{
assert(masterConfig);
/* Initializes the configure structure to zero. */
memset(masterConfig, 0, sizeof(*masterConfig));
masterConfig->baudRate = 500000;
masterConfig->bitsPerFrame = 8;
masterConfig->cpol = kLPSPI_ClockPolarityActiveHigh;
masterConfig->cpha = kLPSPI_ClockPhaseFirstEdge;
masterConfig->direction = kLPSPI_MsbFirst;
masterConfig->pcsToSckDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
masterConfig->lastSckToPcsDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
masterConfig->betweenTransferDelayInNanoSec = 1000000000 / masterConfig->baudRate * 2;
masterConfig->whichPcs = kLPSPI_Pcs0;
masterConfig->pcsActiveHighOrLow = kLPSPI_PcsActiveLow;
masterConfig->pinCfg = kLPSPI_SdiInSdoOut;
masterConfig->dataOutConfig = kLpspiDataOutRetained;
}
/*!
* brief LPSPI slave configuration.
*
* param base LPSPI peripheral address.
* param slaveConfig Pointer to a structure lpspi_slave_config_t.
*/
void LPSPI_SlaveInit(LPSPI_Type *base, const lpspi_slave_config_t *slaveConfig)
{
assert(slaveConfig);
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
uint32_t instance = LPSPI_GetInstance(base);
/* Enable LPSPI clock */
CLOCK_EnableClock(s_lpspiClocks[instance]);
#if defined(LPSPI_PERIPH_CLOCKS)
CLOCK_EnableClock(s_LpspiPeriphClocks[instance]);
#endif
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
LPSPI_SetMasterSlaveMode(base, kLPSPI_Slave);
LPSPI_SetOnePcsPolarity(base, slaveConfig->whichPcs, slaveConfig->pcsActiveHighOrLow);
base->CFGR1 = (base->CFGR1 & ~(LPSPI_CFGR1_OUTCFG_MASK | LPSPI_CFGR1_PINCFG_MASK)) |
LPSPI_CFGR1_OUTCFG(slaveConfig->dataOutConfig) | LPSPI_CFGR1_PINCFG(slaveConfig->pinCfg);
LPSPI_SetFifoWatermarks(base, kLpspiDefaultTxWatermark, kLpspiDefaultRxWatermark);
base->TCR = LPSPI_TCR_CPOL(slaveConfig->cpol) | LPSPI_TCR_CPHA(slaveConfig->cpha) |
LPSPI_TCR_LSBF(slaveConfig->direction) | LPSPI_TCR_FRAMESZ(slaveConfig->bitsPerFrame - 1);
/* This operation will set the dummy data for edma transfer, no effect in interrupt way. */
LPSPI_SetDummyData(base, LPSPI_DUMMY_DATA);
LPSPI_Enable(base, true);
}
/*!
* brief Sets the lpspi_slave_config_t structure to default values.
*
* This API initializes the configuration structure for LPSPI_SlaveInit().
* The initialized structure can remain unchanged in LPSPI_SlaveInit() or can be modified
* before calling the LPSPI_SlaveInit().
* Example:
* code
* lpspi_slave_config_t slaveConfig;
* LPSPI_SlaveGetDefaultConfig(&slaveConfig);
* endcode
* param slaveConfig pointer to lpspi_slave_config_t structure.
*/
void LPSPI_SlaveGetDefaultConfig(lpspi_slave_config_t *slaveConfig)
{
assert(slaveConfig);
/* Initializes the configure structure to zero. */
memset(slaveConfig, 0, sizeof(*slaveConfig));
slaveConfig->bitsPerFrame = 8; /*!< Bits per frame, minimum 8, maximum 4096.*/
slaveConfig->cpol = kLPSPI_ClockPolarityActiveHigh; /*!< Clock polarity. */
slaveConfig->cpha = kLPSPI_ClockPhaseFirstEdge; /*!< Clock phase. */
slaveConfig->direction = kLPSPI_MsbFirst; /*!< MSB or LSB data shift direction. */
slaveConfig->whichPcs = kLPSPI_Pcs0; /*!< Desired Peripheral Chip Select (pcs) */
slaveConfig->pcsActiveHighOrLow = kLPSPI_PcsActiveLow; /*!< Desired PCS active high or low */
slaveConfig->pinCfg = kLPSPI_SdiInSdoOut;
slaveConfig->dataOutConfig = kLpspiDataOutRetained;
}
/*!
* brief Restores the LPSPI peripheral to reset state. Note that this function
* sets all registers to reset state. As a result, the LPSPI module can't work after calling
* this API.
* param base LPSPI peripheral address.
*/
void LPSPI_Reset(LPSPI_Type *base)
{
/* Reset all internal logic and registers, except the Control Register. Remains set until cleared by software.*/
base->CR |= LPSPI_CR_RST_MASK;
/* Software reset doesn't reset the CR, so manual reset the FIFOs */
base->CR |= LPSPI_CR_RRF_MASK | LPSPI_CR_RTF_MASK;
/* Master logic is not reset and module is disabled.*/
base->CR = 0x00U;
}
/*!
* brief De-initializes the LPSPI peripheral. Call this API to disable the LPSPI clock.
* param base LPSPI peripheral address.
*/
void LPSPI_Deinit(LPSPI_Type *base)
{
/* Reset to default value */
LPSPI_Reset(base);
#if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
uint32_t instance = LPSPI_GetInstance(base);
/* Enable LPSPI clock */
CLOCK_DisableClock(s_lpspiClocks[instance]);
#if defined(LPSPI_PERIPH_CLOCKS)
CLOCK_DisableClock(s_LpspiPeriphClocks[instance]);
#endif
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
}
static void LPSPI_SetOnePcsPolarity(LPSPI_Type *base,
lpspi_which_pcs_t pcs,
lpspi_pcs_polarity_config_t activeLowOrHigh)
{
uint32_t cfgr1Value = 0;
/* Clear the PCS polarity bit */
cfgr1Value = base->CFGR1 & ~(1U << (LPSPI_CFGR1_PCSPOL_SHIFT + pcs));
/* Configure the PCS polarity bit according to the activeLowOrHigh setting */
base->CFGR1 = cfgr1Value | ((uint32_t)activeLowOrHigh << (LPSPI_CFGR1_PCSPOL_SHIFT + pcs));
}
/*!
* brief Sets the LPSPI baud rate in bits per second.
*
* This function takes in the desired bitsPerSec (baud rate) and calculates the nearest
* possible baud rate without exceeding the desired baud rate and returns the
* calculated baud rate in bits-per-second. It requires the caller to provide
* the frequency of the module source clock (in Hertz). Note that the baud rate
* does not go into effect until the Transmit Control Register (TCR) is programmed
* with the prescale value. Hence, this function returns the prescale tcrPrescaleValue
* parameter for later programming in the TCR. The higher level
* peripheral driver should alert the user of an out of range baud rate input.
*
* Note that the LPSPI module must first be disabled before configuring this.
* Note that the LPSPI module must be configured for master mode before configuring this.
*
* param base LPSPI peripheral address.
* param baudRate_Bps The desired baud rate in bits per second.
* param srcClock_Hz Module source input clock in Hertz.
* param tcrPrescaleValue The TCR prescale value needed to program the TCR.
* return The actual calculated baud rate. This function may also return a "0" if the
* LPSPI is not configured for master mode or if the LPSPI module is not disabled.
*/
uint32_t LPSPI_MasterSetBaudRate(LPSPI_Type *base,
uint32_t baudRate_Bps,
uint32_t srcClock_Hz,
uint32_t *tcrPrescaleValue)
{
assert(tcrPrescaleValue);
/* For master mode configuration only, if slave mode detected, return 0.
* Also, the LPSPI module needs to be disabled first, if enabled, return 0
*/
if ((!LPSPI_IsMaster(base)) || (base->CR & LPSPI_CR_MEN_MASK))
{
return 0;
}
uint32_t prescaler, bestPrescaler;
uint32_t scaler, bestScaler;
uint32_t realBaudrate, bestBaudrate;
uint32_t diff, min_diff;
uint32_t desiredBaudrate = baudRate_Bps;
/* find combination of prescaler and scaler resulting in baudrate closest to the
* requested value
*/
min_diff = 0xFFFFFFFFU;
/* Set to maximum divisor value bit settings so that if baud rate passed in is less
* than the minimum possible baud rate, then the SPI will be configured to the lowest
* possible baud rate
*/
bestPrescaler = 7;
bestScaler = 255;
bestBaudrate = 0; /* required to avoid compilation warning */
/* In all for loops, if min_diff = 0, the exit for loop*/
for (prescaler = 0; (prescaler < 8) && min_diff; prescaler++)
{
for (scaler = 0; (scaler < 256) && min_diff; scaler++)
{
realBaudrate = (srcClock_Hz / (s_baudratePrescaler[prescaler] * (scaler + 2U)));
/* calculate the baud rate difference based on the conditional statement
* that states that the calculated baud rate must not exceed the desired baud rate
*/
if (desiredBaudrate >= realBaudrate)
{
diff = desiredBaudrate - realBaudrate;
if (min_diff > diff)
{
/* a better match found */
min_diff = diff;
bestPrescaler = prescaler;
bestScaler = scaler;
bestBaudrate = realBaudrate;
}
}
}
}
/* Write the best baud rate scalar to the CCR.
* Note, no need to check for error since we've already checked to make sure the module is
* disabled and in master mode. Also, there is a limit on the maximum divider so we will not
* exceed this.
*/
base->CCR = (base->CCR & ~LPSPI_CCR_SCKDIV_MASK) | LPSPI_CCR_SCKDIV(bestScaler);
/* return the best prescaler value for user to use later */
*tcrPrescaleValue = bestPrescaler;
/* return the actual calculated baud rate */
return bestBaudrate;
}
/*!
* brief Manually configures a specific LPSPI delay parameter (module must be disabled to
* change the delay values).
*
* This function configures the following:
* SCK to PCS delay, or
* PCS to SCK delay, or
* The configurations must occur between the transfer delay.
*
* The delay names are available in type lpspi_delay_type_t.
*
* The user passes the desired delay along with the delay value.
* This allows the user to directly set the delay values if they have
* pre-calculated them or if they simply wish to manually increment the value.
*
* Note that the LPSPI module must first be disabled before configuring this.
* Note that the LPSPI module must be configured for master mode before configuring this.
*
* param base LPSPI peripheral address.
* param scaler The 8-bit delay value 0x00 to 0xFF (255).
* param whichDelay The desired delay to configure, must be of type lpspi_delay_type_t.
*/
void LPSPI_MasterSetDelayScaler(LPSPI_Type *base, uint32_t scaler, lpspi_delay_type_t whichDelay)
{
/*These settings are only relevant in master mode */
switch (whichDelay)
{
case kLPSPI_PcsToSck:
base->CCR = (base->CCR & (~LPSPI_CCR_PCSSCK_MASK)) | LPSPI_CCR_PCSSCK(scaler);
break;
case kLPSPI_LastSckToPcs:
base->CCR = (base->CCR & (~LPSPI_CCR_SCKPCS_MASK)) | LPSPI_CCR_SCKPCS(scaler);
break;
case kLPSPI_BetweenTransfer:
base->CCR = (base->CCR & (~LPSPI_CCR_DBT_MASK)) | LPSPI_CCR_DBT(scaler);
break;
default:
assert(false);
break;
}
}
/*!
* brief Calculates the delay based on the desired delay input in nanoseconds (module must be
* disabled to change the delay values).
*
* This function calculates the values for the following:
* SCK to PCS delay, or
* PCS to SCK delay, or
* The configurations must occur between the transfer delay.
*
* The delay names are available in type lpspi_delay_type_t.
*
* The user passes the desired delay and the desired delay value in
* nano-seconds. The function calculates the value needed for the desired delay parameter
* and returns the actual calculated delay because an exact delay match may not be possible. In this
* case, the closest match is calculated without going below the desired delay value input.
* It is possible to input a very large delay value that exceeds the capability of the part, in
* which case the maximum supported delay is returned. It is up to the higher level
* peripheral driver to alert the user of an out of range delay input.
*
* Note that the LPSPI module must be configured for master mode before configuring this. And note that
* the delayTime = LPSPI_clockSource / (PRESCALE * Delay_scaler).
*
* param base LPSPI peripheral address.
* param delayTimeInNanoSec The desired delay value in nano-seconds.
* param whichDelay The desired delay to configuration, which must be of type lpspi_delay_type_t.
* param srcClock_Hz Module source input clock in Hertz.
* return actual Calculated delay value in nano-seconds.
*/
uint32_t LPSPI_MasterSetDelayTimes(LPSPI_Type *base,
uint32_t delayTimeInNanoSec,
lpspi_delay_type_t whichDelay,
uint32_t srcClock_Hz)
{
uint64_t realDelay, bestDelay;
uint32_t scaler, bestScaler;
uint32_t diff, min_diff;
uint64_t initialDelayNanoSec;
uint32_t clockDividedPrescaler;
/* For delay between transfer, an additional scaler value is needed */
uint32_t additionalScaler = 0;
/*As the RM note, the LPSPI baud rate clock is itself divided by the PRESCALE setting, which can vary between
* transfers.*/
clockDividedPrescaler =
srcClock_Hz / s_baudratePrescaler[(base->TCR & LPSPI_TCR_PRESCALE_MASK) >> LPSPI_TCR_PRESCALE_SHIFT];
/* Find combination of prescaler and scaler resulting in the delay closest to the requested value.*/
min_diff = 0xFFFFFFFFU;
/* Initialize scaler to max value to generate the max delay */
bestScaler = 0xFFU;
/* Calculate the initial (min) delay and maximum possible delay based on the specific delay as
* the delay divisors are slightly different based on which delay we are configuring.
*/
if (whichDelay == kLPSPI_BetweenTransfer)
{
/* First calculate the initial, default delay, note min delay is 2 clock cycles. Due to large size of
calculated values (uint64_t), we need to break up the calculation into several steps to ensure
accurate calculated results
*/
initialDelayNanoSec = 1000000000U;
initialDelayNanoSec *= 2U;
initialDelayNanoSec /= clockDividedPrescaler;
/* Calculate the maximum delay */
bestDelay = 1000000000U;
bestDelay *= 257U; /* based on DBT+2, or 255 + 2 */
bestDelay /= clockDividedPrescaler;
additionalScaler = 1U;
}
else
{
/* First calculate the initial, default delay, min delay is 1 clock cycle. Due to large size of calculated
values (uint64_t), we need to break up the calculation into several steps to ensure accurate calculated
results.
*/
initialDelayNanoSec = 1000000000U;
initialDelayNanoSec /= clockDividedPrescaler;
/* Calculate the maximum delay */
bestDelay = 1000000000U;
bestDelay *= 256U; /* based on SCKPCS+1 or PCSSCK+1, or 255 + 1 */
bestDelay /= clockDividedPrescaler;
additionalScaler = 0;
}
/* If the initial, default delay is already greater than the desired delay, then
* set the delay to their initial value (0) and return the delay. In other words,
* there is no way to decrease the delay value further.
*/
if (initialDelayNanoSec >= delayTimeInNanoSec)
{
LPSPI_MasterSetDelayScaler(base, 0, whichDelay);
return initialDelayNanoSec;
}
/* If min_diff = 0, the exit for loop */
for (scaler = 0; (scaler < 256U) && min_diff; scaler++)
{
/* Calculate the real delay value as we cycle through the scaler values.
Due to large size of calculated values (uint64_t), we need to break up the
calculation into several steps to ensure accurate calculated results
*/
realDelay = 1000000000U;
realDelay *= (scaler + 1 + additionalScaler);
realDelay /= clockDividedPrescaler;
/* calculate the delay difference based on the conditional statement
* that states that the calculated delay must not be less then the desired delay
*/
if (realDelay >= delayTimeInNanoSec)
{
diff = realDelay - delayTimeInNanoSec;
if (min_diff > diff)
{
/* a better match found */
min_diff = diff;
bestScaler = scaler;
bestDelay = realDelay;
}
}
}
/* write the best scaler value for the delay */
LPSPI_MasterSetDelayScaler(base, bestScaler, whichDelay);
/* return the actual calculated delay value (in ns) */
return bestDelay;
}
/*Transactional APIs -- Master*/
/*!
* brief Initializes the LPSPI master handle.
*
* This function initializes the LPSPI handle, which can be used for other LPSPI transactional APIs. Usually, for a
* specified LPSPI instance, call this API once to get the initialized handle.
* param base LPSPI peripheral address.
* param handle LPSPI handle pointer to lpspi_master_handle_t.
* param callback DSPI callback.
* param userData callback function parameter.
*/
void LPSPI_MasterTransferCreateHandle(LPSPI_Type *base,
lpspi_master_handle_t *handle,
lpspi_master_transfer_callback_t callback,
void *userData)
{
assert(handle);
/* Zero the handle. */
memset(handle, 0, sizeof(*handle));
s_lpspiHandle[LPSPI_GetInstance(base)] = handle;
/* Set irq handler. */
s_lpspiMasterIsr = LPSPI_MasterTransferHandleIRQ;
handle->callback = callback;
handle->userData = userData;
}
/*!
* brief Check the argument for transfer .
*
* param transfer the transfer struct to be used.
* param bitPerFrame The bit size of one frame.
* param bytePerFrame The byte size of one frame.
* return Return true for right and false for wrong.
*/
bool LPSPI_CheckTransferArgument(lpspi_transfer_t *transfer, uint32_t bitsPerFrame, uint32_t bytesPerFrame)
{
assert(transfer);
/* If the transfer count is zero, then return immediately.*/
if (transfer->dataSize == 0)
{
return false;
}
/* If both send buffer and receive buffer is null */
if ((!(transfer->txData)) && (!(transfer->rxData)))
{
return false;
}
/*The transfer data size should be integer multiples of bytesPerFrame if bytesPerFrame is less than or equal to 4 .
*For bytesPerFrame greater than 4 situation:
*the transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not integer multiples of 4 ,
*otherwise , the transfer data size can be integer multiples of bytesPerFrame.
*/
if (bytesPerFrame <= 4)
{
if ((transfer->dataSize % bytesPerFrame) != 0)
{
return false;
}
}
else
{
if ((bytesPerFrame % 4U) != 0)
{
if (transfer->dataSize != bytesPerFrame)
{
return false;
}
}
else
{
if ((transfer->dataSize % bytesPerFrame) != 0)
{
return false;
}
}
}
return true;
}
/*!
* brief LPSPI master transfer data using a polling method.
*
* This function transfers data using a polling method. This is a blocking function, which does not return until all
* transfers have been
* completed.
*
* Note:
* The transfer data size should be integer multiples of bytesPerFrame if bytesPerFrame is less than or equal to 4.
* For bytesPerFrame greater than 4:
* The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not integer multiples of 4.
* Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
*
* param base LPSPI peripheral address.
* param transfer pointer to lpspi_transfer_t structure.
* return status of status_t.
*/
status_t LPSPI_MasterTransferBlocking(LPSPI_Type *base, lpspi_transfer_t *transfer)
{
assert(transfer);
uint32_t bitsPerFrame = ((base->TCR & LPSPI_TCR_FRAMESZ_MASK) >> LPSPI_TCR_FRAMESZ_SHIFT) + 1;
uint32_t bytesPerFrame = (bitsPerFrame + 7) / 8;
uint32_t temp = 0U;
uint8_t dummyData = g_lpspiDummyData[LPSPI_GetInstance(base)];
if (!LPSPI_CheckTransferArgument(transfer, bitsPerFrame, bytesPerFrame))
{
return kStatus_InvalidArgument;
}
/* Check that LPSPI is not busy.*/
if (LPSPI_GetStatusFlags(base) & kLPSPI_ModuleBusyFlag)
{
return kStatus_LPSPI_Busy;
}
uint8_t *txData = transfer->txData;
uint8_t *rxData = transfer->rxData;
uint32_t txRemainingByteCount = transfer->dataSize;
uint32_t rxRemainingByteCount = transfer->dataSize;
uint8_t bytesEachWrite;
uint8_t bytesEachRead;
uint32_t readData = 0;
uint32_t wordToSend =
((uint32_t)dummyData) | ((uint32_t)dummyData << 8) | ((uint32_t)dummyData << 16) | ((uint32_t)dummyData << 24);
/*The TX and RX FIFO sizes are always the same*/
uint32_t fifoSize = LPSPI_GetRxFifoSize(base);
uint32_t whichPcs = (transfer->configFlags & LPSPI_MASTER_PCS_MASK) >> LPSPI_MASTER_PCS_SHIFT;
bool isPcsContinuous = (bool)(transfer->configFlags & kLPSPI_MasterPcsContinuous);
bool isRxMask = false;
bool isByteSwap = (bool)(transfer->configFlags & kLPSPI_MasterByteSwap);
LPSPI_FlushFifo(base, true, true);
LPSPI_ClearStatusFlags(base, kLPSPI_AllStatusFlag);
if (!rxData)
{
isRxMask = true;
}
LPSPI_Enable(base, false);
base->CFGR1 &= (~LPSPI_CFGR1_NOSTALL_MASK);
/* Check if using 3-wire mode and the txData is NULL, set the output pin to tristated. */
temp = base->CFGR1;
temp &= LPSPI_CFGR1_PINCFG_MASK;
if ((temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdiInSdiOut)) || (temp == LPSPI_CFGR1_PINCFG(kLPSPI_SdoInSdoOut)))
{
if (!txData)
{
base->CFGR1 |= LPSPI_CFGR1_OUTCFG_MASK;
}
/* The 3-wire mode can't send and receive data at the same time. */
if ((txData) && (rxData))
{
return kStatus_InvalidArgument;
}
}
LPSPI_Enable(base, true);
base->TCR =
(base->TCR & ~(LPSPI_TCR_CONT_MASK | LPSPI_TCR_CONTC_MASK | LPSPI_TCR_RXMSK_MASK | LPSPI_TCR_PCS_MASK)) |
LPSPI_TCR_CONT(isPcsContinuous) | LPSPI_TCR_CONTC(0) | LPSPI_TCR_RXMSK(isRxMask) | LPSPI_TCR_PCS(whichPcs);
if (bytesPerFrame <= 4)
{
bytesEachWrite = bytesPerFrame;
bytesEachRead = bytesPerFrame;
}
else
{
bytesEachWrite = 4;
bytesEachRead = 4;
}
/*Write the TX data until txRemainingByteCount is equal to 0 */
while (txRemainingByteCount > 0)
{
if (txRemainingByteCount < bytesEachWrite)
{
bytesEachWrite = txRemainingByteCount;
}
/*Wait until TX FIFO is not full*/
while (LPSPI_GetTxFifoCount(base) == fifoSize)
{
}
if (txData)
{
wordToSend = LPSPI_CombineWriteData(txData, bytesEachWrite, isByteSwap);
txData += bytesEachWrite;
}
LPSPI_WriteData(base, wordToSend);
txRemainingByteCount -= bytesEachWrite;
/*Check whether there is RX data in RX FIFO . Read out the RX data so that the RX FIFO would not overrun.*/
if (rxData)
{
while (LPSPI_GetRxFifoCount(base))
{
readData = LPSPI_ReadData(base);
if (rxRemainingByteCount < bytesEachRead)
{
bytesEachRead = rxRemainingByteCount;
}
LPSPI_SeparateReadData(rxData, readData, bytesEachRead, isByteSwap);
rxData += bytesEachRead;
rxRemainingByteCount -= bytesEachRead;
}
}
}
/* After write all the data in TX FIFO , should write the TCR_CONTC to 0 to de-assert the PCS. Note that TCR
* register also use the TX FIFO.
*/
while ((LPSPI_GetTxFifoCount(base) == fifoSize))
{
}
base->TCR = (base->TCR & ~(LPSPI_TCR_CONTC_MASK));
/*Read out the RX data in FIFO*/
if (rxData)
{
while (rxRemainingByteCount > 0)
{
while (LPSPI_GetRxFifoCount(base))
{
readData = LPSPI_ReadData(base);
if (rxRemainingByteCount < bytesEachRead)
{
bytesEachRead = rxRemainingByteCount;
}
LPSPI_SeparateReadData(rxData, readData, bytesEachRead, isByteSwap);
rxData += bytesEachRead;
rxRemainingByteCount -= bytesEachRead;
}
}
}
else
{
/* If no RX buffer, then transfer is not complete until transfer complete flag sets */
while (!(LPSPI_GetStatusFlags(base) & kLPSPI_TransferCompleteFlag))
{
}
}
return kStatus_Success;
}
/*!
* brief LPSPI master transfer data using an interrupt method.
*
* This function transfers data using an interrupt method. This is a non-blocking function, which returns right away.
* When all data
* is transferred, the callback function is called.
*
* Note:
* The transfer data size should be integer multiples of bytesPerFrame if bytesPerFrame is less than or equal to 4.
* For bytesPerFrame greater than 4:
* The transfer data size should be equal to bytesPerFrame if the bytesPerFrame is not integer multiples of 4.
* Otherwise, the transfer data size can be an integer multiple of bytesPerFrame.
*
* param base LPSPI peripheral address.
* param handle pointer to lpspi_master_handle_t structure which stores the transfer state.
* param transfer pointer to lpspi_transfer_t structure.
* return status of status_t.
*/
status_t LPSPI_MasterTransferNonBlocking(LPSPI_Type *base, lpspi_master_handle_t *handle, lpspi_transfer_t *transfer)
{
assert(handle);
assert(transfer);
uint32_t bitsPerFrame = ((base->TCR & LPSPI_TCR_FRAMESZ_MASK) >> LPSPI_TCR_FRAMESZ_SHIFT) + 1;
uint32_t bytesPerFrame = (bitsPerFrame + 7) / 8;
uint32_t temp = 0U;
uint8_t dummyData = g_lpspiDummyData[LPSPI_GetInstance(base)];
if (!LPSPI_CheckTransferArgument(transfer, bitsPerFrame, bytesPerFrame))
{
return kStatus_InvalidArgument;
}
/* Check that we're not busy.*/
if (handle->state == kLPSPI_Busy)
{
return kStatus_LPSPI_Busy;
}
handle->state = kLPSPI_Busy;
bool isRxMask = false;
uint8_t txWatermark;
uint32_t whichPcs = (transfer->configFlags & LPSPI_MASTER_PCS_MASK) >> LPSPI_MASTER_PCS_SHIFT;
handle->txData = transfer->txData;
handle->rxData = transfer->rxData;
handle->txRemainingByteCount = transfer->dataSize;
handle->rxRemainingByteCount = transfer->dataSize;
handle->totalByteCount = transfer->dataSize;
handle->writeTcrInIsr = false;
handle->writeRegRemainingTimes = (transfer->dataSize / bytesPerFrame) * ((bytesPerFrame + 3) / 4);
handle->readRegRemainingTimes = handle->writeRegRemainingTimes;
handle->txBuffIfNull =