I2CSensor.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "I2CSensor.h"
  2. #include "../sensors/SensorsDriver.h"
  3. uint8_t readReg(I2CSensor* i2c_sensor, uint8_t reg) {
  4. //Блокировка шины
  5. furi_hal_i2c_acquire(i2c_sensor->i2c);
  6. uint8_t buff[1];
  7. furi_hal_i2c_read_mem(i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, reg, buff, 1, 0xFF);
  8. furi_hal_i2c_release(i2c_sensor->i2c);
  9. return buff[0];
  10. }
  11. bool readRegArray(I2CSensor* i2c_sensor, uint8_t startReg, uint8_t len, uint8_t* data) {
  12. furi_hal_i2c_acquire(i2c_sensor->i2c);
  13. bool status = furi_hal_i2c_read_mem(
  14. i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, startReg, data, len, 0xFF);
  15. furi_hal_i2c_release(i2c_sensor->i2c);
  16. return status;
  17. }
  18. bool writeReg(I2CSensor* i2c_sensor, uint8_t reg, uint8_t value) {
  19. //Блокировка шины
  20. furi_hal_i2c_acquire(i2c_sensor->i2c);
  21. uint8_t buff[1] = {value};
  22. bool status = furi_hal_i2c_write_mem(
  23. i2c_sensor->i2c, i2c_sensor->currentI2CAdr << 1, reg, buff, 1, 0xFF);
  24. furi_hal_i2c_release(i2c_sensor->i2c);
  25. return status;
  26. }
  27. bool unitemp_I2C_sensorInit(void* s) {
  28. Sensor* sensor = (Sensor*)s;
  29. I2CSensor* i2c_sensor = (I2CSensor*)sensor->instance;
  30. //BMP280
  31. if(sensor->type == BMP280) {
  32. if(BMP280_init(i2c_sensor)) {
  33. sensor->status = UT_OK;
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. bool unitemp_I2C_sensorDeInit(void* sensor) {
  40. //TODO датчик в спячку, очистить память
  41. UNUSED(sensor);
  42. return true;
  43. }
  44. UnitempStatus unitemp_I2C_updateData(void* sensor) {
  45. if(((Sensor*)sensor)->status == UT_ERROR || ((Sensor*)sensor)->status == UT_TIMEOUT) {
  46. if(((Sensor*)sensor)->initializer(sensor) != true) return UT_ERROR;
  47. }
  48. BMP280_updateData(sensor);
  49. return UT_OK;
  50. }
  51. bool unitemp_I2C_sensorAlloc(Sensor* sensor, SensorType st, uint16_t* anotherValues) {
  52. bool status = false;
  53. I2CSensor* instance = malloc(sizeof(I2CSensor));
  54. instance->interface = I2C;
  55. instance->i2c = &furi_hal_i2c_handle_external;
  56. sensor->lastPollingTime = 0xFFFFFFFF;
  57. sensor->instance = instance;
  58. sensor->type = st;
  59. // //Настройки для BMP280
  60. // if(st == BMP280) {
  61. // instance->minI2CAdr = 0x76;
  62. // instance->maxI2CAdr = 0x77;
  63. // }
  64. if(st == LM75) {
  65. //Указание функций инициализации, деинициализации и обновления данных, а так же адреса на шине I2C
  66. status = unitemp_LM75_alloc(sensor);
  67. }
  68. if(anotherValues[0] >= instance->minI2CAdr && anotherValues[0] <= instance->maxI2CAdr) {
  69. instance->currentI2CAdr = anotherValues[0];
  70. } else {
  71. instance->currentI2CAdr = instance->minI2CAdr;
  72. }
  73. return status;
  74. }