21ic问答首页 - GD32F130的串口+DMA发送
GD32F130的串口+DMA发送
ylmting2023-01-10
按照手册来配置的,但是传输不了数据,不知道啥问题,劳请大家看看
#define ARRAYNUM(arr_name) (uint32_t)(sizeof(arr_name) / sizeof(*(arr_name)))
uint8_t txbuffer[]="\n\rUSART DMA receive and transmit example, please input 10 bytes:\n\r";
#define USART1_DATA_ADDRESS USART_TDATA(USART1) //0x4000 4400 - 0x4000 47FF
void RCU_config(void)
{
rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA时钟
rcu_usart_clock_config(CK_APB2); //选择USART0外设的时钟源
rcu_periph_clock_enable(RCU_USART1) ; //使能USART0外设时钟
}
void NVIC_config(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
//没有使用USART1的中断
}
void GPIO_config(void)
{
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2 );
gpio_output_options_set(GPIOA,GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10 );
}
void USART1_config(void)
{
usart_deinit(USART1); //复位USART0
usart_baudrate_set(USART1,115200); //设置波特率
usart_enable(USART1); //使能USART(UEN=1)
usart_transmit_config(USART1,USART_TRANSMIT_ENABLE); //使能发送器(TEN=1)
usart_receive_config(USART1,USART_RECEIVE_ENABLE); //使能接收器(REN=1)
usart_dma_transmit_config(USART1,USART_DENT_ENABLE);
}
void uart1_dma_txinit(void)
{
dma_parameter_struct dma_init_struct;
/* enable DMA1 */
rcu_periph_clock_enable(RCU_DMA);
//dma_struct_para_init(&dma_init_struct);
dma_deinit(DMA_CH3);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;//设置方向
dma_init_struct.memory_addr = NULL;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = 0;
dma_init_struct.periph_addr = USART1_DATA_ADDRESS; //外设地址
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA_CH3, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA_CH3);
dma_memory_to_memory_disable(DMA_CH3);
dma_channel_enable(DMA_CH3);
}
void usart1_dma_send(uint8_t *buffer,uint16_t len)
{
dma_channel_disable(DMA_CH3);
dma_memory_address_config(DMA_CH3,(uint32_t)buffer);//设置要发送数据的内存地址
dma_transfer_number_config(DMA_CH3,len);//一共发多少个数据
dma_channel_enable(DMA_CH3);
usart_dma_transmit_config(USART1,USART_DENT_ENABLE);//使能串口DMA发送
while(RESET == dma_flag_get(DMA_CH3, DMA_FLAG_FTF));
}
int main(void)
{
RCU_config(); //时钟初始化
NVIC_config(); //中断初始化
GPIO_config(); //GPIO初始化
uart1_dma_txinit();
USART1_config(); //USART1初始化
cycleCounterInit();
while(1)
{
uint32_t last = micros();
usart1_dma_send(txbuffer,ARRAYNUM(txbuffer));
uint32_t now = micros();
_dt = now - last;
}
}
#define ARRAYNUM(arr_name) (uint32_t)(sizeof(arr_name) / sizeof(*(arr_name)))
uint8_t txbuffer[]="\n\rUSART DMA receive and transmit example, please input 10 bytes:\n\r";
#define USART1_DATA_ADDRESS USART_TDATA(USART1) //0x4000 4400 - 0x4000 47FF
void RCU_config(void)
{
rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA时钟
rcu_usart_clock_config(CK_APB2); //选择USART0外设的时钟源
rcu_periph_clock_enable(RCU_USART1) ; //使能USART0外设时钟
}
void NVIC_config(void)
{
nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
//没有使用USART1的中断
}
void GPIO_config(void)
{
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2 );
gpio_output_options_set(GPIOA,GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10 );
}
void USART1_config(void)
{
usart_deinit(USART1); //复位USART0
usart_baudrate_set(USART1,115200); //设置波特率
usart_enable(USART1); //使能USART(UEN=1)
usart_transmit_config(USART1,USART_TRANSMIT_ENABLE); //使能发送器(TEN=1)
usart_receive_config(USART1,USART_RECEIVE_ENABLE); //使能接收器(REN=1)
usart_dma_transmit_config(USART1,USART_DENT_ENABLE);
}
void uart1_dma_txinit(void)
{
dma_parameter_struct dma_init_struct;
/* enable DMA1 */
rcu_periph_clock_enable(RCU_DMA);
//dma_struct_para_init(&dma_init_struct);
dma_deinit(DMA_CH3);
dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;//设置方向
dma_init_struct.memory_addr = NULL;
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
dma_init_struct.number = 0;
dma_init_struct.periph_addr = USART1_DATA_ADDRESS; //外设地址
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
dma_init(DMA_CH3, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA_CH3);
dma_memory_to_memory_disable(DMA_CH3);
dma_channel_enable(DMA_CH3);
}
void usart1_dma_send(uint8_t *buffer,uint16_t len)
{
dma_channel_disable(DMA_CH3);
dma_memory_address_config(DMA_CH3,(uint32_t)buffer);//设置要发送数据的内存地址
dma_transfer_number_config(DMA_CH3,len);//一共发多少个数据
dma_channel_enable(DMA_CH3);
usart_dma_transmit_config(USART1,USART_DENT_ENABLE);//使能串口DMA发送
while(RESET == dma_flag_get(DMA_CH3, DMA_FLAG_FTF));
}
int main(void)
{
RCU_config(); //时钟初始化
NVIC_config(); //中断初始化
GPIO_config(); //GPIO初始化
uart1_dma_txinit();
USART1_config(); //USART1初始化
cycleCounterInit();
while(1)
{
uint32_t last = micros();
usart1_dma_send(txbuffer,ARRAYNUM(txbuffer));
uint32_t now = micros();
_dt = now - last;
}
}
您需要登录后才可以回复 登录 | 注册