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