~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/tk-attiny.h

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#ifndef TK_ATTINY_H
2
 
#define TK_ATTINY_H
3
 
/*
4
 
 * Attiny portability header.
5
 
 * This helps abstract away the differences between various attiny MCUs.
6
 
 *
7
 
 * Copyright (C) 2017 Selene Scriven
8
 
 *
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.
13
 
 *
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.
18
 
 *
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/>.
21
 
 *
22
 
 */
 
1
// tk-attiny.h: Attiny portability header.
 
2
// Copyright (C) 2014-2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
// This helps abstract away the differences between various attiny MCUs.
23
7
 
24
8
// Choose your MCU here, or in the main .c file, or in the build script
25
9
//#define ATTINY 13
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)
 
59
    #define AVRXMEGA3
 
60
    #define F_CPU 10000000UL
 
61
    #define BOGOMIPS (F_CPU/4700)
 
62
    #define EEPSIZE 128
 
63
    #define DELAY_ZERO_TIME 1020
74
64
#else
75
65
    #error Hey, you need to define ATTINY.
76
66
#endif
138
128
      clock_div_256 = 8
139
129
  } clock_div_t;
140
130
 
 
131
#elif defined(AVRXMEGA3)  // ATTINY816, 817, etc
 
132
    // this should work, but needs further validation
 
133
    inline void clock_prescale_set(uint8_t n) {
 
134
        cli();         
 
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
 
138
        sei();
 
139
    }
 
140
    typedef enum
 
141
    {
 
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 
 
153
    } clock_div_t;
141
154
#else
142
155
#error Unable to define MCU macros.
143
156
#endif
144
157
 
145
 
#endif  // TK_ATTINY_H