Rename Files of different Devices
[owSlave2.git] / DS2423 / DS2423.c
1
2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //  * Redistributions of source code must retain the above copyright
10 //    notice, this list of conditions and the following disclaimer.
11 //  * Redistributions in binary form must reproduce the above copyright
12 //    notice, this list of conditions and the following disclaimer in the
13 //    documentation and/or other materials provided with the
14 //    distribution.
15 //  * All advertising materials mentioning features or use of this
16 //    software must display the following acknowledgement: This product
17 //    includes software developed by tm3d.de and its contributors.
18 //  * Neither the name of tm3d.de nor the names of its contributors may
19 //    be used to endorse or promote products derived from this software
20 //    without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34 #define _4_COUNTERS_
35 #define _EEPROM_SAVE_
36 //#define _CPULLUP_
37
38 #define F_CPU 8000000UL
39 #include <avr/io.h>
40 #include <avr/interrupt.h>
41 #include <util/delay.h>
42 #include <avr/wdt.h>
43 #include <avr/sleep.h>
44
45
46 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
47
48 #if defined(_4_COUNTERS_)&&defined(_EEPROM_SAVE_)
49 #error on ATtinyX5 you can not enable four counters and EEPROM save
50 #endif
51 #endif
52
53
54 extern void OWINIT();
55
56 uint8_t owid[8]={0x1D, 0xA2, 0xD9, 0x84, 0x00, 0x26, 0x02, 0x5C};/**/
57 uint8_t config_info[16]={0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
58
59         
60
61 extern uint8_t mode;
62 extern uint8_t gcontrol;
63 extern uint8_t reset_indicator;
64
65
66 volatile uint8_t wdcounter;
67
68
69
70
71 typedef union {
72         volatile uint8_t bytes[45];
73         struct {
74                 uint16_t addr;
75                 uint8_t status;
76                 uint8_t scratch[32];//3
77                 uint32_t counter;  //35
78                 uint32_t zero;   //39
79                 uint16_t crc;  //43
80         };
81 } counterpack_t;
82 counterpack_t pack;
83
84 volatile uint8_t lastcps;
85 typedef union {
86         uint32_t c32[4];
87         uint8_t c8[16];
88 } counters_t;
89
90 volatile counters_t counters;
91
92 volatile uint8_t istat;
93
94 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
95 #define PCINT_VECTOR PCINT0_vect
96 #define PIN_REG PINB
97 #define PIN_DDR DDRB
98 #define PIN_CH2 (1<<PINB3)
99 #define PIN_CH3 (1<<PINB4)
100 #define PIN_CH0 (1<<PINB0)
101 #define PIN_CH1 (1<<PINB1)
102 #define PORT_EE PINB
103 #define PIN_EE (1<<PINB0)
104 #define TEST_TIMER  ((TIMSK & (1<<TOIE0))==0)
105 #endif
106
107
108 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
109 #define PCINT_VECTOR PCINT0_vect
110 #define PIN_REG PINA
111 #define PIN_DDR DDRA
112 #define PIN_CH2 (1<<PINA4)
113 #define PIN_CH3 (1<<PINA5)
114 #define PIN_CH0 (1<<PINA6)
115 #define PIN_CH1 (1<<PINA7)
116 #define PORT_EE PINA //WARNING have to be the same PORT like PINREG because of istat
117 #define PIN_EE (1<<PINA0)
118 #define TEST_TIMER  ((TIMSK0 & (1<<TOIE0))==0)
119
120 #endif
121
122
123 ISR(PCINT0_vect) {
124         if (((PIN_REG&PIN_CH2)==0)&&((istat&PIN_CH2)==PIN_CH2)) {       counters.c32[2]++;      }
125         if (((PIN_REG&PIN_CH3)==0)&&((istat&PIN_CH3)==PIN_CH3)) {       counters.c32[3]++;      }
126         #ifdef _4_COUNTERS_
127         if (((PIN_REG&PIN_CH0)==0)&&((istat&PIN_CH0)==PIN_CH0)) {       counters.c32[0]++;      }
128         if (((PIN_REG&PIN_CH1)==0)&&((istat&PIN_CH1)==PIN_CH1)) {       counters.c32[1]++;      }
129         #endif
130         istat=PIN_REG;
131         #ifdef _EEPROM_SAVE_
132         if (((PORT_EE&PIN_EE)==0)&&((istat&PIN_EE)==PIN_EE)) {
133                 //PORTB&=~(1<<PINB1);
134                 EEARH=0;
135 #ifndef _4_COUNTERS_
136                 counters.c32[0]++;//Count the save cycles 
137 #endif
138                 for(uint8_t i=0;i<16;i++) {
139                         uint8_t addr=i^0x0C;
140                         while(EECR & (1<<EEPE));
141                         EECR = (0<<EEPM1)|(0<<EEPM0);
142                         EEARL = i;
143                         EEDR = counters.c8[addr];
144                         EECR |= (1<<EEMPE);
145                         EECR |= (1<<EEPE);
146                 }
147                 //PORTB|=(1<<PINB1);
148         }
149         #endif
150 }
151
152
153 int main(void){
154     PRR|=(1<<PRUSI)|(1<<PRADC);  //Switch off usi and adc for save Power
155         OWINIT();
156         
157         pack.zero=0;
158         counters.c32[0]=223433;
159         counters.c32[2]=233;
160         counters.c32[1]=3433;
161         counters.c32[3]=22433;
162         ACSR|=(1<<ACD);  //Disable Comparator
163         ADCSRB|=(1<<ACME); //Disable Analog multiplexer
164         MCUCR &=~(1<<PUD); //All Pins Pullup...
165         
166                 
167 #if  defined(__AVR_ATtiny25__)||defined(__AVR_ATtiny45__)  || defined(__AVR_ATtiny85__)
168         PORTB|=(1<<PINB5) //Reset
169 #ifdef _CPULLUP_
170         |PIN_CH2|PIN_CH3
171 #ifdef _4_COUNTERS_     
172         |PIN_CH0|PIN_CH1
173 #endif  
174 #endif  
175         ;
176         
177         GIMSK|=(1<<PCIE);
178         
179         PCMSK=(1<<PCINT3)|(1<<PCINT4)
180 #ifdef _4_COUNTERS_
181         |(1<<PCINT0)|(1<<PCINT1)
182 #endif
183         ;
184 #ifdef _EEPROM_SAVE_
185         DDRB&=~PIN_EE;
186         PCMSK|=(1<<PCINT0);
187         PORTB&=~PIN_EE; //Spannungsueberpruefung
188 #endif
189         DDRB &=~((1<<PINB3)|(1<<PINB4)
190 #ifdef _4_COUNTERS_
191         |(1<<PINB0)|(1<<PINB1)
192 #endif
193         );
194         istat=PIN_REG;
195 #endif
196
197
198 #if  defined(__AVR_ATtiny24__)||defined(__AVR_ATtiny44__)  || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny24A__)||defined(__AVR_ATtiny44A__)  || defined(__AVR_ATtiny84A__)
199
200         PORTB|=0xFF-(1<<PINB2); //Make PullUp an all Pins but not OW_PIN
201         PORTA|=0xFF; 
202         if (PINB&(1<<PINB0)) { 
203         } else {//SW set, PINB0 =0 no pullup, clear PORT of ios
204                 PORTA&=~((1<<PINA4)|(1<<PINA5));
205 #ifdef _4_COUNTERS_
206                 PORTA&=~((1<<PINA6)|(1<<PINA7));
207 #endif
208                 PORTB&=~(1<<PINB0);  //Disable Pullup io switch to save Power
209                                 
210         } 
211
212         GIMSK|=(1<<PCIE0);
213         PCMSK0=(1<<PCINT4)|(1<<PCINT5)
214 #ifdef _4_COUNTERS_
215                 |(1<<PCINT6)|(1<<PCINT7)
216 #endif
217         ;
218 #ifdef _EEPROM_SAVE_
219         DDRA&=~(1<<PINA0);
220         PCMSK0|=(1<<PCINT0);
221         PORTA&=~(1<<PINA0); //Spannungsueberpruefung
222 #endif
223         istat=PINB;
224 #endif
225
226 #ifdef _EEPROM_SAVE_
227                 EEARH=0;
228                 
229                 uint8_t addr;
230                 for(uint8_t i=0;i<16;i++) {
231                         addr=i^0x0C;
232                         while(EECR & (1<<EEPE));   
233                         EEARL=i;
234                         EECR |= (1<<EERE);
235                         counters.c8[addr]=EEDR;
236                 }
237                 for (uint8_t i=0;i<4;i++) {
238                         if (counters.c32[i]==0xFFFFFFFF) counters.c32[i]=0;
239                         //counters.c32[i]=0;
240                 }
241
242                 /*for(uint8_t i=0;i<16;i++) {
243                         while(EECR & (1<<EEPE));
244                         EECR = (1<<EEPM0);
245                         EEARL = i;
246                         EECR |= (1<<EEMPE);
247                         EECR |= (1<<EEPE);
248                 }*/
249         
250    
251 #endif
252         sei();
253     while(1)   {
254                 
255                 //Test if timer active and no sleep then Idle else Power Down
256                 if (TEST_TIMER&&(mode==0)) {
257                         MCUCR|=(1<<SE)|(1<<SM1); //Power Down, only low level on 1-Wire and pin change on PCINT wakes up
258                         MCUCR&=~(1<<ISC01);
259                 } else {
260                         MCUCR|=(1<<SE);
261                         MCUCR&=~(1<<SM1); 
262                 }
263                 asm("SLEEP");
264    }
265 }