avr: added r04.
This commit is contained in:
370
avr/devices/r04/main/main.asm
Normal file
370
avr/devices/r04/main/main.asm
Normal file
@@ -0,0 +1,370 @@
|
||||
; ***************************************************************************
|
||||
; 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/tn841def.inc" ; Define device ATtiny841
|
||||
.list
|
||||
|
||||
.include "../defs.asm"
|
||||
.include "version.asm"
|
||||
;.include "defs_all.asm"
|
||||
|
||||
.include "devices/all/defs.asm"
|
||||
|
||||
.include "common/calls.asm"
|
||||
.include "common/utils_wait.asm"
|
||||
.include "common/utils_io.asm"
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; defines
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; generic
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; firmware settings including list of modules used
|
||||
|
||||
|
||||
#define MAIN_WITHOUT_MSG_HANDLING ; we do message handling ourselfes
|
||||
|
||||
#define MODULES_CLOCK
|
||||
;#define MODULES_COM
|
||||
;#define MODULES_COM_WITH_ADDR_PROTO
|
||||
;#define MODULES_LED
|
||||
#define MODULES_LED_SIMPLE
|
||||
;#define MODULES_TWI_MASTER
|
||||
;#define MODULES_LCD
|
||||
;#define LCD_MINIMAL_FONT
|
||||
;#define MODULES_SI7021
|
||||
;#define MODULES_STATS
|
||||
;#define MODULES_CNY70
|
||||
;#define MODULES_REED
|
||||
;#define MODULES_OWI_MASTER
|
||||
;#define MODULES_DS18B20
|
||||
;#define MODULES_MOTION
|
||||
#define MODULES_NETWORK
|
||||
;#define MODULES_TTYONUART1
|
||||
#define MODULES_COMONUART0
|
||||
#define MODULES_COMONUART1
|
||||
#define APPS_STATS
|
||||
#define APPS_NETWORK
|
||||
|
||||
|
||||
|
||||
.equ NET_BUFFERS_NUM = 12
|
||||
.equ NET_BUFFERS_SIZE = 32
|
||||
|
||||
.equ UART_HW_MSGNUMINBUF_SIZE = 8
|
||||
.equ UART_HW_MSGNUMOUTBUF_SIZE = 8
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; defines for values
|
||||
|
||||
.equ VALUE_ID_LEDSIMPLE_TIMING = 0x88
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; code segment
|
||||
|
||||
.cseg
|
||||
.org 000000
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; Reset and interrupt vectors
|
||||
|
||||
rjmp BOOTLOADER_ADDR ; 1: RESET Reset vector use this for flashed system
|
||||
reti ; 2: INT0 External Interrupt Request 0
|
||||
rjmp onAttnChangeIsr ; 3: PCINT0 Pin Change Interrupt 0
|
||||
reti ; 4: PCINT1 Pin Change Interrupt 1
|
||||
reti ; 5: WDT Watchdog Time-out
|
||||
reti ; 6: TIM1_CAPT Timer/Counter1 Capture Event
|
||||
reti ; 7: TIM1_COMPA (OC1A) Timer/Counter1 Compare Match A
|
||||
reti ; 8: TIM1_COMPB (OC1B) Timer/Counter1 Compare Match B
|
||||
reti ; 9: TIM1_OVF (OVF1) Timer/Counter1 Overflow
|
||||
rjmp baseTimerIrqOC0A ; 10: TIM0_COMPA (OC0A) Timer/Counter0 Compare Match A
|
||||
reti ; 11: TIM0_COMPB (OC0B) Timer/Counter0 Compare Match B
|
||||
reti ; 12: TIM0_OVF (OVF0) Timer/Counter0 Overflow
|
||||
reti ; 13: ANA_COMP0 Analog Comparator 0
|
||||
reti ; 14: ADC_READY ADC Conversion Complete
|
||||
reti ; 15: EE_RDY (ERDY) EEPROM Ready
|
||||
reti ; 16: ANA_COMP1 Analog Comparator 1
|
||||
reti ; 17: TIM2_CAPT Timer/Counter2 Capture Event
|
||||
reti ; 18: TIM2_COMPA (OC2A) Timer/Counter2 Compare Match A
|
||||
reti ; 19: TIM2_COMPB (OC2B) Timer/Counter2 Compare Match B
|
||||
reti ; 20: TIM2_OVF (OVF2) Timer/Counter2 Overflow
|
||||
reti ; 21: SPI SPI Serial Transfer Complete
|
||||
reti ; 22: USART0_RXS USART0 Rx Start
|
||||
rjmp ComOnUart0_RxCharIsr ; 23: USART0_RXC USART0 Rx Complete
|
||||
rjmp ComOnUart0_TxUdreIsr ; 24: USART0_DRE USART0 Data Register Empty
|
||||
rjmp ComOnUart0_TxCharIsr ; 25: USART0_TXC USART0 Tx Complete
|
||||
reti ; 26: USART1_RXS USART1 Rx Start
|
||||
rjmp ComOnUart1_RxCharIsr ; 27: USART1_RXC USART1 Rx Complete
|
||||
rjmp ComOnUart1_TxUdreIsr ; 28: USART1_DRE USART1 Data Register Empty
|
||||
rjmp ComOnUart1_TxCharIsr ; 29: USART1_TXC USART1 Tx Complete
|
||||
reti ; 30: TWI Two-Wire-Interface
|
||||
reti ; 31: RESERVED reserved
|
||||
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine firmwareStart @global
|
||||
|
||||
firmwareStart:
|
||||
rjmp main
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine onSystemStart
|
||||
|
||||
onSystemStart:
|
||||
clr r16
|
||||
sts debugCounter, r16
|
||||
ldi r16, 0xff
|
||||
sts attnByte, r16
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
onEvery100ms:
|
||||
onEverySecond:
|
||||
onEveryHour:
|
||||
onEveryDay:
|
||||
ret
|
||||
|
||||
|
||||
|
||||
onEveryMinute:
|
||||
rcall sendPacketsIface2In
|
||||
rcall sendErrorsIface2
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine onEveryLoop
|
||||
;
|
||||
; Called on every loop (i.e. after awakening from sleep).
|
||||
|
||||
onEveryLoop:
|
||||
rcall checkRecvdMsg
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine onMessageReceived
|
||||
;
|
||||
; Called on every message received
|
||||
|
||||
onMessageReceived:
|
||||
clc
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine onAttnChange @global @isr
|
||||
;
|
||||
; Called whenever PCINT0 is activated.
|
||||
; This code assumes that both ATTN pins are on the same port which is true for
|
||||
; R04 nodes.
|
||||
|
||||
onAttnChangeIsr:
|
||||
push r15
|
||||
in r15, SREG
|
||||
push r16
|
||||
in r16, COM_ATTN_INPUT
|
||||
push r17
|
||||
lds r17, attnByte
|
||||
sts attnByte, r16
|
||||
eor r17, r16
|
||||
sbrc r17, COM_ATTN1_PIN
|
||||
rcall onAttnChangeAttn1
|
||||
sbrc r17, COM_ATTN_PIN
|
||||
rcall onAttnChangeAttn0
|
||||
pop r17
|
||||
pop r16
|
||||
out SREG, r15
|
||||
pop r15
|
||||
reti
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
onAttnChangeAttn0:
|
||||
sbrs r16, COM_ATTN_PIN
|
||||
rcall ComOnUart0_HandleAttnChange ; (none)
|
||||
ret
|
||||
|
||||
onAttnChangeAttn1:
|
||||
sbrs r16, COM_ATTN1_PIN
|
||||
rcall ComOnUart1_HandleAttnChange ; (none)
|
||||
ret
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine checkRecvdMsg
|
||||
;
|
||||
; Read messages from either interface and forward to the other one.
|
||||
|
||||
checkRecvdMsg:
|
||||
rcall NET_PeekNextIncomingMsgNum ; check read queue (bufNum->r16)
|
||||
brcc checkRecvdMsg_end ; no msg, jmp
|
||||
rcall NET_Buffer_Locate ; (R17)
|
||||
; let system handle incoming messages
|
||||
push r16
|
||||
push xl
|
||||
push xh
|
||||
adiw xh:xl, 1
|
||||
rcall onMessageReceived
|
||||
rcall mainModulesOnPacketReceived
|
||||
rcall mainAppsOnPacketReceived
|
||||
pop xh
|
||||
pop xl
|
||||
pop r16
|
||||
|
||||
; forward to other interface
|
||||
ld r17, X
|
||||
andi r17, (NET_IFACE_BUFFER_IFACENUM1_BIT | NET_IFACE_BUFFER_IFACENUM0_BIT)
|
||||
rcall reverseInterfaceNum ; (R16, R17)
|
||||
; ldi r17, COMONUART0_IFACENUM ; DEBUG: send everything to uart0 to test that code first
|
||||
rcall addMsgToInterface
|
||||
brcc checkRecvdMsg_end ; could not add, jmp
|
||||
rcall NET_GetNextIncomingMsgNum ; take off the queue
|
||||
rjmp checkRecvdMsg
|
||||
checkRecvdMsg_end:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine reverseInterfaceNum
|
||||
;
|
||||
; @param r17 buffer num
|
||||
; @return r17 reversed interface number
|
||||
; @clobbers r16, r17
|
||||
|
||||
reverseInterfaceNum:
|
||||
ldi r16, (NET_IFACE_BUFFER_IFACENUM1_BIT | NET_IFACE_BUFFER_IFACENUM0_BIT)
|
||||
eor r17, r16
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine addMsgToInterface
|
||||
; @param r16 buffer num
|
||||
; @param r17 interface num
|
||||
|
||||
addMsgToInterface:
|
||||
cpi r17, COMONUART0_IFACENUM
|
||||
brne addMsgToInterface_notUart0
|
||||
ldi yl, LOW(comOnUart0_iface)
|
||||
ldi yh, HIGH(comOnUart0_iface)
|
||||
rjmp NET_Interface_AddOutgoingMsgNum ; try to add msg to interface
|
||||
addMsgToInterface_notUart0:
|
||||
ldi yl, LOW(comOnUart1_iface)
|
||||
ldi yh, HIGH(comOnUart1_iface)
|
||||
rjmp NET_Interface_AddOutgoingMsgNum ; try to add msg to interface
|
||||
addMsgToInterface_end:
|
||||
clc
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
sendPacketsIface2In:
|
||||
ldi yl, LOW(netInterfaceData)
|
||||
ldi yh, HIGH(netInterfaceData)
|
||||
ldi r17, AQHOME_VALUEID_STATS_PACKETS2_IN
|
||||
lds r18, comOnUart1_iface+NET_IFACE_OFFS_PACKETSIN_LOW
|
||||
lds r19, comOnUart1_iface+NET_IFACE_OFFS_PACKETSIN_HIGH
|
||||
rjmp appStatsSend16BitValue
|
||||
|
||||
sendErrorsIface2:
|
||||
ldi yl, LOW(netInterfaceData)
|
||||
ldi yh, HIGH(netInterfaceData)
|
||||
ldi r17, 0xf0
|
||||
lds r18, comOnUart1_iface+NET_IFACE_OFFS_ERR_MISSED_LOW
|
||||
lds r19, comOnUart1_iface+NET_IFACE_OFFS_ERR_MISSED_HIGH
|
||||
; lds r18, debugCounter
|
||||
; clr r19
|
||||
rjmp appStatsSend16BitValue
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; includes
|
||||
|
||||
.include "devices/all/hw_tn841.asm"
|
||||
.include "devices/all/includes.asm"
|
||||
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; defines for network interface
|
||||
|
||||
.equ netInterfaceData = comOnUart0_iface
|
||||
|
||||
|
||||
.dseg
|
||||
attnByte: .byte 1
|
||||
debugCounter: .byte 1
|
||||
|
||||
Reference in New Issue
Block a user