cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4DISCOVERY using DAC pins A4 & A5 without the codec

jean_prieur
Associate III
Posted on April 10, 2013 at 15:44

Hello,

I'm using a STM32F4DISCOVERY to program the firmware of my future board. I want to use the integrated 12 bit DAC of the board to generate analog simple signals on the A4 and A5 pins, but I don't want to use the external audio codec CS43L22 of the discovery board. On theSTM32F4DISCOVERY board A4 and A5 are connected to the LIS30 and the CS43L22 but I think it's OK to use these pins for generate analog signals. Am I right ? So I try to program with this code I found on a forum:


int
main(
void
)

{

GPIO_InitTypeDef GPIO_InitStructure;

DAC_InitTypeDef DAC_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;


/* Enable DAC and GPIOC clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC | RCC_APB1Periph_TIM2, ENABLE);


/* Configure PA.04/05 (DAC) as output -------------------------*/

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);



/* DAC channel1 Configuration */

DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;

DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;

DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;

DAC_Init(DAC_Channel_2, &DAC_InitStructure);


/* Enable DAC Channel2: Once the DAC channel2 is enabled, PA.05 is

automatically connected to the DAC converter. */

DAC_Cmd(DAC_Channel_2, ENABLE);


while
(1)

{

int
i;


for
(i=0; i<4096; i++)

DAC_SetChannel2Data(DAC_Align_12b_R, i);

}



/* does not exit - kind of important */


return
(1);

}

This code works on STM32F3xx but there is some problems with STM32F4xxcompactibility. Line 8, ''RCC_APB2Periph_AFIO'' and ''RCC_APB2Periph_GPIOA'' are not declared in librairies. Is anyone know what are the RCC_APB2Periph_AFIO andRCC_APB2Periph_GPIOA equivalent in STM32F4xx ? Thanks for your help ! note: I'm using the latest STM32F4xx librairies, but not thestm32f4_discovery librairy. #dac #stm32f407 #stm32f4discovery
15 REPLIES 15
Posted on April 10, 2013 at 16:15

There are parts of that which look

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/DispForm.aspx?ID=15242&RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/dac%20configuration

.

SYSCFG is some what equivalent to AFIO, but irrelevant in this context.

/* GPIOA Periph clock enable */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
Posted on April 10, 2013 at 16:27

Yes clive1, I think it comes from your code !

So I need to change:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);

With:

/* GPIOA Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

Now I have no compilation errors, but I have nothing on PA5 and PA4...
jean_prieur
Associate III
Posted on April 10, 2013 at 16:56

Sorry, in fact it's working fine !

I can generate analog signals between 0V and 3V!

The next step is to control a modular synth, I have to link an Opamp between PA5 and the modular synth.

I found this ref, I think it's the perfect choice: 

http://www.st.com/web/catalog/sense_power/FM123/SC61/SS1614/PF65462?s_searchtype=partnumber

jean_prieur
Associate III
Posted on April 11, 2013 at 17:05

I have a new problem with the DAC...

I just try to put the code in .c .h files but now the two DAC are not working anymore... This is my init_DAC.c code:

/* Use standard template */
#define USE_STDPERIPH_DRIVER
#define STM32F4XX
/* Includes */
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_dac.h''
#include ''stm32f4xx_tim.h''
void
DAC_Config(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* GPIOA Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* GPIOD Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Enable DAC and GPIOC clock */
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC | RCC_APB1Periph_TIM2, ENABLE);
/* Configure PA.04/05 (DAC) as output -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* DAC channel1 & DAC_Channel_2 Configuration */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* Enable DAC Channel1 & Channel2: Once the DAC is enabled, PA.05 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_2, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
}

In main.c I call this function...

DAC_Config();

...and I gererate analog levels:

DAC_SetChannel1Data(DAC_Align_12b_R, 4095);
DAC_SetChannel2Data(DAC_Align_12b_R, 2095);

