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_spi.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     spi_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     spi_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 Array to map SPI instance number to base address. */
45 static const SPI_Type *s_spiBases[] = SPI_BASE_PTRS;
46 
47 /*******************************************************************************
48  * Code
49  ******************************************************************************/
50 
HAL_SpiGetStatus(status_t status)51 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_SPI_Busy:
62         {
63             returnStatus = kStatus_HAL_SpiBusy;
64             break;
65         }
66         case kStatus_SPI_Idle:
67         {
68             returnStatus = kStatus_HAL_SpiIdle;
69             break;
70         }
71         default:
72         {
73             returnStatus = kStatus_HAL_SpiError;
74             break;
75         }
76     }
77     return returnStatus;
78 }
79 
HAL_SpiMasterCallback(SPI_Type * base,spi_master_handle_t * handle,status_t status,void * callbackParam)80 static void HAL_SpiMasterCallback(SPI_Type *base, spi_master_handle_t *handle, status_t status, void *callbackParam)
81 {
82     hal_spi_master_t *spiMasterHandle;
83     assert(callbackParam);
84 
85     spiMasterHandle = (hal_spi_master_t *)callbackParam;
86 
87     if (spiMasterHandle->callback)
88     {
89         spiMasterHandle->callback(spiMasterHandle, HAL_SpiGetStatus(status), spiMasterHandle->callbackParam);
90     }
91 }
92 
HAL_SpiSlaveCallback(SPI_Type * base,spi_slave_handle_t * handle,status_t status,void * callbackParam)93 static void HAL_SpiSlaveCallback(SPI_Type *base, spi_slave_handle_t *handle, status_t status, void *callbackParam)
94 {
95     hal_spi_slave_t *spiSlaveHandle;
96     assert(callbackParam);
97 
98     spiSlaveHandle = (hal_spi_slave_t *)callbackParam;
99 
100     if (spiSlaveHandle->callback)
101     {
102         spiSlaveHandle->callback(spiSlaveHandle, HAL_SpiGetStatus(status), spiSlaveHandle->callbackParam);
103     }
104 }
105 
HAL_SpiMasterInit(hal_spi_master_handle_t handle,const hal_spi_master_config_t * config)106 hal_spi_status_t HAL_SpiMasterInit(hal_spi_master_handle_t handle, const hal_spi_master_config_t *config)
107 {
108     hal_spi_master_t *masterHandle;
109     spi_master_config_t spiConfig;
110     assert(handle);
111     assert(config);
112 
113     if (HAL_SPI_MASTER_HANDLE_SIZE < sizeof(hal_spi_master_t))
114     {
115         return kStatus_HAL_SpiError;
116     }
117 
118     masterHandle = (hal_spi_master_t *)handle;
119 
120     SPI_MasterGetDefaultConfig(&spiConfig);
121 
122     spiConfig.polarity = (kHAL_SpiClockPolarityActiveHigh == config->polarity) ? kSPI_ClockPolarityActiveHigh :
123                                                                                  kSPI_ClockPolarityActiveLow;
124     spiConfig.phase =
125         (kHAL_SpiClockPhaseFirstEdge == config->phase) ? kSPI_ClockPhaseFirstEdge : kSPI_ClockPhaseSecondEdge;
126     spiConfig.direction    = (kHAL_SpiMsbFirst == config->direction) ? kSPI_MsbFirst : kSPI_LsbFirst;
127     spiConfig.baudRate_Bps = config->baudRate_Bps;
128     spiConfig.enableMaster = config->enableMaster;
129 
130     masterHandle->instance = config->instance;
131 
132     SPI_MasterInit((SPI_Type *)s_spiBases[masterHandle->instance], &spiConfig, config->srcClock_Hz);
133 
134     return kStatus_HAL_SpiSuccess;
135 }
136 
HAL_SpiSlaveInit(hal_spi_slave_handle_t handle,const hal_spi_slave_config_t * config)137 hal_spi_status_t HAL_SpiSlaveInit(hal_spi_slave_handle_t handle, const hal_spi_slave_config_t *config)
138 {
139     hal_spi_slave_t *slaveHandle;
140     spi_slave_config_t spiConfig;
141     assert(handle);
142     assert(config);
143 
144     if (HAL_SPI_SLAVE_HANDLE_SIZE < sizeof(hal_spi_slave_t))
145     {
146         return kStatus_HAL_SpiError;
147     }
148 
149     slaveHandle = (hal_spi_slave_t *)handle;
150 
151     SPI_SlaveGetDefaultConfig(&spiConfig);
152 
153     spiConfig.polarity = (kHAL_SpiClockPolarityActiveHigh == config->polarity) ? kSPI_ClockPolarityActiveHigh :
154                                                                                  kSPI_ClockPolarityActiveLow;
155     spiConfig.phase =
156         (kHAL_SpiClockPhaseFirstEdge == config->phase) ? kSPI_ClockPhaseFirstEdge : kSPI_ClockPhaseSecondEdge;
157     spiConfig.direction = (kHAL_SpiMsbFirst == config->direction) ? kSPI_MsbFirst : kSPI_LsbFirst;
158 
159     slaveHandle->instance = config->instance;
160 
161     SPI_SlaveInit((SPI_Type *)s_spiBases[slaveHandle->instance], &spiConfig);
162 
163     return kStatus_HAL_SpiSuccess;
164 }
165 
HAL_SpiMasterDeinit(hal_spi_master_handle_t handle)166 hal_spi_status_t HAL_SpiMasterDeinit(hal_spi_master_handle_t handle)
167 {
168     hal_spi_master_t *masterHandle;
169 
170     assert(handle);
171 
172     masterHandle = (hal_spi_master_t *)handle;
173     SPI_Deinit((SPI_Type *)s_spiBases[masterHandle->instance]);
174 
175     return kStatus_HAL_SpiSuccess;
176 }
177 
HAL_SpiSlaveDeinit(hal_spi_slave_handle_t handle)178 hal_spi_status_t HAL_SpiSlaveDeinit(hal_spi_slave_handle_t handle)
179 {
180     hal_spi_slave_t *slaveHandle;
181 
182     assert(handle);
183 
184     slaveHandle = (hal_spi_slave_t *)handle;
185 
186     SPI_Deinit((SPI_Type *)s_spiBases[slaveHandle->instance]);
187 
188     return kStatus_HAL_SpiSuccess;
189 }
190 
HAL_SpiMasterTransferInstallCallback(hal_spi_master_handle_t handle,hal_spi_master_transfer_callback_t callback,void * callbackParam)191 hal_spi_status_t HAL_SpiMasterTransferInstallCallback(hal_spi_master_handle_t handle,
192                                                       hal_spi_master_transfer_callback_t callback,
193                                                       void *callbackParam)
194 {
195     hal_spi_master_t *masterHandle;
196 
197     assert(handle);
198 
199     masterHandle = (hal_spi_master_t *)handle;
200 
201     masterHandle->callback      = callback;
202     masterHandle->callbackParam = callbackParam;
203 
204     SPI_MasterTransferCreateHandle((SPI_Type *)s_spiBases[masterHandle->instance], &masterHandle->hardwareHandle,
205                                    HAL_SpiMasterCallback, masterHandle);
206 
207     return kStatus_HAL_SpiSuccess;
208 }
209 
HAL_SpiMasterTransferBlocking(hal_spi_master_handle_t handle,hal_spi_transfer_t * xfer)210 hal_spi_status_t HAL_SpiMasterTransferBlocking(hal_spi_master_handle_t handle, hal_spi_transfer_t *xfer)
211 {
212     hal_spi_master_t *masterHandle;
213     spi_transfer_t transfer;
214 
215     assert(handle);
216     assert(xfer);
217 
218     masterHandle = (hal_spi_master_t *)handle;
219 
220     transfer.txData      = xfer->txData;
221     transfer.rxData      = xfer->rxData;
222     transfer.dataSize    = xfer->dataSize;
223     transfer.configFlags = xfer->flags;
224 
225     return HAL_SpiGetStatus(SPI_MasterTransferBlocking((SPI_Type *)s_spiBases[masterHandle->instance], &transfer));
226 }
227 
HAL_SpiMasterTransferNonBlocking(hal_spi_master_handle_t handle,hal_spi_transfer_t * xfer)228 hal_spi_status_t HAL_SpiMasterTransferNonBlocking(hal_spi_master_handle_t handle, hal_spi_transfer_t *xfer)
229 {
230     hal_spi_master_t *masterHandle;
231     spi_transfer_t transfer;
232 
233     assert(handle);
234     assert(xfer);
235 
236     masterHandle         = (hal_spi_master_t *)handle;
237     transfer.txData      = xfer->txData;
238     transfer.rxData      = xfer->rxData;
239     transfer.dataSize    = xfer->dataSize;
240     transfer.configFlags = xfer->flags;
241     return HAL_SpiGetStatus(SPI_MasterTransferNonBlocking((SPI_Type *)s_spiBases[masterHandle->instance],
242                                                           &masterHandle->hardwareHandle, &transfer));
243 }
244 
HAL_SpiMasterTransferGetCount(hal_spi_master_handle_t handle,size_t * count)245 hal_spi_status_t HAL_SpiMasterTransferGetCount(hal_spi_master_handle_t handle, size_t *count)
246 {
247     hal_spi_master_t *masterHandle;
248     assert(handle);
249     assert(count);
250 
251     masterHandle = (hal_spi_master_t *)handle;
252     return HAL_SpiGetStatus(SPI_MasterTransferGetCount((SPI_Type *)s_spiBases[masterHandle->instance],
253                                                        &masterHandle->hardwareHandle, count));
254 }
255 
HAL_SpiMasterTransferAbort(hal_spi_master_handle_t handle)256 hal_spi_status_t HAL_SpiMasterTransferAbort(hal_spi_master_handle_t handle)
257 {
258     hal_spi_master_t *masterHandle;
259     assert(handle);
260 
261     masterHandle = (hal_spi_master_t *)handle;
262     SPI_MasterTransferAbort((SPI_Type *)s_spiBases[masterHandle->instance], &masterHandle->hardwareHandle);
263     return kStatus_HAL_SpiSuccess;
264 }
265 
HAL_SpiSlaveTransferInstallCallback(hal_spi_slave_handle_t handle,hal_spi_slave_transfer_callback_t callback,void * callbackParam)266 hal_spi_status_t HAL_SpiSlaveTransferInstallCallback(hal_spi_slave_handle_t handle,
267                                                      hal_spi_slave_transfer_callback_t callback,
268                                                      void *callbackParam)
269 {
270     hal_spi_slave_t *slaveHandle;
271 
272     assert(handle);
273 
274     slaveHandle                = (hal_spi_slave_t *)handle;
275     slaveHandle->callback      = callback;
276     slaveHandle->callbackParam = callbackParam;
277 
278     SPI_SlaveTransferCreateHandle((SPI_Type *)s_spiBases[slaveHandle->instance], &slaveHandle->hardwareHandle,
279                                   HAL_SpiSlaveCallback, slaveHandle);
280     return kStatus_HAL_SpiSuccess;
281 }
282 
HAL_SpiSlaveTransferNonBlocking(hal_spi_slave_handle_t handle,hal_spi_transfer_t * xfer)283 hal_spi_status_t HAL_SpiSlaveTransferNonBlocking(hal_spi_slave_handle_t handle, hal_spi_transfer_t *xfer)
284 {
285     hal_spi_slave_t *slaveHandle;
286     spi_transfer_t transfer;
287 
288     assert(handle);
289     assert(xfer);
290 
291     slaveHandle          = (hal_spi_slave_t *)handle;
292     transfer.txData      = xfer->txData;
293     transfer.rxData      = xfer->rxData;
294     transfer.dataSize    = xfer->dataSize;
295     transfer.configFlags = xfer->flags;
296     return HAL_SpiGetStatus(SPI_SlaveTransferNonBlocking((SPI_Type *)s_spiBases[slaveHandle->instance],
297                                                          &slaveHandle->hardwareHandle, &transfer));
298 }
299 
HAL_SpiSlaveTransferGetCount(hal_spi_slave_handle_t handle,size_t * count)300 hal_spi_status_t HAL_SpiSlaveTransferGetCount(hal_spi_slave_handle_t handle, size_t *count)
301 {
302     hal_spi_slave_t *slaveHandle;
303     assert(handle);
304     assert(count);
305 
306     slaveHandle = (hal_spi_slave_t *)handle;
307     return HAL_SpiGetStatus(
308         SPI_SlaveTransferGetCount((SPI_Type *)s_spiBases[slaveHandle->instance], &slaveHandle->hardwareHandle, count));
309 }
310 
HAL_SpiSlaveTransferAbort(hal_spi_slave_handle_t handle)311 hal_spi_status_t HAL_SpiSlaveTransferAbort(hal_spi_slave_handle_t handle)
312 {
313     hal_spi_slave_t *slaveHandle;
314     assert(handle);
315 
316     slaveHandle = (hal_spi_slave_t *)handle;
317     SPI_SlaveTransferAbort((SPI_Type *)s_spiBases[slaveHandle->instance], &slaveHandle->hardwareHandle);
318     return kStatus_HAL_SpiSuccess;
319 }
320