实现电机,按键,虚拟串口,未验证

This commit is contained in:
ranchuan
2023-06-30 14:23:54 +08:00
parent edd3010a20
commit b15db16b91
14 changed files with 1130 additions and 368 deletions

View File

@@ -0,0 +1,125 @@
#include "board.h"
#include "lock_resource.h"
// PG3 KEY
// PE6,PH9,PE1,PH8 拨码
// 输入脚
#define GPIO_Initer() {.Mode=GPIO_MODE_INPUT,\
.Pull=GPIO_PULLUP \
}
typedef struct{
const char *name;
void (*gpio_clock_fun)(void);
GPIO_TypeDef *gpio_base;
uint16_t gpio_pin;
}gpioin_dtb;
static void rcc_gpioe_enable(void)
{
__HAL_RCC_GPIOE_CLK_ENABLE();
}
static void rcc_gpioh_enable(void)
{
__HAL_RCC_GPIOH_CLK_ENABLE();
}
static void rcc_gpiog_enable(void)
{
__HAL_RCC_GPIOG_CLK_ENABLE();
}
static const gpioin_dtb g_dtb[]={
{
.name="gpioin1",
.gpio_clock_fun=rcc_gpioe_enable,
.gpio_base=GPIOE,
.gpio_pin=GPIO_PIN_6,
},
{
.name="gpioin2",
.gpio_clock_fun=rcc_gpioh_enable,
.gpio_base=GPIOH,
.gpio_pin=GPIO_PIN_9,
},
{
.name="gpioin3",
.gpio_clock_fun=rcc_gpioe_enable,
.gpio_base=GPIOE,
.gpio_pin=GPIO_PIN_1,
},
{
.name="gpioin4",
.gpio_clock_fun=rcc_gpioh_enable,
.gpio_base=GPIOH,
.gpio_pin=GPIO_PIN_8,
},
{
.name="key",
.gpio_clock_fun=rcc_gpiog_enable,
.gpio_base=GPIOG,
.gpio_pin=GPIO_PIN_3,
},
};
typedef struct{
const gpioin_dtb *dtb;
}self_data;
static self_data g_self[LENGTH(g_dtb)];
def_find_fun(gpioin_dtb,g_dtb)
static int init(gpioin_def *g)
{
param_check(g);
if(g->private_data) return 0;
GPIO_InitTypeDef init=GPIO_Initer();
int index;
const gpioin_dtb *dtb=find(g->name,&index);
self_data *self=&g_self[index];
self->dtb=dtb;
g->private_data=self;
dtb->gpio_clock_fun();
init.Pin = 1<<dtb->gpio_pin;
HAL_GPIO_Init(dtb->gpio_base, &init);
return 0;
}
static int deinit(gpioin_def *g)
{
return 0;
}
static int state(gpioin_def *g)
{
param_check(g);
param_check(g->private_data);
self_data *self=g->private_data;
return HAL_GPIO_ReadPin(self->dtb->gpio_base,self->dtb->gpio_pin)==GPIO_PIN_RESET?0:1;
}
gpioin_init_export(gpioin1,init,deinit,state,0)
gpioin_init_export(gpioin2,init,deinit,state,0)
gpioin_init_export(gpioin3,init,deinit,state,0)
gpioin_init_export(gpioin4,init,deinit,state,0)
gpioin_init_export(key,init,deinit,state,0)

View File

@@ -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);
}

111
source/interface/if_vuart.c Normal file
View File

@@ -0,0 +1,111 @@
#include "board.h"
#include "lock_resource.h"
#include "openamp.h"
#include "string.h"
// 与a7通信的虚拟串口
// 系统启动的时候自动依次初始化,为了保证顺序
typedef struct{
VIRT_UART_HandleTypeDef huart;
void (*irq_fun)(void *t,uint8_t d);
void *t;
void *mutex;
}self_def;
static self_def g_self[3];
static int vuart_init(void)
{
self_def *s=g_self;
log_info("Virtual UART0 OpenAMP-rpmsg channel creation\r\n");
if (VIRT_UART_Init(&s[0].huart) != VIRT_UART_OK) {
log_err("VIRT_UART_Init UART0 failed.\r\n");
}
log_info("Virtual UART1 OpenAMP-rpmsg channel creation\r\n");
if (VIRT_UART_Init(&s[1].huart) != VIRT_UART_OK) {
log_err("VIRT_UART_Init UART1 failed.\r\n");
}
log_info("Virtual UART2 OpenAMP-rpmsg channel creation\r\n");
if (VIRT_UART_Init(&s[2].huart) != VIRT_UART_OK) {
log_err("VIRT_UART_Init UART2 failed.\r\n");
}
}
app_init_export(vuart_init);
static int init(uart_def *u)
{
self_def *s=g_self;
if(strcmp(u->name,"vuart0")==0)
{
u->private_data=&s[0].huart;
}else if(strcmp(u->name,"vuart1")==0)
{
u->private_data=&s[1].huart;
}else if(strcmp(u->name,"vuart2")==0)
{
u->private_data=&s[2].huart;
}
return 0;
}
static int deinit(uart_def *u)
{
return 0;
}
static int set_irq(uart_def *u,void (*irq)(void *t,uint8_t d),void *t)
{
param_check(u);
param_check(u->private_data);
self_def *self=u->private_data;
irq_disable();
self->irq_fun=irq;
self->t=t;
irq_enable();
return 0;
}
static int read(uart_def *u,uint8_t *b,int len)
{
param_check(u);
param_check(u->private_data);
return 0;
}
static int write(uart_def *u,const uint8_t *b,int len)
{
param_check(u);
param_check(u->private_data);
self_def *self=u->private_data;
rt_mutex_take(self->mutex,RT_WAITING_FOREVER);
VIRT_UART_Transmit(&self->huart, (uint8_t *)b, len);
rt_mutex_release(self->mutex);
return len;
}