But no outputs on PA3 and PA4. If I replace

DAC_Config();

by the corresponding code of DAC_Config() it's working ! Do you have any idea ? Thanks !
Posted on April 11, 2013 at 19:07

Per STM32F2xx_StdPeriph_Lib_V1.1.0\Project\STM32F2xx_StdPeriph_Examples\DAC\DAC_SignalsGeneration (applicable to F4 too)

/* DMA1 clock and GPIOA clock enable (to be used with DAC) */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE);
/* DAC Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
Posted on April 12, 2013 at 14:34

Thanks clive1, I think that improved my code, but I have still the same problem...

1) The DAC initialisation is in the file init_DAC.c that I call from main.c In this configuration the DAC outputs are not working, in fact, they are working 1 of ten times (other times the two outputs generates randomly 0V or 3V) 2) If I copy EXACTLY the code of the dac initialisation into main.c this is perfectly working ! This is init_DAC.c:

/* Includes */
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_dac.h''
void
DAC_Config(
void
)
{
GPIO_InitTypeDef GPIO_InitStructure_DAC;
DAC_InitTypeDef DAC_InitStructure;
/* GPIOA Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE);
/* Enable DAC clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
/* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */
GPIO_InitStructure_DAC.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure_DAC.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure_DAC.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure_DAC);
/* DAC channel1 & DAC_Channel_2 Configuration */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
/* Enable DAC Channel1 & Channel2: Once the DAC is enabled, PA.05 is
automatically connected to the DAC converter. */
DAC_Cmd(DAC_Channel_2, ENABLE);
DAC_Cmd(DAC_Channel_1, ENABLE);
}

This is main.c:

/* Includes */
#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_dac.h''
#include ''stm32f4xx_tim.h''
#include ''stm32f4xx_dma.h''
#include ''init_DAC.h''
int
main(
void
)
{
int
i;
DAC_Config();
while
(1)
{
for
(i=0; i<4095; i=i+5) DAC_SetChannel2Data(DAC_Align_12b_R, i);
for
(i=4095; i>0; i=i-8) DAC_SetChannel2Data(DAC_Align_12b_R, i);
DAC_SetChannel1Data(DAC_Align_12b_R, 4095);
}
return
(1);
}

Posted on April 12, 2013 at 16:00

Well that would be annoying, what tool chain are you using?

If I were perplexed I'd look at the assembler code generated by the two cases, and see if that shed any light on the situation.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jean_prieur
Associate III
Posted on April 12, 2013 at 16:46

I'm using the yagarto toolchain (with Coocox IDE)...

I forgot to mention that the behaviour is random when I reset the discoveryboard too (with the RST button), not only when I program the flash.

Posted on April 12, 2013 at 18:12

