Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated sht driver + header #172

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added general/.DS_Store
Binary file not shown.
Binary file added general/include/.DS_Store
Binary file not shown.
48 changes: 26 additions & 22 deletions general/include/sht30.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef sht30_h
#define sht30_h

#include "stm32xx_hal.h"
#include <stdbool.h>
#include <stdint.h>

Expand All @@ -10,21 +9,26 @@
* --Datasheet
*
*/
#define SHT30_I2C_ADDR 0x44 << 1u /* If ADDR (pin2) is connected to VDD, 0x45 \
#define SHT30_I2C_ADDR \
0x44 << 1u /* If ADDR (pin2) is connected to VDD, 0x45 \
*/

typedef enum {
SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH = 0x2c06,
SHT3X_COMMAND_CLEAR_STATUS = 0x3041,
SHT3X_COMMAND_SOFT_RESET = 0x30A2,
SHT3X_COMMAND_HEATER_ENABLE = 0x306d,
SHT3X_COMMAND_HEATER_DISABLE = 0x3066,
SHT3X_COMMAND_READ_STATUS = 0xf32d,
SHT3X_COMMAND_FETCH_DATA = 0xe000,
SHT3X_COMMAND_MEASURE_HIGHREP_10HZ = 0x2737,
SHT3X_COMMAND_MEASURE_LOWREP_10HZ = 0x272a
SHT3X_COMMAND_MEASURE_HIGHREP_STRETCH = 0x2c06,
SHT3X_COMMAND_CLEAR_STATUS = 0x3041,
SHT3X_COMMAND_SOFT_RESET = 0x30A2,
SHT3X_COMMAND_HEATER_ENABLE = 0x306d,
SHT3X_COMMAND_HEATER_DISABLE = 0x3066,
SHT3X_COMMAND_READ_STATUS = 0xf32d,
SHT3X_COMMAND_FETCH_DATA = 0xe000,
SHT3X_COMMAND_MEASURE_HIGHREP_10HZ = 0x2737,
SHT3X_COMMAND_MEASURE_LOWREP_10HZ = 0x272a
} sht3x_command_t;

/** Function Pointers */
typedef int (*Write_ptr)(uint8_t *data, uint8_t reg, uint8_t length);
typedef int (*Read_ptr)(uint8_t *data, uint8_t reg, uint8_t length);

/*
* Start measurement command with clock streching enabled and high
* repeatability. This is responsible for retrieving the temp and humidity in
Expand All @@ -37,36 +41,36 @@ typedef enum {
#define SHT30_START_CMD_NCS 0x2400

typedef struct {
I2C_HandleTypeDef *i2c_handle;
uint16_t status_reg;
uint16_t temp;
uint16_t humidity;
bool is_heater_enabled;
Write_ptr write_reg;
Read_ptr read_reg;
uint16_t temp;
uint16_t humidity;
bool is_heater_enabled;
} sht30_t;

/**
* @brief Initializes an SHT30 Driver
*
* @param sht30 - SHT30 driver
* @return HAL_StatusTypeDef
* @return int - Status code
*/
HAL_StatusTypeDef sht30_init(sht30_t *sht30);
int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg);

/**
* @brief Toggles the status of the internal heater
*
* @param sht30 - SHT30 driver
* @param enable - true to enable, false to disable
* @return HAL_StatusTypeDef
* @return int - Status code
*/
HAL_StatusTypeDef sht30_toggle_heater(sht30_t *sht30, bool enable);
int sht30_toggle_heater(sht30_t *sht30, bool enable);

/**
* @brief Retrieves the temperature and humidity
*
* @param sht30 - SHT30 driver
* @return HAL_StatusTypeDef
* @return int - Status code
*/
HAL_StatusTypeDef sht30_get_temp_humid(sht30_t *sht30);
int sht30_get_temp_humid(sht30_t *sht30);

#endif
Binary file added general/src/.DS_Store
Binary file not shown.
153 changes: 73 additions & 80 deletions general/src/sht30.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,112 +2,105 @@
#include <stdbool.h>
#include <stdio.h>

static HAL_StatusTypeDef sht30_write_reg(sht30_t *sht30, uint16_t command) {
uint8_t command_buffer[2] = {(command & 0xff00u) >> 8u, command & 0xffu};
static int sht30_write_reg(sht30_t *sht30, uint16_t command)
{
uint8_t command_buffer[2] = { (command & 0xff00u) >> 8u,
command & 0xffu };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this still uses u after all ints. please remove

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, the verison in main uses u? I didn't touch it since it wasn't in the scope of the ticket, but i'll remove it and recommit without the .ds files


if (HAL_I2C_Master_Transmit(sht30->i2c_handle, SHT30_I2C_ADDR, command_buffer,
sizeof(command_buffer), 30) != HAL_OK) {
return false;
}

return true;
return sht30->write_reg(command_buffer, 0, sizeof(command_buffer));
}

/**
* @brief Calculates the CRC by using the polynomial x^8 + x^5 + x^4 + 1
* @param data: the data to use to calculate the CRC
*/
static uint8_t calculate_crc(const uint8_t *data, size_t length) {
uint8_t crc = 0xff;
for (size_t i = 0; i < length; i++) {
crc ^= data[i];
for (size_t j = 0; j < 8; j++) {
if ((crc & 0x80u) != 0) {
crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u);
} else {
crc <<= 1u;
}
}
}
return crc;
static uint8_t calculate_crc(const uint8_t *data, size_t length)
{
uint8_t crc = 0xff;
for (size_t i = 0; i < length; i++) {
crc ^= data[i];
for (size_t j = 0; j < 8; j++) {
if ((crc & 0x80u) != 0) {
crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u);
} else {
crc <<= 1u;
}
}
}
return crc;
}

