SPISensor.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. SPISensor* instance = malloc(sizeof(SPISensor));
  22. if(instance == NULL) {
  23. FURI_LOG_E(APP_NAME, "Sensor %s instance allocation error", sensor->name);
  24. return false;
  25. }
  26. sensor->instance = instance;
  27. int gpio = 255;
  28. sscanf(args, "%d", &gpio);
  29. instance->CS_pin = unitemp_gpio_getFromInt(gpio);
  30. instance->spi = &furi_hal_spi_bus_handle_external;
  31. if(instance->CS_pin == NULL) {
  32. FURI_LOG_E(APP_NAME, "Sensor %s GPIO setting error", sensor->name);
  33. free(instance);
  34. return false;
  35. }
  36. bool status = sensor->type->allocator(sensor, args);
  37. //Блокировка портов GPIO
  38. sensors_count++;
  39. unitemp_gpio_lock(unitemp_gpio_getFromInt(2), &SPI);
  40. unitemp_gpio_lock(unitemp_gpio_getFromInt(3), &SPI);
  41. unitemp_gpio_lock(unitemp_gpio_getFromInt(5), &SPI);
  42. return status;
  43. }
  44. bool unitemp_spi_sensor_free(Sensor* sensor) {
  45. bool status = sensor->type->mem_releaser(sensor);
  46. free(sensor->instance);
  47. if(--sensors_count == 0) {
  48. unitemp_gpio_unlock(unitemp_gpio_getFromInt(2));
  49. unitemp_gpio_unlock(unitemp_gpio_getFromInt(3));
  50. unitemp_gpio_unlock(unitemp_gpio_getFromInt(5));
  51. }
  52. return status;
  53. }
  54. bool unitemp_spi_sensor_init(Sensor* sensor) {
  55. return sensor->type->initializer(sensor);
  56. }
  57. bool unitemp_spi_sensor_deinit(Sensor* sensor) {
  58. UNUSED(sensor);
  59. return true;
  60. }
  61. UnitempStatus unitemp_spi_sensor_update(Sensor* sensor) {
  62. return sensor->type->updater(sensor);
  63. }