#include "usart_debug.h"
#include <stdio.h>
//#define UART3_RX_LEN 256 /* 单个缓存区字节数 */
#define UATR3_DATA_ADDRESS ((uint32_t)0x40004C04)
uint8_t UART3_RX_BUF[UART3_RX_LEN * 2]; /* 双接收缓冲区 */
uint8_t UART3_RX_STAT = 0; /* 接受状态 0x01:已接收到数据 0x03:接收缓冲区半满 0x07:接收缓冲区全满 */
uint32_t UART3_RX_NUM = 0; /* 接收到的数据个数 */
/* DMA接收缓冲区 */
uint8_t dma_buffer[256];
/* 待处理串口数据缓冲区 */
uint8_t rx0_date_buf[256];
/* 待处理数据个数:大于1为有数据待处理,0为没有数据待处理*/
uint32_t USART_RX_NUM = 0;
#if 1
void Usart3_Config(void)
{
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
usart_deinit(USART0);
/* connect port to USARTx_Tx */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/* connect port to USARTx_Rx */
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
nvic_irq_enable(USART0_IRQn, 0, 0);
usart_interrupt_enable(USART0, USART_INT_RBNE);
// usart_interrupt_enable(USART0, USART_INT_TBE);
}
/* USART0 中断服务函数
* 参数:无
* 返回值:无 */
/* USART0中断服务程序 */
uint8_t Ins_Flag = 0;
uint8_t show_Flag1_BRUSH = RESET;
uint8_t show_Flag2_SIDE_MOTOR = RESET;
uint8_t show_Flag3_FUN_MOTOR = RESET;
uint8_t show_Flag4_MB_MOTOR = RESET;
uint8_t show_Flag5_CHARGER_MOTOR = RESET;
uint8_t show_Flag6_DOWN_MOTOR = RESET;
uint8_t show_Flag7_BUMP_MOTOR = RESET;
uint8_t show_Flag8_SOFT_MOTOR = RESET;
void USART0_IRQHandler(void)
{
uint8_t tag;
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE))
{
/* read one byte from the receive data register */
tag = (uint8_t)usart_data_receive(USART0);
usart_data_transmit(USART0, (uint8_t)tag);
if (tag == 'S')
{
Ins_Flag = 1;
}
if (Ins_Flag == 1)
{
switch (tag)
{
case 'E':
Ins_Flag = 0;
break;
case '1':
show_Flag1_BRUSH = !show_Flag1_BRUSH;
break;
case '2':
show_Flag2_SIDE_MOTOR = !show_Flag2_SIDE_MOTOR;
break;
case '3':
show_Flag3_FUN_MOTOR = !show_Flag3_FUN_MOTOR;
break;
case '4':
show_Flag4_MB_MOTOR = !show_Flag4_MB_MOTOR;
break;
case '5':
show_Flag5_CHARGER_MOTOR = !show_Flag5_CHARGER_MOTOR;
break;
case '6':
show_Flag6_DOWN_MOTOR = !show_Flag6_DOWN_MOTOR;
break;
case '7':
show_Flag7_BUMP_MOTOR = !show_Flag7_BUMP_MOTOR;
break;
case '8':
show_Flag8_SOFT_MOTOR = !show_Flag8_SOFT_MOTOR;
break;
default:
break;
}
}
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
}
}
#endif
void show_data(void)
{
if (show_Flag1_BRUSH == RESET)
{
}
if (show_Flag2_SIDE_MOTOR == RESET)
{
}
if (show_Flag3_FUN_MOTOR == RESET)
{
}
if (show_Flag4_MB_MOTOR == RESET)
{
}
if (show_Flag5_CHARGER_MOTOR == RESET)
{
}
if (show_Flag6_DOWN_MOTOR == RESET)
{
}
if (show_Flag7_BUMP_MOTOR == RESET)
{
}
if (show_Flag8_SOFT_MOTOR == RESET)
{
}
}
#if 0
void Usart3_Config(void)
{
/* enable USART clock */
rcu_periph_clock_enable(RCU_USART0);
usart_deinit(USART0);
/* connect port to USARTx_Tx */
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
/* connect port to USARTx_Rx */
gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
usart_deinit(USART0);
usart_baudrate_set(USART0, 115200);
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
usart_enable(USART0);
}
/* UART3 中断服务函数
* 参数:无
* 返回值:无 */
/* 串口3中断服务程序 */
void UART3_IRQHandler(void)
{
if (RESET != usart_interrupt_flag_get(UART3, USART_INT_FLAG_IDLE)) //空闲中断
{
usart_interrupt_flag_clear(UART3, USART_INT_FLAG_IDLE); /* 清除空闲中断标志位 */
usart_data_receive(UART3); /* 清除接收完成标志位 */
dma_channel_disable(DMA1, DMA_CH2); /* 关闭DMA传输 */
USART_RX_NUM = sizeof(dma_buffer) - dma_transfer_number_get(DMA1, DMA_CH2);
memcpy(rx0_date_buf, dma_buffer, USART_RX_NUM); /* 转存数据到待处理数据缓冲区*/
rx0_date_buf[USART_RX_NUM] = '\0'; /* 添加字符串结束符 */
/* 重新设置DMA传输 */
dma_memory_address_config(DMA1, DMA_CH2, (uint32_t)dma_buffer);
dma_transfer_number_config(DMA1, DMA_CH2, sizeof(dma_buffer));
dma_channel_enable(DMA1, DMA_CH2); /* 开启DMA传输 */
}
}
#endif
void UART3_Disable(void)
{
usart_deinit(USART0);
usart_disable(USART0);
}
void UART3_DMA_INIT()
{
dma_parameter_struct dma_init_struct; /* DMA配置参数结构体 */
rcu_periph_clock_enable(RCU_GPIOC); /* 使能 GPIOA 时钟 */
rcu_periph_clock_enable(RCU_UART3); /* 使能 USART0 时钟 */
rcu_periph_clock_enable(RCU_DMA1); /* 使能 DMA0 时钟 */
/* connect port to USARTx_Tx */
gpio_init(GPIOC, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_10);
/* connect port to USARTx_Rx */
gpio_init(GPIOC, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
/* USART configure */
usart_deinit(UART3);
usart_baudrate_set(UART3, 115200U);
usart_receive_config(UART3, USART_RECEIVE_ENABLE);
usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
usart_enable(UART3);
/* USART0 DMA 接收配置*/
dma_deinit(DMA1, DMA_CH2);
dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY; /* 外设到内存 */
dma_init_struct.memory_addr = (uint32_t)dma_buffer; /* 设置内存接收基地址 */
dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; /* 内存地址递增 */
dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT; /* 8位内存数据 */
dma_init_struct.number = sizeof(dma_buffer);
dma_init_struct.periph_addr = UATR3_DATA_ADDRESS; /* 外设基地址,USART数据寄存器地址 ((uint32_t)0x40004C08) (uint32_t)USART_DATA(UART3)*/
dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; /* 外设地址不递增 */
dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT; /* 8位外设数据 */
dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH; /* 最高DMA通道优先级 */
dma_init(DMA1, DMA_CH2, dma_init_struct); /* 按照结构体的配置初始化DMA */
dma_circulation_disable(DMA1, DMA_CH2); /* 关闭DMA循环模式 */
dma_memory_to_memory_disable(DMA1, DMA_CH2); /* DMA内存到内存模式不开启 */
dma_channel_enable(DMA1, DMA_CH2); /* 使能DMA传输 */
usart_dma_receive_config(UART3, USART_DENR_ENABLE); /* USART0 DMA接收模式开启 */
nvic_irq_enable(UART3_IRQn, 0, 0); /* USART中断设置,抢占优先级0,子优先级0 */
usart_interrupt_enable(UART3, USART_INT_IDLE); /* 使能USART0空闲中断 */
}