Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Nucleo_L432KC_Quadrature_Decoder_with_ADC_and_DAC
Fork of mbed-dev by
Diff: targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c
- Revision:
- 8:69ce7aaad4c4
- Parent:
- 0:9b334a45a8ff
- Child:
- 144:ef7eb2e8f9f7
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c Mon Oct 26 09:00:10 2015 +0000
@@ -36,6 +36,7 @@
#include "PeripheralPins.h"
#define DAC_RANGE (0xFFF) // 12 bits
+#define DAC_NB_BITS (12)
static DAC_HandleTypeDef DacHandle;
@@ -43,8 +44,7 @@
static int pa4_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;
// Get the peripheral name from the pin and assign it to the object
@@ -97,8 +97,7 @@
analogout_write_u16(obj, 0);
}
-void analogout_free(dac_t *obj)
-{
+void analogout_free(dac_t *obj) {
// Reset DAC and disable clock
if (obj->pin == PA_4) pa4_used = 0;
if (obj->pin == PA_5) pa5_used = 0;
@@ -121,22 +120,20 @@
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) {
- 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);
}
#if defined(DAC_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);
}
#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(&DacHandle, DAC_CHANNEL_1);
}
@@ -148,35 +145,28 @@
return 0;
}
-void analogout_write(dac_t *obj, float value)
-{
+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, (uint16_t)DAC_RANGE); // Max value
+ dac_write(obj, (int)DAC_RANGE); // Max value
} 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)
-{
- if (value > (uint16_t)DAC_RANGE) {
- dac_write(obj, (uint16_t)DAC_RANGE); // Max value
- } else {
- dac_write(obj, value);
- }
+void analogout_write_u16(dac_t *obj, uint16_t value) {
+ dac_write(obj, value >> (16 - DAC_NB_BITS));
}
-float analogout_read(dac_t *obj)
-{
+float analogout_read(dac_t *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)
-{
- return (uint16_t)dac_read(obj);
+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
