arduino-libraries / Arduino_AdvancedAnalog

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

Add support for dual ADC mode. #65

Closed iabdalkader closed 6 months ago

iabdalkader commented 6 months ago

Fixes #54 Fixes #59

iabdalkader commented 6 months ago

@jmdodd95682 I rebased the PR and cleaned it up here, can you please give this a try ? There's an included example, but if you have a better one please send it.

Was there any reason we needed to limit the number of channels per ADC to 1 in dual mode ?

github-actions[bot] commented 6 months ago

Memory usage change @ 0b6f3fba5b7148b77e7ac1190637ccae4f182135

Board flash % RAM for global variables %
arduino:mbed_giga:giga :small_red_triangle: 0 - +128 0.0 - +0.01 0 - 0 0.0 - 0.0
Click for full report table Board|`examples/Advanced/ADC_Dual_Mode`
flash|%|`examples/Advanced/ADC_Dual_Mode`
RAM for global variables|%|`examples/Advanced/ADC_Multi`
flash|%|`examples/Advanced/ADC_Multi`
RAM for global variables|%|`examples/Advanced/ADC_Multi_Channel`
flash|%|`examples/Advanced/ADC_Multi_Channel`
RAM for global variables|%|`examples/Advanced/ADC_Multi_Channel_Dynamic`
flash|%|`examples/Advanced/ADC_Multi_Channel_Dynamic`
RAM for global variables|%|`examples/Advanced/ADC_Multi_To_DAC`
flash|%|`examples/Advanced/ADC_Multi_To_DAC`
RAM for global variables|%|`examples/Advanced/ADC_Serial_Plotter`
flash|%|`examples/Advanced/ADC_Serial_Plotter`
RAM for global variables|%|`examples/Advanced/ADC_To_DAC`
flash|%|`examples/Advanced/ADC_To_DAC`
RAM for global variables|%|`examples/Advanced/DAC_One_Channel`
flash|%|`examples/Advanced/DAC_One_Channel`
RAM for global variables|%|`examples/Advanced/DAC_Sine_wave`
flash|%|`examples/Advanced/DAC_Sine_wave`
RAM for global variables|%|`examples/Advanced/DAC_Two_Channels`
flash|%|`examples/Advanced/DAC_Two_Channels`
RAM for global variables|%|`examples/Advanced/I2S_DAC_Output`
flash|%|`examples/Advanced/I2S_DAC_Output`
RAM for global variables|%|`examples/Advanced/I2S_Full_Duplex`
flash|%|`examples/Advanced/I2S_Full_Duplex`
RAM for global variables|%|`examples/Beginner/Audio_Playback`
flash|%|`examples/Beginner/Audio_Playback`
RAM for global variables|%|`examples/Beginner/Audio_Playback_I2S`
flash|%|`examples/Beginner/Audio_Playback_I2S`
RAM for global variables|%|`examples/Beginner/Waveform_Generator`
flash|%|`examples/Beginner/Waveform_Generator`
RAM for global variables|% -|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|- `arduino:mbed_giga:giga`|N/A|N/A|N/A|N/A|128|0.01|0|0.0|64|0.0|0|0.0|64|0.0|0|0.0|64|0.0|0|0.0|128|0.01|0|0.0|128|0.01|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0
Click for full report CSV ``` Board,examples/Advanced/ADC_Dual_Mode
flash,%,examples/Advanced/ADC_Dual_Mode
RAM for global variables,%,examples/Advanced/ADC_Multi
flash,%,examples/Advanced/ADC_Multi
RAM for global variables,%,examples/Advanced/ADC_Multi_Channel
flash,%,examples/Advanced/ADC_Multi_Channel
RAM for global variables,%,examples/Advanced/ADC_Multi_Channel_Dynamic
flash,%,examples/Advanced/ADC_Multi_Channel_Dynamic
RAM for global variables,%,examples/Advanced/ADC_Multi_To_DAC
flash,%,examples/Advanced/ADC_Multi_To_DAC
RAM for global variables,%,examples/Advanced/ADC_Serial_Plotter
flash,%,examples/Advanced/ADC_Serial_Plotter
RAM for global variables,%,examples/Advanced/ADC_To_DAC
flash,%,examples/Advanced/ADC_To_DAC
RAM for global variables,%,examples/Advanced/DAC_One_Channel
flash,%,examples/Advanced/DAC_One_Channel
RAM for global variables,%,examples/Advanced/DAC_Sine_wave
flash,%,examples/Advanced/DAC_Sine_wave
RAM for global variables,%,examples/Advanced/DAC_Two_Channels
flash,%,examples/Advanced/DAC_Two_Channels
RAM for global variables,%,examples/Advanced/I2S_DAC_Output
flash,%,examples/Advanced/I2S_DAC_Output
RAM for global variables,%,examples/Advanced/I2S_Full_Duplex
flash,%,examples/Advanced/I2S_Full_Duplex
RAM for global variables,%,examples/Beginner/Audio_Playback
flash,%,examples/Beginner/Audio_Playback
RAM for global variables,%,examples/Beginner/Audio_Playback_I2S
flash,%,examples/Beginner/Audio_Playback_I2S
RAM for global variables,%,examples/Beginner/Waveform_Generator
flash,%,examples/Beginner/Waveform_Generator
RAM for global variables,% arduino:mbed_giga:giga,N/A,N/A,N/A,N/A,128,0.01,0,0.0,64,0.0,0,0.0,64,0.0,0,0.0,64,0.0,0,0.0,128,0.01,0,0.0,128,0.01,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0 ```
jmdodd95682 commented 6 months ago

