Page 1 of 2

DAC on STM32F072

Posted: Fri Jul 22, 2016 5:39 am
by webbbn
I'm trying to get DAC working on an STM32F072 board. Unfortunately the F0 appears to be the only STM32 that doesn't have an example for DAC!

I started with another F0 example (the USB-CDC example), which works great. Then I added the DAC code from the F3 example, which was pretty straight-forward, except I think I had to add the DAC Driver system settings in mcuconf.h. Unfortunately it's not working. The DAC appears to work, but it's not streaming.

You can find all the code here.

The relevant configuration bits are:

Code: Select all

/*
 * DAC driver system settings.
 */
#define STM32_DAC_DUAL_MODE                 FALSE
#define STM32_DAC_USE_DAC1_CH1              TRUE
#define STM32_DAC_USE_DAC1_CH2              FALSE
#define STM32_DAC_DAC1_CH1_IRQ_PRIORITY     10
#define STM32_DAC_DAC1_CH2_IRQ_PRIORITY     10
#define STM32_DAC_DAC1_CH1_DMA_PRIORITY     2
#define STM32_DAC_DAC1_CH2_DMA_PRIORITY     2
#define STM32_DAC_DAC1_CH1_DMA_STREAM       STM32_DMA_STREAM_ID(1, 3)
#define STM32_DAC_DAC1_CH2_DMA_STREAM       STM32_DMA_STREAM_ID(1, 3)

/*
 * GPT driver system settings.
 */
#define STM32_GPT_USE_TIM1                  TRUE
#define STM32_GPT_USE_TIM2                  FALSE
#define STM32_GPT_USE_TIM3                  FALSE
#define STM32_GPT_USE_TIM14                 FALSE
#define STM32_GPT_TIM1_IRQ_PRIORITY         2
#define STM32_GPT_TIM2_IRQ_PRIORITY         2
#define STM32_GPT_TIM3_IRQ_PRIORITY         2
#define STM32_GPT_TIM14_IRQ_PRIORITY        2

...

static const DACConfig dac1cfg1 = {
  .init         = 2047U,
  .datamode     = DAC_DHRM_12BIT_RIGHT
};

static const DACConversionGroup dacgrpcfg1 = {
  .num_channels = 1U,
  .end_cb       = end_cb1,
  .error_cb     = error_cb1,
  .trigger      = DAC_TRG(0)
};

/*
 * GPT1 configuration.
 */
static const GPTConfig gpt1cfg1 = {
  .frequency    = 1000000U,
  .callback     = NULL,
  .cr2          = TIM_CR2_MMS_1,    /* MMS = 010 = TRGO on Update Event.    */
  .dier         = 0U
};

...

  /*
   * Starting DAC1 driver, setting up the output pin as analog as suggested
   * by the Reference Manual.
   */
  palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
  dacStart(&DACD1, &dac1cfg1);

  /*
   * Starting GPT1 driver, it is used for triggering the DAC.
   */
  gptStart(&GPTD1, &gpt1cfg1);

  /*
   * Starting a continuous conversion.
   */
  dacStartConversion(&DACD1, &dacgrpcfg1, dac_buffer, DAC_BUFFER_SIZE);
  gptStartContinuous(&GPTD1, 2U);


I believe the DAC is working because I can change the init value and see a different output on the DAC, so I suspect it the GPT configuration. I also added a GPIO toggle in the end callback and I don't see that happening.

Re: DAC on STM32F072

Posted: Fri Jul 22, 2016 7:50 am
by Giovanni
hi,

You should enable assertions and state checker in chconf.h and see if something is caught, DMA channels or some other detail.

Giovanni

Re: DAC on STM32F072

Posted: Fri Jul 22, 2016 2:43 pm
by webbbn
Thanks for the suggestion, but those were already turned on.

Re: DAC on STM32F072

Posted: Fri Jul 22, 2016 3:07 pm
by Giovanni
You need to use TIM6 for DAC1, it is a dedicated timer. See the other DAC demos under ./testhal and the STM32 Reference Manual.

Giovanni

Re: DAC on STM32F072

Posted: Fri Jul 22, 2016 3:55 pm
by webbbn
I'm sure I tried that because the example was using TIM6, but maybe I switched TIMs for the wrong reason. I'll try changing it back this evening and see if that fixes it.

Re: DAC on STM32F072

Posted: Fri Jul 22, 2016 4:48 pm
by webbbn
I think I see the problem now. There was no definition of STM32_GPT_USE_TIM6 in the mcuconf.h file. This is probably because it doesn't exist on all F0 devices. In any case, adding that, setting it to TRUE and changing to TIM6 at least compiles. I expect it will work now, but I can't test it for a while.

Re: DAC on STM32F072

Posted: Fri Jul 22, 2016 5:25 pm
by Giovanni
Hi,

If there is DAC1 then there must be TIM6, the two are linked. It is a bug in the registry file probably. Taking note of this.

Giovanni

Re: DAC on STM32F072

Posted: Sat Jul 23, 2016 2:04 am
by webbbn
Just to close it out. Changing to TIM6 works like a champ.

Thanks!

Re: DAC on STM32F072

Posted: Sat Jul 23, 2016 7:42 pm
by RoccoMarco
Giovanni wrote:Hi,

If there is DAC1 then there must be TIM6, the two are linked. It is a bug in the registry file probably. Taking note of this.

Giovanni


Registry is correct i have just checked. It is just matter of creating a proper mcuconf.h

Re: DAC on STM32F072

Posted: Tue Jul 26, 2016 11:01 am
by Giovanni
So we need to update all mcuconf.h files for STM32F072, users are supposed to find everything in there already.

Giovanni