I2CSensor.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "I2CSensor.h"
  16. static uint8_t sensors_count = 0;
  17. uint8_t unitemp_i2c_readReg(I2CSensor* i2c_sensor, uint8_t reg) {
  18. //Блокировка шины
  19. furi_hal_i2c_acquire(i2c_sensor->i2c);
  20. uint8_t buff[1] = {0};
  21. furi_hal_i2c_read_mem(i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, reg, buff, 1, 10);
  22. furi_hal_i2c_release(i2c_sensor->i2c);
  23. return buff[0];
  24. }
  25. bool unitemp_i2c_readRegArray(I2CSensor* i2c_sensor, uint8_t startReg, uint8_t len, uint8_t* data) {
  26. furi_hal_i2c_acquire(i2c_sensor->i2c);
  27. bool status = furi_hal_i2c_read_mem(
  28. i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, startReg, data, len, 10);
  29. furi_hal_i2c_release(i2c_sensor->i2c);
  30. return status;
  31. }
  32. bool unitemp_i2c_writeReg(I2CSensor* i2c_sensor, uint8_t reg, uint8_t value) {
  33. //Блокировка шины
  34. furi_hal_i2c_acquire(i2c_sensor->i2c);
  35. uint8_t buff[1] = {value};
  36. bool status =
  37. furi_hal_i2c_write_mem(i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, reg, buff, 1, 10);
  38. furi_hal_i2c_release(i2c_sensor->i2c);
  39. return status;
  40. }
  41. bool unitemp_I2C_sensor_alloc(Sensor* sensor, char* args) {
  42. bool status = false;
  43. I2CSensor* instance = malloc(sizeof(I2CSensor));
  44. if(instance == NULL) {
  45. FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name);
  46. return false;
  47. }
  48. instance->i2c = &furi_hal_i2c_handle_external;
  49. sensor->instance = instance;
  50. //Указание функций инициализации, деинициализации и обновления данных, а так же адреса на шине I2C
  51. status = sensor->type->allocator(sensor, args);
  52. int i2c_addr;
  53. sscanf(args, "%X", &i2c_addr);
  54. //Установка адреса шины I2C
  55. if(i2c_addr >= instance->minI2CAdr && i2c_addr <= instance->maxI2CAdr) {
  56. instance->currentI2CAdr = i2c_addr;
  57. } else {
  58. instance->currentI2CAdr = instance->minI2CAdr;
  59. }
  60. //Блокировка портов GPIO
  61. sensors_count++;
  62. unitemp_gpio_lock(unitemp_gpio_getFromInt(15), &I2C);
  63. unitemp_gpio_lock(unitemp_gpio_getFromInt(16), &I2C);
  64. return status;
  65. }
  66. bool unitemp_I2C_sensor_free(Sensor* sensor) {
  67. bool status = sensor->type->mem_releaser(sensor);
  68. free(sensor->instance);
  69. if(--sensors_count == 0) {
  70. unitemp_gpio_unlock(unitemp_gpio_getFromInt(15));
  71. unitemp_gpio_unlock(unitemp_gpio_getFromInt(16));
  72. }
  73. return status;
  74. }
  75. UnitempStatus unitemp_I2C_sensor_update(Sensor* sensor) {
  76. if(sensor->status != UT_SENSORSTATUS_OK) {
  77. sensor->type->initializer(sensor);
  78. }
  79. return sensor->type->updater(sensor);
  80. }