Merge pull request #1384 from bcostm/fix_analogout

STM32xx -Fix analog_out issue with 12 to 16bits conversion
pull/1396/head
Martin Kojtal 2015-10-26 08:57:15 +00:00
commit 22ef412435
7 changed files with 118 additions and 181 deletions

View File

@ -36,6 +36,7 @@
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define DAC_RANGE (0xFFF) // 12 bits #define DAC_RANGE (0xFFF) // 12 bits
#define DAC_NB_BITS (12)
static DAC_HandleTypeDef DacHandle; static DAC_HandleTypeDef DacHandle;
@ -80,12 +81,12 @@ void analogout_free(dac_t *obj) {
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
} }
static inline void dac_write(dac_t *obj, uint16_t value) { static inline void dac_write(dac_t *obj, int value) {
if (obj->pin == PA_4) { if (obj->pin == PA_4) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
} else { // PA_5 } else if (obj->pin == PA_5) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
} }
} }
@ -93,36 +94,34 @@ static inline void dac_write(dac_t *obj, uint16_t value) {
static inline int dac_read(dac_t *obj) { static inline int dac_read(dac_t *obj) {
if (obj->pin == PA_4) { if (obj->pin == PA_4) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} else { // PA_5 } else if (obj->pin == PA_5) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
} }
return 0; /* Just silented warning */
} }
void analogout_write(dac_t *obj, float value) { void analogout_write(dac_t *obj, float value) {
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value dac_write(obj, (int)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); dac_write(obj, (int)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) { void analogout_write_u16(dac_t *obj, uint16_t value) {
if (value > (uint16_t)DAC_RANGE) { dac_write(obj, value >> (16 - DAC_NB_BITS));
dac_write(obj, (uint16_t)DAC_RANGE); // Max value
} else {
dac_write(obj, value);
}
} }
float analogout_read(dac_t *obj) { float analogout_read(dac_t *obj) {
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)((float)value * (1.0f / (float)DAC_RANGE)); return (float)value * (1.0f / (float)DAC_RANGE);
} }
uint16_t analogout_read_u16(dac_t *obj) { uint16_t analogout_read_u16(dac_t *obj) {
return (uint16_t)dac_read(obj); uint32_t value = dac_read(obj);
return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
} }
#endif // DEVICE_ANALOGOUT #endif // DEVICE_ANALOGOUT

View File

@ -36,6 +36,7 @@
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define DAC_RANGE (0xFFF) // 12 bits #define DAC_RANGE (0xFFF) // 12 bits
#define DAC_NB_BITS (12)
static DAC_HandleTypeDef DacHandle; static DAC_HandleTypeDef DacHandle;
@ -43,8 +44,7 @@ static DAC_HandleTypeDef DacHandle;
static int pa4_used = 0; static int pa4_used = 0;
static int pa5_used = 0; static int pa5_used = 0;
void analogout_init(dac_t *obj, PinName pin) void analogout_init(dac_t *obj, PinName pin) {
{
DAC_ChannelConfTypeDef sConfig; DAC_ChannelConfTypeDef sConfig;
// Get the peripheral name from the pin and assign it to the object // Get the peripheral name from the pin and assign it to the object
@ -97,8 +97,7 @@ void analogout_init(dac_t *obj, PinName pin)
analogout_write_u16(obj, 0); analogout_write_u16(obj, 0);
} }
void analogout_free(dac_t *obj) void analogout_free(dac_t *obj) {
{
// Reset DAC and disable clock // Reset DAC and disable clock
if (obj->pin == PA_4) pa4_used = 0; if (obj->pin == PA_4) pa4_used = 0;
if (obj->pin == PA_5) pa5_used = 0; if (obj->pin == PA_5) pa5_used = 0;
@ -121,22 +120,20 @@ void analogout_free(dac_t *obj)
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
} }
static inline void dac_write(dac_t *obj, uint16_t value) static inline void dac_write(dac_t *obj, int value) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
} }
#if defined(DAC_CHANNEL_2) #if defined(DAC_CHANNEL_2)
if (obj->channel == 2) { if (obj->channel == 2) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
} }
#endif #endif
} }
static inline int dac_read(dac_t *obj) static inline int dac_read(dac_t *obj) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} }
@ -148,35 +145,28 @@ static inline int dac_read(dac_t *obj)
return 0; return 0;
} }
void analogout_write(dac_t *obj, float value) void analogout_write(dac_t *obj, float value) {
{
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value dac_write(obj, (int)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); dac_write(obj, (int)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) void analogout_write_u16(dac_t *obj, uint16_t value) {
{ dac_write(obj, value >> (16 - DAC_NB_BITS));
if (value > (uint16_t)DAC_RANGE) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value
} else {
dac_write(obj, value);
}
} }
float analogout_read(dac_t *obj) float analogout_read(dac_t *obj) {
{
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)((float)value * (1.0f / (float)DAC_RANGE)); return (float)value * (1.0f / (float)DAC_RANGE);
} }
uint16_t analogout_read_u16(dac_t *obj) uint16_t analogout_read_u16(dac_t *obj) {
{ uint32_t value = dac_read(obj);
return (uint16_t)dac_read(obj); return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
} }
#endif // DEVICE_ANALOGOUT #endif // DEVICE_ANALOGOUT

