Skip to content

First Release #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 162 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
162 commits
Select commit Hold shift + click to select a range
998f8bf
Add conversion from mp_obj_t to cv:Size
sfe-SparkFro May 21, 2025
c538c3d
Add morphology functions
sfe-SparkFro May 21, 2025
8560bcf
Add conversion from mp_obj_t to cv::Point and cv::Scalar
sfe-SparkFro May 21, 2025
164ef13
Ensure morphology functions have all optional arguments
sfe-SparkFro May 21, 2025
2f9b0f7
Add simple drawing functions
sfe-SparkFro May 21, 2025
6590714
Downgrade to OpenCV 4.11
sfe-SparkFro May 22, 2025
4cf886d
Remove min() and max()
sfe-SparkFro May 22, 2025
f4f776d
Add Canny and Hough functions
sfe-SparkFro May 28, 2025
c918dd5
Add blurring/filtering functions
sfe-SparkFro May 30, 2025
1948925
Add gradient filter functions
sfe-SparkFro May 30, 2025
da3452c
Add thresholding functions
sfe-SparkFro May 30, 2025
60cd368
Add template matching
sfe-SparkFro May 30, 2025
ec05574
Add connectedComponents
sfe-SparkFro May 30, 2025
48fcb8f
Initial imshow() implementation
sfe-SparkFro Jun 2, 2025
e1800f9
Add waitKey()
sfe-SparkFro Jun 3, 2025
4d0627e
Improve st7789_spi.py imshow()
sfe-SparkFro Jun 4, 2025
361fbbe
Add convertScaleAbs()
sfe-SparkFro Jun 4, 2025
7e30426
Improve/simplify/modularize ST7789 SPI driver
sfe-SparkFro Jun 4, 2025
56903a4
Make waitKey() get input from REPL
sfe-SparkFro Jun 4, 2025
be186c2
Update waitKey() to return integer instead of string character
sfe-SparkFro Jun 4, 2025
74c1832
Clean up Hello OpenCV example
sfe-SparkFro Jun 4, 2025
4202cb9
Rename Hello OpenCV example with `ex01_` prefix
sfe-SparkFro Jun 4, 2025
a2bf9f2
Fix for #13
sfe-SparkFro Jun 5, 2025
0830116
Merge pull request #7 from sparkfun/imshow
sfe-SparkFro Jun 5, 2025
1d27888
Fix RP2350 atomics not working with PSRAM
sfe-SparkFro Jun 6, 2025
98edf13
Update OpenCV submodule with generic embedded platform cmake files
sfe-SparkFro Jun 6, 2025
4354a73
Update ulab
sfe-SparkFro Jun 6, 2025
e2f0fc6
Update ST7789 SPI driver to use ndarray.ndim
sfe-SparkFro Jun 6, 2025
e98482a
Add imread()
sfe-SparkFro Jun 9, 2025
da558f4
Disable OPENJPEG in OpenCV
sfe-SparkFro Jun 9, 2025
2d9605c
Add imwrite()
sfe-SparkFro Jun 9, 2025
e2defe4
Move module globals into module headers
sfe-SparkFro Jun 9, 2025
a3f6cd5
Set Mat allocator in imread()
sfe-SparkFro Jun 9, 2025
50f29e5
Ensure image codecs are allocated in C heap
sfe-SparkFro Jun 10, 2025
52a290e
Set default Mat allocator to NumpyAllocator
sfe-SparkFro Jun 10, 2025
01931af
Move global initializers to new boot function
sfe-SparkFro Jun 10, 2025
37756bb
Don't set NumPy allocator as default
sfe-SparkFro Jun 10, 2025
918cb58
Change ST7789 SPI driver to require SPI bus instead of initializing it
sfe-SparkFro Jun 11, 2025
660668f
Add boot.py example to initialize display and mount SD card
sfe-SparkFro Jun 11, 2025
18e938d
Add Example 2 - imread() and _imwrite()
sfe-SparkFro Jun 11, 2025
3cca6b0
ST7789 SPI driver - Rename saveDcPin() to savePinModeAlt()
sfe-SparkFro Jun 11, 2025
7f7590b
Add prints to end of Example 1
sfe-SparkFro Jun 11, 2025
9599677
Comment typo and cleanup
sfe-SparkFro Jun 11, 2025
f3d8c11
Add note to Example 2 about large/small images
sfe-SparkFro Jun 11, 2025
8696ea7
Remove main.py
sfe-SparkFro Jun 11, 2025
8b98132
Add splash screen support to boot.py
sfe-SparkFro Jun 11, 2025
79fd3a1
Initial HM01B0 PIO implementation
sfe-SparkFro Jun 12, 2025
8c1abbd
Improve HM01B0 PIO sync
sfe-SparkFro Jun 13, 2025
2944b43
Add Example 3 - Camera
sfe-SparkFro Jun 13, 2025
ec2e3de
Merge pull request #18 from sparkfun/main
sfe-SparkFro Jun 16, 2025
dda0e32
Update build.yml
sfe-SparkFro Jun 16, 2025
8ea6bc4
Update build.yml
sfe-SparkFro Jun 16, 2025
4d38135
Update build.yml
sfe-SparkFro Jun 16, 2025
c190764
Update build.yml
sfe-SparkFro Jun 16, 2025
39629a2
Update build.yml
sfe-SparkFro Jun 16, 2025
d5346a3
Update build.yml
sfe-SparkFro Jun 16, 2025
7e04ea8
Update OpenCV Makefile to use -C instead of cd
sfe-SparkFro Jun 16, 2025
3471137
Update build.yml
sfe-SparkFro Jun 16, 2025
72e08e5
Update build.yml
sfe-SparkFro Jun 16, 2025
279b2d5
Update build.yml
sfe-SparkFro Jun 16, 2025
ab3e5a7
Update OpenCV with fixes for updated compiler
sfe-SparkFro Jun 16, 2025
1ba836f
Fix OpenCV uint typedef
sfe-SparkFro Jun 16, 2025
c7b47b6
Update build.yml
sfe-SparkFro Jun 16, 2025
175b13e
Update build.yml
sfe-SparkFro Jun 16, 2025
1941bce
Add OpenCV Makefile and embedded platform definitions
sfe-SparkFro Jun 17, 2025
ce8c444
Set workflow to build on push to development and PR to main
sfe-SparkFro Jun 17, 2025
7bdeb99
Fix typo in workflow
sfe-SparkFro Jun 17, 2025
c02bd1f
HM01B0 PIO driver: restart state machine in vsync handler
sfe-SparkFro Jun 17, 2025
f99b1c8
Change HM01B0 PIO driver to use 32-bit DMA transfers
sfe-SparkFro Jun 17, 2025
270f2ae
Update comment in savePinModeAlt()
sfe-SparkFro Jun 18, 2025
3b782a1
Restructure drivers
sfe-SparkFro Jun 19, 2025
fc54ace
HM01B0 PIO driver: shift data 24 bits in sm.put()
sfe-SparkFro Jun 19, 2025
199796d
Clean up display drivers
sfe-SparkFro Jun 19, 2025
e9e992e
Optimize savePinModeAlt()
sfe-SparkFro Jun 19, 2025
6150a9e
Change HM01B0 PIO driver to use DMA transfers
sfe-SparkFro Jun 20, 2025
e1dd199
Change HM01B0 PIO driver to use 32-bit transfers after initialization
sfe-SparkFro Jun 20, 2025
0e9857b
Add optional image argument to camera read()
sfe-SparkFro Jun 20, 2025
d4a137b
Fix ST7789 PIO driver swapping pairs of pixels
sfe-SparkFro Jun 20, 2025
da81330
Update waitKey() prompts in examples 2 and 3
sfe-SparkFro Jun 20, 2025
8144fe8
Fix ST7789 PIO driver pin modes
sfe-SparkFro Jun 20, 2025
09aa23c
Add helpful prints to example 2
sfe-SparkFro Jun 20, 2025
dfff6f2
Add more helpful error messages to boot.py SD card initialization
sfe-SparkFro Jun 20, 2025
9e3ec32
Tweak print in example 2
sfe-SparkFro Jun 20, 2025
e959eb6
Add ST7789 PIO driver to example boot.py
sfe-SparkFro Jun 20, 2025
1a4ee29
Fix build on Ubuntu 24.02 (missing return)
sfe-SparkFro Jun 26, 2025
a34c4e9
Remove duplicate installs from GitHub build workflow
sfe-SparkFro Jun 26, 2025
9bc2b20
Refactor display driver folder
sfe-SparkFro Jun 26, 2025
07fa663
Add initial OV5640 PIO driver (broken)
sfe-SparkFro Jun 26, 2025
bdb9fee
Remove base classes from display driver __init__.py
sfe-SparkFro Jun 26, 2025
184df97
Update boot.py with camera driver refactor
sfe-SparkFro Jun 26, 2025
ade4731
Fix RP2 PIO DVP interface
sfe-SparkFro Jun 26, 2025
81f7cb1
Clean up camera drivers a bit
sfe-SparkFro Jul 2, 2025
be7d810
Add comment about copying buffers being slow
sfe-SparkFro Jul 2, 2025
83376cf
Add findContours(), drawContours(), and moments()
sfe-SparkFro Jul 3, 2025
527a8a5
Add more contour functions
sfe-SparkFro Jul 8, 2025
3acc441
Implement HoughLinesP()
sfe-SparkFro Jul 8, 2025
2486762
Add connectedComponentsWithStats()
sfe-SparkFro Jul 8, 2025
3e57d35
Fix HM01B0 reset
sfe-SparkFro Jul 9, 2025
307ea7c
RP2 DVP PIO driver: Disable DMA in active()
sfe-SparkFro Jul 9, 2025
ca68e8c
Initial CST816 touch screen driver
sfe-SparkFro Jul 9, 2025
3ac97b8
Move manifest.py out of cv2_drivers
sfe-SparkFro Jul 10, 2025
90f87dc
Remove TODO comment for ULAB_MAX_DIMS
sfe-SparkFro Jul 10, 2025
a70c047
Remove dependency on OpenCV fork
sfe-SparkFro Jul 10, 2025
4f1acc6
Remove commented lines from cmake file
sfe-SparkFro Jul 10, 2025
d9501a7
Update to use large binary variant
sfe-SparkFro Jul 10, 2025
9edd995
Move hardware initialization out of boot.py
sfe-SparkFro Jul 10, 2025
808dd8d
Update comments in touch screen init example
sfe-SparkFro Jul 10, 2025
8cad950
Add initial touch screen example
sfe-SparkFro Jul 10, 2025
604874b
Renumber examples 2-4
sfe-SparkFro Jul 10, 2025
908ad71
Add SFE logo detection example
sfe-SparkFro Jul 11, 2025
8502bf8
Fix alphabetical order of imgproc module
sfe-SparkFro Jul 14, 2025
a548e37
Remove references to boot.py from examples
sfe-SparkFro Jul 14, 2025
2067566
Fix typo in comment
sfe-SparkFro Jul 14, 2025
0bcb62d
Change examples to import cv2 as cv
sfe-SparkFro Jul 14, 2025
17b0d53
Initial readme
sfe-SparkFro Jul 14, 2025
aa49d59
Remove tabs from Functions section in readme
sfe-SparkFro Jul 14, 2025
258e018
Fix hyperlinks in readme
sfe-SparkFro Jul 14, 2025
53d53ab
Update readme Quick Start info about hardware drivers
sfe-SparkFro Jul 14, 2025
8f16324
Add missing imgcodecs header in readme
sfe-SparkFro Jul 14, 2025
3ca9229
Fix threshrold to return tuple
sfe-SparkFro Jul 15, 2025
9e4649f
Add waitKeyEx()
sfe-SparkFro Jul 15, 2025
6a81d86
Fix ndarray_to_mat to copy if needed
sfe-SparkFro Jul 15, 2025
f37d860
Fix example 5
sfe-SparkFro Jul 15, 2025
3285909
Add header comments to examples
sfe-SparkFro Jul 15, 2025
ba60958
Improve logo detection example
sfe-SparkFro Jul 16, 2025
9a034f8
Add center and size text to logo detection example
sfe-SparkFro Jul 16, 2025
15ffc29
Add performance example
sfe-SparkFro Jul 17, 2025
08a7dbc
Update performance example with all changes this time
sfe-SparkFro Jul 17, 2025
e2872c2
Add XRP touch drive example
sfe-SparkFro Jul 18, 2025
471115f
Change default sm_id values of PIO drivers
sfe-SparkFro Jul 18, 2025
a2f461f
Move display splash screen code to base display class
sfe-SparkFro Jul 18, 2025
0df676a
Add optional splash image filename
sfe-SparkFro Jul 18, 2025
ad298cf
Update logo detection example to draw marker instead of circle for ce…
sfe-SparkFro Jul 21, 2025
dc112ba
Add XRP orange ring example
sfe-SparkFro Jul 21, 2025
9c875a0
Clean up touch screen example
sfe-SparkFro Jul 21, 2025
5728bc4
Add automatic builds that contain examples on release
malcolm-sparkfun Jul 22, 2025
5de809f
Add unmount of immutable directory and persistent tracking so expandi…
malcolm-sparkfun Jul 22, 2025
3439047
Fix XRP touch drive example
sfe-SparkFro Jul 22, 2025
3d65c02
Clean up examples and drivers
sfe-SparkFro Jul 22, 2025
10eed92
Add header comments to files in src
sfe-SparkFro Jul 24, 2025
72887c7
Add header comments to files in cv2_drivers
sfe-SparkFro Jul 24, 2025
94beeac
Add missing docstring comments in drivers
sfe-SparkFro Jul 24, 2025
0885da7
update names for frozen examples
malcolm-sparkfun Jul 24, 2025
fa2d97a
pedantic fixes
malcolm-sparkfun Jul 24, 2025
fe4cc0d
Use built in extraction of freezefs to simplify build script
malcolm-sparkfun Jul 24, 2025
95c72eb
Update content of PERSISTENT_FILE_FOR_UNPACK
sfe-SparkFro Jul 30, 2025
ae33125
Nitpicky fixes in examples
sfe-SparkFro Jul 30, 2025
68584d7
Add splash image and update image paths
sfe-SparkFro Jul 30, 2025
5423f37
Remove sparkfun_logo.png
sfe-SparkFro Jul 30, 2025
b03ca23
Nitpick print tweaks to SD card initialization
sfe-SparkFro Jul 30, 2025
66f61fd
Tweak XRP grab ring example to wait on user button pushes
sfe-SparkFro Jul 30, 2025
18c4039
Rename examaples folder to opencv-examples
sfe-SparkFro Jul 30, 2025
4fc3bbe
Update build.sh
sfe-SparkFro Jul 30, 2025
65cb21a
Merge branch 'features_for_launch' into feature/build_release
sfe-SparkFro Jul 30, 2025
c37d7b3
Change release workflow to rename firmware file
sfe-SparkFro Jul 30, 2025
b6b5958
build.sh: only copy frozen directory if different
sfe-SparkFro Jul 30, 2025
27bbff9
Add animation example
sfe-SparkFro Jul 30, 2025
d327ecd
Merge pull request #38 from sparkfun/feature/build_release
sfe-SparkFro Jul 30, 2025
8637030
Update README.md
sfe-SparkFro Jul 31, 2025
81f3be7
Fix XRP touch screen drive example
sfe-SparkFro Jul 31, 2025
afdf23b
Update performance section of README.md
sfe-SparkFro Jul 31, 2025
e502357
Remove non-OpenCV features
sfe-SparkFro Aug 1, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions .github/workflows/build.yml

