4
* Attiny portability header.
5
* This helps abstract away the differences between various attiny MCUs.
7
* Copyright (C) 2017 Selene Scriven
9
* This program is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation, either version 3 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1
// tk-attiny.h: Attiny portability header.
2
// Copyright (C) 2014-2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
6
// This helps abstract away the differences between various attiny MCUs.
24
8
// Choose your MCU here, or in the main .c file, or in the build script
71
55
#define DELAY_ZERO_TIME 1020
72
56
//#define SWITCH_PORT PINA // set this in hwdef
73
57
//#define VOLTAGE_ADC_DIDR DIDR0 // set this in hwdef
58
#elif (ATTINY == 412) || (ATTINY == 416) || (ATTINY == 417) || (ATTINY == 816) || (ATTINY == 817) || (ATTINY == 1616) || (ATTINY == 1617) || (ATTINY == 3216) || (ATTINY == 3217)
60
#define F_CPU 10000000UL
61
#define BOGOMIPS (F_CPU/4700)
63
#define DELAY_ZERO_TIME 1020
75
65
#error Hey, you need to define ATTINY.
138
128
clock_div_256 = 8
131
#elif defined(AVRXMEGA3) // ATTINY816, 817, etc
132
// this should work, but needs further validation
133
inline void clock_prescale_set(uint8_t n) {
135
CCP = CCP_IOREG_gc; // temporarily disable clock change protection
136
CLKCTRL.MCLKCTRLB = n; // Set the prescaler
137
while (CLKCTRL.MCLKSTATUS & CLKCTRL_SOSC_bm) {} // wait for clock change to finish
142
// Actual clock is 20 MHz, but assume that 10 MHz is the top speed and work from there
143
// TODO: measure PWM speed and power use at 1.25/2.5/5/10 MHz, to determine which speeds are optimal
144
clock_div_1 = (CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm), // 10 MHz
145
clock_div_2 = (CLKCTRL_PDIV_4X_gc | CLKCTRL_PEN_bm), // 5 MHz
146
clock_div_4 = (CLKCTRL_PDIV_8X_gc | CLKCTRL_PEN_bm), // 2.5 MHz
147
clock_div_8 = (CLKCTRL_PDIV_16X_gc | CLKCTRL_PEN_bm), // 1.25 MHz
148
clock_div_16 = (CLKCTRL_PDIV_32X_gc | CLKCTRL_PEN_bm), // 625 kHz
149
clock_div_32 = (CLKCTRL_PDIV_64X_gc | CLKCTRL_PEN_bm), // 312 kHz, max without changing to the 32 kHz ULP
150
clock_div_64 = (CLKCTRL_PDIV_64X_gc | CLKCTRL_PEN_bm), // 312 kHz
151
clock_div_128 = (CLKCTRL_PDIV_64X_gc | CLKCTRL_PEN_bm), // 312 kHz
152
clock_div_256 = (CLKCTRL_PDIV_64X_gc | CLKCTRL_PEN_bm) // 312 kHz
142
155
#error Unable to define MCU macros.
145
#endif // TK_ATTINY_H