FreeRTOS的移植
1、官网下载FreeRTOSv202212.01,搜索官网下载即可。
2、新建一个FreeRTOSDemo的工程,可以把之前的工程中的Bootloader工程复制一份。
3、打开下载的freertos代码将相应代码移植到我们的工程中。
protable文件夹,因为是gcc环境,所以保留Gcc文件夹。而N32G430的内核是CM4F,所以只保留ARM_CM4F
MemMang只保留heap_4.c即可。
移植后的代码路径
修改Makefile将移植过来的文件编译进去。
4、在工程根目录make。第一个错误如下:
我们从demo中copy一份FreeRTOSConfig.h,我选择的是这个目录下的文件。将其放在freertos/include目录下
5、继续编译,第二个错误如下:
N32代码里并没有定义SystemCoreClock这个变量,我们在
system_n32g430.c添加
在FreeRTOSConfig.h中打开多编译器的支持。
5、继续编译,第三个错误如下:
在makefile中添加浮点运算的支持。
6、继续编译,第四个错误如下:
修改FreeRTOSConfig.h文件如下:
7、继续编译,第五个错误:
屏蔽掉中断处理函数,文件为n32g430_it.c
8、 再次编译编译通过。
9、修改systick计数器,文件bsp_delay.c,原来的内容全部屏蔽,修改为下面内容:
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
*\*\file delay.c
*\*\author Nations
*\*\version v1.0.0
*\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
**/
#include "bsp_delay.h"
#include "FreeRTOS.h"
#include "task.h"
extern void xPortSysTickHandler(void);
//systick中断服务函数,使用ucos时用到
void SysTick_Handler(void)
{
if(xTaskGetSchedulerState()!=taskSCHEDULER_NOT_STARTED)//系统已经运行
{
xPortSysTickHandler();
}
}
uint32_t DBG_SysTick_Config()
{
RCC_ClocksType RCC_Clocks;
uint32_t ticks;
RCC_Clocks_Frequencies_Value_Get(&RCC_Clocks);
ticks = (RCC_Clocks.SysclkFreq / configTICK_RATE_HZ);
if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */ /* Load the SysTick Counter Value */
SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
SysTick->LOAD = ticks; /* Enable SysTick IRQ and SysTick Timer */
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
return (0); /* Function successful */
}
#if 0
/**
*\*\name DBG_SysTick_Config.
*\*\fun System tick configuration.
*\*\param ticks :system tick
*\*\return none
**/
static uint32_t DBG_SysTick_Config(uint32_t ticks)
{
if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */
SysTick->VAL = 0; /* Load the SysTick Counter Value */
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
return (0); /* Function successful */
}
/**
*\*\name SysTick_Delay_Us.
*\*\fun microsecond delay.
*\*\param us :any number
*\*\return none
**/
void SysTick_Delay_Us( __IO uint32_t us)
{
uint32_t i;
RCC_ClocksType RCC_Clocks;
RCC_Clocks_Frequencies_Value_Get(&RCC_Clocks);
DBG_SysTick_Config(RCC_Clocks.SysclkFreq / 1000000);
for(i=0;i<us;i++)
{
/* When the counter value decreases to 0, bit 16 of the CRTL register will be set to 1 */
/* When set to 1, reading this bit will clear it to 0 */
while( !((SysTick->CTRL)&(1<<16)) );
}
/* Turn off the SysTick timer */
SysTick->CTRL &=~ SysTick_CTRL_ENABLE_Msk;
}
/**
*\*\name SysTick_Delay_Us.
*\*\fun millisecond delay.
*\*\param ms :any number
*\*\return none
**/
void SysTick_Delay_Ms( __IO uint32_t ms)
{
uint32_t i;
RCC_ClocksType RCC_Clocks;
RCC_Clocks_Frequencies_Value_Get(&RCC_Clocks);
DBG_SysTick_Config(RCC_Clocks.SysclkFreq / 1000);
for(i=0;i<ms;i++)
{
/* When the counter value decreases to 0, bit 16 of the CRTL register will be set to 1 */
/* When set to 1, reading this bit will clear it to 0 */
while( !((SysTick->CTRL)&(1<<16)) );
}
/* Turn off the SysTick timer */
SysTick->CTRL &=~ SysTick_CTRL_ENABLE_Msk;
}
#endif
10、继续编译,报错如下:
修改FreeRTOSConfig.h,屏蔽掉
11、继续编译,报错如下
内存溢出了,我们在config.h文件修改,定义小一点,
再次编译,烧录验证现象。
1灯100ms闪烁,
2灯500ms闪烁。
至此,移植结束。
工程:https://download.csdn.net/download/weixin_43932857/87782342