f545bedf651017e5c00d5e19deb14a196e26269f
[owSlave2.git] / DS2438_DHT22 / OWDS2438.S
1 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
2 // All rights reserved. 
3 // 
4 // Redistribution and use in source and binary forms, with or without 
5 // modification, are permitted provided that the following conditions are 
6 // met: 
7 // 
8 //  * Redistributions of source code must retain the above copyright 
9 //    notice, this list of conditions and the following disclaimer. 
10 //  * Redistributions in binary form must reproduce the above copyright 
11 //    notice, this list of conditions and the following disclaimer in the 
12 //    documentation and/or other materials provided with the 
13 //    distribution. 
14 //  * All advertising materials mentioning features or use of this 
15 //    software must display the following acknowledgement: This product 
16 //    includes software developed by tm3d.de and its contributors. 
17 //  * Neither the name of tm3d.de nor the names of its contributors may 
18 //    be used to endorse or promote products derived from this software 
19 //    without specific prior written permission. 
20 // 
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
32
33 #define _CHANGEABLE_ID_
34 //#define _DIS_FLASH_
35
36 #include "../common/OWConfig.s"
37 #include "../common/OWCRC8.s"
38
39 #if  defined(__AVR_ATtiny25__)
40 .extern pack,16
41 #else
42 .extern pack,64
43 #endif
44 .extern  am2302_temp,2
45 .extern  am2302_hum,2
46 .comm block,1 ; Block der augegeben, geschrieben wird (Parameter von READ/WRITE Scratchpad)
47
48
49 .macro CHIP_INIT
50 .endm
51
52 ; Ab hier Geraeteabhaenging
53 #define OW_READ_SCRATCHPAD_ADR OW_FIRST_COMMAND+0
54 #define OW_READ_SCRATCHPAD OW_FIRST_COMMAND+1
55 #define OW_WRITE_SCRATCHPAD_ADR OW_FIRST_COMMAND+2
56 #define OW_WRITE_SCRATCHPAD OW_FIRST_COMMAND+3
57
58
59
60 .macro COMMAND_TABLE
61                 rjmp h_readscratchpad_adr
62                 rjmp h_readscratchpad
63                 rjmp h_writescratchpad_adr
64                 rjmp h_writescratchpad
65 .endm
66
67 #include "../common/OWRomFunctions.s"
68 #include "../common/OWTimerInterrupt.s"
69
70
71 ;---------------------------------------------------
72 ;       READ COMMAND and start operation
73 ;---------------------------------------------------
74
75
76 h_readcommand:
77         clr r_bytep
78 #ifndef _DIS_FLASH_
79         FLASH_COMMANDS ; muss zu erst sein....
80 #endif
81         cset 0xBE,OW_READ_SCRATCHPAD_ADR
82         cset 0x4E,OW_WRITE_SCRATCHPAD_ADR
83         cjmp 0x44,hrc_set_convertT
84         cjmp 0xB4,hrc_set_convertV
85 #ifdef _CHANGEABLE_ID_
86         CHANGE_ID_COMMANDS
87 #endif
88         rjmp handle_end_sleep
89
90
91 hrc_set_convertT:
92         ldi r_temp,1
93         sts gcontrol,r_temp
94         lds r_temp,am2302_temp
95         sts pack+1,r_temp
96         lds r_temp,am2302_temp+1
97         sts pack+2,r_temp
98         rjmp handle_end_sleep
99 hrc_set_convertV:
100         ldi r_temp,2
101         sts gcontrol,r_temp
102         lds r_temp,pack
103         sbrs r_temp,3
104         rjmp hrc_set_convertVV
105         ldi r_temp,0xF4
106         sts pack+3,r_temp
107         ldi r_temp,0x01
108         sts pack+4,r_temp
109         rjmp hrc_set_convertend
110 hrc_set_convertVV:
111         lds r_temp,am2302_hum
112         sts pack+3,r_temp
113         lds r_temp,am2302_hum+1
114         sts pack+4,r_temp
115 hrc_set_convertend:
116         rjmp handle_end_sleep
117
118
119
120 ;---------------------------------------------------
121 ;   READ SCRATCHPAD
122 ;---------------------------------------------------
123
124 h_readscratchpad_adr:
125         lsl r_rwbyte
126         lsl r_rwbyte
127         lsl r_rwbyte
128 #if  defined(__AVR_ATtiny25__)
129         andi r_rwbyte,0x01 ;nur Page 0 und 1 und das immer wiederholen
130 #endif
131         sts block,r_rwbyte
132         ldi r_sendflag,1
133         ldi r_mode,OW_READ_SCRATCHPAD
134         CRCInit2
135 h_readscratchpad:
136         cpi  r_bytep,8
137         breq h_readscratchpad_crc
138         cpi  r_bytep,9
139         breq h_readscratchpad_all
140         lds  r_temp,block
141         add  r_temp,r_bytep
142         configZ pack,r_temp
143         ld   r_rwbyte,Z
144         rjmp handle_end_inc
145 h_readscratchpad_crc:
146         lds  r_rwbyte,crc
147         rjmp handle_end_inc
148 h_readscratchpad_all:
149         rjmp handle_end_sleep
150
151
152
153
154 ;---------------------------------------------------
155 ;   WRITE SCRATCHPAD
156 ;---------------------------------------------------
157 h_writescratchpad_adr:
158         lsl r_rwbyte
159         lsl r_rwbyte
160         lsl r_rwbyte
161 #if  defined(__AVR_ATtiny25__)
162         andi r_rwbyte,0x01 ;nur Page 0 und 1 und das immer wiederholen
163 #endif
164         sts block,r_rwbyte
165         ldi r_mode,OW_WRITE_SCRATCHPAD
166         ldi  r_bcount,1 
167         rjmp handle_end 
168 h_writescratchpad:
169         cpi  r_bytep,8
170         breq h_writescratchpad_all
171         lds  r_temp,block
172         add  r_temp,r_bytep
173         configZ pack,r_temp
174         st   Z,r_rwbyte
175         rjmp handle_end_inc
176 h_writescratchpad_all:
177         rjmp handle_end_sleep
178
179
180
181
182
183 #include "../common/OWPinInterrupt.s"
184 .end