1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2020 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_flexio_camera_edma.h"
10
11 /*******************************************************************************
12 * Definitions
13 ******************************************************************************/
14
15 /* Component ID definition, used by tools. */
16 #ifndef FSL_COMPONENT_ID
17 #define FSL_COMPONENT_ID "platform.drivers.flexio_camera_edma"
18 #endif
19
20 /*<! Structure definition for camera_edma_private_handle_t. The structure is private. */
21 typedef struct _flexio_camera_edma_private_handle
22 {
23 FLEXIO_CAMERA_Type *base;
24 flexio_camera_edma_handle_t *handle;
25 } flexio_camera_edma_private_handle_t;
26
27 /* CAMERA EDMA transfer handle. */
28 enum _flexio_camera_edma_tansfer_states
29 {
30 kFLEXIO_CAMERA_RxIdle, /* RX idle. */
31 kFLEXIO_CAMERA_RxBusy /* RX busy. */
32 };
33
34 /*******************************************************************************
35 * Variables
36 ******************************************************************************/
37
38 /*< @brief user configurable flexio camera handle count. */
39 #define FLEXIO_CAMERA_HANDLE_COUNT 1
40
41 /*<! Private handle only used for internally. */
42 static flexio_camera_edma_private_handle_t s_edmaPrivateHandle[FLEXIO_CAMERA_HANDLE_COUNT];
43
44 /*******************************************************************************
45 * Prototypes
46 ******************************************************************************/
47
48 /*!
49 * @brief FLEXIO CAMERA EDMA receive finished callback function.
50 *
51 * This function is called when FLEXIO CAMERA EDMA receive finished. It disables the CAMERA
52 * RX EDMA request and sends @ref kStatus_FLEXIO_CAMERA_RxIdle to CAMERA callback.
53 *
54 * @param handle The EDMA handle.
55 * @param param Callback function parameter.
56 */
57 static void FLEXIO_CAMERA_TransferReceiveEDMACallback(edma_handle_t *handle,
58 void *param,
59 bool transferDone,
60 uint32_t tcds);
61
62 /*******************************************************************************
63 * Code
64 ******************************************************************************/
65
FLEXIO_CAMERA_TransferReceiveEDMACallback(edma_handle_t * handle,void * param,bool transferDone,uint32_t tcds)66 static void FLEXIO_CAMERA_TransferReceiveEDMACallback(edma_handle_t *handle,
67 void *param,
68 bool transferDone,
69 uint32_t tcds)
70 {
71 flexio_camera_edma_private_handle_t *cameraPrivateHandle = (flexio_camera_edma_private_handle_t *)param;
72
73 /* Avoid the warning for unused variables. */
74 handle = handle;
75 tcds = tcds;
76
77 if (transferDone)
78 {
79 FLEXIO_CAMERA_TransferAbortReceiveEDMA(cameraPrivateHandle->base, cameraPrivateHandle->handle);
80
81 if (cameraPrivateHandle->handle->callback != NULL)
82 {
83 cameraPrivateHandle->handle->callback(cameraPrivateHandle->base, cameraPrivateHandle->handle,
84 kStatus_FLEXIO_CAMERA_RxIdle, cameraPrivateHandle->handle->userData);
85 }
86 }
87 }
88 /*!
89 * brief Initializes the Camera handle, which is used in transactional functions.
90 *
91 * param base Pointer to the FLEXIO_CAMERA_Type.
92 * param handle Pointer to flexio_camera_edma_handle_t structure.
93 * param callback The callback function.
94 * param userData The parameter of the callback function.
95 * param rxEdmaHandle User requested DMA handle for RX DMA transfer.
96 * retval kStatus_Success Successfully create the handle.
97 * retval kStatus_OutOfRange The FlexIO Camera eDMA type/handle table out of range.
98 */
FLEXIO_CAMERA_TransferCreateHandleEDMA(FLEXIO_CAMERA_Type * base,flexio_camera_edma_handle_t * handle,flexio_camera_edma_transfer_callback_t callback,void * userData,edma_handle_t * rxEdmaHandle)99 status_t FLEXIO_CAMERA_TransferCreateHandleEDMA(FLEXIO_CAMERA_Type *base,
100 flexio_camera_edma_handle_t *handle,
101 flexio_camera_edma_transfer_callback_t callback,
102 void *userData,
103 edma_handle_t *rxEdmaHandle)
104 {
105 assert(handle != NULL);
106
107 uint8_t index;
108
109 /* Find the an empty handle pointer to store the handle. */
110 for (index = 0U; index < (uint8_t)FLEXIO_CAMERA_HANDLE_COUNT; index++)
111 {
112 if (s_edmaPrivateHandle[index].base == NULL)
113 {
114 s_edmaPrivateHandle[index].base = base;
115 s_edmaPrivateHandle[index].handle = handle;
116 break;
117 }
118 }
119
120 if (index == (uint8_t)FLEXIO_CAMERA_HANDLE_COUNT)
121 {
122 return kStatus_OutOfRange;
123 }
124
125 s_edmaPrivateHandle[index].base = base;
126 s_edmaPrivateHandle[index].handle = handle;
127
128 (void)memset(handle, 0, sizeof(*handle));
129
130 handle->rxState = (uint8_t)kFLEXIO_CAMERA_RxIdle;
131 handle->rxEdmaHandle = rxEdmaHandle;
132
133 handle->callback = callback;
134 handle->userData = userData;
135
136 /* Configure RX. */
137 if (rxEdmaHandle != NULL)
138 {
139 EDMA_SetCallback(handle->rxEdmaHandle, FLEXIO_CAMERA_TransferReceiveEDMACallback, &s_edmaPrivateHandle);
140 }
141
142 return kStatus_Success;
143 }
144
145 /*!
146 * brief Receives data using eDMA.
147 *
148 * This function receives data using eDMA. This is a non-blocking function, which returns
149 * right away. When all data is received, the receive callback function is called.
150 *
151 * param base Pointer to the FLEXIO_CAMERA_Type.
152 * param handle Pointer to the flexio_camera_edma_handle_t structure.
153 * param xfer Camera eDMA transfer structure, see #flexio_camera_transfer_t.
154 * retval kStatus_Success if succeeded, others failed.
155 * retval kStatus_CAMERA_RxBusy Previous transfer on going.
156 */
FLEXIO_CAMERA_TransferReceiveEDMA(FLEXIO_CAMERA_Type * base,flexio_camera_edma_handle_t * handle,flexio_camera_transfer_t * xfer)157 status_t FLEXIO_CAMERA_TransferReceiveEDMA(FLEXIO_CAMERA_Type *base,
158 flexio_camera_edma_handle_t *handle,
159 flexio_camera_transfer_t *xfer)
160 {
161 assert(handle->rxEdmaHandle != NULL);
162
163 edma_transfer_config_t xferConfig;
164 status_t status;
165
166 /* If previous RX not finished. */
167 if ((uint8_t)kFLEXIO_CAMERA_RxBusy == handle->rxState)
168 {
169 status = kStatus_FLEXIO_CAMERA_RxBusy;
170 }
171 else
172 {
173 handle->rxState = (uint8_t)kFLEXIO_CAMERA_RxBusy;
174
175 /* Prepare transfer. */
176 EDMA_PrepareTransfer(&xferConfig, (uint32_t *)FLEXIO_CAMERA_GetRxBufferAddress(base), 32,
177 (uint32_t *)xfer->dataAddress, 32, 32, xfer->dataNum, kEDMA_PeripheralToMemory);
178
179 /* Store the initially configured eDMA minor byte transfer count into the FLEXIO CAMERA handle */
180 handle->nbytes = 32;
181
182 /* Submit transfer. */
183 (void)EDMA_SubmitTransfer(handle->rxEdmaHandle, &xferConfig);
184 EDMA_StartTransfer(handle->rxEdmaHandle);
185 /* Enable CAMERA RX EDMA. */
186 FLEXIO_CAMERA_EnableRxDMA(base, true);
187 status = kStatus_Success;
188 }
189
190 return status;
191 }
192
193 /*!
194 * brief Aborts the receive data which used the eDMA.
195 *
196 * This function aborts the receive data which used the eDMA.
197 *
198 * param base Pointer to the FLEXIO_CAMERA_Type.
199 * param handle Pointer to the flexio_camera_edma_handle_t structure.
200 */
FLEXIO_CAMERA_TransferAbortReceiveEDMA(FLEXIO_CAMERA_Type * base,flexio_camera_edma_handle_t * handle)201 void FLEXIO_CAMERA_TransferAbortReceiveEDMA(FLEXIO_CAMERA_Type *base, flexio_camera_edma_handle_t *handle)
202 {
203 assert(handle->rxEdmaHandle != NULL);
204
205 /* Disable CAMERA RX EDMA. */
206 FLEXIO_CAMERA_EnableRxDMA(base, false);
207
208 /* Stop transfer. */
209 EDMA_StopTransfer(handle->rxEdmaHandle);
210
211 handle->rxState = (uint8_t)kFLEXIO_CAMERA_RxIdle;
212 }
213
214 /*!
215 * brief Gets the remaining bytes to be received.
216 *
217 * This function gets the number of bytes still not received.
218 *
219 * param base Pointer to the FLEXIO_CAMERA_Type.
220 * param handle Pointer to the flexio_camera_edma_handle_t structure.
221 * param count Number of bytes sent so far by the non-blocking transaction.
222 * retval kStatus_Success Succeed get the transfer count.
223 * retval kStatus_InvalidArgument The count parameter is invalid.
224 */
FLEXIO_CAMERA_TransferGetReceiveCountEDMA(FLEXIO_CAMERA_Type * base,flexio_camera_edma_handle_t * handle,size_t * count)225 status_t FLEXIO_CAMERA_TransferGetReceiveCountEDMA(FLEXIO_CAMERA_Type *base,
226 flexio_camera_edma_handle_t *handle,
227 size_t *count)
228 {
229 assert(handle->rxEdmaHandle != NULL);
230
231 if (NULL == count)
232 {
233 return kStatus_InvalidArgument;
234 }
235
236 if ((uint8_t)kFLEXIO_CAMERA_RxBusy == handle->rxState)
237 {
238 *count = (handle->rxSize -
239 (uint32_t)handle->nbytes *
240 EDMA_GetRemainingMajorLoopCount(handle->rxEdmaHandle->base, handle->rxEdmaHandle->channel));
241 }
242 else
243 {
244 *count = handle->rxSize;
245 }
246
247 return kStatus_Success;
248 }
249