This file was deleted.

13 changes: 5 additions & 8 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[submodule "src/opencv"]
path = src/opencv
url = https://github.com/sfe-SparkFro/opencv.git
[submodule "src/ulab"]
path = src/ulab
[submodule "opencv"]
path = opencv
url = https://github.com/opencv/opencv.git
[submodule "ulab"]
path = ulab
url = https://github.com/v923z/micropython-ulab.git
[submodule "micropython"]
path = micropython
url = https://github.com/sparkfun/micropython.git
24 changes: 10 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
# Set Pico SDK flags to create our own malloc wrapper and enable exceptions
CMAKE_ARGS += -DSKIP_PICO_MALLOC=1 -DPICO_CXX_ENABLE_EXCEPTIONS=1
ifndef PLATFORM
$(error PLATFORM not specified. Use 'make PLATFORM=rp2350' or similar.)
endif

# Get current directory
CURRENT_DIR = $(shell pwd)
TOOLCHAIN_FILE = platforms/${PLATFORM}.toolchain.cmake

# Set the MicroPython user C module path to the OpenCV module
MAKE_ARGS = USER_C_MODULES="$(CURRENT_DIR)/src/opencv_upy.cmake"
# TODO: For some reason, specifying this in the toolchain file doesn't work
CMAKE_ARGS += -DBUILD_LIST=core,imgproc,imgcodecs

