avr: added n23.

This commit is contained in:
Martin Preuss
2025-02-09 21:03:40 +01:00
parent 702acb3304
commit dbe23f7e73
6 changed files with 823 additions and 0 deletions

2
avr/devices/n23/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
*.eep.hex
*.obj

53
avr/devices/n23/0BUILD Normal file
View File

@@ -0,0 +1,53 @@
<?xml?>
<gwbuild>
<target type="AvrHexFile" name="n23_firmware" >
<includes type="avrasm" >
-I $(builddir)
-I $(srcdir)
-I $(topsrcdir)/avr
-I $(topbuilddir)/avr
</includes>
<sources type="avrasm" >
main.asm
</sources>
</target>
<target type="AvrHexFile" name="n23_boot" >
<includes type="avrasm" >
-I $(builddir)
-I $(srcdir)
-I $(topsrcdir)/avr
-I $(topbuilddir)/avr
</includes>
<sources type="avrasm" >
boot.asm
</sources>
</target>
<subdirs>
</subdirs>
<extradist>
defs.asm
README
</extradist>
</gwbuild>

13
avr/devices/n23/README Normal file
View File

@@ -0,0 +1,13 @@
N20
===
- Role: Air quality and climate sensors
- MCU: AtTiny84
- Connection: RJ45
- Periphery:
- PIR sensor (AMN31112)
- TWI interface
- SI7021 temperature and humidity sensor
- CCS811 air quality sensor

144
avr/devices/n23/boot.asm Normal file
View File

@@ -0,0 +1,144 @@
; ***************************************************************************
; Source file for base system node on AtTiny 84
;
; This is for the maintenance system (i.e. the flash loader).
;
; All definitions and changes should go into this file.
; ***************************************************************************
.equ clock=1000000 ; Define the clock frequency
.nolist
.include "include/tn84def.inc" ; Define device ATtiny84
.list
.include "./defs.asm"
.include "defs_all.asm"
; ***************************************************************************
; defines
; ---------------------------------------------------------------------------
; generic
.include "common/utils_wait.asm"
.include "modules/com2/defs.asm"
.include "modules/comproto/defs.asm"
; ---------------------------------------------------------------------------
; firmware settings
.equ FIRMWARE_VERSION_MAJOR = 0
.equ FIRMWARE_VERSION_MINOR = 0
.equ FIRMWARE_VERSION_PATCHLEVEL = 1
; ---------------------------------------------------------------------------
; LED
.equ LED_DDR = DDRA
.equ LED_PORT = PORTA
.equ LED_PIN = PINA
.equ LED_PINNUM = PORTA3
; ***************************************************************************
; code segment
.cseg
.org 0x0000
; ---------------------------------------------------------------------------
; Reset and interrupt vectors
; rjmp start ; Reset vector
rjmp main ; Reset vector
reti ; EXT_INT0
reti ; PCI0
reti ; PCI1
reti ; WATCHDOG
reti ; ICP1
reti ; OC1A
reti ; OC1B
reti ; OVF1
reti ; OC0A
reti ; OC0B
reti ; OVF0
reti ; ACI
reti ; ADCC
reti ; ERDY
reti ; USI_STR
reti ; USI_OVF
devInfoBlock: ; 12 bytes
devInfoManufacturer: .db 'A', 'Q', 'U', 'A'
devInfoId: .db DEVICEINFO_ID, 0
devInfoVersion: .db DEVICEINFO_VERSION, DEVICEINFO_REVISION ; version, revision
firmwareVersion: .db FIRMWARE_VARIANT_BOOT, FIRMWARE_VERSION_MAJOR
.db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL
firmwareStart: rjmp main ; will be overwritten when flashing
; ***************************************************************************
; main code
.org BOOTLOADER_ADDR
main:
rjmp bootLoader ; this routine is in modules/bootloader/main.asm
; ***************************************************************************
; includes
.include "modules/uart_bitbang/bytelevel.asm"
.include "modules/uart_bitbang/packetlevel.asm"
.include "modules/com2/crc.asm"
.include "common/crc8.asm"
.include "common/utils_wait_fixed.asm"
.include "common/utils_copy_from_flash.asm"
.include "common/utils_copy_sdram.asm"
.include "modules/flash/defs.asm"
.include "modules/flash/eeprom.asm"
.include "modules/flash/io.asm"
.include "modules/flash/io_attn.asm"
.include "modules/flash/io_bitbang.asm"
.include "modules/flash/flash1p.asm"
.include "modules/flash/flashprocess.asm"
.include "modules/flash/wait.asm"
.include "modules/bootloader/main.asm"
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
.if clock == 1000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret

