fsl_csi.c 20 KB

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