【APM32F402R Micro-EVB】-6-FreeRTOS体验
本文体验在keil环境下CMSIS-FreeRTOS在APM32F402上的使用。内核初始化:
/*
Initialize the RTOS Kernel.
*/
osStatus_t osKernelInitialize (void) {
osStatus_t stat;
BaseType_t state;
if (IRQ_Context() != 0U) {
stat = osErrorISR;
}
else {
state = xTaskGetSchedulerState();
/* Initialize if scheduler not started and not initialized before */
if ((state == taskSCHEDULER_NOT_STARTED) && (KernelState == osKernelInactive)) {
#if defined(USE_TRACE_EVENT_RECORDER)
/* Initialize the trace macro debugging output channel */
EvrFreeRTOSSetup(0U);
#endif
#if defined(USE_FreeRTOS_HEAP_5) && (HEAP_5_REGION_SETUP == 1)
/* Initialize the memory regions when using heap_5 variant */
vPortDefineHeapRegions (configHEAP_5_REGIONS);
#endif
KernelState = osKernelReady;
stat = osOK;
} else {
stat = osError;
}
}
/* Return execution status */
return (stat);
}
创建线程:
/* Create the thread */
ledThreadID = osThreadNew(Led_Thread, NULL, &ledThreadattr);
if (ledThreadID == NULL)
{
printf("Create led thread failed!\r\n");
}
usartThreadID = osThreadNew(Usart_TestThread, NULL, &usartThreadattr);
if (usartThreadID == NULL)
{
printf("Create usart thread failed!\r\n");
}
启动调度器:
/* Start the RTOS scheduler */
osKernelStart();
其中LED线程与USART任务定义如下:
void Led_Thread(void *argument)
{
UNUSED(argument);
while (1)
{
/* Toggle LED2 */
BOARD_LED_Toggle(LED2);
osDelay(1000);
}
}
/**
* @brief Usart1 test task
*
* @param argumentNot used
*
* @retvalNone
*/
void Usart_TestThread(void *argument)
{
UNUSED(argument);
while (1)
{
/* Print message */
printf("Hello, world!\r\n");
osDelay(1000);
}
}
实物演示:
问问lz,操作运行上感觉怎么样,也想申请一块 看起来你已经成功地在APM32F402上运行了FreeRTOS,并且创建了线程。你的代码示例对于初学者来说非常有用。如果遇到任何问题,社区总是乐于帮助解决。
我也喜欢FreeRTOS
不过,printf()函数是不是可重入的啊?
页:
[1]