135
avr/devices/n23/defs.asm Normal file
View File

@@ -0,0 +1,135 @@
; ***************************************************************************
; copyright : (C) 2023 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome". *
; * Please see toplevel file COPYING of that project for license details. *
; ***************************************************************************
; ***************************************************************************
;
; AtTiny84
; --------
; VCC 1 14 GND
; PB0 2 13 PA0 AUX-A0
; PIR PB1 3 12 PA1 COM-DATA
; /RESET PB3 4 11 PA2
; AUX-B2 PB2 5 10 PA3 LED
; COM_ATTN PA7 6 9 PA4 TWI-SCL
; TWI-SDA PA6 7 8 PA5
; --------
;
; ***************************************************************************
.equ BOOTLOADER_ADDR = 0xd00
.equ FIRMWARE_VARIANT_BOOT = 0
.equ FIRMWARE_VARIANT_TEMP_WINDOW = 1
.equ DEVICEINFO_ID = 'N'
.equ DEVICEINFO_VERSION = 23
.equ DEVICEINFO_REVISION = 0
; ---------------------------------------------------------------------------
; LED module
.equ LED_SIMPLE_ONTIME = 1 ; shorter
.equ LED_SIMPLE_OFFTIME = 50 ; longer
.equ LED_SIMPLE_DDR = DDRA
.equ LED_SIMPLE_PORT = PORTA
.equ LED_SIMPLE_PORTIN = PINA
.equ LED_SIMPLE_PINNUM = PORTA3
; ---------------------------------------------------------------------------
; COM module
.equ COM_BIT_LENGTH = 52000 ; 104000ns=9600, 52000ns=19200, 26000ns=38400
.equ COM_HALFBIT_LENGTH = 26000 ; see https://de.wikipedia.org/wiki/Universal_Asynchronous_Receiver_Transmitter
.equ COM_DATA_DDR = DDRA
.equ COM_DATA_INPUT = PINA
.equ COM_DATA_OUTPUT = PORTA
.equ COM_DATA_PIN = PORTA1
.equ COM_ATTN_DDR = DDRA
.equ COM_ATTN_INPUT = PINA
.equ COM_ATTN_OUTPUT = PORTA
.equ COM_ATTN_PIN = PORTA7
.equ COM_IRQ_ADDR_ATTN = PCMSK0
.equ COM_IRQ_BIT_ATTN = PCINT7 ; bit 7 in PCMSK0
.equ COM_IRQ_GIFR_ATTN = PCIF0
.equ COM_IRQ_GIMSK_ATTN = PCIE0
; ---------------------------------------------------------------------------
; TWI master module
;.equ TWI_BIT_LENGTH = 10000 ; 100000 and 200000 works for display: 10000, 100000, 200000
.equ TWI_BIT_LENGTH = 1 ; 10, 100, 500, 100000 and 200000 works for display: 10000, 100000, 200000
.equ TWI_DDR_SCL = DDRA
.equ TWI_PORT_SCL = PORTA
.equ TWI_PIN_SCL = PINA
.equ TWI_PINNUM_SCL = PORTA4
.equ TWI_DDR_SDA = DDRA
.equ TWI_PORT_SDA = PORTA
.equ TWI_PIN_SDA = PINA
.equ TWI_PINNUM_SDA = PORTA6
; ---------------------------------------------------------------------------
; LCD module
.equ LCD_TWI_ADDRESS = 0x3c
; ---------------------------------------------------------------------------
; SI 7021
.equ SI7021_ADDR = 0x40
; ---------------------------------------------------------------------------
; 1-Wire Master
;
.equ OWI_DDR = DDRB
.equ OWI_PORTOUT = PORTB
.equ OWI_PORTIN = PINB
.equ OWI_PINNUM = PORTB2
; ---------------------------------------------------------------------------
; Motion Sensor
;
.equ MOTION_DDR = DDRB
.equ MOTION_INPUT = PINB
.equ MOTION_OUTPUT = PORTB
.equ MOTION_PIN = PORTB1
; ---------------------------------------------------------------------------
; CCS 811
;
.equ CCS811_ADDR = 0x5a ; or 0x5b

