diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h index 77ad0cc2f1..e81d727759 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h index 39afece523..e83cd368dc 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h index 39afece523..e83cd368dc 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F0/analogout_api.c b/targets/TARGET_STM/TARGET_STM32F0/analogout_device.c similarity index 61% rename from targets/TARGET_STM/TARGET_STM32F0/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32F0/analogout_device.c index f9f907b9f2..c8891f85d8 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32F0/analogout_device.c @@ -35,11 +35,6 @@ #include "mbed_error.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -static DAC_HandleTypeDef DacHandle; - void analogout_init(dac_t *obj, PinName pin) { DAC_ChannelConfTypeDef sConfig; @@ -50,7 +45,20 @@ void analogout_init(dac_t *obj, PinName pin) { // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_DAC); 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 pinmap_pinout(pin, PinMap_DAC); @@ -62,15 +70,16 @@ void analogout_init(dac_t *obj, PinName pin) { __HAL_RCC_DAC1_CLK_ENABLE(); // Configure DAC - DacHandle.Instance = (DAC_TypeDef *)(obj->dac); + obj->handle.Instance = (DAC_TypeDef *)(obj->dac); + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { + error("HAL_DAC_Init failed"); + } sConfig.DAC_Trigger = DAC_TRIGGER_NONE; - sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; + sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; - if (pin == PA_4) { - HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); - } else { // PA_5 - HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2); + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { + error("HAL_DAC_ConfigChannel failed"); } analogout_write_u16(obj, 0); @@ -86,53 +95,4 @@ void analogout_free(dac_t *obj) { pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } -static inline void dac_write(dac_t *obj, int value) { - if (obj->channel == 1) { - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - 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) { - if (obj->channel == 1) { - 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) { - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) { - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) { - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) { - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} - #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h index 9943c87ba8..c0e1bc1dd4 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F0/common_objects.h @@ -113,6 +113,15 @@ struct i2c_s { #include "gpio_object.h" +#if DEVICE_ANALOGOUT +struct dac_s { + DACName dac; + PinName pin; + uint32_t channel; + DAC_HandleTypeDef handle; +}; +#endif + #ifdef __cplusplus } #endif diff --git a/targets/TARGET_STM/TARGET_STM32F2/analogout_api.c b/targets/TARGET_STM/TARGET_STM32F2/analogout_device.c similarity index 55% rename from targets/TARGET_STM/TARGET_STM32F2/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32F2/analogout_device.c index ee7aad9026..584034fc85 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32F2/analogout_device.c @@ -35,16 +35,9 @@ #include "stm32f2xx_hal.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -DAC_HandleTypeDef DacHandle; -static DAC_ChannelConfTypeDef sConfig; - void analogout_init(dac_t *obj, PinName pin) { - uint32_t channel ; - HAL_StatusTypeDef status; + DAC_ChannelConfTypeDef sConfig; // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); @@ -52,7 +45,19 @@ void analogout_init(dac_t *obj, PinName pin) uint32_t function = pinmap_function(pin, PinMap_DAC); MBED_ASSERT(function != (uint32_t)NC); // Save the channel for the write and read functions - 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; + } if (obj->dac == (DACName)NC) { error("DAC pin mapping failed"); @@ -65,91 +70,25 @@ void analogout_init(dac_t *obj, PinName pin) __DAC_CLK_ENABLE(); - DacHandle.Instance = DAC; - - status = HAL_DAC_Init(&DacHandle); - if (status != HAL_OK) { + obj->handle.Instance = DAC; + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { error("HAL_DAC_Init failed"); } sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; - if (obj->channel == 1) { - channel = DAC_CHANNEL_1; - } else { - channel = DAC_CHANNEL_2; - } - - if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) { + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { error("HAL_DAC_ConfigChannel failed"); } - if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) { - error("HAL_DAC_Start failed"); - } - - if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) { - error("HAL_DAC_SetValue failed"); - } - + analogout_write_u16(obj, 0); } void analogout_free(dac_t *obj) { } -static inline void dac_write(dac_t *obj, int value) -{ - HAL_StatusTypeDef status = HAL_ERROR; - if (obj->channel == 1) { - status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - } else if (obj->channel == 2) { - status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - } - - if (status != HAL_OK) { - error("DAC pin mapping failed"); - } -} - -static inline int dac_read(dac_t *obj) -{ - if (obj->channel == 1) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); - } else if (obj->channel == 2) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); - } - return 0; /* Just silented warning */ -} - -void analogout_write(dac_t *obj, float value) -{ - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) -{ - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) -{ - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) -{ - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32F2/objects.h b/targets/TARGET_STM/TARGET_STM32F2/objects.h index 3bd0a18c6c..b2824cacd2 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F2/objects.h @@ -63,6 +63,7 @@ struct analogin_s { struct dac_s { DACName dac; uint8_t channel; + DAC_HandleTypeDef handle; }; struct serial_s { diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h index 39afece523..e83cd368dc 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h index 39afece523..e83cd368dc 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h index 39afece523..1706a1a120 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xC/objects.h @@ -58,12 +58,7 @@ struct analogin_s { ADCName adc; PinName pin; uint32_t channel; -}; - -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; + DAC_HandleTypeDef handle; }; struct can_s { diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h index 39afece523..e83cd368dc 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303xE/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h index ed18a49b4a..de612becfb 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F334x8/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - #if defined (DEVICE_CAN) struct can_s { CANName can; diff --git a/targets/TARGET_STM/TARGET_STM32F3/analogout_api.c b/targets/TARGET_STM/TARGET_STM32F3/analogout_device.c similarity index 64% rename from targets/TARGET_STM/TARGET_STM32F3/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32F3/analogout_device.c index 6b28142bb9..6bc7da1e5b 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32F3/analogout_device.c @@ -35,11 +35,6 @@ #include "mbed_error.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -static DAC_HandleTypeDef DacHandle; - // These variables are used for the "free" function static int pa4_used = 0; static int pa5_used = 0; @@ -54,7 +49,21 @@ void analogout_init(dac_t *obj, PinName pin) { // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_DAC); MBED_ASSERT(function != (uint32_t)NC); - obj->channel = STM_PIN_CHANNEL(function); + + // Save the channel for the write and read functions + 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 pinmap_pinout(pin, PinMap_DAC); @@ -73,25 +82,32 @@ void analogout_init(dac_t *obj, PinName pin) { #endif // Configure DAC - DacHandle.Instance = (DAC_TypeDef *)(obj->dac); + obj->handle.Instance = (DAC_TypeDef *)(obj->dac); + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { + error("HAL_DAC_Init failed"); + } + /* Enable both Buffer and Switch in the configuration, + * letting HAL layer in charge of selecting either one + * or the other depending on the actual DAC instance and + * channel being configured. + */ sConfig.DAC_Trigger = DAC_TRIGGER_NONE; - sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; + sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; +#if defined(DAC_OUTPUTSWITCH_ENABLE) + sConfig.DAC_OutputSwitch = DAC_OUTPUTSWITCH_ENABLE; +#endif if (pin == PA_4) { - HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); pa4_used = 1; } -#if defined(DAC_CHANNEL_2) if (pin == PA_5) { - HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2); pa5_used = 1; } -#endif - if (pin == PA_6) { - HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { + error("HAL_DAC_ConfigChannel failed"); } analogout_write_u16(obj, 0); @@ -120,53 +136,4 @@ void analogout_free(dac_t *obj) { pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } -static inline void dac_write(dac_t *obj, int value) { - if (obj->channel == 1) { - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - 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) { - if (obj->channel == 1) { - 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) { - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) { - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) { - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) { - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} - #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h index 0554d2511e..1e1d3c1263 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F3/common_objects.h @@ -111,6 +111,13 @@ struct i2c_s { #endif }; +struct dac_s { + DACName dac; + PinName pin; + uint32_t channel; + DAC_HandleTypeDef handle; +}; + #include "gpio_object.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h index 5405007fb9..d423c5ebef 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_MTS_MDOT_F405RG/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h index 01c4e0736b..48f9b79b01 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F407xG/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h index 75746368b6..1766e2b570 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F410xB/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h index 2372c20943..f8625bb942 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F413xH/objects.h @@ -55,11 +55,6 @@ struct trng_s { RNG_HandleTypeDef handle; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h index 09b00b04b6..7229a50a28 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h index 581a5a1e71..d92fa15901 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F437xG/objects.h @@ -64,11 +64,6 @@ struct trng_s { RNG_HandleTypeDef handle; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - #include "common_objects.h" struct can_s { CANName can; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h index 09b00b04b6..7229a50a28 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h index 1d48fe7587..f3fde7a621 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F446xE/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h index e9f971b2fc..50f0cfc362 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F469xI/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - uint8_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F4/analogout_api.c b/targets/TARGET_STM/TARGET_STM32F4/analogout_device.c similarity index 55% rename from targets/TARGET_STM/TARGET_STM32F4/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32F4/analogout_device.c index fb4eaa1595..802fad6bbb 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32F4/analogout_device.c @@ -35,23 +35,29 @@ #include "stm32f4xx_hal.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -DAC_HandleTypeDef DacHandle; -static DAC_ChannelConfTypeDef sConfig; - void analogout_init(dac_t *obj, PinName pin) { - uint32_t channel ; - HAL_StatusTypeDef status; + DAC_ChannelConfTypeDef sConfig; // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); // Get the functions (dac channel) from the pin and assign it to the object uint32_t function = pinmap_function(pin, PinMap_DAC); MBED_ASSERT(function != (uint32_t)NC); + // Save the channel for the write and read functions - 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; + } if (obj->dac == (DACName)NC) { error("DAC pin mapping failed"); @@ -64,84 +70,22 @@ void analogout_init(dac_t *obj, PinName pin) { __HAL_RCC_DAC_CLK_ENABLE(); - DacHandle.Instance = DAC; - - status = HAL_DAC_Init(&DacHandle); - if ( status != HAL_OK ) { + obj->handle.Instance = DAC; + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { error("HAL_DAC_Init failed"); } sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; - if (obj->channel == 1) { - channel = DAC_CHANNEL_1; - } else { - channel = DAC_CHANNEL_2; - } - - if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) { + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { error("HAL_DAC_ConfigChannel failed"); } - if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) { - error("HAL_DAC_Start failed"); - } - - if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) { - error("HAL_DAC_SetValue failed"); - } - + analogout_write_u16(obj, 0); } void analogout_free(dac_t *obj) { } -static inline void dac_write(dac_t *obj, int value) { - HAL_StatusTypeDef status = HAL_ERROR; - - if (obj->channel == 1) { - status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - } else if (obj->channel == 2) { - status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - } - - if ( status != HAL_OK ) { - error("DAC pin mapping failed"); - } -} - -static inline int dac_read(dac_t *obj) { - if (obj->channel == 1) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); - } else if (obj->channel == 2) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); - } - return 0; /* Just silented warning */ -} - -void analogout_write(dac_t *obj, float value) { - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) { - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) { - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) { - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} - #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32F4/common_objects.h b/targets/TARGET_STM/TARGET_STM32F4/common_objects.h index 3e586ede3d..3c8583a895 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F4/common_objects.h @@ -113,6 +113,14 @@ struct i2c_s { #define GPIO_IP_WITHOUT_BRR #include "gpio_object.h" +#if DEVICE_ANALOGOUT +struct dac_s { + DACName dac; + uint32_t channel; + DAC_HandleTypeDef handle; +}; +#endif + #ifdef __cplusplus } #endif diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h index 99d0f34b51..e43e5aea05 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F746xG/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h index 99d0f34b51..e43e5aea05 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F756xG/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h index 99d0f34b51..e43e5aea05 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F767xI/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h index 99d0f34b51..e43e5aea05 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint8_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32F7/analogout_api.c b/targets/TARGET_STM/TARGET_STM32F7/analogout_device.c similarity index 55% rename from targets/TARGET_STM/TARGET_STM32F7/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32F7/analogout_device.c index 56a3d11f61..8003d35a51 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32F7/analogout_device.c @@ -35,23 +35,29 @@ #include "stm32f7xx_hal.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -DAC_HandleTypeDef DacHandle; -static DAC_ChannelConfTypeDef sConfig; - void analogout_init(dac_t *obj, PinName pin) { - uint32_t channel ; - HAL_StatusTypeDef status; + DAC_ChannelConfTypeDef sConfig; // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); // Get the functions (dac channel) from the pin and assign it to the object uint32_t function = pinmap_function(pin, PinMap_DAC); MBED_ASSERT(function != (uint32_t)NC); + // Save the channel for the write and read functions - 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; + } if (obj->dac == (DACName)NC) { error("DAC pin mapping failed"); @@ -64,84 +70,23 @@ void analogout_init(dac_t *obj, PinName pin) { __DAC_CLK_ENABLE(); - DacHandle.Instance = DAC; - - status = HAL_DAC_Init(&DacHandle); - if (status != HAL_OK) { + obj->handle.Instance = DAC; + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { error("HAL_DAC_Init failed"); } sConfig.DAC_Trigger = DAC_TRIGGER_NONE; sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; - if (obj->channel == 1) { - channel = DAC_CHANNEL_1; - } else { - channel = DAC_CHANNEL_2; - } - - if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, channel) != HAL_OK) { + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { error("HAL_DAC_ConfigChannel failed"); } - if (HAL_DAC_Start(&DacHandle, channel) != HAL_OK) { - error("HAL_DAC_Start failed"); - } - - if (HAL_DAC_SetValue(&DacHandle, channel, DAC_ALIGN_12B_R, 0x000) != HAL_OK) { - error("HAL_DAC_SetValue failed"); - } - + analogout_write_u16(obj, 0); } void analogout_free(dac_t *obj) { } -static inline void dac_write(dac_t *obj, int value) { - HAL_StatusTypeDef status = HAL_ERROR; - - if (obj->channel == 1) { - status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - } else if (obj->channel == 2) { - status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - } - - if (status != HAL_OK) { - error("DAC pin mapping failed"); - } -} - -static inline int dac_read(dac_t *obj) { - if (obj->channel == 1) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); - } else if (obj->channel == 2) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); - } - return 0; /* Just silented warning */ -} - -void analogout_write(dac_t *obj, float value) { - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) { - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) { - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) { - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32F7/common_objects.h b/targets/TARGET_STM/TARGET_STM32F7/common_objects.h index 68b964fad2..cf5cad0028 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32F7/common_objects.h @@ -114,6 +114,13 @@ struct i2c_s { #define GPIO_IP_WITHOUT_BRR #include "gpio_object.h" +struct dac_s { + DACName dac; + PinName pin; + uint32_t channel; + DAC_HandleTypeDef handle; +}; + #ifdef __cplusplus } #endif diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h index 3bcbd56164..27c980be5d 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L053C8/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h index 63dc73c511..9728243d34 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_DISCO_L072CZ_LRWAN1/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h index 8160a10ad4..cc273c9206 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L053R8/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h index 3bcbd56164..27c980be5d 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/TARGET_NUCLEO_L073RZ/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct trng_s { RNG_HandleTypeDef handle; }; diff --git a/targets/TARGET_STM/TARGET_STM32L0/analogout_api.c b/targets/TARGET_STM/TARGET_STM32L0/analogout_device.c similarity index 59% rename from targets/TARGET_STM/TARGET_STM32L0/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32L0/analogout_device.c index a83c73a54a..dbf8f9cab1 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32L0/analogout_device.c @@ -35,11 +35,6 @@ #include "mbed_error.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -static DAC_HandleTypeDef DacHandle; - // These variables are used for the "free" function static int channel1_used = 0; static int channel2_used = 0; @@ -54,7 +49,19 @@ void analogout_init(dac_t *obj, PinName pin) { // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_DAC); 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 pinmap_pinout(pin, PinMap_DAC); @@ -66,26 +73,22 @@ void analogout_init(dac_t *obj, PinName pin) { __DAC_CLK_ENABLE(); // Configure DAC - DacHandle.Instance = DAC; + obj->handle.Instance = DAC; + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { + error("HAL_DAC_Init failed"); + } sConfig.DAC_Trigger = DAC_TRIGGER_NONE; - sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; + sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; -#if defined(DAC_CHANNEL_2) - if (obj->channel == 2) { - if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2) != HAL_OK) { - error("Cannot configure DAC channel 2"); - } - channel2_used = 1; - } else -#endif - { - // channel 1 per default - if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1) != HAL_OK) { - error("Cannot configure DAC channel 1"); - } - obj->channel = 1; + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { + error("Cannot configure DAC channel 2"); + } + + if (obj->channel == DAC_CHANNEL_1) { channel1_used = 1; + } else { + channel2_used = 1; } analogout_write_u16(obj, 0); @@ -93,9 +96,10 @@ void analogout_init(dac_t *obj, PinName pin) { void analogout_free(dac_t *obj) { // Reset DAC and disable clock - if (obj->channel == 1) channel1_used = 0; - if (obj->channel == 2) channel2_used = 0; - + if (obj->channel == DAC_CHANNEL_1) channel1_used = 0; +#if defined(DAC_CHANNEL_2) + if (obj->channel == DAC_CHANNEL_2) channel2_used = 0; +#endif if ((channel1_used == 0) && (channel2_used == 0)) { __DAC_FORCE_RESET(); __DAC_RELEASE_RESET(); @@ -106,53 +110,4 @@ void analogout_free(dac_t *obj) { pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } -static inline void dac_write(dac_t *obj, int value) { - if (obj->channel == 1) { - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - 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) { - if (obj->channel == 1) { - 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) { - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) { - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) { - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) { - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} - #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32L0/common_objects.h b/targets/TARGET_STM/TARGET_STM32L0/common_objects.h index 4f6d7cf20f..7233c0871e 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L0/common_objects.h @@ -118,6 +118,15 @@ struct flash_s { #include "gpio_object.h" +#if DEVICE_ANALOGOUT +struct dac_s { + DACName dac; + PinName pin; + uint32_t channel; + DAC_HandleTypeDef handle; +}; +#endif + #ifdef __cplusplus } #endif diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c index 8a26780f57..b48df5be2b 100755 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/PeripheralPins.c @@ -67,8 +67,8 @@ const PinMap PinMap_ADC[] = { //*** DAC *** const PinMap PinMap_DAC[] = { - {PA_4, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT1 - {PA_5, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT2 (Warning: LED1 is also on this pin) + {PA_4, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 1, 0)}, // DAC1_OUT1 + {PA_5, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 2, 0)}, // DAC1_OUT2 (Warning: LED1 is also on this pin) {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h index d643f64c3c..69653d026a 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_MOTE_L152RC/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; -}; - #define GPIO_IP_WITHOUT_BRR #include "common_objects.h" diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c index 64910782d6..bf85c1d365 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/PeripheralPins.c @@ -67,8 +67,8 @@ const PinMap PinMap_ADC[] = { //*** DAC *** const PinMap PinMap_DAC[] = { - {PA_4, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT1 - {PA_5, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT2 (Warning: LED1 is also on this pin) + {PA_4, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 1, 0)}, // DAC1_OUT1 + {PA_5, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 2, 0)}, // DAC1_OUT2 (Warning: LED1 is also on this pin) {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h index 73e9d5a294..4ef06de688 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NUCLEO_L152RE/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c index e63f41f54e..a1a61f25c2 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/PeripheralPins.c @@ -67,8 +67,8 @@ const PinMap PinMap_ADC[] = { //*** DAC *** const PinMap PinMap_DAC[] = { - {PA_4, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT1 - {PA_5, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT2 (Warning: LED1 is also on this pin) + {PA_4, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 1, 0)}, // DAC1_OUT1 + {PA_5, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 2, 0)}, // DAC1_OUT2 (Warning: LED1 is also on this pin) {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h index 73e9d5a294..4ef06de688 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_NZ32_SC151/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; -}; - #include "common_objects.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c index 4a8ab11503..c80e07b648 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/PeripheralPins.c @@ -57,8 +57,8 @@ const PinMap PinMap_ADC[] = { //*** DAC *** const PinMap PinMap_DAC[] = { - {PA_4, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT1 - {PA_5, DAC_1, STM_PIN_DATA(STM_MODE_ANALOG, GPIO_NOPULL, 0)}, // DAC_OUT2 (Warning: LED1 is also on this pin) + {PA_4, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 1, 0)}, // DAC1_OUT1 + {PA_5, DAC_1, STM_PIN_DATA_EXT(STM_MODE_ANALOG, GPIO_NOPULL, 0, 2, 0)}, // DAC1_OUT2 (Warning: LED1 is also on this pin) {NC, NC, 0} }; diff --git a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h index d643f64c3c..69653d026a 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/TARGET_XDOT_L151CC/objects.h @@ -60,11 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; -}; - #define GPIO_IP_WITHOUT_BRR #include "common_objects.h" diff --git a/targets/TARGET_STM/TARGET_STM32L1/analogout_api.c b/targets/TARGET_STM/TARGET_STM32L1/analogout_device.c similarity index 64% rename from targets/TARGET_STM/TARGET_STM32L1/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32L1/analogout_device.c index 226a04f454..7dd2cb7eac 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/analogout_device.c @@ -35,11 +35,6 @@ #include "mbed_error.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -static DAC_HandleTypeDef DacHandle; - // These variables are used for the "free" function static int pa4_used = 0; static int pa5_used = 0; @@ -47,11 +42,24 @@ static int pa5_used = 0; void analogout_init(dac_t *obj, PinName pin) { DAC_ChannelConfTypeDef sConfig; - DacHandle.Instance = DAC; - // Get the peripheral name (DAC_1, ...) from the pin and assign it to the object obj->dac = (DACName)pinmap_peripheral(pin, PinMap_DAC); MBED_ASSERT(obj->dac != (DACName)NC); + // Get the pin function and assign the used channel to the object + uint32_t function = pinmap_function(pin, PinMap_DAC); + 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 pinmap_pinout(pin, PinMap_DAC); @@ -59,21 +67,28 @@ void analogout_init(dac_t *obj, PinName pin) { // Save the channel for future use obj->pin = pin; + obj->handle.Instance = DAC; + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { + error("HAL_DAC_Init failed"); + } + // Enable DAC clock __DAC_CLK_ENABLE(); // Configure DAC sConfig.DAC_Trigger = DAC_TRIGGER_NONE; - sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE; + sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE; if (pin == PA_4) { - HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1); pa4_used = 1; } else { // PA_5 - HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2); pa5_used = 1; } + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { + error("Cannot configure DAC channel\n"); + } + analogout_write_u16(obj, 0); } @@ -91,46 +106,4 @@ void analogout_free(dac_t *obj) { pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } -static inline void dac_write(dac_t *obj, int value) { - if (obj->pin == PA_4) { - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); - } else { // PA_5 - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); - } -} - -static inline int dac_read(dac_t *obj) { - if (obj->pin == PA_4) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); - } else { // PA_5 - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); - } -} - -void analogout_write(dac_t *obj, float value) { - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) { - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) { - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) { - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} - #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h index f40ffcdb32..ef2e65c75e 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L1/common_objects.h @@ -46,7 +46,7 @@ struct pwmout_s { uint32_t period; uint32_t pulse; uint8_t channel; - uint8_t inverted; + uint8_t inverted; }; struct serial_s { @@ -110,6 +110,13 @@ struct i2c_s { #endif }; +struct dac_s { + DACName dac; + PinName pin; + uint32_t channel; + DAC_HandleTypeDef handle; +}; + #include "gpio_object.h" #ifdef __cplusplus diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h index e547ad300b..0b8ee2687a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h index e568f77c90..6f41dbeb74 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L475xG/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h index e547ad300b..0b8ee2687a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L476xG/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h index e547ad300b..0b8ee2687a 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L486xG/objects.h @@ -60,12 +60,6 @@ struct analogin_s { uint32_t channel; }; -struct dac_s { - DACName dac; - PinName pin; - uint32_t channel; -}; - struct can_s { CANName can; int index; diff --git a/targets/TARGET_STM/TARGET_STM32L4/analogout_api.c b/targets/TARGET_STM/TARGET_STM32L4/analogout_device.c similarity index 62% rename from targets/TARGET_STM/TARGET_STM32L4/analogout_api.c rename to targets/TARGET_STM/TARGET_STM32L4/analogout_device.c index d5e3fcc016..bd5bafbd92 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/analogout_api.c +++ b/targets/TARGET_STM/TARGET_STM32L4/analogout_device.c @@ -35,11 +35,6 @@ #include "mbed_error.h" #include "PeripheralPins.h" -#define DAC_RANGE (0xFFF) // 12 bits -#define DAC_NB_BITS (12) - -static DAC_HandleTypeDef DacHandle; - // These variables are used for the "free" function static int channel1_used = 0; static int channel2_used = 0; @@ -54,7 +49,20 @@ void analogout_init(dac_t *obj, PinName pin) { // Get the pin function and assign the used channel to the object uint32_t function = pinmap_function(pin, PinMap_DAC); 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 pinmap_pinout(pin, PinMap_DAC); @@ -66,10 +74,9 @@ void analogout_init(dac_t *obj, PinName pin) { __HAL_RCC_DAC1_CLK_ENABLE(); // Configure DAC - DacHandle.Instance = DAC; - - if (HAL_DAC_Init(&DacHandle) != HAL_OK) { - error("Cannot initialize DAC\n"); + obj->handle.Instance = DAC; + if (HAL_DAC_Init(&obj->handle) != HAL_OK ) { + error("HAL_DAC_Init failed"); } sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE; @@ -78,17 +85,14 @@ void analogout_init(dac_t *obj, PinName pin) { sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_DISABLE; sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY; - if (obj->channel == 2) { - if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_2) != HAL_OK) { - error("Cannot configure DAC channel 2\n"); - } - channel2_used = 1; - } else { // channel 1 per default - if (HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1) != HAL_OK) { - error("Cannot configure DAC channel 1\n"); - } - obj->channel = 1; + if (obj->channel == DAC_CHANNEL_1) { channel1_used = 1; + } else { // channel 1 per default + channel2_used = 1; + } + + if (HAL_DAC_ConfigChannel(&obj->handle, &sConfig, obj->channel) != HAL_OK) { + error("Cannot configure DAC channel\n"); } analogout_write_u16(obj, 0); @@ -96,8 +100,10 @@ void analogout_init(dac_t *obj, PinName pin) { void analogout_free(dac_t *obj) { // Reset DAC and disable clock - if (obj->channel == 1) channel1_used = 0; - if (obj->channel == 2) channel2_used = 0; + if (obj->channel == DAC_CHANNEL_1) channel1_used = 0; +#if defined(DAC_CHANNEL_2) + if (obj->channel == DAC_CHANNEL_2) channel2_used = 0; +#endif if ((channel1_used == 0) && (channel2_used == 0)) { __HAL_RCC_DAC1_FORCE_RESET(); @@ -109,49 +115,4 @@ void analogout_free(dac_t *obj) { pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); } -static inline void dac_write(dac_t *obj, int value) { - if (obj->channel == 1) { - HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE)); - HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); - } - 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); - } -} - -static inline int dac_read(dac_t *obj) { - if (obj->channel == 1) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); - } - if (obj->channel == 2) { - return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); - } - return 0; -} - -void analogout_write(dac_t *obj, float value) { - if (value < 0.0f) { - dac_write(obj, 0); // Min value - } else if (value > 1.0f) { - dac_write(obj, (int)DAC_RANGE); // Max value - } else { - dac_write(obj, (int)(value * (float)DAC_RANGE)); - } -} - -void analogout_write_u16(dac_t *obj, uint16_t value) { - dac_write(obj, value >> (16 - DAC_NB_BITS)); -} - -float analogout_read(dac_t *obj) { - uint32_t value = dac_read(obj); - return (float)value * (1.0f / (float)DAC_RANGE); -} - -uint16_t analogout_read_u16(dac_t *obj) { - uint32_t value = dac_read(obj); - return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits -} - #endif // DEVICE_ANALOGOUT diff --git a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h index 22fc902c83..d5d9d2707b 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/common_objects.h +++ b/targets/TARGET_STM/TARGET_STM32L4/common_objects.h @@ -118,6 +118,13 @@ struct flash_s { #include "gpio_object.h" +struct dac_s { + DACName dac; + PinName pin; + uint32_t channel; + DAC_HandleTypeDef handle; +}; + #ifdef __cplusplus } #endif diff --git a/targets/TARGET_STM/analogout_api.c b/targets/TARGET_STM/analogout_api.c new file mode 100644 index 0000000000..4dac5ce527 --- /dev/null +++ b/targets/TARGET_STM/analogout_api.c @@ -0,0 +1,80 @@ +/* mbed Microcontroller Library + * Copyright (c) 2017, STMicroelectronics + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "mbed_assert.h" +#include "analogout_api.h" + +#if DEVICE_ANALOGOUT + +#include "cmsis.h" +#include "pinmap.h" +#include "mbed_error.h" +#include "PeripheralPins.h" + +#define DAC_RANGE (0xFFF) // 12 bits +#define DAC_NB_BITS (12) + +static inline void dac_write(dac_t *obj, int value) +{ + HAL_DAC_SetValue(&obj->handle, obj->channel, DAC_ALIGN_12B_R, (value & DAC_RANGE)); + HAL_DAC_Start(&obj->handle, obj->channel); +} + +static inline int dac_read(dac_t *obj) +{ + return (int)HAL_DAC_GetValue(&obj->handle, obj->channel); +} + +void analogout_write(dac_t *obj, float value) +{ + if (value < 0.0f) { + dac_write(obj, 0); // Min value + } else if (value > 1.0f) { + dac_write(obj, (int)DAC_RANGE); // Max value + } else { + dac_write(obj, (int)(value * (float)DAC_RANGE)); + } +} + +void analogout_write_u16(dac_t *obj, uint16_t value) +{ + dac_write(obj, value >> (16 - DAC_NB_BITS)); +} + +float analogout_read(dac_t *obj) +{ + uint32_t value = dac_read(obj); + return (float)value * (1.0f / (float)DAC_RANGE); +} + +uint16_t analogout_read_u16(dac_t *obj) +{ + uint32_t value = dac_read(obj); + return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits +} + +#endif // DEVICE_ANALOGOUT