Merged scd4x_minimal

This commit is contained in:
dooku
2021-07-18 10:53:19 +02:00
14 changed files with 134942 additions and 116 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,4 +1,4 @@
update=St 3. března 2021, 12:32:50
update=Thu 17 Jun 2021 08:18:44 AM CEST
version=1
last_client=kicad
[general]
+1 -1
View File
@@ -65,7 +65,7 @@
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel.1650648268" name="Debug level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel" useByScannerDiscovery="false" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel.value.g3" valueType="enumerated"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.622042" name="Optimization level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level" useByScannerDiscovery="false" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.value.os" valueType="enumerated"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.622042" name="Optimization level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level" useByScannerDiscovery="false" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.value.o0" valueType="enumerated"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.definedsymbols.2027210563" name="Define symbols (-D)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.definedsymbols" useByScannerDiscovery="false" valueType="definedSymbols">
+1 -1
View File
@@ -49,7 +49,7 @@ extern "C" {
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "i2c.h"
#include "sht4x.h"
#include "scd4x.h"
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/
+57
View File
@@ -0,0 +1,57 @@
/*
* sht4x.h
*
* Created on: Jun 8, 2021
* Author: user
*/
#ifndef INC_SHT4X_H_
#define INC_SHT4X_H_
#include "stdint.h"
#include "stm32l0xx_ll_i2c.h"
#include "stm32l0xx_ll_utils.h"
#include "i2c.h"
#include "crc8.h"
/*
* Defines & macros
*/
#define SCD4X_I2C_ADDRESS 0x62
/*
* Return values
*/
#define SCD4X_OK 0
#define SCD4X_ERROR -1 // generic error
#define SCD4X_CRC8_ERROR -2 // checksum failed
/*
* Data types
*/
typedef enum {
START_PERIODIC_MEASUREMENT = 0x21B1,
READ_MEASUREMENT = 0xEC05,
STOP_PERIODIC_MEASUREMENT = 0x3F86,
GET_DATA_READY_STATUS = 0xe4b8,
PERFORM_FACTORY_RESET = 0x3632
} scd4x_cmd_t;
/*
* Function prototypes
*/
int8_t scd4x_send_cmd(scd4x_cmd_t cmd);
int8_t scd4x_read_data(uint8_t *buffer, int len);
int8_t scd4x_start_periodic_measurement( void );
int8_t scd4x_stop_periodic_measurement( void );
int8_t scd4x_perform_factory_reset( void );
int8_t scd4x_read_measurement(int * co2, int *temperature, int *relative_humidity);
#endif /* INC_SHT4X_H_ */
-57
View File
@@ -1,57 +0,0 @@
/*
* sht4x.h
*
* Created on: Jun 8, 2021
* Author: user
*/
#ifndef INC_SHT4X_H_
#define INC_SHT4X_H_
#include "stdint.h"
#include "stm32l0xx_ll_i2c.h"
#include "stm32l0xx_ll_utils.h"
#include "i2c.h"
#include "crc8.h"
/*
* Defines & macros
*/
#define SHT4X_I2C_ADDRESS 0x44
/*
* Return values
*/
#define SHT4X_OK 0
#define SHT4X_ERROR -1 // generic error
#define SHT4X_CRC8_ERROR -2 // checksum failed
/*
* Data types
*/
typedef enum {
START_MEAS_HIGH_PRECISION = 0xFD,
START_MEAS_MEDIUM_PRECISION = 0xF6,
START_MEAS_LOW_PRECISION = 0xE0,
READ_SERIAL = 0x89,
SOFT_RESET = 0x94,
HEATER_200_mW_1_s = 0x39,
HEATER_200_mW_01_s = 0x32,
HEATER_110_mW_1_s = 0x2F,
HEATER_110_mW_01_s = 0x24,
HEATER_20_mW_1_s = 0x1E,
HEATER_20_mW_01_s = 0x15
} sht4x_cmd_t;
/*
* Function prototypes
*/
int8_t sht4x_send_cmd(sht4x_cmd_t cmd);
int8_t sht4x_read_data(uint8_t *buffer, int len);
int8_t sht4x_measure(int *temperature, int *relative_humidity);
#endif /* INC_SHT4X_H_ */
+1 -1
View File
@@ -21,7 +21,7 @@ int i2c_init(i2c_context_t *context)
int i2c_transmit(uint8_t address, uint8_t *buffer, int len)
{
LL_I2C_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len,
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
int i = 0;
// Autoend mode will raise STOP flag if NACK is detected
// (or if desired number of bytes is transmitted)
+10 -2
View File
@@ -108,6 +108,12 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint8_t uart_data_dummy = 0;
//scd4x_perform_factory_reset();
//LL_mDelay(2000);
scd4x_start_periodic_measurement();
LL_mDelay(2000);
int CO2, T_SCD4x, RH_SCD4x;
int T_SHT4x, RH_SHT4x;
while (1)
{
/* RS485 test */
@@ -116,8 +122,10 @@ int main(void)
uart_data_dummy++;
/* SHT41 measurement */
int T, RH;
sht4x_measure(&T, &RH);
sht4x_measure(&T_SHT4x, &RH_SHT4x);
/* SCD4x measurement */
scd4x_read_measurement(&CO2, &T_SCD4x, &RH_SCD4x);
LL_mDelay(1000);
/* USER CODE END WHILE */
+104
View File
@@ -0,0 +1,104 @@
/*
* sht4x.c
*
* Created on: Jun 8, 2021
* Author: user
*/
#include "scd4x.h"
int8_t scd4x_send_cmd(scd4x_cmd_t cmd)
{
uint8_t buffer[32];
int result;
// start measurement
buffer[0] = cmd >> 8;
buffer[1] = cmd & 0x00ff;
result = i2c_transmit(SCD4X_I2C_ADDRESS<<1, buffer, 2);
if (result != I2C_OK) {
return SCD4X_ERROR;
}
return SCD4X_OK;
}
int8_t scd4x_read_data(uint8_t *buffer, int len)
{
}
int8_t scd4x_start_periodic_measurement( void )
{
return scd4x_send_cmd(START_PERIODIC_MEASUREMENT);
}
int8_t scd4x_stop_periodic_measurement( void )
{
return scd4x_send_cmd(STOP_PERIODIC_MEASUREMENT);
}
int8_t scd4x_perform_factory_reset( void )
{
return scd4x_send_cmd(PERFORM_FACTORY_RESET);
}
int8_t scd4x_read_measurement(int * co2, int *temperature, int *relative_humidity)
{
uint8_t buffer[32];
int result;
// start measurement
// TODO: Check for data ready
/*
buffer[0] = GET_DATA_READY_STATUS >> 8;
buffer[1] = GET_DATA_READY_STATUS & 0x00ff;
result = i2c_transmit(SCD4X_I2C_ADDRESS<<1, buffer, 2);
if (result != I2C_OK) {
return SCD4X_ERROR;
}
LL_mDelay(100); // 10 ms should be enough
// read out
result = i2c_receive(SCD4X_I2C_ADDRESS<<1, buffer, 6);
if (result != I2C_OK) {
return SCD4X_ERROR;
}*/
// start measurement
buffer[0] = READ_MEASUREMENT >> 8;
buffer[1] = READ_MEASUREMENT & 0x00ff;
result = i2c_transmit(SCD4X_I2C_ADDRESS<<1, buffer, 2);
// TODO: Proc to vraci NACK? Vyresit.
/*if (result != I2C_OK) {
return SCD4X_ERROR;
}*/
LL_mDelay(1); // 10 ms should be enough
// read out
result = i2c_receive(SCD4X_I2C_ADDRESS<<1, buffer, 9);
if (result != I2C_OK)
{
return SCD4X_ERROR;
}
// TODO checksum
// Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3
uint32_t co2_ticks = (buffer[0] << 8) + buffer[1];
uint32_t t_ticks = (buffer[3] << 8) + buffer[4];
uint32_t rh_ticks = (buffer[6] << 8) + buffer[7];
int t_degC = -45 + 175 * t_ticks / 65535;
int rh_pRH = 100 * rh_ticks / 65535;
if (rh_pRH > 100) {
rh_pRH = 100;
}
if (rh_pRH < 0) {
rh_pRH = 0;
}
*co2 = co2_ticks;
*temperature = t_degC;
*relative_humidity = rh_pRH;
return SCD4X_OK;
}
-53
View File
@@ -1,53 +0,0 @@
/*
* sht4x.c
*
* Created on: Jun 8, 2021
* Author: user
*/
#include "sht4x.h"
int8_t sht4x_send_cmd(sht4x_cmd_t cmd)
{
}
int8_t sht4x_read_data(uint8_t *buffer, int len)
{
}
int8_t sht4x_measure(int *temperature, int *relative_humidity)
{
uint8_t buffer[32];
int result;
// start measurement
buffer[0] = START_MEAS_HIGH_PRECISION;
result = i2c_transmit(SHT4X_I2C_ADDRESS<<1, buffer, 1);
if (result != I2C_OK) {
return SHT4X_ERROR;
}
LL_mDelay(100); // 10 ms should be enough
// read out
result = i2c_receive(SHT4X_I2C_ADDRESS<<1, buffer, 6);
if (result != I2C_OK) {
return SHT4X_ERROR;
}
// TODO checksum
// Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3
uint32_t t_ticks = (buffer[0] << 8) + buffer[1];
uint32_t rh_ticks = (buffer[3] << 8) + buffer[4];
int t_degC = -45 + 175 * t_ticks / 65535;
int rh_pRH = -6 + 125 * rh_ticks / 65535;
if (rh_pRH > 100) {
rh_pRH = 100;
}
if (rh_pRH < 0) {
rh_pRH = 0;
}
*temperature = t_degC;
*relative_humidity = rh_pRH;
return SHT4X_OK;
}