Merge branch 'master' of https://github.com/mbedmicro/mbed into dev_disco_f469ni

pull/1392/head
dbestm 2015-10-26 10:14:58 +01:00
commit 8c549f9b9a
10 changed files with 207 additions and 185 deletions

View File

@ -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++) {

View File

@ -55,8 +55,10 @@ static volatile uint32_t msb_counter = 0;
static uint32_t timer_ldval = 0;
static void timer_isr(void) {
msb_counter++;
PIT_TIMER.TFLG = 1;
if (PIT_TIMER.TFLG == 1) {
msb_counter++;
PIT_TIMER.TFLG = 1;
}
}
static void timer_init(void) {

View File

@ -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;
}

View File

@ -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 @@ void analogout_free(dac_t *obj) {
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 void dac_write(dac_t *obj, uint16_t value) {
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

View File

@ -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 DAC_HandleTypeDef DacHandle;
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 @@ void analogout_init(dac_t *obj, PinName pin)
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 @@ void analogout_free(dac_t *obj)
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 @@ static inline int dac_read(dac_t *obj)
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

View File

@ -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_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;
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 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) {
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

View File

@ -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_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;
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 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) {
return (int)HAL_DAC_GetValue(&DacHandle, DAC_CHANNEL_1);
} else if (obj->channel == 2) {
@ -123,36 +120,28 @@ static inline int dac_read(dac_t *obj)
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

View File

@ -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 DAC_HandleTypeDef DacHandle;
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 @@ void analogout_init(dac_t *obj, PinName pin)
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 @@ void analogout_free(dac_t *obj)
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 @@ static inline int dac_read(dac_t *obj)
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

View File

@ -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 DAC_HandleTypeDef DacHandle;
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 @@ void analogout_init(dac_t *obj, PinName pin)
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 @@ void analogout_free(dac_t *obj)
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 @@ 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) {
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

View File

@ -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 DAC_HandleTypeDef DacHandle;
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 @@ void analogout_init(dac_t *obj, PinName pin)
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 @@ void analogout_free(dac_t *obj)
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 @@ static inline int dac_read(dac_t *obj)
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