I limited ADC1 to a single input pin, and the remaining pins are assigned to ADC2. The reason I did this was that since I'm only passing a single array of pin numbers to the AdvancedADCDual.begin() I was not sure how else to split this up. If you want to add an additional array so that there is one array of pins per ADC, that would work too. I was trying to keep the complexity of usage down. You would also need two num_channels to denote the length of each array of pin numbers.

I only did it to limit the number of parameters being passed to the begin routine. Its easy change.

I guess you could split the array in half and assign half to one ADC and half to the other. But then the num_channels needs to be even, or the remainder is always assigned to one of the other ADC.

For example, num_channels=5. Assign num_channels>>1=2 of the pins to ADC1, and num_channels-(num_channels>>1)=3 to the other ADC.

iabdalkader commented 6 months ago

I don't think we need to do that, see the update API.

iabdalkader commented 6 months ago

We can't assign pins dynamically with this, but I don't think we need to support this for dual ADCs.

jmdodd95682 commented 6 months ago

Yes. You could make it a requirement that pins are assigned upfront to each instance of AdvancedADC prior to calling the AdvancedADCDual.begin(). This solves the problem as well. I'll need to make a few minor changes to my code because I am calling it incorrectly. I'll make the changes to my app and see if everything works.

iabdalkader commented 6 months ago

No problem, take your time. Note there's an example here, but if you have a more useful one please send it.

jmdodd95682 commented 6 months ago

I'm having a real problem with the refactored code. I'm finding it difficult to declare an instance of AdvancedADCDual as a member of another class. Since there is no blank constructor I have to declare it as a pointer of type AdvancedADCDual*. A snippet of my code is below.

class osc{
    private:

        uint16_t sample_period;
        uint8_t probes[MAX_NUM_PROBES];
        uint16_t num_samples;
        uint32_t sample_rate;
        bool    dual_mode;
        uint32_t timestamp;
        uint32_t sampleDelay;

    public:
        uint32_t max_num_pins;
        AdvancedADC adc_input[2];
        AdvancedADCDual *adc_dual;

