42e9e9a29d4a3c8bdb0fa2f8ba875314176fd64b
[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 <cpu/types.h>
54
55 #include <io/cm3.h>
56
57 #include <string.h>
58
59 struct DacHardware
60 {
61         uint16_t channels;
62         uint32_t rate;
63         bool end;
64 };
65
66 struct DacHardware dac_hw;
67
68 #if CONFIG_DAC_TIMER == DACC_TRGSEL_TIO_CH0 /* Select Timer counter TIO Channel 0 */
69         #define DAC_TC_ID         TC0_ID
70         #define DAC_TC_CCR        TC0_CCR0
71         #define DAC_TC_IDR        TC0_IDR0
72         #define DAC_TC_CMR        TC0_CMR0
73         #define DAC_TC_SR         TC0_SR0
74         #define DAC_TC_RA         TC0_RA0
75         #define DAC_TC_RC         TC0_RC0
76 #elif CONFIG_DAC_TIMER == DACC_TRGSEL_TIO_CH1 /* Select Timer counter TIO Channel 1 */
77         #define DAC_TC_ID         TC1_ID
78         #define DAC_TC_CCR        TC0_CCR1
79         #define DAC_TC_IDR        TC0_IDR1
80         #define DAC_TC_CMR        TC0_CMR1
81         #define DAC_TC_SR         TC0_SR1
82         #define DAC_TC_RA         TC0_RA1
83         #define DAC_TC_RC         TC0_RC1
84 #elif CONFIG_DAC_TIMER == DACC_TRGSEL_TIO_CH2 /* Select Timer counter TIO Channel 2 */
85         #define DAC_TC_ID         TC2_ID
86         #define DAC_TC_CCR        TC0_CCR2
87         #define DAC_TC_IDR        TC0_IDR2
88         #define DAC_TC_CMR        TC0_CMR2
89         #define DAC_TC_SR         TC0_SR2
90         #define DAC_TC_RA         TC0_RA2
91         #define DAC_TC_RC         TC0_RC2
92 #elif CONFIG_DAC_TIMER == DACC_TRGSEL_PWM0 || CONFIG_DAC_TIMER == DACC_TRGSEL_PWM1
93         #error unimplemented pwm triger select.
94 #endif
95
96 INLINE void tc_setup(uint32_t freq, size_t n_sample)
97 {
98         pmc_periphEnable(DAC_TC_ID);
99
100         /*  Disable TC clock */
101         DAC_TC_CCR = TC_CCR_CLKDIS;
102         /*  Disable interrupts */
103         DAC_TC_IDR = 0xFFFFFFFF;
104         /*  Clear status register */
105         volatile uint32_t dummy = DAC_TC_SR;
106         (void)dummy;
107
108         /*
109          * Setup the timer counter:
110          * - select clock TCLK1 (MCK/2)
111          * - enable wave form mode
112          * - RA compare effect SET
113          * - RC compare effect CLEAR
114          * - UP mode with automatic trigger on RC Compare
115          */
116         DAC_TC_CMR = TC_TIMER_CLOCK1 | BV(TC_CMR_WAVE) | TC_CMR_ACPA_SET | TC_CMR_ACPC_CLEAR | BV(TC_CMR_CPCTRG);
117
118         /*
119          * Compute the sample frequency
120          * the RC counter will update every MCK/2 (see above)
121          * so to convert one sample at the user freq we generate
122          * the trigger every TC_CLK / (numer_of_sample * user_freq)
123          * where TC_CLK = MCK / 2.
124          */
125         uint32_t rc = DIV_ROUND((CPU_FREQ / 2), n_sample * freq);
126         DAC_TC_RC = rc;
127         /* generate the square wave with duty = 50% */
128         DAC_TC_RA = DIV_ROUND(50 * rc, 100);
129
130         PIOB_PDR = BV(25);
131         PIO_PERIPH_SEL(PIOB_BASE, BV(25), PIO_PERIPH_B);
132 }
133
134 INLINE void tc_start(void)
135 {
136         DAC_TC_CCR =  BV(TC_CCR_CLKEN)| BV(TC_CCR_SWTRG);
137 }
138
139 INLINE void tc_stop(void)
140 {
141         DAC_TC_CCR =  BV(TC_CCR_CLKDIS);
142 }
143
144 static int sam3x_dac_write(struct Dac *dac, unsigned channel, uint16_t sample)
145 {
146         (void)dac;
147
148         ASSERT(channel <= DAC_MAXCH);
149
150         DACC_MR |= (channel << DACC_USER_SEL_SHIFT) & DACC_USER_SEL_MASK;
151         DACC_CHER |= BV(channel);
152
153         DACC_CDR = sample ;
154
155         return 0;
156 }
157
158 static void sam3x_dac_setCh(struct Dac *dac, uint32_t mask)
159 {
160         /* we have only the ch0 and ch1 */
161         ASSERT(mask < BV(3));
162         dac->hw->channels = mask;
163 }
164
165 static void sam3x_dac_setSampleRate(struct Dac *dac, uint32_t rate)
166 {
167         (void)dac;
168
169         /* Eneble hw trigger */
170         DACC_MR |= BV(DACC_TRGEN) | (CONFIG_DAC_TIMER << DACC_TRGSEL_SHIFT);
171         dac->hw->rate = rate;
172 }
173
174 static void sam3x_dac_conversion(struct Dac *dac, void *buf, size_t len)
175 {
176         if (dac->hw->channels & BV(DACC_CH0))
177                 DACC_MR |= (DACC_CH0 << DACC_USER_SEL_SHIFT) & DACC_USER_SEL_MASK;
178
179         if (dac->hw->channels & BV(DACC_CH1))
180                 DACC_MR |= (DACC_CH1 << DACC_USER_SEL_SHIFT) & DACC_USER_SEL_MASK;
181
182         DACC_CHER |= dac->hw->channels;
183
184         /* setup timer and start it */
185         tc_setup(dac->hw->rate, len);
186         tc_start();
187
188         /* Setup dma and start it */
189         DACC_TPR = (uint32_t)buf ;
190         DACC_TCR = len;
191         DACC_PTCR |= BV(DACC_PTCR_TXTEN);
192 }
193
194 static bool sam3x_dac_isFinished(struct Dac *dac)
195 {
196         (void)dac;
197         return 0;
198 }
199
200 static void sam3x_dac_start(struct Dac *dac, void *buf, size_t len, size_t slice_len)
201 {
202         (void)dac;
203         (void)buf;
204         (void)len;
205         (void)slice_len;
206 }
207
208 static void sam3x_dac_stop(struct Dac *dac)
209 {
210         (void)dac;
211 }
212
213
214 void dac_init(struct Dac *dac)
215 {
216
217         /* Fill the virtual table */
218         dac->ctx.write = sam3x_dac_write;
219         dac->ctx.setCh = sam3x_dac_setCh;
220         dac->ctx.setSampleRate = sam3x_dac_setSampleRate;
221         dac->ctx.conversion = sam3x_dac_conversion;
222         dac->ctx.isFinished = sam3x_dac_isFinished;
223         dac->ctx.start = sam3x_dac_start;
224         dac->ctx.stop = sam3x_dac_stop;
225         DB(dac->ctx._type = DAC_SAM3X;)
226         dac->hw = &dac_hw;
227
228         /* Clock DAC peripheral */
229         pmc_periphEnable(DACC_ID);
230
231         /* Reset hw */
232         DACC_CR |= BV(DACC_SWRST);
233         DACC_MR = 0;
234
235         /* Configure the dac */
236         DACC_MR |= (CONFIG_DAC_REFRESH << DACC_REFRESH_SHIFT) & DACC_REFRESH_MASK;
237         DACC_MR |= (CONFIG_DAC_STARTUP << DACC_STARTUP_SHIFT) & DACC_STARTUP_MASK;
238 }