1 /*
2 * Copyright 2018 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_common.h"
10 #include "fsl_dspi.h"
11
12 #include "fsl_adapter_spi.h"
13
14 /*******************************************************************************
15 * Definitions
16 ******************************************************************************/
17
18 /*! @brief hal_spi master state structure. */
19 typedef struct _hal_spi_master
20 {
21 hal_spi_master_transfer_callback_t callback;
22 void *callbackParam;
23 dspi_master_handle_t hardwareHandle;
24 uint8_t instance;
25 } hal_spi_master_t;
26
27 /*! @brief hal_spi slave state structure. */
28 typedef struct _hal_spi_slave
29 {
30 hal_spi_slave_transfer_callback_t callback;
31 void *callbackParam;
32 dspi_slave_handle_t hardwareHandle;
33 uint8_t instance;
34 } hal_spi_slave_t;
35
36 /*******************************************************************************
37 * Prototypes
38 ******************************************************************************/
39
40 /*******************************************************************************
41 * Variables
42 ******************************************************************************/
43
44 /*! @brief Base pointer array */
45 static SPI_Type *const s_spiBases[] = SPI_BASE_PTRS;
46
47 /*******************************************************************************
48 * Code
49 ******************************************************************************/
50
HAL_SpiGetStatus(status_t status)51 static hal_spi_status_t HAL_SpiGetStatus(status_t status)
52 {
53 hal_spi_status_t returnStatus;
54 switch (status)
55 {
56 case kStatus_Success:
57 {
58 returnStatus = kStatus_HAL_SpiSuccess;
59 break;
60 }
61 case kStatus_DSPI_Busy:
62 {
63 returnStatus = kStatus_HAL_SpiBusy;
64 break;
65 }
66 default:
67 {
68 returnStatus = kStatus_HAL_SpiError;
69 break;
70 }
71 }
72 return returnStatus;
73 }
74
HAL_SpiMasterCallback(SPI_Type * base,dspi_master_handle_t * handle,status_t status,void * callbackParam)75 static void HAL_SpiMasterCallback(SPI_Type *base, dspi_master_handle_t *handle, status_t status, void *callbackParam)
76 {
77 hal_spi_master_t *spiMasterHandle;
78
79 assert(callbackParam);
80
81 spiMasterHandle = (hal_spi_master_t *)callbackParam;
82
83 if (spiMasterHandle->callback != NULL)
84 {
85 spiMasterHandle->callback(spiMasterHandle, HAL_SpiGetStatus(status), spiMasterHandle->callbackParam);
86 }
87 }
88
HAL_SpiSlaveCallback(SPI_Type * base,dspi_slave_handle_t * handle,status_t status,void * callbackParam)89 static void HAL_SpiSlaveCallback(SPI_Type *base, dspi_slave_handle_t *handle, status_t status, void *callbackParam)
90 {
91 hal_spi_slave_t *spiSlaveHandle;
92
93 assert(callbackParam);
94
95 spiSlaveHandle = (hal_spi_slave_t *)callbackParam;
96
97 if (spiSlaveHandle->callback != NULL)
98 {
99 spiSlaveHandle->callback(spiSlaveHandle, HAL_SpiGetStatus(status), spiSlaveHandle->callbackParam);
100 }
101 }
102
HAL_SpiMasterInit(hal_spi_master_handle_t handle,const hal_spi_master_config_t * config)103 hal_spi_status_t HAL_SpiMasterInit(hal_spi_master_handle_t handle, const hal_spi_master_config_t *config)
104 {
105 hal_spi_master_t *masterHandle;
106 dspi_master_config_t dspiConfig;
107 assert(handle);
108 assert(config);
109 assert(HAL_SPI_MASTER_HANDLE_SIZE >= sizeof(hal_spi_master_t));
110
111 masterHandle = (hal_spi_master_t *)handle;
112
113 DSPI_MasterGetDefaultConfig(&dspiConfig);
114
115 dspiConfig.ctarConfig.cpol = (kHAL_SpiClockPolarityActiveHigh == config->polarity) ? kDSPI_ClockPolarityActiveHigh :
116 kDSPI_ClockPolarityActiveLow;
117 dspiConfig.ctarConfig.cpha =
118 (kHAL_SpiClockPhaseFirstEdge == config->phase) ? kDSPI_ClockPhaseFirstEdge : kDSPI_ClockPhaseSecondEdge;
119 dspiConfig.ctarConfig.direction = (kHAL_SpiMsbFirst == config->direction) ? kDSPI_MsbFirst : kDSPI_LsbFirst;
120 dspiConfig.ctarConfig.baudRate = config->baudRate_Bps;
121
122 masterHandle->instance = config->instance;
123
124 DSPI_MasterInit((SPI_Type *)s_spiBases[masterHandle->instance], (void *)&dspiConfig, config->srcClock_Hz);
125
126 return kStatus_HAL_SpiSuccess;
127 }
128
HAL_SpiSlaveInit(hal_spi_slave_handle_t handle,const hal_spi_slave_config_t * config)129 hal_spi_status_t HAL_SpiSlaveInit(hal_spi_slave_handle_t handle, const hal_spi_slave_config_t *config)
130 {
131 hal_spi_slave_t *slaveHandle;
132 dspi_slave_config_t dspiConfig;
133 assert(handle);
134 assert(config);
135 assert(HAL_SPI_SLAVE_HANDLE_SIZE >= sizeof(hal_spi_slave_t));
136
137 slaveHandle = (hal_spi_slave_t *)handle;
138
139 DSPI_SlaveGetDefaultConfig(&dspiConfig);
140
141 dspiConfig.ctarConfig.cpol = (kHAL_SpiClockPolarityActiveHigh == config->polarity) ? kDSPI_ClockPolarityActiveHigh :
142 kDSPI_ClockPolarityActiveLow;
143 dspiConfig.ctarConfig.cpha =
144 (kHAL_SpiClockPhaseFirstEdge == config->phase) ? kDSPI_ClockPhaseFirstEdge : kDSPI_ClockPhaseSecondEdge;
145
146 slaveHandle->instance = config->instance;
147
148 DSPI_SlaveInit((SPI_Type *)s_spiBases[slaveHandle->instance], (void *)&dspiConfig);
149
150 return kStatus_HAL_SpiSuccess;
151 }
152
HAL_SpiMasterDeinit(hal_spi_master_handle_t handle)153 hal_spi_status_t HAL_SpiMasterDeinit(hal_spi_master_handle_t handle)
154 {
155 hal_spi_master_t *masterHandle;
156
157 assert(handle);
158
159 masterHandle = (hal_spi_master_t *)handle;
160 DSPI_Deinit((SPI_Type *)s_spiBases[masterHandle->instance]);
161
162 return kStatus_HAL_SpiSuccess;
163 }
164
HAL_SpiSlaveDeinit(hal_spi_slave_handle_t handle)165 hal_spi_status_t HAL_SpiSlaveDeinit(hal_spi_slave_handle_t handle)
166 {
167 hal_spi_slave_t *slaveHandle;
168
169 assert(handle);
170
171 slaveHandle = (hal_spi_slave_t *)handle;
172
173 DSPI_Deinit((SPI_Type *)s_spiBases[slaveHandle->instance]);
174
175 return kStatus_HAL_SpiSuccess;
176 }
177
HAL_SpiMasterTransferInstallCallback(hal_spi_master_handle_t handle,hal_spi_master_transfer_callback_t callback,void * callbackParam)178 hal_spi_status_t HAL_SpiMasterTransferInstallCallback(hal_spi_master_handle_t handle,
179 hal_spi_master_transfer_callback_t callback,
180 void *callbackParam)
181 {
182 hal_spi_master_t *masterHandle;
183
184 assert(handle);
185
186 masterHandle = (hal_spi_master_t *)handle;
187
188 masterHandle->callback = callback;
189 masterHandle->callbackParam = callbackParam;
190
191 DSPI_MasterTransferCreateHandle((SPI_Type *)s_spiBases[masterHandle->instance], &masterHandle->hardwareHandle,
192 HAL_SpiMasterCallback, masterHandle);
193
194 return kStatus_HAL_SpiSuccess;
195 }
196
HAL_SpiMasterTransferBlocking(hal_spi_master_handle_t handle,hal_spi_transfer_t * xfer)197 hal_spi_status_t HAL_SpiMasterTransferBlocking(hal_spi_master_handle_t handle, hal_spi_transfer_t *xfer)
198 {
199 hal_spi_master_t *masterHandle;
200 dspi_transfer_t transfer;
201
202 assert(handle);
203 assert(xfer);
204
205 masterHandle = (hal_spi_master_t *)handle;
206
207 transfer.txData = xfer->txData;
208 transfer.rxData = xfer->rxData;
209 transfer.dataSize = xfer->dataSize;
210 transfer.configFlags =
211 (uint32_t)kDSPI_MasterCtar0 | (uint32_t)kDSPI_MasterPcs0 | (uint32_t)kDSPI_MasterPcsContinuous;
212
213 return HAL_SpiGetStatus(DSPI_MasterTransferBlocking((SPI_Type *)s_spiBases[masterHandle->instance], &transfer));
214 }
215
HAL_SpiMasterTransferNonBlocking(hal_spi_master_handle_t handle,hal_spi_transfer_t * xfer)216 hal_spi_status_t HAL_SpiMasterTransferNonBlocking(hal_spi_master_handle_t handle, hal_spi_transfer_t *xfer)
217 {
218 hal_spi_master_t *masterHandle;
219 dspi_transfer_t transfer;
220
221 assert(handle);
222 assert(xfer);
223
224 masterHandle = (hal_spi_master_t *)handle;
225 transfer.txData = xfer->txData;
226 transfer.rxData = xfer->rxData;
227 transfer.dataSize = xfer->dataSize;
228 transfer.configFlags =
229 (uint32_t)kDSPI_MasterCtar0 | (uint32_t)kDSPI_MasterPcs0 | (uint32_t)kDSPI_MasterPcsContinuous;
230 return HAL_SpiGetStatus(DSPI_MasterTransferNonBlocking((SPI_Type *)s_spiBases[masterHandle->instance],
231 &masterHandle->hardwareHandle, &transfer));
232 }
233
HAL_SpiMasterTransferGetCount(hal_spi_master_handle_t handle,size_t * spiCount)234 hal_spi_status_t HAL_SpiMasterTransferGetCount(hal_spi_master_handle_t handle, size_t *spiCount)
235 {
236 hal_spi_master_t *masterHandle;
237 assert(handle);
238 assert(spiCount);
239
240 masterHandle = (hal_spi_master_t *)handle;
241 return HAL_SpiGetStatus(DSPI_MasterTransferGetCount((SPI_Type *)s_spiBases[masterHandle->instance],
242 &masterHandle->hardwareHandle, spiCount));
243 }
244
HAL_SpiMasterTransferAbort(hal_spi_master_handle_t handle)245 hal_spi_status_t HAL_SpiMasterTransferAbort(hal_spi_master_handle_t handle)
246 {
247 hal_spi_master_t *masterHandle;
248 assert(handle);
249
250 masterHandle = (hal_spi_master_t *)handle;
251 DSPI_MasterTransferAbort((SPI_Type *)s_spiBases[masterHandle->instance], &masterHandle->hardwareHandle);
252 return kStatus_HAL_SpiSuccess;
253 }
254
HAL_SpiSlaveTransferInstallCallback(hal_spi_slave_handle_t handle,hal_spi_slave_transfer_callback_t callback,void * callbackParam)255 hal_spi_status_t HAL_SpiSlaveTransferInstallCallback(hal_spi_slave_handle_t handle,
256 hal_spi_slave_transfer_callback_t callback,
257 void *callbackParam)
258 {
259 hal_spi_slave_t *slaveHandle;
260
261 assert(handle);
262
263 slaveHandle = (hal_spi_slave_t *)handle;
264 slaveHandle->callback = callback;
265 slaveHandle->callbackParam = callbackParam;
266
267 DSPI_SlaveTransferCreateHandle((SPI_Type *)s_spiBases[slaveHandle->instance], &slaveHandle->hardwareHandle,
268 HAL_SpiSlaveCallback, slaveHandle);
269 return kStatus_HAL_SpiSuccess;
270 }
271
HAL_SpiSlaveTransferNonBlocking(hal_spi_slave_handle_t handle,hal_spi_transfer_t * xfer)272 hal_spi_status_t HAL_SpiSlaveTransferNonBlocking(hal_spi_slave_handle_t handle, hal_spi_transfer_t *xfer)
273 {
274 hal_spi_slave_t *slaveHandle;
275 dspi_transfer_t transfer;
276
277 assert(handle);
278 assert(xfer);
279
280 slaveHandle = (hal_spi_slave_t *)handle;
281 transfer.txData = xfer->txData;
282 transfer.rxData = xfer->rxData;
283 transfer.dataSize = xfer->dataSize;
284 transfer.configFlags =
285 (uint32_t)kDSPI_MasterCtar0 | (uint32_t)kDSPI_MasterPcs0 | (uint32_t)kDSPI_MasterPcsContinuous;
286 return HAL_SpiGetStatus(DSPI_SlaveTransferNonBlocking((SPI_Type *)s_spiBases[slaveHandle->instance],
287 &slaveHandle->hardwareHandle, &transfer));
288 }
289
HAL_SpiSlaveTransferGetCount(hal_spi_slave_handle_t handle,size_t * spiCount)290 hal_spi_status_t HAL_SpiSlaveTransferGetCount(hal_spi_slave_handle_t handle, size_t *spiCount)
291 {
292 hal_spi_slave_t *slaveHandle;
293 assert(handle);
294 assert(spiCount);
295
296 slaveHandle = (hal_spi_slave_t *)handle;
297 return HAL_SpiGetStatus(DSPI_SlaveTransferGetCount((SPI_Type *)s_spiBases[slaveHandle->instance],
298 &slaveHandle->hardwareHandle, spiCount));
299 }
300
HAL_SpiSlaveTransferAbort(hal_spi_slave_handle_t handle)301 hal_spi_status_t HAL_SpiSlaveTransferAbort(hal_spi_slave_handle_t handle)
302 {
303 hal_spi_slave_t *slaveHandle;
304 assert(handle);
305
306 slaveHandle = (hal_spi_slave_t *)handle;
307 DSPI_SlaveTransferAbort((SPI_Type *)s_spiBases[slaveHandle->instance], &slaveHandle->hardwareHandle);
308 return kStatus_HAL_SpiSuccess;
309 }
310