Skip to content

pschatzmann/stm32f411-adc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Arduino STM32F411 Analog Input DMA Library

I wanted to use the ADC with DMA in Arduino with my STM32F411 Black Pill processor together with my Arduino Audio Tools!

stm32f411

Unfortunately STMDuino does not provide this functionality.

My first trials failed miserably using the DMA versions of the HAL API, so I decided to generate a working solution using the STM Cube IDE and then convert this to Arduino library, that provides the following functionality:

  • The DMA is used to transfer the data
  • Optionally we can use a timer to define the sampling rate
  • The API is using Callbacks to transfer the data or you can call the analogRead() instance method.
  • max 8 input channels
  • Only 16bit data is supported
  • We can normalize the data so that the avg is displayed as 0 (this is e.gl usefull for audio)
  • Please note that this functionality deactivates the standard implementation of analogRead()!

Pins for ADC

PINs FUNCTIONs
PA0 Channel0
PA1 Channel1
PA3 Channel2
PA4 Channel3
PA5 Channel4
PA6 Channel5
PA7 Channel6
PB0 Channel7

API

Below I demonstrate the basic API provided by this library.

Receiving Data via Callback

#include "AnalogReaderDMA.h"

const int sample_rate = 8000;
const int channels = 2;
const int buffer_size = 1024;
void writeData(int16_t *data, int sampleCount);
// DMA with timer and defined sample rate
AnalogReaderDMA adc(channels, TIM3, sample_rate, writeData, 1024);

// data callback
void writeData(int16_t *rec, int sampleCount){
}

void setup() {
  Serial.begin(115200);
  while(!Serial);

  adc.setCenterZero(true); // optional: we treat it as audio data
  adc.begin();  
}

void loop() {
}

Using Analog Read

The preferred way to read the data in continuous mode is by using analogRead();

#include "AnalogReaderDMA.h"

// define continuous mode adc w/o timer
const int channels = 2;
AnalogReaderDMA adc(channels);

void setup() {
  Serial.begin(115200);
  while(!Serial);

  adc.begin();  

}

void loop() {
  for (int j=0; j<channels; j++){
    Serial.print(adc.analogRead(j));
    Serial.print(" ");
  }
  Serial.println();
}

Documentation

Here is the link to the actual documentation.

You might also find further information in my Blogs

Installation in Arduino

You can download the library as zip and call include Library -> zip library. Or you can git clone this project into the Arduino libraries folder e.g. with

cd  ~/Documents/Arduino/libraries
git clone https://github.com/pschatzmann/stm32f411-adc

I recommend to use git because you can easily update to the latest version just by executing the git pull command in the project folder.

Copyright

Copyright © 2022 Phil Schatzmann

GNU General Public License

Copyright © 2015 STMicroelectronics

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of STMicroelectronics nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.