diff --git a/src/omv/py/py_sensor.c b/src/omv/py/py_sensor.c index 6d61bfeaf..ef7e8f6bc 100644 --- a/src/omv/py/py_sensor.c +++ b/src/omv/py/py_sensor.c @@ -27,7 +27,10 @@ static mp_obj_t py_sensor_reset() { static mp_obj_t py_sensor_snapshot() { mp_obj_t image = py_image(0, 0, 0, 0); - sensor_snapshot((struct image*) py_image_cobj(image)); + if (sensor_snapshot((struct image*) py_image_cobj(image))==-1) { + nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "Sensor Timeout!!")); + return mp_const_false; + } return image; } diff --git a/src/omv/sensor.c b/src/omv/sensor.c index 0d9730c99..eb084b0d3 100644 --- a/src/omv/sensor.c +++ b/src/omv/sensor.c @@ -291,6 +291,7 @@ int sensor_snapshot(struct image *image) { volatile uint32_t addr; volatile uint16_t length; + uint32_t snapshot_start; addr = (uint32_t) fb->pixels; @@ -303,13 +304,22 @@ int sensor_snapshot(struct image *image) /* Lock framebuffer mutex */ mutex_lock(&fb->lock); + // Snapshot start tick + snapshot_start = HAL_GetTick(); + /* Start the DCMI */ HAL_DCMI_Start_DMA(&DCMIHandle, DCMI_MODE_SNAPSHOT, addr, length); /* Wait for frame */ while ((DCMI->CR & DCMI_CR_CAPTURE) != 0) { - + if (sys_tick_has_passed(snapshot_start, 1000)) { + // Sensor timeout, most likely a HW issue. + // unlock fb mutex and abort the DMA request + mutex_unlock(&fb->lock); + HAL_DMA_Abort(&DMAHandle); + return -1; + } } if (sensor.pixformat == PIXFORMAT_GRAYSCALE) {