Home2L - C/C++ API  v1.2-2-ga4fe (2023-04-15)
Smart Tools for a Private Home
interface.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 _INTERFACE_
23 #define _INTERFACE_
24 
25 
42 #include <stdint.h>
43 #include <stdbool.h>
44 
45 
46 
47 
48 
49 // *************************** General Device Settings *************************
50 
51 
66 #define BR_CPU_FREQ 1000000
67 
68 #if AVR && BR_CPU_FREQ != F_CPU
69 #error "The CPU frequency (F_CPU) must be set to 1 MHz / BR_CPU_FREQ"
70 #endif
71 
72 
73 #define BR_TICKS_PER_SECOND (((float) BR_CPU_FREQ) / 1024.0)
74 #define BR_TICKS_PER_MS (BR_TICKS_PER_SECOND / 1000.0)
75 #define BR_MS_PER_TICK (1.0 / BR_TICKS_PER_MS)
76 
77 #define BR_TICKS_OF_MS(T) ((T) * BR_TICKS_PER_MS)
78 #define BR_MS_OF_TICKS(T) (((float) (T)) * BR_MS_PER_TICK)
79 
80 #define BR_TICKS_NEVER 0
81 
82 
84 
85 
86 
87 
88 
89 // *************************** Protocol ****************************************
90 
91 
112 #define BR_MEM_BLOCKSIZE_SHIFT 4
113 #define BR_MEM_BLOCKSIZE (1 << BR_MEM_BLOCKSIZE_SHIFT)
116 
117 
118 
121 typedef enum {
122  brOk = 0,
132 
133  brNoReply = 0x0f,
134  brEND
135 } EBrStatus;
136 
137 
140 typedef struct SBrRequest {
141  uint8_t check;
142  uint8_t op;
143  union {
144  struct { // Register write data ...
145  uint8_t val;
146  } regWrite;
147  struct { // Memory read data ...
148  uint8_t adr;
149  } memRead;
150  struct { // Memory write ...
151  uint8_t adr;
153  } memWrite;
154  };
156 
157 
160 typedef struct SBrReply {
161  uint8_t status;
162  union {
163  struct { // Register read ...
164  uint8_t val;
165  } regRead;
166  struct { // Memory read ...
168  uint8_t dataCheck;
169  } memRead;
170  };
172 
173 
174 
175 
176 
177 // ***** Macros *****
178 
179 
180 // Request-related ...
181 #define BR_REQUEST_SIZE_MAX ((int) sizeof (TBrRequest))
182 #define BR_REQUEST_SIZE_MIN 2
183 
184 // Reply-related ...
185 #define BR_REPLY_SIZE_MAX ((int) sizeof (TBrReply))
186 #define BR_REPLY_SIZE_MIN 1
187 #define BR_REPLY_SIZE_STATUS 1
188 
189 
190 // Contructing operation words ...
191 #define BR_OP_REG_READ(REG) ((0x00 | (REG)))
192 #define BR_OP_REG_WRITE(REG) ((0x40 | (REG)))
193 #define BR_OP_MEM_READ(BLKADR) (0x80 | ((BLKADR) >> 8))
194 #define BR_OP_MEM_WRITE(BLKADR) (0x90 | ((BLKADR) >> 8))
195 
196 // Analyzing operation words ...
197 #define BR_OP_IS_REG_READ(OP) (((OP) & 0xc0) == 0x00)
198 #define BR_OP_IS_REG_WRITE(OP) (((OP) & 0xc0) == 0x40)
199 #define BR_OP_IS_MEM_READ(OP) (((OP) & 0xf0) == 0x80)
200 #define BR_OP_IS_MEM_WRITE(OP) (((OP) & 0xf0) == 0x90)
201 
202 
203 
204 
205 
206 // ***** Functions *****
207 
208 
209 // Requests ...
210 int8_t BrRequestSize (uint8_t op);
212 #if !AVR
215 #endif
216 EBrStatus BrRequestCheck (TBrRequest *msg, int8_t bytes);
219 
220 
221 // Replies ...
222 int8_t BrReplySize (uint8_t op);
224 void BrReplyPackage (TBrReply *reply, int8_t len);
226 #if !AVR
227 EBrStatus BrReplyCheck (TBrReply *reply, uint8_t op, int8_t bytes);
230 #endif
231 
232 
234 
235 
236 
237 
238 
239 // ************ Brownie Memory Organization and Data Records *******************
240 
241 
257 
258 // Memory base pages ...
259 #define BR_MEM_PAGE_FLASH 0x0
260 #define BR_MEM_PAGE_SRAM 0x8
261 #define BR_MEM_PAGE_EEPROM 0x9
262 #define BR_MEM_PAGE_VROM 0xa
263 
264 // Identifying memory space region ...
265 #define BR_MEM_ADR_IS_FLASH(ADR) (((ADR) >> (BR_MEM_BLOCKSIZE_SHIFT+8)) <= BR_MEM_PAGE_FLASH + 7)
266 #define BR_MEM_ADR_IS_SRAM(ADR) (((ADR) >> (BR_MEM_BLOCKSIZE_SHIFT+8)) == BR_MEM_PAGE_SRAM)
267 #define BR_MEM_ADR_IS_EEPROM(ADR) (((ADR) >> (BR_MEM_BLOCKSIZE_SHIFT+8)) == BR_MEM_PAGE_EEPROM)
268 #define BR_MEM_ADR_IS_VROM(ADR) (((ADR) >> (BR_MEM_BLOCKSIZE_SHIFT+8)) == BR_MEM_PAGE_VROM)
269 
270 // Building the address from offset inside a region ...
271 #define BR_MEM_ADR_FLASH(OFS) ((OFS) | (BR_MEM_PAGE_FLASH << (BR_MEM_BLOCKSIZE_SHIFT+8)))
272 #define BR_MEM_ADR_SRAM(OFS) ((OFS) | (BR_MEM_PAGE_SRAM << (BR_MEM_BLOCKSIZE_SHIFT+8)))
273 #define BR_MEM_ADR_EEPROM(OFS) ((OFS) | (BR_MEM_PAGE_EEPROM << (BR_MEM_BLOCKSIZE_SHIFT+8)))
274 #define BR_MEM_ADR_VROM(OFS) ((OFS) | (BR_MEM_PAGE_VROM << (BR_MEM_BLOCKSIZE_SHIFT+8)))
275 
276 // Getting the offset inside a region ...
277 #define BR_MEM_OFS(ADR) ((ADR) & ((BR_MEM_ADR_IS_FLASH(ADR) ? 0x7ff : 0x0ff) << BR_MEM_BLOCKSIZE_SHIFT))
278 
279 
280 
281 // Flash ...
282 #define BR_FLASH_PAGESIZE 0x40
291 #define BR_FLASH_BASE_MAINTENANCE 0x0040
293 #define BR_FLASH_BASE_OPERATIONAL 0x0a00
296 
297 
298 // VROM ...
299 #define BR_VROM_SIZE sizeof (TBrFeatureRecord)
300 
301 
302 // EEPROM ...
303 #define BR_EEPROM_ID_BASE 0x0000
304 #define BR_EEPROM_ID_SIZE sizeof (TBrIdRecord)
305 
306 #define BR_EEPROM_CFG_BASE 0x0000 + BR_EEPROM_ID_SIZE
307 #define BR_EEPROM_CFG_SIZE sizeof (TBrConfigRecord)
308 
309 
310 
311 
312 
313 // *************************** Brownie Data Records ****************************
314 
315 
319 
320 
321 #define BR_MAGIC 0xb1
322 
323 
332 typedef struct SBrFeatureRecord {
333 
334  // Firmware version ...
335  uint8_t versionMajor;
336  uint8_t versionMinor;
337  uint16_t versionRevision;
338 
339  // Feature presence ...
340  uint16_t features;
341 
342  uint16_t gpiPresence;
343  uint16_t gpiPullup;
344  uint16_t gpoPresence;
345  uint16_t gpoPreset;
346 
347  uint8_t matDim;
348 
349  uint8_t reserved[3];
350 
351  // Written firmware name ...
352  char fwName[12];
353 
354  // MCU model ...
355  uint8_t mcuType;
356 
357  // Magic byte (for verification) ...
358  uint8_t magic;
359 
360 } __attribute__((packed)) TBrFeatureRecord;
361 
362 
363 #define brFeatureRecordRcVec0 offsetof (TBrFeatureRecord, features)
365 #define brFeatureRecordRcVec1 offsetof (TBrFeatureRecord, reserved)
367 
368 
373 
374 #define BR_FEATURE_MAINTENANCE 0x0001
375 #define BR_FEATURE_TIMER 0x0002
376 #define BR_FEATURE_NOTIFY 0x0004
377 #define BR_FEATURE_TWIHUB 0x0008
378 #define BR_FEATURE_ADC_0 0x0010
379 #define BR_FEATURE_ADC_1 0x0020
380 #define BR_FEATURE_ADC_PASSIVE 0x0400
381 #define BR_FEATURE_UART 0x0040
382 #define BR_FEATURE_TEMP 0x0080
383 #define BR_FEATURE_SHADES_0 0x0100
384 #define BR_FEATURE_SHADES_1 0x0200
385 
386 
391 
392 #define BR_MATDIM_ROWS(X) ((X) >> 4)
393 #define BR_MATDIM_COLS(X) ((X) & 0x0f)
394 
399 
400 #define BR_MCU_NONE 0
401 #define BR_MCU_ATTINY85 1
402 #define BR_MCU_ATTINY84 2
403 #define BR_MCU_ATTINY861 3
404 
405 
406 
407 
408 
412 
413 
423 typedef char TBrIdRecord[32];
424 
425 
426 
427 
428 
432 
433 
447 typedef struct SBrConfigRecord {
448  uint8_t adr;
449  uint8_t magic;
450 
451  uint8_t oscCal;
452  uint8_t reserved1;
453 
454  int8_t hubMaxAdr;
457  uint8_t hubSpeed;
458 
459  uint8_t shadesDelayUp[2];
460  uint8_t shadesDelayDown[2];
461  uint8_t shadesSpeedUp[2];
462  uint8_t shadesSpeedDown[2];
463 
464  uint8_t reserved[2];
465 
466 } __attribute__((packed)) TBrConfigRecord;
467 
468 
470 
471 
473 
474 
475 
476 
477 
478 // *************************** Brownie Registers *******************************
479 
480 
492 // ***** General Organization *****
493 
494 
497 #define BR_REGISTERS 0x40
498 
499 
500 
501 // ***** Register Map *****
502 
503 
507 #define BR_REG_CHANGED 0x00
508 
509 #define BR_CHANGED_CHILD 0x01
510 #define BR_CHANGED_GPIO 0x02
511 #define BR_CHANGED_MATRIX 0x04
512 #define BR_CHANGED_ADC 0x40
513 #define BR_CHANGED_UART 0x08
514 #define BR_CHANGED_SHADES 0x10
515 
516 #define BR_CHANGED_TEMP 0x20
517 
518 
519 
523 #define BR_REG_GPIO_0 0x02
524 #define BR_REG_GPIO_1 0x03
525 
526 #define BR_REG_TICKS_LO 0x04
527 #define BR_REG_TICKS_HI 0x05
528 
529 #define BR_REG_TEMP_LO 0x06
530 #define BR_REG_TEMP_HI 0x07
534 
535 
536 
541 #define BR_REG_ADC_0_LO 0x08
542 #define BR_REG_ADC_0_HI 0x09
543 
544 #define BR_REG_ADC_1_LO 0x0a
545 #define BR_REG_ADC_1_HI 0x0b
546 
547 
548 
573 #define BR_REG_UART_CTRL 0x0c
574 #define BR_REG_UART_STATUS 0x0d
575 #define BR_REG_UART_RX 0x0e
576 #define BR_REG_UART_TX 0x0f
577 
578 #define BR_UART_CTRL_RESET_RX 0x01
579 #define BR_UART_CTRL_RESET_TX 0x02
580 #define BR_UART_CTRL_RESET_FLAGS 0x04
581 
582 #define BR_UART_STATUS_RX_MASK 0x07
583 #define BR_UART_STATUS_RX_SHIFT 0
584 #define BR_UART_STATUS_TX_MASK 0x38
585 #define BR_UART_STATUS_TX_SHIFT 3
586 #define BR_UART_STATUS_OVERFLOW 0x40
587 #define BR_UART_STATUS_ERROR 0x80
588 
589 
590 
591 
624 #define BR_REG_MATRIX_0 0x10
625 #define BR_REG_MATRIX_1 0x11
626 #define BR_REG_MATRIX_2 0x12
627 #define BR_REG_MATRIX_3 0x13
628 #define BR_REG_MATRIX_4 0x14
629 #define BR_REG_MATRIX_5 0x15
630 #define BR_REG_MATRIX_6 0x16
631 #define BR_REG_MATRIX_7 0x17
632 #define BR_REG_MATRIX_EVENT 0x18
633 #define BR_REG_MATRIX_ECYCLE 0x19
634 
635 #define BR_MATRIX_EV_VAL_SHIFT 6
636 #define BR_MATRIX_EV_ROW_SHIFT 3
637 #define BR_MATRIX_EV_COL_SHIFT 0
638 #define BR_MATRIX_EV_EMPTY 0x80
639 #define BR_MATRIX_EV_OVERFLOW 0x81
640 
641 
642 
685 #define BR_REG_SHADES_STATUS 0x20
686 #define BR_REG_SHADES_0_POS 0x22
687 #define BR_REG_SHADES_0_RINT 0x23
688 #define BR_REG_SHADES_0_REXT 0x24
689 #define BR_REG_SHADES_1_POS 0x25
690 #define BR_REG_SHADES_1_RINT 0x26
691 #define BR_REG_SHADES_1_REXT 0x27
692 
693 #define BR_SHADES_0_ACT_UP 0x01
694 #define BR_SHADES_0_ACT_DN 0x02
695 #define BR_SHADES_0_BTN_UP 0x04
696 #define BR_SHADES_0_BTN_DN 0x08
697 #define BR_SHADES_1_ACT_UP 0x10
698 #define BR_SHADES_1_ACT_DN 0x20
699 #define BR_SHADES_1_BTN_UP 0x40
700 #define BR_SHADES_1_BTN_DN 0x80
701 
702 
703 
707 #define BR_REG_DEBUG_0 0x38
708 #define BR_REG_DEBUG_1 0x39
709 #define BR_REG_DEBUG_2 0x3a
710 #define BR_REG_DEBUG_3 0x3b
711 
712 
713 
717 #define BR_REG_FWBASE 0x3d
725 #define BR_REG_CTRL 0x3e
726 #define BR_REG_MAGIC 0x3f
727 
728 #define BR_CTRL_UNLOCK_EEPROM 0x01
729 #define BR_CTRL_UNLOCK_FLASH 0x02
730 #define BR_CTRL_HUB_RESURRECTION 0x04
731 
732 #define BR_CTRL_REBOOT 0xe0
733 #define BR_CTRL_REBOOT_NEWFW 0xa0
734 
735 
738 
739 
741 
742 
743 #endif // _INTERFACE_
struct SBrConfigRecord TBrConfigRecord
Brownie configuration record (stored in EEPROM).
struct SBrFeatureRecord TBrFeatureRecord
Brownie feature record (stored in VROM).
char TBrIdRecord[32]
Brownie ID (stored in EEPROM).
Definition: interface.h:423
EBrStatus BrReplyCheck(TBrReply *reply, uint8_t op, int8_t bytes)
Check received message. 'bytes' is the number of received bytes.
EBrStatus
Definition: interface.h:121
#define BR_MEM_BLOCKSIZE
Block size for 'memRead'/memWrite' operations. Note: This value is an integral part of the communicat...
Definition: interface.h:113
struct SBrReply TBrReply
Reply message.
EBrStatus BrRequestCheck(TBrRequest *msg, int8_t bytes)
Check received message. 'bytes' is the number of valid bytes in the beginning of the message.
struct SBrRequest TBrRequest
Request message.
int8_t BrRequestSize(uint8_t op)
Get the size of a request message in bytes, depending on the operation.
int8_t BrReplySize(uint8_t op)
Get the size of a reply message in bytes, depending on the operation.
void BrRequestPackage(TBrRequest *msg)
Complete the message for sending (i.e. add checksum).
void BrReplyPackage(TBrReply *reply, int8_t len)
Complete the reply for sending (i.e. add checksum).
@ brOk
Last command executed successfully.
Definition: interface.h:122
@ brNoDevice
(for masters) No device can be reached under a given address
Definition: interface.h:130
@ brReplyCheckError
Checksum of reply incorrect or message too short.
Definition: interface.h:126
@ brNoBrownie
(for masters) No brownie can be reached under a given address (wrong magic number)
Definition: interface.h:129
@ brForbidden
Operation not allowed.
Definition: interface.h:128
@ brNoReply
A device did not respond anything (SDA remained pulled up -> 0x[f]f)
Definition: interface.h:133
@ brIllegalOperation
Non-existing operation.
Definition: interface.h:127
@ brNoBus
(for masters) General I/O error when accessing the TWI bus
Definition: interface.h:131
@ brIncomplete
No or incomplete message/reply received.
Definition: interface.h:123
@ brUnchecked
Message complete, but not yet checked for checksum.
Definition: interface.h:124
@ brRequestCheckError
Checksum of request incorrect or message too short.
Definition: interface.h:125
Brownie configuration record (stored in EEPROM).
Definition: interface.h:447
uint8_t hubSpeed
TWI master speed-down (1 ~= 100KHz; n ~= 100/n KHz)
Definition: interface.h:457
uint8_t shadesDelayDown[2]
Shades delay in ticks when starting to move down.
Definition: interface.h:460
uint8_t oscCal
Timer calibration: AVR's OSCCAL register (0xff = load factory default on boot)
Definition: interface.h:451
uint8_t shadesSpeedDown[2]
Shades motion down per tick.
Definition: interface.h:462
uint8_t adr
Own TWI address.
Definition: interface.h:448
uint8_t reserved[2]
(Padding to fill up 16 bytes.)
Definition: interface.h:464
uint8_t magic
Identify as a Brownie (should always be BR_MAGIC)
Definition: interface.h:449
uint8_t shadesSpeedUp[2]
Shades motion up per tick.
Definition: interface.h:461
uint8_t shadesDelayUp[2]
Shades delay in ticks when starting to move up.
Definition: interface.h:459
int8_t hubMaxAdr
TWI hub subnet: Last address managed by this hub.
Definition: interface.h:454
Brownie feature record (stored in VROM).
Definition: interface.h:332
uint16_t gpoPreset
GPIO output default state (will be set on init before Z-state is left)
Definition: interface.h:345
uint8_t magic
Brownie identification (always = BR_MAGIC)
Definition: interface.h:358
uint8_t mcuType
MCU type (see BR_MCU_... constants)
Definition: interface.h:355
uint16_t gpiPresence
GPIO input presence mask (must be disjoint with output presence)
Definition: interface.h:342
uint8_t versionMajor
Version: major/minor/revision...
Definition: interface.h:335
uint16_t features
Feature presence (see BR_FEATURE_... masks)
Definition: interface.h:340
uint16_t gpoPresence
GPIO output presence mask (must be disjoint with input presence)
Definition: interface.h:344
uint16_t gpiPullup
GPIO input pullup selection; bits for which the internal pullup is activated.
Definition: interface.h:343
uint8_t reserved[3]
(reserved for future features)
Definition: interface.h:349
char fwName[12]
Written name of the firmware variant (base name of the .elf file without MCU part)
Definition: interface.h:352
uint8_t matDim
Matrix dimensions (Bits 7:4: rows, bits 3:0 = cols)
Definition: interface.h:347
Reply message.
Definition: interface.h:160
uint8_t status
Checksum (bits 7..4) and status (bits 3:0) (EBrStatus)
Definition: interface.h:161
uint8_t data[BR_MEM_BLOCKSIZE]
(memRead) Data
Definition: interface.h:167
uint8_t dataCheck
(memRead) 8-bit checksum for 'data'
Definition: interface.h:168
uint8_t val
(regRead) Value
Definition: interface.h:164
Request message.
Definition: interface.h:140
uint8_t val
(regWrite) Value to write.
Definition: interface.h:145
uint8_t check
Checksum (8 bits).
Definition: interface.h:141
uint8_t data[BR_MEM_BLOCKSIZE]
(memWrite) Data to write
Definition: interface.h:152
uint8_t op
Operation.
Definition: interface.h:142
uint8_t adr
(memRead) Memory block address
Definition: interface.h:148