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.
106 lines
2.6 KiB
106 lines
2.6 KiB
|
|
#pragma once
|
|
#include <functional>
|
|
|
|
#include "sdk/basic/base.hpp"
|
|
|
|
namespace iflytop {
|
|
using namespace std;
|
|
|
|
#define STM32_GPIO_LISTENER_NUM 10
|
|
class ZGPIO {
|
|
public:
|
|
typedef enum {
|
|
kRisingIrqEvent,
|
|
kFallingIrqEvent,
|
|
} IrqTypeEvent_t;
|
|
|
|
typedef enum {
|
|
kMode_nopull, //
|
|
kMode_pullup, //
|
|
kMode_pulldown, //
|
|
kMode_od, //
|
|
} GPIOMode_t;
|
|
|
|
typedef enum { kType_AIN, kType_Input, kType_Output } GPIOType_t;
|
|
|
|
typedef enum {
|
|
kIRQ_noIrq,
|
|
kIRQ_risingIrq,
|
|
kIRQ_fallingIrq,
|
|
kIRQ_risingAndFallingIrq,
|
|
} GPIOIrqType_t;
|
|
|
|
typedef function<void(ZGPIO *GPIO_Pin, IrqTypeEvent_t irqevent)> onirq_t;
|
|
|
|
typedef struct {
|
|
Pin_t pin;
|
|
GPIOMode_t mode;
|
|
GPIOIrqType_t irqtype;
|
|
bool mirror;
|
|
} InputGpioCfg_t;
|
|
|
|
typedef struct {
|
|
Pin_t pin;
|
|
GPIOMode_t mode;
|
|
bool mirror;
|
|
bool initLevel;
|
|
bool log_when_setstate;
|
|
} OutputGpioCfg_t;
|
|
|
|
private:
|
|
Pin_t m_pin = PinNull;
|
|
GPIO_TypeDef *m_gpio;
|
|
uint16_t m_pinoff;
|
|
GPIOType_t m_gpiotype;
|
|
GPIOMode_t m_mode;
|
|
GPIOIrqType_t m_irqtype;
|
|
bool m_mirror;
|
|
bool lastLevel;
|
|
bool m_log_when_setstate = false;
|
|
|
|
onirq_t m_onirq;
|
|
bool m_initflag;
|
|
|
|
public:
|
|
ZGPIO() {};
|
|
|
|
static void initAsOutputStatic(Pin_t pin, GPIOMode_t mode, bool mirror, bool initLevel);
|
|
|
|
void initAsInput(Pin_t pin, GPIOMode_t mode, GPIOIrqType_t irqtype, bool mirror);
|
|
void initAsOutput(Pin_t pin, GPIOMode_t mode, bool mirror, bool initLevel);
|
|
void initAsOutput(OutputGpioCfg_t *outputcfg) {
|
|
m_log_when_setstate = outputcfg->log_when_setstate;
|
|
initAsOutput(outputcfg->pin, outputcfg->mode, outputcfg->mirror, outputcfg->initLevel);
|
|
}
|
|
void initAsInput(InputGpioCfg_t *inputcfg) { initAsInput(inputcfg->pin, inputcfg->mode, inputcfg->irqtype, inputcfg->mirror); }
|
|
void enableTrace(bool enable) { m_log_when_setstate = enable; }
|
|
void regListener(onirq_t listener);
|
|
|
|
bool isMirror();
|
|
bool isItRisingEXITGPIO();
|
|
bool isItFallingEXITGPIO();
|
|
bool isItRisingAndItFallingEXITGPIO();
|
|
|
|
bool getState();
|
|
uint32_t getStateUint32();
|
|
bool setState(bool state);
|
|
void toggleState();
|
|
|
|
bool isNull() { return m_pin == PinNull; }
|
|
|
|
Pin_t getPin() { return m_pin; }
|
|
|
|
IRQn_Type getEXTIIRQn();
|
|
GPIO_TypeDef *getHalPinPort() { return m_gpio; }
|
|
uint16_t getHalPin() { return m_pinoff; }
|
|
|
|
bool tryTriggerIRQ(uint16_t GPIO_Pin);
|
|
|
|
bool isInit() { return m_initflag; }
|
|
|
|
private:
|
|
bool enableClock();
|
|
static bool enableClock(GPIO_TypeDef *port);
|
|
};
|
|
} // namespace iflytop
|