From 0e56e855824dd845253486fce55b4feeec663e28 Mon Sep 17 00:00:00 2001 From: marvin-g-neu <141779828+marvin-g-neu@users.noreply.github.com> Date: Thu, 10 Oct 2024 19:08:25 -0400 Subject: [PATCH 1/6] updated sht driver + header --- general/include/sht30.h | 57 +++++++++++---------- general/src/sht30.c | 107 +++++++++++++++++----------------------- 2 files changed, 76 insertions(+), 88 deletions(-) diff --git a/general/include/sht30.h b/general/include/sht30.h index 89ca1928..24a4972d 100644 --- a/general/include/sht30.h +++ b/general/include/sht30.h @@ -1,28 +1,27 @@ -#ifndef sht30_h -#define sht30_h +#ifndef SHT30_H +#define SHT30_H -#include "stm32xx_hal.h" #include #include +#include /** * https://www.mouser.com/datasheet/2/682/Sensirion_Humidity_Sensors_SHT3x_Datasheet_digital-971521.pdf * --Datasheet * - */ -#define SHT30_I2C_ADDR 0x44 << 1u /* If ADDR (pin2) is connected to VDD, 0x45 \ - */ + */ +#define SHT30_I2C_ADDR (0x44u << 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 = 0x2c06u, + SHT3X_COMMAND_CLEAR_STATUS = 0x3041u, + SHT3X_COMMAND_SOFT_RESET = 0x30A2u, + SHT3X_COMMAND_HEATER_ENABLE = 0x306du, + SHT3X_COMMAND_HEATER_DISABLE = 0x3066u, + SHT3X_COMMAND_READ_STATUS = 0xf32du, + SHT3X_COMMAND_FETCH_DATA = 0xe000u, + SHT3X_COMMAND_MEASURE_HIGHREP_10HZ = 0x2737u, + SHT3X_COMMAND_MEASURE_LOWREP_10HZ = 0x272au } sht3x_command_t; /* @@ -36,37 +35,45 @@ typedef enum { * repeatability */ #define SHT30_START_CMD_NCS 0x2400 +// Function pointer types +typedef int (*I2C_TransmitFunc)(uint8_t addr, const uint8_t *data, size_t len, uint32_t timeout); +typedef int (*I2C_ReceiveFunc)(uint8_t addr, uint8_t *data, size_t len, uint32_t timeout); +typedef int (*I2C_MemReadFunc)(uint8_t addr, uint16_t mem_addr, uint16_t mem_addr_size, uint8_t *data, size_t len, uint32_t timeout); +typedef void (*DelayFunc)(uint32_t ms); + + typedef struct { - I2C_HandleTypeDef *i2c_handle; - uint16_t status_reg; + I2C_TransmitFunc i2c_transmit; + I2C_ReceiveFunc i2c_receive; + I2C_MemReadFunc i2c_mem_read; + DelayFunc delay; 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 - 0 on success, negative value on error */ -HAL_StatusTypeDef sht30_init(sht30_t *sht30); +int sht30_init(sht30_t *sht30); /** * @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 - 0 on success, negative value on error */ -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 - 0 on success, negative value on error */ -HAL_StatusTypeDef sht30_get_temp_humid(sht30_t *sht30); +int sht30_get_temp_humid(sht30_t *sht30); -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/general/src/sht30.c b/general/src/sht30.c index 9f69ac67..ea69a690 100644 --- a/general/src/sht30.c +++ b/general/src/sht30.c @@ -1,16 +1,8 @@ #include "sht30.h" -#include -#include -static HAL_StatusTypeDef sht30_write_reg(sht30_t *sht30, uint16_t command) { - uint8_t command_buffer[2] = {(command & 0xff00u) >> 8u, command & 0xffu}; - - if (HAL_I2C_Master_Transmit(sht30->i2c_handle, SHT30_I2C_ADDR, command_buffer, - sizeof(command_buffer), 30) != HAL_OK) { - return false; - } - - return true; +static int sht30_write_reg(const sht30_t *sht30, uint16_t command) { + uint8_t command_buffer[2] = {(uint8_t)((command & 0xff00u) >> 8u), (uint8_t)(command & 0xffu)}; + return sht30->i2c_transmit(SHT30_I2C_ADDR, command_buffer, sizeof(command_buffer), 30u); } /** @@ -18,14 +10,14 @@ static HAL_StatusTypeDef sht30_write_reg(sht30_t *sht30, uint16_t command) { * @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++) { + uint8_t crc = 0xffu; + for (size_t i = 0u; i < length; i++) { crc ^= data[i]; - for (size_t j = 0; j < 8; j++) { - if ((crc & 0x80u) != 0) { + for (uint8_t j = 0u; j < 8u; j++) { + if ((crc & 0x80u) != 0u) { crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u); } else { - crc <<= 1u; + crc = (uint8_t)(crc << 1u); } } } @@ -36,78 +28,67 @@ 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) { 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; + int status = sht30->i2c_mem_read(SHT30_I2C_ADDR, SHT3X_COMMAND_READ_STATUS, 2u, + status_reg_and_checksum, sizeof(status_reg_and_checksum), 30u); + if (status != 0) { + return status; } - uint8_t calculated_crc = calculate_crc(status_reg_and_checksum, 2); - + uint8_t calculated_crc = calculate_crc(status_reg_and_checksum, 2u); if (calculated_crc != status_reg_and_checksum[2]) { - return false; + 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; - +int sht30_get_temp_humid(sht30_t *sht30) { union { struct __attribute__((packed)) { - uint16_t - temp; // The packed attribute does not correctly arrange the bytes + uint16_t temp; uint8_t temp_crc; - uint16_t - humidity; // The packed attribute does not correctly arrange the bytes + uint16_t humidity; uint8_t humidity_crc; } raw_data; uint8_t databuf[6]; } data; - uint16_t temp, humidity; - - sht30_write_reg(sht30, (SHT30_START_CMD_WCS)); - - HAL_Delay(1); - - status = HAL_I2C_Master_Receive(sht30->i2c_handle, SHT30_I2C_ADDR, - data.databuf, sizeof(data.databuf), 30); - if (status != HAL_OK) { - return false; + int status = sht30_write_reg(sht30, SHT30_START_CMD_WCS); + if (status != 0) { + return status; } - temp = uint8_to_uint16(data.databuf[0], data.databuf[1]); + sht30->delay(1u); - if (data.raw_data.temp_crc != calculate_crc(data.databuf, 2)) { - return HAL_ERROR; + status = sht30->i2c_receive(SHT30_I2C_ADDR, data.databuf, sizeof(data.databuf), 30u); + if (status != 0) { + return status; } - float val = -45.0f + 175.0f * (float)temp / 65535.0f; + uint16_t temp = uint8_to_uint16(data.databuf[0], data.databuf[1]); + if (data.raw_data.temp_crc != calculate_crc(data.databuf, 2u)) { + return -1; + } - sht30->temp = (uint16_t)val; + uint32_t temp_val = (uint32_t)(175000u * (uint32_t)temp / 65535u - 45000u); + sht30->temp = (uint16_t)(temp_val / 1000u); - 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; + uint16_t humidity = uint8_to_uint16(data.databuf[3], data.databuf[4]); + if (data.raw_data.humidity_crc != calculate_crc(data.databuf + 3, 2u)) { + return -1; } - humidity = (uint16_t)(100.0f * (float)humidity / 65535.0f); - sht30->humidity = humidity; + sht30->humidity = (uint16_t)((100000u * (uint32_t)humidity) / 65535u); - return HAL_OK; -} \ No newline at end of file + return 0; +} From 670bc17553ad9ab25a78806e3b77233c413fe1dc Mon Sep 17 00:00:00 2001 From: marvin-g-neu <141779828+marvin-g-neu@users.noreply.github.com> Date: Sun, 20 Oct 2024 16:00:19 -0400 Subject: [PATCH 2/6] updated sht --- general/.DS_Store | Bin 0 -> 6148 bytes general/include/.DS_Store | Bin 0 -> 6148 bytes general/include/sht30.h | 14 +++++++------- general/src/.DS_Store | Bin 0 -> 6148 bytes general/src/sht30.c | 6 ++---- 5 files changed, 9 insertions(+), 11 deletions(-) create mode 100644 general/.DS_Store create mode 100644 general/include/.DS_Store create mode 100644 general/src/.DS_Store diff --git a/general/.DS_Store b/general/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e0d9c9ed14685549f51b33584312fa082ed234d1 GIT binary patch literal 6148 zcmeHK%Sr=55Ue%<1733UIKSW@3?Y7jKOpXU5EcW5-1p>n`Ds=^5W`0B;zg>Vr)Fxq zd)PW`Zv(L1X}klr05)_-y!)~=f9}4pt7052YMe0Qc{~o&(_s;Pneg0eyxH1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 #include -/** +/**A * https://www.mouser.com/datasheet/2/682/Sensirion_Humidity_Sensors_SHT3x_Datasheet_digital-971521.pdf * --Datasheet * @@ -36,16 +36,16 @@ typedef enum { #define SHT30_START_CMD_NCS 0x2400 // Function pointer types -typedef int (*I2C_TransmitFunc)(uint8_t addr, const uint8_t *data, size_t len, uint32_t timeout); -typedef int (*I2C_ReceiveFunc)(uint8_t addr, uint8_t *data, size_t len, uint32_t timeout); -typedef int (*I2C_MemReadFunc)(uint8_t addr, uint16_t mem_addr, uint16_t mem_addr_size, uint8_t *data, size_t len, uint32_t timeout); +typedef int (*Write_ptr)(uint8_t addr, const uint8_t *data, size_t len, uint32_t timeout); +typedef int (*Read_Ptr)(uint8_t addr, uint8_t *data, size_t len, uint32_t timeout); +typedef int (*Mem_Read_Ptr)(uint8_t addr, uint16_t mem_addr, uint16_t mem_addr_size, uint8_t *data, size_t len, uint32_t timeout); typedef void (*DelayFunc)(uint32_t ms); typedef struct { - I2C_TransmitFunc i2c_transmit; - I2C_ReceiveFunc i2c_receive; - I2C_MemReadFunc i2c_mem_read; + Write_ptr write; + Read_Ptr read; + Mem_Read_Ptr mem_read; DelayFunc delay; uint16_t temp; uint16_t humidity; diff --git a/general/src/.DS_Store b/general/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0delay(1u); - status = sht30->i2c_receive(SHT30_I2C_ADDR, data.databuf, sizeof(data.databuf), 30u); if (status != 0) { return status; @@ -80,7 +78,6 @@ int sht30_get_temp_humid(sht30_t *sht30) { return -1; } - uint32_t temp_val = (uint32_t)(175000u * (uint32_t)temp / 65535u - 45000u); sht30->temp = (uint16_t)(temp_val / 1000u); uint16_t humidity = uint8_to_uint16(data.databuf[3], data.databuf[4]); @@ -88,7 +85,8 @@ int sht30_get_temp_humid(sht30_t *sht30) { return -1; } - sht30->humidity = (uint16_t)((100000u * (uint32_t)humidity) / 65535u); + humidity = (uint16_t)(100.0f * (float)humidity / 65535.0f); + sht30->humidity = humidity; return 0; } From 8ac8973e2bcfce5b0d6ca9051f104038ca25020b Mon Sep 17 00:00:00 2001 From: marvin-g-neu <141779828+marvin-g-neu@users.noreply.github.com> Date: Sun, 3 Nov 2024 14:58:52 -0500 Subject: [PATCH 3/6] working version of sht --- general/include/sht30.h | 65 ++++++++-------- general/src/sht30.c | 163 ++++++++++++++++++++++------------------ 2 files changed, 122 insertions(+), 106 deletions(-) diff --git a/general/include/sht30.h b/general/include/sht30.h index deb5b47e..58105f60 100644 --- a/general/include/sht30.h +++ b/general/include/sht30.h @@ -1,29 +1,35 @@ -#ifndef SHT30_H -#define SHT30_H +#ifndef sht30_h +#define sht30_h #include #include -#include -/**A +/** * https://www.mouser.com/datasheet/2/682/Sensirion_Humidity_Sensors_SHT3x_Datasheet_digital-971521.pdf * --Datasheet * - */ -#define SHT30_I2C_ADDR (0x44u << 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 = 0x2c06u, - SHT3X_COMMAND_CLEAR_STATUS = 0x3041u, - SHT3X_COMMAND_SOFT_RESET = 0x30A2u, - SHT3X_COMMAND_HEATER_ENABLE = 0x306du, - SHT3X_COMMAND_HEATER_DISABLE = 0x3066u, - SHT3X_COMMAND_READ_STATUS = 0xf32du, - SHT3X_COMMAND_FETCH_DATA = 0xe000u, - SHT3X_COMMAND_MEASURE_HIGHREP_10HZ = 0x2737u, - SHT3X_COMMAND_MEASURE_LOWREP_10HZ = 0x272au + 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); +typedef void (*DelayFunc)(uint32_t ms); + /* * Start measurement command with clock streching enabled and high * repeatability. This is responsible for retrieving the temp and humidity in @@ -35,36 +41,29 @@ typedef enum { * repeatability */ #define SHT30_START_CMD_NCS 0x2400 -// Function pointer types -typedef int (*Write_ptr)(uint8_t addr, const uint8_t *data, size_t len, uint32_t timeout); -typedef int (*Read_Ptr)(uint8_t addr, uint8_t *data, size_t len, uint32_t timeout); -typedef int (*Mem_Read_Ptr)(uint8_t addr, uint16_t mem_addr, uint16_t mem_addr_size, uint8_t *data, size_t len, uint32_t timeout); -typedef void (*DelayFunc)(uint32_t ms); - - typedef struct { - Write_ptr write; - Read_Ptr read; - Mem_Read_Ptr mem_read; - DelayFunc delay; - uint16_t temp; - uint16_t humidity; + Write_ptr write_reg; + Read_ptr read_reg; + DelayFunc delay; + uint16_t temp; + uint16_t humidity; + bool is_heater_enabled; } sht30_t; /** * @brief Initializes an SHT30 Driver * * @param sht30 - SHT30 driver - * @return int - 0 on success, negative value on error + * @return int - Status code */ -int sht30_init(sht30_t *sht30); +int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg, DelayFunc delay); /** * @brief Toggles the status of the internal heater * * @param sht30 - SHT30 driver * @param enable - true to enable, false to disable - * @return int - 0 on success, negative value on error + * @return int - Status code */ int sht30_toggle_heater(sht30_t *sht30, bool enable); @@ -72,8 +71,8 @@ int sht30_toggle_heater(sht30_t *sht30, bool enable); * @brief Retrieves the temperature and humidity * * @param sht30 - SHT30 driver - * @return int - 0 on success, negative value on error + * @return int - Status code */ int sht30_get_temp_humid(sht30_t *sht30); -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/general/src/sht30.c b/general/src/sht30.c index 28a8de34..0d6c7c26 100644 --- a/general/src/sht30.c +++ b/general/src/sht30.c @@ -1,92 +1,109 @@ #include "sht30.h" +#include +#include -static int sht30_write_reg(const sht30_t *sht30, uint16_t command) { - uint8_t command_buffer[2] = {(uint8_t)((command & 0xff00u) >> 8u), (uint8_t)(command & 0xffu)}; - return sht30->i2c_transmit(SHT30_I2C_ADDR, command_buffer, sizeof(command_buffer), 30u); +static int sht30_write_reg(sht30_t *sht30, uint16_t command) +{ + uint8_t command_buffer[2] = { (command & 0xff00u) >> 8u, + command & 0xffu }; + + 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 = 0xffu; - for (size_t i = 0u; i < length; i++) { - crc ^= data[i]; - for (uint8_t j = 0u; j < 8u; j++) { - if ((crc & 0x80u) != 0u) { - crc = (uint8_t)((uint8_t)(crc << 1u) ^ 0x31u); - } else { - crc = (uint8_t)(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; } -int sht30_init(sht30_t *sht30) { - uint8_t status_reg_and_checksum[3]; - int status = sht30->i2c_mem_read(SHT30_I2C_ADDR, SHT3X_COMMAND_READ_STATUS, 2u, - status_reg_and_checksum, sizeof(status_reg_and_checksum), 30u); - if (status != 0) { - return status; - } +int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg, DelayFunc delay) +{ + sht30->write_reg = write_reg; + sht30->read_reg = read_reg; + sht30->delay = delay; + + 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, 2u); - if (calculated_crc != status_reg_and_checksum[2]) { - return -1; - } + uint8_t calculated_crc = calculate_crc(status_reg_and_checksum, 2); - return 0; + if (calculated_crc != status_reg_and_checksum[2]) { + return -1; + } + + return 0; } -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); - } +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); + } } -int sht30_get_temp_humid(sht30_t *sht30) { - union { - struct __attribute__((packed)) { - uint16_t temp; - uint8_t temp_crc; - uint16_t humidity; - uint8_t humidity_crc; - } raw_data; - uint8_t databuf[6]; - } data; - - int status = sht30_write_reg(sht30, SHT30_START_CMD_WCS); - if (status != 0) { - return status; - } - - status = sht30->i2c_receive(SHT30_I2C_ADDR, data.databuf, sizeof(data.databuf), 30u); - if (status != 0) { - return status; - } - - uint16_t temp = uint8_to_uint16(data.databuf[0], data.databuf[1]); - if (data.raw_data.temp_crc != calculate_crc(data.databuf, 2u)) { - return -1; - } - - sht30->temp = (uint16_t)(temp_val / 1000u); - - uint16_t humidity = uint8_to_uint16(data.databuf[3], data.databuf[4]); - if (data.raw_data.humidity_crc != calculate_crc(data.databuf + 3, 2u)) { - return -1; - } - - humidity = (uint16_t)(100.0f * (float)humidity / 65535.0f); - sht30->humidity = humidity; - - return 0; +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; + + sht30_write_reg(sht30, (SHT30_START_CMD_WCS)); + + sht30->delay(1); + + if (sht30->read_reg(data.databuf, 0, sizeof(data.databuf)) != 0) { + return -1; + } + + temp = uint8_to_uint16(data.databuf[0], data.databuf[1]); + + if (data.raw_data.temp_crc != calculate_crc(data.databuf, 2)) { + return -1; + } + + float val = -45.0f + 175.0f * (float)temp / 65535.0f; + + 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 = (uint16_t)(100.0f * (float)humidity / 65535.0f); + sht30->humidity = humidity; + + return 0; } From f1b3514a534d77e3d6608d910c12c0f54696677f Mon Sep 17 00:00:00 2001 From: marvin-g-neu <141779828+marvin-g-neu@users.noreply.github.com> Date: Sun, 3 Nov 2024 16:56:08 -0500 Subject: [PATCH 4/6] removed delay --- general/include/sht30.h | 4 +--- general/src/sht30.c | 5 +---- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/general/include/sht30.h b/general/include/sht30.h index 58105f60..0ba8b886 100644 --- a/general/include/sht30.h +++ b/general/include/sht30.h @@ -28,7 +28,6 @@ typedef enum { /** 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); -typedef void (*DelayFunc)(uint32_t ms); /* * Start measurement command with clock streching enabled and high @@ -44,7 +43,6 @@ typedef void (*DelayFunc)(uint32_t ms); typedef struct { Write_ptr write_reg; Read_ptr read_reg; - DelayFunc delay; uint16_t temp; uint16_t humidity; bool is_heater_enabled; @@ -56,7 +54,7 @@ typedef struct { * @param sht30 - SHT30 driver * @return int - Status code */ -int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg, DelayFunc delay); +int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg); /** * @brief Toggles the status of the internal heater diff --git a/general/src/sht30.c b/general/src/sht30.c index 0d6c7c26..cc5408ba 100644 --- a/general/src/sht30.c +++ b/general/src/sht30.c @@ -35,11 +35,10 @@ static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb) return (uint16_t)((uint16_t)msb << 8u) | lsb; } -int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg, DelayFunc delay) +int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg) { sht30->write_reg = write_reg; sht30->read_reg = read_reg; - sht30->delay = delay; uint8_t status_reg_and_checksum[3]; if (sht30->read_reg(status_reg_and_checksum, SHT3X_COMMAND_READ_STATUS, @@ -81,8 +80,6 @@ int sht30_get_temp_humid(sht30_t *sht30) sht30_write_reg(sht30, (SHT30_START_CMD_WCS)); - sht30->delay(1); - if (sht30->read_reg(data.databuf, 0, sizeof(data.databuf)) != 0) { return -1; } From b1ab0a908b394a87dbf67c9d3361ca8a8ea3a29b Mon Sep 17 00:00:00 2001 From: Jake-Hensley Date: Mon, 20 Jan 2025 19:20:52 -0500 Subject: [PATCH 5/6] made changes to fit style --- general/include/sht30.h | 6 +++--- general/src/sht30.c | 27 ++++++++++++++------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/general/include/sht30.h b/general/include/sht30.h index 0ba8b886..0f6613e8 100644 --- a/general/include/sht30.h +++ b/general/include/sht30.h @@ -27,7 +27,7 @@ typedef enum { /** 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); +typedef int (*Read_ptr)(unit8_t *data, uint8_t reg, uint8_t length); /* * Start measurement command with clock streching enabled and high @@ -43,8 +43,8 @@ typedef int (*Read_ptr)(uint8_t *data, uint8_t reg, uint8_t length); typedef struct { Write_ptr write_reg; Read_ptr read_reg; - uint16_t temp; - uint16_t humidity; + float temp; + float humidity; bool is_heater_enabled; } sht30_t; diff --git a/general/src/sht30.c b/general/src/sht30.c index cc5408ba..2ab28088 100644 --- a/general/src/sht30.c +++ b/general/src/sht30.c @@ -1,7 +1,7 @@ #include "sht30.h" #include #include - +//ner flash --ftdi for msb static int sht30_write_reg(sht30_t *sht30, uint16_t command) { uint8_t command_buffer[2] = { (command & 0xff00u) >> 8u, @@ -17,13 +17,13 @@ static int sht30_write_reg(sht30_t *sht30, uint16_t command) static uint8_t calculate_crc(const uint8_t *data, size_t length) { uint8_t crc = 0xff; - for (size_t i = 0; i < length; i++) { + 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); + if ((crc & 0x80) != 0) { + crc = (uint8_t)((uint8_t)(crc << 1) ^ 0x91); } else { - crc <<= 1u; + crc <<= 1; } } } @@ -32,7 +32,7 @@ static uint8_t calculate_crc(const uint8_t *data, size_t length) static uint16_t uint8_to_uint16(uint8_t msb, uint8_t lsb) { - return (uint16_t)((uint16_t)msb << 8u) | lsb; + return (uint16_t)((uint16_t)msb << 8) | lsb; } int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg) @@ -43,7 +43,7 @@ int sht30_init(sht30_t *sht30, Write_ptr write_reg, Read_ptr read_reg) 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; + return -1; } uint8_t calculated_crc = calculate_crc(status_reg_and_checksum, 2); @@ -76,7 +76,7 @@ int sht30_get_temp_humid(sht30_t *sht30) uint8_t databuf[6]; } data; - uint16_t temp, humidity; + uint8_t temp, humidity; sht30_write_reg(sht30, (SHT30_START_CMD_WCS)); @@ -90,17 +90,18 @@ int sht30_get_temp_humid(sht30_t *sht30) return -1; } - float val = -45.0f + 175.0f * (float)temp / 65535.0f; + float tempVal = -45.0f + 175.0f * (((float)temp) / 65535.0f); - sht30->temp = (uint16_t)val; + sht30->temp = tempVal; 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 = (uint16_t)(100.0f * (float)humidity / 65535.0f); - sht30->humidity = humidity; + float humVal = (100.0f * ((float)humidity / 65535.0f)); + + sht30->humidity = humVal; return 0; -} +} \ No newline at end of file From 0f7b718221040170aba79ea7568849f199ef126c Mon Sep 17 00:00:00 2001 From: Jake-Hensley Date: Thu, 23 Jan 2025 19:57:20 -0500 Subject: [PATCH 6/6] made command buffer casted as uint8 --- general/src/sht30.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/general/src/sht30.c b/general/src/sht30.c index 2ab28088..14e09d99 100644 --- a/general/src/sht30.c +++ b/general/src/sht30.c @@ -4,8 +4,8 @@ //ner flash --ftdi for msb static int sht30_write_reg(sht30_t *sht30, uint16_t command) { - uint8_t command_buffer[2] = { (command & 0xff00u) >> 8u, - command & 0xffu }; + uint8_t command_buffer[2] = { (uint8_t)(command & 0xff00) >> 8, + (uint8_t)(command & 0xff) }; return sht30->write_reg(command_buffer, 0, sizeof(command_buffer)); }