openmv/boards/ARDUINO_NANO_RP2040_CONNECT/dcmi.pio
iabdalkader be6b3fa80d ports/rp2: Add support for multiple CSIs.
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2025-06-25 21:15:24 +02:00

112 lines
3.3 KiB
Plaintext

; This file is part of the OpenMV project.
;
; Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io>
; Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io>
;
; This work is licensed under the MIT license, see the file LICENSE for details.
; Image Sensor PIO program.
.define public PXCLK 29
.define public HSYNC 12
.define public VSYNC 13
.program dcmi_default
.wrap_target
pull block ; Read number of lines in OSR
mov y, osr ; Copy OSR to Y
pull block ; Read line width in bytes in OSR
wait 1 gpio VSYNC ; Wait for VSYNC to go low
wait 0 gpio VSYNC
HSYNC_LOOP:
mov x, osr ; Copy line width from OSR to X
wait 0 gpio HSYNC ; Wait for HSYNC to go high
wait 1 gpio HSYNC
PIXEL_LOOP:
wait 0 gpio PXCLK
wait 1 gpio PXCLK
in pins 7 ; Workaround for NANO noncontiguous data bus.
jmp pin SET_MSB
in null, 1
jmp DONE
SET_MSB:
in osr, 1 ; OSR always has an odd value (width - 1)
DONE:
jmp x-- PIXEL_LOOP ; Keep reading pixels while x != 0
jmp y-- HSYNC_LOOP ; Keep reading lines while y != 0
.wrap
.program dcmi_odd_byte
.wrap_target
pull block ; Read number of lines in OSR
mov y, osr ; Copy OSR to Y
pull block ; Read line width in bytes in OSR
wait 1 gpio VSYNC ; Wait for VSYNC to go low
wait 0 gpio VSYNC
HSYNC_LOOP:
mov x, osr ; Copy line width from OSR to X
wait 0 gpio HSYNC ; Wait for HSYNC to go high
wait 1 gpio HSYNC
PIXEL_LOOP:
wait 0 gpio PXCLK
wait 1 gpio PXCLK
in pins 7 ; Workaround for NANO noncontiguous data bus.
jmp pin SET_MSB
in null, 1
jmp DONE
SET_MSB:
in osr, 1 ; OSR always has an odd value (width - 1)
DONE:
wait 0 gpio PXCLK
wait 1 gpio PXCLK
jmp x-- PIXEL_LOOP ; Keep reading pixels while x != 0
jmp y-- HSYNC_LOOP ; Keep reading lines while y != 0
.wrap
% c-sdk {
int omv_csi_config(omv_csi_t *csi, omv_csi_config_t config) {
if (config == OMV_CSI_CONFIG_PIXFORMAT) {
uint offset;
pio_sm_config config;
pio_sm_set_enabled(OMV_CSI_PIO, OMV_CSI_SM, false);
pio_sm_clear_fifos(OMV_CSI_PIO, OMV_CSI_SM);
for(uint i=OMV_CSI_D0_PIN; i<OMV_CSI_D0_PIN+7; i++) {
pio_gpio_init(OMV_CSI_PIO, i);
}
pio_sm_set_consecutive_pindirs(OMV_CSI_PIO, OMV_CSI_SM, OMV_CSI_D0_PIN, 7, false);
if (csi->pixformat == PIXFORMAT_GRAYSCALE) {
offset = pio_add_program(OMV_CSI_PIO, &dcmi_odd_byte_program);
config = dcmi_odd_byte_program_get_default_config(offset);
} else {
offset = pio_add_program(OMV_CSI_PIO, &dcmi_default_program);
config = dcmi_default_program_get_default_config(offset);
}
sm_config_set_clkdiv(&config, 1);
sm_config_set_in_pins(&config, OMV_CSI_D0_PIN);
gpio_init(OMV_CSI_D7_PIN);
gpio_set_dir(OMV_CSI_D7_PIN, GPIO_IN);
sm_config_set_jmp_pin(&config, OMV_CSI_D7_PIN);
sm_config_set_in_shift(&config, true, true, 32);
pio_sm_init(OMV_CSI_PIO, OMV_CSI_SM, offset, &config);
pio_sm_set_enabled(OMV_CSI_PIO, OMV_CSI_SM, true);
}
return 0;
}
%}