mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Files at this revision

API Documentation at this revision

Comitter:
mbed_official
Date:
Mon Oct 26 09:00:10 2015 +0000
Parent:
7:cf567a118ec7
Child:
9:673ec039aeb3
Commit message:
Synchronized with git revision 22ef4124357e8536842581c7afd8e0c2c7858d6f

Full URL: https://github.com/mbedmicro/mbed/commit/22ef4124357e8536842581c7afd8e0c2c7858d6f/

STM32xx -Fix analog_out issue with 12 to 16bits conversion

Changed in this revision

targets/cmsis/TARGET_Freescale/TARGET_K22F/cmsis_nvic.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/us_ticker.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_Freescale/TARGET_K20XX/us_ticker.c Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_STM32F0/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_STM32F3/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_STM32F4/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_STM32F7/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_STM32L0/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_STM32L1/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
targets/hal/TARGET_STM/TARGET_STM32L4/analogout_api.c Show annotated file Show diff for this revision Revisions of this file
--- a/targets/cmsis/TARGET_Freescale/TARGET_K22F/cmsis_nvic.c	Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/cmsis/TARGET_Freescale/TARGET_K22F/cmsis_nvic.c	Mon Oct 26 09:00:10 2015 +0000
@@ -31,14 +31,13 @@
 #include "cmsis_nvic.h"
 
 #define NVIC_RAM_VECTOR_ADDRESS (0x1FFF0000)  // Vectors positioned at start of RAM
-#define NVIC_FLASH_VECTOR_ADDRESS (0x0)       // Initial vector position in flash
 
 void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
     uint32_t *vectors = (uint32_t*)SCB->VTOR;
     uint32_t i;
 
     // Copy and switch to dynamic vectors if the first time called