        osc(uint8_t *p,uint8_t ena, uint16_t ns,uint32_t sr) 
        {
            max_num_pins=MAX_NUM_PROBES;
            for(uint8_t i=0;i<MAX_NUM_PROBES;i++)
            {
                probes[i]=p[i];
                adc_input[i]=AdvancedADC(p[i]);
            }
            adc_dual=new AdvancedADCDual(adc_input[0],adc_input[1]);

It compiles with no errors. Calling the begin() also seems to work correctly as far as I can tell, but when I go to read() the captured data its like ADC is not actually running. I never receive any data back from the dma buffers.

I'm going to try running your example and see if that works for me.

jmdodd95682 commented 6 months ago

Your example works fine. When I globally declare the AdvancedADC instances and the AdvancedADCDual instance in a similar way my app also works fine as well. So, I must have some problem with how I'm declaring or using the AdvancedADCDual.

So, I would say the refactored code is fine. I've just got to get a little more clever about how I use the API.

iabdalkader commented 6 months ago

@leonardocavagnis updated.

github-actions[bot] commented 6 months ago

Memory usage change @ e89b1ede479e834abf8fe8ea344d7a3d51102186

Board flash % RAM for global variables %
arduino:mbed_giga:giga :small_red_triangle: 0 - +64 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table Board|`examples/Advanced/ADC_Dual_Mode`
flash|%|`examples/Advanced/ADC_Dual_Mode`
RAM for global variables|%|`examples/Advanced/ADC_Multi`
flash|%|`examples/Advanced/ADC_Multi`
RAM for global variables|%|`examples/Advanced/ADC_Multi_Channel`
flash|%|`examples/Advanced/ADC_Multi_Channel`
RAM for global variables|%|`examples/Advanced/ADC_Multi_Channel_Dynamic`
flash|%|`examples/Advanced/ADC_Multi_Channel_Dynamic`
RAM for global variables|%|`examples/Advanced/ADC_Multi_To_DAC`
flash|%|`examples/Advanced/ADC_Multi_To_DAC`
RAM for global variables|%|`examples/Advanced/ADC_Serial_Plotter`
flash|%|`examples/Advanced/ADC_Serial_Plotter`
RAM for global variables|%|`examples/Advanced/ADC_To_DAC`
flash|%|`examples/Advanced/ADC_To_DAC`
RAM for global variables|%|`examples/Advanced/DAC_One_Channel`
flash|%|`examples/Advanced/DAC_One_Channel`
RAM for global variables|%|`examples/Advanced/DAC_Sine_wave`
flash|%|`examples/Advanced/DAC_Sine_wave`
RAM for global variables|%|`examples/Advanced/DAC_Two_Channels`
flash|%|`examples/Advanced/DAC_Two_Channels`
RAM for global variables|%|`examples/Advanced/I2S_DAC_Output`
flash|%|`examples/Advanced/I2S_DAC_Output`
RAM for global variables|%|`examples/Advanced/I2S_Full_Duplex`
flash|%|`examples/Advanced/I2S_Full_Duplex`
RAM for global variables|%|`examples/Beginner/Audio_Playback`
flash|%|`examples/Beginner/Audio_Playback`
RAM for global variables|%|`examples/Beginner/Audio_Playback_I2S`
flash|%|`examples/Beginner/Audio_Playback_I2S`
RAM for global variables|%|`examples/Beginner/Waveform_Generator`
flash|%|`examples/Beginner/Waveform_Generator`
RAM for global variables|% -|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|-|- `arduino:mbed_giga:giga`|N/A|N/A|N/A|N/A|64|0.0|0|0.0|64|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|64|0.0|0|0.0|64|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0|0|0.0
Click for full report CSV ``` Board,examples/Advanced/ADC_Dual_Mode
flash,%,examples/Advanced/ADC_Dual_Mode
RAM for global variables,%,examples/Advanced/ADC_Multi
flash,%,examples/Advanced/ADC_Multi
RAM for global variables,%,examples/Advanced/ADC_Multi_Channel
flash,%,examples/Advanced/ADC_Multi_Channel
RAM for global variables,%,examples/Advanced/ADC_Multi_Channel_Dynamic
flash,%,examples/Advanced/ADC_Multi_Channel_Dynamic
RAM for global variables,%,examples/Advanced/ADC_Multi_To_DAC
flash,%,examples/Advanced/ADC_Multi_To_DAC
RAM for global variables,%,examples/Advanced/ADC_Serial_Plotter
flash,%,examples/Advanced/ADC_Serial_Plotter
RAM for global variables,%,examples/Advanced/ADC_To_DAC
flash,%,examples/Advanced/ADC_To_DAC
RAM for global variables,%,examples/Advanced/DAC_One_Channel
flash,%,examples/Advanced/DAC_One_Channel
RAM for global variables,%,examples/Advanced/DAC_Sine_wave
flash,%,examples/Advanced/DAC_Sine_wave
RAM for global variables,%,examples/Advanced/DAC_Two_Channels
flash,%,examples/Advanced/DAC_Two_Channels
RAM for global variables,%,examples/Advanced/I2S_DAC_Output
flash,%,examples/Advanced/I2S_DAC_Output
RAM for global variables,%,examples/Advanced/I2S_Full_Duplex
flash,%,examples/Advanced/I2S_Full_Duplex
RAM for global variables,%,examples/Beginner/Audio_Playback
flash,%,examples/Beginner/Audio_Playback
RAM for global variables,%,examples/Beginner/Audio_Playback_I2S
flash,%,examples/Beginner/Audio_Playback_I2S
RAM for global variables,%,examples/Beginner/Waveform_Generator
flash,%,examples/Beginner/Waveform_Generator
RAM for global variables,% arduino:mbed_giga:giga,N/A,N/A,N/A,N/A,64,0.0,0,0.0,64,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,64,0.0,0,0.0,64,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0 ```