开发环境:IAR for ARM 7.80.2
问题描述:1、使用CMSIS库,开了串口1和串口2,除波特率不同,其他都一样。串口2工作正常,串口1接收数据出错。
2.使用HAL库,串口1和串口2都正常。
排除了硬件的故障,所以问题肯定还是出在软件上面。但是比较两者不同,貌似又没有什么不同。调了好几天,实在没头绪。
代码如下:
CMSIS下:/**
* [url=home.php?mod=space&uid=159083]@brief[/url] uart1 init
* @param none
* @retval None
*/
void USART1_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIOInitType;
USART_InitTypeDef USART_InitStructure;
//RCC_USARTCLKConfig(RCC_USART1CLK_PCLK);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
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(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
GPIOInitType.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIOInitType.GPIO_Speed = GPIO_Speed_50MHz;
GPIOInitType.GPIO_Mode = GPIO_Mode_AF;
GPIOInitType.GPIO_OType = GPIO_OType_PP;
GPIOInitType.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIOInitType);
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief uart2 init
* @param none
* @retval None
*/
void USART2_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIOInitType;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
USART_InitStructure.USART_BaudRate = 115200;
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);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
GPIOInitType.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIOInitType.GPIO_Speed = GPIO_Speed_50MHz;
GPIOInitType.GPIO_Mode = GPIO_Mode_AF;
GPIOInitType.GPIO_OType = GPIO_OType_PP;
GPIOInitType.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIOInitType);
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief uart1 interrupt
* @param None
* @retval None
*/
void USART1_IRQHandler(void)
{
U8 dat = 0;
if (USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_ORE);
dat = USART_ReceiveData(USART1);
if(!QueueIsFull(&BoardRxQueue)) //队列不满
{
PushInQueue_Q(&BoardRxQueue, dat);
TIM6->CNT = 0; //定时器计数设为0
if(g_devWorkStatus.bRecvDataUart1 == 0)//打开定时器。如果定时器产生了中断,则说明一帧数据接收完毕了
TIM6_Set(1);
}
else
g_devWorkStatus.bRecvDataUart1 = 1;
}
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
dat = USART_ReceiveData(USART1);
if(!QueueIsFull(&BoardRxQueue)) //队列不满
{
PushInQueue_Q(&BoardRxQueue, dat);
TIM6->CNT = 0; //定时器计数设为0
if(g_devWorkStatus.bRecvDataUart1 == 0)//打开定时器。如果定时器产生了中断,则说明一帧数据接收完毕了
TIM6_Set(1);
}
else
g_devWorkStatus.bRecvDataUart1 = 1;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
/**
* @brief uart2 interrupt
* @param None
* @retval None
*/
void USART2_IRQHandler(void)
{
U8 dat = 0;
if (USART_GetFlagStatus(USART2, USART_FLAG_ORE) != RESET)
{
USART_ClearITPendingBit(USART2,USART_IT_ORE);
dat = USART_ReceiveData(USART2);
if(!QueueIsFull(&MG2618RxQueue)) //队列不满
{
PushInQueue_Q(&MG2618RxQueue, dat);
TIM7->CNT = 0; //定时器计数设为0
if(g_devWorkStatus.bRecvDataUart2 == 0)//打开定时器。如果定时器产生了中断,则说明一帧数据接收完毕了
TIM7_Set(1);
}
else
g_devWorkStatus.bRecvDataUart2 = 1;
}
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
dat = USART_ReceiveData(USART2);
if(!QueueIsFull(&MG2618RxQueue)) //队列不满
{
PushInQueue_Q(&MG2618RxQueue, dat);
TIM7->CNT = 0; //定时器计数设为0
if(g_devWorkStatus.bRecvDataUart2 == 0)//打开定时器。如果定时器产生了中断,则说明一帧数据接收完毕了
TIM7_Set(1);
}
else
g_devWorkStatus.bRecvDataUart2 = 1;
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
} 复制代码
HAL库下的代码:
/* USART1 init function */
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 9600;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
__HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);
// if(HAL_UART_Receive_IT(&huart1,aRxBuffer,1)!=HAL_OK)
// {
// Error_Handler();
// }
NVIC_SetPriority(USART1_IRQn, 1);
HAL_NVIC_EnableIRQ(USART1_IRQn);
}
/* USART2 init function */
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
// if(HAL_UART_Receive_IT(&huart2,aRxBuffer,1)!=HAL_OK)
// {
// Error_Handler();
// }
NVIC_SetPriority(USART2_IRQn, 0);
HAL_NVIC_EnableIRQ(USART2_IRQn);
}
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(huart->Instance==USART1)
{
/* USER CODE BEGIN USART1_MspInit 0 */
/* USER CODE END USART1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART1_CLK_ENABLE();
/**USART1 GPIO Configuration
PA9 ------> USART1_TX
PA10 ------> USART1_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART1_MspInit 1 */
/* USER CODE END USART1_MspInit 1 */
}
else if(huart->Instance==USART2)
{
/* USER CODE BEGIN USART2_MspInit 0 */
/* USER CODE END USART2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART2_CLK_ENABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA3 ------> USART2_RX
*/
GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF1_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN USART2_MspInit 1 */
/* USER CODE END USART2_MspInit 1 */
}
}
void USART1_IRQHandler(void)
{
if((USART1->ISR & 0x20)==0x20)
{
RingBufWriteOne(&mowerRingBuffObj,USART1->RDR);
}
// else
// {
//
// }
//HAL_UART_IRQHandler(&huart1);
}
extern UART_HandleTypeDef huart2;
extern tRingBufObject GprsRingBuffObj;
void USART2_IRQHandler(void)
{
//uint8_t ret=0;
if((USART2->ISR & 0x20)==0x20)
{
RingBufWriteOne(&GprsRingBuffObj,USART2->RDR);
}
// else
// {
//
// }
//ret=RingBufReadOne(&GprsRingBuffObj);
//HAL_UART_Transmit(&huart2,&ret,1,20);
//HAL_UART_IRQHandler(&huart2);
} 复制代码