Odrive_0.5.5运行代码分析_(三)_GPIO详解

这篇博客详细介绍了STM32 GPIO的封装,包括Gpio对象的构造、配置、读写操作。重点分析了`config`函数如何初始化GPIO,`subscribe`函数如何订阅和设置中断,以及`unsubscribe`函数如何取消中断订阅。此外,还提到了`maybe_handle`函数用于处理GPIO中断事件。整个过程展示了STM32 GPIO的底层操作和中断管理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概览

odrive对stm32的gpio和gpio中断进行了自己的封装,故对其进行分析

Gpio对象

class Stm32Gpio {
public:
    static const Stm32Gpio none;

    Stm32Gpio() : port_(nullptr), pin_mask_(0) {}
    Stm32Gpio(GPIO_TypeDef* port, uint16_t pin) : port_(port), pin_mask_(pin) {}

    operator bool() const { return port_ && pin_mask_; }

    /**
     * @brief Configures the GPIO with the specified parameters.
     * 
     * This can be done regardless of the current state of the GPIO.
     * 
     * If any subscription is in place, it is not disabled by this function.
     */
    bool config(uint32_t mode, uint32_t pull, uint32_t speed = GPIO_SPEED_FREQ_LOW);

    void write(bool state) {
        if (port_) {
            HAL_GPIO_WritePin(port_, pin_mask_, state ? GPIO_PIN_SET : GPIO_PIN_RESET);
        }
    }

    bool read() {
        return port_ && (port_->IDR & pin_mask_);
    }

    /**
     * @brief Subscribes to external interrupts on the specified GPIO.
     * 
     * Before calling this function the gpio should most likely be configured as
     * input (however this is not mandatory, the interrupt works in output mode
     * too).
     * Also you need to enable the EXTIx_IRQn interrupt vectors in the NVIC,
     * otherwise the subscription won't have any effect.
     * 
     * Only one subscription is allowed per pin number. I.e. it is not possible
     * to set up a subscription for both PA0 and PB0 at the same time.
     * 
     * This function is thread-safe with respect to all other public functions
     * of this class.
     * 
     * Returns true if the subscription was set up successfully or false otherwise.
     */
    bool subscribe(bool rising_edge, bool falling_edge, void (*callback)(void*), void* ctx);

    /**
     * @brief Unsubscribes from external interrupt on the specified GPIO.
     * 
     * If no subscription was active for this GPIO, calling this function has no
     * effect.
     *
     * This function is thread-safe with respect to all other public functions
     * of this class, however it must not be called from an interrupt routine
     * running at a higher priority than the interrupt that is being unsubscribed.
     *
     * After this function returns the callback given to subscribe() will no
     * longer be invoked.
     */
    void unsubscribe();

    uint16_t get_pin_number() {
        uint16_t pin_number = 0;
        uint16_t pin_mask = pin_mask_ >> 1;
        while (pin_mask) {
            pin_mask >>= 1;
            pin_number++;
        }
        return pin_number;
    }

    GPIO_TypeDef* port_;
    uint16_t pin_mask_; // TODO: store pin_number_ instead of pin_mask_
};

pin_mask_ 用于将gpio的序号改写成便于位操作的形式
eg : pin3 ,pin_mask_ = 0000000000001000
pin0 ,pin_mask_ = 0000000000000001
于是get_pin_number()即为逆向操作

bool read() {
        return port_ && (port_->IDR & pin_mask_);
    }

read函数使用直接读取寄存器的方法
write是直接调用HAL库

config函数

