新手上路
- 积分
- 36
- 金钱
- 36
- 注册时间
- 2018-5-24
- 在线时间
- 8 小时
|
10金钱
main.c
[mw_shl_code=c,true]/**/
#include "main.h"
#include "sys.h"
#include "delay.h"
#include "FreeRTOS.h"
#include "task.h"
#define START_TASK_PRIO 1 /*ÈÎÎñÓÅÏȼ¶*/
#define START_STK_SIZE 128 /*ÈÎÎñ¶ÑÕ»´óС*/
TaskHandle_t StartTask_Handle; /*ÈÎÎñ¾ä±ú*/
void StartTask(void *pvParameters); /*ÈÎÎñº¯Êý*/
#define TEST1_TASK_PRIO 2
#define TEST1_STK_SIZE 50
TaskHandle_t TEST1Task_Handle;
void Test1Task(void *pvParameters);
#define TEST2_TASK_PRIO 3
#define TEST2_STK_SIZE 50
TaskHandle_t TEST2Task_Handle;
void Test2Task(void *pvParameters);
uint8_t test1, test2, test3;
/***** ÉùÃ÷ *****/
static void SystemInitial(void);
void StartTask(void *pvParameters)
{
taskENTER_CRITICAL(); /*½øÈëÁÙ½çÇø*/
xTaskCreate((TaskFunction_t )Test1Task, /*ÈÎÎñº¯Êý*/
(const char * )"Test1Task", /*ÈÎÎñÃû³Æ*/
(uint16_t )TEST1_STK_SIZE, /*ÈÎÎñ¶ÑÕ»´óС*/
(void * )NULL, /*´«µÝ¸øÈÎÎñº¯ÊýµÄ²ÎÊý*/
(UBaseType_t )TEST1_TASK_PRIO, /*ÈÎÎñÓÅÏȼ¶*/
(TaskHandle_t )&TEST1Task_Handle); /*ÈÎÎñ¾ä±ú*/
xTaskCreate((TaskFunction_t )Test2Task,
(const char * )"Test2Task",
(uint16_t )TEST2_STK_SIZE,
(void * )NULL,
(UBaseType_t )TEST2_TASK_PRIO,
(TaskHandle_t )&TEST2Task_Handle);
vTaskDelete(StartTask_Handle); /*ɾ³ý¿ªÊ¼ÈÎÎñ*/
taskEXIT_CRITICAL(); /*ÍƳöÁÙ½çÇø*/
}
void Test1Task(void *pvParameters)
{
while (1)
{
test1 = ~test1;
vTaskDelay(2);
// DelayNms(2);
}
}
void Test2Task(void *pvParameters)
{
while (1)
{
test2 = ~test2;
vTaskDelay(5);
}
}
static void SystemInitial(void)
{
DelayInitial();
}
int main(void)
{
SystemInitial();
/*´´½¨¿ªÊ¼ÈÎÎñ*/
xTaskCreate((TaskFunction_t )StartTask, /*ÈÎÎñº¯Êý*/
(const char * )"StartTask", /*ÈÎÎñÃû³Æ*/
(uint16_t )START_STK_SIZE, /*ÈÎÎñ¶ÑÕ»´óС*/
(void * )NULL, /*´«µÝ¸øÈÎÎñº¯ÊýµÄ²ÎÊý*/
(UBaseType_t )START_TASK_PRIO, /*ÈÎÎñÓÅÏȼ¶*/
(TaskHandle_t )&StartTask_Handle); /*ÈÎÎñ¾ä±ú*/
/*¿ªÆôÈÎÎñµ÷¶È*/
vTaskStartScheduler();
}
/***************************END OF FILE***************************/
[/mw_shl_code]
delay.c
[mw_shl_code=c,true]/**/
#include "delay.h"
#include "stm32f10x.h"
#include "sys.h"
#if SYSTEM_SUPPORT_OS
#include "FreeRTOS.h"
#include "task.h"
#endif
static uint8_t fac_us=0; //usÑÓʱ±¶³ËÊý
static uint16_t fac_ms=0; //msÑÓʱ±¶³ËÊý,ÔÚucosÏÂ,´ú±íÿ¸ö½ÚÅĵÄmsÊý
__IO uint32_t TimingDelay;
extern void xPortSysTickHandler(void);
extern void TimingDelayDecrement(void);
//systickÖжϷþÎñº¯Êý,ʹÓÃFreeRTOSʱÓõ½
void SysTick_Handler(void)
{
if(xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED)//ϵͳÒѾ­ÔËÐÐ
{
xPortSysTickHandler();
}
TimingDelayDecrement();
}
//³õʼ»¯ÑÓ³Ùº¯Êý
//SYSTICKµÄʱÖӹ̶¨ÎªAHBʱÖÓ£¬»ù´¡Àý³ÌÀïÃæSYSTICKʱÖÓƵÂÊΪAHB/8
//ÕâÀïΪÁ˼æÈÝFreeRTOS£¬ËùÒÔ½«SYSTICKµÄʱÖÓƵÂʸÄΪAHBµÄƵÂÊ£¡
//SYSCLK:ϵͳʱÖÓƵÂÊ
void DelayInitial(void)
{
/* uint32_t reload;
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); //Ñ¡ÔñÍⲿʱÖÓ HCLK
fac_us=SystemCoreClock/1000000; //²»ÂÛÊÇ·ñʹÓÃOS,fac_us¶¼ÐèҪʹÓÃ
reload=SystemCoreClock/1000000; //ÿÃëÖӵļÆÊý´ÎÊý µ¥Î»ÎªM
reload*=1000000/configTICK_RATE_HZ; //¸ù¾ÝconfigTICK_RATE_HZÉ趨Òç³öʱ¼ä
//reloadΪ24λ¼Ä´æÆ÷,×î´óÖµ:16777216,ÔÚ72MÏÂ,Ô¼ºÏ0.233s×óÓÒ
fac_ms=1000/configTICK_RATE_HZ; //´ú±íOS¿ÉÒÔÑÓʱµÄ×îÉÙµ¥Î»
SysTick->CTRL|=SysTick_CTRL_TICKINT_Msk; //¿ªÆôSYSTICKÖжÏ
SysTick->LOAD=reload; //ÿ1/configTICK_RATE_HZÃëÖжÏÒ»´Î
SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk; //¿ªÆôSYSTICK */
fac_us=SystemCoreClock/1000000; //²»ÂÛÊÇ·ñʹÓÃOS,fac_us¶¼ÐèҪʹÓÃ
fac_ms=1000/configTICK_RATE_HZ; //´ú±íOS¿ÉÒÔÑÓʱµÄ×îÉÙµ¥Î»
/*
* SystemCoreClock / 1000 1msÖжÏÒ»´Î
* SystemCoreClock / 100000 10usÖжÏÒ»´Î
* SystemCoreClock / 1000000 1usÖжÏÒ»´Î
*/
if (SysTick_Config(SystemCoreClock / 1000))
{
while (1);
}
/*¹Ø±Õsystick timer¶¨Ê±Æ÷*/
/* SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;*/
/*ʹÄܵδð¶¨Ê±Æ÷*/
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
}
//ÑÓʱnus
//nus:ÒªÑÓʱµÄusÊý.
//nus:0~204522252(×î´óÖµ¼´2^32/fac_us@fac_us=168)
void DelayNus(uint32_t nus)
{
uint32_t ticks;
uint32_t told,tnow,tcnt=0;
uint32_t reload=SysTick->LOAD; //LOADµÄÖµ
ticks=nus*fac_us; //ÐèÒªµÄ½ÚÅÄÊý
told=SysTick->VAL; //¸Õ½øÈëʱµÄ¼ÆÊýÆ÷Öµ
while(1)
{
tnow=SysTick->VAL;
if(tnow!=told)
{
if(tnow<told)tcnt+=told-tnow; //ÕâÀï×¢ÒâÒ»ÏÂSYSTICKÊÇÒ»¸öµÝ¼õµÄ¼ÆÊýÆ÷¾Í¿ÉÒÔÁË.
else tcnt+=reload-tnow+told;
told=tnow;
if(tcnt>=ticks)break; //ʱ¼ä³¬¹ý/µÈÓÚÒªÑÓ³ÙµÄʱ¼ä,ÔòÍ˳ö.
}
};
}
/*
* ±¾º¯ÊýÔÚÖжϺ¯ÊýÖе÷Ó㬵δð¶¨Ê±Æ÷ÖжÏÒ»´Îµ÷ÓÃÒ»´Î¡£
*/
void TimingDelayDecrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
/*
* TimingDelayÖµÔÚTimingDelayDecrementº¯ÊýÖеݼõ
*/
void DelayNms(uint32_t nTimes)
{
TimingDelay = nTimes;
while (TimingDelay!=0); //µÈ´ý¼ÆÊýÍ£Ö¹
}
/* //ÑÓʱnms
//nms:ÒªÑÓʱµÄmsÊý
//nms:0~65535
void DelayNms(uint32_t nms)
{
if(xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED)//ϵͳÒѾ­ÔËÐÐ
{
if(nms>=fac_ms) //ÑÓʱµÄʱ¼ä´óÓÚOSµÄ×îÉÙʱ¼äÖÜÆÚ
{
vTaskDelay(nms/fac_ms); //FreeRTOSÑÓʱ
}
nms%=fac_ms; //OSÒѾ­ÎÞ·¨ÌṩÕâôСµÄÑÓʱÁË,²ÉÓÃÆÕͨ·½Ê½ÑÓʱ
}
DelayNus((uint32_t)(nms*1000)); //ÆÕͨ·½Ê½ÑÓʱ
} */
//ÑÓʱnms,²»»áÒýÆðÈÎÎñµ÷¶È
//nms:ÒªÑÓʱµÄmsÊý
void DelayXms(uint32_t nms)
{
uint32_t i;
for(i=0;i<nms;i++) DelayNus(1000);
}
/***************************END OF FILE***************************/
[/mw_shl_code]
FreeRTOSConfig.h
[mw_shl_code=c,true]/*
FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
All rights reserved
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
This file is part of the FreeRTOS distribution.
FreeRTOS is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License (version 2) as published by the
Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
***************************************************************************
>>! NOTE: The modification to the GPL is included to allow you to !<<
>>! distribute a combined work that includes FreeRTOS without being !<<
>>! obliged to provide the source code for proprietary components !<<
>>! outside of the FreeRTOS kernel. !<<
***************************************************************************
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. Full license text is available on the following
link: http://www.freertos.org/a00114.html
***************************************************************************
* *
* FreeRTOS provides completely free yet professionally developed, *
* robust, strictly quality controlled, supported, and cross *
* platform software that is more than just the market leader, it *
* is the industry's de facto standard. *
* *
* Help yourself get started quickly while simultaneously helping *
* to support the FreeRTOS project by purchasing a FreeRTOS *
* tutorial book, reference manual, or both: *
* http://www.FreeRTOS.org/Documentation *
* *
***************************************************************************
http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
the FAQ page "My application does not run, what could be wrong?". Have you
defined configASSERT()?
http://www.FreeRTOS.org/support - In return for receiving this top quality
embedded software for free we request you assist our global community by
participating in the support forum.
http://www.FreeRTOS.org/training - Investing in training allows your team to
be as productive as possible as early as possible. Now you can receive
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
Ltd, and the world's leading authority on the world's leading RTOS.
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
compatible FAT file system, and our tiny thread aware UDP/IP stack.
http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
licenses offer ticketed support, indemnification and commercial middleware.
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
engineered and independently SIL3 certified version for use in safety and
mission critical applications that require provable dependability.
1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
#include "sys.h"
//#include "usart.h"
//Õë¶Ô²»Í¬µÄ±àÒëÆ÷µ÷Óò»Í¬µÄstdint.hÎļþ
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
extern uint32_t SystemCoreClock;
#endif
/*·ÀÖ¹printf±¨´í*/
#include "stdio.h"
//¶ÏÑÔ
#define vAssertCalled(char,int) printf("Error:%s,%d\r\n",char,int)
#define configASSERT(x) if((x)==0) vAssertCalled(__FILE__,__LINE__)
/***************************************************************************************************************/
/* FreeRTOS»ù´¡ÅäÖÃÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define configUSE_PREEMPTION 1 //1ʹÓÃÇÀռʽÄںˣ¬0ʹÓÃЭ³Ì
#define configUSE_TIME_SLICING 1 //1ʹÄÜʱ¼äƬµ÷¶È(ĬÈÏʽʹÄܵÄ)
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 //1ÆôÓÃÌØÊâ·½·¨À´Ñ¡ÔñÏÂÒ»¸öÒªÔËÐеÄÈÎÎñ
//Ò»°ãÊÇÓ²¼þ¼ÆËãÇ°µ¼ÁãÖ¸ÁÈç¹ûËùʹÓõÄ
//MCUûÓÐÕâЩӲ¼þÖ¸ÁîµÄ»°´ËºêÓ¦¸ÃÉèÖÃΪ0£¡
#define configUSE_TICKLESS_IDLE 0 //1ÆôÓõ͹¦ºÄticklessģʽ
#define configUSE_QUEUE_SETS 1 //Ϊ1ʱÆôÓöÓÁÐ
#define configCPU_CLOCK_HZ (SystemCoreClock) //CPUƵÂÊ
#define configTICK_RATE_HZ (1000) //ʱÖÓ½ÚÅÄƵÂÊ£¬ÕâÀïÉèÖÃΪ1000£¬ÖÜÆÚ¾ÍÊÇ1ms
#define configMAX_PRIORITIES (32) //¿ÉʹÓõÄ×î´óÓÅÏȼ¶
#define configMINIMAL_STACK_SIZE ((unsigned short)130) //¿ÕÏÐÈÎÎñʹÓõĶÑÕ»´óС
#define configMAX_TASK_NAME_LEN (16) //ÈÎÎñÃû×Ö×Ö·û´®³¤¶È
#define configUSE_16_BIT_TICKS 0 //ϵͳ½ÚÅļÆÊýÆ÷±äÁ¿Êý¾ÝÀàÐÍ£¬
//1±íʾΪ16λÎÞ·ûºÅÕûÐΣ¬0±íʾΪ32λÎÞ·ûºÅÕûÐÎ
#define configIDLE_SHOULD_YIELD 1 //Ϊ1ʱ¿ÕÏÐÈÎÎñ·ÅÆúCPUʹÓÃȨ¸øÆäËûͬÓÅÏȼ¶µÄÓû§ÈÎÎñ
#define configUSE_TASK_NOTIFICATIONS 1 //Ϊ1ʱ¿ªÆôÈÎÎñ֪ͨ¹¦ÄÜ£¬Ä¬ÈÏ¿ªÆô
#define configUSE_MUTEXES 1 //Ϊ1ʱʹÓû¥³âÐźÅÁ¿
#define configQUEUE_REGISTRY_SIZE 8 //²»Îª0ʱ±íʾÆôÓöÓÁмǼ£¬¾ßÌåµÄÖµÊÇ¿ÉÒÔ
//¼Ç¼µÄ¶ÓÁкÍÐźÅÁ¿×î´óÊýÄ¿¡£
#define configCHECK_FOR_STACK_OVERFLOW 0 //´óÓÚ0ʱÆôÓöÑÕ»Òç³ö¼ì²â¹¦ÄÜ£¬Èç¹ûʹÓô˹¦ÄÜ
//Óû§±ØÐëÌṩһ¸öÕ»Òç³ö¹³×Óº¯Êý£¬Èç¹ûʹÓõĻ°
//´ËÖµ¿ÉÒÔΪ1»òÕß2£¬ÒòΪÓÐÁ½ÖÖÕ»Òç³ö¼ì²â·½·¨¡£
#define configUSE_RECURSIVE_MUTEXES 1 //Ϊ1ʱʹÓõݹ黥³âÐźÅÁ¿
#define configUSE_MALLOC_FAILED_HOOK 0 //1ʹÓÃÄÚ´æÉêÇëʧ°Ü¹³×Óº¯Êý
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1 //Ϊ1ʱʹÓüÆÊýÐźÅÁ¿
/***************************************************************************************************************/
/* FreeRTOSÓëÄÚ´æÉêÇëÓйØÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define configSUPPORT_DYNAMIC_ALLOCATION 1 //Ö§³Ö¶¯Ì¬ÄÚ´æÉêÇë
#define configTOTAL_HEAP_SIZE ((size_t)(20*1024)) //ϵͳËùÓÐ×ܵĶѴóС
/***************************************************************************************************************/
/* FreeRTOSÓë¹³×Óº¯ÊýÓйصÄÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define configUSE_IDLE_HOOK 0 //1£¬Ê¹ÓÿÕÏй³×Ó£»0£¬²»Ê¹ÓÃ
#define configUSE_TICK_HOOK 0 //1£¬Ê¹ÓÃʱ¼äƬ¹³×Ó£»0£¬²»Ê¹ÓÃ
/***************************************************************************************************************/
/* FreeRTOSÓëÔËÐÐʱ¼äºÍÈÎÎñ״̬ÊÕ¼¯ÓйصÄÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define configGENERATE_RUN_TIME_STATS 0 //Ϊ1ʱÆôÓÃÔËÐÐʱ¼äͳ¼Æ¹¦ÄÜ
#define configUSE_TRACE_FACILITY 1 //Ϊ1ÆôÓÿÉÊÓ»¯¸ú×Ùµ÷ÊÔ
#define configUSE_STATS_FORMATTING_FUNCTIONS 1 //ÓëºêconfigUSE_TRACE_FACILITYͬʱΪ1ʱ»á±àÒëÏÂÃæ3¸öº¯Êý
//prvWriteNameToBuffer(),vTaskList(),
//vTaskGetRunTimeStats()
/***************************************************************************************************************/
/* FreeRTOSÓëЭ³ÌÓйصÄÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define configUSE_CO_ROUTINES 0 //Ϊ1ʱÆôÓÃЭ³Ì£¬ÆôÓÃЭ³ÌÒÔºó±ØÐëÌí¼ÓÎļþcroutine.c
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) //Э³ÌµÄÓÐЧÓÅÏȼ¶ÊýÄ¿
/***************************************************************************************************************/
/* FreeRTOSÓëÈí¼þ¶¨Ê±Æ÷ÓйصÄÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define configUSE_TIMERS 1 //Ϊ1ʱÆôÓÃÈí¼þ¶¨Ê±Æ÷
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES-1) //Èí¼þ¶¨Ê±Æ÷ÓÅÏȼ¶
#define configTIMER_QUEUE_LENGTH 5 //Èí¼þ¶¨Ê±Æ÷¶ÓÁ㤶È
#define configTIMER_TASK_STACK_DEPTH (configMINIMAL_STACK_SIZE*2) //Èí¼þ¶¨Ê±Æ÷ÈÎÎñ¶ÑÕ»´óС
/***************************************************************************************************************/
/* FreeRTOS¿ÉÑ¡º¯ÊýÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define INCLUDE_xTaskGetSchedulerState 1
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 1
/***************************************************************************************************************/
/* FreeRTOSÓëÖжÏÓйصÄÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#ifdef __NVIC_PRIO_BITS
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 4
#endif
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15 //ÖжÏ×îµÍÓÅÏȼ¶
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 //ϵͳ¿É¹ÜÀíµÄ×î¸ßÖжÏÓÅÏȼ¶
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/***************************************************************************************************************/
/* FreeRTOSÓëÖжϷþÎñº¯ÊýÓйصÄÅäÖÃÑ¡Ïî */
/***************************************************************************************************************/
#define xPortPendSVHandler PendSV_Handler
#define vPortSVCHandler SVC_Handler
#endif /* FREERTOS_CONFIG_H */
[/mw_shl_code]
|
|