:020000040800F2
:10000000000400209D010008730300086F0300082E
:1000100071030008F5010008DD040008000000007D
:10002000000000000000000000000000AD03000818
:10003000C9020008000000007903000889040008D4
:10004000C3010008C3010008C3010008C301000880
:10005000C3010008C3010008CB020008C301000867
:10006000C3010008C3010008C3010008C301000860
:10007000C3010008C3010008C3010008C301000850
:10008000C3010008C3010008C3010008C301000840
:10009000C3010008C3010008C3010008C301000830
:1000A000C3010008C3010008C3010008C301000820
:1000B000C3010008C3010008C3010008C301000810
:1000C000C3010008C3010008C3010008C301000800
:1000D000C3010008C3010008C3010008C3010008F0
:1000E000C3010008C301000877030008C30100082A
:1000F000C3010008C3010008C3010008C3010008D0
:10010000C3010008C3010008C3010008C3010008BF
:10011000C3010008C3010008C3010008C3010008AF
:10012000C3010008C3010008C3010008C30100089F
:10013000C3010008C3010008C3010008C30100088F
:10014000C3010008C3010008C301000875030008CB
:10015000C3010008C3010008C3010008C30100086F
:10016000C3010008C3010008C3010008C30100085F
:10017000C3010008C3010008C3010008C30100084F
:10018000C3010008C3010008DFF80CD000F020F81C
:1001900000480047130500080004002009480168D2
:1001A00041F470010160084880470848004700009A
:1001B000FEE7FEE7FEE7FEE7FEE7FEE7FEE7FEE717
:1001C000FEE7FEE788ED00E08D04000889010008E5
:1001D000064C074D06E0E06840F0010394E8070094
:1001E00098471034AC42F6D3FFF7D2FF4C05000815
:1001F0005C050008FEE70000044A01238340106804
:1002000009B1184300E09843106070470074004043
:1002100010B5012186B0084600F0B0F8012114485D
:1002200000F0ACF80121480700F0B4F83020049049
:1002300003208DF8140000248DF8174004A90D4800
:1002400000F044F80094019469461020039400F0F3
:1002500013F86946002000F00FF801211020FFF785
:10026000CBFF01210020FFF7C7FF06B010BD000043
:10027000010020000000024030B5084C236840F621
:10028000FE7282409343D1E900252A43D1E902510D
:100290000D432A4382401A43226030BD007400405F
:1002A000034A08B5104408300090016008BD000002
:1002B00000740040034A08B51044143000900160F7
:1002C00008BD00000074004070477047F0B500237F
:1002D000012403270D6804FA03F21540954242D128
:1002E000D0F800C05E0007FA06F52CEA050CC0F84D
:1002F00000C091F804C0D0F800E00CFA06FC4CEA0B
:100300000E0CC0F800C091F804C0BCF1010F02D07F
:10031000BCF1020F1FD1D0F808C02CEA050CC0F8C0
:1003200008C091F805C0D0F808E00CFA06FC4CEAC9
:100330000E0CC0F808C0D0F804C02CEA020CC0F8BB
:1003400004C0426891F806C00CFA03FC1FFA8CFC4A
:1003500042EA0C024260C268AA43C260CA79C56818
:10036000B2402A43C2605B1C102BB3D3F0BDFEE742
:10037000FEE770477047704770470000044A002945
:10038000116801D0014300E0814311607047000013
:1003900030380240044A0029116801D0014300E0CE
:1003A00081431160704700004038024070470000F0
:1003B0000CB500222E4901920092086840F480306A
:1003C00008604FF4A063086800F4003000900198C2
:1003D000401C0190009810B901989842F3D1086828
:1003E0008303244841D501220092224A38321368FF
:1003F00043F080531360204A136843F48043136032
:1004000002680260026842F400420260026842F43C
:10041000A0520260031F194A1A600A6842F08072F3
:100420000A600A689201FCD5154B40F205621A6019
:10043000026822F003020260026842F002020260D7
:100440000268C2F38102022AFAD1026822F4000291
:100450000260084A0B487C321060086840F08060F7
:10046000086008680001FCD50CBD0092EDE70000B3
:10047000003802400838024000700040085440072D
:10048000003C024000300050704700000F4810B59B
:10049000016841F0010101600C4A0021083211603D
:1004A00002680B4B1A400260031D0A4A1A60026878
:1004B00022F48022026005480C300160FFF778FFCB
:1004C00005494FF00060086010BD00000038024090
:1004D000FFFFF6FE1030002408ED00E0FEE702E02A
:1004E00008C8121F08C1002AFAD1704770470020BF
:1004F00001E001C1121F002AFBD1704708B54FF47B
:10050000FA700090411E0091002801D00846F9E7DA
:1005100008BDFFF77DFE40F6FF750024A1B2002064
:10052000FFF7C8FEFFF7EAFF641DAC42F6DB2C467E
:10053000A1B20020FFF7BEFEFFF7E0FF083C002C51
:10054000F6DC29460020FFF7ABFEE6E75C05000875
:0C0550000000002000040000EE04000881
:040000050800018965
:00000001FF

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..