# Build MicroPython with the OpenCV module
# Generic build
all:
@cd micropython/ports/rp2 && export CMAKE_ARGS="$(CMAKE_ARGS)" && make -f Makefile $(MAKEFLAGS) $(MAKE_ARGS)
cd opencv && mkdir -p build && cmake -S . -B build -DPICO_BUILD_DOCS=0 -DCMAKE_TOOLCHAIN_FILE=../${TOOLCHAIN_FILE} ${CMAKE_ARGS} && make -C build -f Makefile $(MAKEFLAGS) $(MAKE_ARGS)

# Clean the MicroPython build
# Clean the OpenCV build
clean:
@cd micropython/ports/rp2 && make -f Makefile $(MAKEFLAGS) clean

# Load the MicroPython submodules
submodules:
@cd micropython/ports/rp2 && make -f Makefile $(MAKEFLAGS) submodules
cd opencv && rm -rf build
293 changes: 292 additions & 1 deletion README.md

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion micropython
Submodule micropython deleted from 186caf
37 changes: 27 additions & 10 deletions src/opencv_upy.cmake → micropython_opencv.cmake
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
#-------------------------------------------------------------------------------
# SPDX-License-Identifier: MIT
#
# Copyright (c) 2025 SparkFun Electronics
#-------------------------------------------------------------------------------
# opencv_upy.cmake
#
# CMake file for the MicroPython port of OpenCV.
#-------------------------------------------------------------------------------

