0

我花了一整天和今晚试图找出我的接收功能不起作用的原因。当我最初开始这个项目时它很好(这是几天前)。从那以后我已经做了更多的工作,现在它不起作用了。

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

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

我正在使用 STM32F207 芯片。

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

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

我也强制激活了 UART 中断,但这并没有做任何事情。

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

当向 MCU 发送数据时,触发 RXNE 标志并且数据寄存器被更改,但是不会更改为远程发送的任何内容。

我的想法不多了!

主要.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;
    }
}
4

1 回答 1

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。在那之前,我将使用我所知道的。

于 2018-10-08T01:24:28.533 回答