View File

@ -35,13 +35,13 @@
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define RANGE_12BIT (0xFFF) #define DAC_RANGE (0xFFF) // 12 bits
#define DAC_NB_BITS (12)
DAC_HandleTypeDef DacHandle; DAC_HandleTypeDef DacHandle;
static DAC_ChannelConfTypeDef sConfig; static DAC_ChannelConfTypeDef sConfig;
void analogout_init(dac_t *obj, PinName pin) void analogout_init(dac_t *obj, PinName pin) {
{
uint32_t channel ; uint32_t channel ;
HAL_StatusTypeDef status; HAL_StatusTypeDef status;
@ -94,18 +94,16 @@ void analogout_init(dac_t *obj, PinName pin)
} }
void analogout_free(dac_t *obj) void analogout_free(dac_t *obj) {
{
} }
static inline void dac_write(dac_t *obj, uint16_t value) static inline void dac_write(dac_t *obj, int value) {
{
HAL_StatusTypeDef status = HAL_ERROR; HAL_StatusTypeDef status = HAL_ERROR;
if (obj->channel == 1) { if (obj->channel == 1) {
status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
} else if (obj->channel == 2) { } else if (obj->channel == 2) {
status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
} }
if ( status != HAL_OK ) { if ( status != HAL_OK ) {
@ -113,46 +111,37 @@ static inline void dac_write(dac_t *obj, uint16_t value)
} }
} }
static inline int dac_read(dac_t *obj) static inline int dac_read(dac_t *obj) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} else if (obj->channel == 2) { } else if (obj->channel == 2) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_2);
} }
return 0; /* Just silented warning */ return 0; /* Just silented warning */
} }
void analogout_write(dac_t *obj, float value) void analogout_write(dac_t *obj, float value) {
{
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value dac_write(obj, (int)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT)); dac_write(obj, (int)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) void analogout_write_u16(dac_t *obj, uint16_t value) {
{ dac_write(obj, value >> (16 - DAC_NB_BITS));
if (value > (uint16_t)RANGE_12BIT) {
value = (uint16_t)RANGE_12BIT; // Max value
}
dac_write(obj, value);
} }
float analogout_read(dac_t *obj) float analogout_read(dac_t *obj) {
{
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)value * (1.0f / (float)RANGE_12BIT); return (float)value * (1.0f / (float)DAC_RANGE);
} }
uint16_t analogout_read_u16(dac_t *obj) uint16_t analogout_read_u16(dac_t *obj) {
{ uint32_t value = dac_read(obj);
return (uint16_t)dac_read(obj); return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
} }
#endif // DEVICE_ANALOGOUT #endif // DEVICE_ANALOGOUT

