mbed library sources. Supersedes mbed-src. Fixes analogIn and analogOut problems for TARGET_STM32F3. Tested on NUCLEO-F303K8, using 3 analogout and 7 analogin channels simultaneously. Added ability for STM32F334R8 and STM32F303K8 to use all three channels of DAC simultaneously. https://developer.mbed.org/users/StevieWray/code/mbed-dev/ Added ability for TARGET_STM32F3 to use more than one ADC simultaneously. https://developer.mbed.org/questions/67997/NUCLEO-F303K8ADC/
Fork of mbed-dev by
Diff: targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c
- Revision:
- 8:69ce7aaad4c4
- Parent:
- 0:9b334a45a8ff
- Child:
- 70:b3a5af880266
--- 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