Many changes from 2018
[owSlave2.git] / common / I2C / HDC2010.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 \r
33 //---------- Includes ----------------------------------------------------------\r
34 #define F_CPU 8000000UL\r
35 #include <avr/io.h>\r
36 #include <avr/interrupt.h>\r
37 #include <util/delay.h>\r
38 #include <avr/wdt.h>\r
39 #include <avr/sleep.h>\r
40 #include "TWI_Master.h"\r
41 #include "HDC2010.h"\r
42 typedef enum{\r
43         I2C_ADR_W = 130, // sensor I2C address + write bit //ADR=VDD\r
44         I2C_ADR_R = 131 // sensor I2C address + read bit\r
45 }etI2cHeader;\r
46 \r
47 uint8_t HDC2010_Init() {\r
48         volatile uint8_t error=0;\r
49         /*I2c_StartCondition();\r
50         error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr\r
51         error |= I2c_WriteByte (0x02); // Config\r
52         error |= I2c_WriteByte (0x0); // 14 bit\r
53         error |= I2c_WriteByte (0x0); // \r
54         I2c_StopCondition();*/\r
55         return error;\r
56 \r
57 }\r
58 \r
59 uint8_t HDC2010_Readf(double * temperature, double * hum) {     \r
60         int16_t t;\r
61         uint16_t h;\r
62         uint8_t error=0;\r
63         error=HDC2010_Readi(&t,&h);\r
64         *temperature=(double)t/65536.0*165.0-40.0;\r
65         *hum=(double)h/65536.0*100;\r
66         return error;\r
67 \r
68 }\r
69 uint8_t HDC2010_Readi(int16_t * temperature, uint16_t * hum) {\r
70         volatile uint8_t error=0;\r
71         I2c_StartCondition();\r
72         error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr\r
73         error |= I2c_WriteByte (0x0F); //\r
74         error |= I2c_WriteByte (0x01); //\r
75         I2c_StopCondition();\r
76         _delay_ms(10);\r
77         I2c_StartCondition();\r
78         error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr\r
79         error |= I2c_WriteByte (0x0); //\r
80         I2c_StartCondition();\r
81         error |= I2c_WriteByte (I2C_ADR_R); //I2C address\r
82         *temperature=0;\r
83         *temperature |= I2c_ReadByte(ACK);\r
84         *temperature |= I2c_ReadByte(ACK)<<8; \r
85         *hum=0;\r
86         *hum|= I2c_ReadByte(ACK);\r
87         *hum|= I2c_ReadByte(NO_ACK)<<8;\r
88         I2c_StopCondition();\r
89 \r
90         return error;\r
91         }\r
92 \r
93 /*\r
94 \r
95 const uint16_t POLYNOMIAL = 0x131; //P(x)=x^8+x^5+x^4+1 = 100110001\r
96 \r
97 \r
98 //==============================================================================\r
99 uint8_t SHT2x_CheckCrc(uint8_t data[], uint8_t nbrOfBytes, uint8_t checksum)\r
100 //==============================================================================\r
101 {\r
102         uint8_t crc = 0;\r
103         uint8_t byteCtr;\r
104         //calculates 8-Bit checksum with given polynomial\r
105         for (byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)\r
106         { crc ^= (data[byteCtr]);\r
107                 for (uint8_t bit = 8; bit > 0; --bit)\r
108                 { if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;\r
109                         else crc = (crc << 1);\r
110                 }\r
111         }\r
112         if (crc != checksum) return CHECKSUM_ERROR;\r
113         else return 0;\r
114 }\r
115 //===========================================================================\r
116 uint8_t SHT2x_ReadUserRegister(uint8_t *pRegisterValue)\r
117 //===========================================================================\r
118 {\r
119         uint8_t checksum; //variable for checksum byte\r
120         uint8_t error=0; //variable for error code\r
121         I2c_StartCondition();\r
122         error |= I2c_WriteByte (I2C_ADR_W);\r
123         error |= I2c_WriteByte (USER_REG_R);\r
124         I2c_StartCondition();\r
125         error |= I2c_WriteByte (I2C_ADR_R);\r
126         *pRegisterValue = I2c_ReadByte(ACK);\r
127         checksum=I2c_ReadByte(NO_ACK);\r
128         error |= SHT2x_CheckCrc (pRegisterValue,1,checksum);\r
129         I2c_StopCondition();\r
130         return error;\r
131 }\r
132 //===========================================================================\r
133 uint8_t SHT2x_WriteUserRegister(uint8_t *pRegisterValue)\r
134 //===========================================================================\r
135 {\r
136         uint8_t error=0; //variable for error code\r
137         I2c_StartCondition();\r
138         error |= I2c_WriteByte (I2C_ADR_W);\r
139         error |= I2c_WriteByte (USER_REG_W);\r
140         error |= I2c_WriteByte (*pRegisterValue);\r
141         I2c_StopCondition();\r
142         return error;\r
143 }\r
144 //===========================================================================\r
145 uint8_t SHT2x_MeasureHM(etSHT2xMeasureType eSHT2xMeasureType, int16_t *pMeasurand)\r
146 //===========================================================================\r
147 {\r
148         uint8_t checksum; //checksum\r
149         uint8_t data[2]; //data array for checksum verification\r
150         uint8_t error=0; //error variable\r
151         uint16_t i; //counting variable\r
152         //-- write I2C sensor address and command --\r
153         I2c_StartCondition();\r
154         error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr\r
155         switch(eSHT2xMeasureType)\r
156         { case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_HM); break;\r
157                 case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_HM); break;\r
158                 //default: assert(0);\r
159         }\r
160         //-- wait until hold master is released --\r
161         I2c_StartCondition();\r
162         error |= I2c_WriteByte (I2C_ADR_R);\r
163         //SCL=HIGH; // set SCL I/O port as input\r
164         DDR_USI&=~(1<<PIN_USI_SCL);\r
165         for(i=0; i<1000; i++) // wait until master hold is released or ;;;;; Son quatsch.... 1000 s *kopfschuettel*\r
166         { _delay_ms(1); // a timeout (~1s) is reached\r
167                 //if (SCL_CONF==1) break;\r
168                 if (PIN_USI&(1<<PIN_USI_SCL)) break;\r
169         }\r
170         //-- check for timeout --\r
171         //Was wenn der SHT2x die leitung auf 0 laesst? Kurzschluss???\r
172         if((PIN_USI&(1<<PIN_USI_SCL))==0) error |= TIME_OUT_ERROR; else DDR_USI|=(1<<PIN_USI_SCL);\r
173         \r
174         //-- read two data bytes and one checksum byte --\r
175         *pMeasurand=((data[0] = I2c_ReadByte(ACK))>>8) & 0xFF;\r
176         *pMeasurand|=0xFF & (data[1] = I2c_ReadByte(ACK));\r
177 //      pMeasurand->s16.u8H = data[0] = I2c_ReadByte(ACK);\r
178 //      pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK);\r
179 \r
180         checksum=I2c_ReadByte(NO_ACK);\r
181         //-- verify checksum --\r
182         error |= SHT2x_CheckCrc (data,2,checksum);\r
183         I2c_StopCondition();\r
184         return error;\r
185 }\r
186 //===========================================================================\r
187 uint8_t SHT2x_MeasurePoll(etSHT2xMeasureType eSHT2xMeasureType, int16_t *pMeasurand)\r
188 //===========================================================================\r
189 {\r
190         uint8_t checksum; //checksum\r
191         uint8_t data[2]; //data array for checksum verification\r
192         uint8_t error=0; //error variable\r
193         uint16_t i=0; //counting variable\r
194         //-- write I2C sensor address and command --\r
195         I2c_StartCondition();\r
196         error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr\r
197         switch(eSHT2xMeasureType)\r
198         { case HUMIDITY: error |= I2c_WriteByte (TRIG_RH_MEASUREMENT_POLL); break;\r
199                 case TEMP : error |= I2c_WriteByte (TRIG_T_MEASUREMENT_POLL); break;\r
200                 //default: assert(0);\r
201         }\r
202         //-- poll every 10ms for measurement ready. Timeout after 20 retries (200ms)--\r
203         do\r
204         { I2c_StartCondition();\r
205                 _delay_ms(200); //delay 10ms\r
206                 if(i++ >= 20) break;\r
207         } while(I2c_WriteByte (I2C_ADR_R) == ACK_ERROR);\r
208         if (i>=20) error |= TIME_OUT_ERROR;\r
209         //-- read two data bytes and one checksum byte --\r
210         data[0]=I2c_ReadByte(ACK);\r
211         data[1]=I2c_ReadByte(ACK);\r
212         *pMeasurand=(data[0]<<8)|data[1];\r
213         \r
214 //      pMeasurand->s16.u8H = data[0] = I2c_ReadByte(ACK);\r
215 //      pMeasurand->s16.u8L = data[1] = I2c_ReadByte(ACK);\r
216         checksum=I2c_ReadByte(NO_ACK);\r
217         //-- verify checksum --\r
218         error |= SHT2x_CheckCrc (data,2,checksum);\r
219         I2c_StopCondition();\r
220         return error;\r
221 }\r
222 //===========================================================================\r
223 uint8_t SHT2x_SoftReset(void)\r
224 //===========================================================================\r
225 {\r
226         uint8_t error=0; //error variable\r
227         I2c_StartCondition();\r
228         error |= I2c_WriteByte (I2C_ADR_W); // I2C Adr\r
229         error |= I2c_WriteByte (SOFT_RESET); // Command\r
230         I2c_StopCondition();\r
231         _delay_ms(15); // wait till sensor has restarted\r
232         return error;\r
233 }\r
234 //==============================================================================\r
235 float SHT2x_CalcRH(uint16_t u16sRH)\r
236 //==============================================================================\r
237 {\r
238         double humidityRH; // variable for result\r
239         u16sRH &= ~0x0003; // clear bits [1..0] (status bits)\r
240         //-- calculate relative humidity [%RH] --\r
241         humidityRH = -6.0 + 125.0/65536 * (double)u16sRH; // RH= -6 + 125 * SRH/2^16\r
242         return humidityRH;\r
243 }\r
244 //==============================================================================\r
245 float SHT2x_CalcTemperatureC(uint16_t u16sT)\r
246 //==============================================================================\r
247 {\r
248         double temperatureC; // variable for result\r
249         u16sT &= ~0x0003; // clear bits [1..0] (status bits)\r
250         //-- calculate temperature [°C] --\r
251         temperatureC= -46.85 + 175.72/65536 *(double)u16sT; //T= -46.85 + 175.72 * ST/2^16\r
252         return temperatureC;\r
253 }\r
254 //==============================================================================\r
255 uint8_t SHT2x_GetSerialNumber(uint8_t u8SerialNumber[])\r
256 //==============================================================================\r
257 {\r
258         uint8_t error=0; //error variable\r
259         //Read from memory location 1\r
260         I2c_StartCondition();\r
261         error |= I2c_WriteByte (I2C_ADR_W); //I2C address\r
262         error |= I2c_WriteByte (0xFA); //Command for readout on-chip memory\r
263         error |= I2c_WriteByte (0x0F); //on-chip memory address\r
264         I2c_StartCondition();\r
265         error |= I2c_WriteByte (I2C_ADR_R); //I2C address\r
266         u8SerialNumber[5] = I2c_ReadByte(ACK); //Read SNB_3\r
267         I2c_ReadByte(ACK); //Read CRC SNB_3 (CRC is not analyzed)\r
268         u8SerialNumber[4] = I2c_ReadByte(ACK); //Read SNB_2\r
269         I2c_ReadByte(ACK); //Read CRC SNB_2 (CRC is not analyzed)\r
270         u8SerialNumber[3] = I2c_ReadByte(ACK); //Read SNB_1\r
271         I2c_ReadByte(ACK); //Read CRC SNB_1 (CRC is not analyzed)\r
272         u8SerialNumber[2] = I2c_ReadByte(ACK); //Read SNB_0\r
273         I2c_ReadByte(NO_ACK); //Read CRC SNB_0 (CRC is not analyzed)\r
274         I2c_StopCondition();\r
275         //Read from memory location 2\r
276         I2c_StartCondition();\r
277         error |= I2c_WriteByte (I2C_ADR_W); //I2C address\r
278         error |= I2c_WriteByte (0xFC); //Command for readout on-chip memory\r
279         error |= I2c_WriteByte (0xC9); //on-chip memory address\r
280         I2c_StartCondition();\r
281         error |= I2c_WriteByte (I2C_ADR_R); //I2C address\r
282         u8SerialNumber[1] = I2c_ReadByte(ACK); //Read SNC_1\r
283         u8SerialNumber[0] = I2c_ReadByte(ACK); //Read SNC_0\r
284         I2c_ReadByte(ACK); //Read CRC SNC0/1 (CRC is not analyzed)\r
285         u8SerialNumber[7] = I2c_ReadByte(ACK); //Read SNA_1\r
286         u8SerialNumber[6] = I2c_ReadByte(ACK); //Read SNA_0\r
287         I2c_ReadByte(NO_ACK); //Read CRC SNA0/1 (CRC is not analyzed)\r
288         I2c_StopCondition();\r
289         return error;\r
290 }\r
291 */