Fast ADC and DAC on Arduino required

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
vlad
Posts: 12
Joined: Tue Mar 30, 2021 5:19 pm
Answers: 1

Fast ADC and DAC on Arduino required

Post by vlad »

Hi guys
Since my last post here (viewtopic.php?f=7&t=986) I’ve been working on a solution for the lack of memory issue that I need for program storage. Initially working with Daisy Seed (DS), I realized that the available 128 Kb of STM32H750IB processor wasn’t enough. Although I knew that the code can be stored in an external SD using QSPI, that isn’t an easy task to do. I mean, there are several issues to make things work, and there are no easy ways to do that. Even considering that Daisy Seed is a powerful board, I decided to migrate the project to DevEBOX (DB), a generic STM32H743VIT based board, with 1 Mb flash. But what in fact I didn’t know was that I was trying to escape from a pitfall just to fall into several others. To make things clear, I need a time interrupt function running at 44100 Hz and a fast ADC and DAC functions to be called inside it. This is perfectly achieved with Daisy Seed on Arduino IDE, using its own library. So, on DB I shouldn't expect anything different since their processors are almost the same. However, all the tools that worked with Daisy Seed didn’t work with DB, like callback and analog functions, essential to my sound processing project. Recently I found an example on net that shows a time interrupt function based on HardwareTimer, that worked as expected on DB. On the other side, the Arduino’s analogRead is ridiculously slow, taking 40% of processing time, and 3% of analogWrite. I wonder if someone may have some fast functions for ADC and DAC that suits to me (probably based on HAL).
Best regards.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Fast ADC and DAC on Arduino required

Post by fpiSTM »

You can refers to stm32cube example and adapt it ti Arduino. I've made the exercise in the example for F1 with analog watchdog example.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Fast ADC and DAC on Arduino required

Post by mrburnette »

Recently I found an example on net that shows a time interrupt function based on HardwareTimer, that worked as expected on DB. On the other side, the Arduino’s analogRead is ridiculously slow, taking 40% of processing time, and 3% of analogWrite.
Opinion and generalized observation:
Too many Arduino projects are Frankenstein projects: a part here, a library there, some stitching code to hold the parts together. Such programming is poor form and is prone to be disappointing. At best, one winds up with a working project but only mediocre performance. Often such projects cannot be evolved to the next revision level.

Arduino is a quick-prototyping environment and good code can be produced, but:

- avoid using libraries you do not understand fully (if you cannot reproduce it, you do not understand)*,
- avoid starting a project until a thorough analysis is undertaken from a "systems" approach,
- plan the next hardware step (select uC models with compatible growth paths),
- inventory your knowledge-set before programming and refresh stale concepts,
- assemble all PDF/paper documentation in one place.
* libraries are intended to make for quick implementation, not a component to replace programming knowledge!

Arduino is implemented by "code wrappers" around more professional code. Not always, but often performance is impacted.

The nasty secret is that Arduino was (and is) created as a teaching tool and "enabler: for non-programmers. As stated, Arduino and the underlying components can produce "good code: but for complex projects, Arduino will never produce great code.


Ray
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Fast ADC and DAC on Arduino required

Post by ag123 »

For maximum speed, you can try accessing the registers directly. e.g.

Code: Select all

// analog read
uint16_t value = ADC1->DR;
^ this is likely incorrect, check the ref manual. There are missing steps, you need to trigger the conversion, the code is not here. And you need to check that the value is valid (i.e. that it is the converted value), the code is also not here. But it is possibly as fast as you can get, short of dma.

There are also various 'levels' of API you could explore in addition to analogRead(), among which there is HAL, that one should work well, it is the 'official' api of the STM32Cube toolchain. it'd take some efforts to review the codes. it is also a good way to avoid referencing the registers directly, just in case you need to change a board.

'bare metal' programming seemed to be a lost art these days, 'everyone' uses 'libraries'. Well the registers are just there, just a single instruction access away. oh and the ref manual is there, STM32 is one of them i stick to because they are kind enough to share the detailed ref manuals. ;)
writing codes that work across the lines is very hard, sometimes impossible as the different 'generations' of socs has hugely different prowess in terms of hardware.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Fast ADC and DAC on Arduino required

