Many changes from 2018
[owSlave2.git] / common / OWSet_ATMEGA168.s
1 // Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de\r
2 // All rights reserved. \r
3 // \r
4 // Redistribution and use in source and binary forms, with or without \r
5 // modification, are permitted provided that the following conditions are \r
6 // met: \r
7 // \r
8 //  * Redistributions of source code must retain the above copyright \r
9 //    notice, this list of conditions and the following disclaimer. \r
10 //  * Redistributions in binary form must reproduce the above copyright \r
11 //    notice, this list of conditions and the following disclaimer in the \r
12 //    documentation and/or other materials provided with the \r
13 //    distribution. \r
14 //  * All advertising materials mentioning features or use of this \r
15 //    software must display the following acknowledgement: This product \r
16 //    includes software developed by tm3d.de and its contributors. \r
17 //  * Neither the name of tm3d.de nor the names of its contributors may \r
18 //    be used to endorse or promote products derived from this software \r
19 //    without specific prior written permission. \r
20 // \r
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \r
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT \r
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR \r
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT \r
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, \r
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT \r
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, \r
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY \r
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \r
32 \r
33 \r
34 #define OW_PORT _SFR_IO_ADDR(PORTD) //1 Wire Port\r
35 #define OW_PIN _SFR_IO_ADDR(PIND) //1 Wire Pin as number\r
36 #define OW_PINN PORTD2\r
37 #define OW_DDR _SFR_IO_ADDR(DDRD)  //pin direction register\r
38 \r
39 #define TCNT_REG _SFR_IO_ADDR(TCNT0)\r
40 \r
41 \r
42 #define DB_PORT _SFR_IO_ADDR(PORTB) //DEBUG\r
43 #define DB_PIN _SFR_IO_ADDR(PINB) //DEBUG\r
44 #define DB_DDR _SFR_IO_ADDR(DDRB) //DEBUG\r
45 #define DB_PINN PORTB1\r
46 \r
47 #define SETZEROMARKER sbi _SFR_IO_ADDR(DDRC),6\r
48 #define RESETZEROMARKER cbi _SFR_IO_ADDR(DDRC),6\r
49 #define TESTZEROMARKER sbic _SFR_IO_ADDR(DDRC),6\r
50 \r
51 #ifdef _DB_\r
52 #define sdb sbi _SFR_IO_ADDR(PORTB),DB_PINN\r
53 #define cdb cbi _SFR_IO_ADDR(PORTB),DB_PINN\r
54 #else\r
55 #define sdb\r
56 #define cdb\r
57 #endif\r
58 \r
59 #define TIMER_INTERRUPT TIMER0_OVF_vect\r
60 #define PIN_INTERRUPT INT0_vect\r
61 \r
62 \r
63 //#define OWT_MIN_RESET 160 \r
64 //#define OWT_RESET2 40\r
65 //#define OWT_RESET_PRESENT 15\r
66 //#define OWT_PRESENT 50\r
67 //#define OWT_WRITE 18\r
68 //#define OWT_READ 4\r
69 #ifdef __4MHZ__\r
70 #define OWT_MIN_RESET 80\r
71 #define OWT_RESET2 30\r
72 #define OWT_RESET_PRESENT 15\r
73 #define OWT_PRESENT 60\r
74 #define OWT_WRITE 16\r
75 #define OWT_READ 5\r
76 #define OWT_ZP_WAIT_LOW_TO 36  ; Zeit fuer die Maximale Lowzeit bei der Zerro Polling pruefung. Ist die Leitung laenger low, ist es vieleicht ein Reset \r
77 #define OWT_ZP_WAIT_HIGH_TO 54 ; Zeit fuer die Maximale Hight zeit bis zu einem Low Impuls vom Master. Nach laengeren Pausen wird nicht mehr gepollt sondern der Interrup vererndet.\r
78 #else\r
79 #define OWT_MIN_RESET 180\r
80 #define OWT_RESET2 80\r
81 #define OWT_RESET_PRESENT 30\r
82 #define OWT_PRESENT 130\r
83 #define OWT_WRITE 35\r
84 #define OWT_READ 12\r
85 #define OWT_ZP_WAIT_LOW_TO 60  ; Zeit fuer die Maximale Lowzeit bei der Zerro Polling pruefung. Ist die Leitung laenger low, ist es vieleicht ein Reset \r
86 #define OWT_ZP_WAIT_HIGH_TO 90 ; Zeit fuer die Maximale Hight zeit bis zu einem Low Impuls vom Master. Nach laengeren Pausen wird nicht mehr gepollt sondern der Interrup vererndet.\r
87 #endif\r
88 \r
89 .macro CLEAR_TOV_FLAG\r
90         ldi r_temp,1\r
91         out _SFR_IO_ADDR(TIFR0),r_temp\r
92 .endm\r
93 \r
94 .macro JMP_NO_TOV\r
95         in r_temp, _SFR_IO_ADDR(TIFR0)\r
96         sbrc r_temp,TOV0 ; wenn ueberlauf gleich weiter\r
97 .endm\r
98 \r
99 .macro CLEAR_INTERRUPT_FLAG \r
100         ldi r_temp,(1<<INTF0);inerrupt flags durch 1 loeschen..... 0 macht nix\r
101         out _SFR_IO_ADDR(EIFR),r_temp\r
102 .endm\r
103 \r
104 .macro EN_TIM_INT\r
105         ldi r_temp,(1<<TOIE0)\r
106         sts TIMSK0,r_temp\r
107         ldi r_temp,(1<<TOV0) ;inerrupt flags durch 1 loeschen..... 0 macht nix\r
108         out _SFR_IO_ADDR(TIFR0),r_temp\r
109 .endm\r
110 \r
111 .macro DIS_TIM_INT\r
112         ldi r_temp,0\r
113         sts TIMSK0,r_temp\r
114 .endm\r
115 \r
116 //Wiederherstellen von Idle mode und umschalten von Low-Level interrupt zu Falling edge\r
117 .macro SET_FALLING_RESET_SLEEP\r
118         lds r_temp,EICRA\r
119         ori r_temp,(1<<ISC01)\r
120         sts     EICRA,r_temp\r
121         lds r_temp,SMCR\r
122         andi r_temp,~(1<<SM1)\r
123         sts     SMCR,r_temp\r
124 .endm\r
125 \r
126 .macro HW_INIT  ;r_temp is pushed other Registers should be saved\r
127         ;set clock to 8 MHz\r
128         ldi r_temp,0x80;\r
129         sts CLKPR,r_temp\r
130         //ldi r_temp,(1<<CLKPS0)\r
131 #ifdef __4MHZ__\r
132         ldi r_temp,0x01;\r
133 #else\r
134         ldi r_temp,0\r
135 #endif\r
136         sts CLKPR,r_temp\r
137         ;Disable Timer int\r
138         ldi r_temp,0\r
139         sts TIMSK0,r_temp ;; is default\r
140         ;Enable Pin int\r
141         ldi r_temp,(1<<INT0)\r
142         out _SFR_IO_ADDR(EIMSK),r_temp\r
143         ;Set Timerclock to Clock / 8 (2us bei 4MHz) bzw 1us bei 8 MHz\r
144         ldi r_temp,(1<<CS01)\r
145         out _SFR_IO_ADDR(TCCR0B),r_temp\r
146         ;OWPin as input\r
147         cbi OW_DDR,OW_PINN ;; is default.... \r
148         cbi OW_PORT,OW_PINN ;; vereinfachung im Hauptprogram (PORTB=0xFF) wegen pullup\r
149         ;set falling edge\r
150         ldi r_temp,(1<<ISC01)\r
151         sts EICRA,r_temp\r
152 #ifdef _DB_\r
153         sbi DB_DDR,DB_PINN\r
154 #endif\r
155 .endm\r
156 \r
157 .macro JMP_FLASHER\r
158         ldi r_temp,0x00\r
159         push r_temp\r
160         ldi r_temp,0x3E\r
161         push r_temp\r
162         ret ; Direkter Sprung zum Bootloader\r
163 .endm\r
164 \r
165 .macro CHECK_BOOTLOADER_PIN\r
166 .endm\r
167 \r