api-hal-gpio.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include "main.h"
  3. #include "stdbool.h"
  4. // this defined in xx_hal_gpio.c, so...
  5. #define GPIO_NUMBER (16U)
  6. typedef enum {
  7. GpioModeInput = GPIO_MODE_INPUT,
  8. GpioModeOutputPushPull = GPIO_MODE_OUTPUT_PP,
  9. GpioModeOutputOpenDrain = GPIO_MODE_OUTPUT_OD,
  10. GpioModeAltFunctionPushPull = GPIO_MODE_AF_PP,
  11. GpioModeAltFunctionOpenDrain = GPIO_MODE_AF_OD,
  12. GpioModeAnalog = GPIO_MODE_ANALOG,
  13. GpioModeInterruptRise = GPIO_MODE_IT_RISING,
  14. GpioModeInterruptFall = GPIO_MODE_IT_FALLING,
  15. GpioModeInterruptRiseFall = GPIO_MODE_IT_RISING_FALLING,
  16. GpioModeEventRise = GPIO_MODE_EVT_RISING,
  17. GpioModeEventFall = GPIO_MODE_EVT_FALLING,
  18. GpioModeEventRiseFall = GPIO_MODE_EVT_RISING_FALLING,
  19. } GpioMode;
  20. typedef enum {
  21. GpioSpeedLow = GPIO_SPEED_FREQ_LOW,
  22. GpioSpeedMedium = GPIO_SPEED_FREQ_MEDIUM,
  23. GpioSpeedHigh = GPIO_SPEED_FREQ_HIGH,
  24. GpioSpeedVeryHigh = GPIO_SPEED_FREQ_VERY_HIGH,
  25. } GpioSpeed;
  26. typedef enum {
  27. GpioPullNo = GPIO_NOPULL,
  28. GpioPullUp = GPIO_PULLUP,
  29. GpioPullDown = GPIO_PULLDOWN,
  30. } GpioPull;
  31. typedef struct {
  32. GPIO_TypeDef* port;
  33. uint16_t pin;
  34. } GpioPin;
  35. // init GPIO
  36. void hal_gpio_init(
  37. const GpioPin* gpio,
  38. const GpioMode mode,
  39. const GpioPull pull,
  40. const GpioSpeed speed);
  41. // write value to GPIO, false = LOW, true = HIGH
  42. static inline void hal_gpio_write(const GpioPin* gpio, const bool state) {
  43. // writing to BSSR is an atomic operation
  44. if(state == true) {
  45. gpio->port->BSRR = gpio->pin;
  46. } else {
  47. gpio->port->BSRR = (uint32_t)gpio->pin << GPIO_NUMBER;
  48. }
  49. }
  50. // read value from GPIO, false = LOW, true = HIGH
  51. static inline bool hal_gpio_read(const GpioPin* gpio) {
  52. if((gpio->port->IDR & gpio->pin) != 0x00U) {
  53. return true;
  54. } else {
  55. return false;
  56. }
  57. }
  58. bool hal_gpio_read_sd_detect(void);
  59. void enable_cc1101_irq();