天天看点

STM32CubeMX快速开发系列-Freerots移植STM32CubeMX快速开发系列-Freerots移植

[TOC]

STM32CubeMX快速开发系列-Freerots移植

CubeMX配置

STM32CubeMX快速开发系列-Freerots移植STM32CubeMX快速开发系列-Freerots移植

保存工程,生成代码以后,打开freertos.c,在StartDefaultTask,中添加代码

void StartDefaultTask(void const * argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    LED0_TOGGLE;
    osDelay(1000);
  }
  /* USER CODE END StartDefaultTask */
}           

将程序烧入开发板,可以看到LED开始闪烁。

任务创建分析

打开freertos.c文件,创建启动任务的代码如下

osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

/// Create a Thread Definition with function, priority, and stack requirements.
/// \param         name         name of the thread function.
/// \param         priority     initial priority of the thread function.
/// \param         instances    number of possible thread instances.
/// \param         stacksz      stack size (in bytes) requirements for the thread function.
#define osThreadDef(name, thread, priority, instances, stacksz)  \
const osThreadDef_t os_thread_def_##name = \
{ #name, (thread), (priority), (instances), (stacksz), NULL, NULL }

/// Access a Thread definition.
/// \param         name          name of the thread definition object.
/// \note CAN BE CHANGED: The parameter to \b osThread shall be consistent but the
///       macro body is implementation specific in every CMSIS-RTOS.
#define osThread(name)  \
&os_thread_def_##name           

上面两行1~2代码完成任务创建,返回一个任务句柄

typedef TaskHandle_t osThreadId;           

扩展后的代码为

const osThreadDef_t os_thread_def_defaultTask = { "defaultTask", (StartDefaultTask), (osPriorityNormal), (0), (128), NULL, NULL }
defaultTaskHandle = osThreadCreate(&os_thread_def_defaultTask, NULL);           

Freertos创建任务有动态和静态两种方式,由FreeRTOSConfig.h中

#define configSUPPORT_STATIC_ALLOCATION          1
#define configSUPPORT_DYNAMIC_ALLOCATION         1           

两个宏定义来配置

创建用户任务osThreadCreate

打开CubeMX按如下操作创建userWork任务

STM32CubeMX快速开发系列-Freerots移植STM32CubeMX快速开发系列-Freerots移植

保存,生成代码,打开工程的freertos.c文件

/* USER CODE END Header_userWorkTask */
void userWorkTask(void const * argument)
{
  /* USER CODE BEGIN userWorkTask */
  /* Infinite loop */
  for(;;)
  {
    LED1_TOGGLE;
    osDelay(1000);
  }
  /* USER CODE END userWorkTask */
}           

在userWorkTask中添加LED1反转(StartDefaultTask中LED0在闪烁),下载运行程序发现两个灯在闪烁

查询任务IDosThreadGetId

返回当前运行线程的线程ID

/// Return the thread ID of the current running thread.
/// \return thread ID for reference by other functions or NULL in case of error.
/// \note MUST REMAIN UNCHANGED: \b osThreadGetId shall be consistent in every CMSIS-RTOS.
osThreadId osThreadGetId (void);           

删除任务osThreadTerminate

/// Terminate execution of a thread and remove it from Active Threads.
/// \param[in]     thread_id   thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
/// \return status code that indicates the execution status of the function.
/// \note MUST REMAIN UNCHANGED: \b osThreadTerminate shall be consistent in every CMSIS-RTOS.
osStatus osThreadTerminate (osThreadId thread_id);           

任务调度osThreadYield

将控制权传递给处于状态的下一个线程

/// Pass control to next thread that is in state \b READY.
/// \return status code that indicates the execution status of the function.
/// \note MUST REMAIN UNCHANGED: \b osThreadYield shall be consistent in every CMSIS-RTOS.
osStatus osThreadYield (void);           

设置任务优先级osThreadSetPriority

/// Change priority of an active thread.
/// \param[in]     thread_id     thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
/// \param[in]     priority      new priority value for the thread function.
/// \return status code that indicates the execution status of the function.
/// \note MUST REMAIN UNCHANGED: \b osThreadSetPriority shall be consistent in every CMSIS-RTOS.
osStatus osThreadSetPriority (osThreadId thread_id, osPriority priority);           

查询任务优先级osThreadGetPriority

/// Get current priority of an active thread.
/// \param[in]     thread_id     thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
/// \return current priority value of the thread function.
/// \note MUST REMAIN UNCHANGED: \b osThreadGetPriority shall be consistent in every CMSIS-RTOS.
osPriority osThreadGetPriority (osThreadId thread_id);           

挂起任务osThreadSuspend

/**
* @brief  Suspend execution of a thread.
* @param   thread_id   thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
* @retval  status code that indicates the execution status of the function.
*/
osStatus osThreadSuspend (osThreadId thread_id);           

恢复任务osThreadResume

/**
* @brief  Resume execution of a suspended thread.
* @param   thread_id   thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
* @retval  status code that indicates the execution status of the function.
*/
osStatus osThreadResume (osThreadId thread_id);           

挂起所有任务osThreadSuspendAll

/**
* @brief  Suspend execution of a all active threads.
* @retval  status code that indicates the execution status of the function.
*/
osStatus osThreadSuspendAll (void);           

恢复所有任务osThreadResumeAll

/**
* @brief  Resume execution of a all suspended threads.
* @retval  status code that indicates the execution status of the function.
*/
osStatus osThreadResumeAll (void);           

查询任务当前状态osThreadGetState

#if ( INCLUDE_eTaskGetState == 1 )
/**
* @brief  Obtain the state of any thread.
* @param   thread_id   thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
* @retval  the stae of the thread, states are encoded by the osThreadState enumerated type.
*/
osThreadState osThreadGetState(osThreadId thread_id);
#endif /* INCLUDE_eTaskGetState */           

判断任务是否被挂起osThreadIsSuspended

#if ( INCLUDE_eTaskGetState == 1 )
/**
* @brief Check if a thread is already suspended or not.
* @param thread_id thread ID obtained by \ref osThreadCreate or \ref osThreadGetId.
* @retval status code that indicates the execution status of the function.
*/

osStatus osThreadIsSuspended(osThreadId thread_id);

#endif /* INCLUDE_eTaskGetState */           

继续阅读