SCD40.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n)
  4. Contributed by divinebird (https://github.com/divinebird)
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. // Some information may be seen on https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library
  17. #include "SCD40.h"
  18. #include "../interfaces/I2CSensor.h"
  19. #include "../interfaces/endianness.h"
  20. //#include <3rdparty/everest/include/everest/kremlin/c_endianness.h>
  21. const SensorType SCD40 = {
  22. .typename = "SCD40",
  23. .interface = &I2C,
  24. .datatype = UT_DATA_TYPE_TEMP_HUM_CO2,
  25. .pollingInterval = 5000,
  26. .allocator = unitemp_SCD40_alloc,
  27. .mem_releaser = unitemp_SCD40_free,
  28. .initializer = unitemp_SCD40_init,
  29. .deinitializer = unitemp_SCD40_deinit,
  30. .updater = unitemp_SCD40_update};
  31. #define SCD40_ID 0x62
  32. #define COMMAND_START_PERIODIC_MEASUREMENT 0X21B1
  33. #define COMMAND_READ_MEASUREMENT 0XEC05
  34. #define COMMAND_STOP_PERIODIC_MEASUREMENT 0X3F86
  35. #define COMMAND_PERSIST_SETTINGS 0X3615
  36. #define COMMAND_GET_SERIAL_NUMBER 0X3682
  37. #define COMMAND_PERFORM_SELF_TEST 0X3639
  38. #define COMMAND_PERFORM_FACTORY_RESET 0X3632
  39. #define COMMAND_REINIT 0X3646
  40. #define COMMAND_SET_TEMPERATURE_OFFSET 0X241D
  41. #define COMMAND_GET_TEMPERATURE_OFFSET 0X2318
  42. #define COMMAND_SET_SENSOR_ALTITUDE 0X2427
  43. #define COMMAND_GET_SENSOR_ALTITUDE 0X2322
  44. #define COMMAND_SET_AMBIENT_PRESSURE 0XE000
  45. #define COMMAND_PERFORM_FORCED_RECALIBRATION 0X362F
  46. #define COMMAND_SET_AUTOMATIC_SELF_CALIBRATION_ENABLED 0X2416
  47. #define COMMAND_GET_AUTOMATIC_SELF_CALIBRATION_ENABLED 0X2313
  48. static bool readMeasurement(Sensor* sensor) __attribute__((unused));
  49. static void reset(Sensor* sensor) __attribute__((unused));
  50. static bool setAutoSelfCalibration(Sensor* sensor, bool enable) __attribute__((unused));
  51. static bool getAutoSelfCalibration(Sensor* sensor) __attribute__((unused));
  52. static bool getFirmwareVersion(Sensor* sensor, uint16_t* val) __attribute__((unused));
  53. static float getTemperatureOffset(Sensor* sensor) __attribute__((unused));
  54. static bool setTemperatureOffset(Sensor* sensor, float tempOffset) __attribute__((unused));
  55. static bool beginMeasuring(Sensor* sensor) __attribute__((unused));
  56. static bool stopMeasurement(Sensor* sensor) __attribute__((unused));
  57. bool unitemp_SCD40_alloc(Sensor* sensor, char* args) {
  58. UNUSED(args);
  59. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  60. i2c_sensor->minI2CAdr = SCD40_ID << 1;
  61. i2c_sensor->maxI2CAdr = SCD40_ID << 1;
  62. return true;
  63. }
  64. bool unitemp_SCD40_free(Sensor* sensor) {
  65. //Нечего высвобождать, так как ничего не было выделено
  66. UNUSED(sensor);
  67. return true;
  68. }
  69. bool unitemp_SCD40_init(Sensor* sensor) {
  70. return beginMeasuring(sensor);
  71. }
  72. bool unitemp_SCD40_deinit(Sensor* sensor) {
  73. return stopMeasurement(sensor);
  74. }
  75. UnitempStatus unitemp_SCD40_update(Sensor* sensor) {
  76. readMeasurement(sensor);
  77. return UT_SENSORSTATUS_OK;
  78. }
  79. #define CRC8_POLYNOMIAL 0x31
  80. #define CRC8_INIT 0xFF
  81. static uint8_t computeCRC8(uint8_t* message, uint8_t len) {
  82. uint8_t crc = CRC8_INIT; // Init with 0xFF
  83. for(uint8_t x = 0; x < len; x++) {
  84. crc ^= message[x]; // XOR-in the next input byte
  85. for(uint8_t i = 0; i < 8; i++) {
  86. if((crc & 0x80) != 0)
  87. crc = (uint8_t)((crc << 1) ^ CRC8_POLYNOMIAL);
  88. else
  89. crc <<= 1;
  90. }
  91. }
  92. return crc; // No output reflection
  93. }
  94. // Sends a command along with arguments and CRC
  95. static bool sendCommandWithCRC(Sensor* sensor, uint16_t command, uint16_t arguments) {
  96. static const uint8_t cmdSize = 5;
  97. uint8_t bytes[cmdSize];
  98. uint8_t* pointer = bytes;
  99. store16_be(pointer, command);
  100. pointer += 2;
  101. uint8_t* argPos = pointer;
  102. store16_be(pointer, arguments);
  103. pointer += 2;
  104. *pointer = computeCRC8(argPos, pointer - argPos);
  105. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  106. return unitemp_i2c_writeArray(i2c_sensor, cmdSize, bytes);
  107. }
  108. // Sends just a command, no arguments, no CRC
  109. static bool sendCommand(Sensor* sensor, uint16_t command) {
  110. static const uint8_t cmdSize = 2;
  111. uint8_t bytes[cmdSize];
  112. store16_be(bytes, command);
  113. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  114. return unitemp_i2c_writeArray(i2c_sensor, cmdSize, bytes);
  115. }
  116. static uint16_t readRegister(Sensor* sensor, uint16_t registerAddress) {
  117. static const uint8_t regSize = 2;
  118. if(!sendCommand(sensor, registerAddress)) return 0; // Sensor did not ACK
  119. furi_delay_ms(3);
  120. uint8_t bytes[regSize];
  121. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  122. if(!unitemp_i2c_readArray(i2c_sensor, regSize, bytes)) return 0;
  123. return load16_be(bytes);
  124. }
  125. static bool loadWord(uint8_t* buff, uint16_t* val) {
  126. uint16_t tmp = load16_be(buff);
  127. uint8_t expectedCRC = computeCRC8(buff, 2);
  128. if(buff[2] != expectedCRC) return false;
  129. *val = tmp;
  130. return true;
  131. }
  132. static bool getSettingValue(Sensor* sensor, uint16_t registerAddress, uint16_t* val) {
  133. static const uint8_t respSize = 3;
  134. if(!sendCommand(sensor, registerAddress)) return false; // Sensor did not ACK
  135. furi_delay_ms(3);
  136. uint8_t bytes[respSize];
  137. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  138. if(!unitemp_i2c_readArray(i2c_sensor, respSize, bytes)) return false;
  139. return loadWord(bytes, val);
  140. }
  141. // Get 18 bytes from SCD40
  142. // Updates global variables with floats
  143. // Returns true if success
  144. static bool readMeasurement(Sensor* sensor) {
  145. if(!sendCommand(sensor, COMMAND_READ_MEASUREMENT)) {
  146. FURI_LOG_E(APP_NAME, "Sensor did not ACK");
  147. return false; // Sensor did not ACK
  148. }
  149. furi_delay_ms(3);
  150. static const uint8_t respSize = 9;
  151. uint8_t buff[respSize];
  152. uint8_t* bytes = buff;
  153. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  154. if(!unitemp_i2c_readArray(i2c_sensor, respSize, bytes)) {
  155. FURI_LOG_E(APP_NAME, "Error while read measures");
  156. return false;
  157. }
  158. uint16_t tmpValue;
  159. bool error = false;
  160. if(loadWord(bytes, &tmpValue)) {
  161. sensor->co2 = tmpValue;
  162. } else {
  163. FURI_LOG_E(APP_NAME, "Error while parsing CO2");
  164. error = true;
  165. }
  166. bytes += 3;
  167. if(loadWord(bytes, &tmpValue)) {
  168. sensor->temp = (float)tmpValue * 175.0f / 65535.0f - 45.0f;
  169. } else {
  170. FURI_LOG_E(APP_NAME, "Error while parsing temp");
  171. error = true;
  172. }
  173. bytes += 3;
  174. if(loadWord(bytes, &tmpValue)) {
  175. sensor->hum = (float)tmpValue * 100.0f / 65535.0f;
  176. } else {
  177. FURI_LOG_E(APP_NAME, "Error while parsing humidity");
  178. error = true;
  179. }
  180. return !error;
  181. }
  182. static void reset(Sensor* sensor) {
  183. sendCommand(sensor, COMMAND_REINIT);
  184. }
  185. static bool setAutoSelfCalibration(Sensor* sensor, bool enable) {
  186. return sendCommandWithCRC(
  187. sensor, COMMAND_SET_AUTOMATIC_SELF_CALIBRATION_ENABLED, enable); // Activate continuous ASC
  188. }
  189. // Get the current ASC setting
  190. static bool getAutoSelfCalibration(Sensor* sensor) {
  191. return 1 == readRegister(sensor, COMMAND_GET_AUTOMATIC_SELF_CALIBRATION_ENABLED);
  192. }
  193. // Unfinished
  194. static bool getFirmwareVersion(Sensor* sensor, uint16_t* val) {
  195. if(!sendCommand(sensor, COMMAND_READ_MEASUREMENT)) {
  196. FURI_LOG_E(APP_NAME, "Sensor did not ACK");
  197. return false; // Sensor did not ACK
  198. }
  199. static const uint8_t respSize = 9;
  200. uint8_t buff[respSize];
  201. uint8_t* bytes = buff;
  202. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  203. if(!unitemp_i2c_readArray(i2c_sensor, respSize, bytes)) {
  204. FURI_LOG_E(APP_NAME, "Error while read measures");
  205. return false;
  206. }
  207. *val = 0;
  208. return true;
  209. }
  210. static bool beginMeasuring(Sensor* sensor) {
  211. return sendCommand(sensor, COMMAND_START_PERIODIC_MEASUREMENT);
  212. }
  213. // Stop continuous measurement
  214. static bool stopMeasurement(Sensor* sensor) {
  215. return sendCommand(sensor, COMMAND_READ_MEASUREMENT);
  216. }
  217. static float getTemperatureOffset(Sensor* sensor) {
  218. uint16_t curOffset;
  219. if(!getSettingValue(sensor, COMMAND_GET_TEMPERATURE_OFFSET, &curOffset)) return 0.0;
  220. return (float)curOffset * 175.0f / 65536.0f;
  221. }
  222. static bool setTemperatureOffset(Sensor* sensor, float tempOffset) {
  223. uint16_t newOffset = tempOffset * 65536.0 / 175.0 + 0.5f;
  224. return sendCommandWithCRC(
  225. sensor, COMMAND_SET_TEMPERATURE_OFFSET, newOffset); // Activate continuous ASC
  226. }