diff --git a/general/.DS_Store b/general/.DS_Store new file mode 100644 index 00000000..e0d9c9ed Binary files /dev/null and b/general/.DS_Store differ diff --git a/general/include/.DS_Store b/general/include/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/general/include/.DS_Store differ diff --git a/general/include/sht30.h b/general/include/sht30.h index 89ca1928..0f6613e8 100644 --- a/general/include/sht30.h +++ b/general/include/sht30.h @@ -1,7 +1,6 @@ #ifndef sht30_h #define sht30_h -#include "stm32xx_hal.h" #include #include @@ -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)(unit8_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 @@ -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; + float temp; + float 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 \ No newline at end of file diff --git a/general/src/.DS_Store b/general/src/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/general/src/.DS_Store differ diff --git a/general/src/sht30.c b/general/src/sht30.c index 9f69ac67..14e09d99 100644 --- a/general/src/sht30.c +++ b/general/src/sht30.c @@ -1,113 +1,107 @@ #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] = { (uint8_t)(command & 0xff00) >> 8, + (uint8_t)(command & 0xff) }; -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; + 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 & 0x80) != 0) { + crc = (uint8_t)((uint8_t)(crc << 1) ^ 0x91); + } else { + crc <<= 1; + } + } + } + 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 << 8) | 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; + uint8_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 tempVal = -45.0f + 175.0f * (((float)temp) / 65535.0f); - float val = -45.0f + 175.0f * (float)temp / 65535.0f; + sht30->temp = tempVal; - 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; - } + float humVal = (100.0f * ((float)humidity / 65535.0f)); - humidity = (uint16_t)(100.0f * (float)humidity / 65535.0f); - sht30->humidity = humidity; + sht30->humidity = humVal; - return HAL_OK; + return 0; } \ No newline at end of file