You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.6 KiB

1 year ago
2 months ago
1 year ago
1 year ago
1 year ago
  1. #pragma once
  2. #include <functional>
  3. #include "sdk/basic/base.hpp"
  4. namespace iflytop {
  5. using namespace std;
  6. #define STM32_GPIO_LISTENER_NUM 10
  7. class ZGPIO {
  8. public:
  9. typedef enum {
  10. kRisingIrqEvent,
  11. kFallingIrqEvent,
  12. } IrqTypeEvent_t;
  13. typedef enum {
  14. kMode_nopull, //
  15. kMode_pullup, //
  16. kMode_pulldown, //
  17. kMode_od, //
  18. } GPIOMode_t;
  19. typedef enum { kType_AIN, kType_Input, kType_Output } GPIOType_t;
  20. typedef enum {
  21. kIRQ_noIrq,
  22. kIRQ_risingIrq,
  23. kIRQ_fallingIrq,
  24. kIRQ_risingAndFallingIrq,
  25. } GPIOIrqType_t;
  26. typedef function<void(ZGPIO *GPIO_Pin, IrqTypeEvent_t irqevent)> onirq_t;
  27. typedef struct {
  28. Pin_t pin;
  29. GPIOMode_t mode;
  30. GPIOIrqType_t irqtype;
  31. bool mirror;
  32. } InputGpioCfg_t;
  33. typedef struct {
  34. Pin_t pin;
  35. GPIOMode_t mode;
  36. bool mirror;
  37. bool initLevel;
  38. bool log_when_setstate;
  39. } OutputGpioCfg_t;
  40. private:
  41. Pin_t m_pin = PinNull;
  42. GPIO_TypeDef *m_gpio;
  43. uint16_t m_pinoff;
  44. GPIOType_t m_gpiotype;
  45. GPIOMode_t m_mode;
  46. GPIOIrqType_t m_irqtype;
  47. bool m_mirror;
  48. bool lastLevel;
  49. bool m_log_when_setstate = false;
  50. onirq_t m_onirq;
  51. bool m_initflag;
  52. public:
  53. ZGPIO() {};
  54. static void initAsOutputStatic(Pin_t pin, GPIOMode_t mode, bool mirror, bool initLevel);
  55. void initAsInput(Pin_t pin, GPIOMode_t mode, GPIOIrqType_t irqtype, bool mirror);
  56. void initAsOutput(Pin_t pin, GPIOMode_t mode, bool mirror, bool initLevel);
  57. void initAsOutput(OutputGpioCfg_t *outputcfg) {
  58. m_log_when_setstate = outputcfg->log_when_setstate;
  59. initAsOutput(outputcfg->pin, outputcfg->mode, outputcfg->mirror, outputcfg->initLevel);
  60. }
  61. void initAsInput(InputGpioCfg_t *inputcfg) { initAsInput(inputcfg->pin, inputcfg->mode, inputcfg->irqtype, inputcfg->mirror); }
  62. void enableTrace(bool enable) { m_log_when_setstate = enable; }
  63. void regListener(onirq_t listener);
  64. bool isMirror();
  65. bool isItRisingEXITGPIO();
  66. bool isItFallingEXITGPIO();
  67. bool isItRisingAndItFallingEXITGPIO();
  68. bool getState();
  69. uint32_t getStateUint32();
  70. bool setState(bool state);
  71. void toggleState();
  72. bool isNull() { return m_pin == PinNull; }
  73. Pin_t getPin() { return m_pin; }
  74. IRQn_Type getEXTIIRQn();
  75. GPIO_TypeDef *getHalPinPort() { return m_gpio; }
  76. uint16_t getHalPin() { return m_pinoff; }
  77. bool tryTriggerIRQ(uint16_t GPIO_Pin);
  78. bool isInit() { return m_initflag; }
  79. private:
  80. bool enableClock();
  81. static bool enableClock(GPIO_TypeDef *port);
  82. };
  83. } // namespace iflytop