MR16/User/device/sx1281_driver/sx1281_driver_gpio.c
2025-12-12 20:42:40 +08:00

122 lines
3.6 KiB
C

#include "device/sx1281_driver/sx1281_header.h"
static GpioIrqHandler *GpioIrq[16] = { NULL };
/*!
* @brief Records the interrupt handler for the GPIO object
*
* @param GPIOx: where x can be (A..E and H)
* @param GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* All port bits are not necessarily available on all GPIOs.
* @param [IN] prio NVIC priority (0 is highest)
* @param [IN] irqHandler points to the function to execute
* @retval none
*/
void GpioSetIrq( GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint32_t prio, GpioIrqHandler *irqHandler )
{
IRQn_Type IRQnb;
uint32_t BitPos = GpioGetBitPos( GPIO_Pin ) ;
if ( irqHandler != NULL )
{
GpioIrq[BitPos] = irqHandler;
IRQnb = MSP_GetIRQn( GPIO_Pin );
HAL_NVIC_SetPriority( IRQnb , prio, 0 );
HAL_NVIC_EnableIRQ( IRQnb );
}
}
/*!
* @brief Execute the interrupt from the object
*
* @param GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* All port bits are not necessarily available on all GPIOs.
* @retval none
*/
void GpioLaunchIrqHandler( uint16_t GPIO_Pin )
{
uint32_t BitPos = GpioGetBitPos( GPIO_Pin );
if ( GpioIrq[BitPos] != NULL )
{
GpioIrq[BitPos]( );
}
}
/*!
* @brief Get the position of the bit set in the GPIO_Pin
* @param GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* All port bits are not necessarily available on all GPIOs.
* @retval the position of the bit
*/
uint8_t GpioGetBitPos( uint16_t GPIO_Pin )
{
uint8_t PinPos=0;
if ( ( GPIO_Pin & 0xFF00 ) != 0 ) { PinPos |= 0x8; }
if ( ( GPIO_Pin & 0xF0F0 ) != 0 ) { PinPos |= 0x4; }
if ( ( GPIO_Pin & 0xCCCC ) != 0 ) { PinPos |= 0x2; }
if ( ( GPIO_Pin & 0xAAAA ) != 0 ) { PinPos |= 0x1; }
return PinPos;
}
/*!
* @brief Writes the given value to the GPIO output
*
* @param GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* All port bits are not necessarily available on all GPIOs.
* @param [IN] value New GPIO output value
* @retval none
*/
void GpioWrite( GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint32_t value )
{
HAL_GPIO_WritePin( GPIOx, GPIO_Pin , ( GPIO_PinState ) value );
}
/*!
* @brief Reads the current GPIO input value
*
* @param GPIOx: where x can be (A..E and H)
* @param GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* All port bits are not necessarily available on all GPIOs.
* @retval value Current GPIO input value
*/
uint32_t GpioRead( GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin )
{
return HAL_GPIO_ReadPin( GPIOx, GPIO_Pin );
}
/**
* @brief 根据 GPIO 引脚号返回对应的 EXTI 中断向量号
* @param gpioPin : GPIO_PIN_x (x=0..15)
* @retval IRQn_Type 中断号
*/
IRQn_Type MSP_GetIRQn(uint16_t gpioPin)
{
uint32_t pinPos = GpioGetBitPos(gpioPin); /* 0..15 */
/* STM32L4 / F4 / F1 等通用系列 EXTI 线 0~15 对应中断向量 */
if (pinPos <= 1) return EXTI0_IRQn;
if (pinPos <= 3) return EXTI1_IRQn;
if (pinPos <= 5) return EXTI2_IRQn;
if (pinPos <= 7) return EXTI3_IRQn;
if (pinPos <= 9) return EXTI4_IRQn;
if (pinPos <= 15) return EXTI9_5_IRQn; /* 5~9 共用 */
/* 10~15 共用 */
return EXTI15_10_IRQn;
}