void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
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_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
//////////////////中断的相关配置////////////////////////////
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //允许中断方式
USART_Cmd(USART2, ENABLE);
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40004404;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART2_RECEIVE_DATA;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 512;
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_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Channel6, ENABLE);
USART_ITConfig(USART2, USART_IT_IDLE , ENABLE);
}
void USART2_IRQHandler(void)
{
// uint8_t data;
int i = 0;
// static u8 usart2_i;
int DATA_LEN;
OSIntEnter();
// if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
// {
// USART_ClearITPendingBit(USART2,USART_IT_RXNE);
// data = USART_ReceiveData ( USART2 ) ;
// // USART_SendData(USART1, data);
// // printf("%c",data);
// // usart2_buffer[usart2_i] = data;
// }
if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel6, DISABLE);
i = USART2->DR;
//防止不必要的错误出现
if( DMA_GetCurrDataCounter(DMA1_Channel6) > 512)
{
return;
}
DATA_LEN = 512 - DMA_GetCurrDataCounter(DMA1_Channel6);
for(i=0;i < DATA_LEN;i ++ )
{
USART2_SEND_DATA[i] = USART2_RECEIVE_DATA[i];
}
usart2_received_data(USART2_SEND_DATA);
//printf("%s",USART2_SEND_DATA);
DMA1_Channel6->CNDTR = 512;
DMA_Cmd(DMA1_Channel6, ENABLE);
USART_ClearITPendingBit(USART2, USART_IT_IDLE);
}
memset(USART2_SEND_DATA,'\0' ,sizeof(USART2_SEND_DATA));
memset(USART2_RECEIVE_DATA,'\0' ,sizeof(USART2_RECEIVE_DATA));
OSIntExit();
}
上一篇:stm32的RTC操作
下一篇:stm32函数放入段section中
推荐阅读最新更新时间:2024-03-16 15:13