Many changes from 2018
[owSlave2.git] / common / I2C / SGP30.c
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 #ifdef  __4MHZ__\r
33 #define F_CPU 4000000UL\r
34 #else\r
35 #define F_CPU 8000000UL\r
36 #endif\r
37 #include <avr/io.h>\r
38 \r
39 \r
40 #include <util/delay.h>\r
41 #include <avr/pgmspace.h>\r
42 \r
43 #include "USI_TWI_Master.h"\r
44 #include "SGP30.h"\r
45 //0x58\r
46 #define WC 0b10110000\r
47 #define RC 0b10110001\r
48 #define CRC8_POLYNOMIAL             0x31\r
49 #define CRC8_INIT                   0xFF\r
50 #define CRC8_LEN                    1\r
51 \r
52 \r
53 uint8_t sensirion_common_generate_crc(uint8_t *data, uint8_t count)\r
54 {\r
55         uint16_t current_byte;\r
56         uint8_t crc = CRC8_INIT;\r
57         uint8_t crc_bit;\r
58 \r
59         /* calculates 8-Bit checksum with given polynomial */\r
60         for (current_byte = 0; current_byte < count; ++current_byte) {\r
61                 crc ^= (data[current_byte]);\r
62                 for (crc_bit = 8; crc_bit > 0; --crc_bit) {\r
63                         if (crc & 0x80)\r
64                         crc = (crc << 1) ^ CRC8_POLYNOMIAL;\r
65                         else\r
66                         crc = (crc << 1);\r
67                 }\r
68         }\r
69         return crc;\r
70 }\r
71 \r
72 int8_t sensirion_common_check_crc(uint8_t *data, uint8_t count, uint8_t checksum)\r
73 {\r
74         if (sensirion_common_generate_crc(data, count) != checksum)\r
75         return 0;\r
76         return 1;\r
77 }\r
78 \r
79 void readSGPXX(uint16_t com, uint8_t* data, uint8_t len) {\r
80         I2c_StartCondition();\r
81         I2c_WriteByte(WC);\r
82         I2c_WriteByte(com>>8);  //Ctrl hum\r
83         I2c_WriteByte(com&0x00FF);  //Ctrl hum\r
84         _delay_ms(100);\r
85         I2c_StartCondition();\r
86         I2c_WriteByte (RC);\r
87         for(uint8_t i=0;i<len-1;i++) {\r
88                 data[i]=I2c_ReadByte(ACK);\r
89         }\r
90         data[len-1]=I2c_ReadByte(NO_ACK);\r
91         I2c_StopCondition();\r
92 }\r
93 \r
94 void writeSGPXX(uint16_t com, uint8_t* data, uint8_t len) {\r
95         I2c_StartCondition();\r
96         I2c_WriteByte(WC);\r
97         I2c_WriteByte(com>>8);  //Ctrl hum\r
98         I2c_WriteByte(com&0x00FF);  //Ctrl hum\r
99         for(uint8_t i=0;i<len;i++) {\r
100                 I2c_WriteByte(data[i]);\r
101         }\r
102         I2c_StopCondition();\r
103 }\r
104 \r
105 \r
106 \r
107 \r
108 \r
109 uint16_t bl1S=0,bl2S=0;\r
110 \r
111 uint16_t getbl=1000;
112 \r
113 \r
114 int8_t check_convert_buf(uint8_t *b,uint16_t *v1,uint16_t *v2) {\r
115         int8_t ret=1;\r
116         if (sensirion_common_check_crc(b,2,b[2])) {     *v1=((int16_t)b[0])<<8|b[1];} else {*v1=0;ret=0;}\r
117         if (sensirion_common_check_crc(b+3,2,b[5])) {*v2=((int16_t)b[3])<<8|b[4];} else {*v2=0;ret=0;}\r
118         return ret;\r
119 }\r
120 \r
121 \r
122 \r
123 uint16_t readEEPROM(uint8_t addr,uint16_t def) {\r
124         uint16_t hr;\r
125         EEARH=0;\r
126         while(EECR & (1<<EEPE));\r
127         EEARL=addr+1;\r
128         EECR |= (1<<EERE);\r
129         hr=EEDR;\r
130         if (hr!=0xFF) {\r
131                 hr=hr<<8;\r
132                 while(EECR & (1<<EEPE));\r
133                 EEARL=addr;\r
134                 EECR |= (1<<EERE);\r
135                 hr|=EEDR;\r
136                 return hr;\r
137         }\r
138         return def;\r
139 }\r
140 \r
141 void writeEEPROM(uint8_t addr,uint16_t val) {\r
142         EEARH=0;\r
143         while(EECR & (1<<EEPE));\r
144         EECR = (0<<EEPM1)|(0<<EEPM0);\r
145         EEARL = addr;\r
146         EEDR = val&0xFF;\r
147         EECR |= (1<<EEMPE);\r
148         EECR |= (1<<EEPE);\r
149         while(EECR & (1<<EEPE));\r
150         EECR = (0<<EEPM1)|(0<<EEPM0);\r
151         EEARL = addr+1;\r
152         EEDR = val>>8;\r
153         EECR |= (1<<EEMPE);\r
154         EECR |= (1<<EEPE);\r
155 }\r
156 \r
157 void save_baseline(uint16_t b1,uint16_t b2 ) {\r
158         writeEEPROM(0,b1);\r
159         writeEEPROM(2,b2);\r
160 }\r
161 \r
162 void read_baseline(uint16_t *b1,uint16_t *b2) {\r
163         *b1=readEEPROM(0,0xFFFF);\r
164         *b2=readEEPROM(2,0xFFFF);\r
165         *b1=0xFFFF;\r
166         *b2=0xFFFF;\r
167 }\r
168 \r
169 void set_baseline(uint16_t b1,uint16_t b2) {\r
170         uint8_t b[10];\r
171         b[3]=(uint16_t)b1>>8;  //irgendwie werden die Werte vertauscht zwischen lesen und schreiben\r
172         b[4]=(uint16_t)b1&0x00FF;\r
173         b[5]=sensirion_common_generate_crc(b+3,2);\r
174         b[0]=(uint16_t)b2>>8;\r
175         b[1]=(uint16_t)b2&0x00FF;\r
176         b[2]=sensirion_common_generate_crc(b,2);\r
177         writeSGPXX(0x201e,b,6);\r
178         bl1S=b1;\r
179         bl2S=b2;\r
180 }\r
181 \r
182 int8_t initSGP30() {\r
183         uint8_t b[10];\r
184         readSGPXX(0x2032,b,3);\r
185         _delay_ms(300);\r
186         writeSGPXX(0x2003,0,0);\r
187         _delay_ms(10);\r
188         uint16_t b1=0,b2=0;\r
189         //set_baseline(0x2210,0x3320);\r
190         //readSGPXX(0x2015,b,6);\r
191         read_baseline(&b1,&b2);\r
192         if (b1!=0xFFFF) {\r
193                 set_baseline(b1,b2);\r
194                 //readSGPXX(0x2015,b,6);\r
195                 //uint16_t bl1,bl2;\r
196                 //check_convert_buf(b,&bl1,&bl2);\r
197                 \r
198         }\r
199         return 0x1;\r
200 \r
201 }\r
202 \r
203 void runSGP30(uint16_t *CO2,uint16_t *VOC,uint16_t *ETH,uint16_t *H2){\r
204         uint8_t b[10];\r
205         readSGPXX(0x2008,b,6);\r
206         check_convert_buf(b,CO2,VOC);\r
207         getbl--;\r
208         if (getbl==0) {\r
209                 uint8_t eq=1;\r
210                 readSGPXX(0x2015,b,6);\r
211                 uint16_t bl1,bl2;\r
212                 check_convert_buf(b,&bl1,&bl2);\r
213                 int8_t bl1d=bl1-bl1S;\r
214                 int8_t bl2d=bl2-bl2S;\r
215                 if (bl1d<0) bl1d=-bl1d;\r
216                 if (bl2d<0) bl2d=-bl2d;\r
217                 if (bl1d>4) {bl1S=bl1;eq=0;}\r
218                 if (bl2d>4) {bl2S=bl2;eq=0;}\r
219                 getbl=200;  //Naechste bruefunf in 100 s\r
220                 if (eq==0) {\r
221                         *VOC+=1000;\r
222                         save_baseline(bl1,bl2);\r
223                 } \r
224         }\r
225         readSGPXX(0x2050,b,6);\r
226         check_convert_buf(b,ETH,H2);\r
227 \r
228 }\r
229 \r
230 \r