STM32 UART接收无法正常运行

时间:2018-10-04 13:15:06

标签: stm32 cubemx

我整天整夜都在努力弄清为什么我的接收功能无法正常工作。当我最初开始该项目时,情况还不错(几天前)。从那以后,我做了更多的工作,但现在不起作用了。

我现在甚至在CubeMX上创建了一个新项目,仅设置了jtag和uart5,但仍然没有接收功能。我可以确认董事会在我从事的许多较早的项目中都能正常工作。

我相信在构建过程中会发生一些事情,但我无法弄清楚。

我正在使用STM32F207芯片。

下面是我编写的用于测试UART功能的非常基本的程序。我可以完美传输,只是没有接收功能起作用。

我什至在主循环中添加了一个简单的“ HAL_UART_Receive()”,超时时间很长,它完全跳过了整个功能!

我也已经强制激活UART中断,但没有做任何事情。

我今天早些时候通过先前的提交恢复了该项目,并且构建运行得很好。然后它在编译后停止工作。我尝试再次还原它,第二次它没有解决问题。

将数据发送到MCU时,会触发RXNE标志,并且数据寄存器会更改,而不是更改为正在远程发送的任何内容。

我的想法不多了!

Main.c:

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f2xx_hal.h"
#include "usart.h"
#include "gpio.h"
#include "string.h"

/* Private variables ---------------------------------------------------------*/

extern uint8_t pc_uart_receive_flag;
extern char pc_buff[8];

char sys_ready[] = "UART Transmit test\r\n";

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);

/**
  * @brief  The application entry point.
  *
  * @retval None
  */
int main(void)
{

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();


  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_UART5_Init();

  /*
   * FORCING THESE ACTIVE AND STILL NOTHING
   */
  /* UART5 interrupt Init */
  HAL_NVIC_SetPriority(UART5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(UART5_IRQn);

  HAL_UART_Transmit_IT(&huart5, (uint8_t*)sys_ready, strlen(sys_ready));

  /* Infinite loop */
  while (1)
  {


      /*
       * NOT EVEN THIS WORKS!
       */
      HAL_UART_Receive(&huart5, (uint8_t*)pc_buff, strlen(pc_buff), 100000);

      //PC receive flag
      if(pc_uart_receive_flag==1){

            /*
             * BEGIN DEBUG
             */

            HAL_UART_Transmit_IT(&huart5, (uint8_t*)pc_buff, strlen(pc_buff)); // Return value that is saved

            /*
             * END DEBUG
             */

            pc_uart_receive_flag=0; // Reset flag

        }

  }

}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Initializes the CPU, AHB and APB busses clocks 
    */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time 
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick 
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}


/**
  * @brief  This function is executed in case of error occurrence.
  * @param  file: The file name as string.
  * @param  line: The line in file as a number.
  * @retval None
  */
void _Error_Handler(char *file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

usart.c:

/* Includes ------------------------------------------------------------------*/
#include "usart.h"

#include "gpio.h"

UART_HandleTypeDef huart5;

/* UART5 init function */
void MX_UART5_Init(void)
{

  huart5.Instance = UART5;
  huart5.Init.BaudRate = 115200;
  huart5.Init.WordLength = UART_WORDLENGTH_8B;
  huart5.Init.StopBits = UART_STOPBITS_1;
  huart5.Init.Parity = UART_PARITY_NONE;
  huart5.Init.Mode = UART_MODE_TX_RX;
  huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart5.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart5) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(uartHandle->Instance==UART5)
  {
    /* UART5 clock enable */
    __HAL_RCC_UART5_CLK_ENABLE();

    /**UART5 GPIO Configuration    
    PC12     ------> UART5_TX
    PD2     ------> UART5_RX 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    /* UART5 interrupt Init */
    HAL_NVIC_SetPriority(UART5_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(UART5_IRQn);
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==UART5)
  {
    /* Peripheral clock disable */
    __HAL_RCC_UART5_CLK_DISABLE();

    /**UART5 GPIO Configuration    
    PC12     ------> UART5_TX
    PD2     ------> UART5_RX 
    */
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_12);

    HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);

    /* UART5 interrupt Deinit */
    HAL_NVIC_DisableIRQ(UART5_IRQn);
  }
}

stm32f2xx_it.c(中断):

/* Includes ------------------------------------------------------------------*/
#include "stm32f2xx_hal.h"
#include "stm32f2xx.h"
#include "stm32f2xx_it.h"
#include "string.h"

/* External variables --------------------------------------------------------*/
extern UART_HandleTypeDef huart5;


uint16_t pc_uart_receive_flag=0;
char pc_buff[8];

/******************************************************************************/
/*            Cortex-M3 Processor Interruption and Exception Handlers         */ 
/******************************************************************************/

/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
}

/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
  while (1)
  {
  }
}

/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
  while (1)
  {
  }
}

/**
* @brief This function handles Pre-fetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
  while (1)
  {
  }
}

/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
  while (1)
  {
  }
}

/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
}

/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
}

/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
}

/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
}

/******************************************************************************/
/* STM32F2xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f2xx.s).                    */
/******************************************************************************/

/**
* @brief This function handles UART5 global interrupt.
*/
void UART5_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart5);

  HAL_UART_Receive_IT(&huart5, (uint8_t*) pc_buff, strlen(pc_buff));

}


/**
* @brief This function handles the callback for the UART channels.
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){


    // Callback triggered by UART5
    if (huart->Instance == UART5){
        pc_uart_receive_flag=1;
    }
}

1 个答案:

答案 0 :(得分:0)

所以!经过大量的头部抓挠之后,我发现了一个可行的解决方案! 这可能不是“最佳”方法,但它是可行的方法,并且我确定该方法对我正在做的项目应该没问题。 现在,在我的ISR文件(stm32f2xx_it.c“)中:

/**
* @brief This function handles UART5 global interrupt.
*/
void UART5_IRQHandler(void)
{
  HAL_UART_IRQHandler(&huart5);


  if(HAL_UART_Receive_IT(&huart5, (uint8_t*) pc_buff, PC_BUFF_LENGTH)==HAL_BUSY){

      // Add some sort of flag in this process for errors
  }

}


/**
* @brief This function handles the callback for the UART channels.
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){


    // Callback triggered by UART5
    if (huart->Instance == UART5){
        pc_uart_receive_flag=1;
    }
}

这是通过检查if语句中的“ HAL_BUSY”来假设数据正在输入中。我对嵌入式编程充满信心,可以更好地实现UART ISR。在那之前,我将使用我所知道的东西。