api-gpio.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "api-gpio.h"
  2. #include <cmsis_os2.h>
  3. #include <furi/record.h>
  4. osMutexId_t gpioInitMutex;
  5. bool gpio_api_init(void) {
  6. gpioInitMutex = osMutexNew(NULL);
  7. if(gpioInitMutex == NULL) return false;
  8. return true;
  9. }
  10. // init GPIO
  11. void gpio_init(const GpioPin* gpio, const GpioMode mode) {
  12. if(osMutexAcquire(gpioInitMutex, osWaitForever) == osOK) {
  13. hal_gpio_init(gpio, mode, GpioPullNo, GpioSpeedLow);
  14. osMutexRelease(gpioInitMutex);
  15. }
  16. }
  17. // init GPIO, extended version
  18. void gpio_init_ex(
  19. const GpioPin* gpio,
  20. const GpioMode mode,
  21. const GpioPull pull,
  22. const GpioSpeed speed) {
  23. hal_gpio_init(gpio, mode, pull, speed);
  24. }
  25. // put GPIO to Z-state
  26. void gpio_disable(GpioDisableRecord* gpio_record) {
  27. const GpioPin* gpio_pin = acquire_mutex(gpio_record->gpio_mutex, 0);
  28. if(gpio_pin == NULL) {
  29. gpio_pin = gpio_record->gpio;
  30. }
  31. hal_gpio_init(gpio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  32. release_mutex(gpio_record->gpio_mutex, gpio_pin);
  33. }
  34. // get GPIO record
  35. ValueMutex* gpio_open_mutex(const char* name) {
  36. ValueMutex* gpio_mutex = (ValueMutex*)furi_record_open(name);
  37. // TODO disable gpio on app exit
  38. //if(gpio_mutex != NULL) flapp_on_exit(gpio_disable, gpio_mutex);
  39. return gpio_mutex;
  40. }
  41. // get GPIO record and acquire mutex
  42. GpioPin* gpio_open(const char* name) {
  43. ValueMutex* gpio_mutex = gpio_open_mutex(name);
  44. GpioPin* gpio_pin = acquire_mutex(gpio_mutex, osWaitForever);
  45. return gpio_pin;
  46. }