Merge branch 'master'
[fw/altos] / src / samd21 / ao_tcc_samd21.c
1 /*
2  * Copyright © 2019 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include <ao.h>
16 #include <ao_tcc-samd21.h>
17
18 void
19 ao_tcc_set(struct samd21_tcc *tcc, uint8_t channel, uint32_t value)
20 {
21         tcc->cc[channel] = value;
22 }
23
24 static void
25 ao_tcc_init(struct samd21_tcc *tcc, uint32_t apbcmask)
26 {
27         samd21_pm.apbcmask |= apbcmask;
28
29         /* Reset the device */
30         tcc->ctrla = (1 << SAMD21_TCC_CTRLA_SWRST);
31
32         while ((tcc->ctrla & (1 << SAMD21_TCC_CTRLA_SWRST)) != 0 ||
33                (tcc->syncbusy & (1 << SAMD21_TCC_SYNCBUSY_SWRST)) != 0)
34                 ;
35
36         tcc->per = AO_TCC_PERIOD - 1;
37
38 #if 0
39         tcc->evctrl = ((1 << SAMD21_TCC_EVCTRL_MCEO(0)) |
40                        (1 << SAMD21_TCC_EVCTRL_MCEO(1)) |
41                        (1 << SAMD21_TCC_EVCTRL_MCEO(2)) |
42                        (1 << SAMD21_TCC_EVCTRL_MCEO(3)));
43 #endif
44
45         tcc->wave = ((SAMD21_TCC_WAVE_WAVEGEN_NPWM << SAMD21_TCC_WAVE_WAVEGEN) |
46                      (0 << SAMD21_TCC_WAVE_RAMP) |
47                      (0 << SAMD21_TCC_WAVE_CIPEREN) |
48                      (0 << SAMD21_TCC_WAVE_CCCEN(0)) |
49                      (0 << SAMD21_TCC_WAVE_CCCEN(1)) |
50                      (0 << SAMD21_TCC_WAVE_CCCEN(2)) |
51                      (0 << SAMD21_TCC_WAVE_CCCEN(3)) |
52                      (0 << SAMD21_TCC_WAVE_POL(0)) |
53                      (0 << SAMD21_TCC_WAVE_POL(1)) |
54                      (0 << SAMD21_TCC_WAVE_POL(1)) |
55                      (0 << SAMD21_TCC_WAVE_POL(3)) |
56                      (0 << SAMD21_TCC_WAVE_SWAP(0)) |
57                      (0 << SAMD21_TCC_WAVE_SWAP(1)) |
58                      (0 << SAMD21_TCC_WAVE_SWAP(1)) |
59                      (0 << SAMD21_TCC_WAVE_SWAP(3)));
60
61         tcc->ctrla = (1 << SAMD21_TCC_CTRLA_ENABLE);
62         tcc->dbgctrl = (1 << SAMD21_TCC_DBGCTRL_DBGRUN);
63 }
64
65 void
66 ao_tcc_samd21_init(void)
67 {
68         samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_TCC0_TCC1);
69         samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_TCC2_TC3);
70
71         ao_tcc_init(&samd21_tcc0, 1 << SAMD21_PM_APBCMASK_TCC0);
72         ao_tcc_init(&samd21_tcc1, 1 << SAMD21_PM_APBCMASK_TCC1);
73         ao_tcc_init(&samd21_tcc2, 1 << SAMD21_PM_APBCMASK_TCC2);
74 }