# Create an INTERFACE library for our CPP module.
add_library(usermod_cv2 INTERFACE)

# Add our source files to the library.
target_sources(usermod_cv2 INTERFACE
${CMAKE_CURRENT_LIST_DIR}/alloc.c
${CMAKE_CURRENT_LIST_DIR}/convert.cpp
${CMAKE_CURRENT_LIST_DIR}/core.cpp
${CMAKE_CURRENT_LIST_DIR}/imgproc.cpp
${CMAKE_CURRENT_LIST_DIR}/numpy.cpp
${CMAKE_CURRENT_LIST_DIR}/opencv_upy.c
${CMAKE_CURRENT_LIST_DIR}/src/alloc.c
${CMAKE_CURRENT_LIST_DIR}/src/convert.cpp
${CMAKE_CURRENT_LIST_DIR}/src/core.cpp
${CMAKE_CURRENT_LIST_DIR}/src/highgui.cpp
${CMAKE_CURRENT_LIST_DIR}/src/imgcodecs.cpp
${CMAKE_CURRENT_LIST_DIR}/src/imgproc.cpp
${CMAKE_CURRENT_LIST_DIR}/src/numpy.cpp
${CMAKE_CURRENT_LIST_DIR}/src/opencv_upy.c
)

# Add the src directory as an include directory.
Expand All @@ -21,16 +33,16 @@ target_link_libraries(usermod INTERFACE usermod_cv2)