View File

@ -35,13 +35,13 @@
#include "stm32f7xx_hal.h" #include "stm32f7xx_hal.h"
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define RANGE_12BIT (0xFFF) #define DAC_RANGE (0xFFF) // 12 bits
#define DAC_NB_BITS (12)
DAC_HandleTypeDef DacHandle; DAC_HandleTypeDef DacHandle;
static DAC_ChannelConfTypeDef sConfig; static DAC_ChannelConfTypeDef sConfig;
void analogout_init(dac_t *obj, PinName pin) void analogout_init(dac_t *obj, PinName pin) {
{
uint32_t channel ; uint32_t channel ;
HAL_StatusTypeDef status; HAL_StatusTypeDef status;
@ -94,18 +94,16 @@ void analogout_init(dac_t *obj, PinName pin)
} }
void analogout_free(dac_t *obj) void analogout_free(dac_t *obj) {
{
} }
static inline void dac_write(dac_t *obj, uint16_t value) static inline void dac_write(dac_t *obj, int value) {
{
HAL_StatusTypeDef status = HAL_ERROR; HAL_StatusTypeDef status = HAL_ERROR;
if (obj->channel == 1) { if (obj->channel == 1) {
status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
} else if (obj->channel == 2) { } else if (obj->channel == 2) {
status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); status = HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
} }
if (status != HAL_OK) { if (status != HAL_OK) {
@ -113,8 +111,7 @@ static inline void dac_write(dac_t *obj, uint16_t value)
} }
} }
static inline int dac_read(dac_t *obj) static inline int dac_read(dac_t *obj) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} else if (obj->channel == 2) { } else if (obj->channel == 2) {
@ -123,36 +120,28 @@ static inline int dac_read(dac_t *obj)
return 0; /* Just silented warning */ return 0; /* Just silented warning */
} }
void analogout_write(dac_t *obj, float value) void analogout_write(dac_t *obj, float value) {
{
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value dac_write(obj, (int)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT)); dac_write(obj, (int)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) void analogout_write_u16(dac_t *obj, uint16_t value) {
{ dac_write(obj, value >> (16 - DAC_NB_BITS));
if (value > (uint16_t)RANGE_12BIT) {
value = (uint16_t)RANGE_12BIT; // Max value
}
dac_write(obj, value);
} }
float analogout_read(dac_t *obj) float analogout_read(dac_t *obj) {
{
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)value * (1.0f / (float)RANGE_12BIT); return (float)value * (1.0f / (float)DAC_RANGE);
} }
uint16_t analogout_read_u16(dac_t *obj) uint16_t analogout_read_u16(dac_t *obj) {
{ uint32_t value = dac_read(obj);
return (uint16_t)dac_read(obj); return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
} }
#endif // DEVICE_ANALOGOUT #endif // DEVICE_ANALOGOUT

View File

