Modified sps30 to function as a standalone lib
This commit is contained in:
parent
35a281c505
commit
13c1ef5e68
104
sps30.c
104
sps30.c
@ -6,7 +6,25 @@
|
||||
*/
|
||||
|
||||
#include "sps30.h"
|
||||
#include "main.h" /* for uart_disable_interrupts() */
|
||||
|
||||
/*
|
||||
* Functions to be implemented by user
|
||||
*/
|
||||
|
||||
/* I2C */
|
||||
int8_t sps30_i2c_transmit(uint8_t address, uint8_t *buffer, int len) __attribute__((weak));
|
||||
int8_t sps30_i2c_receive(uint8_t address, uint8_t *buffer, int len) __attribute__((weak));
|
||||
/* CRC */
|
||||
uint8_t sensirion_crc8_calculate(const uint8_t *data, uint16_t count) __attribute__((weak));
|
||||
/* Interrupts */
|
||||
int8_t sps30_disable_interrupts(void) __attribute__((weak));
|
||||
int8_t sps30_enable_interrupts(void) __attribute__((weak));
|
||||
/* delay function */
|
||||
void delay_ms(int delay_ms) __attribute__((weak));
|
||||
|
||||
/*
|
||||
* Public functions
|
||||
*/
|
||||
|
||||
int8_t sps30_send_cmd(sps30_cmd_t cmd)
|
||||
{
|
||||
@ -16,11 +34,11 @@ int8_t sps30_send_cmd(sps30_cmd_t cmd)
|
||||
// start measurement
|
||||
buffer[0] = cmd >> 8;
|
||||
buffer[1] = cmd & 0x00ff;
|
||||
uart_disable_interrupts();
|
||||
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
||||
uart_enable_interrupts();
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
||||
sps30_enable_interrupts();
|
||||
// TODO: Proc to vraci NACK? Vyresit.
|
||||
if (result != I2C_OK) {
|
||||
if (result != 0) {
|
||||
return SPS30_ERROR;
|
||||
}
|
||||
|
||||
@ -36,13 +54,13 @@ int8_t sps30_start_measurement( void )
|
||||
buffer[1] = SPS30_START_MEASUREMENT & 0x00ff;
|
||||
buffer[2] = SPS30_UINT16_FORMAT;
|
||||
buffer[3] = 0x00;
|
||||
buffer[4] = crc8_calculate(buffer + 2, 2);
|
||||
buffer[4] = sensirion_crc8_calculate(buffer + 2, 2);
|
||||
|
||||
uart_disable_interrupts();
|
||||
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 5);
|
||||
uart_enable_interrupts();
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 5);
|
||||
sps30_enable_interrupts();
|
||||
|
||||
if (result != I2C_OK) {
|
||||
if (result != 0) {
|
||||
return SPS30_ERROR;
|
||||
}
|
||||
return SPS30_OK;
|
||||
@ -59,28 +77,28 @@ int8_t sps30_read_measured_values(sps30_data_t *measured_data)
|
||||
|
||||
uint8_t result;
|
||||
|
||||
// start measurement
|
||||
/* start measurement */
|
||||
buffer[0] = SPS30_READ_MEASURED_VALUES >> 8;
|
||||
buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF;
|
||||
uart_disable_interrupts();
|
||||
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
||||
uart_enable_interrupts();
|
||||
if (result != I2C_OK) {
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
||||
sps30_enable_interrupts();
|
||||
if (result != 0) {
|
||||
return SPS30_ERROR;
|
||||
}
|
||||
LL_mDelay(10); // 10 ms should be enough
|
||||
// read out
|
||||
uart_disable_interrupts();
|
||||
result = i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 3 * SPS30_MEASURED_VALUES_COUNT);
|
||||
uart_enable_interrupts();
|
||||
if (result != I2C_OK)
|
||||
delay_ms(10);
|
||||
/* read out */
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 3 * SPS30_MEASURED_VALUES_COUNT);
|
||||
sps30_enable_interrupts();
|
||||
if (result != 0)
|
||||
{
|
||||
return SPS30_ERROR;
|
||||
}
|
||||
/* check data integrity */
|
||||
for (uint8_t i = 0; i < SPS30_MEASURED_VALUES_COUNT; i++)
|
||||
{
|
||||
uint8_t checksum_calculated = crc8_calculate(buffer + 3*i, 2);
|
||||
uint8_t checksum_calculated = sensirion_crc8_calculate(buffer + 3*i, 2);
|
||||
uint8_t checksum_received = buffer[3*i + 2];
|
||||
if (checksum_calculated != checksum_received) {
|
||||
return SPS30_CRC8_ERROR;
|
||||
@ -131,20 +149,20 @@ int8_t sps30_read_status_register ( void )
|
||||
|
||||
uint8_t result;
|
||||
|
||||
// start measurement
|
||||
/* start measurement */
|
||||
buffer[0] = SPS30_READ_DEVICE_STATUS_REGISTER >> 8;
|
||||
buffer[1] = SPS30_READ_DEVICE_STATUS_REGISTER & 0x00ff;
|
||||
uart_disable_interrupts();
|
||||
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
||||
uart_enable_interrupts();
|
||||
if (result != I2C_OK) {
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
||||
sps30_enable_interrupts();
|
||||
if (result != 0) {
|
||||
return SPS30_ERROR;
|
||||
}
|
||||
LL_mDelay(10); // 10 ms should be enough
|
||||
// read out
|
||||
uart_disable_interrupts();
|
||||
result = i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 6);
|
||||
uart_enable_interrupts();
|
||||
delay_ms(10);
|
||||
/* read out */
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 6);
|
||||
sps30_enable_interrupts();
|
||||
// TODO
|
||||
|
||||
return SPS30_OK;
|
||||
@ -157,25 +175,25 @@ int8_t sps30_read_firmware_version ( uint8_t * fw_ver_hi, uint8_t * fw_ver_lo )
|
||||
|
||||
uint8_t result;
|
||||
|
||||
// start measurement
|
||||
/* start measurement */
|
||||
i2c_tx_buffer[0] = SPS30_READ_VERSION >> 8;
|
||||
i2c_tx_buffer[1] = SPS30_READ_VERSION & 0x00ff;
|
||||
uart_disable_interrupts();
|
||||
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2);
|
||||
uart_enable_interrupts();
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2);
|
||||
sps30_enable_interrupts();
|
||||
|
||||
// TODO: Proc to vraci NACK? Vyresit.
|
||||
/*if (result != I2C_OK) {
|
||||
/*if (result != 0) {
|
||||
return SPS30_ERROR;
|
||||
}
|
||||
return SPS30_OK;*/
|
||||
|
||||
LL_mDelay(1); // 10 ms should be enough
|
||||
// read out
|
||||
uart_disable_interrupts();
|
||||
result = i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 3);
|
||||
uart_enable_interrupts();
|
||||
/*if (result != I2C_OK)
|
||||
delay_ms(1);
|
||||
/* read out */
|
||||
sps30_disable_interrupts();
|
||||
result = sps30_i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 3);
|
||||
sps30_enable_interrupts();
|
||||
/*if (result != 0)
|
||||
{
|
||||
return SPS30_ERROR;
|
||||
}*/
|
||||
|
23
sps30.h
23
sps30.h
@ -9,10 +9,6 @@
|
||||
#define INC_SPS30_H_
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stm32l0xx_ll_i2c.h"
|
||||
#include "stm32l0xx_ll_utils.h"
|
||||
#include "i2c.h"
|
||||
#include "crc8.h"
|
||||
|
||||
/*
|
||||
* Defines & macros
|
||||
@ -74,19 +70,14 @@ typedef struct {
|
||||
|
||||
int8_t sps30_send_cmd(sps30_cmd_t cmd);
|
||||
|
||||
int8_t sps30_start_measurement( void );
|
||||
int8_t sps30_stop_measurement( void );
|
||||
int8_t sps30_start_measurement(void);
|
||||
int8_t sps30_stop_measurement(void);
|
||||
int8_t sps30_read_measured_values(sps30_data_t *measured_data);
|
||||
|
||||
int8_t sps30_sleep( void );
|
||||
int8_t sps30_wake_up( void );
|
||||
|
||||
int8_t sps30_sleep(void);
|
||||
int8_t sps30_wake_up(void);
|
||||
int8_t sps30_start_fan_cleaning( void );
|
||||
|
||||
int8_t sps30_reset( void );
|
||||
|
||||
int8_t sps30_read_status_register ( void );
|
||||
|
||||
int8_t sps30_read_firmware_version ( uint8_t * fw_ver_hi, uint8_t * fw_ver_lo );
|
||||
int8_t sps30_reset(void);
|
||||
int8_t sps30_read_status_register(void);
|
||||
int8_t sps30_read_firmware_version(uint8_t *fw_ver_hi, uint8_t *fw_ver_lo);
|
||||
|
||||
#endif /* INC_SPS30_H_ */
|
||||
|
Loading…
Reference in New Issue
Block a user