Home2L - C/C++ API v1.4-0-g38cc (2024-05-25)
Smart Tools for a Private Home
core.h
Go to the documentation of this file.
1/*
2 * This file is part of the Home2L project.
3 *
4 * (C) 2015-2021 Gundolf Kiefer
5 *
6 * Home2L 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 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Home2L 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 Home2L. If not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20
21
22#ifndef _CORE_
23#define _CORE_
24
25#include "configure.h"
26
27
39#define EMPTY_MODULE(NAME) \
40 static inline void NAME##Init () {} \
41 static inline void NAME##Iterate () {} \
42 static inline void NAME##OnRegRead (uint8_t reg) {} \
43 static inline void NAME##OnRegWrite (uint8_t reg, uint8_t val) {} \
44 static inline void NAME##ISR () {}
46
47
48
49
50// *************************** Memory and Registers ****************************
52
55
56
57
58// ***** VROM *****
59
60extern const __flash TBrFeatureRecord brFeatureRecord;
62
63
64
65// ***** Configuration (SRAM-backed) *****
66
67extern TBrConfigRecord _brConfigRecord;
68#define brConfigRecord ((const TBrConfigRecord) _brConfigRecord)
70
71
72
73
74// ***** Registers *****
76extern uint8_t _regFile[BR_REGISTERS];
77
78#define RegGet(REG) (_regFile[REG])
80#define RegSet(REG, VAL) do { _regFile[REG] = (VAL); } while (0)
82
83
84
85
87// ***************** Persistent Storage (EEPROM) *******************************
88
93
94
99struct SBrEeprom {
102
103 // Module-related data for persistency ...
104 uint8_t shadesPos[2];
105};
106
108extern struct SBrEeprom brEeprom;
110
111
113
114
115// *************************** Change Reporting ********************************
116
117
121
122
123#if !IS_MAINTENANCE || DOXYGEN
124
125
126extern uint8_t chgShadow;
127
128
129static inline void ReportChange (uint8_t mask) { chgShadow |= mask; }
131void ReportChangeAndNotify (uint8_t mask);
133
134
135#endif // IS_MAINTENANCE
136
138
140
141// *************************** Timer *******************************************
142
143
147
148
149#if WITH_TIMER || DOXYGEN
150
151
152// Constants are defined in 'interface.h' (BR_TICKS_*).
153
154
155uint16_t TimerNow ();
161
162
163#else // WITH_TIMER
164static inline uint16_t TimerNow () { return 0; }
165#endif // WITH_TIMER
166
167
168
169
170
171// *************************** Mini-Timer **************************************
172
173
186
187
188#define MINITICKS_OF_US(X) (X * (BR_CPU_FREQ / 1000000) / 8)
190
191
192#if DOXYGEN
193
194#define MINI_CLOCK_SCALE_1
195#define MINI_CLOCK_SCALE_8
196#define MINI_CLOCK_SCALE_64
197#define MINI_CLOCK_SCALE_256
198#define MINI_CLOCK_SCALE_1024
199
200void MinitimerStart (int clockScale);
201void MinitimerStop ();
202void MinitimerReset();
203uint8_t MinitimerNow();
206#elif MCU_TYPE == BR_MCU_ATTINY85 || MCU_TYPE == BR_MCU_ATTINY84
208
209#define MINI_CLOCK_SCALE_1 1
210#define MINI_CLOCK_SCALE_8 2
211#define MINI_CLOCK_SCALE_64 3
212#define MINI_CLOCK_SCALE_256 4
213#define MINI_CLOCK_SCALE_1024 5
214
215static inline void MinitimerStart (int CLOCK_SCALE) {
216 TCCR0A = 0; // Normal port operation (OC1A/OC1B disconnected); No waveform generation
217 TCCR0B = CLOCK_SCALE; // enable 8-bit timer with clk_io / 8; one tick is 8 µs; one ZACwire bit is 125 µs ~= 16 clock units
218}
219
220static inline void MinitimerStop () {
221 TCCR0B = 0;
222}
223
224#define MinitimerReset() do { TCNT0 = 0; } while (0)
225
226#define MinitimerNow() ((uint8_t) TCNT0)
227
228
229#elif MCU_TYPE == BR_MCU_ATTINY861
230
231
232#define MINI_CLOCK_SCALE_1 1
233#define MINI_CLOCK_SCALE_8 4
234#define MINI_CLOCK_SCALE_64 7
235#define MINI_CLOCK_SCALE_256 9
236#define MINI_CLOCK_SCALE_1024 11
237
238static inline void MinitimerStart (int CLOCK_SCALE) {
239 TCCR1A = 0; // Normal port operation (OC1A/OC1B disconnected); No waveform generation
240 TCCR1B = (1 << PSR1) | CLOCK_SCALE;
241 TC1H = 0; // Set high byte to 0 (affects TCNT1, OCR1C and any other 10 bit registers)
242 OCR1C = 0xff; // "top" value for counter
243 PLLCSR = 0; // disable PLL, ensure synchronous mode
244}
245
246static inline void MinitimerStop () {
247 TCCR1B = 0;
248}
249
250#define MinitimerReset() do { TCNT1 = 0; TCCR1B |= (1 << PSR1); } while (0)
251 // Note: According to the manual, "Due to synchronization of the CPU, Timer/Counter1
252 // data written into Timer/Counter1 is delayed by one and half CPU clock cycles
253 // in synchronous mode". Hence, we add a delay here before reading TCNT1.
254
255#define MinitimerNow() ((uint8_t) TCNT1)
256
257
258#endif // MCU_TYPE == BR_MCU_ATTINY861
259
260
261
262
263
264// ***************** Interface Functions of the "core" Module ******************
265
266
279
280
281void CoreInit ();
286static inline void CoreIterate () {}
292void CoreOnRegRead (uint8_t reg);
304void CoreOnRegWrite (uint8_t reg, uint8_t val);
314
315
317
319
320
321
322#endif // _CORE_
void MinitimerStop()
Stop the minitimer.
static void CoreIterate()
Iterate the module.
Definition: core.h:295
const __flash TBrFeatureRecord brFeatureRecord
The Brownie feature recourd.
static void ReportChange(uint8_t mask)
Set (a) bit(s) in the BR_REG_CHANGED register.
Definition: core.h:137
void CoreInit()
Initialize the module.
uint16_t TimerNow()
Get current time in ticks.
void ReportChangeAndNotify(uint8_t mask)
Set (a) bit(s) in the BR_REG_CHANGED register and issue a TWI host notification.
struct SBrEeprom brEeprom
Complete EEPROM contents.
void MinitimerReset()
Reset the minitimer.
void CoreOnRegWrite(uint8_t reg, uint8_t val)
Write a register.
uint8_t MinitimerNow()
Get the current minitimer value.
void MinitimerStart(int clockScale)
Start the minitimer (pass any MINI_CLOCK_SCALE_* constants)
void CoreOnRegRead(uint8_t reg)
Update a register when it is read.
char TBrIdRecord[32]
Brownie ID (stored in EEPROM).
Definition: interface.h:423
#define BR_REGISTERS
Number of registers.
Definition: interface.h:497
Brownie configuration record (stored in EEPROM).
Definition: interface.h:447
Structure to describe the complete EEPROM content.
Definition: core.h:107
TBrIdRecord id
Brownie ID record
Definition: core.h:108
uint8_t shadesPos[2]
(shades) Position
Definition: core.h:112
TBrConfigRecord cfg
Brownie config record (persistent EEPROM copy)
Definition: core.h:109
Brownie feature record (stored in VROM).
Definition: interface.h:332