Main.c, Kernel.c,Kernel.h,Config.c,Config.h代码如下提供。
Main.c
#include "os.h"
#include "Kernel.h"
#include "Config.h"
int main()
{
SystemConfigInit();
KeranlTask();
return 0;
}
Kernel.c
/*-------------------------------------------------------------------------
软件主体
-------------------------------------------------------------------------*/
#include "os.h"
#include "kernel.h"
#include "config.h"
extern void SysTickInit(void);
static OS_TCB taskStartTCB;
static CPU_STK startTaskStk[STARTUP_TASK_STK_SIZE]; //启动任务的程序空间
static OS_TCB task1TCB;
static CPU_STK task1_stk[TASK1_STK_SIZE];
static OS_TCB task2TCB;
static CPU_STK task2_stk[TASK2_STK_SIZE];
static OS_TCB task3TCB;
static CPU_STK task3_stk[TASK3_STK_SIZE];
static volatile OS_SEM taskSem;
static volatile OS_ERR err;
/*******************************************************************************
* Function Name :void StartTask(void)
* Description :启动任务
* Input :
* Output :
* Other :
* Date :2012.04.18 11:48:23
*******************************************************************************/
void StartTask(void)
{
led_init();
SysTickInit();
OSTaskCreate( (OS_TCB *)&task1TCB,
(CPU_CHAR *)"task1",
(OS_TASK_PTR)task1,
(void *)0,
(OS_PRIO )TASK1_PRIO,
(CPU_STK *)&task1_stk[0],
(CPU_STK_SIZE)TASK1_STK_SIZE / 10,
(CPU_STK_SIZE)TASK1_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void *)0,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSTaskCreate( (OS_TCB *)&task2TCB,
(CPU_CHAR *)"task2",
(OS_TASK_PTR)task2,
(void *)0,
(OS_PRIO ) TASK2_PRIO,
(CPU_STK *)&task2_stk[0],
(CPU_STK_SIZE)TASK2_STK_SIZE / 10,
(CPU_STK_SIZE)TASK2_STK_SIZE,
(OS_MSG_QTY)0,
(OS_TICK )0,
(void *)0,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSTaskCreate( (OS_TCB *)&task3TCB,
(CPU_CHAR *)"task3",
(OS_TASK_PTR)task3,
(void *)0,
(OS_PRIO )TASK3_PRIO,
(CPU_STK *)&task3_stk[0],
(CPU_STK_SIZE)TASK3_STK_SIZE / 10,
(CPU_STK_SIZE)TASK3_STK_SIZE,
(OS_MSG_QTY)0,
(OS_TICK )0,
(void *)0,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSSemCreate( (OS_SEM *)&taskSem,
(CPU_CHAR *)"taskSem",
(OS_SEM_CTR)0,
(OS_ERR *)err);
OSTaskDel( (OS_TCB *)&taskStartTCB,
(OS_ERR *)&err);
}
static void task1(void *p_arg)
{
while (1)
{
led_on(LED_4);
OSTimeDly( (OS_TICK )200,
(OS_OPT )OS_OPT_TIME_DLY,
(OS_ERR *)&err);
led_off(LED_4);
OSTimeDly( (OS_TICK )200,
(OS_OPT )OS_OPT_TIME_DLY,
(OS_ERR *)&err);
OSSemPost( (OS_SEM *)&taskSem,
(OS_OPT )OS_OPT_POST_ALL,
(OS_ERR *)&err);
}
}
static void task2(void *p_arg)
{
while (1)
{
led_on(LED_5);
OSSemPend( (OS_SEM *)&taskSem,
(OS_TICK )10000,
(OS_OPT )OS_OPT_PEND_BLOCKING,
(CPU_TS *)0,
(OS_ERR *)&err);
led_off(LED_5);
OSSemPend( (OS_SEM *)&taskSem,
(OS_TICK )10000,
(OS_OPT )OS_OPT_PEND_BLOCKING,
(CPU_TS *)0,
(OS_ERR *)&err);
}
}
static void task3(void *p_arg)
{
while (1)
{
led_on(LED_3);
OSTimeDly( (OS_TICK )100,
(OS_OPT )OS_OPT_TIME_DLY,
(OS_ERR *)&err);
led_off(LED_3);
OSTimeDly( (OS_TICK )100,
(OS_OPT )OS_OPT_TIME_DLY,
(OS_ERR *)&err);
}
}
/*******************************************************************************
* Function Name :void KeranlTask(void)
* Description :启动任务
* Input :
* Output :
* Other :
* Date :2012.04.18 11:05:47
*******************************************************************************/
void KeranlTask(void)
{
CPU_Init();
OSInit((OS_ERR *)&err);
OSTaskCreate( (OS_TCB *)&taskStartTCB,
(CPU_CHAR *)"task_start",
(OS_TASK_PTR)StartTask,
(void *)0,
(OS_PRIO ) STARTUP_TASK_PRIO,
(CPU_STK *)&startTaskStk[0],
(CPU_STK_SIZE)STARTUP_TASK_STK_SIZE / 10,
(CPU_STK_SIZE)STARTUP_TASK_STK_SIZE,
(OS_MSG_QTY)0,
(OS_TICK )0,
(void *)0,
(OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
(OS_ERR *)&err);
OSStart((OS_ERR *)&err);
}
Kernel.h
#ifndef _kernel_h_
#define _kernel_h_
#include "os.h"
static void task1(void *p_arg);
static void task2(void *p_arg);
static void task3(void *p_arg);
void KeranlTask(void);
#endif
Config.c
/*
********************************************************************************
* uC/OS-II
*
* ARM Cortex-M3 Port
*
* File : Config.C
* Version : V1.0
* By : 王宏强
*
* For : Stm32f10x
* Mode : Thumb2
* Toolchain :
* RealView Microcontroller Development Kit (MDK)
* Keil uVision
* Description : STM32F10x 内部 系统的配置
*
* 1,系统中断优先级模式设置
* 2,系统程序启动指定
* 3,系统时钟计时器配置
* 4,芯片引脚初始化
*
* Date : 2012.05.22
*******************************************************************************/
#include "misc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_flash.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_iwdg.h"
#include "config.h"
GPIO_InitTypeDef GPIO_InitStructure;
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
#ifdef USE_STM3210B_EVAL
/* Enable the USART2 Pins Software Remapping */
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
#endif
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_Init(GPIOE, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
RCC_APB2Periph_GPIOE, DISABLE);
}
/*******************************************************************************
* Function Name : Delay
* Description : Inserts a delay time.
* Input : nCount: specifies the delay time length.
* Output : None
* Return : None
*******************************************************************************/
void Delay(volatile CPU_INT32U nCount)
{
for(; nCount != 0; nCount--);
}
/*******************************************************************************
函 数 名:void IWDG_Init(void)
功能描述:看门狗初始化
入口参数:
返回参数:
创建时间: 2011.6.24
********************************************************************************/
void IWDG_Init(void)
{
IWDG_WriteAccessCmd( IWDG_WriteAccess_Enable );
IWDG_SetPrescaler( IWDG_Prescaler_64); //最小
IWDG_SetReload( 0x138); //40KHz内部时钟 0.5s
IWDG_WriteAccessCmd( IWDG_WriteAccess_Disable );
IWDG_Enable();
IWDG_ReloadCounter();
}
/*******************************************************************************
* Function Name :void SysTickInit(void)
* Description :系统定时器时间配置
* Input :
* Output :
* Other :时基为1ms
* Date :2011.11.03 12:59:13
*******************************************************************************/
void SysTickInit(void)
{
SysTick_Config(SystemCoreClock / 1000); //uCOS时基1ms
}
/*******************************************************************************
* Function Name :void InterruptOrder(void)
* Description :中断向量,优先级
* Input :
* Output :
* Other :
* Date :2011.10.27 11:50:05
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );//优先级设置
}
/*******************************************************************************
* Function Name :void SystemConfig(void)
* Description :系统初始化
* Input :
* Output :
* Other :
* Date :2011.10.27 13:14:59
*******************************************************************************/
void SystemConfigInit(void)
{
NVIC_Configuration(); //中断优先级设置
GPIO_Configuration(); //端口初始化,所有端口关
}
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void led_on(CPU_INT32U n)
{
switch (n)
{
case LED_0:
GPIO_SetBits(GPIOD, GPIO_Pin_2);
break;
case LED_1:
GPIO_SetBits(GPIOD, GPIO_Pin_3);
break;
case LED_2:
GPIO_SetBits(GPIOD, GPIO_Pin_4);
break;
case LED_3:
GPIO_SetBits(GPIOB, GPIO_Pin_9);
break;
case LED_4:
GPIO_SetBits(GPIOA, GPIO_Pin_12);
break;
case LED_5:
GPIO_SetBits(GPIOA, GPIO_Pin_11);
break;
default:
break;
}
}
void led_off(CPU_INT32U n)
{
switch (n)
{
case LED_0:
GPIO_ResetBits(GPIOD, GPIO_Pin_2);
break;
case LED_1:
GPIO_ResetBits(GPIOD, GPIO_Pin_3);
break;
case LED_2:
GPIO_ResetBits(GPIOD, GPIO_Pin_4);
break;
case LED_3:
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
break;
case LED_4:
GPIO_ResetBits(GPIOA, GPIO_Pin_12);
break;
case LED_5:
GPIO_ResetBits(GPIOA, GPIO_Pin_11);
break;
default:
break;
}
}
Config.h
/****************************************Copyright (c)**************************************************
** Modified by: 王宏强
** Modified date: 2012-05-20
** Version: v3.0
** Descriptions: 修改用于STM32F10x
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "os.h"
/********************************/
/* 系统配置函数 */
/********************************/
#define LED_0 0
#define LED_1 1
#define LED_2 2
#define LED_3 3
#define LED_4 4
#define LED_5 5
void Delay(volatile CPU_INT32U nCount);
void IWDG_Init(void);
void SysTickInit(void);
void SystemConfigInit(void);
void led_init(void);
void led_on(CPU_INT32U n);
void led_off(CPU_INT32U n);
/********************************************************************************************************
** End Of File
********************************************************************************************************/
|