This commit is contained in:
chenyf 2024-06-12 16:49:23 +08:00
parent 2e4983dc0b
commit 23c061a6e9
13 changed files with 54508 additions and 41 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1 @@
[]

2
.vscode/launch.json vendored
View File

@ -18,7 +18,7 @@
// "svdFile": "${workspaceFolder}/STM32F407.svd",
"cwd": "${workspaceFolder}",
"executable": "${workspaceFolder}/build/f407-2.elf",
"svdFile": "${workspaceFolder}/STM32F40x.svd",
}

View File

@ -63,7 +63,7 @@
/* #define HAL_SD_MODULE_ENABLED */
/* #define HAL_MMC_MODULE_ENABLED */
/* #define HAL_SPI_MODULE_ENABLED */
/* #define HAL_TIM_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
#define HAL_UART_MODULE_ENABLED
/* #define HAL_USART_MODULE_ENABLED */
/* #define HAL_IRDA_MODULE_ENABLED */

View File

@ -55,6 +55,7 @@ void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void TIM2_IRQHandler(void);
/* USER CODE BEGIN EFP */
/* USER CODE END EFP */

55
Core/Src/gpio/bit_band.h Normal file
View File

@ -0,0 +1,55 @@
#include "stm32f4xx.h"
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414
#define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
#define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010
#define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
#define PHout(n) BIT_ADDR(GPIOH_ODR_Addr,n) //输出
#define PHin(n) BIT_ADDR(GPIOH_IDR_Addr,n) //输入
#define PIout(n) BIT_ADDR(GPIOI_ODR_Addr,n) //输出
#define PIin(n) BIT_ADDR(GPIOI_IDR_Addr,n) //输入

145
Core/Src/gpio/gpio_scan.c Normal file
View File

@ -0,0 +1,145 @@
#include <stdio.h>
#include "bit_band.h"
#include "stm32f4xx_hal.h"
#include "stm32f407xx.h"
void gpio_input_set(GPIO_TypeDef *gpio, uint16_t pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(gpio, &GPIO_InitStruct);
}
void gpio_output_set(GPIO_TypeDef *gpio, uint16_t pin)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(gpio, &GPIO_InitStruct);
}
void gpio_init()
{
// fan read
gpio_input_set(GPIOD, GPIO_PIN_4);
gpio_input_set(GPIOD, GPIO_PIN_5);
gpio_input_set(GPIOD, GPIO_PIN_6);
gpio_input_set(GPIOD, GPIO_PIN_7);
gpio_input_set(GPIOB, GPIO_PIN_4);
gpio_input_set(GPIOB, GPIO_PIN_3);
// pwm output
// gpio_output_set(GPIOC, GPIO_PIN_6);
// gpio_output_set(GPIOC, GPIO_PIN_7);
// gpio_output_set(GPIOC, GPIO_PIN_8);
// gpio_output_set(GPIOC, GPIO_PIN_9);
gpio_output_set(GPIOA, GPIO_PIN_5);
}
// void start_time()
// {
// PAout(5) = 1;
// }
// void stop_time()
// {
// PAout(5) = 0;
// }
static volatile char old[6];
static volatile int cnt[6];
static volatile int speed[6];
#define GPIO_DETECT(state, reg, cnt) \
if (state) \
{ \
if (reg == 0) \
{ \
reg = 1; \
cnt++; \
} \
} \
else \
{ \
if (reg == 1) \
{ \
reg = 0; \
} \
}
#define SAVE_SPEED(speed, cnt) \
{ \
speed = cnt; \
cnt = 0; \
}
void gpio_save_speed()
{
SAVE_SPEED(speed[0], cnt[0]);
SAVE_SPEED(speed[1], cnt[1]);
SAVE_SPEED(speed[2], cnt[2]);
SAVE_SPEED(speed[3], cnt[3]);
SAVE_SPEED(speed[4], cnt[4]);
SAVE_SPEED(speed[5], cnt[5]);
}
void gpio_scan()
{
char state = PDin(4);
GPIO_DETECT(state, old[0], cnt[0]);
state = PDin(5);
GPIO_DETECT(state, old[1], cnt[1]);
state = PDin(6);
GPIO_DETECT(state, old[2], cnt[2]);
state = PDin(7);
GPIO_DETECT(state, old[3], cnt[3]);
state = PBin(4);
GPIO_DETECT(state, old[4], cnt[4]);
state = PBin(3);
GPIO_DETECT(state, old[5], cnt[5]);
static int save = 0;
if (save > 1000)
{
gpio_save_speed();
save = 0;
}
else
{
save++;
}
}
static char duty[4] = {90, 60, 40, 10};
static char now = 0;
#define PWM_OUT(duty, now, io_deal) \
if (duty < now) \
{ \
io_deal = 1; \
} \
else \
{ \
io_deal = 0; \
}
void gpio_pwm_output()
{
// static char cnt = 0;
// if (cnt < 10)
// {
// cnt++;
// return;
// }
// cnt = 0;
// PAout(5) = 1;
// PCout(6) = 1;
// PCout(7) = 1;
// HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6,1);
// PCout(8) = 1;
// PCout(9) = 1;
// PAout(5) = 0;
// PWM_OUT(duty[0], now, PCout(6));
// PWM_OUT(duty[1], now, PCout(7));
// PWM_OUT(duty[2], now, PCout(8));
// PWM_OUT(duty[3], now, PCout(9));
// now++;
// if (now == 99)
// {
// now = 0;
// }
}

