[STM32F4] HAL_UART_RxCpltCallback串口接收中断回调不正常

[复制链接]
1167|19
 楼主| 是你的乱码 发表于 2022-11-17 14:20 | 显示全部楼层 |阅读模式
AC, LTC, UART, ck, tc

背景:【MXCube+FreeRTOS+STM32F407】

为了将串口数据上下文分离。在回调函数中,使用消息队列将接收的字符发至队列。在任务线程中进行处理数据。


void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

  //taskENTER_CRITICAL();

  /* Prevent unused argument(s) compilation warning */

  //UNUSED(huart);

  if (huart->Instance == USART2) {

    osMessageQueuePut(remoteQueueHandle, (uint8_t*)&remoteTmpBuffer[0], 0, 0);

  } else if(huart->Instance == USART6) {

    osMessageQueuePut(ledQueueHandle, (uint8_t*)&ledTmpBuffer[0], 0, 0);

  } else if(huart->Instance == USART3) {

          osMessageQueuePut(lockQueueHandle, (uint8_t*)&lockTmpBuffer[0], 0, 0);

  }

  //taskEXIT_CRITICAL();

}



 楼主| 是你的乱码 发表于 2022-11-17 14:21 | 显示全部楼层
Step1分析:

osMessageQueuePut 使用在中断,需要设置 timeout 参数为0;
774666375d2f97765c.png
 楼主| 是你的乱码 发表于 2022-11-17 14:22 | 显示全部楼层
Step2分析:

osMessageQueuePut 在中断中使用最终调用为:xQueueGenericSendFromISR
 楼主| 是你的乱码 发表于 2022-11-17 14:23 | 显示全部楼层
在单步追踪中:void vPortValidateInterruptPriority( void ),最终在此函数ASSERT断言

  1.         void vPortValidateInterruptPriority( void )
  2.         {
  3.         uint32_t ulCurrentInterrupt;
  4.         uint8_t ucCurrentPriority;

  5.                 /* Obtain the number of the currently executing interrupt. */
  6.                 ulCurrentInterrupt = vPortGetIPSR();

  7.                 /* Is the interrupt number a user defined interrupt? */
  8.                 if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
  9.                 {
  10.                         /* Look up the interrupt's priority. */
  11.                         ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];

  12.                         /* The following assertion will fail if a service routine (ISR) for
  13.                         an interrupt that has been assigned a priority above
  14.                         configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
  15.                         function.  ISR safe FreeRTOS API functions must *only* be called
  16.                         from interrupts that have been assigned a priority at or below
  17.                         configMAX_SYSCALL_INTERRUPT_PRIORITY.
  18.                         Numerically low interrupt priority numbers represent logically high
  19.                         interrupt priorities, therefore the priority of the interrupt must
  20.                         be set to a value equal to or numerically *higher* than
  21.                         configMAX_SYSCALL_INTERRUPT_PRIORITY.
  22.                         Interrupts that        use the FreeRTOS API must not be left at their
  23.                         default priority of        zero as that is the highest possible priority,
  24.                         which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
  25.                         and        therefore also guaranteed to be invalid.
  26.                         FreeRTOS maintains separate thread and ISR API functions to ensure
  27.                         interrupt entry is as fast and simple as possible.
  28.                         The following links provide detailed information:
  29.                         http://www.freertos.org/RTOS-Cortex-M3-M4.html
  30.                         http://www.freertos.org/FAQHelp.html */
  31.                         configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
  32.                 }

  33.                 /* Priority grouping:  The interrupt controller (NVIC) allows the bits
  34.                 that define each interrupt's priority to be split between bits that
  35.                 define the interrupt's pre-emption priority bits and bits that define
  36.                 the interrupt's sub-priority.  For simplicity all bits must be defined
  37.                 to be pre-emption priority bits.  The following assertion will fail if
  38.                 this is not the case (if some bits represent a sub-priority).
  39.                 If the application only uses CMSIS libraries for interrupt
  40.                 configuration then the correct setting can be achieved on all Cortex-M
  41.                 devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
  42.                 scheduler.  Note however that some vendor specific peripheral libraries
  43.                 assume a non-zero priority group setting, in which cases using a value
  44.                 of zero will result in unpredictable behaviour. */
  45.                 configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
  46.         }

 楼主| 是你的乱码 发表于 2022-11-17 14:24 | 显示全部楼层
configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );

注释明白的讲明, 当前中断优先级大于等于FREERTOS系统能调用的最大优先级,起初以为FreeRTOS配置没对,将Timer、Systick等按MXCube生成的优先级进行调整,无作用。
 楼主| 是你的乱码 发表于 2022-11-17 14:26 | 显示全部楼层
FreeRTOS 控制的优先级范围:5~15

Timer:2 -> 5

Systick :0 -> 5
 楼主| 是你的乱码 发表于 2022-11-17 14:27 | 显示全部楼层
Step3分析:

既然网上搜到优先级不对,会不会是串口优先级 高于 FreeRTOS的控制范围?
614166375d457db3f9.png
 楼主| 是你的乱码 发表于 2022-11-17 14:28 | 显示全部楼层
MXCube里将USART相关中断优先级降低至6,问题解决
 楼主| 是你的乱码 发表于 2022-11-17 14:30 | 显示全部楼层
总结:MXCude好东西,解耦了需要对底层寄存器的理解,但对原理不稍微掌握,还是会出些难以预测的问题,在网上还不一定有答案。
OKAKAKO 发表于 2024-6-25 15:42 | 显示全部楼层
回调不正常一般是回调函数调用有问题
帛灿灿 发表于 2024-12-1 08:11 | 显示全部楼层

MinUnit 是一个 用 C 语言编写的极其简单的单元测试框架
Bblythe 发表于 2024-12-1 09:14 | 显示全部楼层

功率开关和整流器的交流电流环路包含非常高的PWM开关电源典型的梯形电流波形
周半梅 发表于 2024-12-1 11:10 | 显示全部楼层

电源电压处于1.6V到5.5V之间
Pulitzer 发表于 2024-12-1 12:13 | 显示全部楼层

无法提供开关电源所需的脉冲电流
童雨竹 发表于 2024-12-1 14:09 | 显示全部楼层

单片机的外部都连接有象电池等电源部分
Wordsworth 发表于 2024-12-1 15:12 | 显示全部楼层

电源内部都有四个电流环路
Clyde011 发表于 2024-12-1 16:15 | 显示全部楼层

作为电路的共同电位参考点
公羊子丹 发表于 2024-12-1 17:08 | 显示全部楼层

主时钟振荡器主要用作CPU的工作时钟
万图 发表于 2024-12-1 18:11 | 显示全部楼层

传送直流信号的引线
Uriah 发表于 2024-12-1 19:14 | 显示全部楼层

Buck(或降压)变换器功率部分布局
您需要登录后才可以回帖 登录 | 注册

本版积分规则

35

主题

490

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部