mirror of https://github.com/ARMmbed/mbed-os.git
STM32: analag_out: Use dynamic handle in object rather than static
This allows a proper handling of multiple instances. Also this commit stores the channel in the HAL format so that it can be re-used more easily and call to HAL are straightforward.pull/4689/head
parent
e911d9867c
commit
cea590963c
|
@ -64,6 +64,7 @@ struct dac_s {
|
||||||
DACName dac;
|
DACName dac;
|
||||||
PinName pin;
|
PinName pin;
|
||||||
uint32_t channel;
|
uint32_t channel;
|
||||||
|
DAC_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
|
|
|
@ -64,6 +64,7 @@ struct dac_s {
|
||||||
DACName dac;
|
DACName dac;
|
||||||
PinName pin;
|
PinName pin;
|
||||||
uint32_t channel;
|
uint32_t channel;
|
||||||
|
DAC_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
|
|
|
@ -58,6 +58,7 @@ struct analogin_s {
|
||||||
ADCName adc;
|
ADCName adc;
|
||||||
PinName pin;
|
PinName pin;
|
||||||
uint32_t channel;
|
uint32_t channel;
|
||||||
|
DAC_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct dac_s {
|
struct dac_s {
|
||||||
|
|
|
@ -64,6 +64,7 @@ struct dac_s {
|
||||||
DACName dac;
|
DACName dac;
|
||||||
PinName pin;
|
PinName pin;
|
||||||
uint32_t channel;
|
uint32_t channel;
|
||||||
|
DAC_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct can_s {
|
struct can_s {
|
||||||
|
|
|
@ -64,6 +64,7 @@ struct dac_s {
|
||||||
DACName dac;
|
DACName dac;
|
||||||
PinName pin;
|
PinName pin;
|
||||||
uint32_t channel;
|
uint32_t channel;
|
||||||
|
DAC_HandleTypeDef handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
#if defined (DEVICE_CAN)
|
#if defined (DEVICE_CAN)
|
||||||
|
|
|
@ -38,8 +38,6 @@
|
||||||
#define DAC_RANGE (0xFFF) // 12 bits
|
#define DAC_RANGE (0xFFF) // 12 bits
|
||||||
#define DAC_NB_BITS (12)
|
#define DAC_NB_BITS (12)
|
||||||
|
|
||||||
static DAC_HandleTypeDef DacHandle;
|
|
||||||
|
|
||||||
// These variables are used for the "free" function
|
// These variables are used for the "free" function
|
||||||
static int pa4_used = 0;
|
static int pa4_used = 0;
|
||||||
static int pa5_used = 0;
|
static int pa5_used = 0;
|
||||||
|
@ -54,7 +52,20 @@ void analogout_init(dac_t *obj, PinName pin) {
|
||||||
// Get the pin function and assign the used channel to the object
|
// Get the pin function and assign the used channel to the object
|
||||||
uint32_t function = pinmap_function(pin, PinMap_DAC);
|
uint32_t function = pinmap_function(pin, PinMap_DAC);
|
||||||
MBED_ASSERT(function != (uint32_t)NC);
|
MBED_ASSERT(function != (uint32_t)NC);
|
||||||
obj->channel = STM_PIN_CHANNEL(function);
|
|
||||||
|
switch (STM_PIN_CHANNEL(function)) {
|
||||||
|
case 1:
|
||||||
|
obj->channel = DAC_CHANNEL_1;
|
||||||
|
break;
|
||||||
|
#if defined(DAC_CHANNEL_2)
|
||||||
|
case 2:
|
||||||
|
obj->channel = DAC_CHANNEL_2;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
|
default:
|
||||||
|
error("Unknown DAC channel");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Configure GPIO
|
// Configure GPIO
|
||||||
pinmap_pinout(pin, PinMap_DAC);
|
pinmap_pinout(pin, PinMap_DAC);
|
||||||
|
@ -73,26 +84,23 @@ void analogout_init(dac_t *obj, PinName pin) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Configure DAC
|
// Configure DAC
|
||||||
DacHandle.Instance = (DAC_TypeDef *)(obj->dac);
|
obj->handle.Instance = (DAC_TypeDef *)(obj->dac);
|
||||||
|
|
||||||
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
|
sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
|
||||||
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
|
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
|
||||||
|
|
||||||
|
|
||||||
if (pin == PA_4) {
|
if (pin == PA_4) {
|
||||||
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
|
|
||||||
pa4_used = 1;
|
pa4_used = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(DAC_CHANNEL_2)
|
#if defined(DAC_CHANNEL_2)
|
||||||
if (pin == PA_5) {
|
if (pin == PA_5) {
|
||||||
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2);
|
|
||||||
pa5_used = 1;
|
pa5_used = 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (pin == PA_6) {
|
HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel);
|
||||||
HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
|
|
||||||
}
|
|
||||||
|
|
||||||
analogout_write_u16(obj, 0);
|
analogout_write_u16(obj, 0);
|
||||||
}
|
}
|
||||||
|
@ -121,28 +129,12 @@ void analogout_free(dac_t *obj) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void dac_write(dac_t *obj, int value) {
|
static inline void dac_write(dac_t *obj, int value) {
|
||||||
if (obj->channel == 1) {
|
HAL_DAC_SetValue(&obj->handle, obj->channel, DAC_ALIGN_12B_R, (value & DAC_RANGE));
|
||||||
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
|
HAL_DAC_Start(&obj->handle, obj->channel);
|
||||||
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
|
|
||||||
}
|
|
||||||
#if defined(DAC_CHANNEL_2)
|
|
||||||
if (obj->channel == 2) {
|
|
||||||
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
|
|
||||||
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int dac_read(dac_t *obj) {
|
static inline int dac_read(dac_t *obj) {
|
||||||
if (obj->channel == 1) {
|
return (int)HAL_DAC_GetValue(&obj->handle, obj->channel);
|
||||||
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
|
|
||||||
}
|
|
||||||
#if defined(DAC_CHANNEL_2)
|
|
||||||
if (obj->channel == 2) {
|
|
||||||
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void analogout_write(dac_t *obj, float value) {
|
void analogout_write(dac_t *obj, float value) {
|
||||||
|
|
Loading…
Reference in New Issue