2 #define CAL_MAGIC 0x9e // attiny4 handwired
3 //#define CAL_MAGIC 0x8d // attiny4 devboard
4 //#define CAL_MAGIC 0xa7 // attiny9 devboard
15 #define x r23 //==a1==Mh
25 #define Ml t //mod3 vars
50 .org 0x0000 ; RESET interrupt
52 .org 0x0002 ; INT0 interrupt
53 CBI EIMSK, 0 ; disable interrupt
55 .org 0x0008 ; TIM0_OVF interrupt
59 .byte 0x84, 0x9d, 0xb0, 0x69, 0x9d, 0x84, 0x69, 0x58
60 .byte 0x75, 0x8c, 0xb0, 0x69, 0x8c, 0x75, 0x69, 0x58
62 mod3: ; mod3(Mh.Ml) -> t
66 ADC Mh, Mh ; store carry in Mh
112 ; begin of mulitiplication:
121 ; BRCC skip2 -- this bit is always zero
164 MOV t, a1 ;;TODO: use a1 in loop: directly
169 main: ; setup routine
173 CLR acc ; we output a dummy sample before the actual first one
174 LDI Xhi, hi8(FLASHM + notes) ; never changes
175 LDI one, 1 ; mostly for clearing TIM0_OVF bit
179 OUT SPL, x ; init stack ptr
181 OUT PUEB, zero ; disable pullups
183 OUT DDRB, x ; PORTB0:pwm, PORTB1:debug (PORTB2:wakeup-input)
185 OUT CCP, x ; change protected ioregs
186 OUT CLKPSR, one ; clock prescaler 1/2 (4Mhz)
187 LDI x, CAL_MAGIC ; determined by trial-and-error (->PORTB1)
188 OUT OSCCAL, x ; set oscillator calibration
189 OUT WDTCSR, zero; turn off watchdog
191 ;set timer/counter0 to 8bit fastpwm, non-inverting, no prescaler
196 OUT TIMSK0, one ; enable tim0_ovf
201 CPI i2, 0x78 ; 16m23 -- one loop
207 //we use an external pullup(>= 1kohm), as line-input usually has an impedance
208 //between 20-100kohm, while the attiny's internal pullups are "only" 20-50kohm
209 //(which is too strong)
211 ;stop the music, and check whether PINB0 is plugged into an audio
212 ;sink. Until then, conserve as much battery as possible.
213 CLR i2 ; clear halt condition
216 ; disable timer to free audio pin for wakeup function:
223 ;assert high level on pullup pins to avoid accidentally triggering INT0:
226 OUT DDRB, zero ; set all pins as input
228 ;set up INT0 to wake up when a audio sink is connected
229 SBI EIMSK, 0 ; set-bit-0 high => enable interrupt
230 OUT EICRA, zero ; logical low generates INT0
232 ;enter power-down-mode
233 OUT SMCR, five ; sleep mode: power-down, enabled
235 ;OUT SMCR, one ; sleep mode: idle, enabled
236 OUT SMCR, zero ; sleep mode: disabled
242 LDI x, 0x03 ; restore output pins
245 OUT TCCR0A, x ; reenable COMA bits
247 OUT TCCR0B, x ; reenable timer0
252 OUT OCR0AL, acc ; start by outputting a sample, because routine has variable runtime
254 SBI PORTB, 1 ; to measure runtime
379 SWAP acc ; acc<<4, to be passed to OCR0AL
386 CBI PORTB, 1 ; end runtime measurement
388 OUT TIFR0, one ; clear pending interrupt (routine takes two intr.cycles)
389 RETI ; reenables interrupts