2024-06-12 08:49:23 +00:00
|
|
|
#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()
|
|
|
|
{
|
2024-06-13 08:01:47 +00:00
|
|
|
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOE_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOF_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOG_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOH_CLK_ENABLE();
|
2024-06-12 08:49:23 +00:00
|
|
|
// 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
|
2024-06-13 08:01:47 +00:00
|
|
|
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);
|
2024-06-12 08:49:23 +00:00
|
|
|
|
2024-06-13 08:01:47 +00:00
|
|
|
// gpio_output_set(GPIOA, GPIO_PIN_5);
|
2024-06-12 08:49:23 +00:00
|
|
|
}
|
|
|
|
// 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]);
|
2024-06-13 08:01:47 +00:00
|
|
|
#include "debug.h"
|
|
|
|
// Printf("speed:%d,%d,%d,%d,%d\n", speed[0], speed[1], speed[2], speed[3], speed[4]);
|
2024-06-12 08:49:23 +00:00
|
|
|
}
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-13 08:01:47 +00:00
|
|
|
static volatile int duty[4] = {90, 60, 40, 10};
|
|
|
|
static volatile int now = 0;
|
2024-06-12 08:49:23 +00:00
|
|
|
#define PWM_OUT(duty, now, io_deal) \
|
2024-06-13 08:01:47 +00:00
|
|
|
if (duty <= now) \
|
2024-06-12 08:49:23 +00:00
|
|
|
{ \
|
2024-06-13 08:01:47 +00:00
|
|
|
io_deal = 0; \
|
2024-06-12 08:49:23 +00:00
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
2024-06-13 08:01:47 +00:00
|
|
|
io_deal = 1; \
|
2024-06-12 08:49:23 +00:00
|
|
|
}
|
|
|
|
void gpio_pwm_output()
|
|
|
|
{
|
2024-06-13 08:01:47 +00:00
|
|
|
static char cnt = 0;
|
|
|
|
if (cnt < 10)
|
|
|
|
{
|
|
|
|
cnt++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cnt = 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 == 100)
|
|
|
|
{
|
|
|
|
now = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void duty_set(unsigned char ch, unsigned char _duty)
|
|
|
|
{
|
|
|
|
if (ch >= sizeof(duty))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
duty[ch] = _duty;
|
2024-06-12 08:49:23 +00:00
|
|
|
}
|
2024-06-13 08:01:47 +00:00
|
|
|
void soft_gpio_function()
|
|
|
|
{
|
|
|
|
gpio_pwm_output();
|
|
|
|
gpio_scan();
|
|
|
|
}
|