SPISensor.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. unitemp_gpio_lock(instance->CS_pin, &SPI);
  47. return status;
  48. }
  49. bool unitemp_spi_sensor_free(Sensor* sensor) {
  50. bool status = sensor->type->mem_releaser(sensor);
  51. unitemp_gpio_unlock(((SPISensor*)sensor->instance)->CS_pin);
  52. free(((SPISensor*)(sensor->instance))->spi);
  53. free(sensor->instance);
  54. if(--sensors_count == 0) {
  55. unitemp_gpio_unlock(unitemp_gpio_getFromInt(2));
  56. unitemp_gpio_unlock(unitemp_gpio_getFromInt(3));
  57. unitemp_gpio_unlock(unitemp_gpio_getFromInt(5));
  58. }
  59. return status;
  60. }
  61. bool unitemp_spi_sensor_init(Sensor* sensor) {
  62. return sensor->type->initializer(sensor);
  63. }
  64. bool unitemp_spi_sensor_deinit(Sensor* sensor) {
  65. UNUSED(sensor);
  66. return true;
  67. }
  68. UnitempStatus unitemp_spi_sensor_update(Sensor* sensor) {
  69. return sensor->type->updater(sensor);
  70. }