WIP SHT4x
This commit is contained in:
parent
22612f63e4
commit
dc7f86f5af
@ -49,6 +49,7 @@ extern "C" {
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
#include "led.h"
|
||||
#include "sht4x.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
|
51
fw/Core/Inc/sht4x.h
Normal file
51
fw/Core/Inc/sht4x.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
/*
|
||||
* Defines & macros
|
||||
*/
|
||||
|
||||
#define SHT4X_I2C_ADDRESS 0x44
|
||||
|
||||
/*
|
||||
* Data types
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
I2C_TypeDef *i2c;
|
||||
} sht4x_context_t;
|
||||
|
||||
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
|
||||
*/
|
||||
|
||||
int sht4x_init(sht4x_context_t *context);
|
||||
int sht4x_send_cmd(sht4x_cmd_t cmd);
|
||||
int sht4x_read_data(uint8_t *buffer, int len);
|
||||
int sht4x_measure(int *temperature, int *relative_humidity);
|
||||
|
||||
#endif /* INC_SHT4X_H_ */
|
@ -108,14 +108,20 @@ int main(void)
|
||||
led_context.blue_led_pin = LED_B_Pin;
|
||||
led_init(&led_context, 50, 1000);
|
||||
led_set_color(80, 0, 0);
|
||||
/* SHT4x context init */
|
||||
sht4x_context_t sht4x_context;
|
||||
sht4x_context.i2c = I2C1;
|
||||
sht4x_init(&sht4x_context);
|
||||
/* USER CODE END 2 */
|
||||
|
||||
/* Infinite loop */
|
||||
/* USER CODE BEGIN WHILE */
|
||||
int T, RH;
|
||||
sht4x_measure(&T, &RH);
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
led_pwm_handler(); // TODO temporary, should be placed in timer interrupt (SysTick)
|
||||
/* USER CODE END WHILE */
|
||||
/* USER CODE BEGIN 3 */
|
||||
}
|
||||
/* USER CODE END 3 */
|
||||
@ -218,7 +224,6 @@ static void MX_I2C1_Init(void)
|
||||
LL_I2C_Init(I2C1, &I2C_InitStruct);
|
||||
LL_I2C_SetOwnAddress2(I2C1, 0, LL_I2C_OWNADDRESS2_NOMASK);
|
||||
/* USER CODE BEGIN I2C1_Init 2 */
|
||||
|
||||
/* USER CODE END I2C1_Init 2 */
|
||||
|
||||
}
|
||||
|
83
fw/Core/Src/sht4x.c
Normal file
83
fw/Core/Src/sht4x.c
Normal file
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* sht4x.c
|
||||
*
|
||||
* Created on: Jun 8, 2021
|
||||
* Author: user
|
||||
*/
|
||||
|
||||
#include "sht4x.h"
|
||||
|
||||
sht4x_context_t *sht4x_context;
|
||||
|
||||
int sht4x_init(sht4x_context_t *context)
|
||||
{
|
||||
sht4x_context = context;
|
||||
}
|
||||
|
||||
void i2c_transmit(uint8_t address, uint8_t *buffer, int len)
|
||||
{
|
||||
LL_I2C_HandleTransfer(sht4x_context->i2c, address, LL_I2C_ADDRESSING_MODE_7BIT, len,
|
||||
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
|
||||
int i = 0;
|
||||
while (!LL_I2C_IsActiveFlag_STOP(sht4x_context->i2c)) {
|
||||
if (LL_I2C_IsActiveFlag_TXE(sht4x_context->i2c)) {
|
||||
if (i < len) {
|
||||
LL_I2C_TransmitData8(sht4x_context->i2c, buffer[i++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
LL_I2C_ClearFlag_STOP(sht4x_context->i2c);
|
||||
}
|
||||
|
||||
void i2c_receive(uint8_t address, uint8_t *buffer, int len)
|
||||
{
|
||||
LL_I2C_HandleTransfer(sht4x_context->i2c, address, LL_I2C_ADDRESSING_MODE_7BIT, len,
|
||||
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
|
||||
int i = 0;
|
||||
while (!LL_I2C_IsActiveFlag_STOP(sht4x_context->i2c)) {
|
||||
if (LL_I2C_IsActiveFlag_RXNE(sht4x_context->i2c)) {
|
||||
if (i < len) {
|
||||
buffer[i++] = LL_I2C_ReceiveData8(sht4x_context->i2c);
|
||||
}
|
||||
}
|
||||
}
|
||||
LL_I2C_ClearFlag_STOP(sht4x_context->i2c);
|
||||
}
|
||||
|
||||
int sht4x_send_cmd(sht4x_cmd_t cmd)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int sht4x_read_data(uint8_t *buffer, int len)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int sht4x_measure(int *temperature, int *relative_humidity)
|
||||
{
|
||||
uint8_t buffer[32];
|
||||
|
||||
// start measurement
|
||||
buffer[0] = START_MEAS_HIGH_PRECISION;
|
||||
i2c_transmit(SHT4X_I2C_ADDRESS, buffer, 1);
|
||||
// delay TODO
|
||||
volatile int j,k;
|
||||
for (j = 0; j < 100000; j++);
|
||||
// read out
|
||||
i2c_receive(SHT4X_I2C_ADDRESS, buffer, 6);
|
||||
// TODO
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user