移植到stm32f1,实现手动线赋码控制器功能
This commit is contained in:
@@ -1,17 +1,19 @@
|
||||
|
||||
#include "board.h"
|
||||
#include "stm32f4xx.h"
|
||||
#include "stm32f10x.h"
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
// PB6,7,8,9
|
||||
// 输入脚
|
||||
输入通道对应关系
|
||||
PC3|PC2|PC1|PC0|PB9|PB8|PB7|PB6|PB5|PC12
|
||||
IN0|IN1|IN2|IN3|IN4|IN5|IN6|IN7|IN8|IN9
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Mode=GPIO_Mode_IN,\
|
||||
.GPIO_Speed=GPIO_Speed_100MHz,\
|
||||
.GPIO_OType=GPIO_OType_PP,\
|
||||
.GPIO_PuPd=GPIO_PuPd_UP \
|
||||
*/
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Speed=GPIO_Speed_50MHz,\
|
||||
.GPIO_Mode=GPIO_Mode_IPU,\
|
||||
}
|
||||
|
||||
|
||||
@@ -28,37 +30,85 @@ typedef struct{
|
||||
|
||||
|
||||
static const gpioin_dtb g_dtb[]={
|
||||
{
|
||||
.name="gpioin0",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=3,
|
||||
.bitmap_pin=&PININ(C,3),
|
||||
},
|
||||
{
|
||||
.name="gpioin1",
|
||||
.gpio_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rcc=RCC_AHB1Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=6,
|
||||
.bitmap_pin=&PININ(B,6),
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=2,
|
||||
.bitmap_pin=&PININ(C,2),
|
||||
},
|
||||
{
|
||||
.name="gpioin2",
|
||||
.gpio_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rcc=RCC_AHB1Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=7,
|
||||
.bitmap_pin=&PININ(B,7),
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=1,
|
||||
.bitmap_pin=&PININ(C,1),
|
||||
},
|
||||
{
|
||||
.name="gpioin3",
|
||||
.gpio_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rcc=RCC_AHB1Periph_GPIOB,
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=0,
|
||||
.bitmap_pin=&PININ(C,0),
|
||||
},
|
||||
{
|
||||
.name="gpioin4",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=9,
|
||||
.bitmap_pin=&PININ(B,9),
|
||||
},
|
||||
{
|
||||
.name="gpioin5",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=8,
|
||||
.bitmap_pin=&PININ(B,8),
|
||||
},
|
||||
{
|
||||
.name="gpioin4",
|
||||
.gpio_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rcc=RCC_AHB1Periph_GPIOB,
|
||||
.name="gpioin6",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=9,
|
||||
.bitmap_pin=&PININ(B,9),
|
||||
.gpio_pin=7,
|
||||
.bitmap_pin=&PININ(B,7),
|
||||
},
|
||||
{
|
||||
.name="gpioin7",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=6,
|
||||
.bitmap_pin=&PININ(B,6),
|
||||
},
|
||||
{
|
||||
.name="gpioin8",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=5,
|
||||
.bitmap_pin=&PININ(B,5),
|
||||
},
|
||||
{
|
||||
.name="gpioin9",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=12,
|
||||
.bitmap_pin=&PININ(C,12),
|
||||
},
|
||||
};
|
||||
|
||||
@@ -105,14 +155,21 @@ static int state(gpioin_def *g)
|
||||
param_check(g);
|
||||
param_check(g->private_data);
|
||||
self_data *self=g->private_data;
|
||||
return *self->dtb->bitmap_pin;
|
||||
// 电路上0为导通,逻辑上1为真,这里反向
|
||||
return !(*self->dtb->bitmap_pin);
|
||||
}
|
||||
|
||||
|
||||
|
||||
gpioin_init_export(gpioin0,init,deinit,state,0)
|
||||
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(gpioin5,init,deinit,state,0)
|
||||
gpioin_init_export(gpioin6,init,deinit,state,0)
|
||||
gpioin_init_export(gpioin7,init,deinit,state,0)
|
||||
gpioin_init_export(gpioin8,init,deinit,state,0)
|
||||
gpioin_init_export(gpioin9,init,deinit,state,0)
|
||||
|
||||
|
||||
|
218
source/interface/if_gpioout.c
Normal file
218
source/interface/if_gpioout.c
Normal file
@@ -0,0 +1,218 @@
|
||||
#include "board.h"
|
||||
#include "stm32f10x.h"
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
---------------------------------------------------
|
||||
| 输出通道对应关系 |
|
||||
|-------------------------------------------------|
|
||||
|PA4 |PA5 |PA6 |PA7 |PC4 |PC5 |PB0 |PB1 |PB12|PB13|
|
||||
|-------------------------------------------------|
|
||||
|OUT0|OUT1|OUT2|OUT3|OUT4|OUT5|OUT6|OUT7|OUT8|OUT9|
|
||||
---------------------------------------------------
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Speed=GPIO_Speed_50MHz,\
|
||||
.GPIO_Mode=GPIO_Mode_Out_PP,\
|
||||
}
|
||||
|
||||
|
||||
typedef struct{
|
||||
const char *name;
|
||||
void (*gpio_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_rcc;
|
||||
GPIO_TypeDef *gpio_base;
|
||||
uint16_t gpio_pin;
|
||||
volatile uint32_t *bitmap_pin;
|
||||
}gpioout_dtb;
|
||||
|
||||
|
||||
static const gpioout_dtb g_dtb[]={
|
||||
{
|
||||
.name="gpioout0",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_base=GPIOA,
|
||||
.gpio_pin=4,
|
||||
.bitmap_pin=&PINOUT(A,4),
|
||||
},
|
||||
{
|
||||
.name="gpioout1",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_base=GPIOA,
|
||||
.gpio_pin=5,
|
||||
.bitmap_pin=&PINOUT(A,5),
|
||||
},
|
||||
{
|
||||
.name="gpioout2",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_base=GPIOA,
|
||||
.gpio_pin=6,
|
||||
.bitmap_pin=&PINOUT(A,6),
|
||||
},
|
||||
{
|
||||
.name="gpioout3",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_base=GPIOA,
|
||||
.gpio_pin=7,
|
||||
.bitmap_pin=&PINOUT(A,7),
|
||||
},
|
||||
{
|
||||
.name="gpioout4",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=4,
|
||||
.bitmap_pin=&PINOUT(C,4),
|
||||
},
|
||||
{
|
||||
.name="gpioout5",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=5,
|
||||
.bitmap_pin=&PINOUT(C,5),
|
||||
},
|
||||
{
|
||||
.name="gpioout6",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=0,
|
||||
.bitmap_pin=&PINOUT(B,0),
|
||||
},
|
||||
{
|
||||
.name="gpioout7",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=1,
|
||||
.bitmap_pin=&PINOUT(B,1),
|
||||
},
|
||||
{
|
||||
.name="gpioout8",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=12,
|
||||
.bitmap_pin=&PINOUT(B,12),
|
||||
},
|
||||
{
|
||||
.name="gpioout9",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_base=GPIOB,
|
||||
.gpio_pin=13,
|
||||
.bitmap_pin=&PINOUT(B,13),
|
||||
},
|
||||
{
|
||||
.name="led",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=6,
|
||||
.bitmap_pin=&PINOUT(C,6),
|
||||
},
|
||||
{
|
||||
.name="mod1_use",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=7,
|
||||
.bitmap_pin=&PINOUT(C,7),
|
||||
},
|
||||
{
|
||||
.name="bus_sel",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=8,
|
||||
.bitmap_pin=&PINOUT(C,8),
|
||||
},
|
||||
{
|
||||
.name="mod_sel",
|
||||
.gpio_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rcc=RCC_APB2Periph_GPIOC,
|
||||
.gpio_base=GPIOC,
|
||||
.gpio_pin=9,
|
||||
.bitmap_pin=&PINOUT(C,9),
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
const gpioout_dtb *dtb;
|
||||
}self_data;
|
||||
|
||||
|
||||
static self_data g_self[LENGTH(g_dtb)];
|
||||
|
||||
|
||||
def_find_fun(gpioout_dtb,g_dtb)
|
||||
|
||||
|
||||
|
||||
static int init(gpioout_def *g)
|
||||
{
|
||||
param_check(g);
|
||||
if(g->private_data) return 0;
|
||||
GPIO_InitTypeDef init=GPIO_Initer();
|
||||
int index;
|
||||
const gpioout_dtb *dtb=find(g->name,&index);
|
||||
self_data *self=&g_self[index];
|
||||
self->dtb=dtb;
|
||||
g->private_data=self;
|
||||
|
||||
dtb->gpio_clock_fun(dtb->gpio_rcc,ENABLE);
|
||||
init.GPIO_Pin = 1<<dtb->gpio_pin;
|
||||
GPIO_Init(dtb->gpio_base, &init);
|
||||
*dtb->bitmap_pin=1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int deinit(gpioout_def *g)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int set(gpioout_def *g,int state)
|
||||
{
|
||||
param_check(g);
|
||||
param_check(g->private_data);
|
||||
self_data *self=g->private_data;
|
||||
// 电路上0为导通,逻辑上1为真,在此反向
|
||||
*self->dtb->bitmap_pin=state?0:1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
gpioout_init_export(gpioout0,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout1,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout2,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout3,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout4,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout5,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout6,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout7,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout8,init,deinit,set,0)
|
||||
gpioout_init_export(gpioout9,init,deinit,set,0)
|
||||
gpioout_init_export(led,init,deinit,set,0)
|
||||
gpioout_init_export(mod1_use,init,deinit,set,0)
|
||||
gpioout_init_export(bus_sel,init,deinit,set,0)
|
||||
gpioout_init_export(mod_sel,init,deinit,set,0)
|
||||
|
||||
|
||||
|
||||
|
||||
|
83
source/interface/if_key.c
Normal file
83
source/interface/if_key.c
Normal file
@@ -0,0 +1,83 @@
|
||||
|
||||
#include "board.h"
|
||||
#include "stm32f10x.h"
|
||||
#include "if_key.h"
|
||||
#include "core_delay.h"
|
||||
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Speed=GPIO_Speed_50MHz,\
|
||||
.GPIO_Mode=GPIO_Mode_IPU,\
|
||||
}
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
int key_old;
|
||||
|
||||
}self_def;
|
||||
|
||||
static self_def g_self;
|
||||
|
||||
static int init(void)
|
||||
{
|
||||
GPIO_InitTypeDef init2=GPIO_Initer();
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
|
||||
|
||||
init2.GPIO_Pin=(1<<0);
|
||||
GPIO_Init(GPIOB,&init2);
|
||||
|
||||
}
|
||||
|
||||
|
||||
static int scan(void)
|
||||
{
|
||||
self_def *s=&g_self;
|
||||
int key=0,key2=0;
|
||||
key=PININ(B,0);
|
||||
if(key!=s->key_old)
|
||||
{
|
||||
//连续采集5次,都为相反电平时才输出此电平
|
||||
for(int i=0;i<2;i++)
|
||||
{
|
||||
delay_ms(20);
|
||||
key2=PININ(B,0);
|
||||
if(key!=key2) {
|
||||
return key2;
|
||||
}
|
||||
}
|
||||
return key;
|
||||
}
|
||||
return s->key_old;
|
||||
}
|
||||
|
||||
|
||||
// 读取按键,1按下,0未按下
|
||||
static int read(void)
|
||||
{
|
||||
self_def *s=&g_self;
|
||||
int key=scan();
|
||||
int key_ret=0;
|
||||
if(key!=s->key_old)
|
||||
{
|
||||
if(key==0){
|
||||
key_ret=1;
|
||||
}else{
|
||||
key_ret=0;
|
||||
}
|
||||
s->key_old=key;
|
||||
}
|
||||
return key_ret;
|
||||
}
|
||||
|
||||
|
||||
static key_def g_key={
|
||||
.init=init,
|
||||
.read=read,
|
||||
};
|
||||
|
||||
|
||||
key_def *key(void)
|
||||
{
|
||||
return &g_key;
|
||||
}
|
||||
|
22
source/interface/if_key.h
Normal file
22
source/interface/if_key.h
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
|
||||
#ifndef if_key_h__
|
||||
#define if_key_h__
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
int (*init)(void);
|
||||
int (*read)(void);
|
||||
}key_def;
|
||||
|
||||
key_def *key(void);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -1,426 +0,0 @@
|
||||
|
||||
|
||||
#include "stm32f4xx.h"
|
||||
#include "board.h"
|
||||
#include "if_pwm.h"
|
||||
#include "math.h"
|
||||
|
||||
|
||||
// PE0 PWM 输出脚
|
||||
// PE0,PE1,PE2,PE4,都不是定时器输出脚
|
||||
// 只能使用普通方式
|
||||
// PB7==0时到达上升零点
|
||||
// tim2用于调速
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Mode=GPIO_Mode_OUT,\
|
||||
.GPIO_Speed=GPIO_Speed_100MHz,\
|
||||
.GPIO_OType=GPIO_OType_PP,\
|
||||
.GPIO_PuPd=GPIO_PuPd_UP \
|
||||
}
|
||||
|
||||
// 重装载值设为31us,引脚翻转一个周期是62us
|
||||
#define TIMER_Initer(){\
|
||||
.TIM_Period = 31-1,\
|
||||
.TIM_Prescaler= 84-1,\
|
||||
.TIM_CounterMode=TIM_CounterMode_Up,\
|
||||
.TIM_ClockDivision=TIM_CKD_DIV1,\
|
||||
}
|
||||
|
||||
#define NVIC_Initer() {0}
|
||||
|
||||
|
||||
#define EXTI_Initer() {\
|
||||
.EXTI_Mode=EXTI_Mode_Interrupt,\
|
||||
.EXTI_Trigger=EXTI_Trigger_Falling,\
|
||||
.EXTI_LineCmd=ENABLE,\
|
||||
}
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
char *name;
|
||||
TIM_TypeDef *tim;
|
||||
void (*tim_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t tim_rcc;
|
||||
int irq_channel;
|
||||
|
||||
TIM_TypeDef *tim2;
|
||||
void (*tim2_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t tim2_rcc;
|
||||
int irq2_channel;
|
||||
|
||||
void (*gpio_tx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_tx_rcc;
|
||||
GPIO_TypeDef *gpio_tx_base;
|
||||
uint16_t gpio_tx_pin;
|
||||
volatile uint32_t *bitmap_pin;
|
||||
|
||||
void (*gpio_dir_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_dir_rcc;
|
||||
GPIO_TypeDef *gpio_dir_base;
|
||||
uint16_t gpio_dir_pin;
|
||||
volatile uint32_t *bitmap_pin_dir;
|
||||
|
||||
void (*gpio_zero_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_zero_rcc;
|
||||
GPIO_TypeDef *gpio_zero_base;
|
||||
uint16_t gpio_zero_pin;
|
||||
volatile uint32_t *bitmap_pin_zero;
|
||||
int zero_irq_channel;
|
||||
uint8_t exti_src_port;
|
||||
}pwm_dtb;
|
||||
|
||||
|
||||
// 可以基于定时器2345
|
||||
static const pwm_dtb g_pwmdtb[]={
|
||||
{
|
||||
.name="pwm1",
|
||||
.tim=TIM2,
|
||||
.tim_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.tim_rcc=RCC_APB1Periph_TIM2,
|
||||
.irq_channel=TIM2_IRQn,
|
||||
|
||||
.tim2=TIM3,
|
||||
.tim2_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.tim2_rcc=RCC_APB1Periph_TIM3,
|
||||
.irq2_channel=TIM3_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOE,
|
||||
.gpio_tx_base=GPIOE,
|
||||
.gpio_tx_pin=0,
|
||||
.bitmap_pin=&PINOUT(E,0),
|
||||
|
||||
.gpio_dir_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_dir_rcc=RCC_AHB1Periph_GPIOE,
|
||||
.gpio_dir_base=GPIOE,
|
||||
.gpio_dir_pin=1,
|
||||
.bitmap_pin_dir=&PINOUT(E,1),
|
||||
|
||||
.gpio_zero_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_zero_rcc=RCC_AHB1Periph_GPIOB,
|
||||
.gpio_zero_base=GPIOB,
|
||||
.gpio_zero_pin=7,
|
||||
.bitmap_pin_zero=&PININ(B,7),
|
||||
.zero_irq_channel=EXTI9_5_IRQn,
|
||||
.exti_src_port=EXTI_PortSourceGPIOB,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
int tick;
|
||||
int step;
|
||||
int fre;
|
||||
int fre_max;
|
||||
int fre_min;
|
||||
int up_tick;
|
||||
}ctrl_fre;
|
||||
|
||||
typedef struct{
|
||||
const pwm_dtb *dtb;
|
||||
int count_all;
|
||||
int count_past;
|
||||
int fre;
|
||||
ctrl_fre cfre;
|
||||
void (*end_irq)(void *t);
|
||||
void *t;
|
||||
}self_data;
|
||||
|
||||
|
||||
static self_data g_self[LENGTH(g_pwmdtb)];
|
||||
|
||||
|
||||
def_find_fun(pwm_dtb,g_pwmdtb)
|
||||
|
||||
|
||||
|
||||
static int init(pwm_def *p)
|
||||
{
|
||||
param_check(p);
|
||||
if(p->private_data) return 0;
|
||||
GPIO_InitTypeDef init=GPIO_Initer();
|
||||
TIM_TimeBaseInitTypeDef init2=TIMER_Initer();
|
||||
NVIC_InitTypeDef init3=NVIC_Initer();
|
||||
EXTI_InitTypeDef init4=EXTI_Initer();
|
||||
int index;
|
||||
const pwm_dtb *dtb=find(p->name,&index);
|
||||
self_data *self=&g_self[index];
|
||||
self->dtb=dtb;
|
||||
self->cfre.step=320;
|
||||
self->cfre.fre_min=1100;
|
||||
p->private_data=self;
|
||||
|
||||
dtb->tim_clock_fun(dtb->tim_rcc,ENABLE);
|
||||
TIM_TimeBaseInit(dtb->tim,&init2);
|
||||
TIM_ITConfig(dtb->tim,TIM_IT_Update,ENABLE);
|
||||
|
||||
init2.TIM_Period=1000-1;
|
||||
dtb->tim2_clock_fun(dtb->tim2_rcc,ENABLE);
|
||||
TIM_TimeBaseInit(dtb->tim2,&init2);
|
||||
TIM_ITConfig(dtb->tim2,TIM_IT_Update,ENABLE);
|
||||
|
||||
dtb->gpio_tx_clock_fun(dtb->gpio_tx_rcc,ENABLE);
|
||||
init.GPIO_Pin = 1<<dtb->gpio_tx_pin;
|
||||
GPIO_Init(dtb->gpio_tx_base, &init);
|
||||
dtb->gpio_dir_clock_fun(dtb->gpio_dir_rcc,ENABLE);
|
||||
init.GPIO_Pin = 1<<dtb->gpio_dir_pin;
|
||||
GPIO_Init(dtb->gpio_dir_base, &init);
|
||||
dtb->gpio_zero_clock_fun(dtb->gpio_zero_rcc,ENABLE);
|
||||
init.GPIO_Pin = 1<<dtb->gpio_zero_pin;
|
||||
init.GPIO_Mode=GPIO_Mode_IN;
|
||||
GPIO_Init(dtb->gpio_zero_base, &init);
|
||||
|
||||
init3.NVIC_IRQChannel = dtb->irq_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=0;
|
||||
init3.NVIC_IRQChannelSubPriority =0;
|
||||
init3.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&init3);
|
||||
init3.NVIC_IRQChannel = dtb->irq2_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=1;
|
||||
init3.NVIC_IRQChannelSubPriority =1;
|
||||
NVIC_Init(&init3);
|
||||
|
||||
init4.EXTI_Line=1<<dtb->gpio_zero_pin;
|
||||
EXTI_Init(&init4);
|
||||
SYSCFG_EXTILineConfig(dtb->exti_src_port,dtb->gpio_zero_pin);
|
||||
init3.NVIC_IRQChannel = dtb->zero_irq_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=0;
|
||||
init3.NVIC_IRQChannelSubPriority =0;
|
||||
init3.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&init3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int deinit(pwm_def *p)
|
||||
{
|
||||
param_check(p);
|
||||
if(p->private_data==0) return 0;
|
||||
NVIC_InitTypeDef init3=NVIC_Initer();
|
||||
const pwm_dtb *dtb=find(p->name,0);
|
||||
{
|
||||
TIM_Cmd(dtb->tim, DISABLE);
|
||||
dtb->tim_clock_fun(dtb->tim_rcc,DISABLE);
|
||||
dtb->gpio_tx_clock_fun(dtb->gpio_tx_rcc,DISABLE);
|
||||
init3.NVIC_IRQChannelCmd = DISABLE;
|
||||
NVIC_Init(&init3);
|
||||
TIM_ITConfig(dtb->tim,TIM_IT_Update,DISABLE);
|
||||
p->private_data=0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// step_count有方向,向下为正
|
||||
static int start(pwm_def *p,int step_count)
|
||||
{
|
||||
param_check(p);
|
||||
param_check(p->private_data);
|
||||
self_data *self=p->private_data;
|
||||
if(step_count==0) return 0;
|
||||
if((*self->dtb->bitmap_pin_zero==0)&&(step_count<0))
|
||||
{
|
||||
// 到达零点后不能继续上升
|
||||
if(self->end_irq)
|
||||
self->end_irq(self->t);
|
||||
return -1;
|
||||
}
|
||||
if(step_count>0)
|
||||
*self->dtb->bitmap_pin_dir=0;
|
||||
else{
|
||||
step_count=-step_count;
|
||||
*self->dtb->bitmap_pin_dir=1;
|
||||
}
|
||||
if(self->fre==0)
|
||||
{
|
||||
ctrl_fre *cfre=&self->cfre;
|
||||
memset(cfre,0,sizeof(ctrl_fre));
|
||||
int max_count=0;
|
||||
cfre->tick=0;
|
||||
cfre->step=320;
|
||||
cfre->fre_max=16000;
|
||||
cfre->fre_min=1100;
|
||||
cfre->up_tick=0;
|
||||
cfre->fre=cfre->fre_min;
|
||||
}
|
||||
irq_disable();
|
||||
if(step_count>0)
|
||||
{
|
||||
self->count_all=step_count;
|
||||
}else{
|
||||
self->count_all=0;
|
||||
}
|
||||
self->count_past=0;
|
||||
irq_enable();
|
||||
TIM_Cmd(self->dtb->tim, ENABLE);
|
||||
TIM_Cmd(self->dtb->tim2, ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void self_stop__(self_data *self);
|
||||
|
||||
static int stop(pwm_def *p)
|
||||
{
|
||||
param_check(p);
|
||||
param_check(p->private_data);
|
||||
self_data *self=p->private_data;
|
||||
self_stop__(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static inline void self_set_fre(self_data *self,int fre)
|
||||
{
|
||||
// 两个定时器溢出为一个翻转周期,这里重装载值要除2
|
||||
TIM_SetAutoreload(self->dtb->tim,1000000/2/fre);
|
||||
TIM_SetCounter(self->dtb->tim,0);
|
||||
}
|
||||
|
||||
// 设置频率,最低8hz,最高16000hz
|
||||
// 如果设置fre为0,则自动运行
|
||||
static int set_fre(pwm_def *p,int fre)
|
||||
{
|
||||
param_check(p);
|
||||
param_check(p->private_data);
|
||||
self_data *self=p->private_data;
|
||||
if(fre==0)
|
||||
{
|
||||
self->fre=fre;
|
||||
return 0;
|
||||
}
|
||||
if((fre<8)||(fre>16000)) return -1;
|
||||
self->fre=fre;
|
||||
self_set_fre(self,fre);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// 设置中断回调
|
||||
static int set_irq_fun(pwm_def *p,void (*fun)(void *t),void *t)
|
||||
{
|
||||
param_check(p);
|
||||
param_check(p->private_data);
|
||||
self_data *self=p->private_data;
|
||||
irq_disable();
|
||||
self->end_irq=fun;
|
||||
self->t=t;
|
||||
irq_enable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static inline void self_stop__(self_data *self)
|
||||
{
|
||||
TIM_Cmd(self->dtb->tim, DISABLE);
|
||||
TIM_Cmd(self->dtb->tim2, DISABLE);
|
||||
if(self->end_irq)
|
||||
self->end_irq(self->t);
|
||||
}
|
||||
|
||||
|
||||
static inline void self_irq(self_data *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
volatile uint32_t *pin=self->dtb->bitmap_pin;
|
||||
if(TIM_GetFlagStatus(self->dtb->tim,TIM_FLAG_Update))
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
|
||||
static inline void calc_up(self_data *self)
|
||||
{
|
||||
ctrl_fre *cfre=&self->cfre;
|
||||
if(self->count_past<self->count_all/2)
|
||||
{
|
||||
if(cfre->fre<cfre->fre_max)
|
||||
cfre->fre+=cfre->step;
|
||||
else
|
||||
cfre->up_tick++;
|
||||
}else{
|
||||
if(cfre->up_tick>0)
|
||||
cfre->up_tick--;
|
||||
else{
|
||||
cfre->fre-=cfre->step;
|
||||
}
|
||||
}
|
||||
// 防止速度减到0,永远不停止
|
||||
if(cfre->fre<cfre->fre_min)
|
||||
cfre->fre=cfre->fre_min;
|
||||
}
|
||||
|
||||
|
||||
static int calc_fre(self_data *self)
|
||||
{
|
||||
ctrl_fre *cfre=&self->cfre;
|
||||
int fre=0;
|
||||
calc_up(self);
|
||||
fre=cfre->fre;
|
||||
// 会每1ms更新一次频率,因此频率必须大于1000
|
||||
param_check(fre>1000);
|
||||
cfre->tick++;
|
||||
return fre;
|
||||
}
|
||||
static inline void self_irq2(self_data *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));
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
|
||||
static inline void self_stop_irq(self_data *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);
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
void TIM2_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[0];
|
||||
self_irq(self);
|
||||
}
|
||||
void TIM3_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[0];
|
||||
self_irq2(self);
|
||||
}
|
||||
|
||||
|
||||
void EXTI9_5_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[0];
|
||||
self_stop_irq(self);
|
||||
}
|
||||
|
||||
|
||||
pwm_init_export(pwm1,init,deinit,start,stop,set_fre,set_irq_fun,0)
|
||||
|
||||
|
@@ -1,32 +1,7 @@
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "stm32f4xx.h"
|
||||
|
||||
#include "board.h"
|
||||
#include "if_uart.h"
|
||||
|
||||
|
||||
|
||||
#ifndef RT_THREAD
|
||||
|
||||
#define rt_interrupt_enter()
|
||||
#define rt_interrupt_leave()
|
||||
#define rt_mutex_create(...) 0
|
||||
#define rt_mutex_delete(...)
|
||||
#define rt_mutex_take(...)
|
||||
#define rt_mutex_release(...)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Mode=GPIO_Mode_AF,\
|
||||
.GPIO_Speed=GPIO_Speed_50MHz,\
|
||||
.GPIO_OType=GPIO_OType_PP,\
|
||||
.GPIO_PuPd=GPIO_PuPd_UP \
|
||||
}
|
||||
#include "stm32f10x.h"
|
||||
|
||||
|
||||
#define UART_Initer() {.USART_WordLength=USART_WordLength_8b,\
|
||||
@@ -36,10 +11,43 @@
|
||||
.USART_Mode=USART_Mode_Rx | USART_Mode_Tx,\
|
||||
}
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Speed=GPIO_Speed_50MHz,\
|
||||
.GPIO_Mode=GPIO_Mode_AF_PP,\
|
||||
}
|
||||
|
||||
|
||||
|
||||
#define NVIC_Initer() {0}
|
||||
|
||||
|
||||
|
||||
#define DMA_RX_Initer() {\
|
||||
.DMA_DIR = DMA_DIR_PeripheralSRC,\
|
||||
.DMA_PeripheralInc = DMA_PeripheralInc_Disable,\
|
||||
.DMA_MemoryInc = DMA_MemoryInc_Enable,\
|
||||
.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,\
|
||||
.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte,\
|
||||
.DMA_Mode = DMA_Mode_Normal,\
|
||||
.DMA_Priority = DMA_Priority_High,\
|
||||
.DMA_M2M = DMA_M2M_Disable,\
|
||||
.DMA_BufferSize = 0,\
|
||||
}
|
||||
|
||||
|
||||
#define DMA_TX_Initer() {\
|
||||
.DMA_DIR = DMA_DIR_PeripheralDST,\
|
||||
.DMA_PeripheralInc = DMA_PeripheralInc_Disable,\
|
||||
.DMA_MemoryInc = DMA_MemoryInc_Enable,\
|
||||
.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,\
|
||||
.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte,\
|
||||
.DMA_Mode = DMA_Mode_Normal,\
|
||||
.DMA_Priority = DMA_Priority_High,\
|
||||
.DMA_M2M = DMA_M2M_Disable,\
|
||||
.DMA_BufferSize = 0,\
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
char *name;
|
||||
@@ -47,43 +55,59 @@ typedef struct{
|
||||
void (*uart_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t uart_rcc;
|
||||
int baudrate;
|
||||
int irq_channel;
|
||||
|
||||
void (*gpio_tx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_tx_rcc;
|
||||
GPIO_TypeDef *gpio_tx_base;
|
||||
uint16_t gpio_tx_pin;
|
||||
uint8_t gpio_tx_af;
|
||||
void (*gpio_rx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_rx_rcc;
|
||||
GPIO_TypeDef *gpio_rx_base;
|
||||
uint16_t gpio_rx_pin;
|
||||
uint8_t gpio_rx_af;
|
||||
|
||||
int irq_channel;
|
||||
DMA_Channel_TypeDef *dma_rx_channel;
|
||||
void (*dma_rx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t dma_rx_rcc;
|
||||
|
||||
DMA_Channel_TypeDef *dma_tx_channel;
|
||||
void (*dma_tx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t dma_tx_rcc;
|
||||
int dma_tx_irq_channel;
|
||||
uint32_t dma_tx_tc_flag;
|
||||
|
||||
}uart_dtb;
|
||||
|
||||
|
||||
|
||||
|
||||
static const uart_dtb g_uartdtb[]={
|
||||
{
|
||||
.name="uart1",
|
||||
.uart=USART1,
|
||||
.uart_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB2Periph_USART1,
|
||||
.baudrate=57600,
|
||||
.baudrate=115200,
|
||||
.irq_channel=USART1_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOA,
|
||||
.gpio_tx_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_tx_base=GPIOA,
|
||||
.gpio_tx_pin=9,
|
||||
.gpio_tx_af=GPIO_AF_USART1,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOA,
|
||||
.gpio_rx_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_rx_base=GPIOA,
|
||||
.gpio_rx_pin=10,
|
||||
.gpio_rx_af=GPIO_AF_USART1,
|
||||
|
||||
.irq_channel=USART1_IRQn,
|
||||
.dma_rx_clock_fun=RCC_AHBPeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHBPeriph_DMA1,
|
||||
.dma_rx_channel=DMA1_Channel5,
|
||||
|
||||
.dma_tx_clock_fun=RCC_AHBPeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHBPeriph_DMA1,
|
||||
.dma_tx_channel=DMA1_Channel4,
|
||||
.dma_tx_irq_channel=DMA1_Channel4_IRQn,
|
||||
.dma_tx_tc_flag=DMA1_FLAG_TC4,
|
||||
},
|
||||
{
|
||||
.name="uart2",
|
||||
@@ -91,19 +115,26 @@ static const uart_dtb g_uartdtb[]={
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_USART2,
|
||||
.baudrate=57600,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_tx_base=GPIOD,
|
||||
.gpio_tx_pin=5,
|
||||
.gpio_tx_af=GPIO_AF_USART2,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_rx_base=GPIOD,
|
||||
.gpio_rx_pin=6,
|
||||
.gpio_rx_af=GPIO_AF_USART2,
|
||||
|
||||
.irq_channel=USART2_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_tx_base=GPIOA,
|
||||
.gpio_tx_pin=2,
|
||||
.gpio_rx_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_APB2Periph_GPIOA,
|
||||
.gpio_rx_base=GPIOA,
|
||||
.gpio_rx_pin=3,
|
||||
|
||||
.dma_rx_clock_fun=RCC_AHBPeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHBPeriph_DMA1,
|
||||
.dma_rx_channel=DMA1_Channel6,
|
||||
|
||||
.dma_tx_clock_fun=RCC_AHBPeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHBPeriph_DMA1,
|
||||
.dma_tx_channel=DMA1_Channel7,
|
||||
.dma_tx_irq_channel=DMA1_Channel7_IRQn,
|
||||
.dma_tx_tc_flag=DMA1_FLAG_TC7,
|
||||
},
|
||||
{
|
||||
.name="uart3",
|
||||
@@ -111,89 +142,43 @@ static const uart_dtb g_uartdtb[]={
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_USART3,
|
||||
.baudrate=57600,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_tx_base=GPIOD,
|
||||
.gpio_tx_pin=8,
|
||||
.gpio_tx_af=GPIO_AF_USART3,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_rx_base=GPIOD,
|
||||
.gpio_rx_pin=9,
|
||||
.gpio_rx_af=GPIO_AF_USART3,
|
||||
|
||||
.irq_channel=USART3_IRQn,
|
||||
},
|
||||
{
|
||||
.name="uart4",
|
||||
.uart=UART4,
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_UART4,
|
||||
.baudrate=115200,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_tx_base=GPIOC,
|
||||
.gpio_tx_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_tx_base=GPIOB,
|
||||
.gpio_tx_pin=10,
|
||||
.gpio_tx_af=GPIO_AF_UART4,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_rx_base=GPIOC,
|
||||
.gpio_rx_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_APB2Periph_GPIOB,
|
||||
.gpio_rx_base=GPIOB,
|
||||
.gpio_rx_pin=11,
|
||||
.gpio_rx_af=GPIO_AF_UART4,
|
||||
|
||||
.irq_channel=UART4_IRQn,
|
||||
},
|
||||
{
|
||||
.name="uart5",
|
||||
.uart=UART5,
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_UART5,
|
||||
.baudrate=57600,
|
||||
.dma_rx_clock_fun=RCC_AHBPeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHBPeriph_DMA1,
|
||||
.dma_rx_channel=DMA1_Channel3,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_tx_base=GPIOC,
|
||||
.gpio_tx_pin=12,
|
||||
.gpio_tx_af=GPIO_AF_UART5,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_rx_base=GPIOD,
|
||||
.gpio_rx_pin=2,
|
||||
.gpio_rx_af=GPIO_AF_UART5,
|
||||
|
||||
.irq_channel=UART5_IRQn,
|
||||
},
|
||||
{
|
||||
.name="uart6",
|
||||
.uart=USART6,
|
||||
.uart_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB2Periph_USART6,
|
||||
.baudrate=57600,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_tx_base=GPIOC,
|
||||
.gpio_tx_pin=6,
|
||||
.gpio_tx_af=GPIO_AF_USART6,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_rx_base=GPIOC,
|
||||
.gpio_rx_pin=7,
|
||||
.gpio_rx_af=GPIO_AF_USART6,
|
||||
|
||||
.irq_channel=USART6_IRQn,
|
||||
.dma_tx_clock_fun=RCC_AHBPeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHBPeriph_DMA1,
|
||||
.dma_tx_channel=DMA1_Channel2,
|
||||
.dma_tx_irq_channel=DMA1_Channel2_IRQn,
|
||||
.dma_tx_tc_flag=DMA1_FLAG_TC2,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
const uart_dtb *dtb;
|
||||
void (*irq_fun)(void *t,uint8_t d);
|
||||
void (*irq_fun_end)(void *t,uint32_t len);
|
||||
void *t;
|
||||
void *mutex;
|
||||
uint8_t *rx_buff;
|
||||
uint32_t rx_buff_size;
|
||||
void *sem;
|
||||
int in_send;
|
||||
}self_data;
|
||||
|
||||
|
||||
@@ -204,119 +189,257 @@ def_find_fun(uart_dtb,g_uartdtb)
|
||||
|
||||
|
||||
|
||||
static int init(uart_def *u)
|
||||
static int init(uart_def *u,int bsp)
|
||||
{
|
||||
param_check(u);
|
||||
if(u->private_data) return 0;
|
||||
GPIO_InitTypeDef init=GPIO_Initer();
|
||||
USART_InitTypeDef init2=UART_Initer();
|
||||
USART_InitTypeDef init2=UART_Initer();
|
||||
NVIC_InitTypeDef init3=NVIC_Initer();
|
||||
|
||||
int index;
|
||||
const uart_dtb *dtb=find(u->name,&index);
|
||||
self_data *self=&g_self[index];
|
||||
self->dtb=dtb;
|
||||
self->irq_fun=0;
|
||||
self->t=0;
|
||||
self->mutex=rt_mutex_create(u->name,RT_IPC_FLAG_FIFO);
|
||||
{
|
||||
u->private_data=self;
|
||||
dtb->uart_clock_fun(dtb->uart_rcc,ENABLE);
|
||||
init2.USART_BaudRate = dtb->baudrate;
|
||||
USART_Init(dtb->uart, &init2);
|
||||
USART_Cmd(dtb->uart, ENABLE);
|
||||
|
||||
dtb->gpio_tx_clock_fun(dtb->gpio_tx_rcc,ENABLE);
|
||||
GPIO_PinAFConfig(dtb->gpio_tx_base,dtb->gpio_tx_pin,dtb->gpio_tx_af);
|
||||
init.GPIO_Pin=1<<dtb->gpio_tx_pin;
|
||||
GPIO_Init(dtb->gpio_tx_base,&init);
|
||||
self->sem=rt_sem_create(u->name,1,RT_IPC_FLAG_FIFO);
|
||||
self->in_send=0;
|
||||
|
||||
dtb->gpio_rx_clock_fun(dtb->gpio_rx_rcc,ENABLE);
|
||||
GPIO_PinAFConfig(dtb->gpio_rx_base,dtb->gpio_rx_pin,dtb->gpio_rx_af);
|
||||
init.GPIO_Pin=1<<dtb->gpio_rx_pin;
|
||||
GPIO_Init(dtb->gpio_rx_base,&init);
|
||||
u->private_data=self;
|
||||
dtb->uart_clock_fun(dtb->uart_rcc,ENABLE);
|
||||
if(bsp==0) bsp=dtb->baudrate;
|
||||
init2.USART_BaudRate = bsp;
|
||||
USART_Init(dtb->uart, &init2);
|
||||
USART_Cmd(dtb->uart, ENABLE);
|
||||
|
||||
dtb->gpio_tx_clock_fun(dtb->gpio_tx_rcc,ENABLE);
|
||||
init.GPIO_Pin=1<<dtb->gpio_tx_pin;
|
||||
GPIO_Init(dtb->gpio_tx_base,&init);
|
||||
|
||||
init3.NVIC_IRQChannel = dtb->irq_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=3;
|
||||
init3.NVIC_IRQChannelSubPriority =3;
|
||||
init3.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&init3);
|
||||
USART_ITConfig(dtb->uart, USART_IT_RXNE, ENABLE);
|
||||
}
|
||||
dtb->gpio_rx_clock_fun(dtb->gpio_rx_rcc,ENABLE);
|
||||
init.GPIO_Pin=1<<dtb->gpio_rx_pin;
|
||||
init.GPIO_Mode=GPIO_Mode_IPU;
|
||||
GPIO_Init(dtb->gpio_rx_base,&init);
|
||||
|
||||
init3.NVIC_IRQChannel = dtb->irq_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=3;
|
||||
init3.NVIC_IRQChannelSubPriority =3;
|
||||
init3.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&init3);
|
||||
|
||||
init3.NVIC_IRQChannel = dtb->dma_tx_irq_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=3;
|
||||
init3.NVIC_IRQChannelSubPriority =3;
|
||||
init3.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&init3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int deinit(uart_def *u)
|
||||
{
|
||||
param_check(u);
|
||||
if(u->private_data==0) return 0;
|
||||
NVIC_InitTypeDef init3=NVIC_Initer();
|
||||
const uart_dtb *dtb=find(u->name,0);
|
||||
{
|
||||
USART_Cmd(dtb->uart, DISABLE);
|
||||
dtb->uart_clock_fun(dtb->uart_rcc,DISABLE);
|
||||
dtb->gpio_tx_clock_fun(dtb->gpio_tx_rcc,DISABLE);
|
||||
dtb->gpio_rx_clock_fun(dtb->gpio_rx_rcc,DISABLE);
|
||||
init3.NVIC_IRQChannelCmd = DISABLE;
|
||||
NVIC_Init(&init3);
|
||||
USART_ITConfig(dtb->uart, USART_IT_RXNE, DISABLE);
|
||||
rt_mutex_delete(((self_data *)u->private_data)->mutex);
|
||||
u->private_data=0;
|
||||
}
|
||||
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_data *self=u->private_data;
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
irq_disable();
|
||||
self->irq_fun=irq;
|
||||
self->t=t;
|
||||
irq_enable();
|
||||
if(irq)
|
||||
USART_ITConfig(dtb->uart, USART_IT_RXNE, ENABLE);
|
||||
else
|
||||
USART_ITConfig(dtb->uart, USART_IT_RXNE, DISABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int dma_rx_init(self_data *self,uint8_t *rx_buff,uint32_t rx_buff_size)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
DMA_InitTypeDef init=DMA_RX_Initer();
|
||||
|
||||
USART_DMACmd(dtb->uart,USART_DMAReq_Rx,ENABLE);
|
||||
DMA_DeInit(dtb->dma_rx_channel);
|
||||
dtb->dma_rx_clock_fun(dtb->dma_rx_rcc,ENABLE);
|
||||
init.DMA_PeripheralBaseAddr = (u32)(&(dtb->uart->DR));
|
||||
init.DMA_MemoryBaseAddr = (u32)rx_buff;
|
||||
init.DMA_BufferSize=rx_buff_size;
|
||||
DMA_Init(dtb->dma_rx_channel, &init);
|
||||
DMA_Cmd(dtb->dma_rx_channel, DISABLE);
|
||||
USART_ITConfig(dtb->uart, USART_IT_IDLE, ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dma_rx_reset(self_data *self)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
|
||||
DMA_Cmd(dtb->dma_rx_channel, DISABLE);
|
||||
DMA_SetCurrDataCounter(dtb->dma_rx_channel, self->rx_buff_size);
|
||||
DMA_Cmd(dtb->dma_rx_channel, ENABLE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int set_end_irq(uart_def *u,uint8_t *rx_buff,int rx_buff_size,
|
||||
void (*irq)(void *t,uint32_t len),void *t)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
self_data *self=u->private_data;
|
||||
dma_rx_init(self,rx_buff,rx_buff_size);
|
||||
self->rx_buff=rx_buff;
|
||||
self->rx_buff_size=rx_buff_size;
|
||||
dma_rx_reset(self);
|
||||
irq_disable();
|
||||
self->irq_fun_end=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 dma_tx_init(self_data *self,const uint8_t *tx_buff,uint32_t tx_buff_size)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
DMA_InitTypeDef init=DMA_TX_Initer();
|
||||
|
||||
USART_DMACmd(dtb->uart,USART_DMAReq_Tx,ENABLE);
|
||||
DMA_DeInit(dtb->dma_tx_channel);
|
||||
dtb->dma_tx_clock_fun(dtb->dma_tx_rcc,ENABLE);
|
||||
init.DMA_PeripheralBaseAddr = (u32)(&(dtb->uart->DR));
|
||||
init.DMA_MemoryBaseAddr = (u32)tx_buff;
|
||||
init.DMA_BufferSize=tx_buff_size;
|
||||
DMA_Init(dtb->dma_tx_channel, &init);
|
||||
DMA_Cmd(dtb->dma_tx_channel, ENABLE);
|
||||
DMA_ITConfig(dtb->dma_tx_channel,DMA_IT_TC,ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int dma_tx_reset(self_data *self,uint8_t *tx_buff,uint32_t tx_buff_size)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
|
||||
USART_DMACmd(dtb->uart,USART_DMAReq_Tx,ENABLE);
|
||||
DMA_Cmd(dtb->dma_tx_channel, DISABLE);
|
||||
DMA_ClearFlag(dtb->dma_tx_tc_flag);
|
||||
DMA_SetCurrDataCounter(dtb->dma_tx_channel, tx_buff_size);
|
||||
dtb->dma_tx_channel->CMAR=(uint32_t)tx_buff;
|
||||
DMA_Cmd(dtb->dma_tx_channel, ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// dma后台发送需要保证数据在发送期间有效
|
||||
static int write(uart_def *u,const uint8_t *b,int len)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
self_data *self=u->private_data;
|
||||
USART_TypeDef *uart=self->dtb->uart;
|
||||
rt_mutex_take(self->mutex,RT_WAITING_FOREVER);
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
while(!USART_GetFlagStatus(uart,USART_FLAG_TXE));
|
||||
uart->DR=b[i];
|
||||
if(1){
|
||||
dma_tx_init(self,b,len);
|
||||
}else{
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
while(!USART_GetFlagStatus(uart,USART_FLAG_TXE));
|
||||
uart->DR=b[i];
|
||||
}
|
||||
}
|
||||
rt_mutex_release(self->mutex);
|
||||
return len;
|
||||
}
|
||||
|
||||
// 阻塞发送,此函数结束即可释放内存
|
||||
static int write_block(uart_def *u,const uint8_t *b,int len)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
self_data *self=u->private_data;
|
||||
USART_TypeDef *uart=self->dtb->uart;
|
||||
self->in_send=1;
|
||||
if(1){
|
||||
dma_tx_init(self,b,len);
|
||||
}else{
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
while(!USART_GetFlagStatus(uart,USART_FLAG_TXE));
|
||||
uart->DR=b[i];
|
||||
}
|
||||
self->in_send=0;
|
||||
}
|
||||
while(self->in_send){}
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static inline void self_irq(self_data *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
if(USART_GetFlagStatus(self->dtb->uart,USART_FLAG_RXNE))
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
if(USART_GetFlagStatus(dtb->uart,USART_FLAG_RXNE))
|
||||
{
|
||||
uint8_t d=dtb->uart->DR;
|
||||
if(self->irq_fun){
|
||||
self->irq_fun(self->t,self->dtb->uart->DR);
|
||||
self->irq_fun(self->t,d);
|
||||
}
|
||||
}
|
||||
else if(USART_GetFlagStatus(self->dtb->uart,USART_FLAG_TC))
|
||||
{
|
||||
USART_ClearFlag(self->dtb->uart,USART_FLAG_TC);
|
||||
USART_ClearFlag(dtb->uart,USART_FLAG_TC);
|
||||
}
|
||||
else if(USART_GetFlagStatus(self->dtb->uart,USART_FLAG_IDLE))
|
||||
{
|
||||
USART_ReceiveData(dtb->uart);
|
||||
if(self->irq_fun_end){
|
||||
self->irq_fun_end(self->t,self->rx_buff_size-DMA_GetCurrDataCounter(dtb->dma_rx_channel));
|
||||
}
|
||||
dma_rx_reset(self);
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
static inline void self_dma_irq(self_data *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
if(DMA_GetFlagStatus(dtb->dma_tx_tc_flag))
|
||||
{
|
||||
DMA_ClearFlag(dtb->dma_tx_tc_flag);
|
||||
DMA_Cmd(dtb->dma_tx_channel, DISABLE);
|
||||
self->in_send=0;
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
@@ -327,6 +450,7 @@ void USART1_IRQHandler(void)
|
||||
self_data *self=&g_self[0];
|
||||
self_irq(self);
|
||||
}
|
||||
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[1];
|
||||
@@ -337,31 +461,38 @@ void USART3_IRQHandler(void)
|
||||
self_data *self=&g_self[2];
|
||||
self_irq(self);
|
||||
}
|
||||
void UART4_IRQHandler(void)
|
||||
void DMA1_Channel4_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[3];
|
||||
self_irq(self);
|
||||
self_data *self=&g_self[0];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
void UART5_IRQHandler(void)
|
||||
void DMA1_Channel7_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[4];
|
||||
self_irq(self);
|
||||
self_data *self=&g_self[1];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
void USART6_IRQHandler(void)
|
||||
void DMA1_Channel2_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[5];
|
||||
self_irq(self);
|
||||
self_data *self=&g_self[2];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
|
||||
|
||||
uart_init_export(uart1,init,deinit,set_irq,set_end_irq,read,write_block,0)
|
||||
uart_init_export(uart2,init,deinit,set_irq,set_end_irq,read,write,0)
|
||||
uart_init_export(uart3,init,deinit,set_irq,set_end_irq,read,write,0)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
uart_init_export(uart1,init,deinit,set_irq,0,read,write,0)
|
||||
uart_init_export(uart2,init,deinit,set_irq,0,read,write,0)
|
||||
uart_init_export(uart3,init,deinit,set_irq,0,read,write,0)
|
||||
uart_init_export(uart4,init,deinit,set_irq,0,read,write,0)
|
||||
uart_init_export(uart5,init,deinit,set_irq,0,read,write,0)
|
||||
uart_init_export(uart6,init,deinit,set_irq,0,read,write,0)
|
||||
|
||||
|
||||
|
@@ -1,18 +1,7 @@
|
||||
|
||||
|
||||
|
||||
#ifndef if_uart_h__
|
||||
#define if_uart_h__
|
||||
|
||||
#include "stdint.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -20,7 +9,3 @@
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -1,652 +0,0 @@
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "stm32f4xx.h"
|
||||
#include "board.h"
|
||||
#include "if_uart_dma.h"
|
||||
|
||||
|
||||
#ifndef RT_THREAD
|
||||
|
||||
#define rt_interrupt_enter()
|
||||
#define rt_interrupt_leave()
|
||||
#define rt_sem_create(...) 0
|
||||
#define rt_sem_delete(...)
|
||||
#define rt_sem_take(...)
|
||||
#define rt_sem_release(...)
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define GPIO_Initer() {.GPIO_Mode=GPIO_Mode_AF,\
|
||||
.GPIO_Speed=GPIO_Speed_50MHz,\
|
||||
.GPIO_OType=GPIO_OType_PP,\
|
||||
.GPIO_PuPd=GPIO_PuPd_UP \
|
||||
}
|
||||
|
||||
|
||||
#define UART_Initer() {.USART_WordLength=USART_WordLength_8b,\
|
||||
.USART_StopBits=USART_StopBits_1,\
|
||||
.USART_Parity=USART_Parity_No,\
|
||||
.USART_HardwareFlowControl=USART_HardwareFlowControl_None,\
|
||||
.USART_Mode=USART_Mode_Rx | USART_Mode_Tx,\
|
||||
}
|
||||
|
||||
#define NVIC_Initer() {0}
|
||||
|
||||
|
||||
#define DMA_RX_Initer() {\
|
||||
.DMA_DIR = DMA_DIR_PeripheralToMemory,\
|
||||
.DMA_PeripheralInc = DMA_PeripheralInc_Disable,\
|
||||
.DMA_MemoryInc = DMA_MemoryInc_Enable,\
|
||||
.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,\
|
||||
.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,\
|
||||
.DMA_Mode = DMA_Mode_Circular,\
|
||||
.DMA_Priority = DMA_Priority_High,\
|
||||
.DMA_FIFOMode = DMA_FIFOMode_Disable,\
|
||||
.DMA_FIFOThreshold = DMA_FIFOThreshold_Full,\
|
||||
.DMA_MemoryBurst = DMA_MemoryBurst_Single,\
|
||||
.DMA_PeripheralBurst = DMA_PeripheralBurst_Single,\
|
||||
}
|
||||
|
||||
|
||||
#define DMA_TX_Initer() {\
|
||||
.DMA_DIR = DMA_DIR_MemoryToPeripheral,\
|
||||
.DMA_PeripheralInc = DMA_PeripheralInc_Disable,\
|
||||
.DMA_MemoryInc = DMA_MemoryInc_Enable,\
|
||||
.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,\
|
||||
.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,\
|
||||
.DMA_Mode = DMA_Mode_Normal,\
|
||||
.DMA_Priority = DMA_Priority_Medium,\
|
||||
.DMA_FIFOMode = DMA_FIFOMode_Disable,\
|
||||
.DMA_FIFOThreshold = DMA_FIFOThreshold_Full,\
|
||||
.DMA_MemoryBurst = DMA_MemoryBurst_Single,\
|
||||
.DMA_PeripheralBurst = DMA_PeripheralBurst_Single,\
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
char *name;
|
||||
USART_TypeDef *uart;
|
||||
void (*uart_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t uart_rcc;
|
||||
int baudrate;
|
||||
int irq_channel;
|
||||
|
||||
void (*gpio_tx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_tx_rcc;
|
||||
GPIO_TypeDef *gpio_tx_base;
|
||||
uint16_t gpio_tx_pin;
|
||||
uint8_t gpio_tx_af;
|
||||
void (*gpio_rx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t gpio_rx_rcc;
|
||||
GPIO_TypeDef *gpio_rx_base;
|
||||
uint16_t gpio_rx_pin;
|
||||
uint8_t gpio_rx_af;
|
||||
|
||||
DMA_Stream_TypeDef *dma_rx_stream;
|
||||
void (*dma_rx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t dma_rx_rcc;
|
||||
uint32_t dma_rx_channel;
|
||||
|
||||
DMA_Stream_TypeDef *dma_tx_stream;
|
||||
void (*dma_tx_clock_fun)(uint32_t,FunctionalState);
|
||||
uint32_t dma_tx_rcc;
|
||||
uint32_t dma_tx_channel;
|
||||
int dma_tx_irq_channel;
|
||||
uint32_t dma_tx_tc_flag;
|
||||
|
||||
}uart_dtb;
|
||||
|
||||
|
||||
|
||||
static const uart_dtb g_uartdtb[]={
|
||||
{
|
||||
.name="uart1",
|
||||
.uart=USART1,
|
||||
.uart_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB2Periph_USART1,
|
||||
.baudrate=57600,
|
||||
.irq_channel=USART1_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOA,
|
||||
.gpio_tx_base=GPIOA,
|
||||
.gpio_tx_pin=9,
|
||||
.gpio_tx_af=GPIO_AF_USART1,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOA,
|
||||
.gpio_rx_base=GPIOA,
|
||||
.gpio_rx_pin=10,
|
||||
.gpio_rx_af=GPIO_AF_USART1,
|
||||
|
||||
.dma_rx_stream=DMA2_Stream5,
|
||||
.dma_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHB1Periph_DMA2,
|
||||
.dma_rx_channel=DMA_Channel_4,
|
||||
|
||||
.dma_tx_stream=DMA2_Stream7,
|
||||
.dma_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHB1Periph_DMA2,
|
||||
.dma_tx_channel=DMA_Channel_4,
|
||||
.dma_tx_irq_channel=DMA2_Stream7_IRQn,
|
||||
.dma_tx_tc_flag=DMA_FLAG_TCIF7,
|
||||
},
|
||||
{
|
||||
.name="uart2",
|
||||
.uart=USART2,
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_USART2,
|
||||
.baudrate=57600,
|
||||
.irq_channel=USART2_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_tx_base=GPIOD,
|
||||
.gpio_tx_pin=5,
|
||||
.gpio_tx_af=GPIO_AF_USART2,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_rx_base=GPIOD,
|
||||
.gpio_rx_pin=6,
|
||||
.gpio_rx_af=GPIO_AF_USART2,
|
||||
|
||||
.dma_rx_stream=DMA1_Stream5,
|
||||
.dma_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_rx_channel=DMA_Channel_4,
|
||||
|
||||
.dma_tx_stream=DMA1_Stream6,
|
||||
.dma_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_tx_channel=DMA_Channel_4,
|
||||
.dma_tx_irq_channel=DMA1_Stream6_IRQn,
|
||||
.dma_tx_tc_flag=DMA_FLAG_TCIF6,
|
||||
},
|
||||
{
|
||||
.name="uart3",
|
||||
.uart=USART3,
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_USART3,
|
||||
.baudrate=57600,
|
||||
.irq_channel=USART3_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_tx_base=GPIOD,
|
||||
.gpio_tx_pin=8,
|
||||
.gpio_tx_af=GPIO_AF_USART3,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_rx_base=GPIOD,
|
||||
.gpio_rx_pin=9,
|
||||
.gpio_rx_af=GPIO_AF_USART3,
|
||||
|
||||
.dma_rx_stream=DMA1_Stream1,
|
||||
.dma_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_rx_channel=DMA_Channel_4,
|
||||
|
||||
.dma_tx_stream=DMA1_Stream3,
|
||||
.dma_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_tx_channel=DMA_Channel_4,
|
||||
.dma_tx_irq_channel=DMA1_Stream3_IRQn,
|
||||
.dma_tx_tc_flag=DMA_FLAG_TCIF3,
|
||||
},
|
||||
{
|
||||
.name="uart4",
|
||||
.uart=UART4,
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_UART4,
|
||||
.baudrate=115200,
|
||||
.irq_channel=UART4_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_tx_base=GPIOC,
|
||||
.gpio_tx_pin=10,
|
||||
.gpio_tx_af=GPIO_AF_UART4,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_rx_base=GPIOC,
|
||||
.gpio_rx_pin=11,
|
||||
.gpio_rx_af=GPIO_AF_UART4,
|
||||
|
||||
.dma_rx_stream=DMA1_Stream2,
|
||||
.dma_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_rx_channel=DMA_Channel_4,
|
||||
|
||||
.dma_tx_stream=DMA1_Stream4,
|
||||
.dma_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_tx_channel=DMA_Channel_4,
|
||||
.dma_tx_irq_channel=DMA1_Stream4_IRQn,
|
||||
.dma_tx_tc_flag=DMA_FLAG_TCIF4,
|
||||
},
|
||||
{
|
||||
.name="uart5",
|
||||
.uart=UART5,
|
||||
.uart_clock_fun=RCC_APB1PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB1Periph_UART5,
|
||||
.baudrate=57600,
|
||||
.irq_channel=UART5_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_tx_base=GPIOC,
|
||||
.gpio_tx_pin=12,
|
||||
.gpio_tx_af=GPIO_AF_UART5,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOD,
|
||||
.gpio_rx_base=GPIOD,
|
||||
.gpio_rx_pin=2,
|
||||
.gpio_rx_af=GPIO_AF_UART5,
|
||||
|
||||
.dma_rx_stream=DMA1_Stream0,
|
||||
.dma_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_rx_channel=DMA_Channel_4,
|
||||
|
||||
.dma_tx_stream=DMA1_Stream7,
|
||||
.dma_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHB1Periph_DMA1,
|
||||
.dma_tx_channel=DMA_Channel_4,
|
||||
.dma_tx_irq_channel=DMA1_Stream7_IRQn,
|
||||
.dma_tx_tc_flag=DMA_FLAG_TCIF7,
|
||||
},
|
||||
{
|
||||
.name="uart6",
|
||||
.uart=USART6,
|
||||
.uart_clock_fun=RCC_APB2PeriphClockCmd,
|
||||
.uart_rcc=RCC_APB2Periph_USART6,
|
||||
.baudrate=57600,
|
||||
.irq_channel=USART6_IRQn,
|
||||
|
||||
.gpio_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_tx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_tx_base=GPIOC,
|
||||
.gpio_tx_pin=6,
|
||||
.gpio_tx_af=GPIO_AF_USART6,
|
||||
.gpio_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.gpio_rx_rcc=RCC_AHB1Periph_GPIOC,
|
||||
.gpio_rx_base=GPIOC,
|
||||
.gpio_rx_pin=7,
|
||||
.gpio_rx_af=GPIO_AF_USART6,
|
||||
|
||||
.dma_rx_stream=DMA2_Stream2,
|
||||
.dma_rx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_rx_rcc=RCC_AHB1Periph_DMA2,
|
||||
.dma_rx_channel=DMA_Channel_5,
|
||||
|
||||
.dma_tx_stream=DMA2_Stream6,
|
||||
.dma_tx_clock_fun=RCC_AHB1PeriphClockCmd,
|
||||
.dma_tx_rcc=RCC_AHB1Periph_DMA2,
|
||||
.dma_tx_channel=DMA_Channel_5,
|
||||
.dma_tx_irq_channel=DMA2_Stream6_IRQn,
|
||||
.dma_tx_tc_flag=DMA_FLAG_TCIF6,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
|
||||
typedef struct{
|
||||
const uart_dtb *dtb;
|
||||
void (*irq_fun)(void *t,uint8_t d);
|
||||
void (*irq_fun_end)(void *t,uint32_t len);
|
||||
void *t;
|
||||
uint8_t *rx_buff;
|
||||
uint32_t rx_buff_size;
|
||||
void *sem;
|
||||
int in_send;
|
||||
}self_data;
|
||||
|
||||
|
||||
static self_data g_self[LENGTH(g_uartdtb)];
|
||||
|
||||
|
||||
def_find_fun(uart_dtb,g_uartdtb)
|
||||
|
||||
|
||||
|
||||
static int init(uart_def *u,int bsp)
|
||||
{
|
||||
param_check(u);
|
||||
if(u->private_data) return 0;
|
||||
GPIO_InitTypeDef init=GPIO_Initer();
|
||||
USART_InitTypeDef init2=UART_Initer();
|
||||
NVIC_InitTypeDef init3=NVIC_Initer();
|
||||
int index;
|
||||
const uart_dtb *dtb=find(u->name,&index);
|
||||
self_data *self=&g_self[index];
|
||||
self->dtb=dtb;
|
||||
self->irq_fun=0;
|
||||
self->t=0;
|
||||
self->sem=rt_sem_create(u->name,1,RT_IPC_FLAG_FIFO);
|
||||
self->in_send=0;
|
||||
{
|
||||
u->private_data=self;
|
||||
dtb->uart_clock_fun(dtb->uart_rcc,ENABLE);
|
||||
if(bsp==0) bsp=dtb->baudrate;
|
||||
init2.USART_BaudRate = bsp;
|
||||
USART_Init(dtb->uart, &init2);
|
||||
USART_Cmd(dtb->uart, ENABLE);
|
||||
|
||||
dtb->gpio_tx_clock_fun(dtb->gpio_tx_rcc,ENABLE);
|
||||
GPIO_PinAFConfig(dtb->gpio_tx_base,dtb->gpio_tx_pin,dtb->gpio_tx_af);
|
||||
init.GPIO_Pin=1<<dtb->gpio_tx_pin;
|
||||
GPIO_Init(dtb->gpio_tx_base,&init);
|
||||
|
||||
dtb->gpio_rx_clock_fun(dtb->gpio_rx_rcc,ENABLE);
|
||||
GPIO_PinAFConfig(dtb->gpio_rx_base,dtb->gpio_rx_pin,dtb->gpio_rx_af);
|
||||
init.GPIO_Pin=1<<dtb->gpio_rx_pin;
|
||||
GPIO_Init(dtb->gpio_rx_base,&init);
|
||||
|
||||
init3.NVIC_IRQChannel = dtb->irq_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=3;
|
||||
init3.NVIC_IRQChannelSubPriority =3;
|
||||
init3.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&init3);
|
||||
USART_ITConfig(dtb->uart, USART_IT_IDLE, ENABLE);
|
||||
|
||||
init3.NVIC_IRQChannel = dtb->dma_tx_irq_channel;
|
||||
init3.NVIC_IRQChannelPreemptionPriority=3;
|
||||
init3.NVIC_IRQChannelSubPriority =3;
|
||||
init3.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&init3);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int dma_rx_init(self_data *self,uint8_t *rx_buff,uint32_t rx_buff_size)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
DMA_InitTypeDef init=DMA_RX_Initer();
|
||||
|
||||
USART_DMACmd(dtb->uart,USART_DMAReq_Rx,ENABLE);
|
||||
DMA_DeInit(dtb->dma_rx_stream);
|
||||
while (DMA_GetCmdStatus(dtb->dma_rx_stream) != DISABLE);
|
||||
RCC_AHB1PeriphClockCmd(dtb->dma_rx_rcc,ENABLE);
|
||||
init.DMA_Channel = dtb->dma_rx_channel;
|
||||
init.DMA_PeripheralBaseAddr = (u32)(&(dtb->uart->DR));
|
||||
init.DMA_Memory0BaseAddr = (u32)rx_buff;
|
||||
DMA_Init(dtb->dma_rx_stream, &init);
|
||||
DMA_Cmd(dtb->dma_rx_stream, DISABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int dma_rx_reset(self_data *self)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
|
||||
DMA_Cmd(dtb->dma_rx_stream, DISABLE);
|
||||
while(DMA_GetCmdStatus(dtb->dma_rx_stream)!=DISABLE);
|
||||
DMA_SetCurrDataCounter(dtb->dma_rx_stream, self->rx_buff_size);
|
||||
DMA_Cmd(dtb->dma_rx_stream, ENABLE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static int dma_tx_init(self_data *self,const uint8_t *tx_buff,uint32_t tx_buff_size)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
DMA_InitTypeDef init=DMA_TX_Initer();
|
||||
|
||||
USART_DMACmd(dtb->uart,USART_DMAReq_Tx,ENABLE);
|
||||
DMA_DeInit(dtb->dma_tx_stream);
|
||||
while (DMA_GetCmdStatus(dtb->dma_tx_stream) != DISABLE);
|
||||
RCC_AHB1PeriphClockCmd(dtb->dma_tx_rcc,ENABLE);
|
||||
init.DMA_Channel = dtb->dma_tx_channel;
|
||||
init.DMA_PeripheralBaseAddr = (u32)(&(dtb->uart->DR));
|
||||
init.DMA_Memory0BaseAddr = (u32)tx_buff;
|
||||
init.DMA_BufferSize=tx_buff_size;
|
||||
DMA_Init(dtb->dma_tx_stream, &init);
|
||||
DMA_Cmd(dtb->dma_tx_stream, ENABLE);
|
||||
DMA_ITConfig(dtb->dma_tx_stream,DMA_IT_TC,ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static int dma_tx_reset(self_data *self,uint8_t *tx_buff,uint32_t tx_buff_size)
|
||||
{
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
|
||||
USART_DMACmd(dtb->uart,USART_DMAReq_Tx,ENABLE);
|
||||
DMA_Cmd(dtb->dma_tx_stream, DISABLE);
|
||||
while(DMA_GetCmdStatus(dtb->dma_tx_stream)!=DISABLE);
|
||||
DMA_ClearFlag(dtb->dma_tx_stream,dtb->dma_tx_tc_flag);
|
||||
DMA_SetCurrDataCounter(dtb->dma_tx_stream, tx_buff_size);
|
||||
dtb->dma_rx_stream->M0AR=(uint32_t)tx_buff;
|
||||
DMA_Cmd(dtb->dma_tx_stream, ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static int deinit(uart_def *u)
|
||||
{
|
||||
param_check(u);
|
||||
if(u->private_data==0) return 0;
|
||||
NVIC_InitTypeDef init3=NVIC_Initer();
|
||||
const uart_dtb *dtb=find(u->name,0);
|
||||
{
|
||||
USART_Cmd(dtb->uart, DISABLE);
|
||||
dtb->uart_clock_fun(dtb->uart_rcc,DISABLE);
|
||||
dtb->gpio_tx_clock_fun(dtb->gpio_tx_rcc,DISABLE);
|
||||
dtb->gpio_rx_clock_fun(dtb->gpio_rx_rcc,DISABLE);
|
||||
init3.NVIC_IRQChannelCmd = DISABLE;
|
||||
NVIC_Init(&init3);
|
||||
USART_ITConfig(dtb->uart, USART_IT_RXNE, DISABLE);
|
||||
rt_sem_delete(((self_data *)u->private_data)->sem);
|
||||
u->private_data=0;
|
||||
}
|
||||
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_data *self=u->private_data;
|
||||
irq_disable();
|
||||
self->irq_fun=irq;
|
||||
self->t=t;
|
||||
irq_enable();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int set_end_irq(uart_def *u,uint8_t *rx_buff,int rx_buff_size,
|
||||
void (*irq)(void *t,uint32_t len),void *t)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
self_data *self=u->private_data;
|
||||
dma_rx_init(self,rx_buff,rx_buff_size);
|
||||
self->rx_buff=rx_buff;
|
||||
self->rx_buff_size=rx_buff_size;
|
||||
dma_rx_reset(self);
|
||||
irq_disable();
|
||||
self->irq_fun_end=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;
|
||||
}
|
||||
|
||||
|
||||
// dma后台发送需要保证数据在发送期间有效
|
||||
static int write(uart_def *u,const uint8_t *b,int len)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
self_data *self=u->private_data;
|
||||
USART_TypeDef *uart=self->dtb->uart;
|
||||
if(1){
|
||||
dma_tx_init(self,b,len);
|
||||
}else{
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
while(!USART_GetFlagStatus(uart,USART_FLAG_TXE));
|
||||
uart->DR=b[i];
|
||||
}
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
// 阻塞发送,此函数结束即可释放内存
|
||||
static int write_block(uart_def *u,const uint8_t *b,int len)
|
||||
{
|
||||
param_check(u);
|
||||
param_check(u->private_data);
|
||||
self_data *self=u->private_data;
|
||||
USART_TypeDef *uart=self->dtb->uart;
|
||||
self->in_send=1;
|
||||
if(1){
|
||||
dma_tx_init(self,b,len);
|
||||
}else{
|
||||
for(int i=0;i<len;i++)
|
||||
{
|
||||
while(!USART_GetFlagStatus(uart,USART_FLAG_TXE));
|
||||
uart->DR=b[i];
|
||||
}
|
||||
self->in_send=0;
|
||||
}
|
||||
while(self->in_send){}
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static inline void self_irq(self_data *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
|
||||
if(USART_GetFlagStatus(dtb->uart,USART_FLAG_RXNE))
|
||||
{
|
||||
if(self->irq_fun){
|
||||
self->irq_fun(self->t,dtb->uart->DR);
|
||||
}
|
||||
}
|
||||
else if(USART_GetFlagStatus(dtb->uart,USART_FLAG_TC))
|
||||
{
|
||||
USART_ClearFlag(dtb->uart,USART_FLAG_TC);
|
||||
}
|
||||
else if(USART_GetFlagStatus(dtb->uart,USART_FLAG_IDLE))
|
||||
{
|
||||
USART_ReceiveData(dtb->uart);
|
||||
if(self->irq_fun_end){
|
||||
self->irq_fun_end(self->t,self->rx_buff_size-DMA_GetCurrDataCounter(dtb->dma_rx_stream));
|
||||
}
|
||||
dma_rx_reset(self);
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
static inline void self_dma_irq(self_data *self)
|
||||
{
|
||||
rt_interrupt_enter();
|
||||
const uart_dtb *dtb=self->dtb;
|
||||
if(DMA_GetFlagStatus(dtb->dma_tx_stream,dtb->dma_tx_tc_flag))
|
||||
{
|
||||
DMA_ClearFlag(dtb->dma_tx_stream,dtb->dma_tx_tc_flag);
|
||||
DMA_Cmd(dtb->dma_tx_stream, DISABLE);
|
||||
self->in_send=0;
|
||||
}
|
||||
rt_interrupt_leave();
|
||||
}
|
||||
|
||||
void USART1_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[0];
|
||||
self_irq(self);
|
||||
}
|
||||
void USART2_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[1];
|
||||
self_irq(self);
|
||||
}
|
||||
void USART3_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[2];
|
||||
self_irq(self);
|
||||
}
|
||||
void UART4_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[3];
|
||||
self_irq(self);
|
||||
}
|
||||
void UART5_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[4];
|
||||
self_irq(self);
|
||||
}
|
||||
void USART6_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[5];
|
||||
self_irq(self);
|
||||
}
|
||||
|
||||
void DMA2_Stream7_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[0];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
void DMA1_Stream6_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[1];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
void DMA1_Stream3_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[2];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
void DMA1_Stream4_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[3];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
void DMA1_Stream7_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[4];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
void DMA2_Stream6_IRQHandler(void)
|
||||
{
|
||||
self_data *self=&g_self[5];
|
||||
self_dma_irq(self);
|
||||
}
|
||||
|
||||
|
||||
uart_init_export(uart1,init,deinit,set_irq,set_end_irq,read,write,0)
|
||||
uart_init_export(uart2,init,deinit,set_irq,set_end_irq,read,write,0)
|
||||
uart_init_export(uart3,init,deinit,set_irq,set_end_irq,read,write,0)
|
||||
uart_init_export(uart5,init,deinit,set_irq,set_end_irq,read,write,0)
|
||||
uart_init_export(uart6,init,deinit,set_irq,set_end_irq,read,write,0)
|
||||
uart_init_export(uart4,init,deinit,set_irq,set_end_irq,read,write_block,0)
|
||||
|
||||
|
@@ -1,26 +0,0 @@
|
||||
|
||||
|
||||
|
||||
#ifndef if_uart_dma_h__
|
||||
#define if_uart_dma_h__
|
||||
|
||||
#include "stdint.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user