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
129156int16_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
136163int16_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
146173int16_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
163190int16_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