Skip to content

Commit 864b561

Browse files
committed
chore(dac): add HAL v2 support
Signed-off-by: Frederic Pillon <frederic.pillon@st.com>
1 parent a8047cc commit 864b561

4 files changed

Lines changed: 170 additions & 21 deletions

File tree

cores/arduino/wiring_analog.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ extern "C" {
2424
#endif
2525

2626
/* DAC/PWM */
27-
#if (defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)) ||\
27+
#if !defined(HAL_DAC_MODULE_ONLY) &&\
28+
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\
2829
(defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY))
2930
//This is the list of the IOs configured
3031
uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0};
@@ -200,15 +201,14 @@ void analogWriteFrequency(uint32_t freq)
200201
// For the other pins, default to digital output.
201202
void analogWrite(pin_size_t pinNumber, int value)
202203
{
203-
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
204-
uint8_t do_init = 0;
205-
#endif
206204
PinName p = digitalPinToPinName(pinNumber);
207205
if (p != NC) {
208-
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
206+
#if !defined(HAL_DAC_MODULE_ONLY) &&\
207+
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1)))
208+
bool do_init = false;
209209
if (pin_in_pinmap(p, PinMap_DAC)) {
210210
if (is_pin_configured(p, g_anOutputPinConfigured) == false) {
211-
do_init = 1;
211+
do_init = true;
212212
set_pin_configured(p, g_anOutputPinConfigured);
213213
}
214214
value = mapResolution(value, _writeResolution, DACC_RESOLUTION);
@@ -225,7 +225,7 @@ void analogWrite(pin_size_t pinNumber, int value)
225225
} else
226226
#endif /* HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY */
227227
{
228-
//DIGITAL PIN ONLY
228+
// DIGITAL PIN ONLY
229229
// Defaults to digital write
230230
pinMode(pinNumber, OUTPUT);
231231
value = mapResolution(value, _writeResolution, 8);

cores/arduino/wiring_digital.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ void pinMode(pin_size_t pinNumber, PinMode pinMode)
3232

3333
if (p != NC) {
3434
// If the pin that support PWM or DAC output, we need to turn it off
35-
#if (defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)) ||\
35+
#if !defined(HAL_DAC_MODULE_ONLY) &&\
36+
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\
3637
(defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY))
3738
if (is_pin_configured(p, g_anOutputPinConfigured)) {
38-
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
39+
#if !defined(HAL_DAC_MODULE_ONLY) &&\
40+
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1)))
3941
if (pin_in_pinmap(p, PinMap_DAC)) {
4042
dac_stop(p);
4143
} else
42-
#endif //HAL_DAC_MODULE_ENABLED && !HAL_DAC_MODULE_ONLY
44+
#endif
45+
{
4346
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
4447
if (pin_in_pinmap(p, PinMap_TIM)) {
4548
pwm_stop(p);
4649
}
4750
#endif //HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY
48-
{
49-
reset_pin_configured(p, g_anOutputPinConfigured);
5051
}
52+
/* Unconditionally reset the pin configuration */
53+
reset_pin_configured(p, g_anOutputPinConfigured);
5154
}
5255
#endif
5356
switch (pinMode) {

libraries/SrcWrapper/inc/analog.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,22 @@ hal_adc_channel_t get_adc_internal_channel(PinName pin);
6262
uint16_t adc_read_value(PinName pin, uint32_t resolution);
6363
#endif
6464
#endif /* !HAL_ADC_MODULE_ONLY */
65-
#if defined(HAL_DAC_MODULE_ENABLED) && !defined(HAL_DAC_MODULE_ONLY)
65+
#if !defined(HAL_DAC_MODULE_ONLY)
66+
#if defined(HAL_DAC_MODULE_ENABLED)
6667
uint32_t get_dac_channel(PinName pin);
67-
void dac_write_value(PinName pin, uint32_t value, uint8_t do_init);
68+
#endif
69+
#if defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1)
70+
hal_dac_channel_t get_dac_channel(PinName pin);
71+
#endif
72+
#if defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))
73+
void dac_write_value(PinName pin, uint32_t value, bool do_init);
6874
void dac_stop(PinName pin);
6975
#endif
76+
#endif /* !HAL_DAC_MODULE_ONLY */
7077
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
7178
void pwm_start(PinName pin, uint32_t clock_freq, uint32_t value, TimerCompareFormat_t resolution);
7279
void pwm_stop(PinName pin);
7380
#endif
74-
7581
#ifdef __cplusplus
7682
}
7783
#endif

libraries/SrcWrapper/src/stm32/analog.cpp

Lines changed: 146 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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))
2728
static 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

Comments
 (0)