STM32L4xx_HAL_Driver  1.14.0
Peripheral Control functions

Peripheral Control functions. More...

Functions

uint32_t HAL_DAC_GetValue (DAC_HandleTypeDef *hdac, uint32_t Channel)
 Returns the last data output value of the selected DAC channel. More...
 
HAL_StatusTypeDef HAL_DAC_ConfigChannel (DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
 Configures the selected DAC channel. More...
 

Detailed Description

Peripheral Control functions.

  ==============================================================================
             ##### Peripheral Control functions #####
  ==============================================================================
    [..]  This section provides functions allowing to:
      (+) Configure channels.
      (+) Set the specified data holding register value for DAC channel.

Function Documentation

◆ HAL_DAC_ConfigChannel()

HAL_StatusTypeDef HAL_DAC_ConfigChannel ( DAC_HandleTypeDef hdac,
DAC_ChannelConfTypeDef sConfig,
uint32_t  Channel 
)

Configures the selected DAC channel.

Note
By calling this function, the high frequency interface mode (HFSEL bits) will be set. This parameter scope is the DAC instance. As the function is called for each channel, the DAC high frequency interface mode of
  • sConfig must be the same at each call. (or DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC self detect).
Parameters
hdacpointer to a DAC_HandleTypeDef structure that contains the configuration information for the specified DAC.
sConfigDAC configuration structure.
ChannelThe selected DAC channel. This parameter can be one of the following values:
  • DAC_CHANNEL_1: DAC Channel1 selected
  • DAC_CHANNEL_2: DAC Channel2 selected (Whenever present)
Return values
HALstatus

Definition at line 1199 of file stm32l4xx_hal_dac.c.

1200 {
1201  uint32_t tmpreg1;
1202  uint32_t tmpreg2;
1203  uint32_t tickstart = 0U;
1204 #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1205  uint32_t hclkfreq;
1206 #endif /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
1207 
1208  /* Check the DAC parameters */
1209 #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1210  assert_param(IS_DAC_HIGH_FREQUENCY_MODE(sConfig->DAC_HighFrequency));
1211 #endif /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
1212  assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
1213  assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
1214  assert_param(IS_DAC_CHIP_CONNECTION(sConfig->DAC_ConnectOnChipPeripheral));
1215  assert_param(IS_DAC_TRIMMING(sConfig->DAC_UserTrimming));
1216  if ((sConfig->DAC_UserTrimming) == DAC_TRIMMING_USER)
1217  {
1218  assert_param(IS_DAC_TRIMMINGVALUE(sConfig->DAC_TrimmingValue));
1219  }
1220  assert_param(IS_DAC_SAMPLEANDHOLD(sConfig->DAC_SampleAndHold));
1221  if ((sConfig->DAC_SampleAndHold) == DAC_SAMPLEANDHOLD_ENABLE)
1222  {
1223  assert_param(IS_DAC_SAMPLETIME(sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime));
1224  assert_param(IS_DAC_HOLDTIME(sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime));
1225  assert_param(IS_DAC_REFRESHTIME(sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime));
1226  }
1227  assert_param(IS_DAC_CHANNEL(Channel));
1228 
1229  /* Process locked */
1230  __HAL_LOCK(hdac);
1231 
1232  /* Change DAC state */
1233  hdac->State = HAL_DAC_STATE_BUSY;
1234 
1235  if (sConfig->DAC_SampleAndHold == DAC_SAMPLEANDHOLD_ENABLE)
1236  /* Sample on old configuration */
1237  {
1238  /* SampleTime */
1239  if (Channel == DAC_CHANNEL_1)
1240  {
1241  /* Get timeout */
1242  tickstart = HAL_GetTick();
1243 
1244  /* SHSR1 can be written when BWST1 is cleared */
1245  while (((hdac->Instance->SR) & DAC_SR_BWST1) != 0UL)
1246  {
1247  /* Check for the Timeout */
1248  if ((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
1249  {
1250  /* Update error code */
1251  SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
1252 
1253  /* Change the DMA state */
1254  hdac->State = HAL_DAC_STATE_TIMEOUT;
1255 
1256  return HAL_TIMEOUT;
1257  }
1258  }
1259  HAL_Delay(1);
1260  hdac->Instance->SHSR1 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
1261  }
1262 #if !defined (STM32L451xx) & !defined (STM32L452xx) & !defined (STM32L462xx)
1263  else /* Channel 2 */
1264  {
1265  /* SHSR2 can be written when BWST2 is cleared */
1266 
1267  while (((hdac->Instance->SR) & DAC_SR_BWST2) != 0UL)
1268  {
1269  /* Check for the Timeout */
1270  if ((HAL_GetTick() - tickstart) > TIMEOUT_DAC_CALIBCONFIG)
1271  {
1272  /* Update error code */
1273  SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_TIMEOUT);
1274 
1275  /* Change the DMA state */
1276  hdac->State = HAL_DAC_STATE_TIMEOUT;
1277 
1278  return HAL_TIMEOUT;
1279  }
1280  }
1281  HAL_Delay(1U);
1282  hdac->Instance->SHSR2 = sConfig->DAC_SampleAndHoldConfig.DAC_SampleTime;
1283  }
1284 #endif /* STM32L451xx STM32L452xx STM32L462xx */
1285 
1286  /* HoldTime */
1287  MODIFY_REG(hdac->Instance->SHHR, DAC_SHHR_THOLD1 << (Channel & 0x10UL), (sConfig->DAC_SampleAndHoldConfig.DAC_HoldTime) << (Channel & 0x10UL));
1288  /* RefreshTime */
1289  MODIFY_REG(hdac->Instance->SHRR, DAC_SHRR_TREFRESH1 << (Channel & 0x10UL), (sConfig->DAC_SampleAndHoldConfig.DAC_RefreshTime) << (Channel & 0x10UL));
1290  }
1291 
1292  if (sConfig->DAC_UserTrimming == DAC_TRIMMING_USER)
1293  /* USER TRIMMING */
1294  {
1295  /* Get the DAC CCR value */
1296  tmpreg1 = hdac->Instance->CCR;
1297  /* Clear trimming value */
1298  tmpreg1 &= ~(((uint32_t)(DAC_CCR_OTRIM1)) << (Channel & 0x10UL));
1299  /* Configure for the selected trimming offset */
1300  tmpreg2 = sConfig->DAC_TrimmingValue;
1301  /* Calculate CCR register value depending on DAC_Channel */
1302  tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
1303  /* Write to DAC CCR */
1304  hdac->Instance->CCR = tmpreg1;
1305  }
1306  /* else factory trimming is used (factory setting are available at reset)*/
1307  /* SW Nothing has nothing to do */
1308 
1309  /* Get the DAC MCR value */
1310  tmpreg1 = hdac->Instance->MCR;
1311  /* Clear DAC_MCR_MODEx bits */
1312  tmpreg1 &= ~(((uint32_t)(DAC_MCR_MODE1)) << (Channel & 0x10UL));
1313  /* Configure for the selected DAC channel: mode, buffer output & on chip peripheral connect */
1314  tmpreg2 = (sConfig->DAC_SampleAndHold | sConfig->DAC_OutputBuffer | sConfig->DAC_ConnectOnChipPeripheral);
1315  /* Calculate MCR register value depending on DAC_Channel */
1316  tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
1317  /* Write to DAC MCR */
1318  hdac->Instance->MCR = tmpreg1;
1319 
1320  /* DAC in normal operating mode hence clear DAC_CR_CENx bit */
1321  CLEAR_BIT(hdac->Instance->CR, DAC_CR_CEN1 << (Channel & 0x10UL));
1322 
1323  /* Get the DAC CR value */
1324  tmpreg1 = hdac->Instance->CR;
1325  /* Clear TENx, TSELx, WAVEx and MAMPx bits */
1326  tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1)) << (Channel & 0x10UL));
1327  /* Configure for the selected DAC channel: trigger */
1328  /* Set TSELx and TENx bits according to DAC_Trigger value */
1329  tmpreg2 = sConfig->DAC_Trigger;
1330  /* Calculate CR register value depending on DAC_Channel */
1331  tmpreg1 |= tmpreg2 << (Channel & 0x10UL);
1332 #if defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1333  if (DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_80MHZ == sConfig->DAC_HighFrequency)
1334  {
1335  tmpreg1 |= DAC_CR_HFSEL;
1336  }
1337  else
1338  {
1339  if (DAC_HIGH_FREQUENCY_INTERFACE_MODE_DISABLE == sConfig->DAC_HighFrequency)
1340  {
1341  tmpreg1 &= ~(DAC_CR_HFSEL);
1342  }
1343  else /* Automatic selection */
1344  {
1345  hclkfreq = HAL_RCC_GetHCLKFreq();
1346  if (hclkfreq > HFSEL_ENABLE_THRESHOLD_80MHZ)
1347  {
1348  /* High frequency enable when HCLK frequency higher than 80 */
1349  tmpreg1 |= DAC_CR_HFSEL;
1350  }
1351  else
1352  {
1353  /* High frequency disable when HCLK frequency higher than 80 */
1354  tmpreg1 &= ~(DAC_CR_HFSEL);
1355  }
1356  }
1357  }
1358 
1359 #endif /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
1360 
1361  /* Write to DAC CR */
1362  hdac->Instance->CR = tmpreg1;
1363  /* Disable wave generation */
1364  hdac->Instance->CR &= ~(DAC_CR_WAVE1 << (Channel & 0x10UL));
1365 
1366  /* Change DAC state */
1367  hdac->State = HAL_DAC_STATE_READY;
1368 
1369  /* Process unlocked */
1370  __HAL_UNLOCK(hdac);
1371 
1372  /* Return function status */
1373  return HAL_OK;
1374 }
void HAL_Delay(uint32_t Delay)
This function provides minimum delay (in milliseconds) based on variable incremented.
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
__HAL_UNLOCK(hrtc)
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
__HAL_LOCK(hrtc)
return HAL_OK
uint32_t HAL_RCC_GetHCLKFreq(void)
Return the HCLK frequency.
DAC_SampleAndHoldConfTypeDef DAC_SampleAndHoldConfig
MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL,(uint32_t) WakeUpClock)
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_DAC_GetValue()

