Remove tools - Create extra repository
[owSlave2.git] / DS2406 / DS2406.c
1 \r
2 // Copyright (c) 2015, Tobias Mueller tm(at)tm3d.de\r
3 // All rights reserved.\r
4 //\r
5 // Redistribution and use in source and binary forms, with or without\r
6 // modification, are permitted provided that the following conditions are\r
7 // met:\r
8 //\r
9 //  * Redistributions of source code must retain the above copyright\r
10 //    notice, this list of conditions and the following disclaimer.\r
11 //  * Redistributions in binary form must reproduce the above copyright\r
12 //    notice, this list of conditions and the following disclaimer in the\r
13 //    documentation and/or other materials provided with the\r
14 //    distribution.\r
15 //  * All advertising materials mentioning features or use of this\r
16 //    software must display the following acknowledgement: This product\r
17 //    includes software developed by tm3d.de and its contributors.\r
18 //  * Neither the name of tm3d.de nor the names of its contributors may\r
19 //    be used to endorse or promote products derived from this software\r
20 //    without specific prior written permission.\r
21 //\r
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\r
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\r
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\r
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
33 \r
34 \r
35 #define F_CPU 8000000UL\r
36 #include <avr/io.h>\r
37 #include <avr/interrupt.h>\r
38 #include <util/delay.h>\r
39 #include <avr/wdt.h>\r
40 #include <avr/sleep.h>\r
41 \r
42 \r
43 extern void OWINIT();\r
44 \r
45 uint8_t owid[8]={0x12, 0xA2, 0xD9, 0x84, 0x00, 0x26, 0x02, 0x1E};/**/\r
46 \r
47         \r
48 \r
49 extern uint8_t mode;\r
50 extern uint8_t gcontrol;\r
51 extern uint8_t reset_indicator;\r
52 \r
53 \r
54 \r
55 typedef union {\r
56         volatile uint8_t bytes[1];\r
57         struct {\r
58                 uint8_t status;\r
59         };\r
60 } pack_t;\r
61 pack_t pack;\r
62 \r
63 \r
64 \r
65 \r
66 int main(void){\r
67     PRR|=(1<<PRUSI)|(1<<PRADC);  //Switch off usi and adc for save Power\r
68         OWINIT();\r
69         \r
70         ACSR|=(1<<ACD);  //Disable Comparator\r
71         ADCSRB|=(1<<ACME); //Disable Analog multiplexer\r
72         MCUCR &=~(1<<PUD); //All Pins Pullup...\r
73         \r
74                 \r
75         \r
76 \r
77 \r
78 \r
79 #ifdef _EEPROM_SAVE_\r
80                 EEARH=0;\r
81                 \r
82                 uint8_t addr;\r
83                 for(uint8_t i=0;i<16;i++) {\r
84                         addr=i^0x0C;\r
85                         while(EECR & (1<<EEPE));   \r
86                         EEARL=i;\r
87                         EECR |= (1<<EERE);\r
88                         counters.c8[addr]=EEDR;\r
89                 }\r
90                 for (uint8_t i=0;i<4;i++) {\r
91                         if (counters.c32[i]==0xFFFFFFFF) counters.c32[i]=0;\r
92                         //counters.c32[i]=0;\r
93                 }\r
94 \r
95                 /*for(uint8_t i=0;i<16;i++) {\r
96                         while(EECR & (1<<EEPE));\r
97                         EECR = (1<<EEPM0);\r
98                         EEARL = i;\r
99                         EECR |= (1<<EEMPE);\r
100                         EECR |= (1<<EEPE);\r
101                 }*/\r
102         \r
103    \r
104 #endif\r
105         sei();\r
106     while(1)   {\r
107                 \r
108                 //Test if timer active and no sleep then Idle else Power Down\r
109                 if (TEST_TIMER&&(mode==0)) {\r
110                         MCUCR|=(1<<SE)|(1<<SM1); //Power Down, only low level on 1-Wire and pin change on PCINT wakes up\r
111                         MCUCR&=~(1<<ISC01);\r
112                 } else {\r
113                         MCUCR|=(1<<SE);\r
114                         MCUCR&=~(1<<SM1); \r
115                 }\r
116                 asm("SLEEP");\r
117    }\r
118 }