FreeRTOS is transplanted on STM32F4.

Source: Internet
Author: User

This article is "Alientek stm32f429 FreeRTOS Development Course" Chapter II study notes
Chapter One notes –freertos Introduction and source code download First, transplant 1. Prepare the project documents

The MCU uses the stm32f429 core, creating a foundation project with Keli

Create a folder named FreeRTOS in the project
2. Add the source code to the file

Copy the FreeRTOS source into the FreeRTOS folder.

Only KELI,MEMMANG,RVDS three folders are kept in the portable folder
3. Add files to the project group

New grouping Freertos_core and freertos_portable in project files

Add the relevant kernel. c files into the grouping Freertos_core

Add the heap_4.c in the port.c and Memmang folders under the Rvds folder to the group freertos_portable in arm_cm4f

Add the FreeRTOS source header file path
4. Add a FreeRTOSConfig.h file

Under FreeRTOS's official Transplant Project Demo folder, locate the Cortex_m4f_stm32f407zg-sk folder and open the FreeRTOSConfig.h file found inside the

Add to the Include folder under the project Files FreeRTOS folder you created
changing conditional compilation criteria for Systemcoreclock

#ifdef __iccarm__
#include <stdint.h>
extern uint32_t systemcoreclock;
#endif

Change to I

#if defined (__iccarm__) | | Defined (__cc_arm) | | Defined (__gunc__)
#include <stdint.h>
extern uint32_t systemcoreclock;
#endif
5. Comment out the duplicate definition function

There are duplicate functions in the port.c and stm32f4xx_it.c two files, and you need to comment out the Pendsv_handler (), Svc_handler (), and Systick_handler () three functions in stm32f4xx_it.c 6. Close the hook function

The FreeRTOSConfig.h file that is copied by default opens some hook functions, all end with hooks, but not defined, in FreeRTOSConfig.h Configuse_idle_hook, Configuse_tick_hook , Configcheck_for_stack_overflow, and malloc_failed_hook the macro definition to 0 7. Modify the Sys.h file

Modify the Sys.h file under the System folder to use the OS

0, OS//1 not supported
, OS
#define SYSTEM_SUPPORT_OS       1       //define System Folder support OS
8. Modify the Usart.c file

Open the Usart.c file under the System folder and add the FreeRTOS.h header file

#if system_support_os
#include "FreeRTOS.h"                   //os use   
#endif

The interrupt service function of USART1 is added Osintenter () and Osintexit () when using Ucos, and it is not necessary to use FreeRTOS, so comment out

Serial 1 Interrupt Service program
void Usart1_irqhandler (void)                    
{ 
    u32 timeout=0;
    U32 maxdelay=0x1ffff;
#if system_support_os     //using OS
//  osintenter ();    
#endif

    Hal_uart_irqhandler (&uart1_handler);    Call the HAL Library interrupt handling common function

    timeout=0;
    while (Hal_uart_getstate (&uart1_handler)! = Hal_uart_state_ready)//Waiting ready
    {
     timeout++;////timeout processing
     if (timeout>maxdelay) break;        

    }

    timeout=0;
    while (Hal_uart_receive_it (&uart1_handler, (U8 *) Arxbuffer, rxbuffersize)! = HAL_OK)//Once processing is complete, Turn interrupt on again and set Rxxfercount to 1
    {
     timeout++;//timeout handle
     if (timeout>maxdelay) break;    
    }
#if system_support_os     //using OS
//  osintexit ();                                             
#endif
9. Modify the Delay.c file

Open the Delay.c file in the System folder and re-write the required code

#include "delay.h" #include "sys.h"//////////////////////////////////////////////////////////////////////////////
If you are using an OS, include the following header files. #if system_support_os #include "FreeRTOS.h"//freertos use #include "task.h" #endif static u32 Fac_us                            = 0;                        US delay times multiplier #if system_support_os static U16 fac_ms=0;
The MS Delay multiplier, under OS, represents the number of MS Per beat #endif extern void Xportsystickhandler (void); SysTick Interrupt Service function, using the OS with void Systick_handler (void) {if (Xtaskgetschedulerstate ()!=taskscheduler_not_started)//  
    The system has been run {xportsystickhandler ();
} hal_inctick (); }//Initialize delay function//When using Ucos, this function initializes the Ucos clock beat//systick clock fixed to AHB clock//SYSCLK: System clock frequency void Delay_init (U8 sysclk) {u32 Reloa
    D                          Hal_systick_clksourceconfig (SYSTICK_CLKSOURCE_HCLK);//systick frequency is HCLK fac_us=sysclk;                          The use of RELOAD=SYSCLK is required whether or not the os,fac_us is used; The number of counts per second is K Reload*=1000000/confiGtick_rate_hz;  
    Set overflow time according to Configtick_rate_hz//reload 24-bit register, maximum: 16777216, at 180M, about 0.745s         Fac_ms=1000/configtick_rate_hz;                   Represents the minimum unit systick->ctrl|=systick_ctrl_tickint_msk;//the OS can delay to turn on SysTick interrupts systick->load=reload; Once every 1/configtick_rate_hz systick->ctrl|=systick_ctrl_enable_msk;  
Open Systick}//Delay NUS//nus: The number of us to delay.       
    nus:0~190887435 (maximum value is 2^32/fac_us@fac_us=22.5) void Delay_us (U32 nus) {
    U32 ticks;
    U32 told,tnow,tcnt=0;               U32 reload=systick->load;                       The value of load is ticks=nus*fac_us;                      Number of Beats required told=systick->val;  
        The counter value of the first entry while (1) {tnow=systick->val;   if (tnow!=told) {if (tnow<told) Tcnt+=told-tnow;
            Notice here that the Systick is a descending counter on it. else Tcnt+=reLoad-tnow+told;
            Told=tnow;           if (tcnt>=ticks) break;
        Time exceeds/equals the time to delay, then exits.                                      
}  
    }; }//Delay NMS, will cause task scheduling//nms: Ms//nms:0~65535 void Delay_ms (U32 nms) {if (Xtaskgetschedulerstate ()!=taskschedule) to delay 
            r_not_started)//The system has run {if (Nms>=fac_ms)//delay time is greater than the minimum time period of the OS {         Vtaskdelay (Nms/fac_ms);                        FreeRTOS delay} Nms%=fac_ms;              OS has not been able to provide such a small delay, the use of normal mode delay} delay_us ((u32) (nms*1000));
    Normal mode delay}//delay NMS, will not cause task scheduling//nms: Ms void Delay_xms (U32 NMS) to delay {u32 i;
for (i=0;i<nms;i++) Delay_us (1000); }

Consists of an initialization tick timer and three delay functions

Delay_init () completes initialization of the tick timer. FreeRTOS system clock beats are set by the macro configtick_rate_hz, due to the use of the HAL Library, and the HAL Library delay function requires a tick timer time period of 1ms, the system beat of the responsible FreeRTOS should be set to 1000HZ ( That is, the tick timer interrupt period is 1ms)

Delay_us (), Delay_ms (), and DELAY_XMS () are all delay functions.

Delay_ms () is the encapsulation of the delay function vtaskdelay () in FreeRTOS, which causes task switching;

Delay_us () is the US-class delay function, OS system beats is 1ms, can not provide such a small delay, directly using the tick timer count to delay;

The DELAY_XMS () at the US level of delay_us () accumulates to the MS level to form a delay, which completes the MS-level delay, but does not result in a task switch like Delay_ms () 10. Comment out the duplicate definition function in FreeRTOSConfig.h

Comment out the duplicate definition function in FreeRTOSConfig.h systick_handler ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.