1
1
/*
2
- * Copyright (c) 2015, Freescale Semiconductor, Inc.
3
- * All rights reserved.
4
- *
5
- * Redistribution and use in source and binary forms, with or without modification,
6
- * are permitted provided that the following conditions are met:
7
- *
8
- * o Redistributions of source code must retain the above copyright notice, this list
9
- * of conditions and the following disclaimer.
10
- *
11
- * o Redistributions in binary form must reproduce the above copyright notice, this
12
- * list of conditions and the following disclaimer in the documentation and/or
13
- * other materials provided with the distribution.
14
- *
15
- * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16
- * contributors may be used to endorse or promote products derived from this
17
- * software without specific prior written permission.
18
- *
19
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
- */
2
+ * Copyright (c) 2015, Freescale Semiconductor, Inc.
3
+ * Copyright 2016-2017 NXP
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without modification,
6
+ * are permitted provided that the following conditions are met:
7
+ *
8
+ * o Redistributions of source code must retain the above copyright notice, this list
9
+ * of conditions and the following disclaimer.
10
+ *
11
+ * o Redistributions in binary form must reproduce the above copyright notice, this
12
+ * list of conditions and the following disclaimer in the documentation and/or
13
+ * other materials provided with the distribution.
14
+ *
15
+ * o Neither the name of the copyright holder nor the names of its
16
+ * contributors may be used to endorse or promote products derived from this
17
+ * software without specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
30
31
31
#include "fsl_dspi.h"
32
32
@@ -65,27 +65,27 @@ static void DSPI_SetOnePcsPolarity(SPI_Type *base, dspi_which_pcs_t pcs, dspi_pc
65
65
66
66
/*!
67
67
* @brief Master fill up the TX FIFO with data.
68
- * This is not a public API as it is called from other driver functions .
68
+ * This is not a public API.
69
69
*/
70
70
static void DSPI_MasterTransferFillUpTxFifo (SPI_Type * base , dspi_master_handle_t * handle );
71
71
72
72
/*!
73
73
* @brief Master finish up a transfer.
74
74
* It would call back if there is callback function and set the state to idle.
75
- * This is not a public API as it is called from other driver functions .
75
+ * This is not a public API.
76
76
*/
77
77
static void DSPI_MasterTransferComplete (SPI_Type * base , dspi_master_handle_t * handle );
78
78
79
79
/*!
80
80
* @brief Slave fill up the TX FIFO with data.
81
- * This is not a public API as it is called from other driver functions .
81
+ * This is not a public API.
82
82
*/
83
83
static void DSPI_SlaveTransferFillUpTxFifo (SPI_Type * base , dspi_slave_handle_t * handle );
84
84
85
85
/*!
86
86
* @brief Slave finish up a transfer.
87
87
* It would call back if there is callback function and set the state to idle.
88
- * This is not a public API as it is called from other driver functions .
88
+ * This is not a public API.
89
89
*/
90
90
static void DSPI_SlaveTransferComplete (SPI_Type * base , dspi_slave_handle_t * handle );
91
91
@@ -100,7 +100,7 @@ static void DSPI_CommonIRQHandler(SPI_Type *base, void *param);
100
100
/*!
101
101
* @brief Master prepare the transfer.
102
102
* Basically it set up dspi_master_handle .
103
- * This is not a public API as it is called from other driver functions. fsl_dspi_edma.c also call this function .
103
+ * This is not a public API.
104
104
*/
105
105
static void DSPI_MasterTransferPrepare (SPI_Type * base , dspi_master_handle_t * handle , dspi_transfer_t * transfer );
106
106
@@ -129,14 +129,16 @@ static clock_ip_name_t const s_dspiClock[] = DSPI_CLOCKS;
129
129
#endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
130
130
131
131
/*! @brief Pointers to dspi handles for each instance. */
132
- static void * g_dspiHandle [FSL_FEATURE_SOC_DSPI_COUNT ];
132
+ static void * g_dspiHandle [ARRAY_SIZE ( s_dspiBases ) ];
133
133
134
134
/*! @brief Pointer to master IRQ handler for each instance. */
135
135
static dspi_master_isr_t s_dspiMasterIsr ;
136
136
137
137
/*! @brief Pointer to slave IRQ handler for each instance. */
138
138
static dspi_slave_isr_t s_dspiSlaveIsr ;
139
139
140
+ /* @brief Dummy data for each instance. This data is used when user's tx buffer is NULL*/
141
+ volatile uint8_t s_dummyData [ARRAY_SIZE (s_dspiBases )] = {0 };
140
142
/**********************************************************************************************************************
141
143
* Code
142
144
*********************************************************************************************************************/
@@ -145,19 +147,25 @@ uint32_t DSPI_GetInstance(SPI_Type *base)
145
147
uint32_t instance ;
146
148
147
149
/* Find the instance index from base address mappings. */
148
- for (instance = 0 ; instance < FSL_FEATURE_SOC_DSPI_COUNT ; instance ++ )
150
+ for (instance = 0 ; instance < ARRAY_SIZE ( s_dspiBases ) ; instance ++ )
149
151
{
150
152
if (s_dspiBases [instance ] == base )
151
153
{
152
154
break ;
153
155
}
154
156
}
155
157
156
- assert (instance < FSL_FEATURE_SOC_DSPI_COUNT );
158
+ assert (instance < ARRAY_SIZE ( s_dspiBases ) );
157
159
158
160
return instance ;
159
161
}
160
162
163
+ void DSPI_SetDummyData (SPI_Type * base , uint8_t dummyData )
164
+ {
165
+ uint32_t instance = DSPI_GetInstance (base );
166
+ s_dummyData [instance ] = dummyData ;
167
+ }
168
+
161
169
void DSPI_MasterInit (SPI_Type * base , const dspi_master_config_t * masterConfig , uint32_t srcClock_Hz )
162
170
{
163
171
assert (masterConfig );
@@ -202,6 +210,7 @@ void DSPI_MasterInit(SPI_Type *base, const dspi_master_config_t *masterConfig, u
202
210
DSPI_MasterSetDelayTimes (base , masterConfig -> whichCtar , kDSPI_BetweenTransfer , srcClock_Hz ,
203
211
masterConfig -> ctarConfig .betweenTransferDelayInNanoSec );
204
212
213
+ DSPI_SetDummyData (base , DSPI_DUMMY_DATA );
205
214
DSPI_StartTransfer (base );
206
215
}
207
216
@@ -262,6 +271,8 @@ void DSPI_SlaveInit(SPI_Type *base, const dspi_slave_config_t *slaveConfig)
262
271
SPI_CTAR_SLAVE_CPOL (slaveConfig -> ctarConfig .cpol ) |
263
272
SPI_CTAR_SLAVE_CPHA (slaveConfig -> ctarConfig .cpha );
264
273
274
+ DSPI_SetDummyData (base , DSPI_DUMMY_DATA );
275
+
265
276
DSPI_StartTransfer (base );
266
277
}
267
278
@@ -582,7 +593,7 @@ status_t DSPI_MasterTransferBlocking(SPI_Type *base, dspi_transfer_t *transfer)
582
593
583
594
uint16_t wordToSend = 0 ;
584
595
uint16_t wordReceived = 0 ;
585
- uint8_t dummyData = DSPI_DUMMY_DATA ;
596
+ uint8_t dummyData = s_dummyData [ DSPI_GetInstance ( base )] ;
586
597
uint8_t bitsPerFrame ;
587
598
588
599
uint32_t command ;
@@ -897,21 +908,21 @@ status_t DSPI_MasterTransferNonBlocking(SPI_Type *base, dspi_master_handle_t *ha
897
908
handle -> state = kDSPI_Busy ;
898
909
899
910
DSPI_MasterTransferPrepare (base , handle , transfer );
900
- DSPI_StartTransfer (base );
901
911
902
912
/* Enable the NVIC for DSPI peripheral. */
903
913
EnableIRQ (s_dspiIRQ [DSPI_GetInstance (base )]);
904
914
905
- DSPI_MasterTransferFillUpTxFifo (base , handle );
906
-
907
915
/* RX FIFO Drain request: RFDF_RE to enable RFDF interrupt
908
916
* Since SPI is a synchronous interface, we only need to enable the RX interrupt.
909
917
* The IRQ handler will get the status of RX and TX interrupt flags.
910
918
*/
911
919
s_dspiMasterIsr = DSPI_MasterTransferHandleIRQ ;
912
920
913
921
DSPI_EnableInterrupts (base , kDSPI_RxFifoDrainRequestInterruptEnable );
922
+ DSPI_StartTransfer (base );
914
923
924
+ /* Fill up the Tx FIFO to trigger the transfer. */
925
+ DSPI_MasterTransferFillUpTxFifo (base , handle );
915
926
return kStatus_Success ;
916
927
}
917
928
@@ -952,21 +963,20 @@ static void DSPI_MasterTransferComplete(SPI_Type *base, dspi_master_handle_t *ha
952
963
status = kStatus_Success ;
953
964
}
954
965
966
+ handle -> state = kDSPI_Idle ;
967
+
955
968
if (handle -> callback )
956
969
{
957
970
handle -> callback (base , handle , status , handle -> userData );
958
971
}
959
-
960
- /* The transfer is complete.*/
961
- handle -> state = kDSPI_Idle ;
962
972
}
963
973
964
974
static void DSPI_MasterTransferFillUpTxFifo (SPI_Type * base , dspi_master_handle_t * handle )
965
975
{
966
976
assert (handle );
967
977
968
978
uint16_t wordToSend = 0 ;
969
- uint8_t dummyData = DSPI_DUMMY_DATA ;
979
+ uint8_t dummyData = s_dummyData [ DSPI_GetInstance ( base )] ;
970
980
971
981
/* If bits/frame is greater than one byte */
972
982
if (handle -> bitsPerFrame > 8 )
@@ -1257,11 +1267,6 @@ status_t DSPI_SlaveTransferNonBlocking(SPI_Type *base, dspi_slave_handle_t *hand
1257
1267
DSPI_FlushFifo (base , true, true);
1258
1268
DSPI_ClearStatusFlags (base , kDSPI_AllStatusFlag );
1259
1269
1260
- DSPI_StartTransfer (base );
1261
-
1262
- /* Prepare data to transmit */
1263
- DSPI_SlaveTransferFillUpTxFifo (base , handle );
1264
-
1265
1270
s_dspiSlaveIsr = DSPI_SlaveTransferHandleIRQ ;
1266
1271
1267
1272
/* Enable RX FIFO drain request, the slave only use this interrupt */
@@ -1278,6 +1283,11 @@ status_t DSPI_SlaveTransferNonBlocking(SPI_Type *base, dspi_slave_handle_t *hand
1278
1283
DSPI_EnableInterrupts (base , kDSPI_TxFifoUnderflowInterruptEnable );
1279
1284
}
1280
1285
1286
+ DSPI_StartTransfer (base );
1287
+
1288
+ /* Prepare data to transmit */
1289
+ DSPI_SlaveTransferFillUpTxFifo (base , handle );
1290
+
1281
1291
return kStatus_Success ;
1282
1292
}
1283
1293
@@ -1306,7 +1316,7 @@ static void DSPI_SlaveTransferFillUpTxFifo(SPI_Type *base, dspi_slave_handle_t *
1306
1316
assert (handle );
1307
1317
1308
1318
uint16_t transmitData = 0 ;
1309
- uint8_t dummyPattern = DSPI_DUMMY_DATA ;
1319
+ uint8_t dummyPattern = s_dummyData [ DSPI_GetInstance ( base )] ;
1310
1320
1311
1321
/* Service the transmitter, if transmit buffer provided, transmit the data,
1312
1322
* else transmit dummy pattern
@@ -1413,12 +1423,12 @@ static void DSPI_SlaveTransferComplete(SPI_Type *base, dspi_slave_handle_t *hand
1413
1423
status = kStatus_Success ;
1414
1424
}
1415
1425
1426
+ handle -> state = kDSPI_Idle ;
1427
+
1416
1428
if (handle -> callback )
1417
1429
{
1418
1430
handle -> callback (base , handle , status , handle -> userData );
1419
1431
}
1420
-
1421
- handle -> state = kDSPI_Idle ;
1422
1432
}
1423
1433
1424
1434
void DSPI_SlaveTransferAbort (SPI_Type * base , dspi_slave_handle_t * handle )
@@ -1440,7 +1450,7 @@ void DSPI_SlaveTransferHandleIRQ(SPI_Type *base, dspi_slave_handle_t *handle)
1440
1450
{
1441
1451
assert (handle );
1442
1452
1443
- uint8_t dummyPattern = DSPI_DUMMY_DATA ;
1453
+ uint8_t dummyPattern = s_dummyData [ DSPI_GetInstance ( base )] ;
1444
1454
uint32_t dataReceived ;
1445
1455
uint32_t dataSend = 0 ;
1446
1456
@@ -1617,47 +1627,47 @@ static void DSPI_CommonIRQHandler(SPI_Type *base, void *param)
1617
1627
}
1618
1628
}
1619
1629
1620
- #if ( FSL_FEATURE_SOC_DSPI_COUNT > 0 )
1630
+ #if defined( SPI0 )
1621
1631
void SPI0_DriverIRQHandler (void )
1622
1632
{
1623
1633
assert (g_dspiHandle [0 ]);
1624
1634
DSPI_CommonIRQHandler (SPI0 , g_dspiHandle [0 ]);
1625
1635
}
1626
1636
#endif
1627
1637
1628
- #if ( FSL_FEATURE_SOC_DSPI_COUNT > 1 )
1638
+ #if defined( SPI1 )
1629
1639
void SPI1_DriverIRQHandler (void )
1630
1640
{
1631
1641
assert (g_dspiHandle [1 ]);
1632
1642
DSPI_CommonIRQHandler (SPI1 , g_dspiHandle [1 ]);
1633
1643
}
1634
1644
#endif
1635
1645
1636
- #if ( FSL_FEATURE_SOC_DSPI_COUNT > 2 )
1646
+ #if defined( SPI2 )
1637
1647
void SPI2_DriverIRQHandler (void )
1638
1648
{
1639
1649
assert (g_dspiHandle [2 ]);
1640
1650
DSPI_CommonIRQHandler (SPI2 , g_dspiHandle [2 ]);
1641
1651
}
1642
1652
#endif
1643
1653
1644
- #if ( FSL_FEATURE_SOC_DSPI_COUNT > 3 )
1654
+ #if defined( SPI3 )
1645
1655
void SPI3_DriverIRQHandler (void )
1646
1656
{
1647
1657
assert (g_dspiHandle [3 ]);
1648
1658
DSPI_CommonIRQHandler (SPI3 , g_dspiHandle [3 ]);
1649
1659
}
1650
1660
#endif
1651
1661
1652
- #if ( FSL_FEATURE_SOC_DSPI_COUNT > 4 )
1662
+ #if defined( SPI4 )
1653
1663
void SPI4_DriverIRQHandler (void )
1654
1664
{
1655
1665
assert (g_dspiHandle [4 ]);
1656
1666
DSPI_CommonIRQHandler (SPI4 , g_dspiHandle [4 ]);
1657
1667
}
1658
1668
#endif
1659
1669
1660
- #if ( FSL_FEATURE_SOC_DSPI_COUNT > 5 )
1670
+ #if defined( SPI5 )
1661
1671
void SPI5_DriverIRQHandler (void )
1662
1672
{
1663
1673
assert (g_dspiHandle [5 ]);
0 commit comments