api-gpio.c 1.4 KB

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