api-hal-gpio.h 1.9 KB

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