Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 9dc275b

Browse files
authored
Merge pull request #26 from Sensirion/update-sps30-driver
Update included embedded-sps-i2c driver to v3.1.1
2 parents cf56687 + 4de087d commit 9dc275b

File tree

9 files changed

+327
-222
lines changed

9 files changed

+327
-222
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=sensirion-sps
2-
version=0.9.0
2+
version=1.0.0
33
author=Johannes Winkelmann
44
maintainer=Johannes Winkelmann <[email protected]>
55
sentence=Support for Sensirion's SPS30 particulate matter sensor

sensirion_arch_config.h

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,6 @@
7272
*/
7373
#include <stdint.h>
7474

75-
#ifdef __cplusplus
76-
extern "C" {
77-
#endif
78-
7975
/**
8076
* Typedef section for types commonly defined in <stdint.h>
8177
* If your system does not provide stdint headers, please define them
@@ -90,34 +86,25 @@ extern "C" {
9086
* typedef char int8_t;
9187
* typedef unsigned char uint8_t; */
9288

93-
/* Types not typically provided by <stdint.h> */
94-
typedef float float32_t;
89+
#ifndef __cplusplus
9590

9691
/**
97-
* Define the endianness of your architecture:
98-
* 0: little endian, 1: big endian
99-
* Use the following code to determine if unsure:
100-
* ```c
101-
* #include <stdio.h>
102-
*
103-
* int is_big_endian(void) {
104-
* union {
105-
* unsigned int u;
106-
* char c[sizeof(unsigned int)];
107-
* } e = { 0 };
108-
* e.c[0] = 1;
109-
*
110-
* return (e.i != 1);
111-
* }
112-
*
113-
* int main(void) {
114-
* printf("Use #define SENSIRION_BIG_ENDIAN %d\n", is_big_endian());
115-
*
116-
* return 0;
117-
* }
118-
* ```
92+
* If your platform doesn't define the bool type we define it as int. Depending
93+
* on your system update the definition below.
11994
*/
120-
#define SENSIRION_BIG_ENDIAN 0
95+
#if __STDC_VERSION__ >= 199901L
96+
#include <stdbool.h>
97+
#else
98+
99+
#ifndef bool
100+
#define bool int
101+
#define true 1
102+
#define false 0
103+
#endif /* bool */
104+
105+
#endif /* __STDC_VERSION__ */
106+
107+
#endif /* __cplusplus */
121108

122109
/**
123110
* The clock period of the i2c bus in microseconds. Increase this, if your GPIO
@@ -128,8 +115,4 @@ typedef float float32_t;
128115
*/
129116
#define SENSIRION_I2C_CLOCK_PERIOD_USEC 10
130117

131-
#ifdef __cplusplus
132-
}
133-
#endif
134-
135118
#endif /* SENSIRION_ARCH_CONFIG_H */

sensirion_common.cpp

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,26 @@
3838
#include "sensirion_common.h"
3939
#include "sensirion_i2c.h"
4040

