DAC on STM32F072 Topic is solved

Report here problems in any of ChibiOS components. This forum is NOT for support.
webbbn
Posts: 12
Joined: Fri Jul 22, 2016 5:23 am
Been thanked: 3 times

DAC on STM32F072

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: DAC on STM32F072

Post 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
webbbn
Posts: 12
Joined: Fri Jul 22, 2016 5:23 am
Been thanked: 3 times

Re: DAC on STM32F072

Post by webbbn »

Thanks for the suggestion, but those were already turned on.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: DAC on STM32F072

Post 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
webbbn
Posts: 12
Joined: Fri Jul 22, 2016 5:23 am
Been thanked: 3 times

Re: DAC on STM32F072

Post 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.
webbbn
Posts: 12
Joined: Fri Jul 22, 2016 5:23 am
Been thanked: 3 times

Re: DAC on STM32F072

Post 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.
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: DAC on STM32F072

Post 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
webbbn
Posts: 12
Joined: Fri Jul 22, 2016 5:23 am
Been thanked: 3 times

Re: DAC on STM32F072

Post by webbbn »

Just to close it out. Changing to TIM6 works like a champ.

Thanks!
User avatar
RoccoMarco
Posts: 655
Joined: Wed Apr 24, 2013 4:11 pm
Has thanked: 83 times
Been thanked: 67 times

Re: DAC on STM32F072

Post 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
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: DAC on STM32F072

Post by Giovanni »

So we need to update all mcuconf.h files for STM32F072, users are supposed to find everything in there already.

Giovanni
Post Reply