# OpenCV creates some global variables on the heap. These get created before
# the GC is initialized, so we need to allocate some space for them on the C
# heap. 10kB seems sufficient. TODO: See if we can get away with less.
set(MICROPY_C_HEAP_SIZE 10240)
# heap. 64kB seems sufficient.
set(MICROPY_C_HEAP_SIZE 65536)

# Makes m_tracked_calloc() and m_tracked_free() available. These track pointers
# in a linked list to ensure the GC does not free them. Needed for some OpenCV
# functions
set(MICROPY_TRACKED_ALLOC 1)

# Set ULAB max number of dimensions to 4 (default is 2). TODO: See if 4 is
# actually needed, or if we can get away with 2.
# Set ULAB max number of dimensions to 4 (default is 2), which is needed for
# some OpenCV functions
target_compile_definitions(usermod INTERFACE ULAB_MAX_DIMS=4)

# Include ULAB
Expand All @@ -48,3 +60,8 @@ target_link_libraries(usermod INTERFACE "-Wl,--wrap,malloc")
target_link_libraries(usermod INTERFACE "-Wl,--wrap,free")
target_link_libraries(usermod INTERFACE "-Wl,--wrap,calloc")
target_link_libraries(usermod INTERFACE "-Wl,--wrap,realloc")

# __NEWLIB__ is not defined for some reason, which causes a conflicting
# definition of uint here:
# https://github.com/opencv/opencv/blob/9cdd525bc59b34a3db8f6db905216c5398ca93d6/modules/core/include/opencv2/core/hal/interface.h#L35-L39
target_compile_definitions(usermod INTERFACE -D__NEWLIB__)
1 change: 1 addition & 0 deletions opencv
Submodule opencv added at 31b0ee
46 changes: 46 additions & 0 deletions platforms/common.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Derived from:
# https://github.com/joachimBurket/esp32-opencv/blob/master/esp32/doc/detailed_build_procedure.md
set(CMAKE_BUILD_TYPE Release)
set(BUILD_SHARED_LIBS OFF)
set(CV_DISABLE_OPTIMIZATION OFF)
set(WITH_IPP OFF)
set(WITH_TBB OFF)
set(WITH_OPENMP OFF)
set(WITH_PTHREADS_PF OFF)
set(WITH_QUIRC OFF)
set(WITH_1394 OFF)
set(WITH_CUDA OFF)
set(WITH_OPENCL OFF)
set(WITH_OPENCLAMDFFT OFF)
set(WITH_OPENCLAMDBLAS OFF)
set(WITH_VA_INTEL OFF)
set(WITH_EIGEN OFF)
set(WITH_GSTREAMER OFF)
set(WITH_GTK OFF)
set(WITH_JASPER OFF)
set(WITH_JPEG OFF)
set(WITH_OPENJPEG OFF)
set(WITH_WEBP OFF)
set(BUILD_ZLIB ON)
set(BUILD_PNG ON)
set(WITH_TIFF OFF)
set(WITH_V4L OFF)
set(WITH_LAPACK OFF)
set(WITH_ITT OFF)
set(WITH_PROTOBUF OFF)
set(WITH_IMGCODEC_HDR OFF)
set(WITH_IMGCODEC_SUNRASTER OFF)
set(WITH_IMGCODEC_PXM OFF)
set(WITH_IMGCODEC_PFM OFF)
# TODO: For some reason, specifying this in the toolchain file doesn't work
# set(BUILD_LIST core,imgproc,imgcodecs)
set(BUILD_JAVA OFF)
set(BUILD_opencv_python OFF)
set(BUILD_opencv_java OFF)
set(BUILD_opencv_apps OFF)
set(BUILD_PACKAGE OFF)
set(BUILD_PERF_TESTS OFF)
set(BUILD_TESTS OFF)
set(CV_ENABLE_INTRINSICS OFF)
set(CV_TRACE OFF)
set(OPENCV_ENABLE_MEMALIGN OFF)
20 changes: 20 additions & 0 deletions platforms/include/rp2350_unsafe_cv_xadd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef RP2350_UNSAFE_CV_XADD_H
#define RP2350_UNSAFE_CV_XADD_H

