优化实现串口驱动,SPI驱动 W25QXX还需要初始化验证修复
This commit is contained in:
@ -8,79 +8,75 @@
|
||||
/* USER CODE END Header */
|
||||
|
||||
#include "bsp_init.h"
|
||||
#include "bsp_config.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "bsp_board.h"
|
||||
#include "bsp_board_manager.h"
|
||||
#include "hal.h"
|
||||
#include "hal_gpio.h"
|
||||
#include "hal_uart.h"
|
||||
|
||||
/**
|
||||
* @brief Initialize board support package
|
||||
*/
|
||||
void bsp_init(void) {
|
||||
/* Initialize board GPIO */
|
||||
bsp_gpio_init();
|
||||
/* Get board configuration */
|
||||
const bsp_board_config_t* board_config = bsp_board_get_config();
|
||||
|
||||
/* Initialize HAL layer */
|
||||
hal_init();
|
||||
|
||||
/* Initialize peripherals based on configuration */
|
||||
|
||||
/* Initialize LED */
|
||||
if (board_config->led_init != NULL) {
|
||||
board_config->led_init(&board_config->led);
|
||||
} else {
|
||||
/* Use default initialization if no function provided */
|
||||
hal_gpio_config_t gpio_config = {
|
||||
.port = board_config->led.port,
|
||||
.pin = board_config->led.pin,
|
||||
.mode = board_config->led.mode,
|
||||
.speed = board_config->led.speed,
|
||||
.pull = board_config->led.pull
|
||||
};
|
||||
hal_gpio_configure_pin(&gpio_config);
|
||||
}
|
||||
|
||||
/* Initialize Button */
|
||||
if (board_config->button_init != NULL) {
|
||||
board_config->button_init(&board_config->button);
|
||||
}
|
||||
|
||||
/* Initialize UART */
|
||||
if (board_config->uart_init != NULL) {
|
||||
board_config->uart_init(&board_config->uart);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initialize board GPIO
|
||||
* @brief Initialize board GPIO using configuration
|
||||
*/
|
||||
void bsp_gpio_init(void) {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_TypeDef *gpio_port = NULL;
|
||||
uint32_t gpio_pin = 0;
|
||||
/* Get board configuration */
|
||||
const bsp_board_config_t* board_config = bsp_board_get_config();
|
||||
|
||||
/* Convert HAL GPIO port to STM32 GPIO port */
|
||||
switch (BSP_LED_PORT) {
|
||||
case HAL_GPIO_PORT_A:
|
||||
gpio_port = GPIOA;
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_B:
|
||||
gpio_port = GPIOB;
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_C:
|
||||
gpio_port = GPIOC;
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_D:
|
||||
gpio_port = GPIOD;
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_E:
|
||||
gpio_port = GPIOE;
|
||||
__HAL_RCC_GPIOE_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_F:
|
||||
gpio_port = GPIOF;
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_G:
|
||||
gpio_port = GPIOG;
|
||||
__HAL_RCC_GPIOG_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_H:
|
||||
gpio_port = GPIOH;
|
||||
__HAL_RCC_GPIOH_CLK_ENABLE();
|
||||
break;
|
||||
case HAL_GPIO_PORT_I:
|
||||
gpio_port = GPIOI;
|
||||
__HAL_RCC_GPIOI_CLK_ENABLE();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
/* Initialize LED GPIO */
|
||||
hal_gpio_config_t gpio_config = {
|
||||
.port = board_config->led.port,
|
||||
.pin = board_config->led.pin,
|
||||
.mode = board_config->led.mode,
|
||||
.speed = board_config->led.speed,
|
||||
.pull = board_config->led.pull
|
||||
};
|
||||
hal_gpio_configure_pin(&gpio_config);
|
||||
|
||||
/* Convert HAL GPIO pin to STM32 GPIO pin */
|
||||
gpio_pin = 1 << BSP_LED_PIN;
|
||||
|
||||
if (gpio_port != NULL) {
|
||||
/* Configure GPIO pin Output Level */
|
||||
HAL_GPIO_WritePin(gpio_port, gpio_pin, GPIO_PIN_RESET);
|
||||
|
||||
/* Configure GPIO pin */
|
||||
GPIO_InitStruct.Pin = gpio_pin;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(gpio_port, &GPIO_InitStruct);
|
||||
/* Initialize Button GPIO if configured */
|
||||
if (board_config->button.port != 0 && board_config->button.pin != 0) {
|
||||
gpio_config.port = board_config->button.port;
|
||||
gpio_config.pin = board_config->button.pin;
|
||||
gpio_config.mode = board_config->button.mode;
|
||||
gpio_config.speed = board_config->button.speed;
|
||||
gpio_config.pull = board_config->button.pull;
|
||||
hal_gpio_configure_pin(&gpio_config);
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,5 +85,13 @@ void bsp_gpio_init(void) {
|
||||
* @retval Board name string
|
||||
*/
|
||||
const char* bsp_get_board_name(void) {
|
||||
return BOARD_NAME;
|
||||
return bsp_board_get_name();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get current board configuration
|
||||
* @retval Pointer to board configuration structure
|
||||
*/
|
||||
const bsp_board_config_t* bsp_get_board_config(void) {
|
||||
return bsp_board_get_config();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user