adc_dev set as const, any reason why??

Cores are the underlying magic that make the Arduino API possible
bubulindo
Posts: 31
Joined: Fri Sep 11, 2015 10:51 am

adc_dev set as const, any reason why??

Postby bubulindo » Mon Sep 19, 2016 6:57 am

Hello,

First of all, sorry if this is not the correct place.

I read through the Pig-O-Scope wiki and GitHub and noticed that the ADC handling code was a bit "ugly" as there is little support for the ADC in the libmaple (short of providing the analogRead functionality).
I started off creating a library to use the ADC, but when it came time to include the ISR handlers I had to change the libmaple core as it doesn't have the handlers field in the adc_dev structure. This would be a minor change, but then I ran into problems over the fact that the adc_dev is defined as const.

Do any of you have a possible explanation as to why it is defined like this?

Also, once I complete and clean up the changes I'll push them forward for review and possibly addition to the cores...

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: adc_dev set as const, any reason why??

Postby RogerClark » Mon Sep 19, 2016 11:25 am

Its probably defined as const in an attempt to put the structure into flash instead of RAM, however I suspect it ends up in RAM as we've had a lot of problems with the compiler deciding that the PIN MAP etc need to be in RAM

bubulindo
Posts: 31
Joined: Fri Sep 11, 2015 10:51 am

Re: adc_dev set as const, any reason why??

Postby bubulindo » Mon Sep 19, 2016 1:35 pm

Ok...

Then it's fine to remove the const and add interrupt handling I guess. :)

User avatar
RogerClark
Posts: 5470
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: adc_dev set as const, any reason why??

Postby RogerClark » Mon Sep 19, 2016 8:27 pm

Try it and check if the sketch takes more RAM.

But you will also need to change all the functions that use it, as they will expect const data ( from the function signature)

bubulindo
Posts: 31
Joined: Fri Sep 11, 2015 10:51 am

Re: adc_dev set as const, any reason why??

Postby bubulindo » Tue Sep 20, 2016 7:32 am

I did the modification already... but I forgot to check the byte count previously.

The analogInput example comes in at 2.584 bytes of dynamic memory. Once I get to a proper internet connection, I'll re-download these files to test the size difference.

My idea is to have as much functionality of the ADC as possible by using easy functions instead of going bit by bit.
These are a couple of the examples I have running.

Code: Select all

/*
ADC acquisition with interrupt.
*/
#include <STM32ADC.h>

STM32ADC myADC(ADC1);

uint8 pin = D7;

#define BOARD_LED D33 //this is for Maple Mini
volatile static bool triggered = 0;
uint32 dado_adc = 0;

void int_func() {
  triggered = 1;
  dado_adc = myADC.getData();
  }

void setup() {
  Serial.begin(19200);
  pinMode(BOARD_LED, OUTPUT);
  pinMode(D32, INPUT);
  pinMode(D11, INPUT_ANALOG);//AD pin.
//startup blink... good idea from Pig-O-Scope
  digitalWrite(BOARD_LED, HIGH);
  delay(1000);
  digitalWrite(BOARD_LED, LOW);
  delay(1000);
  myADC.calibrate();
  myADC.setSampleRate(ADC_SMPR_1_5);
  myADC.attachInterrupt(int_func, ADC_EOC);
  myADC.setPins(&pin, 1); 
 
}

void loop() {
      if(digitalRead(D32) == 1 ) {
      Serial.println("begin");     
      // Take our samples
      myADC.startConversion();
      while (triggered == 0); //wait here... for testing purposes
     
      Serial.print("Readin: ");
      Serial.println(dado_adc);     
      }
}


Code: Select all

/*
  This example shows how to use the ADC library to continuously sample
  several channels/pins.
  The acquisition of the channels is done using DMA in circular mode.
 
*/
#include <STM32ADC.h>


STM32ADC myADC(ADC1);

#define BOARD_LED D33 //this is for Maple Mini

//Channels to be acquired.
uint8 pins[] = {11,10,9,8,7,6,5,4};


const int maxSamples = 8; // 8 channels

// Array for the ADC data
uint16_t dataPoints[maxSamples];

void setup() {
  Serial.begin(19200);
  pinMode(BOARD_LED, OUTPUT);
  pinMode(D32, INPUT);
//startup blink... good idea from Pig-O-Scope
  digitalWrite(BOARD_LED, HIGH);
  delay(1000);
  digitalWrite(BOARD_LED, LOW);
  delay(1000);

//calibrate ADC
  myADC.calibrate();

  // Set up our analog pin(s)
  for (unsigned int j = 0; j <8; j++)
    pinMode(pins[j], INPUT_ANALOG);
   
  myADC.setSampleRate(ADC_SMPR_1_5);//set the Sample Rate
  myADC.setScanMode();              //set the ADC in Scan mode.
  myADC.setPins(pins, 8);           //set how many and which pins to convert.
  myADC.setContinuous();            //set the ADC in continuous mode.

//set the DMA transfer for the ADC.
//in this case we want to increment the memory side and run it in circular mode
//By doing this, we can read the last value sampled from the channels by reading the dataPoints array
  myADC.setDMA(dataPoints, 8, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);

//start the conversion.
//because the ADC is set as continuous mode and in circular fashion, this can be done
//on setup(). 
  myADC.startConversion();   

 
}

void loop(){
//send the latest data acquired when the button is pushed.
      if(digitalRead(D32) == 1 ) {
      Serial.println("begin");
      // Take our samples

      for(unsigned int i = 0; i < maxSamples; i ++) {
        Serial.print("sample[");
        Serial.print(i);
        Serial.print("] = ");
        Serial.println(dataPoints[i]);
        }
      while(digitalRead(D32) == 1); //stay here. 
      }
}; //end loop


Yes, most of this is inspired on the Pig-O-Scope code.


Return to “Cores”

Who is online

Users browsing this forum: michael_l and 1 guest