cancel
Showing results for 
Search instead for 
Did you mean: 

How to write your own DAC driver in STM32?

RShre.2
Associate III

I was using HAL Driver for DAC, however, it turned out not to be suitable for my project because at high sampling frequency, using HAL_DAC_Setvalue() inside a timer interrupt caused problem. So i need to write my own driver for DAC. But I have no idea how to get started. Is there any material someone could link?

22 REPLIES 22

Which STM32?

> using HAL_DAC_Setvalue() inside a timer interrupt caused problem.

What problems?

What do you mean by "high sampling frequency"?

> Is there any material someone could link?

The reference manual to your STM32. Using DAC - after having configured it - is only one write to its data register.

JW

Peter BENSCH
ST Employee

Correct, you can also use LL instead of the HAL or control the DAC in bare metal. With the LL library you come quite close to bare metal, so that you only need to activate it: in STM32CubeMX > "project" > Project Manager > Advanced Settings --> click on HAL to the right of the peripheral DAC and change to LL.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

STM32 nucleo G071RB

i am trying to generate a sine waveform using DDS and i was using timer interrupt. The trig_frequency(sampling rate) when increased from 100KHz to higher, the output frequency had shift(decreased in value). The HAL_DAC_SetValue() requires more than 2us to execute while for sampling rate, for instance, 500 KHz = 2us and the code that I had written to assign LUT into a variable runtime > 2us so I need to not use HAL Driver for the DAC Setvalue. Something that doesn't take too much time to execute.

Or just switch on compiler optimization.

Although, generally, high-frequency interrupts are not a good idea.

Also, you start hitting DAC's electrical limitations, see DAC chapter in datasheet (tSETTLING, tW-to-W). Note, that if buffer is ON, DAC is not rail-to-rail.

JW

0693W00000aIfgoQAC.jpg

if you meant this, i couldn't find that option "Project Manager"

I assume it's not for G0 series.

Not in the STM32CubeIDE, but in its sub-function STM32CubeMX, which is also available as a stand-alone programme.

To start this, double-click the IOC on the left in the Project Explorer. In the CubeMX window that appears, the Project Manager is the third tab from the left, and in the left-hand column you will find the Advanced Settings.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Yes, but that is what my project requires. Unfortunately.

However, with 500kHz, i.e. 2µs, you come quite close to the maximum frequency of the DAC. As you can see from the data sheet, the DAC needs about 1.6-1.7µs settling time with the buffer switched on - OK, for a full 12bit jump. Anyway, that's a challenge, especially using an interrupt routine as @Community member​ noted.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.