Post by mrburnette »

ag123 wrote: Sat Oct 23, 2021 4:05 pm ...
'bare metal' programming seemed to be a lost art these days, 'everyone' uses 'libraries'. Well the registers are just there, just a single instruction access away. oh and the ref manual is there, STM32 is one of them i stick to because they are kind enough to share the detailed ref manuals. ;)
writing codes that work across the lines is very hard, sometimes impossible as the different 'generations' of socs has hugely different prowess in terms of hardware.
Arduino & bare-metal are on opposite ends of the "easy" yardstick! Unless you intend on answering bare-metal questions in this forum, I would suggest you wipe that thought from your mind. :lol:
But, a thread was started last year about that topic: viewtopic.php?t=615

Within the context of "STM32duino", Frederic has an ADC example for HAL: here.

Search in upper-right of page for digitalWriteFast for discussions on that topic within this forum.


Ray

PS: for those who are concerned about Arduino's performance, please abandon Arduino and move to STM's professional tools.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Fast ADC and DAC on Arduino required

Post by ag123 »

Well, I should probably suggest HAL, it is probably a most portable platform across the stm32 lines.
Bare metal has the downside of being 'locked in' to a particular mcu/soc. bare-metal programming is probably ok for a 'small' app.
But in a slightly larger app, changing the soc could mean having to update all the register references, this can be bad, a lot of work and error-prone.
vlad
Posts: 12
Joined: Tue Mar 30, 2021 5:19 pm
Answers: 1

Re: Fast ADC and DAC on Arduino required

Post by vlad »

mrburnette wrote: Sat Oct 23, 2021 12:18 pm
Recently I found an example on net that shows a time interrupt function based on HardwareTimer, that worked as expected on DB. On the other side, the Arduino’s analogRead is ridiculously slow, taking 40% of processing time, and 3% of analogWrite.
Opinion and generalized observation:
Too many Arduino projects are Frankenstein projects: a part here, a library there, some stitching code to hold the parts together. Such programming is poor form and is prone to be disappointing. At best, one winds up with a working project but only mediocre performance. Often such projects cannot be evolved to the next revision level.

Arduino is a quick-prototyping environment and good code can be produced, but:

- avoid using libraries you do not understand fully (if you cannot reproduce it, you do not understand)*,
- avoid starting a project until a thorough analysis is undertaken from a "systems" approach,
- plan the next hardware step (select uC models with compatible growth paths),
- inventory your knowledge-set before programming and refresh stale concepts,
- assemble all PDF/paper documentation in one place.
* libraries are intended to make for quick implementation, not a component to replace programming knowledge!

Arduino is implemented by "code wrappers" around more professional code. Not always, but often performance is impacted.

The nasty secret is that Arduino was (and is) created as a teaching tool and "enabler: for non-programmers. As stated, Arduino and the underlying components can produce "good code: but for complex projects, Arduino will never produce great code.


Ray
My gosh, you really scared me :). From your required list, I can accomplish only with the last one. Ok, I'm trying to do my best,
reading a lot and learning also a lot. Maybe my efforts aren't enough. One of my strong complains is the HAL manual. I think no one can
understand STM without knowing HAL deeply. But the manual doesn't explain how to tie the functions together, functions parameters
are bad explained and there is a total lack of useful examples. Anyway, I'm working on two front lines: one based on Arduino (were I faced
the problems I told in this post) and the other one on CubeIDE, from which I'm also having a lot of troubles (your 5 requirements to produce
a good code also applies). I'll post the CubeIDE problems I found later on.
vlad
Posts: 12
Joined: Tue Mar 30, 2021 5:19 pm
Answers: 1

Re: Fast ADC and DAC on Arduino required

Post by vlad »

In reply of mrburnette and ag123, I'd like to thank you all for your helpful hints. I'll examine them carefully. About the problems I found on CubeIDE, I just made a post on IDE forum: viewtopic.php?f=18&t=1342
Post Reply

Return to “General discussion”