@ -36,6 +36,7 @@
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define DAC_RANGE (0xFFF) // 12 bits #define DAC_RANGE (0xFFF) // 12 bits
#define DAC_NB_BITS (12)
static DAC_HandleTypeDef DacHandle; static DAC_HandleTypeDef DacHandle;
@ -43,8 +44,7 @@ static DAC_HandleTypeDef DacHandle;
static int channel1_used = 0; static int channel1_used = 0;
static int channel2_used = 0; static int channel2_used = 0;
void analogout_init(dac_t *obj, PinName pin) void analogout_init(dac_t *obj, PinName pin) {
{
DAC_ChannelConfTypeDef sConfig; DAC_ChannelConfTypeDef sConfig;
// Get the peripheral name from the pin and assign it to the object // Get the peripheral name from the pin and assign it to the object
@ -91,8 +91,7 @@ void analogout_init(dac_t *obj, PinName pin)
analogout_write_u16(obj, 0); analogout_write_u16(obj, 0);
} }
void analogout_free(dac_t *obj) void analogout_free(dac_t *obj) {
{
// Reset DAC and disable clock // Reset DAC and disable clock
if (obj->channel == 1) channel1_used = 0; if (obj->channel == 1) channel1_used = 0;
if (obj->channel == 2) channel2_used = 0; if (obj->channel == 2) channel2_used = 0;
@ -107,22 +106,20 @@ void analogout_free(dac_t *obj)
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
} }
static inline void dac_write(dac_t *obj, uint16_t value) static inline void dac_write(dac_t *obj, int value) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
} }
#if defined(DAC_CHANNEL_2) #if defined(DAC_CHANNEL_2)
if (obj->channel == 2) { if (obj->channel == 2) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
} }
#endif #endif
} }
static inline int dac_read(dac_t *obj) static inline int dac_read(dac_t *obj) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} }
@ -134,35 +131,28 @@ static inline int dac_read(dac_t *obj)
return 0; return 0;
} }
void analogout_write(dac_t *obj, float value) void analogout_write(dac_t *obj, float value) {
{
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value dac_write(obj, (int)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); dac_write(obj, (int)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) void analogout_write_u16(dac_t *obj, uint16_t value) {
{ dac_write(obj, value >> (16 - DAC_NB_BITS));
if (value > (uint16_t)DAC_RANGE) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value
} else {
dac_write(obj, value);
}
} }
float analogout_read(dac_t *obj) float analogout_read(dac_t *obj) {
{
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)((float)value * (1.0f / (float)DAC_RANGE)); return (float)value * (1.0f / (float)DAC_RANGE);
} }
uint16_t analogout_read_u16(dac_t *obj) uint16_t analogout_read_u16(dac_t *obj) {
{ uint32_t value = dac_read(obj);
return (uint16_t)dac_read(obj); return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
} }
#endif // DEVICE_ANALOGOUT #endif // DEVICE_ANALOGOUT

View File

@ -36,6 +36,7 @@
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define DAC_RANGE (0xFFF) // 12 bits #define DAC_RANGE (0xFFF) // 12 bits
#define DAC_NB_BITS (12)
static DAC_HandleTypeDef DacHandle; static DAC_HandleTypeDef DacHandle;
@ -43,8 +44,7 @@ static DAC_HandleTypeDef DacHandle;
static int pa4_used = 0; static int pa4_used = 0;
static int pa5_used = 0; static int pa5_used = 0;
void analogout_init(dac_t *obj, PinName pin) void analogout_init(dac_t *obj, PinName pin) {
{
DAC_ChannelConfTypeDef sConfig; DAC_ChannelConfTypeDef sConfig;
DacHandle.Instance = DAC; DacHandle.Instance = DAC;
@ -77,8 +77,7 @@ void analogout_init(dac_t *obj, PinName pin)
analogout_write_u16(obj, 0); analogout_write_u16(obj, 0);
} }
void analogout_free(dac_t *obj) void analogout_free(dac_t *obj) {
{
// Reset DAC and disable clock // Reset DAC and disable clock
if (obj->pin == PA_4) pa4_used = 0; if (obj->pin == PA_4) pa4_used = 0;
if (obj->pin == PA_5) pa5_used = 0; if (obj->pin == PA_5) pa5_used = 0;
@ -92,19 +91,17 @@ void analogout_free(dac_t *obj)
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
} }
static inline void dac_write(dac_t *obj, uint16_t value) static inline void dac_write(dac_t *obj, int value) {
{
if (obj->pin == PA_4) { if (obj->pin == PA_4) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
} else { // PA_5 } else { // PA_5
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
} }
} }
static inline int dac_read(dac_t *obj) static inline int dac_read(dac_t *obj) {
{
if (obj->pin == PA_4) { if (obj->pin == PA_4) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} else { // PA_5 } else { // PA_5
@ -112,35 +109,28 @@ static inline int dac_read(dac_t *obj)
} }
} }
void analogout_write(dac_t *obj, float value) void analogout_write(dac_t *obj, float value) {
{
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value dac_write(obj, (int)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); dac_write(obj, (int)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) void analogout_write_u16(dac_t *obj, uint16_t value) {
{ dac_write(obj, value >> (16 - DAC_NB_BITS));
if (value > (uint16_t)DAC_RANGE) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value
} else {
dac_write(obj, value);
}
} }
float analogout_read(dac_t *obj) float analogout_read(dac_t *obj) {
{
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)((float)value * (1.0f / (float)DAC_RANGE)); return (float)value * (1.0f / (float)DAC_RANGE);
} }
uint16_t analogout_read_u16(dac_t *obj) uint16_t analogout_read_u16(dac_t *obj) {
{ uint32_t value = dac_read(obj);
return (uint16_t)dac_read(obj); return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
} }
#endif // DEVICE_ANALOGOUT #endif // DEVICE_ANALOGOUT

