[NUCLEO_F302R8] Implement analogout_free function

This code was missing + make some typo corrections.
pull/260/merge
bcostm 2014-04-11 08:47:56 +02:00 committed by Bogdan Marinescu
parent efbbb2f307
commit 4a1946ae8d
1 changed files with 13 additions and 7 deletions

View File

@ -33,7 +33,7 @@
#include "pinmap.h" #include "pinmap.h"
#include "error.h" #include "error.h"
#define RANGE_12BIT (0xFFF) #define DAC_RANGE (0xFFF) // 12 bits
static const PinMap PinMap_DAC[] = { static const PinMap PinMap_DAC[] = {
{PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1 {PA_4, DAC_1, STM_PIN_DATA(GPIO_Mode_AN, GPIO_OType_PP, GPIO_PuPd_NOPULL, 0xFF)}, // DAC_OUT1
@ -56,7 +56,7 @@ void analogout_init(dac_t *obj, PinName pin) {
// Configure GPIO // Configure GPIO
pinmap_pinout(pin, PinMap_DAC); pinmap_pinout(pin, PinMap_DAC);
// Save the channel for the write and read functions // Save the channel for future use
obj->channel = pin; obj->channel = pin;
// Enable DAC clock // Enable DAC clock
@ -71,6 +71,12 @@ void analogout_init(dac_t *obj, PinName pin) {
} }
void analogout_free(dac_t *obj) { void analogout_free(dac_t *obj) {
DAC_TypeDef *dac = (DAC_TypeDef *)(obj->dac);
// Disable DAC
DAC_DeInit(dac);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, DISABLE);
// Configure GPIO
pin_function(obj->channel, STM_PIN_DATA(GPIO_Mode_IN, 0, GPIO_PuPd_NOPULL, 0xFF));
} }
static inline void dac_write(dac_t *obj, uint16_t value) { static inline void dac_write(dac_t *obj, uint16_t value) {
@ -87,15 +93,15 @@ void analogout_write(dac_t *obj, float value) {
if (value < 0.0f) { if (value < 0.0f) {
dac_write(obj, 0); // Min value dac_write(obj, 0); // Min value
} else if (value > 1.0f) { } else if (value > 1.0f) {
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value dac_write(obj, (uint16_t)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, (uint16_t)(value * (float)RANGE_12BIT)); dac_write(obj, (uint16_t)(value * (float)DAC_RANGE));
} }
} }
void analogout_write_u16(dac_t *obj, uint16_t value) { void analogout_write_u16(dac_t *obj, uint16_t value) {
if (value > (uint16_t)RANGE_12BIT) { if (value > (uint16_t)DAC_RANGE) {
dac_write(obj, (uint16_t)RANGE_12BIT); // Max value dac_write(obj, (uint16_t)DAC_RANGE); // Max value
} else { } else {
dac_write(obj, value); dac_write(obj, value);
} }
@ -103,7 +109,7 @@ void analogout_write_u16(dac_t *obj, uint16_t value) {
float analogout_read(dac_t *obj) { float analogout_read(dac_t *obj) {
uint32_t value = dac_read(obj); uint32_t value = dac_read(obj);
return (float)value * (1.0f / (float)RANGE_12BIT); return (float)((float)value * (1.0f / (float)DAC_RANGE));
} }
uint16_t analogout_read_u16(dac_t *obj) { uint16_t analogout_read_u16(dac_t *obj) {