476
avr/devices/n23/main.asm Normal file
View File

@@ -0,0 +1,476 @@
; ***************************************************************************
; copyright : (C) 2024 by Martin Preuss
; email : martin@libchipcard.de
;
; ***************************************************************************
; * This file is part of the project "AqHome". *
; * Please see toplevel file COPYING of that project for license details. *
; ***************************************************************************
; ***************************************************************************
; Source file for temperature sensor node on AtTiny 84
;
; This is for the full system (i.e. not the boot loader).
;
; All definitions and changes should go into this file.
;
;
; ***************************************************************************
.equ clock=1000000 ; Define the clock frequency
;.equ clock=8000000 ; Define the clock frequency
.nolist
.include "include/tn84def.inc" ; Define device ATtiny84
.list
.include "./defs.asm"
.include "defs_all.asm"
; ***************************************************************************
; defines
; ---------------------------------------------------------------------------
; generic
.include "common/utils_wait.asm"
; ---------------------------------------------------------------------------
; firmware settings including list of modules used
.equ FIRMWARE_VERSION_MAJOR = 0
.equ FIRMWARE_VERSION_MINOR = 0
.equ FIRMWARE_VERSION_PATCHLEVEL = 1
#define MODULES_TIMER
#define MODULES_COM
#define MODULES_COM_WITH_ADDR_PROTO
#define MODULES_LED_SIMPLE
#define MODULES_TWI_MASTER
;#define MODULES_LCD
#define LCD_MINIMAL_FONT
#define MODULES_SI7021
#define MODULES_STATS
;#define MODULES_OWI_MASTER
;#define MODULES_DS18B20
#define MODULES_MOTION
;#define MODULES_CCS811
; ---------------------------------------------------------------------------
; defines for values
.equ VALUE_ID_SI7021_TEMP = 0x01
.equ VALUE_ID_SI7021_HUM = 0x02
.equ VALUE_ID_ADC = 0x03
;.equ VALUE_ID_REED1 = 0x04
;.equ VALUE_ID_REED2 = 0x05
;.equ VALUE_ID_DS18B20_TEMP = 0x06
.equ VALUE_ID_MOTION = 0x07
.equ VALUE_ID_CO2 = 0x08
.equ VALUE_ID_TVOC = 0x09
;.equ VALUE_ID_REED_CONF = 0x81
.equ VALUE_ID_DEBUG = 0x7f
; ***************************************************************************
; code segment
.cseg
.org 000000
; ---------------------------------------------------------------------------
; Reset and interrupt vectors (will be removed as soon as we can flash data over COM)
; rjmp main ; Reset vector
rjmp BOOTLOADER_ADDR ; Reset vector ; use this for flashed system
reti ; EXT_INT0
rjmp uartBitbangIsrPcint0 ; PCI0
reti ; PCI1
reti ; WATCHDOG
reti ; ICP1
reti ; OC1A
reti ; OC1B
reti ; OVF1
rjmp baseTimerIrqOC0A ; OC0A
reti ; OC0B
reti ; OVF0
reti ; ACI
reti ; ADCC
reti ; ERDY
reti ; USI_STR
reti ; USI_OVF
devInfoBlock: ; 12 bytes
devInfoManufacturer: .db 'A', 'Q', 'U', 'A'
devInfoId: .db DEVICEINFO_ID, 0
devInfoVersion: .db DEVICEINFO_VERSION, DEVICEINFO_REVISION ; version, revision
firmwareVersion: .db FIRMWARE_VARIANT_TEMP_WINDOW, FIRMWARE_VERSION_MAJOR
.db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL
firmwareStart: rjmp main
; ***************************************************************************
; includes
.include "common/utils.asm"
.include "common/utils_wait_fixed.asm"
.include "common/utils_copy_from_flash.asm"
.include "common/utils_copy_sdram.asm"
.include "common/crc8.asm"
.include "modules/basetimer/main.asm"
#ifdef MODULES_TIMER
.include "modules/timer/main.asm"
#endif
#ifdef MODULES_LED
.include "modules/led/main.asm"
#endif
#ifdef MODULES_LED_SIMPLE
.include "modules/led_simple/main.asm"
#endif
#ifdef MODULES_COM
.include "modules/com2/defs.asm"
.include "modules/com2/main.asm"
.include "modules/com2/buffer.asm"
.include "modules/uart_bitbang/defs.asm"
.include "modules/uart_bitbang/main.asm"
.include "modules/uart_bitbang/bytelevel.asm"
.include "modules/uart_bitbang/packetlevel.asm"
#ifdef MODULES_COM_WITH_ADDR_PROTO
.include "modules/comproto/defs.asm"
.include "modules/comproto/main.asm"
.include "modules/comproto/addr.asm"
.include "modules/comproto/msg_recvstats.asm"
.include "modules/comproto/msg_sendstats.asm"
.include "modules/comproto/msg_sysstats.asm"
.include "modules/comproto/msg_memstats.asm"
.include "modules/comproto/msg_pong.asm"
.include "modules/comproto/msg_value.asm"
.include "modules/comproto/msg_device.asm"
.include "modules/comproto/msg_reboot.asm"
#endif
#ifdef MODULES_LCD
.include "modules/com2/screen.asm"
.include "modules/comproto/screen.asm"
#endif
#endif
#ifdef MODULES_TWI_MASTER
.include "modules/twimaster/main.asm"
#endif
#ifdef MODULES_OWI_MASTER
.include "modules/owimaster/main.asm"
#endif
#ifdef MODULES_LCD
.include "modules/lcd/main.asm"
#endif
#ifdef MODULES_SI7021
.include "modules/si7021/main.asm"
#endif
#ifdef MODULES_DS18B20
.include "modules/ds18b20/main.asm"
#endif
#ifdef MODULES_STATS
.include "modules/stats/main.asm"
#endif
#ifdef MODULES_CNY70
.include "modules/cny70/main.asm"
#endif
#ifdef MODULES_REED
.include "modules/reed/main.asm"
#endif
#ifdef MODULES_MOTION
.include "modules/motion/main.asm"
#endif
#ifdef MODULES_CCS811
.include "modules/ccs811/main.asm"
#endif
; test
;#include "modules/uart_irq/defs.asm"
;#include "modules/uart_irq/iface.asm"
;#include "modules/uart_irq/iface1.asm"
; ***************************************************************************
; data in SRAM
.dseg
programRamBegin:
#ifdef MODULES_LCD
screenCounter: .byte 1
#endif
programRamEnd:
#ifdef MODULES_LCD
sramTimerWriteStats: .byte 2
sramTimerScreen: .byte 2
#endif
#ifdef MODULES_SI7021
sramTimerSI7021Measure: .byte 2
sramTimerSI7021SendTemp: .byte 2
sramTimerSI7021SendHumidity: .byte 2
#endif
#ifdef MODULES_CNY70
sramTimerCny70SendAdc: .byte 2
#endif
#ifdef MODULES_LCD
sramPeriodicalLcdMark: .byte 2
#endif
#ifdef MODULES_DS18B20
sramDs18b20Timer: .byte 2
sramSendDs18b20TempTimer: .byte 2
#endif
#ifdef MODULES_CCS811
sramCcs811Timer: .byte 2
#endif
; ***************************************************************************
; data in FLASH
.cseg
; ---------------------------------------------------------------------------
; timer list
timerList:
; SRAM variable/counter routine flags secs (0=don't start or restart)
#ifdef MODULES_COM_WITH_ADDR_PROTO
.dw cproAddresModeTimer, CPRO_Address_OnTimer, 0, 0 ; (no restart)
#endif
#ifdef MODULES_STATS
.dw statsSendTimer, Stats_Timer, TIMER_FLAGS_IF_ADDR, 9000 ; every 15m
#endif
#ifdef MODULES_LCD
; .dw sramPeriodicalLcdMark, periodicalLcdMark, 0, 20 ; every 2s
; .dw sramTimerWriteStats, writeStats, 0, 100
.dw sramTimerScreen, printScreen, TIMER_FLAGS_IF_ADDR, 50 ; every 5s
#endif
#ifdef MODULES_SI7021
.dw sramTimerSI7021Measure, SI7021_OnTimer, 0, 300 ; every 30s
.dw sramTimerSI7021SendTemp, sendSI7021Temp, TIMER_FLAGS_IF_ADDR, 600 ; every 60s
.dw sramTimerSI7021SendHumidity, sendSI7021Humidity, TIMER_FLAGS_IF_ADDR, 600 ; every 60s
#endif
#ifdef MODULES_CNY70
.dw sramTimerCny70SendAdc, CNY70_OnTimer, TIMER_FLAGS_IF_ADDR, 50 ; every 5s
#endif
#ifdef MODULES_DS18B20
.dw sramDs18b20Timer, Ds18b20_OnTimer, 0, 300 ; every 30s
.dw sramSendDs18b20TempTimer, sendDs18b20Temp, TIMER_FLAGS_IF_ADDR, 600 ; every 60s
#endif
#ifdef MODULES_CCS811
.dw sramCcs811Timer, CCS811_OnTimer, 0, 10 ; every 1s
#endif
.dw 0 ; end of list
.include "main_all.asm"
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
ret
systemSetBootSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret
; ---------------------------------------------------------------------------
; Called on first time run, i.e. on system start. No arguments, no results.
onSystemStart:
ldi xh, HIGH(programRamBegin)
ldi xl, LOW(programRamBegin)
clr r16
ldi r17, (programRamEnd-programRamBegin)
rcall Utils_FillSram
ret
#ifdef MODULES_LCD
printScreen:
lds r16, screenCounter
tst r16
brne printScreen_l1
rcall CPRO_Screen
rjmp printScreen_counter
printScreen_l1:
cpi r16, 1
brne printScreen_l2
rcall COM2_Screen_RecvStats
rjmp printScreen_counter
printScreen_l2:
; cpi r16, 2
; brne printScreen_l3
; rcall COM2_Screen_SendStats
; rjmp printScreen_counter
printScreen_l3:
; add more screens here
printScreen_counter:
lds r16, screenCounter
inc r16
cpi r16, 2 ; number of screens
brcs printScreen_store
clr r16
printScreen_store:
sts screenCounter, r16
printScreen_end:
ret
;periodicalLcdMark:
; rcall printTimerMark
; ret
;writeStats:
; rcall printSendStats
; ret
#endif
#ifdef MODULES_SI7021
sendSI7021Humidity:
rcall SI7021_SendHumidity
brcs sendSI7021Humidity_okay
; set timer to 1s to retry later
ldi xl, LOW(sramTimerSI7021SendHumidity)
ldi xh, HIGH(sramTimerSI7021SendHumidity)
rjmp Timer_SetValueTo1s
sendSI7021Humidity_okay:
ret
sendSI7021Temp:
rcall SI7021_SendTemp
brcs sendSI7021Temp_okay
; set timer to 1s to retry later
ldi xl, LOW(sramTimerSI7021SendTemp)
ldi xh, HIGH(sramTimerSI7021SendTemp)
rjmp Timer_SetValueTo1s
sendSI7021Temp_okay:
ret
#endif
#ifdef MODULES_DS18B20
sendDs18b20Temp:
rcall Ds18b20_SendTemp
brcs sendDs18b20Temp_okay
; set timer to 1s to retry later
ldi xl, LOW(sramSendDs18b20TempTimer)
ldi xh, HIGH(sramSendDs18b20TempTimer)
rjmp Timer_SetValueTo1s
sendDs18b20Temp_okay:
ret
#endif
; ---------------------------------------------------------------------------
; Called every 100ms. Add your routine calls here. No arguments, no results.
onEvery100ms:
#ifdef MODULES_LED_SIMPLE
rcall LedSimple_Every100ms
#endif
#ifdef MODULES_REED
rcall REED_Every100ms
#endif
#ifdef MODULES_MOTION
rcall Motion_Every100ms
#endif
ret
; ---------------------------------------------------------------------------
; @routine onPacketReceived:
;
; Called after a packet was received via COM module. Add your routine calls here.
;
; The packet will be released in any case after return from this call.
;
; @return CFLAG set if message handled, cleared otherwise
; @param X pointer to received buffer
; @clobbers all
onPacketReceived:
#ifdef MODULES_MOTION_LIGHT
rcall MotionLight_OnPacketReceived
brcs onPacketReceived_end
#endif
#ifdef MODULES_SK6812
rcall SK6812_OnPacketReceived
brcs onPacketReceived_end
#endif
#ifdef MODULES_REED
rcall REED_OnPacketReceived
brcs onPacketReceived_end
#endif
#ifdef MODULES_COM
rcall CPRO_OnPacketReceived
brcs onPacketReceived_end
#endif
clc
onPacketReceived_end:
ret
; @end