arduino-libraries / Arduino_AdvancedAnalog

Advanced Analog Library
GNU Lesser General Public License v2.1
7 stars 5 forks source link

A0 and A1 not working in M4? #72

Closed mrubioroy closed 2 months ago

mrubioroy commented 3 months ago

When I run Arduino_AdvancedAnalog/examples/Advanced/ADC_Multi_Channel/ADC_Multi_Channel.ino in the M4 co-processor of my Portenta H7, it only works for channels A4 to A7. According to the documentation ADC1 should also work with A0 and A1. Why are A0 and A1 not working? They do work in the main M7 core.

I have used these codes: M7:

void setup() {
  bootM4();
  Serial.begin(9600);
}
void loop() {
  Serial.println("M7");
  digitalWrite(LEDG, LOW);
  delay(100);
  digitalWrite(LEDG, HIGH);
  delay(2000);
}

M4:

#include <Arduino_AdvancedAnalog.h>

AdvancedADC adc(A0); // not working with A0 or A1. It does work with A4 to A7
uint64_t last_millis = 0;

void setup() {
  Serial1.begin(9600);

  // Resolution, sample rate, number of samples per channel, queue depth.
  if (!adc.begin(AN_RESOLUTION_16, 16000, 32, 128)) {
      Serial1.println("Failed to start analog acquisition!");
      while (1);
  }
}

void loop() {
  if (adc.available()) {
      SampleBuffer buf = adc.read();
      // Process the buffer.
      if ((millis() - last_millis) > 20) {
        Serial1.println(buf[0]);   // Sample from first channel
        Serial1.println(buf[1]);   // Sample from second channel
        last_millis = millis();
      }
      // Release the buffer to return it to the pool.
      buf.release();
  }
}
iabdalkader commented 3 months ago

This might be related to this issue: https://github.com/arduino/ArduinoCore-mbed/issues/867

But how do you know if it works or not ? The M4 can't use serial to print message. The difference between A0..A3 and A4-A7, is that A0-A3 are dual-pad pins. The M4 may have an issue configuring or accessing them.

mrubioroy commented 3 months ago

Please note I use Serial1 for M4, which does work with output on UART1 pins.

When I use A0 or A1, I get the message “Failed to start analog acquisition!” on UART1. For A4 to A7, it prints the read values as expected. A2 and A3 don't work either, but that's the expected behavior for ADC1 according to the documentation.

iabdalkader commented 3 months ago

Ah I see. Well, as I've mentioned those pins happen to be dual-pads pins. That's the only difference, and I'm guessing but there might be an issue we've missed with using them from the M4. I'll give this a try next week.

iabdalkader commented 2 months ago

I tested this issue and I just can't reproduce it. I tried A0->A4, all seem to work fine, see the screenshot. Note the serial monitor is connect to a USB to serial port.

image

Sketch:

#include "RPC.h"
#ifdef CORE_CM7
void setup() {
  bootM4();
  Serial.begin(9600);
}
void loop() {
}
#else
#include <Arduino_AdvancedAnalog.h>

AdvancedADC adc(A0); // not working with A0 or A1. It does work with A4 to A7
uint64_t last_millis = 0;

void setup() {
  Serial1.begin(9600);

  // Resolution, sample rate, number of samples per channel, queue depth.
  if (!adc.begin(AN_RESOLUTION_16, 16000, 32, 128)) {
      Serial1.println("Failed to start analog acquisition!");
      while (1);
  }
}

void loop() {
  if (adc.available()) {
      SampleBuffer buf = adc.read();
      // Process the buffer.
      if ((millis() - last_millis) > 20) {
        Serial1.println(buf[0]);   // Sample from first channel
        Serial1.println(buf[1]);   // Sample from second channel
        last_millis = millis();
      }
      // Release the buffer to return it to the pool.
      buf.release();
  }
}
#endif
iabdalkader commented 2 months ago

Were you using the latest library or an older release ? You should be using 1.4.0.

mrubioroy commented 2 months ago

I'm using Advanced Analog 1.4.0 and board 4.1.1

I've uploaded your sketch to both cores and I still get the "Failed to start analog acquisition!" message on pin 14. I've even tried changing the #define DUAL_PIN according to commit 992ed2c but I still get the error message.

I'm using a Portenta H7 with no peripherals. Just USB-C for programming and Serial to USB on pin 14 (picture attached).

facchinm commented 2 months ago

@mrubioroy changing only the source files has no effect since the same setting should be applied to the precompiled library. Can you give a spin to core 4.1.3 (just released) that contains the full patch? Thx

iabdalkader commented 2 months ago

I tested this the first time with a Giga, which had a newer core. I tested it again with a Portenta, and the Portenta does fail with an older core (4.0.8) but works with the latest (4.1.3).

mrubioroy commented 2 months ago

Works now, thank you

If I may, what is a "dual pad" pin?

iabdalkader commented 2 months ago

If I may, what is a "dual pad" pin?

Special pins that connect to two pads, or used separately as a GPIO pad and an ADC input.