cancel
Showing results for 
Search instead for 
Did you mean: 

Can't write to DAC's Control Register from a C++ class.

PMach.3
Associate II

Hi,

I've been having some troubles configuring DAC channels and can't seem to find what the problem is.I have a C++ class that is intended to setup and control a DAC and I can't write on the registers to do it. My class is the following (dac_test_class.h):

#include "stm32l0xx_hal.h"
 
#ifndef INC_DAC_TEST_CLASS_H_
#define INC_DAC_TEST_CLASS_H_
 
#ifdef __cplusplus
extern "C" {
#endif
 
class Dac_test_class{
private:
	DAC_HandleTypeDef hdac;										//DAC handler
public:
	Dac_test_class(void);
	void init_dac(void);
	void set_dac(uint32_t dac_channel, uint32_t val);			//Set DAC val to output
 
};
 
#ifdef __cplusplus
}
#endif
 
#endif /* INC_DAC_TEST_CLASS_H_ */

So far, this is my dac_test_class.cpp:

#include "dac_test_class.h"
 
 
Dac_test_class::Dac_test_class(){
 
}
 
void Dac_test_class::init_dac(){
	hdac.Instance = DAC;
	HAL_DAC_Init(&hdac);
 
	DAC_ChannelConfTypeDef sConfig = {0};
 
	sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
	sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
	HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1);
}
 
void Dac_test_class::set_dac(uint32_t dac_channel, uint32_t val){
 
}

In my main.cpp, I start a new object and try to init the DAC:

Dac_test_class dac;
  dac.init_dac();

During debug, what most startles me is that, inside HAL_DAC_ConfigChannel function, it all seems to run well until the line:

hdac->Instance->CR = tmpreg1;

Where tmpreg1 is 2. Running this line does not result in writing 2 to hdac->Instance->CR.

In fact, I've tried to iniciate the DAC channels with the following command:

hdac->Instance->CR |= (EN_CH1 | EN_CH2);

But to no avail. I've also tried to use SET_BIT, but with the same result. If I do this process in the main.cpp file it works. However, I need to do this inside a class for code portability.

What might I be missing here?

Thanks in advance

1 REPLY 1
PMach.3
Associate II

Any clue?