libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stm32_gpio.cpp
1#include "stm32_gpio.hpp"
2
3#ifdef HAL_GPIO_MODULE_ENABLED
4
5using namespace LibXR;
6
7STM32GPIO* STM32GPIO::map[16] = {nullptr};
8
9// NOLINTNEXTLINE
10static inline uint8_t STM32_GPIO_PinToLine(uint16_t pin)
11{
12 ASSERT(pin != 0 && (pin & (pin - 1)) == 0);
13 const uint8_t LINE = static_cast<uint8_t>(__builtin_ctz(static_cast<unsigned>(pin)));
14 return LINE;
15}
16
17STM32GPIO::STM32GPIO(GPIO_TypeDef* port, uint16_t pin, IRQn_Type irq)
18 : port_(port), pin_(pin), irq_(irq)
19{
20 if (irq_ != NonMaskableInt_IRQn)
21 {
22 map[STM32_GPIO_PinToLine(pin)] = this;
23 }
24}
25
27{
28 ASSERT(irq_ != NonMaskableInt_IRQn);
29 HAL_NVIC_EnableIRQ(irq_);
30 return ErrorCode::OK;
31}
32
34{
35 ASSERT(irq_ != NonMaskableInt_IRQn);
36 HAL_NVIC_DisableIRQ(irq_);
37 return ErrorCode::OK;
38}
39
40extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
41{
42 const uint8_t LINE = STM32_GPIO_PinToLine(GPIO_Pin);
43 if (auto* gpio = STM32GPIO::map[LINE])
44 {
45 gpio->callback_.Run(true);
46 }
47}
48
49#endif
STM32 GPIO 驱动实现 / STM32 GPIO driver implementation.
STM32GPIO(GPIO_TypeDef *port, uint16_t pin, IRQn_Type irq=NonMaskableInt_IRQn)
构造 GPIO 对象 / Construct GPIO object
ErrorCode DisableInterrupt()
禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
ErrorCode EnableInterrupt()
使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
LibXR 命名空间
Definition ch32_can.hpp:14