diff --git a/src/omv/Makefile b/src/omv/Makefile index e1bc5e3b1..db19a2217 100644 --- a/src/omv/Makefile +++ b/src/omv/Makefile @@ -20,6 +20,7 @@ SRCS += $(addprefix common/, \ ringbuf.c \ trace.c \ mutex.c \ + usbdbg.c \ ) SRCS += $(addprefix sensors/, \ diff --git a/src/omv/boards/OPENMV1/omv_boardconfig.h b/src/omv/boards/OPENMV1/omv_boardconfig.h index 4d1991cdc..702f9dc2e 100644 --- a/src/omv/boards/OPENMV1/omv_boardconfig.h +++ b/src/omv/boards/OPENMV1/omv_boardconfig.h @@ -30,6 +30,9 @@ // Core VBAT for selftests #define OMV_CORE_VBAT "3.3" +// USB IRQn. +#define OMV_USB_IRQN (OTG_FS_IRQn) + //PLL1 168MHz/48MHz #define OMV_OSC_PLL1M (12) #define OMV_OSC_PLL1N (336) diff --git a/src/omv/boards/OPENMV2/omv_boardconfig.h b/src/omv/boards/OPENMV2/omv_boardconfig.h index 694927e5f..1daa6fa85 100644 --- a/src/omv/boards/OPENMV2/omv_boardconfig.h +++ b/src/omv/boards/OPENMV2/omv_boardconfig.h @@ -75,6 +75,9 @@ // Core VBAT for selftests #define OMV_CORE_VBAT "3.3" +// USB IRQn. +#define OMV_USB_IRQN (OTG_FS_IRQn) + //PLL1 192MHz/48MHz #define OMV_OSC_PLL1M (12) #define OMV_OSC_PLL1N (384) diff --git a/src/omv/boards/OPENMV3/omv_boardconfig.h b/src/omv/boards/OPENMV3/omv_boardconfig.h index afcad5a31..b6b7e8706 100644 --- a/src/omv/boards/OPENMV3/omv_boardconfig.h +++ b/src/omv/boards/OPENMV3/omv_boardconfig.h @@ -75,6 +75,9 @@ // Core VBAT for selftests #define OMV_CORE_VBAT "3.3" +// USB IRQn. +#define OMV_USB_IRQN (OTG_FS_IRQn) + //PLL1 216MHz/48MHz #define OMV_OSC_PLL1M (12) #define OMV_OSC_PLL1N (432) diff --git a/src/omv/boards/OPENMV4/omv_boardconfig.h b/src/omv/boards/OPENMV4/omv_boardconfig.h index da4c5afe4..2f8674093 100644 --- a/src/omv/boards/OPENMV4/omv_boardconfig.h +++ b/src/omv/boards/OPENMV4/omv_boardconfig.h @@ -81,6 +81,9 @@ // Core VBAT for selftests #define OMV_CORE_VBAT "3.3" +// USB IRQn. +#define OMV_USB_IRQN (OTG_FS_IRQn) + //PLL1 480MHz/48MHz for USB, SDMMC and FDCAN #define OMV_OSC_PLL1M (3) #define OMV_OSC_PLL1N (240) diff --git a/src/omv/boards/OPENMV4P/omv_boardconfig.h b/src/omv/boards/OPENMV4P/omv_boardconfig.h index 93383092a..caa42795e 100644 --- a/src/omv/boards/OPENMV4P/omv_boardconfig.h +++ b/src/omv/boards/OPENMV4P/omv_boardconfig.h @@ -85,6 +85,9 @@ // Core VBAT for selftests #define OMV_CORE_VBAT "3.3" +// USB IRQn. +#define OMV_USB_IRQN (OTG_FS_IRQn) + //PLL1 48MHz for USB, SDMMC and FDCAN #define OMV_OSC_PLL1M (3) #define OMV_OSC_PLL1N (240) diff --git a/src/omv/boards/OPENMVPT/omv_boardconfig.h b/src/omv/boards/OPENMVPT/omv_boardconfig.h index b0c0ec2e2..54caf06fd 100644 --- a/src/omv/boards/OPENMVPT/omv_boardconfig.h +++ b/src/omv/boards/OPENMVPT/omv_boardconfig.h @@ -82,6 +82,9 @@ // Core VBAT for selftests #define OMV_CORE_VBAT "3.3" +// USB IRQn. +#define OMV_USB_IRQN (OTG_FS_IRQn) + // Clock Sources #define OMV_OSC_USB_CLKSOURCE RCC_USBCLKSOURCE_PLL #define OMV_OSC_RNG_CLKSOURCE RCC_RNGCLKSOURCE_HSI48 diff --git a/src/omv/boards/PORTENTA/omv_boardconfig.h b/src/omv/boards/PORTENTA/omv_boardconfig.h index e1f21731f..4abbbf35f 100644 --- a/src/omv/boards/PORTENTA/omv_boardconfig.h +++ b/src/omv/boards/PORTENTA/omv_boardconfig.h @@ -79,6 +79,9 @@ // Core VBAT for selftests #define OMV_CORE_VBAT "3.0" +// USB IRQn. +#define OMV_USB_IRQN (OTG_HS_IRQn) + // Defined for cpu frequency scaling to override the revid. #define OMV_MAX_CPU_FREQ (400) diff --git a/src/omv/ports/stm32/usbdbg.c b/src/omv/common/usbdbg.c similarity index 95% rename from src/omv/ports/stm32/usbdbg.c rename to src/omv/common/usbdbg.c index 201760fcf..1d3cdabb6 100644 --- a/src/omv/ports/stm32/usbdbg.c +++ b/src/omv/common/usbdbg.c @@ -21,20 +21,15 @@ #include "pendsv.h" #include "imlib.h" +#if MICROPY_PY_SENSOR #include "sensor.h" +#endif #include "framebuffer.h" #include "ff.h" -#include "usb.h" #include "usbdbg.h" #include "omv_boardconfig.h" #include "py_image.h" -#if MICROPY_HW_USB_HS -#define OTG_IRQn (OTG_HS_IRQn) -#else -#define OTG_IRQn (OTG_FS_IRQn) -#endif - static int xfer_bytes; static int xfer_length; static enum usbdbg_cmd cmd; @@ -43,10 +38,14 @@ static volatile bool script_ready; static volatile bool script_running; static vstr_t script_buf; static mp_obj_t mp_const_ide_interrupt = MP_OBJ_NULL; -extern const char *ffs_strerror(FRESULT res); + +// These functions must be implemented in MicroPython CDC driver. +extern uint32_t usb_cdc_buf_len(); +extern uint32_t usb_cdc_get_buf(uint8_t *buf, uint32_t len); void usbdbg_init() { + cmd = USBDBG_NONE; script_ready=false; script_running=false; vstr_init(&script_buf, 32); @@ -71,9 +70,9 @@ void usbdbg_set_script_running(bool running) inline void usbdbg_set_irq_enabled(bool enabled) { if (enabled) { - HAL_NVIC_EnableIRQ(OTG_IRQn); + NVIC_EnableIRQ(OMV_USB_IRQN); } else { - HAL_NVIC_DisableIRQ(OTG_IRQn); + NVIC_DisableIRQ(OMV_USB_IRQN); } __DSB(); __ISB(); } @@ -99,9 +98,11 @@ void usbdbg_data_in(void *buffer, int length) case USBDBG_SENSOR_ID: { int sensor_id = 0xFF; + #if MICROPY_PY_SENSOR if (sensor_is_detected() == true) { sensor_id = sensor_get_id(); } + #endif memcpy(buffer, &sensor_id, 4); cmd = USBDBG_NONE; break; @@ -244,10 +245,12 @@ void usbdbg_data_out(void *buffer, int length) py_image_descriptor_from_roi(&image, path, roi); #endif //IMLIB_ENABLE_IMAGE_IO && IMLIB_ENABLE_KEYPOINTS + cmd = USBDBG_NONE; break; } case USBDBG_ATTR_WRITE: { + #if MICROPY_PY_SENSOR /* write sensor attribute */ int32_t attr= *((int32_t*)buffer); int32_t val = *((int32_t*)buffer+1); @@ -267,6 +270,7 @@ void usbdbg_data_out(void *buffer, int length) default: break; } + #endif cmd = USBDBG_NONE; break; } @@ -322,8 +326,8 @@ void usbdbg_control(void *buffer, uint8_t request, uint32_t length) break; case USBDBG_SCRIPT_SAVE: - /* save running script */ - // TODO + // TODO: save running script + cmd = USBDBG_NONE; break; case USBDBG_SCRIPT_RUNNING: @@ -348,9 +352,11 @@ void usbdbg_control(void *buffer, uint8_t request, uint32_t length) break; case USBDBG_SYS_RESET_TO_BL:{ + #if defined(RTC_BASE) RTC_HandleTypeDef RTCHandle; RTCHandle.Instance = RTC; HAL_RTCEx_BKUPWrite(&RTCHandle, RTC_BKP_DR0, 0xDF59); + #endif NVIC_SystemReset(); break; } diff --git a/src/omv/ports/stm32/omv_portconfig.mk b/src/omv/ports/stm32/omv_portconfig.mk index a67cdf9b6..90a97c2ce 100644 --- a/src/omv/ports/stm32/omv_portconfig.mk +++ b/src/omv/ports/stm32/omv_portconfig.mk @@ -119,6 +119,7 @@ FIRM_OBJ += $(addprefix $(BUILD)/$(OMV_DIR)/common/, \ ringbuf.o \ trace.o \ mutex.o \ + usbdbg.o \ ) FIRM_OBJ += $(addprefix $(BUILD)/$(OMV_DIR)/sensors/, \