Skip to content

Commit 8223aa0

Browse files
committed
Wrote hdc2021debr driver
1 parent 92c9fae commit 8223aa0

2 files changed

Lines changed: 160 additions & 0 deletions

File tree

general/include/hdc2021debr.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef hdc2021debr_h
2+
#define hdc2021debr_h
3+
#include <stdbool.h>
4+
#include <stdint.h>
5+
/**
6+
* https://www.ti.com/lit/ds/symlink/hdc2021.pdf
7+
* --Datasheet
8+
*
9+
*/
10+
11+
#define HDC2021_I2C_ADDR \
12+
0x40 /* If ADDR is connected to VDD, 0x41 This is shifted left as it is required to be read as a dev address.*/
13+
14+
typedef enum {
15+
REG_TEMP_LOW = 0x00,
16+
REG_TEMP_HIGH = 0x01,
17+
REG_HUMID_LOW = 0x02,
18+
REG_HUMID_HIGH = 0x03,
19+
REG_STATUS = 0x04,
20+
REG_DEVICE_CONFIG = 0x0E,
21+
REG_MEASURE_CONFIG = 0x0F
22+
} HDC2021_REG_ADDR_t;
23+
24+
/** Function Pointers */
25+
typedef int (*Write_ptr)(uint8_t *data, uint8_t dev_address, uint8_t length);
26+
typedef int (*Read_ptr)(uint8_t *data, uint8_t reg, uint8_t dev_address,
27+
uint8_t length);
28+
29+
typedef struct {
30+
Write_ptr write_reg;
31+
Read_ptr read_reg;
32+
float temp;
33+
float humidity;
34+
bool is_heater_enabled;
35+
uint8_t dev_address;
36+
} hdc2021debr_t;
37+
38+
/**
39+
* @brief Initializes an HDC2021 Driver
40+
*
41+
* @param hdc2021 - hdc2021debr driver
42+
* @return int - Status code
43+
*/
44+
int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr write_reg, Read_ptr read_reg,
45+
uint8_t dev_address);
46+
/**
47+
* @brief Toggles the status of the internal heater
48+
*
49+
* @param hdc2021 - hdc2021debr driver
50+
* @param enable - true to enable, false to disable
51+
* @return int - Status code
52+
*/
53+
int hdc2021_toggle_heater(hdc2021debr_t *hdc2021, bool enable);
54+
/**
55+
* @brief Retrieves the temperature and humidity
56+
* @note Call hdc2021_trigger_oneshot() and wait a couple ms before calling this
57+
* @param hdc2021 - hdc2021debr driver
58+
* @return int - Status code
59+
*/
60+
int hdc2021_get_temp_humid(hdc2021debr_t *hdc2021);
61+
#endif

general/src/hdc2021debr.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include "hdc2021debr.h"
2+
#include <stdbool.h>
3+
#include <stdio.h>
4+
5+
static int hdc2021_write_reg(hdc2021debr_t *hdc2021, uint8_t reg, uint8_t data)
6+
{
7+
uint8_t buffer[2] = {reg, data};
8+
return hdc2021->write_reg(buffer, hdc2021->dev_address, sizeof(buffer));
9+
}
10+
11+
static int hdc2021_read_reg(hdc2021debr_t *hdc2021, uint8_t reg, uint8_t *data,
12+
uint8_t length)
13+
{
14+
return hdc2021->read_reg(data, reg, hdc2021->dev_address, length);
15+
}
16+
17+
int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr write_reg, Read_ptr read_reg,
18+
uint8_t dev_address)
19+
{
20+
hdc2021->write_reg = write_reg;
21+
hdc2021->read_reg = read_reg;
22+
hdc2021->dev_address = dev_address << 1u;
23+
24+
return 0;
25+
}
26+
27+
int hdc2021_toggle_heater(hdc2021debr_t *hdc2021, bool enable)
28+
{
29+
uint8_t device_config;
30+
31+
if (hdc2021_read_reg(hdc2021, REG_DEVICE_CONFIG, &device_config,
32+
sizeof(device_config)))
33+
{
34+
return 1;
35+
};
36+
37+
// the 3rd bit toggles the heater
38+
if (enable) {
39+
device_config |= (1 << 3);
40+
} else {
41+
device_config &= ~(1 << 3);
42+
};
43+
44+
if (hdc2021_write_reg(hdc2021, REG_DEVICE_CONFIG, device_config))
45+
{
46+
return 1;
47+
};
48+
49+
return 0;
50+
51+
}
52+
/**
53+
* @brief triggers oneshot measurement
54+
*
55+
* @param hdc2021 - hdc2021debr driver
56+
* @return int - Status code
57+
*/
58+
int hdc2021_trigger_oneshot(hdc2021debr_t *hdc2021)
59+
{
60+
uint8_t config;
61+
62+
if (hdc2021_read_reg(hdc2021, REG_MEASURE_CONFIG, &config, 1)) {
63+
return 1;
64+
}
65+
66+
config |= 0x01;
67+
68+
if (hdc2021_write_reg(hdc2021, REG_MEASURE_CONFIG, config)) {
69+
return 1;
70+
}
71+
72+
return 0;
73+
}
74+
75+
int hdc2021_get_temp_humid(hdc2021debr_t *hdc2021)
76+
{
77+
uint8_t buffer[4];
78+
79+
if (hdc2021_trigger_oneshot(hdc2021)) {
80+
return 1;
81+
}
82+
83+
84+
if (hdc2021_read_reg(hdc2021, REG_TEMP_LOW, buffer, 4))
85+
{
86+
return 1;
87+
};
88+
89+
uint16_t temp_bytes = (buffer[1] << 8) | buffer[0];
90+
uint16_t humid_bytes = (buffer[3] << 8) | buffer[2];
91+
92+
float tempVal = (((float)temp_bytes/65536.0f) * 165.0f) - 40.0f;
93+
hdc2021->temp = tempVal;
94+
95+
float humidVal = ((float)humid_bytes/65536.0f) * 100.0f;
96+
hdc2021->humidity = humidVal;
97+
98+
return 0;
99+
}

0 commit comments

Comments
 (0)