New Devices
[owSlave2.git] / DS2423_komische platine / DS2423.c
1
2 // Copyright (c) 2016, 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
35 #define F_CPU 8000000UL
36 #include <avr/io.h>
37 #include <avr/interrupt.h>
38 #include <util/delay.h>
39 #include <avr/wdt.h>
40 #include <avr/sleep.h>
41
42
43
44
45 extern void OWINIT();
46
47 uint8_t owid[8]={0x1D, 0xA2, 0xD9, 0x84, 0x00, 0x26, 0x02, 0x5C};/**/
48 uint8_t config_info[16]={0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
49
50         
51
52 extern uint8_t mode;
53 extern uint8_t gcontrol;
54 extern uint8_t reset_indicator;
55
56
57 volatile uint8_t wdcounter;
58
59
60
61
62 typedef union {
63         volatile uint8_t bytes[45];
64         struct {
65                 uint16_t addr;
66                 uint8_t status;
67                 uint8_t scratch[32];//3
68                 uint32_t counter;  //35
69                 uint32_t zero;   //39
70                 uint16_t crc;  //43
71         };
72 } counterpack_t;
73 counterpack_t pack;
74
75 typedef union {
76         uint32_t c32[4];
77         uint8_t c8[16];
78 } counters_t;
79
80 volatile counters_t counters;
81
82 volatile uint8_t istat;
83
84 #define PCINT_VECTOR PCINT0_vect
85 #define PIN_REG PINA
86 #define PIN_DDR DDRA
87 #define PIN_CH0 (1<<PINA3)
88 #define PIN_CH1 (1<<PINA4)
89 #define PIN_CH2 (1<<PINA1)
90 #define PIN_CH3 (1<<PINA2)
91 #define LPIN_CH2 (1<<PINB0)
92 #define LDD_CH2 DDRB
93 #define LPORT_CH2 PORTB
94 #define LPIN_CH3 (1<<PINA5)
95 #define LDD_CH3 DDRA
96 #define LPORT_CH3 PORTA
97 #define LPIN_CH0 (1<<PINA7)
98 #define LDD_CH0 DDRA
99 #define LPORT_CH0 PORTA
100 #define LPIN_CH1 (1<<PINB1)
101 #define LDD_CH1 DDRB
102 #define LPORT_CH1 PORTB
103 #define PORT_EE PINA //WARNING have to be the same PORT like PINREG because of istat
104 #define PIN_EE (1<<PINA0)
105 #define TEST_TIMER  ((TIMSK0 & (1<<TOIE0))==0)
106
107
108
109 ISR(PCINT0_vect) {
110         if (((PIN_REG&PIN_CH0)==0)&&((istat&PIN_CH0)==PIN_CH0)) {       counters.c32[0]++;      LPORT_CH2|=LPIN_CH2;}
111         if (((PIN_REG&PIN_CH1)==0)&&((istat&PIN_CH1)==PIN_CH1)) {       counters.c32[1]++;      LPORT_CH2|=LPIN_CH2;}
112         if (((PIN_REG&PIN_CH2)==0)&&((istat&PIN_CH2)==PIN_CH2)) {       counters.c32[2]++;      LPORT_CH2|=LPIN_CH2;}
113         if (((PIN_REG&PIN_CH3)==0)&&((istat&PIN_CH3)==PIN_CH3)) {       counters.c32[3]++;      LPORT_CH2|=LPIN_CH2;}
114
115         if (((PORT_EE&PIN_EE)==0)&&((istat&PIN_EE)==PIN_EE))  {
116                 
117                 _delay_ms(50);
118         if (((PORT_EE&PIN_EE)==0))  {
119
120                 
121
122                 LPORT_CH1|=LPIN_CH1;
123                 EEARH=0;
124                 for(uint8_t i=0;i<16;i++) {
125                         uint8_t addr=i^0x0C;
126                         while(EECR & (1<<EEPE));
127                         EECR = (0<<EEPM1)|(0<<EEPM0);
128                         EEARL = i;
129                         EEDR = counters.c8[addr];
130                         EECR |= (1<<EEMPE);
131                         EECR |= (1<<EEPE);
132                 }
133                 _delay_ms(250);
134                 LPORT_CH1&=~LPIN_CH1;
135
136         }
137         GIFR|=(1<<PCIF0);
138         }
139         if ((((PINA&(1<<PINA6)))==0)&&((istat&(1<<PINA6))==(1<<PINA6)))   {
140                 _delay_ms(100);
141                 if (((PINA&(1<<PINA6)))==0) {
142                         LPORT_CH3|=LPIN_CH3;
143                         _delay_ms(100);
144
145                         counters.c32[0]=0;
146                         counters.c32[2]=0;
147                         counters.c32[1]=0;
148                         counters.c32[3]=0;
149                         LPORT_CH3&=~LPIN_CH3;
150                         }
151                 GIFR|=(1<<PCIF0);
152
153         }
154         istat=PIN_REG;
155         
156 }
157
158
159 int main(void){
160     PRR|=(1<<PRUSI)|(1<<PRADC);  //Switch off usi and adc for save Power
161         OWINIT();
162         
163         pack.zero=0;
164         counters.c32[0]=0;
165         counters.c32[2]=0;
166         counters.c32[1]=0;
167         counters.c32[3]=0;
168         ACSR|=(1<<ACD);  //Disable Comparator
169         ADCSRB|=(1<<ACME); //Disable Analog multiplexer
170         MCUCR &=~(1<<PUD); //All Pins Pullup...
171
172         PORTB|=0xFF-(1<<PINB2); //Make PullUp an all Pins but not OW_PIN
173         PORTA|=0xFF; 
174         PORTA&=~((1<<PINA1)|(1<<PINA2)|(1<<PINA3)|(1<<PINA4));
175                                 
176         GIMSK|=(1<<PCIE0);
177         PCMSK0=(1<<PCINT1)|(1<<PCINT2)|(1<<PCINT3)|(1<<PCINT4);
178         //Spannungsdetektor
179         DDRA&=~(1<<PINA0);
180         PCMSK0|=(1<<PCINT0);
181         PORTA&=~(1<<PINA0);
182         //Taster
183         DDRA&=~(1<<PINA6);
184         PCMSK0|=(1<<PCINT6);
185         PORTA|=(1<<PINA6); //Pullup
186
187
188
189         LDD_CH0|=LPIN_CH0;
190         LPORT_CH0&=~LPIN_CH0;
191         LDD_CH1|=LPIN_CH1;
192         LPORT_CH1&=~LPIN_CH1;
193         LDD_CH2|=LPIN_CH2;
194         LPORT_CH2&=~LPIN_CH2;
195         LDD_CH3|=LPIN_CH3;
196         LPORT_CH3&=~LPIN_CH3;
197
198
199
200         LPORT_CH0|=LPIN_CH0;
201         _delay_ms(500);
202         LPORT_CH0&=~LPIN_CH0;
203         
204         istat=PINA;
205         EEARH=0;
206         uint8_t addr;
207         for(uint8_t i=0;i<16;i++) {
208                 addr=i^0x0C;
209                 while(EECR & (1<<EEPE));   
210                 EEARL=i;
211                 EECR |= (1<<EERE);
212                 counters.c8[addr]=EEDR;
213         }
214         for (uint8_t i=0;i<4;i++) {
215                 if (counters.c32[i]==0xFFFFFFFF) counters.c32[i]=0;
216                 //counters.c32[i]=0;
217         }
218
219    
220         sei();
221     while(1)   {
222             if (LPORT_CH2&LPIN_CH2) {
223                         _delay_ms(50);
224                         LPORT_CH2&=~LPIN_CH2;
225                 }
226                 //Test if timer active and no sleep then Idle else Power Down
227                 if (TEST_TIMER&&(mode==0)) {
228                         MCUCR|=(1<<SE)|(1<<SM1); //Power Down, only low level on 1-Wire and pin change on PCINT wakes up
229                         MCUCR&=~(1<<ISC01);
230                 } else {
231                         MCUCR|=(1<<SE);
232                         MCUCR&=~(1<<SM1); 
233                 }
234                 asm("SLEEP");
235    }
236 }