From 06c8dcda17e140e80c3fcc7799f8ac318c8f843f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Jul 2025 18:13:15 +0200 Subject: [PATCH] ports/mimxrt: Refactor omv_csi_init. Signed-off-by: iabdalkader --- ports/mimxrt/mimxrt_hal.c | 22 ++++-------- ports/mimxrt/omv_csi.c | 70 +++++++-------------------------------- 2 files changed, 19 insertions(+), 73 deletions(-) diff --git a/ports/mimxrt/mimxrt_hal.c b/ports/mimxrt/mimxrt_hal.c index 6341e08c6..ece702208 100644 --- a/ports/mimxrt/mimxrt_hal.c +++ b/ports/mimxrt/mimxrt_hal.c @@ -107,6 +107,11 @@ void mimxrt_hal_init() { edma_config_t edma_config = {0}; EDMA_GetDefaultConfig(&edma_config); EDMA_Init(DMA0, &edma_config); + + #if MICROPY_PY_CSI + // Enable CSI clock and configure pins. + mimxrt_hal_csi_init(CSI); + #endif } void mimxrt_hal_bootloader() { @@ -118,9 +123,10 @@ void mimxrt_hal_bootloader() { } int mimxrt_hal_csi_init(CSI_Type *inst) { + // Enable CSI clock. CLOCK_EnableClock(kCLOCK_Csi); - // Configure DCMI pins. + // Configure CSI pins. omv_gpio_config(OMV_CSI_D0_PIN, OMV_GPIO_MODE_ALT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_MED, -1); omv_gpio_config(OMV_CSI_D1_PIN, OMV_GPIO_MODE_ALT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_MED, -1); omv_gpio_config(OMV_CSI_D2_PIN, OMV_GPIO_MODE_ALT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_MED, -1); @@ -135,20 +141,6 @@ int mimxrt_hal_csi_init(CSI_Type *inst) { omv_gpio_config(OMV_CSI_VSYNC_PIN, OMV_GPIO_MODE_ALT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_MED, -1); omv_gpio_config(OMV_CSI_PXCLK_PIN, OMV_GPIO_MODE_ALT, OMV_GPIO_PULL_NONE, OMV_GPIO_SPEED_MED, -1); - // Configure DCMI GPIOs - #if defined(OMV_CSI_RESET_PIN) - omv_gpio_config(OMV_CSI_RESET_PIN, OMV_GPIO_MODE_OUTPUT, OMV_GPIO_PULL_DOWN, OMV_GPIO_SPEED_LOW, -1); - #endif - #if defined(OMV_CSI_FSYNC_PIN) - omv_gpio_config(OMV_CSI_FSYNC_PIN, OMV_GPIO_MODE_OUTPUT, OMV_GPIO_PULL_DOWN, OMV_GPIO_SPEED_LOW, -1); - #endif - #if defined(OMV_CSI_POWER_PIN) - omv_gpio_config(OMV_CSI_POWER_PIN, OMV_GPIO_MODE_OUTPUT, OMV_GPIO_PULL_UP, OMV_GPIO_SPEED_LOW, -1); - #endif - - // Configure IRQ priority. - NVIC_SetPriority(CSI_IRQn, IRQ_PRI_CSI); - return 0; } diff --git a/ports/mimxrt/omv_csi.c b/ports/mimxrt/omv_csi.c index dc18b6447..c182c7c7f 100644 --- a/ports/mimxrt/omv_csi.c +++ b/ports/mimxrt/omv_csi.c @@ -56,9 +56,8 @@ extern uint8_t _line_buf[OMV_LINE_BUF_SIZE]; int imx_csi_config(omv_csi_t *csi, omv_csi_config_t config) { if (config == OMV_CSI_CONFIG_INIT) { + // Reset and configure CSI. CSI_Reset(CSI); - NVIC_DisableIRQ(CSI_IRQn); - // CSI_Reset does not zero CR1. CSI_REG_CR1(CSI) = 0; // CSI mode: HSYNC, VSYNC, and PIXCLK signals are used. @@ -94,8 +93,11 @@ int imx_csi_config(omv_csi_t *csi, omv_csi_config_t config) { } static int imx_csi_abort(omv_csi_t *csi, bool fifo_flush, bool in_irq) { - NVIC_DisableIRQ(CSI_IRQn); + // Disable CSI interrupts. CSI_DisableInterrupts(CSI, CSI_IRQ_FLAGS); + NVIC_DisableIRQ(CSI_IRQn); + NVIC_ClearPendingIRQ(CSI_IRQn); + CSI_REG_CR3(CSI) &= ~CSI_CR3_DMA_REQ_EN_RFF_MASK; CSI_REG_CR18(CSI) &= ~CSI_CR18_CSI_ENABLE_MASK; csi->dest_inc = 0; @@ -395,8 +397,10 @@ int imx_csi_snapshot(omv_csi_t *csi, image_t *image, uint32_t flags) { (dma_line_bytes << CSI_IMAG_PARA_IMAGE_WIDTH_SHIFT) | (1 << CSI_IMAG_PARA_IMAGE_HEIGHT_SHIFT); - // Configure and enable CSI interrupts. + // Enable CSI interrupts. CSI_EnableInterrupts(CSI, CSI_IRQ_FLAGS); + NVIC_ClearPendingIRQ(CSI_IRQn); + NVIC_SetPriority(CSI_IRQn, IRQ_PRI_CSI); NVIC_EnableIRQ(CSI_IRQn); // Enable CSI @@ -499,60 +503,10 @@ int imx_csi_snapshot(omv_csi_t *csi, image_t *image, uint32_t flags) { return 0; } -int omv_csi_init() { - int init_ret = 0; - static omv_i2c_t i2c; - - mimxrt_hal_csi_init(CSI); - - #if defined(OMV_CSI_POWER_PIN) - omv_gpio_write(OMV_CSI_POWER_PIN, 1); - #endif - - #if defined(OMV_CSI_RESET_PIN) - omv_gpio_write(OMV_CSI_RESET_PIN, 1); - #endif - - // Initialize the CSIs using this driver's ops as defaults, - // which can be overridden by sensor drivers during probe. - for (size_t i=0; ii2c = &i2c; - csi->fb = framebuffer_get(-1); - csi->abort = imx_csi_abort; - csi->config = imx_csi_config; - csi->snapshot = imx_csi_snapshot; - csi->color_palette = rainbow_table; - } - - // Configure the csi external clock (XCLK). - if (omv_csi_set_clk_frequency(OMV_CSI_CLK_FREQUENCY) != 0) { - // Failed to initialize the csi clock. - return OMV_CSI_ERROR_TIM_INIT_FAILED; - } - - // Initialize the camera bus. - omv_i2c_init(&i2c, OMV_CSI_I2C_ID, OMV_CSI_I2C_SPEED); - - // Detect and initialize the image sensor. - if ((init_ret = omv_csi_probe(&i2c)) != 0) { - // Sensor probe/init failed. - return init_ret; - } - - // Configure the CSI interfaces. - for (size_t i=0; ienabled = 0; +int omv_csi_ops_init(omv_csi_t *csi) { + csi->abort = imx_csi_abort; + csi->config = imx_csi_config; + csi->snapshot = imx_csi_snapshot; return 0; } #endif // MICROPY_PY_CSI