优化实现串口驱动,SPI驱动 W25QXX还需要初始化验证修复
This commit is contained in:
@ -7,54 +7,44 @@
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
#include "hal.h"
|
||||
#include "hal_gpio.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
/**
|
||||
* @brief Convert HAL GPIO port to STM32 GPIO port
|
||||
* @param port: HAL GPIO port
|
||||
* @retval STM32 GPIO port
|
||||
*/
|
||||
static GPIO_TypeDef* hal_gpio_port_to_stm32(hal_gpio_port_t port) {
|
||||
switch (port) {
|
||||
case HAL_GPIO_PORT_A: return GPIOA;
|
||||
case HAL_GPIO_PORT_B: return GPIOB;
|
||||
case HAL_GPIO_PORT_C: return GPIOC;
|
||||
case HAL_GPIO_PORT_D: return GPIOD;
|
||||
case HAL_GPIO_PORT_E: return GPIOE;
|
||||
case HAL_GPIO_PORT_F: return GPIOF;
|
||||
case HAL_GPIO_PORT_G: return GPIOG;
|
||||
case HAL_GPIO_PORT_H: return GPIOH;
|
||||
case HAL_GPIO_PORT_I: return GPIOI;
|
||||
// case HAL_GPIO_PORT_J: return GPIOJ;
|
||||
// case HAL_GPIO_PORT_K: return GPIOK;
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert HAL GPIO pin to STM32 GPIO pin
|
||||
* @param pin: HAL GPIO pin
|
||||
* @retval STM32 GPIO pin
|
||||
*/
|
||||
static uint16_t hal_gpio_pin_to_stm32(hal_gpio_pin_t pin) {
|
||||
return (1U << pin);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Convert HAL GPIO state to STM32 GPIO state
|
||||
* @param state: HAL GPIO state
|
||||
* @retval STM32 GPIO state
|
||||
*/
|
||||
static GPIO_PinState hal_gpio_state_to_stm32(hal_gpio_pin_state_t state) {
|
||||
return (state == HAL_GPIO_PIN_SET) ? GPIO_PIN_SET : GPIO_PIN_RESET;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize GPIO hardware
|
||||
*/
|
||||
void hal_gpio_init(void) {
|
||||
/* GPIO initialization is handled by BSP layer */
|
||||
/* Call architecture specific GPIO initialization */
|
||||
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
|
||||
hal_stm32f1_gpio_init();
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
|
||||
hal_stm32f4_gpio_init();
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
|
||||
hal_stm32f7_gpio_init();
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
|
||||
hal_stm32l4_gpio_init();
|
||||
#else
|
||||
#error "Unsupported HAL architecture"
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Configure GPIO pin
|
||||
* @param config: GPIO configuration structure
|
||||
*/
|
||||
void hal_gpio_configure_pin(const hal_gpio_config_t *config) {
|
||||
/* Call architecture specific GPIO configuration */
|
||||
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
|
||||
hal_stm32f1_gpio_configure_pin(config);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
|
||||
hal_stm32f4_gpio_configure_pin(config);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
|
||||
hal_stm32f7_gpio_configure_pin(config);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
|
||||
hal_stm32l4_gpio_configure_pin(config);
|
||||
#else
|
||||
#error "Unsupported HAL architecture"
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,13 +54,18 @@ void hal_gpio_init(void) {
|
||||
* @param state: GPIO pin state
|
||||
*/
|
||||
void hal_gpio_write_pin(hal_gpio_port_t port, hal_gpio_pin_t pin, hal_gpio_pin_state_t state) {
|
||||
GPIO_TypeDef* stm32_port = hal_gpio_port_to_stm32(port);
|
||||
if (stm32_port == NULL) {
|
||||
return;
|
||||
}
|
||||
uint16_t stm32_pin = hal_gpio_pin_to_stm32(pin);
|
||||
GPIO_PinState stm32_state = hal_gpio_state_to_stm32(state);
|
||||
HAL_GPIO_WritePin(stm32_port, stm32_pin, stm32_state);
|
||||
/* Call architecture specific GPIO write implementation */
|
||||
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
|
||||
hal_stm32f1_gpio_write_pin(port, pin, state);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
|
||||
hal_stm32f4_gpio_write_pin(port, pin, state);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
|
||||
hal_stm32f7_gpio_write_pin(port, pin, state);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
|
||||
hal_stm32l4_gpio_write_pin(port, pin, state);
|
||||
#else
|
||||
#error "Unsupported HAL architecture"
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@ -79,12 +74,18 @@ void hal_gpio_write_pin(hal_gpio_port_t port, hal_gpio_pin_t pin, hal_gpio_pin_s
|
||||
* @param pin: GPIO pin
|
||||
*/
|
||||
void hal_gpio_toggle_pin(hal_gpio_port_t port, hal_gpio_pin_t pin) {
|
||||
GPIO_TypeDef* stm32_port = hal_gpio_port_to_stm32(port);
|
||||
if (stm32_port == NULL) {
|
||||
return;
|
||||
}
|
||||
uint16_t stm32_pin = hal_gpio_pin_to_stm32(pin);
|
||||
HAL_GPIO_TogglePin(stm32_port, stm32_pin);
|
||||
/* Call architecture specific GPIO toggle implementation */
|
||||
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
|
||||
hal_stm32f1_gpio_toggle_pin(port, pin);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
|
||||
hal_stm32f4_gpio_toggle_pin(port, pin);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
|
||||
hal_stm32f7_gpio_toggle_pin(port, pin);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
|
||||
hal_stm32l4_gpio_toggle_pin(port, pin);
|
||||
#else
|
||||
#error "Unsupported HAL architecture"
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,11 +95,17 @@ void hal_gpio_toggle_pin(hal_gpio_port_t port, hal_gpio_pin_t pin) {
|
||||
* @retval GPIO pin state
|
||||
*/
|
||||
hal_gpio_pin_state_t hal_gpio_read_pin(hal_gpio_port_t port, hal_gpio_pin_t pin) {
|
||||
GPIO_TypeDef* stm32_port = hal_gpio_port_to_stm32(port);
|
||||
if (stm32_port == NULL) {
|
||||
return HAL_GPIO_PIN_RESET;
|
||||
}
|
||||
uint16_t stm32_pin = hal_gpio_pin_to_stm32(pin);
|
||||
GPIO_PinState stm32_state = HAL_GPIO_ReadPin(stm32_port, stm32_pin);
|
||||
return (stm32_state == GPIO_PIN_SET) ? HAL_GPIO_PIN_SET : HAL_GPIO_PIN_RESET;
|
||||
/* Call architecture specific GPIO read implementation */
|
||||
#if HAL_TARGET_ARCH == HAL_ARCH_STM32F1
|
||||
return hal_stm32f1_gpio_read_pin(port, pin);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F4
|
||||
return hal_stm32f4_gpio_read_pin(port, pin);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32F7
|
||||
return hal_stm32f7_gpio_read_pin(port, pin);
|
||||
#elif HAL_TARGET_ARCH == HAL_ARCH_STM32L4
|
||||
return hal_stm32l4_gpio_read_pin(port, pin);
|
||||
#else
|
||||
#error "Unsupported HAL architecture"
|
||||
return HAL_GPIO_PIN_RESET;
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user