Skip to content

Commit 7ccfe8d

Browse files
committed
Added Support Temperature Sensor HDC2021
1 parent d0cb1e4 commit 7ccfe8d

5 files changed

Lines changed: 75 additions & 4 deletions

File tree

Core/Inc/u_can.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
#include "u_tx_can.h"
44

5-
#define SHT30_CAN_ID 0xBAA
5+
#define SHT30_CAN_ID 0xBAA
66
#define IMU_ACCEL_CAN_ID 0xBAB
77
#define IMU_GYRO_CAN_ID 0xBAC
88
#define MAGNOMETER_CAN_ID 0XBAD
99
#define ORIENTATION_CAN_ID 0XBAE
10+
#define HDC2021_CAN_ID 0xBAF
1011

1112
// u_TODO - finish this file

Core/Inc/u_sensors.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,21 @@ uint16_t read_sht30();
6868
/**
6969
* @brief sends temp and humidity data from the sht30 over CAN
7070
*/
71-
void send_sht30_data();
71+
void send_sht30_data();
72+
73+
/**
74+
* @brief initializes the hdc2021 for reading temp and humidity
75+
* @return whether there were errors initializing the sht30
76+
*/
77+
uint16_t init_hdc2021();
78+
79+
/**
80+
* @brief reads temp and humidity data from the hdc2021
81+
* @return whether there were errors in reading the data
82+
*/
83+
uint16_t read_hdc2021();
84+
85+
/**
86+
* @brief sends temp and humidity data from the hdc2021 over CAN
87+
*/
88+
void send_hdc2021_data();

Core/Src/u_sensors.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
#include "lsm6dsv_reg.h"
1616
#include "lis2mdl_reg.h"
1717

18+
#include "hdc2021debr.h"
19+
1820
extern I2C_HandleTypeDef hi2c1;
1921
extern SPI_HandleTypeDef hspi1;
2022
extern SPI_HandleTypeDef hspi2;
2123

2224
static sht30_t sht30;
25+
static hdc2021debr_t hdc2021;
26+
2327
static stmdev_ctx_t imu;
2428
static stmdev_ctx_t lis2mdl_ctx;
2529

@@ -63,6 +67,10 @@ static struct __attribute__((__packed__)) {
6367
float humidity;
6468
} sht30_data;
6569

70+
static struct __attribute__((__packed__)) {
71+
float temp;
72+
float humidity;
73+
} hdc2021_data;
6674

6775
/**
6876
* IMU
@@ -541,4 +549,47 @@ void send_sht30_data() {
541549
memcpy(can_message.data, &sht30_data, can_message.len);
542550

543551
queue_send(&can_outgoing, &can_message, TX_NO_WAIT);
544-
}
552+
}
553+
554+
/**
555+
* HDC2021
556+
*/
557+
558+
uint8_t _hdc2021_i2c_write(uint8_t *data, uint8_t dev_address, uint8_t length){
559+
return HAL_I2C_Master_Transmit(&hi2c1, dev_address, data, length, HAL_MAX_DELAY);
560+
}
561+
562+
uint8_t _hdc2021_i2c_read(uint8_t *data, uint16_t command, uint8_t dev_address, uint8_t length) {
563+
return HAL_I2C_Mem_Read(&hi2c1, dev_address, command, sizeof(command), data, length, HAL_MAX_DELAY);
564+
}
565+
566+
uint16_t init_hdc2021() {
567+
return hdc2021_init(&hdc2021, (Write_ptr) _hdc2021_i2c_write, (Read_ptr) _hdc2021_i2c_read, HDC2021_I2C_ADDR);
568+
}
569+
570+
uint16_t read_hdc2021() {
571+
int status = hdc2021_get_temp_humid(&hdc2021);
572+
573+
if (status) {
574+
PRINTLN_ERROR("ERROR: Failed to get hdc2021 data (Status: %d/%s).", status, hal_status_toString(status));
575+
return U_ERROR;
576+
}
577+
578+
hdc2021_data.temp = hdc2021.temp;
579+
hdc2021_data.humidity = hdc2021.humidity;
580+
581+
return U_SUCCESS;
582+
}
583+
584+
void send_hdc2021_data() {
585+
can_msg_t can_message = {
586+
.id = HDC2021_CAN_ID,
587+
.len = 8,
588+
.data = { 0 }
589+
};
590+
591+
memcpy(can_message.data, &hdc2021_data, can_message.len);
592+
593+
queue_send(&can_outgoing, &can_message, TX_NO_WAIT);
594+
}
595+

Core/Src/u_threads.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ void sensors_thread(ULONG thread_input) {
4848
if (is_timer_expired(&data_send_timer)) {
4949
CATCH_ERROR(read_sht30(), U_SUCCESS);
5050
send_sht30_data();
51+
CATCH_ERROR(read_hdc2021(), U_SUCCESS);
52+
send_hdc2021_data();
5153
send_imu_and_magnometer_data();
5254
start_timer(&data_send_timer, DATA_SEND_INTERVAL);
5355
}

0 commit comments

Comments
 (0)