bool Stm32Gpio::config(uint32_t mode, uint32_t pull, uint32_t speed) {
    if (port_ == GPIOA) {
        __HAL_RCC_GPIOA_CLK_ENABLE();
    } else if (port_ == GPIOB) {
        __HAL_RCC_GPIOB_CLK_ENABLE();
    } else if (port_ == GPIOC) {
        __HAL_RCC_GPIOC_CLK_ENABLE();
    } else if (port_ == GPIOD) {
        __HAL_RCC_GPIOD_CLK_ENABLE();
    } else if (port_ == GPIOE) {
        __HAL_RCC_GPIOE_CLK_ENABLE();
    } else if (port_ == GPIOF) {
        __HAL_RCC_GPIOF_CLK_ENABLE();
    } else if (port_ == GPIOG) {
        __HAL_RCC_GPIOG_CLK_ENABLE();
    } else if (port_ == GPIOH) {
        __HAL_RCC_GPIOH_CLK_ENABLE();
    } else {
        return false;
    }

    size_t position = get_pin_number();

    // The following code is mostly taken from HAL_GPIO_Init

    /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
    uint32_t temp = port_->MODER;
    temp &= ~(GPIO_MODER_MODER0 << (position * 2U));
    temp |= ((mode & GPIO_MODE) << (position * 2U));
    port_->MODER = temp;

    /* In case of Output or Alternate function mode selection */
    if((mode == GPIO_MODE_OUTPUT_PP) || (mode == GPIO_MODE_AF_PP) ||
       (mode == GPIO_MODE_OUTPUT_OD) || (mode == GPIO_MODE_AF_OD))
    {
        /* Check the Speed parameter */
        assert_param(IS_GPIO_SPEED(speed));
        /* Configure the IO Speed */
        temp = port_->OSPEEDR; 
        temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U));
        temp |= (speed << (position * 2U));
        port_->OSPEEDR = temp;

        /* Configure the IO Output Type */
        temp = port_->OTYPER;
        temp &= ~(GPIO_OTYPER_OT_0 << position) ;
        temp |= (((mode & GPIO_OUTPUT_TYPE) >> 4U) << position);
        port_->OTYPER = temp;
    }

    /* Activate the Pull-up or Pull down resistor for the current IO */
    temp = port_->PUPDR;
    temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U));
    temp |= ((pull) << (position * 2U));
    port_->PUPDR = temp;

    return true;
}

  1. 打开时钟
  2. 获取pin_number
  3. 后面进行gpio的初始化(官方注释:基本从HAL库复制)

subscribe函数

bool Stm32Gpio::subscribe(bool rising_edge, bool falling_edge, void (*callback)(void*), void* ctx) {
    uint32_t pin_number = get_pin_number();
    if (pin_number >= N_EXTI) {
        return false; // invalid pin number
    }

    struct subscription_t& subscription = subscriptions[pin_number];

    GPIO_TypeDef* no_port = nullptr;
    if (!__atomic_compare_exchange_n(&subscription.port, &no_port, port_, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
        return false; // already in use
    }

    // The following code is mostly taken from HAL_GPIO_Init
    __HAL_RCC_SYSCFG_CLK_ENABLE();

    uint32_t temp = SYSCFG->EXTICR[pin_number >> 2U];
    temp &= ~(0x0FU << (4U * (pin_number & 0x03U)));
    temp |= ((uint32_t)(GPIO_GET_INDEX(port_)) << (4U * (pin_number & 0x03U)));
    SYSCFG->EXTICR[pin_number >> 2U] = temp;


    if (rising_edge) {
        EXTI->RTSR |= (uint32_t)pin_mask_;
    } else {
        EXTI->RTSR &= ~((uint32_t)pin_mask_);
    }

    if (falling_edge) {
        EXTI->FTSR |= (uint32_t)pin_mask_;
    } else {
        EXTI->FTSR &= ~((uint32_t)pin_mask_);
    }

    EXTI->EMR &= ~((uint32_t)pin_mask_);
    EXTI->IMR |= (uint32_t)pin_mask_;

    // Clear any previous triggers
    __HAL_GPIO_EXTI_CLEAR_IT(pin_mask_);
    
    subscription.ctx = ctx;
    subscription.callback = callback;
    return true;
}

该函数是配置相关gpio的中断的它没有进行gpio input或output的配置,也没有开中断
同时还有一个结构体数组保存相应的中断线信息


#define N_EXTI 16

struct subscription_t {
    GPIO_TypeDef* port = nullptr;
    void (*callback)(void*) = nullptr;
    void* ctx = nullptr;
} subscriptions[N_EXTI];

未知ctx是干啥的,上下文?求教 ctx作为输入参数传入calllback

unsubscribe函数

即取消gpio的中断线和deinit subscriptions中的配置

杂项函数

void maybe_handle(uint16_t exti_number) {
    if(__HAL_GPIO_EXTI_GET_IT(1 << exti_number) == RESET) {
        return; // This interrupt source did not trigger the interrupt line
    }

    __HAL_GPIO_EXTI_CLEAR_IT(1 << exti_number);
    
    if (exti_number >= N_EXTI) {
        return;
    }

    subscription_t& subscription = subscriptions[exti_number];
    if (subscription.callback) {
        (*subscription.callback)(subscription.ctx);
    }
}

这是gpio的中断线处理程序,首先判断是不是这个gpio的中断请求,然后清标志位,防溢出,调用callback,ctx作为参数传入

main.cpp中的gpio相关

main.hpp中还有个函数

static Stm32Gpio get_gpio(size_t gpio_num) {
    return (gpio_num < GPIO_COUNT) ? gpios[gpio_num] : GPIO_COUNT ? gpios[0] : Stm32Gpio::none;
}

它将gpio_num转换成Stm32Gpio 而有个Stm32Gpio gpios[]保存着每个gpio的参数,它被存在board.c文件中
本文完,To be countinue…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值