为什么我的HAL_UART IT捕获回调例程不会触发?

时间:2019-07-11 12:21:49

标签: c embedded stm32 uart

我正在尝试通过UART在两个STM32F103 MCU之间建立通信。我正在使用STMCubeMX构建外围设备初始化。我已将逻辑上的MCU命名为MASTER和SLAVE。从UART配置为仅发送,而MASTER uart仅接收。我正在使用HAL驱动程序在AtollicTRUEstudio IDE中对MCU进行编程。我想发送uint32_t值,buffer_USART1_rx被声明为易失性uint8_t buffer_USART1_rx [10]。基本上SLAVE UART发送是由Systick定时器每1秒触发一次的,而MASTER UART是在IT模式下定义的,并且一旦中断发生,它就会读取发送的值。

我将示波器探头连接到MASTER的PA10 RX引脚,我发现UART信号很好并且正在通过电线传输。但是要传输的值始终为0,并且永远不会激活HAL_UART_RxCpltCallback中的断点。由于示波器信号正确,因此我认为这是软件问题。 Image of RX PA10 pin of MASTER stm32

这是来自MASTER STM(代码位于主文件中)

UART初始化:

static void MX_USART1_UART_Init(void)
{

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

}
static void MX_NVIC_Init(void)
{

  /* USART1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(USART1_IRQn);

}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
     __NOP();
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    /*UART1 water level from SLAVE */
 if(huart->Instance==USART1)
 {

     water_level=getUSART1();

     /* Water level change triggers LCD refresh */
     if(water_level_prev!=water_level)
     {
         lcd_screen_refresh=true;
     }
     water_level_prev=water_level;

 }
 else
 {
     __NOP();
 }
 /*UART2 target level from NANOPI */
if(huart->Instance==USART2)
 {
    target_level_pi=getUSART2();

    /* Target level change triggers LCD refresh */
    if(target_level_pi!=target_level_prev)
    {
        lcd_screen_refresh=true;
    }

 }
else
{
     __NOP();
}
}

UART反序列化功能:

uint32_t getUSART1()
{

    uint32_t num=0;

    num |= buffer_USART1_rx[0] << 24;
    num |= buffer_USART1_rx[1] << 16;
    num |= buffer_USART1_rx[2] << 8;
    num |= buffer_USART1_rx[3];

    return num;
}

在IT模式下UART的主文件初始化中:

/* Initialize TIM/UART interrupts */
  HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_1);
  HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_2);
  HAL_UART_Receive_IT(&huart1, buffer_USART1_rx, 4);

从MCU配置:

// This is in while loop
      if(send_USART==true)
      {
          buffer[0] = test ;
          buffer[1] = test >>8;
          buffer[2] = test >> 16;
          buffer[3] = test >> 24;
          HAL_UART_Transmit(&huart1,buffer,4,2000);

      }
      else
      {
          __NOP();
      }

// Callback
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
    send_USART=false;
}

//Systick timer triggers UART transmit every 1 second

void HAL_SYSTICK_Callback()
{
    sys_timer++;
    if(sys_timer>=1000)
    {
        sys_timer=0;
        send_USART=true;

    }
    else
    {
        __NOP();
    }
}

//Global declaration of variables used

/* Timer variables */
uint8_t buffer[10];
volatile uint32_t sys_timer=0;
uint32_t test=10;

/* Boolean variables */
bool send_USART=false;

// UART initialization

/* USART1 init function */
static void MX_USART1_UART_Init(void)
{

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

}
static void MX_NVIC_Init(void)
{

  /* USART1_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(USART1_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(USART1_IRQn);
}

我希望接收到的值为10(因为我正在序列化SLAVE stm上的“ test”变量,并在反序列化时将其发送到MASTER stm上)

1 个答案:

答案 0 :(得分:0)

没关系,我已经找到了问题所在,并且是什么原因造成了

void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */

  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
    if(huart1.RxState==HAL_UART_STATE_READY)
    {
        HAL_UART_Receive_IT(&huart1,buffer_USART1_rx,4);
        uart1_received=4; //flag da je primljen podatak
        __HAL_UART_FLUSH_DRREGISTER(&huart1);
        __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
    }
    else
    {
        __NOP();
    }
  /* USER CODE END USART1_IRQn 1 */
}

用于处理的代码位于HAL_UART_IRQHandler(&huart1);之上。这就是为什么它只接收一次数据的原因。当我像上面的代码一样在下面复制它时,一切正常。