2020-07-21 12:20 PM
I'm trying to get a very simple example working with the DAC on the STM32L475VG in the B-L475E-IOT01A development board. I've generated the code below with STM32CubeMX for use in STM32CubeIDE. I can see the DAC register values changing, and LD1 blinks, but can't pick up a signal on any of the output pins A3-A5 (for the OPAMP amplified signal, or either DAC channels).
I imagine I've overlooked some configuration detail, but I can't figure out what! Any insight would be greatly appreciated.
Cheers,
Brett
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
DAC_HandleTypeDef hdac1;
OPAMP_HandleTypeDef hopamp1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DAC1_Init(void);
static void MX_OPAMP1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DAC1_Init();
MX_OPAMP1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
// initialize variables for the DACs
uint8_t triangle=0, rising=1, saw=0;
uint32_t data=0;
#define TRI_CHANNEL DAC_CHANNEL_1
#define SAW_CHANNEL DAC_CHANNEL_2 // we can observe the saw wave on LED1
// start the DACs
HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
HAL_DAC_Start(&hdac1, DAC_CHANNEL_2);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// PART 1
if (HAL_GPIO_ReadPin(B2_GPIO_Port, B2_Pin)) {
// if the pin value is true, then the button IS NOT pressed (pull-up by default)
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
} else {
// if the pin value is false, then the button IS pressed (button connects to GND)
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
} // if-else
// PART 2
// check if it's time for the triangle wave to decrease
if (triangle == UINT8_MAX+1 - 32)
rising = 0;
if (triangle == 0)
rising = 1;
// C2 freqeuency is 65 Hz, or with a period of 15 ms. We could
// divide into 16 steps as an approximation.
// update triangle and saw values
// triangle rises and falls, and must complete the cycle in 16 steps
if (rising)
triangle += 32;
else
triangle -= 32;
// saw rises and then overflows; a cycle must be completed in 16 steps
saw += 16;
// write a value for the triangle wave
HAL_DAC_SetValue(&hdac1, TRI_CHANNEL, DAC_ALIGN_8B_R, triangle);
data = HAL_DAC_GetValue(&hdac1, TRI_CHANNEL);
HAL_DAC_SetValue(&hdac1, SAW_CHANNEL, DAC_ALIGN_8B_R, saw);
data = HAL_DAC_GetValue(&hdac1, SAW_CHANNEL);
// wait for 1 ms
HAL_Delay(1);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = 0;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief DAC1 Initialization Function
* @param None
* @retval None
*/
static void MX_DAC1_Init(void)
{
/* USER CODE BEGIN DAC1_Init 0 */
/* USER CODE END DAC1_Init 0 */
DAC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN DAC1_Init 1 */
/* USER CODE END DAC1_Init 1 */
/** DAC Initialization
*/
hdac1.Instance = DAC1;
if (HAL_DAC_Init(&hdac1) != HAL_OK)
{
Error_Handler();
}
/** DAC channel OUT1 config
*/
sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/** DAC channel OUT2 config
*/
sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN DAC1_Init 2 */
/* USER CODE END DAC1_Init 2 */
}
/**
* @brief OPAMP1 Initialization Function
* @param None
* @retval None
*/
static void MX_OPAMP1_Init(void)
{
/* USER CODE BEGIN OPAMP1_Init 0 */
/* USER CODE END OPAMP1_Init 0 */
/* USER CODE BEGIN OPAMP1_Init 1 */
/* USER CODE END OPAMP1_Init 1 */
hopamp1.Instance = OPAMP1;
hopamp1.Init.PowerSupplyRange = OPAMP_POWERSUPPLY_LOW;
hopamp1.Init.Mode = OPAMP_PGA_MODE;
hopamp1.Init.NonInvertingInput = OPAMP_NONINVERTINGINPUT_DAC_CH;
hopamp1.Init.InvertingInput = OPAMP_INVERTINGINPUT_IO0;
hopamp1.Init.PgaGain = OPAMP_PGA_GAIN_2;
hopamp1.Init.PowerMode = OPAMP_POWERMODE_NORMAL;
hopamp1.Init.UserTrimming = OPAMP_TRIMMING_FACTORY;
if (HAL_OPAMP_Init(&hopamp1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN OPAMP1_Init 2 */
/* USER CODE END OPAMP1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : B2_Pin */
GPIO_InitStruct.Pin = B2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B2_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
}
Solved! Go to Solution.
2020-07-23 03:38 PM
I figured out the problem, and it was indeed something simple that I was missing, but you can't tell it from the code. The code was fine. I was just probing the wrong pins on the Arduino Uno interface, because apparently I don't read schematics carefully. Thank you!
2020-07-21 02:58 PM
Are you initializing clocks and pins within HAL_DAC_MspInit?
2020-07-21 04:15 PM
TDK,
I haven't changed HAL_DAC_MspInit(); here is the generated source. It does appear that the related clocks are initialized. Do I have to manually call this someplace? Or is it handled by HAL_DAC_Init()? There's no place in main that refers to this (or HAL_MspInit()).
Cheers,
Brett
void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hdac->Instance==DAC1)
{
/* USER CODE BEGIN DAC1_MspInit 0 */
/* USER CODE END DAC1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_DAC1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**DAC1 GPIO Configuration
PA4 ------> DAC1_OUT1
PA5 ------> DAC1_OUT2
*/
GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN DAC1_MspInit 1 */
/* USER CODE END DAC1_MspInit 1 */
}
}
2020-07-21 04:42 PM
HAL_DAC_MspInit seems fine. You don't need to call it separately, it's called from within HAL_DAC_Init.
Not real sure what the problem is. I haven't spent a lot of time with the L4 series, or the OPAMP. Might be something unique to one of those.
2020-07-21 11:36 PM
Read our and check/post all relevant DAC/OPAMP/GPIO/SYSCFG/any other relevant registers content.
How do you write to DAC? Did you try DAC without OPAMP?
JW
2020-07-23 03:38 PM
I figured out the problem, and it was indeed something simple that I was missing, but you can't tell it from the code. The code was fine. I was just probing the wrong pins on the Arduino Uno interface, because apparently I don't read schematics carefully. Thank you!