-    if (SCB->VTOR == NVIC_FLASH_VECTOR_ADDRESS) {
+    if (SCB->VTOR < NVIC_RAM_VECTOR_ADDRESS) {
         uint32_t *old_vectors = vectors;
         vectors = (uint32_t*)NVIC_RAM_VECTOR_ADDRESS;
         for (i=0; i<NVIC_NUM_VECTORS; i++) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/hal/TARGET_Freescale/TARGET_K20XX/TARGET_K20D50M/us_ticker.c	Mon Oct 26 09:00:10 2015 +0000
@@ -0,0 +1,161 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stddef.h>
+#include "us_ticker_api.h"
+#include "PeripheralNames.h"
+#include "clk_freqs.h"
+
+#define PIT_TIMER           PIT->CHANNEL[0]
+#define PIT_TIMER_IRQ       PIT0_IRQn
+#define PIT_TICKER          PIT->CHANNEL[1]
+#define PIT_TICKER_IRQ      PIT1_IRQn
+
+static void timer_init(void);
+static void ticker_init(void);
+
+
+static int us_ticker_inited = 0;
+static uint32_t clk_mhz;
+
+void us_ticker_init(void) {
+    if (us_ticker_inited)
+        return;
+    us_ticker_inited = 1;
+    
+    SIM->SCGC6 |= SIM_SCGC6_PIT_MASK;   // Clock PIT
+    PIT->MCR = 0;                       // Enable PIT
+    
+    clk_mhz = bus_frequency() / 1000000;
+
+    timer_init();
+    ticker_init();
+}
+
+/******************************************************************************
+ * Timer for us timing.
+ *
+ * The K20D5M does not have a prescaler on its PIT timer nor the option
+ * to chain timers, which is why a software timer is required to get 32-bit
+ * word length.
+ ******************************************************************************/
+static volatile uint32_t msb_counter = 0;
+static uint32_t timer_ldval = 0;
+
+static void timer_isr(void) {
+    if (PIT_TIMER.TFLG == 1) {
+        msb_counter++;
+        PIT_TIMER.TFLG = 1;
+    }
+}
+
+static void timer_init(void) {  
+    //CLZ counts the leading zeros, returning number of bits not used by clk_mhz
+    timer_ldval = clk_mhz << __CLZ(clk_mhz);
+
+    PIT_TIMER.LDVAL = timer_ldval;  // 1us
+    PIT_TIMER.TCTRL |= PIT_TCTRL_TIE_MASK;
+    PIT_TIMER.TCTRL |= PIT_TCTRL_TEN_MASK;  // Start timer 0
+
+    NVIC_SetVector(PIT_TIMER_IRQ, (uint32_t)timer_isr);
+    NVIC_EnableIRQ(PIT_TIMER_IRQ);
+}
+
+uint32_t us_ticker_read() {
+    if (!us_ticker_inited)
+        us_ticker_init();
+        
+    uint32_t retval;
+    __disable_irq(); 
+    retval = (timer_ldval - PIT_TIMER.CVAL) / clk_mhz; //Hardware bits
+    retval |= msb_counter << __CLZ(clk_mhz);           //Software bits
+    
+    if (PIT_TIMER.TFLG == 1) {                         //If overflow bit is set, force it to be handled
+        timer_isr();                                   //Handle IRQ, read again to make sure software/hardware bits are synced
+        NVIC_ClearPendingIRQ(PIT_TIMER_IRQ);
+        return us_ticker_read();
+    }
+
+    __enable_irq();
+    return retval;
+}
+
+/******************************************************************************
+ * Timer Event
+ *
+ * It schedules interrupts at given (32bit)us interval of time.
+ * It is implemented using PIT channel 1, since no prescaler is available,
+ * some bits are implemented in software.
+ ******************************************************************************/
+static void ticker_isr(void);
+
+static void ticker_init(void) {
+    /* Set interrupt handler */
+    NVIC_SetVector(PIT_TICKER_IRQ, (uint32_t)ticker_isr);
+    NVIC_EnableIRQ(PIT_TICKER_IRQ);
+}
+
+void us_ticker_disable_interrupt(void) {
+    PIT_TICKER.TCTRL &= ~PIT_TCTRL_TIE_MASK;
+}
+
+void us_ticker_clear_interrupt(void) {
+    // we already clear interrupt in lptmr_isr
+}
+
+static uint32_t us_ticker_int_counter = 0;
+
+inline static void ticker_set(uint32_t count) {
+    PIT_TICKER.TCTRL = 0;
+    PIT_TICKER.LDVAL = count;
+    PIT_TICKER.TCTRL = PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK;
+}
+
+static void ticker_isr(void) {
+    // Clear IRQ flag
+    PIT_TICKER.TFLG = 1;
+
+    if (us_ticker_int_counter > 0) {
+        ticker_set(0xFFFFFFFF);
+        us_ticker_int_counter--;
+    } else {
+        // This function is going to disable the interrupts if there are
+        // no other events in the queue
+        us_ticker_irq_handler();
+    }
+}
+
+void us_ticker_set_interrupt(timestamp_t timestamp) {
+    int delta = (int)((uint32_t)timestamp - us_ticker_read());
+    if (delta <= 0) {
+        // This event was in the past:
+        us_ticker_irq_handler();
+        return;
+    }
+
+    //Calculate how much falls outside the 32-bit after multiplying with clk_mhz
+    //We shift twice 16-bit to keep everything within the 32-bit variable
+    us_ticker_int_counter = (uint32_t)(delta >> 16);
+    us_ticker_int_counter *= clk_mhz;
+    us_ticker_int_counter >>= 16;
+    
+    uint32_t us_ticker_int_remainder = (uint32_t)delta * clk_mhz;
+    if (us_ticker_int_remainder == 0) {
+        ticker_set(0xFFFFFFFF);
+        us_ticker_int_counter--;
+    } else {
+        ticker_set(us_ticker_int_remainder);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/targets/hal/TARGET_Freescale/TARGET_K20XX/TARGET_TEENSY3_1/us_ticker.c	Mon Oct 26 09:00:10 2015 +0000
@@ -0,0 +1,84 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2006-2015 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <stddef.h>
+#include "us_ticker_api.h"
+#include "PeripheralNames.h"
+#include "clk_freqs.h"
+
+static int us_ticker_inited = 0;
+
+void us_ticker_init(void) {
+    if (us_ticker_inited)
+        return;
+    us_ticker_inited = 1;
+    
+    SIM->SCGC6 |= SIM_SCGC6_PIT_MASK;   // Clock PIT
+    PIT->MCR = 0;                       // Enable PIT
+    
+    //Timer on PIT0+1, ticker on PIT 2+3
+    //Init timer
+    PIT->CHANNEL[1].LDVAL = 0xFFFFFFFF;
+    PIT->CHANNEL[1].TCTRL = PIT_TCTRL_CHN_MASK | PIT_TCTRL_TEN_MASK;   // Start timer 1, chained to timer 0
+    
+    // Use channel 0 as a prescaler for channel 1
+    uint32_t ldval = (bus_frequency() + 500000) / 1000000 - 1;
+    PIT->CHANNEL[0].LDVAL = ldval;
+    PIT->CHANNEL[0].TCTRL = PIT_TCTRL_TEN_MASK;    // Start timer
+    
+    //Init ticker
+    PIT->CHANNEL[2].LDVAL = ldval;
+    PIT->CHANNEL[2].TCTRL = PIT_TCTRL_TEN_MASK;    // Start timer 2 as prescaler
+    
+    NVIC_SetVector(PIT3_IRQn, (uint32_t)us_ticker_irq_handler);
+    NVIC_EnableIRQ(PIT3_IRQn);
+}
+
+/******************************************************************************
+ * Timer for us timing.
+ ******************************************************************************/
+
+uint32_t us_ticker_read() {
+    if (!us_ticker_inited)
+        us_ticker_init();
+   
+   return ~(PIT->CHANNEL[1].CVAL);
+}
+
+/******************************************************************************
+ * Ticker Event
+ ******************************************************************************/
+
+void us_ticker_disable_interrupt(void) {
+    PIT->CHANNEL[3].TCTRL &= ~PIT_TCTRL_TIE_MASK;
+}
+
+void us_ticker_clear_interrupt(void) {
+    PIT->CHANNEL[3].TFLG = 1;
+}
+
+void us_ticker_set_interrupt(timestamp_t timestamp) {
+    int delta = (int)((uint32_t)timestamp - us_ticker_read());
+    if (delta <= 0) {
+        // This event was in the past:
+        us_ticker_irq_handler();
+        return;
+    }
+    
+    PIT->CHANNEL[3].TCTRL = 0;
+    PIT->CHANNEL[3].LDVAL = delta;
+    PIT->CHANNEL[3].TCTRL = PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK | PIT_TCTRL_CHN_MASK;
+    
+}
--- a/targets/hal/TARGET_Freescale/TARGET_K20XX/us_ticker.c	Fri Oct 16 07:45:35 2015 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,159 +0,0 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2015 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-#include <stddef.h>
-#include "us_ticker_api.h"
-#include "PeripheralNames.h"
-#include "clk_freqs.h"
-
-#define PIT_TIMER           PIT->CHANNEL[0]
-#define PIT_TIMER_IRQ       PIT0_IRQn
-#define PIT_TICKER          PIT->CHANNEL[1]
-#define PIT_TICKER_IRQ      PIT1_IRQn
-
-static void timer_init(void);
-static void ticker_init(void);
-
-
-static int us_ticker_inited = 0;
-static uint32_t clk_mhz;
-
-void us_ticker_init(void) {
-    if (us_ticker_inited)
-        return;
-    us_ticker_inited = 1;
-    
-    SIM->SCGC6 |= SIM_SCGC6_PIT_MASK;   // Clock PIT
-    PIT->MCR = 0;                       // Enable PIT
-    
-    clk_mhz = bus_frequency() / 1000000;
-
-    timer_init();
-    ticker_init();
-}
-
-/******************************************************************************
- * Timer for us timing.
- *
- * The K20D5M does not have a prescaler on its PIT timer nor the option
- * to chain timers, which is why a software timer is required to get 32-bit
- * word length.
- ******************************************************************************/
-static volatile uint32_t msb_counter = 0;
-static uint32_t timer_ldval = 0;
-
-static void timer_isr(void) {
-    msb_counter++;
-    PIT_TIMER.TFLG = 1;
-}
-
-static void timer_init(void) {  
-    //CLZ counts the leading zeros, returning number of bits not used by clk_mhz
-    timer_ldval = clk_mhz << __CLZ(clk_mhz);
-
-    PIT_TIMER.LDVAL = timer_ldval;  // 1us
-    PIT_TIMER.TCTRL |= PIT_TCTRL_TIE_MASK;
-    PIT_TIMER.TCTRL |= PIT_TCTRL_TEN_MASK;  // Start timer 0
-
-    NVIC_SetVector(PIT_TIMER_IRQ, (uint32_t)timer_isr);
-    NVIC_EnableIRQ(PIT_TIMER_IRQ);
-}
-
-uint32_t us_ticker_read() {
-    if (!us_ticker_inited)
-        us_ticker_init();
-        
-    uint32_t retval;
-    __disable_irq(); 
-    retval = (timer_ldval - PIT_TIMER.CVAL) / clk_mhz; //Hardware bits
-    retval |= msb_counter << __CLZ(clk_mhz);           //Software bits
-    
-    if (PIT_TIMER.TFLG == 1) {                         //If overflow bit is set, force it to be handled
-        timer_isr();                                   //Handle IRQ, read again to make sure software/hardware bits are synced
-        NVIC_ClearPendingIRQ(PIT_TIMER_IRQ);
-        return us_ticker_read();
-    }
-
-    __enable_irq();
-    return retval;
-}
-
-/******************************************************************************
- * Timer Event
- *
- * It schedules interrupts at given (32bit)us interval of time.
- * It is implemented using PIT channel 1, since no prescaler is available,
- * some bits are implemented in software.
- ******************************************************************************/
-static void ticker_isr(void);
-
-static void ticker_init(void) {
-    /* Set interrupt handler */
-    NVIC_SetVector(PIT_TICKER_IRQ, (uint32_t)ticker_isr);
-    NVIC_EnableIRQ(PIT_TICKER_IRQ);
-}
-
-void us_ticker_disable_interrupt(void) {
-    PIT_TICKER.TCTRL &= ~PIT_TCTRL_TIE_MASK;
-}
-
-void us_ticker_clear_interrupt(void) {
-    // we already clear interrupt in lptmr_isr
-}
-
-static uint32_t us_ticker_int_counter = 0;
-
-inline static void ticker_set(uint32_t count) {
-    PIT_TICKER.TCTRL = 0;
-    PIT_TICKER.LDVAL = count;
-    PIT_TICKER.TCTRL = PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK;
-}
-
-static void ticker_isr(void) {
-    // Clear IRQ flag
-    PIT_TICKER.TFLG = 1;
-
-    if (us_ticker_int_counter > 0) {
-        ticker_set(0xFFFFFFFF);
-        us_ticker_int_counter--;
-    } else {
-        // This function is going to disable the interrupts if there are
-        // no other events in the queue
-        us_ticker_irq_handler();
-    }
-}
-
-void us_ticker_set_interrupt(timestamp_t timestamp) {
-    int delta = (int)((uint32_t)timestamp - us_ticker_read());
-    if (delta <= 0) {
-        // This event was in the past:
-        us_ticker_irq_handler();
-        return;
-    }
-
-    //Calculate how much falls outside the 32-bit after multiplying with clk_mhz
-    //We shift twice 16-bit to keep everything within the 32-bit variable
-    us_ticker_int_counter = (uint32_t)(delta >> 16);
-    us_ticker_int_counter *= clk_mhz;
-    us_ticker_int_counter >>= 16;
-    
-    uint32_t us_ticker_int_remainder = (uint32_t)delta * clk_mhz;
-    if (us_ticker_int_remainder == 0) {
-        ticker_set(0xFFFFFFFF);
-        us_ticker_int_counter--;
-    } else {
-        ticker_set(us_ticker_int_remainder);
-    }
-}
--- a/targets/hal/TARGET_STM/TARGET_STM32F0/analogout_api.c	Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32F0/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;
 
@@ -80,12 +81,12 @@
     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) {
-        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);
-    } else { // PA_5
-        HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, value);
+    } else if (obj->pin == PA_5) {
+        HAL_DAC_SetValue(&DacHandle, DAC_CHANNEL_2, DAC_ALIGN_12B_R, (value & DAC_RANGE));
         HAL_DAC_Start(&DacHandle, DAC_CHANNEL_2);
     }
 }
