Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
scd4x_i2c.c
00001 /* 00002 * THIS FILE IS AUTOMATICALLY GENERATED AND MUST NOT BE EDITED MANUALLY! 00003 * 00004 * I2C-Generator: 0.2.0 00005 * Yaml Version: 0.1.0 00006 * Template Version: 0.2.1 00007 */ 00008 /* 00009 * Copyright (c) 2021, Sensirion AG 00010 * All rights reserved. 00011 * 00012 * Redistribution and use in source and binary forms, with or without 00013 * modification, are permitted provided that the following conditions are met: 00014 * 00015 * * Redistributions of source code must retain the above copyright notice, this 00016 * list of conditions and the following disclaimer. 00017 * 00018 * * Redistributions in binary form must reproduce the above copyright notice, 00019 * this list of conditions and the following disclaimer in the documentation 00020 * and/or other materials provided with the distribution. 00021 * 00022 * * Neither the name of Sensirion AG nor the names of its 00023 * contributors may be used to endorse or promote products derived from 00024 * this software without specific prior written permission. 00025 * 00026 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00027 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00028 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00029 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00030 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00031 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00032 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00033 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00034 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00035 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00036 * POSSIBILITY OF SUCH DAMAGE. 00037 */ 00038 00039 #include "scd4x_i2c.h" 00040 #include "sensirion_common.h" 00041 #include "sensirion_i2c.h" 00042 #include "sensirion_i2c_hal.h" 00043 00044 #define SCD4X_I2C_ADDRESS 98 00045 00046 int16_t scd4x_start_periodic_measurement() { 00047 int16_t error; 00048 uint8_t buffer[2]; 00049 uint16_t offset = 0; 00050 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x21B1); 00051 00052 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00053 if (error) { 00054 return error; 00055 } 00056 sensirion_i2c_hal_sleep_usec(1000); 00057 return NO_ERROR; 00058 } 00059 00060 int16_t scd4x_read_measurement_ticks(uint16_t* co2, uint16_t* temperature, 00061 uint16_t* humidity) { 00062 int16_t error; 00063 uint8_t buffer[9]; 00064 uint16_t offset = 0; 00065 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xEC05); 00066 00067 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00068 if (error) { 00069 return error; 00070 } 00071 00072 sensirion_i2c_hal_sleep_usec(1000); 00073 00074 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 6); 00075 if (error) { 00076 return error; 00077 } 00078 *co2 = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00079 *temperature = sensirion_common_bytes_to_uint16_t(&buffer[2]); 00080 *humidity = sensirion_common_bytes_to_uint16_t(&buffer[4]); 00081 return NO_ERROR; 00082 } 00083 00084 int16_t scd4x_read_measurement(uint16_t* co2, int32_t* temperature_m_deg_c, 00085 int32_t* humidity_m_percent_rh) { 00086 int16_t error; 00087 uint16_t temperature; 00088 uint16_t humidity; 00089 00090 error = scd4x_read_measurement_ticks(co2, &temperature, &humidity); 00091 if (error) { 00092 return error; 00093 } 00094 *temperature_m_deg_c = ((21875 * (int32_t)temperature) >> 13) - 45000; 00095 *humidity_m_percent_rh = ((12500 * (int32_t)humidity) >> 13); 00096 return NO_ERROR; 00097 } 00098 00099 int16_t scd4x_stop_periodic_measurement() { 00100 int16_t error; 00101 uint8_t buffer[2]; 00102 uint16_t offset = 0; 00103 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3F86); 00104 00105 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00106 if (error) { 00107 return error; 00108 } 00109 sensirion_i2c_hal_sleep_usec(500000); 00110 return NO_ERROR; 00111 } 00112 00113 int16_t scd4x_get_temperature_offset_ticks(uint16_t* t_offset) { 00114 int16_t error; 00115 uint8_t buffer[3]; 00116 uint16_t offset = 0; 00117 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2318); 00118 00119 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00120 if (error) { 00121 return error; 00122 } 00123 00124 sensirion_i2c_hal_sleep_usec(1000); 00125 00126 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2); 00127 if (error) { 00128 return error; 00129 } 00130 *t_offset = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00131 return NO_ERROR; 00132 } 00133 00134 int16_t scd4x_get_temperature_offset(int32_t* t_offset_m_deg_c) { 00135 int16_t error; 00136 uint16_t t_offset; 00137 00138 error = scd4x_get_temperature_offset_ticks(&t_offset); 00139 if (error) { 00140 return error; 00141 } 00142 *t_offset_m_deg_c = ((21875 * (int32_t)t_offset) >> 13); 00143 return NO_ERROR; 00144 } 00145 00146 int16_t scd4x_set_temperature_offset_ticks(uint16_t t_offset) { 00147 int16_t error; 00148 uint8_t buffer[5]; 00149 uint16_t offset = 0; 00150 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x241D); 00151 00152 offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, t_offset); 00153 00154 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00155 if (error) { 00156 return error; 00157 } 00158 sensirion_i2c_hal_sleep_usec(1000); 00159 return NO_ERROR; 00160 } 00161 00162 int16_t scd4x_set_temperature_offset(int32_t t_offset_m_deg_c) { 00163 uint16_t t_offset = (uint16_t)((t_offset_m_deg_c * 12271) >> 15); 00164 return scd4x_set_temperature_offset_ticks(t_offset); 00165 } 00166 00167 int16_t scd4x_get_sensor_altitude(uint16_t* sensor_altitude) { 00168 int16_t error; 00169 uint8_t buffer[3]; 00170 uint16_t offset = 0; 00171 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2322); 00172 00173 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00174 if (error) { 00175 return error; 00176 } 00177 00178 sensirion_i2c_hal_sleep_usec(1000); 00179 00180 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2); 00181 if (error) { 00182 return error; 00183 } 00184 *sensor_altitude = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00185 return NO_ERROR; 00186 } 00187 00188 int16_t scd4x_set_sensor_altitude(uint16_t sensor_altitude) { 00189 int16_t error; 00190 uint8_t buffer[5]; 00191 uint16_t offset = 0; 00192 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2427); 00193 00194 offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, 00195 sensor_altitude); 00196 00197 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00198 if (error) { 00199 return error; 00200 } 00201 sensirion_i2c_hal_sleep_usec(1000); 00202 return NO_ERROR; 00203 } 00204 00205 int16_t scd4x_set_ambient_pressure(uint16_t ambient_pressure) { 00206 int16_t error; 00207 uint8_t buffer[5]; 00208 uint16_t offset = 0; 00209 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xE000); 00210 00211 offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, 00212 ambient_pressure); 00213 00214 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00215 if (error) { 00216 return error; 00217 } 00218 sensirion_i2c_hal_sleep_usec(1000); 00219 return NO_ERROR; 00220 } 00221 00222 int16_t scd4x_perform_forced_recalibration(uint16_t target_co2_concentration, 00223 uint16_t* frc_correction) { 00224 int16_t error; 00225 uint8_t buffer[5]; 00226 uint16_t offset = 0; 00227 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x362F); 00228 00229 offset = sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, 00230 target_co2_concentration); 00231 00232 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00233 if (error) { 00234 return error; 00235 } 00236 00237 sensirion_i2c_hal_sleep_usec(400000); 00238 00239 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2); 00240 if (error) { 00241 return error; 00242 } 00243 *frc_correction = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00244 return NO_ERROR; 00245 } 00246 00247 int16_t scd4x_get_automatic_self_calibration(uint16_t* asc_enabled) { 00248 int16_t error; 00249 uint8_t buffer[3]; 00250 uint16_t offset = 0; 00251 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2313); 00252 00253 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00254 if (error) { 00255 return error; 00256 } 00257 00258 sensirion_i2c_hal_sleep_usec(1000); 00259 00260 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2); 00261 if (error) { 00262 return error; 00263 } 00264 *asc_enabled = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00265 return NO_ERROR; 00266 } 00267 00268 int16_t scd4x_set_automatic_self_calibration(uint16_t asc_enabled) { 00269 int16_t error; 00270 uint8_t buffer[5]; 00271 uint16_t offset = 0; 00272 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2416); 00273 00274 offset = 00275 sensirion_i2c_add_uint16_t_to_buffer(&buffer[0], offset, asc_enabled); 00276 00277 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00278 if (error) { 00279 return error; 00280 } 00281 sensirion_i2c_hal_sleep_usec(1000); 00282 return NO_ERROR; 00283 } 00284 00285 int16_t scd4x_start_low_power_periodic_measurement() { 00286 uint8_t buffer[2]; 00287 uint16_t offset = 0; 00288 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x21AC); 00289 00290 return sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00291 } 00292 00293 int16_t scd4x_get_data_ready_status(uint16_t* data_ready) { 00294 int16_t error; 00295 uint8_t buffer[3]; 00296 uint16_t offset = 0; 00297 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0xE4B8); 00298 00299 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00300 if (error) { 00301 return error; 00302 } 00303 00304 sensirion_i2c_hal_sleep_usec(1000); 00305 00306 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2); 00307 if (error) { 00308 return error; 00309 } 00310 *data_ready = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00311 return NO_ERROR; 00312 } 00313 00314 int16_t scd4x_persist_settings() { 00315 int16_t error; 00316 uint8_t buffer[2]; 00317 uint16_t offset = 0; 00318 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3615); 00319 00320 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00321 if (error) { 00322 return error; 00323 } 00324 sensirion_i2c_hal_sleep_usec(800000); 00325 return NO_ERROR; 00326 } 00327 00328 int16_t scd4x_get_serial_number(uint16_t* serial_0, uint16_t* serial_1, 00329 uint16_t* serial_2) { 00330 int16_t error; 00331 uint8_t buffer[9]; 00332 uint16_t offset = 0; 00333 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3682); 00334 00335 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00336 if (error) { 00337 return error; 00338 } 00339 00340 sensirion_i2c_hal_sleep_usec(1000); 00341 00342 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 6); 00343 if (error) { 00344 return error; 00345 } 00346 *serial_0 = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00347 *serial_1 = sensirion_common_bytes_to_uint16_t(&buffer[2]); 00348 *serial_2 = sensirion_common_bytes_to_uint16_t(&buffer[4]); 00349 return NO_ERROR; 00350 } 00351 00352 int16_t scd4x_perform_self_test(uint16_t* sensor_status) { 00353 int16_t error; 00354 uint8_t buffer[3]; 00355 uint16_t offset = 0; 00356 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3639); 00357 00358 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00359 if (error) { 00360 return error; 00361 } 00362 00363 sensirion_i2c_hal_sleep_usec(10000000); 00364 00365 error = sensirion_i2c_read_data_inplace(SCD4X_I2C_ADDRESS, &buffer[0], 2); 00366 if (error) { 00367 return error; 00368 } 00369 *sensor_status = sensirion_common_bytes_to_uint16_t(&buffer[0]); 00370 return NO_ERROR; 00371 } 00372 00373 int16_t scd4x_perform_factory_reset() { 00374 int16_t error; 00375 uint8_t buffer[2]; 00376 uint16_t offset = 0; 00377 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3632); 00378 00379 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00380 if (error) { 00381 return error; 00382 } 00383 sensirion_i2c_hal_sleep_usec(800000); 00384 return NO_ERROR; 00385 } 00386 00387 int16_t scd4x_reinit() { 00388 int16_t error; 00389 uint8_t buffer[2]; 00390 uint16_t offset = 0; 00391 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x3646); 00392 00393 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00394 if (error) { 00395 return error; 00396 } 00397 sensirion_i2c_hal_sleep_usec(20000); 00398 return NO_ERROR; 00399 } 00400 00401 int16_t scd4x_measure_single_shot() { 00402 int16_t error; 00403 uint8_t buffer[2]; 00404 uint16_t offset = 0; 00405 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x219D); 00406 00407 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00408 if (error) { 00409 return error; 00410 } 00411 sensirion_i2c_hal_sleep_usec(5000000); 00412 return NO_ERROR; 00413 } 00414 00415 int16_t scd4x_measure_single_shot_rht_only() { 00416 int16_t error; 00417 uint8_t buffer[2]; 00418 uint16_t offset = 0; 00419 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x2196); 00420 00421 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00422 if (error) { 00423 return error; 00424 } 00425 sensirion_i2c_hal_sleep_usec(50000); 00426 return NO_ERROR; 00427 } 00428 00429 int16_t scd4x_power_down() { 00430 int16_t error; 00431 uint8_t buffer[2]; 00432 uint16_t offset = 0; 00433 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x36E0); 00434 00435 error = sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00436 if (error) { 00437 return error; 00438 } 00439 sensirion_i2c_hal_sleep_usec(1000); 00440 return NO_ERROR; 00441 } 00442 00443 int16_t scd4x_wake_up() { 00444 uint8_t buffer[2]; 00445 uint16_t offset = 0; 00446 offset = sensirion_i2c_add_command_to_buffer(&buffer[0], offset, 0x36F6); 00447 00448 // Sensor does not acknowledge the wake-up call, error is ignored 00449 (void)sensirion_i2c_write_data(SCD4X_I2C_ADDRESS, &buffer[0], offset); 00450 sensirion_i2c_hal_sleep_usec(20000); 00451 return NO_ERROR; 00452 }
Generated on Sat Aug 6 2022 14:17:29 by
