• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Unionman Technology Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef _I2CINTERFACE_H_
17 #define _I2CINTERFACE_H_
18 
19 #ifdef _cplusplus
20 extern "C" {
21 #endif
22 
23 #undef LOG_DOMAIN
24 #undef LOG_TAG
25 #define LOG_DOMAIN 0 // 标识业务领域,范围0x0~0xFFFFF
26 #define LOG_TAG "i2c_test"
27 
28 #define SHT3X_CMD_READ_SERIAL_NUMBER (0x3780)    // Read the chip serial number
29 #define SHT3X_CMD_GETDATA_H_CLOCKENBLED (0x2C06) // Measurement:high repeatability
30 #define SHT3X_CMD_GETDATA_M_CLOCKENBLED (0x2C0D) // Measurement: medium repeatability
31 #define SHT3X_CMD_GETDATA_L_CLOCKENBLED (0x2C10) // Measurement: low repeatability
32 
33 #define SHT3X_CMD_SETMODE_H_FREQUENCY_HALF_HZ (0x2032) // Measurement: periodic 0.5 mps, high repeatability
34 #define SHT3X_CMD_SETMODE_M_FREQUENCY_HALF_HZ (0x2024) // Measurement: periodic 0.5 mps, medium
35 #define SHT3X_CMD_SETMODE_L_FREQUENCY_HALF_HZ (0x202F) // Measurement: periodic 0.5 mps, low repeatability
36 #define SHT3X_CMD_SETMODE_H_FREQUENCY_1_HZ (0x2130)    // Measurement: periodic 1 mps, high repeatability
37 #define SHT3X_CMD_SETMODE_M_FREQUENCY_1_HZ (0x2126)    // Measurement: periodic 1 mps, medium repeatability
38 #define SHT3X_CMD_SETMODE_L_FREQUENCY_1_HZ (0x212D)    // Measurement: periodic 1 mps, low repeatability
39 #define SHT3X_CMD_SETMODE_H_FREQUENCY_2_HZ (0x2236)    // Measurement: periodic 2 mps, high repeatability
40 #define SHT3X_CMD_SETMODE_M_FREQUENCY_2_HZ (0x2220)    // Measurement: periodic 2 mps, medium repeatability
41 #define SHT3X_CMD_SETMODE_L_FREQUENCY_2_HZ (0x222B)    // Measurement: periodic 2 mps, low repeatability
42 #define SHT3X_CMD_SETMODE_H_FREQUENCY_4_HZ (0x2334)    // Measurement: periodic 4 mps, high repeatability
43 #define SHT3X_CMD_SETMODE_M_FREQUENCY_4_HZ (0x2322)    // Measurement: periodic 4 mps, medium repeatability
44 #define SHT3X_CMD_SETMODE_L_FREQUENCY_4_HZ (0x2329)    // Measurement: periodic 4 mps, low repeatability
45 #define SHT3X_CMD_SETMODE_H_FREQUENCY_10_HZ (0x2737)   // Measurement: periodic 10 mps, high repeatability
46 #define SHT3X_CMD_SETMODE_M_FREQUENCY_10_HZ (0x2721)   // Measurement: periodic 10 mps, medium
47 #define SHT3X_CMD_SETMODE_L_FREQUENCY_10_HZ (0x272A)   // Measurement: periodic 10 mps, low repeatability
48 #define SHT3X_CMD_GETDATA (0xE000)                     // Readout measurements for periodic mode
49 
50 #define SHT3X_CMD_STOP_PERIODIC_ACQUISITION_MODE (0x3093)
51 #define SHT3X_CMD_SOFT_RESET (0x30A2)       // Soft reset
52 #define SHT3X_CMD_HEATER_ENABLE (0x306D)    // Enabled heater
53 #define SHT3X_CMD_HEATER_DISABLE (0x3066)   // Disable heater
54 #define SHT3X_CMD_READ_STATUS_REG (0xF32D)  // Read status register
55 #define SHT3X_CMD_CLEAR_STATUS_REG (0x3041) // Clear status register
56 
57 #define SHT3X_CMD_READ_HIGH_ALERT_LIMIT_SET (0xE11F)    // Read alert limits, high set
58 #define SHT3X_CMD_READ_HIGH_ALERT_LIMIT_CLEAR (0xE114)  // Read alert limits, high clear
59 #define SHT3X_CMD_READ_LOW_ALERT_LIMIT_CLEAR (0xE109)   // Read alert limits, low clear
60 #define SHT3X_CMD_READ_LOW_ALERT_LIMIT_SET (0xE102)     // Read alert limits, low set
61 #define SHT3X_CMD_WRITE_HIGH_ALERT_LIMIT_SET (0x611D)   // Write alert limits, high set
62 #define SHT3X_CMD_WRITE_HIGH_ALERT_LIMIT_CLEAR (0x6116) // Write alert limits, high clear
63 #define SHT3X_CMD_WRITE_LOW_ALERT_LIMIT_CLEAR (0x610B)  // Write alert limits, low clear
64 #define SHT3X_CMD_WRITE_LOW_ALERT_LIMIT_SET (0x6100)    // Write alert limits, low set
65 
66 typedef struct {
67     uint8_t writeDataChecksumStatus : 1;
68     uint8_t commandStatus : 1;
69     uint8_t reserved0 : 2;
70     uint8_t systemResetDeteced : 1;
71     uint8_t reserved1 : 5;
72     uint8_t temperatureAlert : 1;
73     uint8_t humidityAlert : 1;
74     uint8_t reserved2 : 1;
75     uint8_t heaterStaus : 1;
76     uint8_t reserved3 : 1;
77     uint8_t alertPendingStatus : 1;
78 } __attribute__((packed)) sStatusReg_t;
79 
80 /**
81 Structures used to store temperature and relative humidity
82 */
83 typedef struct {
84     float TemperatureC;
85     float Humidity;
86     float TemperatureF;
87 } sRHAndTemp_t;
88 
89 void ConvertTempAndHum(sRHAndTemp_t *tempRH, uint8_t *rawTemp, uint8_t *rawHum);
90 float ConvertTemperature(uint8_t *rawTemp);
91 float ConvertHumidity(uint8_t *rawHum);
92 uint8_t CheckCrc(uint8_t *data);
93 int SoftReset(char *devName, char addr);
94 int ReadStatusReg(char *devName, char addr, sStatusReg_t *regRaw);
95 int SendCommand(char *devName, char addr, uint16_t modeCommand);
96 int ModeSet(char *devName, char addr, uint8_t mps, uint8_t repeatability);
97 int ReadTempAndHum(char *devName, char addr, sRHAndTemp_t *tempRH);
98 
99 int MpsHalfHz(char *devName, char addr, uint8_t repeatability);
100 int Mps1Hz(char *devName, char addr, uint8_t repeatability);
101 int Mps2Hz(char *devName, char addr, uint8_t repeatability);
102 int Mps4Hz(char *devName, char addr, uint8_t repeatability);
103 int Mps10Hz(char *devName, char addr, uint8_t repeatability);
104 
105 #ifdef _cplusplus
106 }
107 #endif
108 
109 #endif
110