Skip to content

Commit 55c9853

Browse files
Wrote hdc2021debr driver (#372)
Co-authored-by: Daniel Nakhooda <dnakhooda@gmail.com> Co-authored-by: Daniel Nakhooda <38150780+dnakhooda@users.noreply.github.com>
1 parent e11e8ee commit 55c9853

2 files changed

Lines changed: 165 additions & 0 deletions

File tree

general/include/hdc2021debr.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 0x40
12+
13+
typedef enum {
14+
REG_TEMP_LOW = 0x00,
15+
REG_TEMP_HIGH = 0x01,
16+
REG_HUMID_LOW = 0x02,
17+
REG_HUMID_HIGH = 0x03,
18+
REG_STATUS = 0x04,
19+
REG_DEVICE_CONFIG = 0x0E,
20+
REG_MEASURE_CONFIG = 0x0F
21+
} HDC2021_REG_ADDR_t;
22+
23+
/** Function Pointers */
24+
typedef int (*Write_ptr_hdc)(uint8_t *data, uint8_t dev_address, uint8_t length);
25+
typedef int (*Read_ptr_hdc)(uint8_t *data, uint8_t reg, uint8_t dev_address,
26+
uint8_t length);
27+
28+
typedef struct {
29+
Write_ptr_hdc write_reg;
30+
Read_ptr_hdc read_reg;
31+
float temp;
32+
float humidity;
33+
bool is_heater_enabled;
34+
uint8_t dev_address;
35+
} hdc2021debr_t;
36+
37+
/**
38+
* @brief Initializes an HDC2021 Driver
39+
*
40+
* @param hdc2021 - hdc2021debr driver
41+
* @return int - Status code
42+
*/
43+
int hdc2021_init(hdc2021debr_t *hdc2021, Write_ptr_hdc write_reg, Read_ptr_hdc read_reg,
44+
uint8_t dev_address);
45+
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+
/**
56+
* @brief triggers oneshot measurement
57+
*
58+
* @param hdc2021 - hdc2021debr driver
59+
* @return int - Status code
60+
*/
61+
int hdc2021_trigger_oneshot(hdc2021debr_t *hdc2021);
62+
63+
/**
64+
* @brief Retrieves the temperature and humidity
65+
* @note Call hdc2021_trigger_oneshot() and wait a couple ms before calling this
66+
* @param hdc2021 - hdc2021debr driver
67+
* @return int - Status code
68+
*/
69+
int hdc2021_get_temp_humid(hdc2021debr_t *hdc2021);
70+
71+
#endif

general/src/hdc2021debr.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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_hdc write_reg, Read_ptr_hdc 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+
{
40+
device_config |= (1 << 3);
41+
} else
42+
{
43+
device_config &= ~(1 << 3);
44+
}
45+
46+
if (hdc2021_write_reg(hdc2021, REG_DEVICE_CONFIG, device_config))
47+
{
48+
return 1;
49+
}
50+
51+
hdc2021->is_heater_enabled = enable;
52+
53+
return 0;
54+
}
55+
56+
int hdc2021_trigger_oneshot(hdc2021debr_t *hdc2021)
57+
{
58+
uint8_t config;
59+
60+
if (hdc2021_read_reg(hdc2021, REG_MEASURE_CONFIG, &config, 1))
61+
{
62+
return 1;
63+
}
64+
65+
config |= 0x01;
66+
67+
if (hdc2021_write_reg(hdc2021, REG_MEASURE_CONFIG, config))
68+
{
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_read_reg(hdc2021, REG_TEMP_LOW, buffer, 4))
80+
{
81+
return 1;
82+
}
83+
84+
uint16_t temp_bytes = ((uint16_t)(buffer[1]) << 8) | buffer[0];
85+
uint16_t humid_bytes = ((uint16_t)(buffer[3]) << 8) | buffer[2];
86+
87+
float tempVal = (((float)temp_bytes/65536.0f) * 165.0f) - 40.0f;
88+
hdc2021->temp = tempVal;
89+
90+
float humidVal = ((float)humid_bytes/65536.0f) * 100.0f;
91+
hdc2021->humidity = humidVal;
92+
93+
return 0;
94+
}

0 commit comments

Comments
 (0)