static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb) {
return (uint16_t)((uint16_t)msb << 8u) | lsb;
static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb)
{
return (uint16_t)((uint16_t)msb << 8u) | lsb;
}

HAL_StatusTypeDef sht30_init(sht30_t *sht30) {
HAL_StatusTypeDef status = HAL_OK;
int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg)
{
sht30->write_reg = write_reg;
sht30->read_reg = read_reg;

uint8_t status_reg_and_checksum[3];
if (HAL_I2C_Mem_Read(sht30->i2c_handle, SHT30_I2C_ADDR,
SHT3X_COMMAND_READ_STATUS, 2,
(uint8_t *)&status_reg_and_checksum,
sizeof(status_reg_and_checksum), 30) != HAL_OK) {
return false;
}
uint8_t status_reg_and_checksum[3];
if (sht30->read_reg(status_reg_and_checksum, SHT3X_COMMAND_READ_STATUS,
sizeof(status_reg_and_checksum)) != 0) {
return -1;
}

uint8_t calculated_crc = calculate_crc(status_reg_and_checksum, 2);
uint8_t calculated_crc = calculate_crc(status_reg_and_checksum, 2);

if (calculated_crc != status_reg_and_checksum[2]) {
return false;
}
if (calculated_crc != status_reg_and_checksum[2]) {
return -1;
}

return status;
return 0;
}

HAL_StatusTypeDef sht30_toggle_heater(sht30_t *sht30, bool enable) {
if (enable) {
return sht30_write_reg(sht30, SHT3X_COMMAND_HEATER_ENABLE);
} else {
return sht30_write_reg(sht30, SHT3X_COMMAND_HEATER_DISABLE);
}
int sht30_toggle_heater(sht30_t *sht30, bool enable)
{
if (enable) {
return sht30_write_reg(sht30, SHT3X_COMMAND_HEATER_ENABLE);
} else {
return sht30_write_reg(sht30, SHT3X_COMMAND_HEATER_DISABLE);
}
}

HAL_StatusTypeDef sht30_get_temp_humid(sht30_t *sht30) {
HAL_StatusTypeDef status;

union {
struct __attribute__((packed)) {
uint16_t
temp; // The packed attribute does not correctly arrange the bytes
uint8_t temp_crc;
uint16_t
humidity; // The packed attribute does not correctly arrange the bytes
uint8_t humidity_crc;
} raw_data;
uint8_t databuf[6];
} data;
int sht30_get_temp_humid(sht30_t *sht30)
{
union {
struct __attribute__((packed)) {
uint16_t temp; // The packed attribute does not correctly arrange the bytes
uint8_t temp_crc;
uint16_t humidity; // The packed attribute does not correctly arrange the bytes
uint8_t humidity_crc;
} raw_data;
uint8_t databuf[6];
} data;

uint16_t temp, humidity;
uint16_t temp, humidity;

sht30_write_reg(sht30, (SHT30_START_CMD_WCS));
sht30_write_reg(sht30, (SHT30_START_CMD_WCS));

HAL_Delay(1);
if (sht30->read_reg(data.databuf, 0, sizeof(data.databuf)) != 0) {
return -1;
}

status = HAL_I2C_Master_Receive(sht30->i2c_handle, SHT30_I2C_ADDR,
data.databuf, sizeof(data.databuf), 30);
if (status != HAL_OK) {
return false;
}
temp = uint8_to_uint16(data.databuf[0], data.databuf[1]);

temp = uint8_to_uint16(data.databuf[0], data.databuf[1]);
if (data.raw_data.temp_crc != calculate_crc(data.databuf, 2)) {
return -1;
}

if (data.raw_data.temp_crc != calculate_crc(data.databuf, 2)) {
return HAL_ERROR;
}
float val = -45.0f + 175.0f * (float)temp / 65535.0f;

float val = -45.0f + 175.0f * (float)temp / 65535.0f;
sht30->temp = (uint16_t)val;

sht30->temp = (uint16_t)val;
humidity = uint8_to_uint16(data.databuf[3], data.databuf[4]);
if (data.raw_data.humidity_crc != calculate_crc(data.databuf + 3, 2)) {
return -1;
}

humidity = uint8_to_uint16(data.databuf[3], data.databuf[4]);
if (data.raw_data.humidity_crc != calculate_crc(data.databuf + 3, 2)) {
return HAL_ERROR;
}
humidity = (uint16_t)(100.0f * (float)humidity / 65535.0f);
sht30->humidity = humidity;

humidity = (uint16_t)(100.0f * (float)humidity / 65535.0f);
sht30->humidity = humidity;

return HAL_OK;
}
return 0;
}
Loading