@@ -23,7 +23,8 @@ extern "C" {
2323
2424/* Private_Variables */
2525#if (defined(HAL_ADC_MODULE_ENABLED) && !defined(HAL_ADC_MODULE_ONLY)) ||\
26- (defined (HAL_DAC_MODULE_ENABLED) && !defined (HAL_DAC_MODULE_ONLY))
26+ (defined (HAL_DAC_MODULE_ENABLED) && !defined (HAL_DAC_MODULE_ONLY)) ||\
27+ (defined (USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1 ))
2728static PinName g_current_pin = NC;
2829#endif
2930
@@ -1174,8 +1175,146 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
11741175#endif /* HAL_ADC_MODULE_ENABLED */
11751176#endif /* !HAL_ADC_MODULE_ONLY */
11761177
1177- #if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY )
1178+ #if !defined(HAL_ADC_MODULE_ONLY )
11781179/* DAC */
1180+ #if defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1)
1181+ void dac_init (hal_dac_t instance, PinName pin)
1182+ {
1183+ /* DAC Periph clock enable */
1184+ #ifdef DAC1
1185+ if (instance == HAL_DAC1) {
1186+ HAL_RCC_DAC1_EnableClock ();
1187+ }
1188+ #endif
1189+
1190+ /* Configure DAC GPIO pin */
1191+ pinmap_pinout (pin, PinMap_DAC);
1192+ }
1193+
1194+ void dac_deinit (hal_dac_t instance, PinName pin)
1195+ {
1196+ /* DAC Periph clock disable */
1197+ #ifdef DAC1
1198+ if (instance == HAL_DAC1) {
1199+ HAL_RCC_DAC1_DisableClock ();
1200+ }
1201+ #endif
1202+ /* Deconfigure DAC GPIO pin */
1203+ pin_function (pin, STM_PIN_DATA (STM_MODE_ANALOG, LL_GPIO_PULL_NO, 0 ));
1204+ }
1205+
1206+ /* *
1207+ * @brief Return DAC HAL channel linked to a PinName
1208+ * @param pin: specific PinName's for ADC internal.
1209+ * @retval Valid HAL channel
1210+ */
1211+ hal_dac_channel_t get_dac_channel (PinName pin)
1212+ {
1213+ uint32_t function = pinmap_function (pin, PinMap_DAC);
1214+ hal_dac_channel_t channel;
1215+ switch (STM_PIN_CHANNEL (function)) {
1216+ case 1 :
1217+ channel = HAL_DAC_CHANNEL_1;
1218+ break ;
1219+ #if defined (DAC_NB_OF_CHANNEL) && (DAC_NB_OF_CHANNEL == 2)
1220+ case 2 :
1221+ channel = HAL_DAC_CHANNEL_2;
1222+ break ;
1223+ #endif
1224+ default :
1225+ _Error_Handler (" DAC: Unknown dac channel" , (int )(STM_PIN_CHANNEL (function)));
1226+ break ;
1227+ }
1228+ return channel;
1229+ }
1230+
1231+ /* *
1232+ * @brief This function will set the DAC to the required value
1233+ * @param port : the gpio port to use
1234+ * @param pin : the gpio pin to use
1235+ * @param value : the value to push on the adc output
1236+ * @param do_init : if set to true the initialization of the adc is done
1237+ * @retval None
1238+ */
1239+ void dac_write_value (PinName pin, uint32_t value, bool do_init)
1240+ {
1241+ hal_status_t status = HAL_OK;
1242+ hal_dac_handle_t dac_handle;
1243+ hal_dac_config_t dac_config;
1244+ hal_dac_channel_config_t dac_channel_config;
1245+ hal_dac_t instance = HAL_DAC1;
1246+ instance = (hal_dac_t )((uint32_t )pinmap_peripheral (pin, PinMap_DAC));
1247+ if (instance != NP) {
1248+ hal_dac_channel_t dacChannel = get_dac_channel (pin);
1249+ /* Initialization of DAC instance */
1250+ status = HAL_DAC_Init (&dac_handle, instance) ;
1251+ if ((do_init) && (status == HAL_OK)) {
1252+ /* ##-1- Configure the DAC peripheral #######################################*/
1253+ g_current_pin = pin;
1254+ dac_init (instance, pin);
1255+
1256+ dac_config.high_frequency_mode = HAL_DAC_HIGH_FREQ_MODE_DISABLED;
1257+ status = HAL_DAC_SetConfig (&dac_handle, &dac_config);
1258+ if (status == HAL_OK) {
1259+ /* Configuration of DAC channel */
1260+
1261+ dac_channel_config.alignment = HAL_DAC_DATA_ALIGN_8_BITS_RIGHT;
1262+ dac_channel_config.trigger = HAL_DAC_TRIGGER_NONE;
1263+ #if defined(DISABLE_DAC_OUTPUTBUFFER)
1264+ dac_channel_config.output_buffer = HAL_DAC_OUTPUT_BUFFER_DISABLED;
1265+ #else
1266+ dac_channel_config.output_buffer = HAL_DAC_OUTPUT_BUFFER_ENABLED;
1267+ #endif
1268+ dac_channel_config.output_connection = HAL_DAC_OUTPUT_CONNECTION_EXTERNAL;
1269+ dac_channel_config.data_sign_format = HAL_DAC_SIGN_FORMAT_UNSIGNED;
1270+ status = HAL_DAC_SetConfigChannel (&dac_handle, dacChannel, &dac_channel_config);
1271+ if (status == HAL_OK) {
1272+ /* The calibration allows a better output voltage precision */
1273+ status = HAL_DAC_CalibrateChannelBuffer (&dac_handle, dacChannel);
1274+ if (status == HAL_OK) {
1275+ /* Enable the DAC channel */
1276+ status = HAL_DAC_StartChannel (&dac_handle, dacChannel);
1277+ }
1278+ }
1279+ }
1280+ }
1281+ if (status == HAL_OK) {
1282+ /* Set the DAC channel data */
1283+ status = HAL_DAC_SetChannelData (&dac_handle, dacChannel, value);
1284+ }
1285+ }
1286+ }
1287+
1288+ /* *
1289+ * @brief This function will stop the DAC
1290+ * @param port : the gpio port to use
1291+ * @param pin : the gpio pin to use
1292+ * @retval None
1293+ */
1294+ void dac_stop (PinName pin)
1295+ {
1296+ hal_dac_handle_t dac_handle;
1297+ hal_dac_t instance = (hal_dac_t )((uint32_t )pinmap_peripheral (pin, PinMap_DAC));
1298+ if (instance != NP) {
1299+ /* Initialization of DAC instance */
1300+ hal_status_t status = HAL_DAC_Init (&dac_handle, instance) ;
1301+ if (status == HAL_OK) {
1302+ // hal_dac_channel_t dacChannel = get_dac_channel(pin);
1303+ #if defined (DAC_NB_OF_CHANNEL) && (DAC_NB_OF_CHANNEL == 2)
1304+ hal_dac_channel_t dacChannel = get_dac_channel (pin);
1305+ /* Stop the DAC channel */
1306+ (void )HAL_DAC_StopChannel (&dac_handle, dacChannel);
1307+ #else
1308+ (void )HAL_DAC_DeInit (&dac_handle);
1309+ #endif
1310+ }
1311+ dac_deinit (instance, pin);
1312+ }
1313+ }
1314+
1315+ #endif /* USE_HAL_DAC_MODULE && (USE_HAL_DAC_MODULE == 1) */
1316+ #if defined(HAL_DAC_MODULE_ENABLED)
1317+
11791318/* *
11801319 * @brief Return DAC HAL channel linked to a PinName
11811320 * @param pin: specific PinName's for ADC internal.
@@ -1265,10 +1404,10 @@ void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
12651404 * @param port : the gpio port to use
12661405 * @param pin : the gpio pin to use
12671406 * @param value : the value to push on the adc output
1268- * @param do_init : if set to 1 the initialization of the adc is done
1407+ * @param do_init : if set to true the initialization of the adc is done
12691408 * @retval None
12701409 */
1271- void dac_write_value (PinName pin, uint32_t value, uint8_t do_init)
1410+ void dac_write_value (PinName pin, uint32_t value, bool do_init)
12721411{
12731412 DAC_HandleTypeDef DacHandle = {};
12741413 DAC_ChannelConfTypeDef dacChannelConf = {};
@@ -1286,7 +1425,7 @@ void dac_write_value(PinName pin, uint32_t value, uint8_t do_init)
12861425#endif
12871426 return ;
12881427 }
1289- if (do_init == 1 ) {
1428+ if (do_init) {
12901429 /* ##-1- Configure the DAC peripheral #######################################*/
12911430 g_current_pin = pin;
12921431 if (HAL_DAC_Init (&DacHandle) != HAL_OK) {
@@ -1461,7 +1600,8 @@ void dac_stop(PinName pin)
14611600 return ;
14621601 }
14631602}
1464- #endif // HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
1603+ #endif // HAL_DAC_MODULE_ENABLED
1604+ #endif /* !HAL_DAC_MODULE_ONLY */
14651605
14661606#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
14671607/* PẄM */
0 commit comments