aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple
diff options
context:
space:
mode:
Diffstat (limited to 'libmaple')
-rw-r--r--libmaple/dac.c28
-rw-r--r--libmaple/dac.h31
2 files changed, 39 insertions, 20 deletions
diff --git a/libmaple/dac.c b/libmaple/dac.c
index 0f44bb4..1726d19 100644
--- a/libmaple/dac.c
+++ b/libmaple/dac.c
@@ -39,47 +39,50 @@ const dac_dev *DAC = &dac;
/**
* @brief Initialize the digital to analog converter
+ * @param dev DAC device
* @param flags Flags:
* DAC_CH1: Enable channel 1
* DAC_CH2: Enable channel 2
* @sideeffect May set PA4 or PA5 to INPUT_ANALOG
*/
-void dac_init(uint32 flags) {
+void dac_init(const dac_dev *dev, uint32 flags) {
/* First turn on the clock */
rcc_clk_enable(RCC_DAC);
rcc_reset_dev(RCC_DAC);
if (flags & DAC_CH1) {
- dac_enable_channel(1);
+ dac_enable_channel(dev, 1);
}
if (flags & DAC_CH2) {
- dac_enable_channel(2);
+ dac_enable_channel(dev, 2);
}
}
/**
* @brief Write a 12-bit value to the DAC to output
+ * @param dev DAC device
* @param channel channel to select (1 or 2)
* @param val value to write
*/
-void dac_write_channel(uint8 channel, uint16 val) {
+void dac_write_channel(const dac_dev *dev, uint8 channel, uint16 val) {
switch(channel) {
case 1:
- DAC->regs->DHR12R1 = DAC_DHR12R1_DACC1DHR & val;
+ dev->regs->DHR12R1 = DAC_DHR12R1_DACC1DHR & val;
break;
case 2:
- DAC->regs->DHR12R2 = DAC_DHR12R2_DACC2DHR & val;
+ dev->regs->DHR12R2 = DAC_DHR12R2_DACC2DHR & val;
break;
}
}
/**
* @brief Enable a DAC channel
+ * @param dev DAC device
* @param channel channel to enable, either 1 or 2
* @sideeffect May change pin mode of PA4 or PA5
*/
-void dac_enable_channel(uint8 channel) {
+void dac_enable_channel(const dac_dev *dev, uint8 channel) {
/*
* Setup ANALOG mode on PA4 and PA5. This mapping is consistent across
* all STM32 chips with a DAC. See RM0008 12.2.
@@ -87,26 +90,27 @@ void dac_enable_channel(uint8 channel) {
switch (channel) {
case 1:
gpio_set_mode(GPIOA, 4, GPIO_INPUT_ANALOG);
- DAC->regs->CR |= DAC_CR_EN1;
+ dev->regs->CR |= DAC_CR_EN1;
break;
case 2:
gpio_set_mode(GPIOA, 5, GPIO_INPUT_ANALOG);
- DAC->regs->CR |= DAC_CR_EN2;
+ dev->regs->CR |= DAC_CR_EN2;
break;
}
}
/**
* @brief Disable a DAC channel
+ * @param dev DAC device
* @param channel channel to disable, either 1 or 2
*/
-void dac_disable_channel(uint8 channel) {
+void dac_disable_channel(const dac_dev *dev, uint8 channel) {
switch (channel) {
case 1:
- DAC->regs->CR &= ~DAC_CR_EN1;
+ dev->regs->CR &= ~DAC_CR_EN1;
break;
case 2:
- DAC->regs->CR &= ~DAC_CR_EN2;
+ dev->regs->CR &= ~DAC_CR_EN2;
break;
}
}
diff --git a/libmaple/dac.h b/libmaple/dac.h
index c897bb2..cf4fe85 100644
--- a/libmaple/dac.h
+++ b/libmaple/dac.h
@@ -38,6 +38,10 @@
extern "C"{
#endif
+/*
+ * Register maps
+ */
+
/** DAC register map. */
typedef struct dac_reg_map {
__io uint32 CR; /**< Control register */
@@ -64,6 +68,13 @@ typedef struct dac_reg_map {
__io uint32 DOR2; /**< Channel 2 data output register */
} dac_reg_map;
+/** DAC register map base address */
+#define DAC_BASE ((dac_reg_map*)0x40007400)
+
+/*
+ * Devices
+ */
+
/** DAC device type. */
typedef struct dac_dev {
dac_reg_map *regs; /**< Register map */
@@ -72,11 +83,8 @@ typedef struct dac_dev {
/** DAC device. */
extern const dac_dev *DAC;
-/** DAC register map base address */
-#define DAC_BASE ((dac_reg_map*)0x40007400)
-
/*
- * Register bit definitions and masks
+ * Register bit definitions
*/
/* Control register */
@@ -137,13 +145,20 @@ extern const dac_dev *DAC;
/* Channel 1 data output register */
#define DAC_DOR2_DACC2DOR 0x00000FFF
+/*
+ * Convenience functions
+ */
+
+/* We take the dev argument in these convenience functions for
+ * future-proofing */
+
#define DAC_CH1 0x1
#define DAC_CH2 0x2
-void dac_init(uint32 flags);
+void dac_init(const dac_dev *dev, uint32 flags);
-void dac_write_channel(uint8 channel, uint16 val);
-void dac_enable_channel(uint8 channel);
-void dac_disable_channel(uint8 channel);
+void dac_write_channel(const dac_dev *dev, uint8 channel, uint16 val);
+void dac_enable_channel(const dac_dev *dev, uint8 channel);
+void dac_disable_channel(const dac_dev *dev, uint8 channel);
#ifdef __cplusplus
} // extern "C"