Giter Club home page Giter Club logo

opi-mcp320x-dma-bitbanging's Introduction

High speed SPI DMA bit banging with Orange Pi Zero LTS and MCP3204/3208

This code contains a PoC demonstration of a software implementation of SPI protocol for communication with MCP3204/MCP3208 and similar ADC chips using direct access to the GPIO registers of Allwinner H2+ (as well as other CPUs, provided that correct register addresses are used: look them up in their respective datasheets).

On my particular instance of OPi I was able to achieve only about 23k samples/s using the SPI functions of the WiringOP library (there's too much overhead between the sampling cycles, so even setting the SPI clock frequency to 2 MHz doesn't help) and about 46k samples/s controlling the pins directly using its digitalRead() / digitalWrite() calls.

Direct register access via mmap()'ed /dev/mem turns out to be much more efficient, allowing to achieve clock frequency of about 2.8 MHz (at 1.368 GHz CPU clock) and the sampling rate of about 140 ksps, exceeding the MCP320x specifications, at which point data can still be read, but the values read at this out-of-spec sampling rate are obviously off.

This leaves plenty of room to decrease the OPi CPU clock frequency to save power, and the difference can be quite significant. I was able to set it to 648 MHz, at which point the OPi plus the ADC circuit are drawing about 1.5 W, at which point the sampling rate, with all the delays in the sampling loop removed, reaches just over 100 ksps, and the readings look good and no different from what they look at a lower sampling rate.

At full 1.368 GHz CPU clock the measured power consumption was about 2.6 W, therefore switching to 648 MHz means a 57% power draw reduction.

It will also work with the MCP3302/3304 13-bit ADC chip family, provided that the obvious necessary changes are added to read the extra sign bit and, if fully differential mode is used, to employ the necessary arithmetics to treat the result as a signed integer. See the respective datasheet.

Tested on Armbian/Linux. Not portable. Possibly not even compiler-portable, maybe not even across versions: some gcc-specific features are used, mainly related to disabling optimizations to prevent it from optimizing out the direct memory access instructions.

See the code for further details.

opi-mcp320x-dma-bitbanging's People

Contributors

shapirus avatar

Stargazers

 avatar

Watchers

 avatar

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.