Reformat dac module to use tc. Clean up code.
[bertos.git] / bertos / cpu / cortex-m3 / drv / dac_sam3.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief DAC hardware-specific implementation
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  */
37
38 #include "dac_sam3.h"
39
40 #include "cfg/cfg_dac.h"
41
42 #include <cfg/macros.h>
43 #include <cfg/compiler.h>
44
45 // Define log settings for cfg/log.h.
46 #define LOG_LEVEL         DAC_LOG_LEVEL
47 #define LOG_FORMAT        DAC_LOG_FORMAT
48 #include <cfg/log.h>
49
50 #include <drv/dac.h>
51 #include <drv/irq_cm3.h>
52
53 #include <io/cm3.h>
54
55 #include <string.h>
56
57 struct DacHardware
58 {
59         uint16_t channels;
60         bool end;
61 };
62
63 struct DacHardware dac_hw;
64
65 #if CONFIG_DAC_TIMER == DACC_TRGSEL_TIO_CH0 /* Select Timer counter TIO Channel 0 */
66         #define DAC_TC_ID         TC0_ID
67         #define DAC_TC_CCR        TC0_CCR0
68         #define DAC_TC_IDR        TC0_IDR0
69         #define DAC_TC_CMR        TC0_CMR0
70         #define DAC_TC_SR         TC0_SR0
71         #define DAC_TC_RA         TC0_RA0
72         #define DAC_TC_RC         TC0_RC0
73 #elif CONFIG_DAC_TIMER == DACC_TRGSEL_TIO_CH1 /* Select Timer counter TIO Channel 1 */
74         #define DAC_TC_ID         TC1_ID
75         #define DAC_TC_CCR        TC0_CCR1
76         #define DAC_TC_IDR        TC0_IDR1
77         #define DAC_TC_CMR        TC0_CMR1
78         #define DAC_TC_SR         TC0_SR1
79         #define DAC_TC_RA         TC0_RA1
80         #define DAC_TC_RC         TC0_RC1
81 #elif CONFIG_DAC_TIMER == DACC_TRGSEL_TIO_CH2 /* Select Timer counter TIO Channel 2 */
82         #define DAC_TC_ID         TC2_ID
83         #define DAC_TC_CCR        TC0_CCR2
84         #define DAC_TC_IDR        TC0_IDR2
85         #define DAC_TC_CMR        TC0_CMR2
86         #define DAC_TC_SR         TC0_SR2
87         #define DAC_TC_RA         TC0_RA2
88         #define DAC_TC_RC         TC0_RC2
89 #elif CONFIG_DAC_TIMER == DACC_TRGSEL_PWM0 || CONFIG_DAC_TIMER == DACC_TRGSEL_PWM1
90         #error unimplemented pwm triger select.
91 #endif
92
93 INLINE void tc_setup(uint32_t freq)
94 {
95         pmc_periphEnable(DAC_TC_ID);
96
97     /*  Disable TC clock */
98     DAC_TC_CCR = TC_CCR_CLKDIS;
99     /*  Disable interrupts */
100     DAC_TC_IDR = 0xFFFFFFFF;
101     /*  Clear status register */
102     volatile uint32_t dummy = DAC_TC_SR;
103         (void)dummy;
104
105         /*
106          * Setup the timer counter:
107          * - select clock TCLK1
108          * - enable wave form mode
109          * - RA compare effect SET
110          * - RC compare effect CLEAR
111          * - UP mode with automatic trigger on RC Compare
112          */
113     DAC_TC_CMR = BV(TC_TIMER_CLOCK1) | BV(TC_CMR_WAVE) | TC_CMR_ACPA_SET | TC_CMR_ACPC_CLEAR | BV(TC_CMR_CPCTRG);
114
115         /* Compute the sample frequency */
116     uint32_t rc = (CPU_FREQ / 8) / (freq * 1000);
117     DAC_TC_RC = rc;
118     DAC_TC_RA = 50 * rc / 100;
119 }
120
121 INLINE void tc_start(void)
122 {
123     DAC_TC_CCR =  BV(TC_CCR_CLKEN)| BV(TC_CCR_SWTRG);
124 }
125
126 INLINE void tc_stop(void)
127 {
128     DAC_TC_CCR =  BV(TC_CCR_CLKDIS);
129 }
130
131 static int sam3x_dac_write(struct Dac *dac, unsigned channel, uint16_t sample)
132 {
133         (void)dac;
134
135         ASSERT(channel <= DAC_MAXCH);
136
137         DACC_MR |= (channel << DACC_USER_SEL_SHIFT) & DACC_USER_SEL_MASK;
138         DACC_CHER |= BV(channel);
139
140         DACC_CDR = sample ;
141
142         return 0;
143 }
144
145 static void sam3x_dac_setCh(struct Dac *dac, uint32_t mask)
146 {
147         /* we have only the ch0 and ch1 */
148         ASSERT(mask < BV(3));
149         dac->hw->channels = mask;
150 }
151
152 static void sam3x_dac_setSampleRate(struct Dac *dac, uint32_t rate)
153 {
154         (void)dac;
155
156         /* Eneble hw trigger */
157         DACC_MR |= BV(DACC_TRGEN) | (CONFIG_DAC_TIMER << DACC_TRGSEL_SHIFT);
158         kprintf("%08lx\n", DACC_MR);
159         tc_setup(rate);
160 }
161
162 static void sam3x_dac_conversion(struct Dac *dac, void *buf, size_t len)
163 {
164         if (dac->hw->channels & BV(DACC_CH0))
165                 DACC_MR |= (DACC_CH0 << DACC_USER_SEL_SHIFT) & DACC_USER_SEL_MASK;
166
167         if (dac->hw->channels & BV(DACC_CH1))
168                 DACC_MR |= (DACC_CH1 << DACC_USER_SEL_SHIFT) & DACC_USER_SEL_MASK;
169
170         DACC_CHER |= dac->hw->channels;
171
172         /* Start syncro timer for the dac */
173         tc_start();
174
175         /* Setup dma and start it */
176         DACC_TPR = (uint32_t)buf ;
177         DACC_TCR = len;
178         DACC_PTCR |= BV(DACC_PTCR_TXTEN);
179 }
180
181 static bool sam3x_dac_isFinished(struct Dac *dac)
182 {
183         return 0;
184 }
185
186 static void sam3x_dac_start(struct Dac *dac, void *buf, size_t len, size_t slicelen)
187 {
188 }
189
190 static void sam3x_dac_stop(struct Dac *dac)
191 {
192 }
193
194
195 void dac_init(struct Dac *dac)
196 {
197
198         /* Fill the virtual table */
199         dac->ctx.write = sam3x_dac_write;
200         dac->ctx.setCh = sam3x_dac_setCh;
201         dac->ctx.setSampleRate = sam3x_dac_setSampleRate;
202         dac->ctx.conversion = sam3x_dac_conversion;
203         dac->ctx.isFinished = sam3x_dac_isFinished;
204         dac->ctx.start = sam3x_dac_start;
205         dac->ctx.stop = sam3x_dac_stop;
206         dac->ctx._type = DAC_SAM3X;
207         dac->hw = &dac_hw;
208
209         /* Clock DAC peripheral */
210         pmc_periphEnable(DACC_ID);
211
212         /* Reset hw */
213         DACC_CR |= BV(DACC_SWRST);
214         DACC_MR = 0;
215
216         /* Configure the dac */
217         DACC_MR |= (CONFIG_DAC_REFRESH << DACC_REFRESH_SHIFT) & DACC_REFRESH_MASK;
218         DACC_MR |= (CONFIG_DAC_STARTUP << DACC_STARTUP_SHIFT) & DACC_STARTUP_MASK;
219 }