极海APM32E103VET6测评之FreeRTOS
[复制链接]
本期分享在APM32E103VET6上,移植FreeRTOS嵌入式实时系统。本例程主要实现FreeRTOS多任务的创建和使用,使用的FreeRTOS版本是v10.3.0,会将源码放在附件中。本程序共创建了3个任务,分别是开始任务、LED任务和按键任务,实际现象是LED2每1S闪烁一次,LED3每500ms闪烁一次,KEY键按下,实现打印输出“KEY Pressed!”。首先,将解压后的FreeRTOS文件添加到工程中,注意APM32E103VET6是corte-m3内核,所以port.c要使用FreeRTOS\portable\RVDS\ARM_CM3中的port.c,并添加相应的头文件路径,如图所示。
头文件路径。
新建os_includes.h头文件,并在其中包含#include "FreeRTOS.h"、#include "task.h"、#include "queue.h"、#include "timers.h"、#include "semphr.h",在apm32e10x.it.c中添加 #include "os_includes.h"和extern void xPortSysTickHandler( void );在SysTick_Handler中断函数中添加如下代码;最后,注释掉void SVC_Handler(void)和void PendSV_Handler(void)两个函数。对apm32e10x.it.c文件的更改到此为止。
void SysTick_Handler(void)
{
uint32_t ulReturn;
if ( xTaskGetSchedulerState() != taskSCHEDULER_NOT_STARTED ) /* ---任务调度已开启--- */
{
ulReturn = taskENTER_CRITICAL_FROM_ISR();
xPortSysTickHandler();
taskEXIT_CRITICAL_FROM_ISR( ulReturn );
}
}
新建myTasks.c和myTask.h两个文件,用于创建任务和功能实现。在myTasks.c中,实现开始任务、LED闪烁任务、按键任务,代码如下:
myTask.h代码
/**
************************************* Copyright ******************************
* (C) Copyright 2022,Zachray,HPU, China.
* All Rights Reserved
*
* FileName : myTasks.h
* Version : v1.0
* Author : Zachary
* Date : 2022-10-04
* Description:
******************************************************************************
*/
#ifndef __MY_TASKS_H_
#define __MY_TASKS_H_
/* Includes --------------------------------------------------------------*/
#include "main.h"
/* --- Typedef -----------------------------------------------------------*/
/* --- Define ------------------------------------------------------------*/
#define START_TASK_PRIO ( 1 )
#define LED_TASK_PRIO ( 2 )
#define KEY_TASK_PRIO ( 2 )
/* --- Macro -------------------------------------------------------------*/
/* --- Variables ---------------------------------------------------------*/
/* --- Functions ---------------------------------------------------------*/
void StartTaskCreate( void );
/*------------------------------------------------------------------------*/
#endif /* __MY_TASKS_H_ */
myTasks.c 代码
/**
************************************* Copyright ******************************
*
* (C) Copyright 2022,Zachary ,HPU, China.
* All Rights Reserved
*
* FileName : myTasks.c
* Version : v1.0
* Author : Zachary
* Date : 2022-10-04
* Description:
******************************************************************************
*/
/* Includes --------------------------------------------------------------*/
#include <stdio.h>
#include "main.h"
#include "myTasks.h"
#include "os_includes.h"
/* --- Typedef -----------------------------------------------------------*/
/* --- Define ------------------------------------------------------------*/
/* --- Macro -------------------------------------------------------------*/
/* --- Variables ---------------------------------------------------------*/
static TaskHandle_t xTaskHandleStart = NULL;
static TaskHandle_t xTaskHandleLed = NULL;
static TaskHandle_t xTaskHandleKey = NULL;
/* --- Functions ---------------------------------------------------------*/
static void xTaskStart( void *pvParameters );
static void AppTaskCreate( void );
static void xTaskLed( void *pvParameters );
static void xTaskKey( void *pvParameters );
/*------------------------------------------------------------------------*/
/**************************************************************
* [url=home.php?mod=space&uid=32621]@name[/url] StartTaskCreate
* [url=home.php?mod=space&uid=159083]@brief[/url] * @param None
* @retval
* [url=home.php?mod=space&uid=1315547]@author[/url] Zachary
* [url=home.php?mod=space&uid=34591]@data[/url] 2022-10-04
**************************************************************/
void StartTaskCreate( void )
{
xTaskCreate( xTaskStart, /* 任务函数 */
"xTaskStart", /* 任务名称 */
512, /* 任务堆栈 */
NULL, /* 传入参数 */
START_TASK_PRIO, /* 任务优先级 */
&xTaskHandleStart ); /* 任务句柄 */
}
/**************************************************************
* @Name xTaskStart
* @brief
* @param pvParameters: [输入/出]
* @retval
* @author Zachary
* @Data 2022-10-04
**************************************************************/
static void xTaskStart( void *pvParameters )
{
/* ---RTOS_MUTEX--------------------------- */
/* ---END RTOS_MUTEX----------------------- */
/* ---RTOS_SEMAPHORES---------------------- */
/* ---END RTOS_SEMAPHORES------------------ */
/* ---RTOS_TIMERS-------------------------- */
/* ---END RTOS_TIMERS---------------------- */
/* ---RTOS_QUEUES-------------------------- */
/* ---END RTOS_QUEUES---------------------- */
taskENTER_CRITICAL();
AppTaskCreate();
taskEXIT_CRITICAL();
for( ;; )
{
GPIOB->ODATA_B.ODATA8 ^= 1;
vTaskDelay( 1000 );
}
}
/**************************************************************
* @Name AppTaskCreate
* @brief
* @param None
* @retval
* @author Zachary
* @Data 2022-10-04
**************************************************************/
static void AppTaskCreate( void )
{
xTaskCreate( xTaskLed,
"xTaskLed",
256,
NULL,
LED_TASK_PRIO,
&xTaskHandleLed );
xTaskCreate( xTaskKey,
"xTaskKey",
256,
NULL,
KEY_TASK_PRIO,
&xTaskHandleKey );
}
/**************************************************************
* @Name xTaskLed
* @brief
* @param pvParameters: [输入/出]
* @retval
* @author Zachary
* @Data 2022-10-04
**************************************************************/
static void xTaskLed( void *pvParameters )
{
for( ;; )
{
GPIOB->ODATA_B.ODATA9 ^= 1;
vTaskDelay( 500 );
}
}
/**************************************************************
* @Name xTaskKey
* @brief
* @param pvParameters: [输入/出]
* @retval
* @author Zachary
* @Data 2022-10-04
**************************************************************/
static void xTaskKey( void *pvParameters )
{
uint8_t keyTemp1 = SET, keyTemp2 = SET;
for( ;; )
{
keyTemp1 = keyTemp2 = GPIOA->IDATA_B.IDATA1;
if( GPIOA->IDATA_B.IDATA1 == RESET )
{
vTaskDelay( 50 );
keyTemp2 = GPIOA->IDATA_B.IDATA1;
if( keyTemp1 ^ keyTemp2 )
{
printf( "KEY Pressed!\r\n" );
}
}
vTaskDelay( 10 );
}
}
main.c中主程序代码如下,主要是创建开始任务和开启任务调度。
int main( void )
{
NVIC_ConfigPriorityGroup( NVIC_PRIORITY_GROUP_4 );
SysInit();
StartTaskCreate();
vTaskStartScheduler();
while( 1 )
{
GPIOB->ODATA_B.ODATA9 ^= 1;
printf( "hello world\r\n" );
delay_ms( 1000 );
}
}
FreeRTOSConfig.h内容如下,这个文件用于裁剪FreeRTOS操作系统,修改与官方文件,代码如下:
/*
* FreeRTOS Kernel V10.3.0
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://www.FreeRTOS.org
* http://aws.amazon.com/freertos
*
* 1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html
*----------------------------------------------------------*/
/* Ensure stdint is only used by the compiler, and not the assembler. */
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
#include <stdint.h>
#include "apm32e10x.h"
#include "apm32e10x_conf.h"
extern uint32_t g_osRuntimeCounter;
extern uint32_t SystemCoreClock;
extern void configureTimerForRunTimeStats(void);
extern unsigned long getRunTimeCounterValue(void);
#endif
#define configUSE_PREEMPTION 1
#define configUSE_TICKLESS_IDLE 0
#define configCPU_CLOCK_HZ ( SystemCoreClock )
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES ( 32 )
#define configMINIMAL_STACK_SIZE ( ( uint16_t ) 256 )
#define configTOTAL_HEAP_SIZE ( (size_t)30*1024 )
#define configMAX_TASK_NAME_LEN ( 32 )
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 0
#define configUSE_TASK_NOTIFICATIONS 1
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 20
#define configUSE_QUEUE_SETS 0
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
#define configSUPPORT_DYNAMIC_ALLOCATION 1
/* Co-routine definitions. */
/* 合作式任务配置 */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
/* Software timer definitions. */
/* 软件定时器配置 */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY ( 2 )
#define configTIMER_QUEUE_LENGTH 10
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE * 2 )
/* Hook function related definitions. */
/* 钩子函数配置 */
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCHECK_FOR_STACK_OVERFLOW 0
#define configUSE_MALLOC_FAILED_HOOK 0
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
/* Run time and task stats gathering related definitions. */
/* 任务运行信息获取配置. */
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_TRACE_FACILITY 0
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#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_xTaskGetSchedulerState 1
#define INCLUDE_xEventGroupSetBitFromISR 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xQueueGetMutexHolder 1
#define INCLUDE_uxTaskGetStackHighWaterMark 1
#define INCLUDE_eTaskGetState 1
/* Cortex-M specific definitions. */
#ifdef __NVIC_PRIO_BITS
/* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
#define configPRIO_BITS __NVIC_PRIO_BITS
#else
#define configPRIO_BITS 4 /* 15 priority levels */
#endif
/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0x0f
/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 0x01
/* Interrupt priorities used by the kernel port layer itself. These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
standard names. */
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
//#define xPortSysTickHandler SysTick_Handler
/* Definitions needed when configGENERATE_RUN_TIME_STATS is on */
#if configGENERATE_RUN_TIME_STATS
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS configureTimerForRunTimeStats
#define portGET_RUN_TIME_COUNTER_VALUE getRunTimeCounterValue
#endif
#endif /* FREERTOS_CONFIG_H */
最后是演示视频与工程代码,本代码可直接在APM32E103VCS MINIBOARD V1.0上运行。
a83419b86fa2f50c9dd3b7096b84935f
|