实现电机,按键,虚拟串口,未验证
This commit is contained in:
@@ -2,10 +2,12 @@
|
||||
#include "debug.h"
|
||||
#include "lock_resource.h"
|
||||
|
||||
// 使用定时器14
|
||||
// 使用定时器14,13
|
||||
|
||||
// PF7,8,9 是输出口
|
||||
// 使用PF7来产生脉冲,PF8来改变方向
|
||||
// PI6 是输入口
|
||||
// 使用PI6来捕获到位中断
|
||||
|
||||
typedef struct{
|
||||
int tick;
|
||||
@@ -20,6 +22,7 @@ typedef struct{
|
||||
typedef struct{
|
||||
TIM_HandleTypeDef htim;
|
||||
TIM_HandleTypeDef htim2;
|
||||
EXTI_HandleTypeDef hexti;
|
||||
int count_all;
|
||||
int count_past;
|
||||
int fre;
|
||||
@@ -32,6 +35,7 @@ typedef struct{
|
||||
|
||||
static self_def g_self;
|
||||
|
||||
static void Exti14FallingCb(void);
|
||||
|
||||
static int init(pwm_def *p)
|
||||
{
|
||||
@@ -67,13 +71,37 @@ static int init(pwm_def *p)
|
||||
GPIO_InitTypeDef init={0};
|
||||
__HAL_RCC_GPIOF_CLK_ENABLE();
|
||||
|
||||
init.Mode = GPIO_MODE_INPUT;
|
||||
init.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
init.Pull = GPIO_NOPULL;
|
||||
init.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
|
||||
PERIPH_LOCK(GPIOF);
|
||||
HAL_GPIO_Init(GPIOF, &init);
|
||||
PERIPH_UNLOCK(GPIOF);
|
||||
|
||||
__HAL_RCC_GPIOI_CLK_ENABLE();
|
||||
init.Mode = GPIO_MODE_INPUT;
|
||||
init.Pull = GPIO_NOPULL;
|
||||
init.Pin = GPIO_PIN_6;
|
||||
PERIPH_LOCK(GPIOI);
|
||||
HAL_GPIO_Init(GPIOI, &init);
|
||||
PERIPH_UNLOCK(GPIOI);
|
||||
|
||||
EXTI_ConfigTypeDef init2={0};
|
||||
init2.Line = EXTI_LINE_14;
|
||||
init2.Trigger = EXTI_TRIGGER_FALLING;
|
||||
init2.GPIOSel = EXTI_GPIOA;
|
||||
init2.Mode = EXTI_MODE_C2_INTERRUPT;
|
||||
PERIPH_LOCK(EXTI);
|
||||
HAL_EXTI_SetConfigLine(&s->hexti, &init2);
|
||||
PERIPH_UNLOCK(EXTI);
|
||||
|
||||
/* Register callback to treat Exti interrupts in user Exti14FallingCb function */
|
||||
HAL_EXTI_RegisterCallback(&s->hexti, HAL_EXTI_FALLING_CB_ID, Exti14FallingCb);
|
||||
|
||||
/* Enable and set line 14 Interrupt to the lowest priority */
|
||||
HAL_NVIC_SetPriority(EXTI6_IRQn, 0, 0);
|
||||
HAL_NVIC_EnableIRQ(EXTI6_IRQn);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -88,7 +116,7 @@ static int start(pwm_def *p,int step_count)
|
||||
param_check(p->private_data);
|
||||
self_def *self=p->private_data;
|
||||
if(step_count==0) return 0;
|
||||
if((*self->dtb->bitmap_pin_zero==0)&&(step_count<0))
|
||||
if((HAL_GPIO_ReadPin(GPIOI,6)==GPIO_PIN_RESET)&&(step_count<0))
|
||||
{
|
||||
// 到达零点后不能继续上升
|
||||
if(self->end_irq)
|
||||
@@ -96,10 +124,10 @@ static int start(pwm_def *p,int step_count)
|
||||
return -1;
|
||||
}
|
||||
if(step_count>0)
|
||||
*self->dtb->bitmap_pin_dir=0;
|
||||
HAL_GPIO_WritePin(GPIOF,8,GPIO_PIN_RESET);
|
||||
else{
|
||||
step_count=-step_count;
|
||||
*self->dtb->bitmap_pin_dir=1;
|
||||
HAL_GPIO_WritePin(GPIOF,8,GPIO_PIN_SET);
|
||||
}
|
||||
if(self->fre==0)
|
||||
{
|
||||
@@ -196,18 +224,14 @@ pwm_init_export(pwm,init,deinit,start,stop,set_fre,set_irq_fun,0)
|
||||
static inline void self_irq(self_def *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
volatile uint32_t *pin=self->dtb->bitmap_pin;
|
||||
if(TIM_GetFlagStatus(self->dtb->tim,TIM_FLAG_Update))
|
||||
|
||||
HAL_GPIO_TogglePin(GPIOF,7);
|
||||
irq_disable();
|
||||
self->count_past++;
|
||||
irq_enable();
|
||||
if(self->count_all>0&&(self->count_all<=self->count_past))
|
||||
{
|
||||
TIM_ClearFlag(self->dtb->tim,TIM_FLAG_Update);
|
||||
*pin=!(*pin);
|
||||
irq_disable();
|
||||
self->count_past++;
|
||||
irq_enable();
|
||||
if(self->count_all>0&&(self->count_all<=self->count_past))
|
||||
{
|
||||
self_stop__(self);
|
||||
}
|
||||
self_stop__(self);
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
@@ -249,13 +273,8 @@ static int calc_fre(self_def *self)
|
||||
static inline void self_irq2(self_def *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
volatile uint32_t *pin=self->dtb->bitmap_pin;
|
||||
if(TIM_GetFlagStatus(self->dtb->tim2,TIM_FLAG_Update))
|
||||
{
|
||||
TIM_ClearFlag(self->dtb->tim2,TIM_FLAG_Update);
|
||||
if(self->fre==0)
|
||||
self_set_fre(self,calc_fre(self));
|
||||
}
|
||||
if(self->fre==0)
|
||||
self_set_fre(self,calc_fre(self));
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
@@ -263,24 +282,35 @@ static inline void self_irq2(self_def *self)
|
||||
static inline void self_stop_irq(self_def *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
if(EXTI_GetFlagStatus(1<<self->dtb->gpio_zero_pin)){
|
||||
irq_disable();
|
||||
self->count_past=0;
|
||||
self->count_all=0;
|
||||
irq_enable();
|
||||
self_stop__(self);
|
||||
EXTI_ClearFlag(1<<self->dtb->gpio_zero_pin);
|
||||
}
|
||||
irq_disable();
|
||||
self->count_past=0;
|
||||
self->count_all=0;
|
||||
irq_enable();
|
||||
self_stop__(self);
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
|
||||
static void Exti14FallingCb(void)
|
||||
{
|
||||
self_def *s=&g_self;
|
||||
self_stop_irq(s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
self_def *s=&g_self;
|
||||
//DBG_LOG("timer updata.");
|
||||
if(htim==&s->htim)
|
||||
{
|
||||
self_irq(s);
|
||||
}
|
||||
else if(htim==&s->htim2)
|
||||
{
|
||||
self_irq2(s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -319,5 +349,10 @@ void TIM13_IRQHandler(void)
|
||||
HAL_TIM_IRQHandler(&s->htim2);
|
||||
}
|
||||
|
||||
void EXTI6_IRQHandler(void)
|
||||
{
|
||||
self_def *s=&g_self;
|
||||
HAL_EXTI_IRQHandler(&s->hexti);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user