uint32_t HAL_DAC_GetValue ( DAC_HandleTypeDef hdac,
uint32_t  Channel 
)

Returns the last data output value of the selected DAC channel.

Parameters
hdacpointer to a DAC_HandleTypeDef structure that contains the configuration information for the specified DAC.
ChannelThe selected DAC channel. This parameter can be one of the following values:
  • DAC_CHANNEL_1: DAC Channel1 selected
  • DAC_CHANNEL_2: DAC Channel2 selected
Return values
Theselected DAC channel data output value.

Definition at line 1154 of file stm32l4xx_hal_dac.c.

1155 {
1156  /* Check the parameters */
1157  assert_param(IS_DAC_CHANNEL(Channel));
1158 
1159  /* Returns the DAC channel data output register value */
1160 #if defined (STM32L451xx) || defined (STM32L452xx) || defined (STM32L462xx)
1161  /* Prevent unused argument(s) compilation warning if no assert_param check */
1162  UNUSED(Channel);
1163 
1164  return hdac->Instance->DOR1;
1165 #endif /* STM32L451xx STM32L452xx STM32L462xx */
1166 
1167 #if defined (STM32L431xx) || defined (STM32L432xx) || defined (STM32L433xx) || defined (STM32L442xx) || defined (STM32L443xx) || \
1168  defined (STM32L471xx) || defined (STM32L475xx) || defined (STM32L476xx) || defined (STM32L485xx) || defined (STM32L486xx) || defined (STM32L496xx) || defined (STM32L4A6xx) || \
1169  defined (STM32L4R5xx) || defined (STM32L4R7xx) || defined (STM32L4R9xx) || defined (STM32L4S5xx) || defined (STM32L4S7xx) || defined(STM32L4S9xx)
1170  if(Channel == DAC_CHANNEL_1)
1171  {
1172  return hdac->Instance->DOR1;
1173  }
1174  else
1175  {
1176  return hdac->Instance->DOR2;
1177  }
1178 #endif /* STM32L431xx STM32L432xx STM32L433xx STM32L442xx STM32L443xx */
1179  /* STM32L471xx STM32L475xx STM32L476xx STM32L485xx STM32L486xx STM32L496xx STM32L4A6xx */
1180  /* STM32L4R5xx STM32L4R7xx STM32L4R9xx STM32L4S5xx STM32L4S7xx STM32L4S9xx */
1181 }
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))