41-
uint8_t sensirion_common_generate_crc(uint8_t *data, uint16_t count) {
41+
uint16_t sensirion_bytes_to_uint16_t(const uint8_t* bytes) {
42+
return (uint16_t)bytes[0] << 8 | (uint16_t)bytes[1];
43+
}
44+
45+
uint32_t sensirion_bytes_to_uint32_t(const uint8_t* bytes) {
46+
return (uint32_t)bytes[0] << 24 | (uint32_t)bytes[1] << 16 |
47+
(uint32_t)bytes[2] << 8 | (uint32_t)bytes[3];
48+
}
49+
50+
float sensirion_bytes_to_float(const uint8_t* bytes) {
51+
union {
52+
uint32_t u32_value;
53+
float float32;
54+
} tmp;
55+
56+
tmp.u32_value = sensirion_bytes_to_uint32_t(bytes);
57+
return tmp.float32;
58+
}
59+
60+
uint8_t sensirion_common_generate_crc(const uint8_t* data, uint16_t count) {
4261
uint16_t current_byte;
4362
uint8_t crc = CRC8_INIT;
4463
uint8_t crc_bit;
@@ -56,15 +75,20 @@ uint8_t sensirion_common_generate_crc(uint8_t *data, uint16_t count) {
5675
return crc;
5776
}
5877

59-
int8_t sensirion_common_check_crc(uint8_t *data, uint16_t count,
78+
int8_t sensirion_common_check_crc(const uint8_t* data, uint16_t count,
6079
uint8_t checksum) {
6180
if (sensirion_common_generate_crc(data, count) != checksum)
6281
return STATUS_FAIL;
63-
return STATUS_OK;
82+
return NO_ERROR;
6483
}
6584

66-
uint16_t sensirion_fill_cmd_send_buf(uint8_t *buf, uint16_t cmd,
67-
const uint16_t *args, uint8_t num_args) {
85+
int16_t sensirion_i2c_general_call_reset(void) {
86+
const uint8_t data = 0x06;
87+
return sensirion_i2c_write(0, &data, (uint16_t)sizeof(data));
88+
}
89+
90+
uint16_t sensirion_fill_cmd_send_buf(uint8_t* buf, uint16_t cmd,
91+
const uint16_t* args, uint8_t num_args) {
6892
uint8_t crc;
6993
uint8_t i;
7094
uint16_t idx = 0;
@@ -76,54 +100,57 @@ uint16_t sensirion_fill_cmd_send_buf(uint8_t *buf, uint16_t cmd,
76100
buf[idx++] = (uint8_t)((args[i] & 0xFF00) >> 8);
77101
buf[idx++] = (uint8_t)((args[i] & 0x00FF) >> 0);
78102

79-
crc = sensirion_common_generate_crc((uint8_t *)&buf[idx - 2],
103+
crc = sensirion_common_generate_crc((uint8_t*)&buf[idx - 2],
80104
SENSIRION_WORD_SIZE);
81105
buf[idx++] = crc;
82106
}
83107
return idx;
84108
}
85109

86-
int16_t sensirion_i2c_read_words_as_bytes(uint8_t address, uint8_t *data,
110+
int16_t sensirion_i2c_read_words_as_bytes(uint8_t address, uint8_t* data,
87111
uint16_t num_words) {
88112
int16_t ret;
89113
uint16_t i, j;
90114
uint16_t size = num_words * (SENSIRION_WORD_SIZE + CRC8_LEN);
91115
uint16_t word_buf[SENSIRION_MAX_BUFFER_WORDS];
92-
uint8_t *const buf8 = (uint8_t *)word_buf;
116+
uint8_t* const buf8 = (uint8_t*)word_buf;
93117

94118
ret = sensirion_i2c_read(address, buf8, size);
95-
if (ret != STATUS_OK)
119+
if (ret != NO_ERROR)
96120
return ret;
97121

98122
/* check the CRC for each word */
99123
for (i = 0, j = 0; i < size; i += SENSIRION_WORD_SIZE + CRC8_LEN) {
100124

101125
ret = sensirion_common_check_crc(&buf8[i], SENSIRION_WORD_SIZE,
102126
buf8[i + SENSIRION_WORD_SIZE]);
103-
if (ret != STATUS_OK)
127+
if (ret != NO_ERROR)
104128
return ret;
105129

106130
data[j++] = buf8[i];
107131
data[j++] = buf8[i + 1];
108132
}
109133

110-
return STATUS_OK;
134+
return NO_ERROR;
111135
}
112136

113-
int16_t sensirion_i2c_read_words(uint8_t address, uint16_t *data_words,
137+
int16_t sensirion_i2c_read_words(uint8_t address, uint16_t* data_words,
114138
uint16_t num_words) {
115139
int16_t ret;
116140
uint8_t i;
141+
const uint8_t* word_bytes;
117142

118-
ret = sensirion_i2c_read_words_as_bytes(address, (uint8_t *)data_words,
143+
ret = sensirion_i2c_read_words_as_bytes(address, (uint8_t*)data_words,
119144
num_words);
120-
if (ret != STATUS_OK)
145+
if (ret != NO_ERROR)
121146
return ret;
122147

123-
for (i = 0; i < num_words; ++i)
124-
data_words[i] = be16_to_cpu(data_words[i]);
148+
for (i = 0; i < num_words; ++i) {
149+
word_bytes = (uint8_t*)&data_words[i];
150+
data_words[i] = ((uint16_t)word_bytes[0] << 8) | word_bytes[1];
151+
}
125152

126-
return STATUS_OK;
153+
return NO_ERROR;
127154
}
128155

129156
int16_t sensirion_i2c_write_cmd(uint8_t address, uint16_t command) {
@@ -134,7 +161,7 @@ int16_t sensirion_i2c_write_cmd(uint8_t address, uint16_t command) {
134161
}
135162

136163
int16_t sensirion_i2c_write_cmd_with_args(uint8_t address, uint16_t command,
137-
const uint16_t *data_words,
164+
const uint16_t* data_words,
138165
uint16_t num_words) {
139166
uint8_t buf[SENSIRION_MAX_BUFFER_WORDS];
140167
uint16_t buf_size;
@@ -144,14 +171,14 @@ int16_t sensirion_i2c_write_cmd_with_args(uint8_t address, uint16_t command,
144171
}
145172

146173
int16_t sensirion_i2c_delayed_read_cmd(uint8_t address, uint16_t cmd,
147-
uint32_t delay_us, uint16_t *data_words,
174+
uint32_t delay_us, uint16_t* data_words,
148175
uint16_t num_words) {
149176
int16_t ret;
150177
uint8_t buf[SENSIRION_COMMAND_SIZE];
151178

152179
sensirion_fill_cmd_send_buf(buf, cmd, NULL, 0);
153180
ret = sensirion_i2c_write(address, buf, SENSIRION_COMMAND_SIZE);
154-
if (ret != STATUS_OK)
181+
if (ret != NO_ERROR)
155182
return ret;
156183

157184
if (delay_us)
@@ -161,7 +188,7 @@ int16_t sensirion_i2c_delayed_read_cmd(uint8_t address, uint16_t cmd,
161188
}
162189

163190
int16_t sensirion_i2c_read_cmd(uint8_t address, uint16_t cmd,
164-
uint16_t *data_words, uint16_t num_words) {
191+
uint16_t* data_words, uint16_t num_words) {
165192
return sensirion_i2c_delayed_read_cmd(address, cmd, 0, data_words,
166193
num_words);
167194
}

0 commit comments

Comments
 (0)