libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
mspm0_gpio.hpp
1#pragma once
2
3#include "dl_gpio.h"
4#include "gpio.hpp"
5
6namespace LibXR
7{
8
9#ifdef GPIOC_BASE
10 constexpr uint8_t MAX_PORTS = 3;
11#elif defined(GPIOB_BASE)
12 constexpr uint8_t MAX_PORTS = 2;
13#else
14 constexpr uint8_t MAX_PORTS = 1;
15#endif
16
17class MSPM0GPIO : public GPIO
18{
19 public:
20 explicit MSPM0GPIO(GPIO_Regs* port, uint32_t pin_mask, uint32_t pincm);
21
22 bool Read() override;
23
24 void Write(bool value) override;
25
26 ErrorCode EnableInterrupt() override;
27
28 ErrorCode DisableInterrupt() override;
29
30 ErrorCode SetConfig(Configuration config) override;
31
32 static inline void OnInterrupt(GPIO_Regs* port)
33 {
34 const int idx = GetPortIndex(reinterpret_cast<uint32_t>(port)); // NOLINT
35 OnInterruptDispatch(port, idx);
36 }
37
38 private:
39 static void OnInterruptDispatch(GPIO_Regs* port, int port_idx);
40
41 static constexpr int GetPortIndex(uint32_t base_addr)
42 {
43 if (base_addr == GPIOA_BASE)
44 {
45 return 0;
46 }
47#ifdef GPIOB_BASE
48 if (base_addr == GPIOB_BASE)
49 {
50 return 1;
51 }
52#endif
53#ifdef GPIOC_BASE
54 if (base_addr == GPIOC_BASE)
55 {
56 return 2;
57 }
58#endif
59 return -1;
60 }
61
62 GPIO_Regs* port_;
63 uint32_t pin_mask_;
64 uint32_t pincm_;
66
67 // 安全的编译时nullptr数组初始化 / Safe compile-time initialization of nullptr array
68 // NOLINTNEXTLINE(bugprone-dynamic-static-initializers)
69 static MSPM0GPIO* instance_map_[MAX_PORTS][32];
70};
71
72} // namespace LibXR
通用输入输出(GPIO)接口类。General Purpose Input/Output (GPIO) interface class.
Definition gpio.hpp:13
Direction
定义 GPIO 引脚的方向类型。Defines the direction types for GPIO pins.
Definition gpio.hpp:20
@ INPUT
输入模式。Input mode.
ErrorCode DisableInterrupt() override
禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
bool Read() override
读取 GPIO 引脚状态。Reads the GPIO pin state.
ErrorCode EnableInterrupt() override
使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
void Write(bool value) override
写入 GPIO 引脚状态。Writes the GPIO pin state.
ErrorCode SetConfig(Configuration config) override
配置 GPIO 引脚参数。Configures the GPIO pin settings.
LibXR 命名空间
Definition ch32_can.hpp:14
存储 GPIO 配置参数的结构体。Structure storing GPIO configuration parameters.
Definition gpio.hpp:46