移植Free RTOS
由于Cubemx吧兔,在STM32中移植Free RTOS變的異常簡(jiǎn)單闲询。只需要勾選一下嚣崭,開(kāi)發(fā)工具將移植的工作全部搞定。如圖所示:
由于后面時(shí)鐘存在沖突需要對(duì)時(shí)鐘修改魔慷,如下圖所示:
同時(shí)修改了時(shí)鐘配置,如下圖所示:
然后再添加兩個(gè)GPIO用于點(diǎn)亮LED著恩,這里不再詳述院尔。
在Free RTOS中添加任務(wù)列表。
然后可以生成MDK V5下的代碼喉誊。
代碼分析
Free RTOS 任務(wù)相關(guān)的代碼會(huì)自動(dòng)生成如下:
創(chuàng)建句柄:
osThreadId LED1Handle;
osThreadId LED2Handle;
創(chuàng)建任務(wù)函數(shù):
void StartLED1Task(void const * argument);
void StartLED2Task(void const * argument);
main函數(shù)中創(chuàng)建線程:
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_SPI2_Init();
MX_USART2_UART_Init();
/* Create the thread(s) */
/* definition and creation of LED1 */
osThreadDef(LED1, StartLED1Task, osPriorityNormal, 0, 128);
LED1Handle = osThreadCreate(osThread(LED1), NULL);
/* definition and creation of LED2 */
osThreadDef(LED2, StartLED2Task, osPriorityIdle, 0, 128);
LED2Handle = osThreadCreate(osThread(LED2), NULL);
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
}
創(chuàng)建自己的任務(wù)函數(shù):
/* StartLED1Task function */
void StartLED1Task(void const * argument)
{
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
HAL_GPIO_WritePin(GPIOB, LED0_Pin, 0);
osDelay(1000);
HAL_GPIO_WritePin(GPIOB, LED0_Pin, 1);
osDelay(1000);
}
/* USER CODE END 5 */
}
/* StartLED2Task function */
void StartLED2Task(void const * argument)
{
/* USER CODE BEGIN StartLED2Task */
/* Infinite loop */
for(;;)
{
HAL_GPIO_WritePin(GPIOB, LED1_Pin, 0);
osDelay(100);
HAL_GPIO_WritePin(GPIOB, LED1_Pin, 1);
osDelay(100);
}
/* USER CODE END StartLED2Task */
}
最終能夠?qū)崿F(xiàn)LED燈的交替顯示邀摆。
調(diào)試中的問(wèn)題:
1、關(guān)于創(chuàng)建任務(wù)函數(shù)xTaskCreate()和osThreadDef()的區(qū)別:
xTaskCreate()是FreeRTOS的原始API函數(shù)伍茄。
osThreadDef()ARM搞的CMIS-RTOS V1封裝層栋盹,對(duì)FreeRTOS的API函數(shù)進(jìn)行封裝。