@@ -93,36 +94,34 @@
 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
+    } else if (obj->pin == PA_5) {
         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, (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);
-    }
+    dac_write(obj, value >> (16 - DAC_NB_BITS));
 }
 
 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);
+    uint32_t value = dac_read(obj);
+    return (value << 4) | ((value >> 8) & 0x000F); // Conversion from 12 to 16 bits
 }
 
 #endif // DEVICE_ANALOGOUT
--- 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
--- a/targets/hal/TARGET_STM/TARGET_STM32F4/analogout_api.c	Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32F4/analogout_api.c	Mon Oct 26 09:00:10 2015 +0000
@@ -35,13 +35,13 @@
 #include "stm32f4xx_hal.h"
 #include "PeripheralPins.h"
 
-#define RANGE_12BIT (0xFFF)
+#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)
-{
+void analogout_init(dac_t *obj, PinName pin) {
     uint32_t channel ;
     HAL_StatusTypeDef status;
 
@@ -94,18 +94,16 @@
 
 }
 
-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;
 
     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) {
-        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 ) {
@@ -113,46 +111,37 @@
     }
 }
 
-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);
     } else if (obj->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) {
         dac_write(obj, 0); // Min value
     } else if (value > 1.0f) {
-        dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
+        dac_write(obj, (int)DAC_RANGE); // Max value
     } 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)