View File

@ -1,20 +1,20 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
@ -40,6 +40,8 @@
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
@ -50,6 +52,7 @@ UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM2_Init(void);
/* USER CODE BEGIN PFP */
void putChar(char c)
{
@ -92,8 +95,10 @@ int main(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
extern void gpio_init();
gpio_init();
/* USER CODE END 2 */
/* Infinite loop */
@ -153,6 +158,51 @@ void SystemClock_Config(void)
}
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 84-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1000-1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
/**
* @brief USART1 Initialization Function
* @param None
@ -199,7 +249,6 @@ static void MX_GPIO_Init(void)
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */

View File

@ -77,6 +77,56 @@ void HAL_MspInit(void)
/* USER CODE END MspInit 1 */
}
/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspInit 0 */
/* USER CODE END TIM2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM2_CLK_ENABLE();
/* TIM2 interrupt Init */
HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM2_IRQn);
/* USER CODE BEGIN TIM2_MspInit 1 */
/* USER CODE END TIM2_MspInit 1 */
}
}
/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspDeInit 0 */
/* USER CODE END TIM2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM2_CLK_DISABLE();
/* TIM2 interrupt DeInit */
HAL_NVIC_DisableIRQ(TIM2_IRQn);
/* USER CODE BEGIN TIM2_MspDeInit 1 */
/* USER CODE END TIM2_MspDeInit 1 */
}
}
/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example

View File

@ -55,7 +55,7 @@
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim2;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
@ -198,6 +198,24 @@ void SysTick_Handler(void)
/* please refer to the startup file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles TIM2 global interrupt.
*/
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
extern void gpio_scan();
gpio_scan();
extern void gpio_pwm_output();
gpio_pwm_output();
/* USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Wed Jun 12 16:18:59 CST 2024]
# File automatically-generated by tool: [projectgenerator] version: [4.3.0-B58] date: [Wed Jun 12 16:44:57 CST 2024]
##########################################################################################################################
# ------------------------------------------------
@ -57,8 +57,9 @@ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c \
Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c \
Core/Src/system_stm32f4xx.c \
Core/Src/sysmem.c \
Core/Src/syscalls.c \
Core/Src/debug.c
Core/Src/syscalls.c \
Core/Src/debug.c \
Core/Src/gpio/gpio_scan.c
# ASM sources
ASM_SOURCES = \
@ -122,7 +123,8 @@ C_INCLUDES = \
-IDrivers/STM32F4xx_HAL_Driver/Inc \
-IDrivers/STM32F4xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F4xx/Include \
-IDrivers/CMSIS/Include
-IDrivers/CMSIS/Include \
-ICore/Src/gpio
# compile gcc flags

54141
STM32F40x.svd Normal file

File diff suppressed because it is too large Load Diff

View File

@ -10,8 +10,9 @@ Mcu.Family=STM32F4
Mcu.IP0=NVIC
Mcu.IP1=RCC
Mcu.IP2=SYS
Mcu.IP3=USART1
Mcu.IPNb=4
Mcu.IP3=TIM2
Mcu.IP4=USART1
Mcu.IPNb=5
Mcu.Name=STM32F407Z(E-G)Tx
Mcu.Package=LQFP144
Mcu.Pin0=PH0-OSC_IN
@ -20,8 +21,8 @@ Mcu.Pin2=PA9
Mcu.Pin3=PA10
Mcu.Pin4=PA13
Mcu.Pin5=PA14
Mcu.Pin6=PB3
Mcu.Pin7=VP_SYS_VS_Systick
Mcu.Pin6=VP_SYS_VS_Systick
Mcu.Pin7=VP_TIM2_VS_ClockSourceINT
Mcu.PinsNb=8
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
@ -38,19 +39,18 @@ NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
NVIC.SysTick_IRQn=true\:15\:0\:false\:false\:true\:false\:true\:false
NVIC.TIM2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false
PA10.Locked=true
PA10.Mode=Asynchronous
PA10.Signal=USART1_RX
PA13.Mode=Trace_Asynchronous_SW
PA13.Mode=Serial_Wire
PA13.Signal=SYS_JTMS-SWDIO
PA14.Mode=Trace_Asynchronous_SW
PA14.Mode=Serial_Wire
PA14.Signal=SYS_JTCK-SWCLK
PA9.Locked=true
PA9.Mode=Asynchronous
PA9.Signal=USART1_TX
PB3.Mode=Trace_Asynchronous_SW
PB3.Signal=SYS_JTDO-SWO
PH0-OSC_IN.Mode=HSE-External-Clock-Source
PH0-OSC_IN.Signal=RCC_OSC_IN
PH1-OSC_OUT.Mode=HSE-External-Clock-Source
@ -119,8 +119,13 @@ RCC.VCOI2SOutputFreq_Value=384000000
RCC.VCOInputFreq_Value=2000000
RCC.VCOOutputFreq_Value=336000000
RCC.VcooutputI2S=192000000
TIM2.IPParameters=Prescaler,Period
TIM2.Period=1000-1
TIM2.Prescaler=84-1
USART1.IPParameters=VirtualMode
USART1.VirtualMode=VM_ASYNC
VP_SYS_VS_Systick.Mode=SysTick
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
VP_TIM2_VS_ClockSourceINT.Mode=Internal
VP_TIM2_VS_ClockSourceINT.Signal=TIM2_VS_ClockSourceINT
board=custom