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.

102 lines
2.4 KiB

1 year ago
  1. #pragma once
  2. #include <functional>
  3. #include "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. void initAsInput(Pin_t pin, GPIOMode_t mode, GPIOIrqType_t irqtype, bool mirror);
  55. void initAsOutput(Pin_t pin, GPIOMode_t mode, bool mirror, bool initLevel);
  56. void initAsOutput(OutputGpioCfg_t *outputcfg) {
  57. m_log_when_setstate = outputcfg->log_when_setstate;
  58. initAsOutput(outputcfg->pin, outputcfg->mode, outputcfg->mirror, outputcfg->initLevel);
  59. }
  60. void initAsInput(InputGpioCfg_t *inputcfg) { initAsInput(inputcfg->pin, inputcfg->mode, inputcfg->irqtype, inputcfg->mirror); }
  61. void enableTrace(bool enable) { m_log_when_setstate = enable; }
  62. void regListener(onirq_t listener);
  63. bool isMirror();
  64. bool isItRisingEXITGPIO();
  65. bool isItFallingEXITGPIO();
  66. bool isItRisingAndItFallingEXITGPIO();
  67. bool getState();
  68. uint32_t getStateUint32();
  69. bool setState(bool state);
  70. void toggleState();
  71. bool isNull() { return m_pin == PinNull; }
  72. Pin_t getPin() { return m_pin; }
  73. IRQn_Type getEXTIIRQn();
  74. GPIO_TypeDef *getHalPinPort() { return m_gpio; }
  75. uint16_t getHalPin() { return m_pinoff; }
  76. bool tryTriggerIRQ(uint16_t GPIO_Pin);
  77. bool isInit() { return m_initflag; }
  78. private:
  79. bool enableClock();
  80. };
  81. } // namespace iflytop