api-hal-gpio.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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(GpioPin* gpio, GpioMode mode, GpioPull pull, GpioSpeed speed);
  37. // write value to GPIO, false = LOW, true = HIGH
  38. static inline void hal_gpio_write(GpioPin* gpio, bool state) {
  39. // writing to BSSR is an atomic operation
  40. if(state == true) {
  41. gpio->port->BSRR = gpio->pin;
  42. } else {
  43. gpio->port->BSRR = (uint32_t)gpio->pin << GPIO_NUMBER;
  44. }
  45. }
  46. // read value from GPIO, false = LOW, true = HIGH
  47. static inline bool hal_gpio_read(const GpioPin* gpio) {
  48. if((gpio->port->IDR & gpio->pin) != 0x00U) {
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. }
  54. bool hal_gpio_read_sd_detect(void);