开发环境:IAR Embedded Workbench / Keil
SmallRTOS版本:V1.00
开发板:STM32F4
功能简介:采用微型嵌入式实时操作系统SmallRTOS创建控制任务CtrlTask,led任务Led1Task、Led2Task及相关同步信号量Led1SemHandle、Led2SemHandle等,由控制任务CtrlTask通过时间休眠的方式SRTaskuSleep控制Led灯的闪烁间隔,并通过Led1SemHandle、Led2SemHandle等信号量同步led任务Led1Task、Led2Task,实现led跑马灯的实验;
相关代码如下所示:
[mw_shl_code=c,true]/*****************************************************************************************************
SmallRTOS - Copyright (C) 2012~2015 SmallRTOS.ORG
http://www.SmallRTOS.org - Documentation, latest information, license and contact details.
http://www.SmallRTOS.com - Commercial support, development, porting, licensing and training services.
http://www.SmallRTOS.net - Forum, question and ask.
******************************************************************************************************/
/* Hardware and starter kit includes. */
#include "iar_stm32f407zg_sk.h"
#include "stm32f4xx.h"
#include "stm32f4xx_conf.h"
//SmallRTOS
#include "SmallRTOS.h"
SRTASK_HANDLE Led1TaskHandle = NULL;
SRTASK_HANDLE Led2TaskHandle = NULL;
SRTASK_HANDLE CtrlTaskHandle = NULL;
SRSEMAPHORE_HANDLE Led1SemHandle = NULL;
SRSEMAPHORE_HANDLE Led2SemHandle = NULL;
static void Led1Task( void *pvParameters );
static void Led2Task( void *pvParameters );
static void CtrlTask( void *pvParameters );
static void prvSetupHardware( void );
int main()
{
prvSetupHardware();
// 创建同步信号量
Led1SemHandle = SRCreateSemaphore("Led1");
Led2SemHandle = SRCreateSemaphore("Led2");
// 创建任务
Led1TaskHandle = SRCreateTask(Led1Task, NULL, SRLOWEAST_PRIORITY+1,256,"Led1Task",eTaskStateReady);
Led2TaskHandle = SRCreateTask(Led2Task, NULL, SRLOWEAST_PRIORITY+1,256,"Led2Task",eTaskStateReady);
CtrlTaskHandle = SRCreateTask(CtrlTask, NULL, SRLOWEAST_PRIORITY+1, 256, "CtrlTask",eTaskStateReady);
// Start the scheduler.
SRTaskStartScheduler();
//if everything is ok, can't reach here
for( ;; );
return 0;
}
void vParTestInitialise( void )
{
/* Initialise all four LEDs that are built onto the starter kit. */
STM_EVAL_LEDInit( LED1 );
STM_EVAL_LEDInit( LED2 );
}
static void prvSetupHardware( void )
{
/* Setup STM32 system (clock, PLL and Flash configuration) */
SystemInit();
/* Ensure all priority bits are assigned as preemption priority bits. */
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
/* Setup the LED outputs. */
vParTestInitialise();
/* Configure the button input. This configures the interrupt to use the
lowest interrupt priority, so it is ok to use the ISR safe SmallRTOS API
from the button interrupt handler. */
STM_EVAL_PBInit( BUTTON_USER, BUTTON_MODE_EXTI );
}
static void CtrlTask( void *pvParameters )
{
for(;;)
{
SRTaskuSleep(0, 500);//休眠500毫秒
SRSemaphorePost(Led1SemHandle);
SRTaskuSleep(0, 500);//休眠500毫秒
SRSemaphorePost(Led2SemHandle);
}
}
static void Led1Task( void *pvParameters )
{
for(;;)
{
SRSemaphorePend(Led1SemHandle, SRPEND_MAX_TICKS);
STM_EVAL_LEDToggle( LED1 );
}
}
static void Led2Task( void *pvParameters )
{
for(;;)
{
SRSemaphorePend(Led2SemHandle, SRPEND_MAX_TICKS);
STM_EVAL_LEDToggle( LED2 );
}
}
[/mw_shl_code] |