// Fix for https://github.com/raspberrypi/pico-sdk/issues/2505
// TLDR; OpenCV uses atomic operations for incrementing reference counters by
// default. However, the Pico SDK does not support atomic operations on data in
// PSRAM; attempting to do so just causes an infinite loop where the value is
// incremented forever. The workaround is to use a non-atomic operation by
// re-defining the `CV_XADD` macro. This is "unsafe" because it's not atomic,
// but it *should* be fine since we're only using one thread. Also see:
// https://github.com/opencv/opencv/blob/52bed3cd7890192700b2451e2713c340209ffd79/modules/core/include/opencv2/core/cvdef.h#L697-L723
static inline int unsafe_cv_xadd(int* addr, int delta)
{
int tmp = *addr;
*addr += delta;
return tmp;
}
#define CV_XADD(addr, delta) unsafe_cv_xadd(addr, delta)

#endif
79 changes: 79 additions & 0 deletions platforms/include/zephyr_stdint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2019 BayLibre SAS
*
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef ZEPHYR_INCLUDE_TOOLCHAIN_STDINT_H_
#define ZEPHYR_INCLUDE_TOOLCHAIN_STDINT_H_

/*
* Some gcc versions and/or configurations as found in the Zephyr SDK
* (questionably) define __INT32_TYPE__ and derivatives as a long int
* which makes the printf format checker to complain about long vs int
* mismatch when %u is given a uint32_t argument, and uint32_t pointers not
* being compatible with int pointers. Let's redefine them to follow
* common expectations and usage.
*/

#if __SIZEOF_INT__ != 4
#error "unexpected int width"
#endif

