api-hal-gpio.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma once
  2. #include "main.h"
  3. #include "stdbool.h"
  4. #include <stm32wbxx_ll_gpio.h>
  5. #include <stm32wbxx_ll_system.h>
  6. #include <stm32wbxx_ll_exti.h>
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /**
  11. * Number of gpio on one port
  12. */
  13. #define GPIO_NUMBER (16U)
  14. /**
  15. * Interrupt callback prototype
  16. */
  17. typedef void (*GpioExtiCallback)(void* ctx);
  18. /**
  19. * Gpio interrupt type
  20. */
  21. typedef struct {
  22. GpioExtiCallback callback;
  23. void *context;
  24. volatile bool ready;
  25. } GpioInterrupt;
  26. /**
  27. * Gpio modes
  28. */
  29. typedef enum {
  30. GpioModeInput,
  31. GpioModeOutputPushPull,
  32. GpioModeOutputOpenDrain,
  33. GpioModeAltFunctionPushPull,
  34. GpioModeAltFunctionOpenDrain,
  35. GpioModeAnalog,
  36. GpioModeInterruptRise,
  37. GpioModeInterruptFall,
  38. GpioModeInterruptRiseFall,
  39. GpioModeEventRise,
  40. GpioModeEventFall,
  41. GpioModeEventRiseFall,
  42. } GpioMode;
  43. /**
  44. * Gpio pull modes
  45. */
  46. typedef enum {
  47. GpioPullNo,
  48. GpioPullUp,
  49. GpioPullDown,
  50. } GpioPull;
  51. /**
  52. * Gpio speed modes
  53. */
  54. typedef enum {
  55. GpioSpeedLow,
  56. GpioSpeedMedium,
  57. GpioSpeedHigh,
  58. GpioSpeedVeryHigh,
  59. } GpioSpeed;
  60. /**
  61. * Gpio structure
  62. */
  63. typedef struct {
  64. GPIO_TypeDef* port;
  65. uint16_t pin;
  66. } GpioPin;
  67. /**
  68. * GPIO initialization function
  69. * @param gpio GpioPin
  70. * @param mode GpioMode
  71. * @param pull GpioPull
  72. * @param speed GpioSpeed
  73. */
  74. void hal_gpio_init(
  75. const GpioPin* gpio,
  76. const GpioMode mode,
  77. const GpioPull pull,
  78. const GpioSpeed speed);
  79. /**
  80. * Add and enable interrupt
  81. * @param gpio GpioPin
  82. * @param cb GpioExtiCallback
  83. * @param ctx context for callback
  84. */
  85. void hal_gpio_add_int_callback(const GpioPin* gpio, GpioExtiCallback cb, void* ctx);
  86. /**
  87. * Enable interrupt
  88. * @param gpio GpioPin
  89. */
  90. void hal_gpio_enable_int_callback(const GpioPin* gpio);
  91. /**
  92. * Disable interrupt
  93. * @param gpio GpioPin
  94. */
  95. void hal_gpio_disable_int_callback(const GpioPin* gpio);
  96. /**
  97. * Remove interrupt
  98. * @param gpio GpioPin
  99. */
  100. void hal_gpio_remove_int_callback(const GpioPin* gpio);
  101. /**
  102. * GPIO write pin
  103. * @param gpio GpioPin
  104. * @param state true / false
  105. */
  106. static inline void hal_gpio_write(const GpioPin* gpio, const bool state) {
  107. // writing to BSSR is an atomic operation
  108. if(state == true) {
  109. gpio->port->BSRR = gpio->pin;
  110. } else {
  111. gpio->port->BSRR = (uint32_t)gpio->pin << GPIO_NUMBER;
  112. }
  113. }
  114. /**
  115. * GPIO read pin
  116. * @param gpio GpioPin
  117. * @return true / false
  118. */
  119. static inline bool hal_gpio_read(const GpioPin* gpio) {
  120. if((gpio->port->IDR & gpio->pin) != 0x00U) {
  121. return true;
  122. } else {
  123. return false;
  124. }
  125. }
  126. /**
  127. * Get RFID IN level
  128. * @return false = LOW, true = HIGH
  129. */
  130. bool hal_gpio_get_rfid_in_level();
  131. #ifdef __cplusplus
  132. }
  133. #endif