-{
-    if (value > (uint16_t)RANGE_12BIT) {
-        value = (uint16_t)RANGE_12BIT; // Max value
-    }
-
-    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)value * (1.0f / (float)RANGE_12BIT);
+    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
--- a/targets/hal/TARGET_STM/TARGET_STM32F7/analogout_api.c	Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32F7/analogout_api.c	Mon Oct 26 09:00:10 2015 +0000
@@ -35,13 +35,13 @@
 #include "stm32f7xx_hal.h"
 #include "PeripheralPins.h"
 
-#define RANGE_12BIT (0xFFF)
+#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)
-{
+void analogout_init(dac_t *obj, PinName pin) {
     uint32_t channel ;
     HAL_StatusTypeDef status;
 
@@ -94,18 +94,16 @@
 
 }
 
-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;
 
     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) {
-        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) {
@@ -113,8 +111,7 @@
     }
 }
 
-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);
     } else if (obj->channel == 2) {
@@ -123,36 +120,28 @@
     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) {
         dac_write(obj, 0); // Min value
     } else if (value > 1.0f) {
-        dac_write(obj, (uint16_t)RANGE_12BIT); // Max value
+        dac_write(obj, (int)DAC_RANGE); // Max value
     } 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)
-{
-    if (value > (uint16_t)RANGE_12BIT) {
-        value = (uint16_t)RANGE_12BIT; // Max value
-    }
-
-    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)value * (1.0f / (float)RANGE_12BIT);
+    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
--- a/targets/hal/TARGET_STM/TARGET_STM32L0/analogout_api.c	Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32L0/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 channel1_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;
 
     // Get the peripheral name from the pin and assign it to the object
@@ -91,8 +91,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->channel == 1) channel1_used = 0;
     if (obj->channel == 2) channel2_used = 0;
@@ -107,22 +106,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);
     }
@@ -134,35 +131,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
--- a/targets/hal/TARGET_STM/TARGET_STM32L1/analogout_api.c	Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32L1/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;
 
     DacHandle.Instance = DAC;
@@ -77,8 +77,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;
@@ -92,19 +91,17 @@
     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) {
-        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);
     } 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);
     }
 }
 
-static inline int dac_read(dac_t *obj)
-{
+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
@@ -112,35 +109,28 @@
     }
 }
 
-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
--- a/targets/hal/TARGET_STM/TARGET_STM32L4/analogout_api.c	Fri Oct 16 07:45:35 2015 +0100
+++ b/targets/hal/TARGET_STM/TARGET_STM32L4/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 channel1_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};
 
     // Get the peripheral name from the pin and assign it to the object
@@ -94,8 +94,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->channel == 1) channel1_used = 0;
     if (obj->channel == 2) channel2_used = 0;
@@ -110,20 +109,18 @@
     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 (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);
     }
 }
 
-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);
     }
@@ -133,35 +130,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