废话不多,直接上代码
#define SENDBUFF_SIZE 128
#define RECEBUFF_SIZE 128
#define USART1_DR_Base 0x40013804
uint8_t SendBuff[SENDBUFF_SIZE];
uint8_t RecvBuff[SENDBUFF_SIZE];
void DMAUart1Tx_Init()
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_Cmd (DMA1_Channel4,ENABLE);
// DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
}
void DMAUsar1Rx_Init()
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RecvBuff;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = RECEBUFF_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel5,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
}
void USART1_Init(u32 My_BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure ;
USART_InitTypeDef USART_InitStructure ;
NVIC_InitTypeDef NVIC_InitStructure ;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING ;
GPIO_Init(GPIOA,&GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE ;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART1);
USART_InitStructure.USART_BaudRate = My_BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx ;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
DMAUart1Tx_Init();
DMAUsar1Rx_Init();
USART_Cmd(USART1 , ENABLE);
}
void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DATA(Data));
/* Transmit Data */
USARTx->DR = (Data & (uint16_t)0x01FF);
}
void Usart_SendString(USART_TypeDef *USARTx, uint8_t *str, uint16_t len)
{
unsigned short count = 0;
for(; count < len; count++)
{
USART_SendData(USARTx, *str++);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
}
}
void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx, uint16_t len)
{
DMA_Cmd(DMA_CHx, DISABLE );
DMA_SetCurrDataCounter(DMA_CHx, len);
DMA_Cmd(DMA_CHx, ENABLE);
}
uint16_t uart_read( uint8_t* buf, uint16_t read_len)
{
uint16_t Usart1_Rec_Cnt = 0;
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
USART_ReceiveData(USART1);
Usart1_Rec_Cnt = RECEBUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);
Usart_SendString(USART1, RecvBuff, Usart1_Rec_Cnt);
memcpy(buf, RecvBuff, Usart1_Rec_Cnt);
USART_ClearITPendingBit(USART1,USART_IT_IDLE);
MYDMA_Enable(DMA1_Channel5, RECEBUFF_SIZE);
}
return Usart1_Rec_Cnt;
}
void USART1_IRQHandler(void)
{
uart_read(RX1_Receive, RX1_Receive_LEN);
}
uint16_t uart_puts(const uint8_t *src, uint16_t len)
{
const uint8_t *pstr_tmp = src;
uint16_t len_temp = len;
MYDMA_Enable(DMA1_Channel4, len_temp);
for(int i = 0;i < len_temp; i++)
{
SendBuff[i] = *pstr_tmp++;
}
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
}
int main(void)
{
SystemInit();
USART1_Init(9600);
for(;;)
{
}
}