#undef __INT32_TYPE__
#undef __UINT32_TYPE__
#undef __INT_FAST32_TYPE__
#undef __UINT_FAST32_TYPE__
#undef __INT_LEAST32_TYPE__
#undef __UINT_LEAST32_TYPE__
#undef __INT64_TYPE__
#undef __UINT64_TYPE__
#undef __INT_FAST64_TYPE__
#undef __UINT_FAST64_TYPE__
#undef __INT_LEAST64_TYPE__
#undef __UINT_LEAST64_TYPE__

#define __INT32_TYPE__ int
#define __UINT32_TYPE__ unsigned int
#define __INT_FAST32_TYPE__ __INT32_TYPE__
#define __UINT_FAST32_TYPE__ __UINT32_TYPE__
#define __INT_LEAST32_TYPE__ __INT32_TYPE__
#define __UINT_LEAST32_TYPE__ __UINT32_TYPE__
#define __INT64_TYPE__ long long int
#define __UINT64_TYPE__ unsigned long long int
#define __INT_FAST64_TYPE__ __INT64_TYPE__
#define __UINT_FAST64_TYPE__ __UINT64_TYPE__
#define __INT_LEAST64_TYPE__ __INT64_TYPE__
#define __UINT_LEAST64_TYPE__ __UINT64_TYPE__

/*
* The confusion also exists with __INTPTR_TYPE__ which is either an int
* (even when __INT32_TYPE__ is a long int) or a long int. Let's redefine
* it to a long int to get some uniformity. Doing so also makes it compatible
* with LP64 (64-bit) targets where a long is always 64-bit wide.
*/

#if __SIZEOF_POINTER__ != __SIZEOF_LONG__
#error "unexpected size difference between pointers and long ints"
#endif

#undef __INTPTR_TYPE__
#undef __UINTPTR_TYPE__
#define __INTPTR_TYPE__ long int
#define __UINTPTR_TYPE__ long unsigned int

/*
* Re-define the INTN_C(value) integer constant expression macros to match the
* integer types re-defined above.
*/

#undef __INT32_C
#undef __UINT32_C
#undef __INT64_C
#undef __UINT64_C
#define __INT32_C(c) c
#define __UINT32_C(c) c ## U
#define __INT64_C(c) c ## LL
#define __UINT64_C(c) c ## ULL

#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_STDINT_H_ */
22 changes: 22 additions & 0 deletions platforms/rp2350.toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Define PICO_SDK_PATH in your environment before running this script
if(NOT DEFINED ENV{PICO_SDK_PATH})
message(FATAL_ERROR "PICO_SDK_PATH environment variable is not set. Please define it before running this script.")
endif()

# Include the RP2350 toolchain file
include("$ENV{PICO_SDK_PATH}/cmake/preload/toolchains/pico_arm_cortex_m33_gcc.cmake")

# Include the common embedded OpenCV settings
include("${CMAKE_CURRENT_LIST_DIR}/common.cmake")

# Set RP2350 specific settings
set(OPENCV_DISABLE_THREAD_SUPPORT ON)

# Fix for https://github.com/raspberrypi/pico-sdk/issues/2505
set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -DOPENCV_INCLUDE_PORT_FILE=\\\"${CMAKE_CURRENT_LIST_DIR}/include/rp2350_unsafe_cv_xadd.h\\\"")
set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} -DOPENCV_INCLUDE_PORT_FILE=\\\"${CMAKE_CURRENT_LIST_DIR}/include/rp2350_unsafe_cv_xadd.h\\\"")

# Fix for https://github.com/sparkfun/micropython-opencv/issues/31
# Source: https://docs.zephyrproject.org/4.0.0/doxygen/html/zephyr__stdint_8h_source.html
set(CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -imacros ${CMAKE_CURRENT_LIST_DIR}/include/zephyr_stdint.h")
set(CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT} -imacros ${CMAKE_CURRENT_LIST_DIR}/include/zephyr_stdint.h")
14 changes: 14 additions & 0 deletions src/alloc.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
/*
*------------------------------------------------------------------------------
* SPDX-License-Identifier: MIT
*
* Copyright (c) 2025 SparkFun Electronics
*------------------------------------------------------------------------------
* alloc.c
*
* Wrapper functions for malloc(), free(), calloc(), and realloc(). These ensure
* memory gets allocated on the C heap before the MicroPython garbage collector
* has been initialized, and and in the GC pool afterwards.
*------------------------------------------------------------------------------
*/

// C headers
#include "py/runtime.h"

Expand Down
Loading