From 0d440118bd4e1ba64f92d3636302fbddeac5bef6 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Fri, 18 Feb 2011 04:00:00 -0500 Subject: Basic DAC functionality (high-density devices only). --- examples/test-dac.cpp | 50 +++++++++++++++++++++----------------------------- libmaple/dac.c | 16 +++++++--------- libmaple/dac.h | 2 ++ libmaple/rcc.c | 2 +- notes/dac.txt | 24 +++++++++++++++++++----- 5 files changed, 50 insertions(+), 44 deletions(-) diff --git a/examples/test-dac.cpp b/examples/test-dac.cpp index 65496f4..3a699e2 100644 --- a/examples/test-dac.cpp +++ b/examples/test-dac.cpp @@ -5,49 +5,41 @@ #include "gpio.h" #include "dac.h" -#define LED_PIN 23 // hack for maple native -#define DISC_PIN 14 // hack for USB on native - -int toggle = 0; uint16 count = 0; void setup() { - pinMode(LED_PIN, OUTPUT); - pinMode(DISC_PIN, OUTPUT); - digitalWrite(DISC_PIN,1); - digitalWrite(LED_PIN,1); + pinMode(BOARD_LED_PIN, OUTPUT); + digitalWrite(BOARD_LED_PIN,1); - Serial1.begin(9600); - Serial1.println("Hello World!"); + Serial1.begin(9600); + Serial1.println("**** Beginning DAC test"); - Serial1.print("Init... "); - dac_init(); - Serial1.println("Done."); + Serial1.print("Init... "); + dac_init(); + Serial1.println("Done."); } void loop() { - digitalWrite(LED_PIN, toggle); - toggle ^= 1; - delay(100); - - count += 100; + toggleLED(); + delay(100); - if(count > 4095) { - count = 0; - } + count += 100; + if(count > 4095) { + count = 0; + } - dac_write(1, 2048); - dac_write(2, count); + dac_write(1, 2048); + dac_write(2, count); } int main(void) { - init(); - setup(); + init(); + setup(); - while (1) { - loop(); - } - return 0; + while (1) { + loop(); + } + return 0; } diff --git a/libmaple/dac.c b/libmaple/dac.c index 7f6101d..63a96ac 100644 --- a/libmaple/dac.c +++ b/libmaple/dac.c @@ -31,12 +31,11 @@ * @brief DAC peripheral routines. */ -/* Only one, so global to this file */ -DAC_Map *dac = (DAC_Map*)(DAC_BASE); - /* This numbering follows the registers (1-indexed) */ -#define DAC_CHA 1 -#define DAC_CHB 2 +#define DAC_CH1 1 +#define DAC_CH2 2 + +DAC_Map *dac = (DAC_Map*)(DAC_BASE); /* Sets up the DAC peripheral */ void dac_init(void) { @@ -49,16 +48,15 @@ void dac_init(void) { /* Then do register stuff. Default does no triggering, and * buffered output, so all good. */ - dac->CR |= DAC_CR_EN1; - dac->CR |= DAC_CR_EN2; + dac->CR = DAC_CR_EN1 | DAC_CR_EN2; } void dac_write(uint8 chan, uint16 val) { switch(chan) { - case DAC_CHA: + case DAC_CH1: dac->DHR12R1 = 0x0FFF & val; break; - case DAC_CHB: + case DAC_CH2: dac->DHR12R2 = 0x0FFF & val; break; default: diff --git a/libmaple/dac.h b/libmaple/dac.h index 9c84f2e..340a49a 100644 --- a/libmaple/dac.h +++ b/libmaple/dac.h @@ -55,6 +55,8 @@ typedef struct { volatile uint32 DOR2; } DAC_Map; +/* There's only one DAC, so expose it. */ +extern DAC_Map *dac; // And here are the register bit ranges #define DAC_CR_EN1 BIT(0) diff --git a/libmaple/rcc.c b/libmaple/rcc.c index 313eaf7..6905c22 100644 --- a/libmaple/rcc.c +++ b/libmaple/rcc.c @@ -70,7 +70,7 @@ static const struct rcc_dev_info rcc_dev_table[] = { [RCC_SPI1] = { .clk_domain = APB2, .line_num = 12 }, [RCC_SPI2] = { .clk_domain = APB1, .line_num = 14 }, [RCC_FSMC] = { .clk_domain = AHB, .line_num = 8 }, // High-density only - [RCC_DAC] = { .clk_domain = APB1, .line_num = 9 }, // High-density only + [RCC_DAC] = { .clk_domain = APB1, .line_num = 29 }, // High-density only [RCC_DMA1] = { .clk_domain = AHB, .line_num = 0 }, [RCC_DMA2] = { .clk_domain = AHB, .line_num = 1 }, // High-density only }; diff --git a/notes/dac.txt b/notes/dac.txt index 9df0782..d027402 100644 --- a/notes/dac.txt +++ b/notes/dac.txt @@ -13,11 +13,25 @@ control, and timer events. We'll just use software triggering for now. There is (obviously) DMA support for DAC output. -There are noise output and triangle wave output features with variable -amplitude. - -There are many additional modes to tigger output to both channels at the same -time. +There are noise (via LFSR) output and triangle wave output features +with variable amplitude. + +There are eleven modes to trigger output to both channels at the same +time, as follows: + + - Independent trigger: + (1) No wave generation + (2) Same LFSR + (3) Different LFSR + (4) Same triangle + (5) Different triangle + - (6) Simultaneous software start + - Simultaneous trigger: + (7) Without wave generation + (8) Same LFSR + (9) Different LFSR + (10) Same triangle + (11) Different triangle Buffering will be enabled by default. -- cgit v1.2.3