Tags:
create new tag
view all tags

A Custom MicroPython binary

Introduction

We have already seen how to create a custom MicroPython binary when we integrated all the modules needed for the WEB server. In case of the camera driver things are even a bit more tricky. The driver integrated into esp-idf is written in C and we must get access to its functions through MicroPython. We therefore need a MicroPython to C interface. A simple example on how to do this, can be found this chapter of the MicroPython manual.

Creating a new port in MicroPython

In addition to the diver itself we must enable the external psram needed to store the rather big image data. You can see how this is done by looking at the esp32 port of MicroPython. In the boards sub-directory you will find folders for different board configurations:

  • GENERIC
  • GENERIC_D2WD
  • GENERIC_SDRAM this is the one we are interested in
  • TINYPICO
In order not to mess up the original code I created a new port called esp32-cam by copying all files from ports/esp32 to ports/esp32-cam. In the boards folder I created the folder ESP32_CAMERA with the files
  • mpconfigboard.h with the following contents:
#define MICROPY_HW_BOARD_NAME "ESP32 module"
#define MICROPY_HW_MCU_NAME "ESP32"
#define MODULE_EXAMPLE_ENABLED (1)
#define MODULE_ESP32_CAMERA_ENABLED (1)  
  • and mpconfigboard.mk
SDKCONFIG += boards/sdkconfig.base
SDKCONFIG += boards/sdkconfig.spiram
SDKCONFIG += boards/sdkconfig.camera  

boards/sdconfig.camera contains the definitions needed for compilation of the camera driver: # ESP32-CAMERA
CONFIG_CAMERA_SUPPORT=y
CONFIG_OV2640_SUPPORT=y
#CONFIG_OV7725_SUPPORT=y  

Finally we must modify the port's Makefile to include compilation of the camera driver. I have attached the modified Makefile to this page:

https://afnog.iotworkshop.africa/pub/AFNOG/CustomNopMicropPython/Makefile

First we have to specify the additional include files needed:

ifeq ($(CONFIG_CAMERA_SUPPORT),y)
INC_ESPCOMP += -I$(ESPCOMP)/esp32-camera/driver/include
INC_ESPCOMP += -I$(ESPCOMP)/esp32-camera/driver/private_include
INC_ESPCOMP += -I$(ESPCOMP)/esp32-camera/conversions/include
INC_ESPCOMP += -I$(ESPCOMP)/esp32-camera/conversions/private_include
INC_ESPCOMP += -I$(ESPCOMP)/esp32-camera/sensors/private_include
endif

Then we must add the additional C sources to be compiled:

ifeq ($(CONFIG_CAMERA_SUPPORT),y)
ESPIDF_CAMERA_O = $(patsubst %.c,%.o,\
    $(wildcard $(ESPCOMP)/esp32-camera/driver/*.c) \
    $(wildcard $(ESPCOMP)/esp32-camera/conversions/*.c) \
    $(wildcard $(ESPCOMP)/esp32-camera/sensors/*.c) \
    )
endif

and finally we add the camera object files created this way.

ifeq ($(CONFIG_CAMERA_SUPPORT),y)
$(eval $(call gen_espidf_lib_rule,esp32_camera,$(ESPIDF_CAMERA_O)))
endif  

The camera driver is not a standard component in esp-idf and must be added there. Go to the component directory of your esp32 development environment esp-idf and clone the driver into this place. You will find it on https://github.com/espressif/esp32-camera/tree/master/driver

The MicroPython C module for the camera driver

I based my MicroPython C module on work done by shariltumin: https://github.com/shariltumin/esp32-cam-micropython but I had to extend it quite a bit to be able to access the other control parameters the driver offers and to be able to read back the setting. The extended C-module and Makefile are attached here:

https://afnog.iotworkshop.africa/pub/AFNOG/CustomNopMicropPython/modcamera.c

https://afnog.iotworkshop.africa/pub/AFNOG/CustomNopMicropPython/micropython.mk

These files are stored in the folder user_modules/esp32_camera at the same level in the files system as the MicroPython sources.

To compile the custom version of MicroPython you have to give the following commands, which I saved in a shell script:

#!/bin/sh
# build Micropython with the integrated esp32-camera driver
#
make BOARD=ESP32_CAMERA ESPIDF=/opt/ucc/afnog/afnog-2020/esp32-cam/esp-idf-micropython clean
make BOARD=ESP32_CAMERA ESPIDF=/opt/ucc/afnog/afnog-2020/esp32-cam/esp-idf-micropython USER_C_MODULES=../../../user_modules

ESPIDF= ... specifies where to find the esp-idf and the camera driver.

-- Uli Raich - 2020-01-25

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
Unknown file formatEXT Makefile r1 manage 37.7 K 2020-03-05 - 09:12 UliRaich  
Unknown file formatmk micropython.mk r1 manage 0.6 K 2020-03-05 - 09:38 UliRaich  
C source code filec modcamera.c r1 manage 27.0 K 2020-03-05 - 09:38 UliRaich  
Edit | Attach | Watch | Print version | History: r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r2 - 2020-03-05 - UliRaich
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback