fsl_csi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * Copyright (c) 2017, NXP Semiconductors, 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 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. #include "fsl_csi.h"
  31. /*******************************************************************************
  32. * Definitions
  33. ******************************************************************************/
  34. /* Two frame buffer loaded to CSI register at most. */
  35. #define CSI_MAX_ACTIVE_FRAME_NUM 2
  36. /*******************************************************************************
  37. * Prototypes
  38. ******************************************************************************/
  39. /*!
  40. * @brief Get the instance from the base address
  41. *
  42. * @param base CSI peripheral base address
  43. *
  44. * @return The CSI module instance
  45. */
  46. static uint32_t CSI_GetInstance(CSI_Type *base);
  47. /*!
  48. * @brief Get the delta value of two index in queue.
  49. *
  50. * @param startIdx Start index.
  51. * @param endIdx End index.
  52. *
  53. * @return The delta between startIdx and endIdx in queue.
  54. */
  55. static uint32_t CSI_TransferGetQueueDelta(uint32_t startIdx, uint32_t endIdx);
  56. /*!
  57. * @brief Increase a index value in queue.
  58. *
  59. * This function increases the index value in the queue, if the index is out of
  60. * the queue range, it is reset to 0.
  61. *
  62. * @param idx The index value to increase.
  63. *
  64. * @return The index value after increase.
  65. */
  66. static uint32_t CSI_TransferIncreaseQueueIdx(uint32_t idx);
  67. /*!
  68. * @brief Get the empty frame buffer count in queue.
  69. *
  70. * @param base CSI peripheral base address
  71. * @param handle Pointer to CSI driver handle.
  72. *
  73. * @return Number of the empty frame buffer count in queue.
  74. */
  75. static uint32_t CSI_TransferGetEmptyBufferCount(CSI_Type *base, csi_handle_t *handle);
  76. /*!
  77. * @brief Load one empty frame buffer in queue to CSI module.
  78. *
  79. * Load one empty frame in queue to CSI module, this function could only be called
  80. * when there is empty frame buffer in queue.
  81. *
  82. * @param base CSI peripheral base address
  83. * @param handle Pointer to CSI driver handle.
  84. */
  85. static void CSI_TransferLoadBufferToDevice(CSI_Type *base, csi_handle_t *handle);
  86. /* Typedef for interrupt handler. */
  87. typedef void (*csi_isr_t)(CSI_Type *base, csi_handle_t *handle);
  88. /*******************************************************************************
  89. * Variables
  90. ******************************************************************************/
  91. /*! @brief Pointers to CSI bases for each instance. */
  92. static CSI_Type *const s_csiBases[] = CSI_BASE_PTRS;
  93. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  94. /*! @brief Pointers to CSI clocks for each CSI submodule. */
  95. static const clock_ip_name_t s_csiClocks[] = CSI_CLOCKS;
  96. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  97. /* Array for the CSI driver handle. */
  98. static csi_handle_t *s_csiHandle[ARRAY_SIZE(s_csiBases)];
  99. /* Array of CSI IRQ number. */
  100. static const IRQn_Type s_csiIRQ[] = CSI_IRQS;
  101. /* CSI ISR for transactional APIs. */
  102. static csi_isr_t s_csiIsr;
  103. /*******************************************************************************
  104. * Code
  105. ******************************************************************************/
  106. static uint32_t CSI_GetInstance(CSI_Type *base)
  107. {
  108. uint32_t instance;
  109. /* Find the instance index from base address mappings. */
  110. for (instance = 0; instance < ARRAY_SIZE(s_csiBases); instance++)
  111. {
  112. if (s_csiBases[instance] == base)
  113. {
  114. break;
  115. }
  116. }
  117. assert(instance < ARRAY_SIZE(s_csiBases));
  118. return instance;
  119. }
  120. static uint32_t CSI_TransferGetQueueDelta(uint32_t startIdx, uint32_t endIdx)
  121. {
  122. if (endIdx >= startIdx)
  123. {
  124. return endIdx - startIdx;
  125. }
  126. else
  127. {
  128. return startIdx + CSI_DRIVER_ACTUAL_QUEUE_SIZE - endIdx;
  129. }
  130. }
  131. static uint32_t CSI_TransferIncreaseQueueIdx(uint32_t idx)
  132. {
  133. uint32_t ret;
  134. /*
  135. * Here not use the method:
  136. * ret = (idx+1) % CSI_DRIVER_ACTUAL_QUEUE_SIZE;
  137. *
  138. * Because the mod function might be slow.
  139. */
  140. ret = idx + 1;
  141. if (ret >= CSI_DRIVER_ACTUAL_QUEUE_SIZE)
  142. {
  143. ret = 0;
  144. }
  145. return ret;
  146. }
  147. static uint32_t CSI_TransferGetEmptyBufferCount(CSI_Type *base, csi_handle_t *handle)
  148. {
  149. return CSI_TransferGetQueueDelta(handle->queueDrvReadIdx, handle->queueUserWriteIdx);
  150. }
  151. static void CSI_TransferLoadBufferToDevice(CSI_Type *base, csi_handle_t *handle)
  152. {
  153. /* Load the frame buffer address to CSI register. */
  154. CSI_SetRxBufferAddr(base, handle->nextBufferIdx, handle->frameBufferQueue[handle->queueDrvReadIdx]);
  155. handle->queueDrvReadIdx = CSI_TransferIncreaseQueueIdx(handle->queueDrvReadIdx);
  156. handle->activeBufferNum++;
  157. /* There are two CSI buffers, so could use XOR to get the next index. */
  158. handle->nextBufferIdx ^= 1U;
  159. }
  160. status_t CSI_Init(CSI_Type *base, const csi_config_t *config)
  161. {
  162. assert(config);
  163. uint32_t reg;
  164. uint32_t imgWidth_Bytes;
  165. imgWidth_Bytes = config->width * config->bytesPerPixel;
  166. /* The image width and frame buffer pitch should be multiple of 8-bytes. */
  167. if ((imgWidth_Bytes & 0x07) | ((uint32_t)config->linePitch_Bytes & 0x07))
  168. {
  169. return kStatus_InvalidArgument;
  170. }
  171. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  172. uint32_t instance = CSI_GetInstance(base);
  173. CLOCK_EnableClock(s_csiClocks[instance]);
  174. #endif
  175. CSI_Reset(base);
  176. /* Configure CSICR1. CSICR1 has been reset to the default value, so could write it directly. */
  177. reg = ((uint32_t)config->workMode) | config->polarityFlags | CSI_CSICR1_FCC_MASK;
  178. if (config->useExtVsync)
  179. {
  180. reg |= CSI_CSICR1_EXT_VSYNC_MASK;
  181. }
  182. base->CSICR1 = reg;
  183. /*
  184. * Generally, CSIIMAG_PARA[IMAGE_WIDTH] indicates how many data bus cycles per line.
  185. * One special case is when receiving 24-bit pixels through 8-bit data bus, and
  186. * CSICR3[ZERO_PACK_EN] is enabled, in this case, the CSIIMAG_PARA[IMAGE_WIDTH]
  187. * should be set to the pixel number per line.
  188. *
  189. * Currently the CSI driver only support 8-bit data bus, so generally the
  190. * CSIIMAG_PARA[IMAGE_WIDTH] is bytes number per line. When the CSICR3[ZERO_PACK_EN]
  191. * is enabled, CSIIMAG_PARA[IMAGE_WIDTH] is pixel number per line.
  192. *
  193. * NOTE: The CSIIMAG_PARA[IMAGE_WIDTH] setting code should be updated if the
  194. * driver is upgraded to support other data bus width.
  195. */
  196. if (4U == config->bytesPerPixel)
  197. {
  198. /* Enable zero pack. */
  199. base->CSICR3 |= CSI_CSICR3_ZERO_PACK_EN_MASK;
  200. /* Image parameter. */
  201. base->CSIIMAG_PARA = ((uint32_t)(config->width) << CSI_CSIIMAG_PARA_IMAGE_WIDTH_SHIFT) |
  202. ((uint32_t)(config->height) << CSI_CSIIMAG_PARA_IMAGE_HEIGHT_SHIFT);
  203. }
  204. else
  205. {
  206. /* Image parameter. */
  207. base->CSIIMAG_PARA = ((uint32_t)(imgWidth_Bytes) << CSI_CSIIMAG_PARA_IMAGE_WIDTH_SHIFT) |
  208. ((uint32_t)(config->height) << CSI_CSIIMAG_PARA_IMAGE_HEIGHT_SHIFT);
  209. }
  210. /* The CSI frame buffer bus is 8-byte width. */
  211. base->CSIFBUF_PARA = (uint32_t)((config->linePitch_Bytes - imgWidth_Bytes) / 8U)
  212. << CSI_CSIFBUF_PARA_FBUF_STRIDE_SHIFT;
  213. /* Enable auto ECC. */
  214. base->CSICR3 |= CSI_CSICR3_ECC_AUTO_EN_MASK;
  215. /*
  216. * For better performance.
  217. * The DMA burst size could be set to 16 * 8 byte, 8 * 8 byte, or 4 * 8 byte,
  218. * choose the best burst size based on bytes per line.
  219. */
  220. if (!(imgWidth_Bytes % (8 * 16)))
  221. {
  222. base->CSICR2 = CSI_CSICR2_DMA_BURST_TYPE_RFF(3U);
  223. base->CSICR3 = (CSI->CSICR3 & ~CSI_CSICR3_RxFF_LEVEL_MASK) | ((2U << CSI_CSICR3_RxFF_LEVEL_SHIFT));
  224. }
  225. else if (!(imgWidth_Bytes % (8 * 8)))
  226. {
  227. base->CSICR2 = CSI_CSICR2_DMA_BURST_TYPE_RFF(2U);
  228. base->CSICR3 = (CSI->CSICR3 & ~CSI_CSICR3_RxFF_LEVEL_MASK) | ((1U << CSI_CSICR3_RxFF_LEVEL_SHIFT));
  229. }
  230. else
  231. {
  232. base->CSICR2 = CSI_CSICR2_DMA_BURST_TYPE_RFF(1U);
  233. base->CSICR3 = (CSI->CSICR3 & ~CSI_CSICR3_RxFF_LEVEL_MASK) | ((0U << CSI_CSICR3_RxFF_LEVEL_SHIFT));
  234. }
  235. CSI_ReflashFifoDma(base, kCSI_RxFifo);
  236. return kStatus_Success;
  237. }
  238. void CSI_Deinit(CSI_Type *base)
  239. {
  240. /* Disable transfer first. */
  241. CSI_Stop(base);
  242. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  243. uint32_t instance = CSI_GetInstance(base);
  244. CLOCK_DisableClock(s_csiClocks[instance]);
  245. #endif
  246. }
  247. void CSI_Reset(CSI_Type *base)
  248. {
  249. uint32_t csisr;
  250. /* Disable transfer first. */
  251. CSI_Stop(base);
  252. /* Disable DMA request. */
  253. base->CSICR3 = 0U;
  254. /* Reset the fame count. */
  255. base->CSICR3 |= CSI_CSICR3_FRMCNT_RST_MASK;
  256. while (base->CSICR3 & CSI_CSICR3_FRMCNT_RST_MASK)
  257. {
  258. }
  259. /* Clear the RX FIFO. */
  260. CSI_ClearFifo(base, kCSI_AllFifo);
  261. /* Reflash DMA. */
  262. CSI_ReflashFifoDma(base, kCSI_AllFifo);
  263. /* Clear the status. */
  264. csisr = base->CSISR;
  265. base->CSISR = csisr;
  266. /* Set the control registers to default value. */
  267. base->CSICR1 = CSI_CSICR1_HSYNC_POL_MASK | CSI_CSICR1_EXT_VSYNC_MASK;
  268. base->CSICR2 = 0U;
  269. base->CSICR3 = 0U;
  270. #if defined(CSI_CSICR18_CSI_LCDIF_BUFFER_LINES)
  271. base->CSICR18 = CSI_CSICR18_AHB_HPROT(0x0DU) | CSI_CSICR18_CSI_LCDIF_BUFFER_LINES(0x02U);
  272. #else
  273. base->CSICR18 = CSI_CSICR18_AHB_HPROT(0x0DU);
  274. #endif
  275. base->CSIFBUF_PARA = 0U;
  276. base->CSIIMAG_PARA = 0U;
  277. }
  278. void CSI_GetDefaultConfig(csi_config_t *config)
  279. {
  280. assert(config);
  281. config->width = 320U;
  282. config->height = 240U;
  283. config->polarityFlags = kCSI_HsyncActiveHigh | kCSI_DataLatchOnRisingEdge;
  284. config->bytesPerPixel = 2U;
  285. config->linePitch_Bytes = 320U * 2U;
  286. config->workMode = kCSI_GatedClockMode;
  287. config->dataBus = kCSI_DataBus8Bit;
  288. config->useExtVsync = true;
  289. }
  290. void CSI_SetRxBufferAddr(CSI_Type *base, uint8_t index, uint32_t addr)
  291. {
  292. if (index)
  293. {
  294. base->CSIDMASA_FB2 = addr;
  295. }
  296. else
  297. {
  298. base->CSIDMASA_FB1 = addr;
  299. }
  300. }
  301. void CSI_ClearFifo(CSI_Type *base, csi_fifo_t fifo)
  302. {
  303. uint32_t cr1;
  304. uint32_t mask = 0U;
  305. /* The FIFO could only be cleared when CSICR1[FCC] = 0, so first clear the FCC. */
  306. cr1 = base->CSICR1;
  307. base->CSICR1 = (cr1 & ~CSI_CSICR1_FCC_MASK);
  308. if ((uint32_t)fifo & (uint32_t)kCSI_RxFifo)
  309. {
  310. mask |= CSI_CSICR1_CLR_RXFIFO_MASK;
  311. }
  312. if ((uint32_t)fifo & (uint32_t)kCSI_StatFifo)
  313. {
  314. mask |= CSI_CSICR1_CLR_STATFIFO_MASK;
  315. }
  316. base->CSICR1 = (cr1 & ~CSI_CSICR1_FCC_MASK) | mask;
  317. /* Wait clear completed. */
  318. while (base->CSICR1 & mask)
  319. {
  320. }
  321. /* Recover the FCC. */
  322. base->CSICR1 = cr1;
  323. }
  324. void CSI_ReflashFifoDma(CSI_Type *base, csi_fifo_t fifo)
  325. {
  326. uint32_t cr3 = 0U;
  327. if ((uint32_t)fifo & (uint32_t)kCSI_RxFifo)
  328. {
  329. cr3 |= CSI_CSICR3_DMA_REFLASH_RFF_MASK;
  330. }
  331. if ((uint32_t)fifo & (uint32_t)kCSI_StatFifo)
  332. {
  333. cr3 |= CSI_CSICR3_DMA_REFLASH_SFF_MASK;
  334. }
  335. base->CSICR3 |= cr3;
  336. /* Wait clear completed. */
  337. while (base->CSICR3 & cr3)
  338. {
  339. }
  340. }
  341. void CSI_EnableFifoDmaRequest(CSI_Type *base, csi_fifo_t fifo, bool enable)
  342. {
  343. uint32_t cr3 = 0U;
  344. if ((uint32_t)fifo & (uint32_t)kCSI_RxFifo)
  345. {
  346. cr3 |= CSI_CSICR3_DMA_REQ_EN_RFF_MASK;
  347. }
  348. if ((uint32_t)fifo & (uint32_t)kCSI_StatFifo)
  349. {
  350. cr3 |= CSI_CSICR3_DMA_REQ_EN_SFF_MASK;
  351. }
  352. if (enable)
  353. {
  354. base->CSICR3 |= cr3;
  355. }
  356. else
  357. {
  358. base->CSICR3 &= ~cr3;
  359. }
  360. }
  361. void CSI_EnableInterrupts(CSI_Type *base, uint32_t mask)
  362. {
  363. base->CSICR1 |= (mask & CSI_CSICR1_INT_EN_MASK);
  364. base->CSICR3 |= (mask & CSI_CSICR3_INT_EN_MASK);
  365. base->CSICR18 |= ((mask & CSI_CSICR18_INT_EN_MASK) >> 6U);
  366. }
  367. void CSI_DisableInterrupts(CSI_Type *base, uint32_t mask)
  368. {
  369. base->CSICR1 &= ~(mask & CSI_CSICR1_INT_EN_MASK);
  370. base->CSICR3 &= ~(mask & CSI_CSICR3_INT_EN_MASK);
  371. base->CSICR18 &= ~((mask & CSI_CSICR18_INT_EN_MASK) >> 6U);
  372. }
  373. status_t CSI_TransferCreateHandle(CSI_Type *base,
  374. csi_handle_t *handle,
  375. csi_transfer_callback_t callback,
  376. void *userData)
  377. {
  378. assert(handle);
  379. uint32_t instance;
  380. memset(handle, 0, sizeof(*handle));
  381. /* Set the callback and user data. */
  382. handle->callback = callback;
  383. handle->userData = userData;
  384. /* Get instance from peripheral base address. */
  385. instance = CSI_GetInstance(base);
  386. /* Save the handle in global variables to support the double weak mechanism. */
  387. s_csiHandle[instance] = handle;
  388. s_csiIsr = CSI_TransferHandleIRQ;
  389. /* Enable interrupt. */
  390. EnableIRQ(s_csiIRQ[instance]);
  391. return kStatus_Success;
  392. }
  393. status_t CSI_TransferStart(CSI_Type *base, csi_handle_t *handle)
  394. {
  395. assert(handle);
  396. uint32_t emptyBufferCount;
  397. emptyBufferCount = CSI_TransferGetEmptyBufferCount(base, handle);
  398. if (emptyBufferCount < 2U)
  399. {
  400. return kStatus_CSI_NoEmptyBuffer;
  401. }
  402. handle->nextBufferIdx = 0U;
  403. handle->activeBufferNum = 0U;
  404. /* Write to memory from second completed frame. */
  405. base->CSICR18 = (base->CSICR18 & ~CSI_CSICR18_MASK_OPTION_MASK) | CSI_CSICR18_MASK_OPTION(2);
  406. /* Load the frame buffer to CSI register, there are at least two empty buffers. */
  407. CSI_TransferLoadBufferToDevice(base, handle);
  408. CSI_TransferLoadBufferToDevice(base, handle);
  409. /* After reflash DMA, the CSI saves frame to frame buffer 0. */
  410. CSI_ReflashFifoDma(base, kCSI_RxFifo);
  411. handle->transferStarted = true;
  412. handle->transferOnGoing = true;
  413. CSI_EnableInterrupts(base, kCSI_RxBuffer1DmaDoneInterruptEnable | kCSI_RxBuffer0DmaDoneInterruptEnable);
  414. CSI_Start(base);
  415. return kStatus_Success;
  416. }
  417. status_t CSI_TransferStop(CSI_Type *base, csi_handle_t *handle)
  418. {
  419. assert(handle);
  420. CSI_Stop(base);
  421. CSI_DisableInterrupts(base, kCSI_RxBuffer1DmaDoneInterruptEnable | kCSI_RxBuffer0DmaDoneInterruptEnable);
  422. handle->transferStarted = false;
  423. handle->transferOnGoing = false;
  424. /* Stoped, reset the state flags. */
  425. handle->queueDrvReadIdx = handle->queueDrvWriteIdx;
  426. handle->activeBufferNum = 0U;
  427. return kStatus_Success;
  428. }
  429. status_t CSI_TransferSubmitEmptyBuffer(CSI_Type *base, csi_handle_t *handle, uint32_t frameBuffer)
  430. {
  431. uint32_t csicr1;
  432. if (CSI_DRIVER_QUEUE_SIZE == CSI_TransferGetQueueDelta(handle->queueUserReadIdx, handle->queueUserWriteIdx))
  433. {
  434. return kStatus_CSI_QueueFull;
  435. }
  436. /* Disable the interrupt to protect the index information in handle. */
  437. csicr1 = base->CSICR1;
  438. base->CSICR1 = (csicr1 & ~(CSI_CSICR1_FB2_DMA_DONE_INTEN_MASK | CSI_CSICR1_FB1_DMA_DONE_INTEN_MASK));
  439. /* Save the empty frame buffer address to queue. */
  440. handle->frameBufferQueue[handle->queueUserWriteIdx] = frameBuffer;
  441. handle->queueUserWriteIdx = CSI_TransferIncreaseQueueIdx(handle->queueUserWriteIdx);
  442. base->CSICR1 = csicr1;
  443. if (handle->transferStarted)
  444. {
  445. /*
  446. * If user has started transfer using @ref CSI_TransferStart, and the CSI is
  447. * stopped due to no empty frame buffer in queue, then start the CSI.
  448. */
  449. if ((!handle->transferOnGoing) && (CSI_TransferGetEmptyBufferCount(base, handle) >= 2U))
  450. {
  451. handle->transferOnGoing = true;
  452. handle->nextBufferIdx = 0U;
  453. /* Load the frame buffers to CSI module. */
  454. CSI_TransferLoadBufferToDevice(base, handle);
  455. CSI_TransferLoadBufferToDevice(base, handle);
  456. CSI_ReflashFifoDma(base, kCSI_RxFifo);
  457. CSI_Start(base);
  458. }
  459. }
  460. return kStatus_Success;
  461. }
  462. status_t CSI_TransferGetFullBuffer(CSI_Type *base, csi_handle_t *handle, uint32_t *frameBuffer)
  463. {
  464. uint32_t csicr1;
  465. /* No full frame buffer. */
  466. if (handle->queueUserReadIdx == handle->queueDrvWriteIdx)
  467. {
  468. return kStatus_CSI_NoFullBuffer;
  469. }
  470. /* Disable the interrupt to protect the index information in handle. */
  471. csicr1 = base->CSICR1;
  472. base->CSICR1 = (csicr1 & ~(CSI_CSICR1_FB2_DMA_DONE_INTEN_MASK | CSI_CSICR1_FB1_DMA_DONE_INTEN_MASK));
  473. *frameBuffer = handle->frameBufferQueue[handle->queueUserReadIdx];
  474. handle->queueUserReadIdx = CSI_TransferIncreaseQueueIdx(handle->queueUserReadIdx);
  475. base->CSICR1 = csicr1;
  476. return kStatus_Success;
  477. }
  478. void CSI_TransferHandleIRQ(CSI_Type *base, csi_handle_t *handle)
  479. {
  480. uint32_t queueDrvWriteIdx;
  481. uint32_t csisr = base->CSISR;
  482. /* Clear the error flags. */
  483. base->CSISR = csisr;
  484. /*
  485. * If both frame buffer 0 and frame buffer 1 flags assert, driver does not
  486. * know which frame buffer ready just now, so reset the CSI transfer to
  487. * start from frame buffer 0.
  488. */
  489. if ((csisr & (CSI_CSISR_DMA_TSF_DONE_FB2_MASK | CSI_CSISR_DMA_TSF_DONE_FB1_MASK)) ==
  490. (CSI_CSISR_DMA_TSF_DONE_FB2_MASK | CSI_CSISR_DMA_TSF_DONE_FB1_MASK))
  491. {
  492. CSI_Stop(base);
  493. /* Reset the active buffers. */
  494. if (1 <= handle->activeBufferNum)
  495. {
  496. queueDrvWriteIdx = handle->queueDrvWriteIdx;
  497. base->CSIDMASA_FB1 = handle->frameBufferQueue[queueDrvWriteIdx];
  498. if (2U == handle->activeBufferNum)
  499. {
  500. queueDrvWriteIdx = CSI_TransferIncreaseQueueIdx(queueDrvWriteIdx);
  501. base->CSIDMASA_FB2 = handle->frameBufferQueue[queueDrvWriteIdx];
  502. handle->nextBufferIdx = 0U;
  503. }
  504. else
  505. {
  506. handle->nextBufferIdx = 1U;
  507. }
  508. }
  509. CSI_ReflashFifoDma(base, kCSI_RxFifo);
  510. CSI_Start(base);
  511. }
  512. else if (csisr & (CSI_CSISR_DMA_TSF_DONE_FB2_MASK | CSI_CSISR_DMA_TSF_DONE_FB1_MASK))
  513. {
  514. handle->queueDrvWriteIdx = CSI_TransferIncreaseQueueIdx(handle->queueDrvWriteIdx);
  515. handle->activeBufferNum--;
  516. if (handle->callback)
  517. {
  518. handle->callback(base, handle, kStatus_CSI_FrameDone, handle->userData);
  519. }
  520. /* No frame buffer to save incoming data, then stop the CSI module. */
  521. if (!(handle->activeBufferNum))
  522. {
  523. CSI_Stop(base);
  524. handle->transferOnGoing = false;
  525. }
  526. else
  527. {
  528. if (CSI_TransferGetEmptyBufferCount(base, handle))
  529. {
  530. CSI_TransferLoadBufferToDevice(base, handle);
  531. }
  532. }
  533. }
  534. else
  535. {
  536. }
  537. }
  538. #if defined(CSI)
  539. void CSI_DriverIRQHandler(void)
  540. {
  541. s_csiIsr(CSI, s_csiHandle[0]);
  542. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  543. exception return operation might vector to incorrect interrupt */
  544. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  545. __DSB();
  546. #endif
  547. }
  548. #endif
  549. #if defined(CSI0)
  550. void CSI0_DriverIRQHandler(void)
  551. {
  552. s_csiIsr(CSI, s_csiHandle[0]);
  553. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  554. exception return operation might vector to incorrect interrupt */
  555. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  556. __DSB();
  557. #endif
  558. }
  559. #endif