From bc37611dfc19df98bdbf99985a4771ab79da3638 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 2 Jul 2025 18:14:27 +0200 Subject: [PATCH] ports/nrf: Refactor omv_csi_init. Signed-off-by: iabdalkader --- ports/nrf/main.c | 31 ++++++++++++++---------- ports/nrf/omv_csi.c | 58 +++------------------------------------------ 2 files changed, 22 insertions(+), 67 deletions(-) diff --git a/ports/nrf/main.c b/ports/nrf/main.c index 6d12e0d39..b0debe3c6 100644 --- a/ports/nrf/main.c +++ b/ports/nrf/main.c @@ -91,13 +91,19 @@ #include "omv_csi.h" #include "mp_utils.h" +extern uint32_t _heap_start; +extern uint32_t _heap_end; + uint32_t HAL_GetHalVersion() { // Hard-coded because it's not defined in SDK return ((2 << 24) | (0 << 16) | (0 << 8) | (0 << 0)); } -extern uint32_t _heap_start; -extern uint32_t _heap_end; +void NORETURN __fatal_error(const char *msg) { + while (1) { + ; + } +} #if MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE static int vfs_mount_and_chdir(mp_obj_t bdev, mp_obj_t mount_point) { @@ -142,6 +148,7 @@ soft_reset: machine_init(); mp_init(); readline_init0(); + pin_init0(); #if MICROPY_PY_MACHINE_SPI spi_init0(); #endif @@ -163,13 +170,20 @@ soft_reset: #if MICROPY_PY_MACHINE_UART uart_init0(); #endif - pin_init0(); - fb_alloc_init0(); framebuffer_init0(); + #if MICROPY_PY_CSI + omv_csi_init0(); + #endif #if MICROPY_PY_CSI - omv_csi_init(); + // Initialize the csi. + if (first_soft_reset) { + int ret = omv_csi_init(); + if (ret != 0 && ret != OMV_CSI_ERROR_ISC_UNDETECTED) { + __fatal_error("Failed to init the CSI"); + } + } #endif #if (MICROPY_PY_BLE_NUS == 0) && (MICROPY_HW_USB_CDC == 0) @@ -372,7 +386,6 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); #endif #endif - void HardFault_Handler(void) { #if defined(NRF52_SERIES) || defined(NRF91_SERIES) static volatile uint32_t reg; @@ -389,12 +402,6 @@ void HardFault_Handler(void) { #endif } -void NORETURN __fatal_error(const char *msg) { - while (1) { - ; - } -} - void nlr_jump_fail(void *val) { printf("FATAL: uncaught exception %p\n", val); mp_obj_print_exception(&mp_plat_print, (mp_obj_t) val); diff --git a/ports/nrf/omv_csi.c b/ports/nrf/omv_csi.c index 041e8ecc8..908e02b0a 100644 --- a/ports/nrf/omv_csi.c +++ b/ports/nrf/omv_csi.c @@ -206,60 +206,8 @@ static int nrf_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; - - #if defined(OMV_CSI_POWER_PIN) - nrf_gpio_cfg_output(OMV_CSI_POWER_PIN); - nrf_gpio_pin_write(OMV_CSI_POWER_PIN, 1); - #endif - - #if defined(OMV_CSI_RESET_PIN) - nrf_gpio_cfg_output(OMV_CSI_RESET_PIN); - nrf_gpio_pin_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 = NULL; - csi->config = nrf_csi_config; - csi->snapshot = nrf_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->config = nrf_csi_config; + csi->snapshot = nrf_csi_snapshot; return 0; }