天天看點

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 */           

繼續閱讀