2 * Copyright © 2019 Keith Packard <keithp@keithp.com>
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.
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.
16 #include <ao_tcc-samd21.h>
19 ao_tcc_set(struct samd21_tcc *tcc, uint8_t channel, uint32_t value)
21 tcc->cc[channel] = value;
25 ao_tcc_init(struct samd21_tcc *tcc, uint32_t apbcmask)
27 samd21_pm.apbcmask |= apbcmask;
29 /* Reset the device */
30 tcc->ctrla = (1 << SAMD21_TCC_CTRLA_SWRST);
32 while ((tcc->ctrla & (1 << SAMD21_TCC_CTRLA_SWRST)) != 0 ||
33 (tcc->syncbusy & (1 << SAMD21_TCC_SYNCBUSY_SWRST)) != 0)
36 tcc->per = AO_TCC_PERIOD - 1;
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)));
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)));
61 tcc->ctrla = (1 << SAMD21_TCC_CTRLA_ENABLE);
62 tcc->dbgctrl = (1 << SAMD21_TCC_DBGCTRL_DBGRUN);
66 ao_tcc_samd21_init(void)
68 samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_TCC0_TCC1);
69 samd21_gclk_clkctrl(0, SAMD21_GCLK_CLKCTRL_ID_TCC2_TC3);
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);