SPISensor.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 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 <furi.h>
  16. #include <furi_hal.h>
  17. #include "SPISensor.h"
  18. static uint8_t sensors_count = 0;
  19. bool unitemp_spi_sensor_alloc(Sensor* sensor, char* args) {
  20. if(args == NULL) return false;
  21. //Создание инстанса датчика SPI
  22. SPISensor* instance = malloc(sizeof(SPISensor));
  23. if(instance == NULL) {
  24. FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name);
  25. return false;
  26. }
  27. sensor->instance = instance;
  28. //Определение GPIO chip select
  29. int gpio = 255;
  30. sscanf(args, "%d", &gpio);
  31. instance->CS_pin = unitemp_gpio_getFromInt(gpio);
  32. if(instance->CS_pin == NULL) {
  33. FURI_LOG_E(APP_NAME, "Sensor %s GPIO setting error", sensor->name);
  34. free(instance);
  35. return false;
  36. }
  37. instance->spi = malloc(sizeof(FuriHalSpiBusHandle));
  38. memcpy(instance->spi, &furi_hal_spi_bus_handle_external, sizeof(FuriHalSpiBusHandle));
  39. instance->spi->cs = instance->CS_pin->pin;
  40. bool status = sensor->type->allocator(sensor, args);
  41. //Блокировка портов GPIO
  42. sensors_count++;
  43. unitemp_gpio_lock(unitemp_gpio_getFromInt(2), &SPI);
  44. unitemp_gpio_lock(unitemp_gpio_getFromInt(3), &SPI);
  45. unitemp_gpio_lock(unitemp_gpio_getFromInt(5), &SPI);
  46. return status;
  47. }
  48. bool unitemp_spi_sensor_free(Sensor* sensor) {
  49. bool status = sensor->type->mem_releaser(sensor);
  50. free(((SPISensor*)(sensor->instance))->spi);
  51. free(sensor->instance);
  52. if(--sensors_count == 0) {
  53. unitemp_gpio_unlock(unitemp_gpio_getFromInt(2));
  54. unitemp_gpio_unlock(unitemp_gpio_getFromInt(3));
  55. unitemp_gpio_unlock(unitemp_gpio_getFromInt(5));
  56. }
  57. return status;
  58. }
  59. bool unitemp_spi_sensor_init(Sensor* sensor) {
  60. return sensor->type->initializer(sensor);
  61. }
  62. bool unitemp_spi_sensor_deinit(Sensor* sensor) {
  63. UNUSED(sensor);
  64. return true;
  65. }
  66. UnitempStatus unitemp_spi_sensor_update(Sensor* sensor) {
  67. return sensor->type->updater(sensor);
  68. }