View File

@ -36,6 +36,7 @@
#include "PeripheralPins.h" #include "PeripheralPins.h"
#define DAC_RANGE (0xFFF) // 12 bits #define DAC_RANGE (0xFFF) // 12 bits
#define DAC_NB_BITS (12)
static DAC_HandleTypeDef DacHandle; static DAC_HandleTypeDef DacHandle;
@ -43,8 +44,7 @@ static DAC_HandleTypeDef DacHandle;
static int channel1_used = 0; static int channel1_used = 0;
static int channel2_used = 0; static int channel2_used = 0;
void analogout_init(dac_t *obj, PinName pin) void analogout_init(dac_t *obj, PinName pin) {
{
DAC_ChannelConfTypeDef sConfig = {0}; DAC_ChannelConfTypeDef sConfig = {0};
// Get the peripheral name from the pin and assign it to the object // Get the peripheral name from the pin and assign it to the object
@ -94,8 +94,7 @@ void analogout_init(dac_t *obj, PinName pin)
analogout_write_u16(obj, 0); analogout_write_u16(obj, 0);
} }
void analogout_free(dac_t *obj) void analogout_free(dac_t *obj) {
{
// Reset DAC and disable clock // Reset DAC and disable clock
if (obj->channel == 1) channel1_used = 0; if (obj->channel == 1) channel1_used = 0;
if (obj->channel == 2) channel2_used = 0; if (obj->channel == 2) channel2_used = 0;
@ -110,20 +109,18 @@ void analogout_free(dac_t *obj)
pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
} }
static inline void dac_write(dac_t *obj, uint16_t value) static inline void dac_write(dac_t *obj, int value) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_1, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_1);
} }
if (obj->channel == 2) { if (obj->channel == 2) {
HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value); HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2); HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
} }
} }
static inline int dac_read(dac_t *obj) static inline int dac_read(dac_t *obj) {
{
if (obj->channel == 1) { if (obj->channel == 1) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1); return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} }
@ -133,35 +130,28 @@ static inline int dac_read(dac_t *obj)
return 0; return 0;
} }
void analogout_write(dac_t *obj, float value) void analogout_write(dac_t *obj, float value) {
{
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value dac_write(obj, (int)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)DAC_RANGE)); dac_write(obj, (int)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) void analogout_write_u16(dac_t *obj, uint16_t value) {
{ dac_write(obj, value >> (16 - DAC_NB_BITS));
if (value > (uint16_t)DAC_RANGE) {
dac_write(obj, (uint16_t)DAC_RANGE); // Max value
} else {
dac_write(obj, value);
}
} }
float analogout_read(dac_t *obj) float analogout_read(dac_t *obj) {
{
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)((float)value * (1.0f / (float)DAC_RANGE)); return (float)value * (1.0f / (float)DAC_RANGE);
} }
uint16_t analogout_read_u16(dac_t *obj) uint16_t analogout_read_u16(dac_t *obj) {
{ uint32_t value = dac_read(obj);
return (uint16_t)dac_read(obj); return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
} }
#endif // DEVICE_ANALOGOUT #endif // DEVICE_ANALOGOUT