Giter Club home page Giter Club logo

dashcam-rs's Introduction

dashcam-rs

Overview

This project is a prototype for a car dashboard camera, created to learn more about the Rust programming language and the embedded Rust ecosystem. The prototype implements the basic functionality needed by a car dash cam:

  • Capture live video, with a display for preview.
  • Buffer past video, ideally several minutes of it.
  • Save buffered video to non-volatile memory on user intervention. For example, after a car accident.

Demo

The STM32F746G Discovery Board is used for the hardware platform with an OV9655 CMOS camera attached. The demo uses QVGA resolution (320x240), 30 fps, and RGB565 color format.

The dash cam buffers as many past frames as possible in SDRAM. On the first button press, the past frames are saved to flash memory. There is a small (~8 second) delay, as write operations for this particular flash device must be done one page (256 bytes) at a time. On the second button press, the saved frames are read from flash into SDRAM and played continuously in a loop.

Embedded Rust

Conclusions

Overall, creating this prototype with Rust was much more enjoyable than similar projects I've done with C/C++. Here are the main takeaways:

  • Language: A lot of ink has been spilled about the benefits of the borrow checker and the ownership model in Rust, but what I enjoyed the most about the language was more minor features like Option, Result<T, E>, closures, and match statements. They made the code cleaner and process of writing code more ergonomic.
  • Ecosystem: The abstractions built by the embedded Rust community for peripheral access (PACs) are hugely helpful. Being able to set or clear a single bit in a register with a series of zero-cost function calls is very useful in embedded software.
  • Tooling: The tooling (cargo, probe-rs, rust-analyzer, etc.) is miles ahead of other languages I've used for embedded. Being able to download dependencies, build code, flash the target, and open a debug window in a single command (cargo embed) that works out of the box is amazing!
  • Device support: In the end your choice to use embedded Rust for a project may come down to device support. The STM32 line is well supported by the community, but others may not be. Engagement from device manufacturers will be crucial in making Rust grow in embedded.

Community

This project relies heavily on a lot of great open-source software created by the embedded Rust community, including:

  • RTIC: A small concurrency framework for Cortex-M processors. Sort of like a mini-RTOS.
  • cortex-m and cortex-m-rt: Low-level device support for Cortex-M processors.
  • stm32f7xx-hal: Hardware abstraction layer (peripheral drivers) for the STM32F7 part family.
  • embedded-hal: Hardware abstraction layer traits for common peripherals.
  • embedded-graphics: 2D graphics library for embedded devices.

Hopefully, this project adds the following contributions to the embedded Rust community:

  • OV9655 CMOS camera device driver
    • Abstract SCCB driver for camera setting configuration using embedded-hal.
    • Parallel interface driver for the STM32F7 using DCIM and DMA, including live video capture with ping-pong DMA.
  • QSPI flash driver
    • A HAL driver for the QSPI peripheral on the STM32F7. Supports indirect mode with polling or DMA.
    • Device driver for the MT25QL128ABA flash memory chip.
    • Upstreamed to stm32f7xx-hal.

Project Details

Hardware Architecture

The primary image data path is outlined in red.

Software Architecture

Limitations and Next Steps

Memory

The biggest limitation with this prototype is the amount of memory, both volatile and non-volatile. The SDRAM chip used for frame buffering is 8 MB and the QSPI flash chip for saving frames is 16 MB. As a result, it can only buffer several seconds of video. With QVGA resolution (320x240), RGB565 color format, and 30 fps, somewhere between 275 MB - 1.3 GB of RAM is needed to buffer a few minutes of video. For non-volatile memory (flash), probably at least 2x to 10x of the RAM size is desired to store multiple clips during a drive. To increase both memories, a new hardware platform is needed. Since the SDRAM chip is connected via a high-speed interface, a custom PCB would be needed.

There are a couple other ways to resolve this problem:

  • Reduce resolution and frame rate.
    • This is partially implemented with the qqvga feature. However that only allows around 7 seconds of video to be buffered and the resolution is very small (160x120).
    • The OV9655 only supports 15 or 30 fps, so manual downsampling may be required.
  • Change the buffering methodology.
    • Buffer only a few seconds, then write to flash/non-volatile memory continuously. Saving a video is simply updating metadata and files in non-volatile memory.
    • Writing to the current flash memory device is very slow, so a different one may be needed.
    • This would be more work for software and more application logic. Overall this solution is not as elegant as the current implementation.

Filesystem

Currently non-volatile memory is accessed using raw data and addresses. To support saving and organizing multiple video clips, a filesystem is most likely needed. A stable, mature embedded filesystem may not exist in the embedded Rust ecosystem, so this may be a case for migrating to an embedded Linux platform rather than bare-metal.

dashcam-rs's People

Contributors

bbrown1867 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

dashcam-rs's Issues

Speedup QSPI flash reads and writes

QSPI driver currently uses "polling indirect mode" for reads and writes. Since there is so much frame data (150KB per frame), it takes extremely long (on the order of minutes) to write the contents of SDRAM (~60 frames) to flash.

There are 2 obvious ways to speed this up:

  • Use DMA instead of polling.
  • Use quad mode (4-wire) instead of extended mode (1 wire).

Write frame buffer to non-volatile memory

Currently a push button press will stop live video capture and loop through the buffered frames (located in SDRAM), displaying them on the LCD screen. This is for debug purposes to demonstrate a successful push button capture and to view the contents of the frame buffer.

However, the intended functionality for the dash cam is to save the buffered video to non-volatile memory (NVM) on a push button press. There are two non-volatile memory options on this platform:

  • QSPI flash
  • SD card

One of these will need to be chosen, a HAL driver will need to be written, and then the HAL driver should be called from the push button handler to write from SDRAM to NVM.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.