mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Files at this revision

API Documentation at this revision

Comitter:
StevieWray
Date:
Wed Sep 28 08:45:18 2016 +0000
Parent:
147:30b64687e01f
Commit message:
Fixed DAC output on STM32F3 boards to allow 3 outputs

Changed in this revision

targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
--- a/targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c	Fri Sep 16 16:24:25 2016 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c	Wed Sep 28 08:45:18 2016 +0000
@@ -27,41 +27,49 @@
  */
 #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 DAC_HandleTypeDef DacHandle;
-
+ 
+//the STM32F334R8 has two DACs so define two different handles and a pointer to pick between them in each function
+static DAC_HandleTypeDef DacHandle1; 
+static DAC_HandleTypeDef DacHandle2;
+DAC_HandleTypeDef* DacHandle;
+ 
 // These variables are used for the "free" function
 static int pa4_used = 0;
 static int pa5_used = 0;
-
+ 
 void analogout_init(dac_t *obj, PinName pin) {
+    if(pin==PA_6) //if the pin being used is PA_6 then it is DAC2 otherwise it's DAC1
+        DacHandle = &DacHandle2;
+    else
+        DacHandle = &DacHandle1;
+        
     DAC_ChannelConfTypeDef sConfig;
-
+ 
     // Get the peripheral name 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);
     MBED_ASSERT(function != (uint32_t)NC);
     obj->channel = STM_PIN_CHANNEL(function);
-
+ 
     // Configure GPIO
     pinmap_pinout(pin, PinMap_DAC);
-
+ 
     // Save the pin for future use
     obj->pin = pin;
-
+ 
     // Enable DAC clock
     if (obj->dac == DAC_1) {
         __DAC1_CLK_ENABLE();
@@ -71,43 +79,43 @@
         __DAC2_CLK_ENABLE();
     }
 #endif
-
+ 
     // Configure DAC
-    DacHandle.Instance = (DAC_TypeDef *)(obj->dac);
-
+    DacHandle->Instance = (DAC_TypeDef *)(obj->dac);
+ 
     sConfig.DAC_Trigger      = DAC_TRIGGER_NONE;
     sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
-
+ 
     if (pin == PA_4) {
-        HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
+        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);
+        HAL_DAC_ConfigChannel(DacHandle, &sConfig, DAC_CHANNEL_2);
         pa5_used = 1;
     }
 #endif
-
+ 
     if (pin == PA_6) {
-        HAL_DAC_ConfigChannel(&DacHandle, &sConfig, DAC_CHANNEL_1);
+        HAL_DAC_ConfigChannel(DacHandle, &sConfig, DAC_CHANNEL_1);
     }
-
+ 
     analogout_write_u16(obj, 0);
 }
-
+ 
 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;
-
+ 
     if ((pa4_used == 0) && (pa5_used == 0)) {
         __DAC1_FORCE_RESET();
         __DAC1_RELEASE_RESET();
         __DAC1_CLK_DISABLE();
     }
-
+ 
 #if defined(DAC2)
     if (obj->pin == PA_6) {
         __DAC2_FORCE_RESET();
@@ -115,36 +123,48 @@
         __DAC2_CLK_DISABLE();
     }
 #endif
-
+ 
     // Configure GPIO
     pin_function(obj->pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
 }
-
+ 
 static inline void dac_write(dac_t *obj, int value) {
+    //if the pin being used is PA_6 then it is DAC2 otherwise it's DAC1
+    if(obj->pin==PA_6)
+        DacHandle = &DacHandle2;
+    else
+        DacHandle = &DacHandle1;
+        
     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);
+        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);
+        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 the pin being used is PA_6 then it is DAC2 otherwise it's DAC1
+    if(obj->pin==PA_6)
+        DacHandle = &DacHandle2;
+    else
+        DacHandle = &DacHandle1;
+        
     if (obj->channel == 1) {
-        return (int)HAL_DAC_GetValue(&DacHandle, DAC_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);
+        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
@@ -154,19 +174,20 @@
         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
+ 
\ No newline at end of file