portfolio/STM32 Video Surveillance

Demo Media

Overview

A video surveillance system built around the STM32F407 microcontroller. The project is designed for video recording with a digital camera, utilizing the STM32F4 Discovery development kit for video encoding and storage.

It implements intelligent memory space management, storing up to a configurable number of videos (default: 10). The OV2640 camera module outputs data to the STM32 microcontroller through the DCMI interface and DMA, with the system then encoding the data in MJPEG format using the LIBJPEG library.

Project Goal: I wanted to see whether it is possible to use STM32F407 Discovery board to encode any sort of video. Obviously, it had zero chances to outperform DSPs specifically designed for video encoding, but I was still curious about the possibilities. And my initial idea was to record 24 one hour long videos per day.

Key Features

  • Video Recording - 320x240 RIFF-AVI Motion JPEG format at up to 3 FPS
  • External Storage - Captured video stored on external USB pendrive
  • Memory Management - Configurable number of videos
  • Time-based Segmentation - RTC-controlled video recording timing
  • AVI File Generation - Proper AVI headers and finalization
  • External Trigger Support - Stop recording via external trigger
  • DMA Optimization - Efficient frame buffer management

Technical Specifications

Hardware Platform

  • STM32F4DISCOVERY dev kit
  • Waveshare OV2640 camera module
  • USB Host interface for storage

Interfaces & Protocols

  • DCMI - Digital Camera Interface with DMA
  • I2C1 - Camera module control
  • USB Host - External storage interface for USB storage
  • MCO2 - DCMI clock generation required for proper camera functioning
  • RTC - Real-Time Clock for timing

Video Specifications

  • 320x240 resolution
  • 3 FPS maximum frame rate bottleneck
  • RIFF-AVI container format
  • Motion JPEG encoding

Software Libraries

  • LIBJPEG - JPEG encoding library
  • FatFs - FAT filesystem support
  • LL Functions - Low-level peripheral control
  • STM32 HAL - Hardware Abstraction Layer for complex peripherals like USB

System Architecture

Initialization and Configuration

The driver initializes the OV2640 camera module and configures it for MJPEG image capture. It sets up the necessary structures and parameters for capturing and encoding JPEG images. The OV2640 camera module is configured to capture frames in MJPEG format, which is suitable for efficient video compression.

AVI File Header Generation

The avi.c driver generates the header for the AVI file format, including the RIFF chunk, AVI main header (avih), and stream header (strh), among others. Various parameters such as frames per second (FPS), video resolution, and compression settings are configured in the AVI file header.

Recording Process

The recording process is initiated by calling the start_output_mjpeg_avi() function. This function captures JPEG frames from the OV2640 camera module and writes them to the AVI file. Each frame is checked for valid JPEG headers before being written to the file. JPEG image binary should begin with 0xFFD8 and end with 0xFFD9. The recording continues until a stop condition is met, such as reaching a predefined duration or triggering an external event.

Stop and Finalization

When recording is complete, the stop_output_mjpeg_avi() function is called to finalize the AVI file. This function writes the 'idx1' chunk, updates the file size, frame count, and other header information. It closes the temporary index file used during recording and sets the recording status to AVI_CLOSED_OUT.

Technologies Used

  • STM32F407-DISC1 - Main processing board
  • Waveshare OV2640 camera module without FIFO - Image capture
  • DCMI (Digital Camera Interface) with DMA - Image capture interface
  • LIBJPEG library - JPEG encoding
  • FatFs library - FAT filesystem support
  • LL functions - Most peripheral initialization
  • STM32 HAL drivers - Complex peripherals
  • Real-Time Clock (RTC) - Timing video recordings
  • USB Host interface - External storage
  • I2C - Camera module control
  • RIFF-AVI Motion JPEG - Encoded video format

Challenges & Solutions

Hardware Integration

  • Configuring DCMI interface for stable capture
  • Implementing efficient DMA transfers from camera
  • Coordinating timing between capture, encoding, storage

Performance Optimization

  • Managing limited microcontroller resources
  • Optimizing for maximum achievable frame rate
  • Efficient DMA-based frame buffer management

File System Management

  • Generating valid AVI format with correct headers
  • JPEG frame validation (0xFFD8/0xFFD9 headers)

System Reliability

  • Proper camera module initialization
  • External trigger support for recording control

Hardware Configuration

C
// Note: Use short jumper wires for OV2640 (less than 7 cm)
PC8  -> DCMI_D2    (Data line 2)
PE1  -> DCMI_D3    (Data line 3)
PE4  -> DCMI_D4    (Data line 4)
PE5  -> DCMI_D6    (Data line 6)
PE6  -> DCMI_D7    (Data line 7)
PB6  -> DCMI_D5    (Data line 5)
PC6  -> DCMI_D0    (Data line 0)
PC7  -> DCMI_D1    (Data line 1)
PA4  -> DCMI_HSYNC (Horizontal sync)
PB7  -> DCMI_VSYNC (Vertical sync)
PA6  -> DCMI_PIXCLK(Pixel clock)

// I2C1 for Camera Control
PB8  -> SCL (Clock)
PB9  -> SDA (Data)

// MCO2 Clock Output
PC9  -> DCMI_XCLX (24MHz camera clock)

// USB Host (External Storage)
PA11 -> USB_OTG_FS_DM
PA12 -> USB_OTG_FS_DP 
PC0  -> GPIO_Output for Drive_VBUS_FS

// Used as example of EXTI for terminating ongoing video recording
PA0  -> EXTI0