FreeRTOS 移植要点(1)

http://blog.csdn.net/liyuanbhu/article/details/7913291


FreeRTOS 的移植主要需要改写如下三个文件。

1.        portmacro.h

2.        port.c

3.        port.asm

 

如果采用的C编译器允许在C 代码中插入汇编,并且支持用C语言写中断处理函数。则port.asm 文件的内容是可以合并到port.c 中的。

下面以将 FreeRTOS 移植到FreeScale 68HCS12 内核的单片机为例,开发环境采用:CodeWarriorDevelopment Studio V5.9.0

之所以采用FreeScale 68HCS12 作为示例 CPU,是因为我以前写过一篇将uC/OS-II移植到FreeScale 68HCS12 核单片机的笔记。采用同样的CPU,同样的开发环境,可以方便我们比较两种不同实时操作系统的移植代码的异同。另外,FreeScale 68HCS12 相对ARM、MIPS 等构架要简单的多。移植代码量相对来说也要小一些,因此也更容易入门。

portmacro.h

portmacro.h 主要包括两部分内容,第一部分定义了一系列内核代码中用到的数据类型。FreeRTOS 与 uC/OS-II 一样,并不直接使用char、int 等这些原生类型,而是将其重新定义为一系列以port开头的新类型。在uC/OS-II的移植代码中,通常采用 typedef 来定义新的类型,而FreeRTOS的作者似乎更喜欢用宏定义。下面是相应的代码片段。


[cpp]  view plain  copy
  1. /* Type definitions. */  
  2. #define portCHAR        char  
  3. #define portFLOAT       float  
  4. #define portDOUBLE      double  
  5. #define portLONG        long  
  6. #define portSHORT       short  
  7. #define portSTACK_TYPE  unsigned portCHAR  
  8. #define portBASE_TYPE   char  
  9.   
  10. #if( configUSE_16_BIT_TICKS == 1 )  
  11.     typedef unsigned portSHORT portTickType;  
  12.     #define portMAX_DELAY ( portTickType ) 0xffff  
  13. #else  
  14.     typedef unsigned portLONG portTickType;  
  15.     #define portMAX_DELAY ( portTickType ) 0xffffffff  
  16. #endif  
  17. /*-----------------------------------------------------------*/  

portTickType 既可以定义为16位的无符号整数,也可以定义为32位的无符号整数。具体用那种定义,要看 FreeRTOSConfig.h 文件中如何设置configUSE_16_BIT_TICKS。

然后是一些硬件相关的定义。包括数据对其方式,堆栈增长方向,Tick Rate,还有任务切换的宏。


[cpp]  view plain  copy
  1. /* Hardware specifics. */  
  2. #define portBYTE_ALIGNMENT          1  
  3. #define portSTACK_GROWTH            ( -1 )  
  4. #define portTICK_RATE_MS            ( ( portTickType ) 1000 / configTICK_RATE_HZ )        
  5. #define portYIELD()                 __asm( "swi" );  
  6. #define portNOP()                   __asm( "nop" );  
  7. /*-----------------------------------------------------------*/  


portBYTE_ALIGNMENT 在uC/OS-II 是不需要的,FreeRTOS的代码中在分配任务堆栈空间时好像用到这个宏定义。(记不清了,有空再看一下源代码)

portSTACK_GROWTH 定义为1 表示堆栈是正向生长的,-1为逆向生长的。一般来说堆栈都是倒生的,68HCS12 也不例外,因此这里定义为 (-1)。

多说一句在 uC/OS-II 中,对应的宏是OS_STK_GROWTH, 1 表示逆向生长,0表示正向生长。

portTICK_RATE_MS 只在应用代码中可能会用到,表示的是Tick 间间隔多少 ms。

portYIELD() 实现的是任务切换,相当于 uC/OS-II中的 OS_TASK_SW()。

portNOP() 顾名思义就是对空操作定义了个宏。具体在FreeRTOS 代码中哪里用到了这个宏没注意过,但是想必是有地方用到了。

然后是有关临界区的处理代码:


[cpp]  view plain  copy
  1. /* Critical section handling. */  
  2. #define portENABLE_INTERRUPTS()             __asm( "cli" )    
  3. #define portDISABLE_INTERRUPTS()            __asm( "sei" )  
  4.   
  5. /* 
  6.  * Disable interrupts before incrementing the count of critical section nesting. 
  7.  * The nesting count is maintained so we know when interrupts should be 
  8.  * re-enabled.  Once interrupts are disabled the nesting count can be accessed 
  9.  * directly.  Each task maintains its own nesting count. 
  10.  */  
  11. #define portENTER_CRITICAL()                                    \  
  12. {                                                               \  
  13.     extern volatile unsigned portBASE_TYPE uxCriticalNesting;   \  
  14.                                                                 \  
  15.     portDISABLE_INTERRUPTS();                                   \  
  16.     uxCriticalNesting++;                                        \  
  17. }  
  18.   
  19. /* 
  20.  * Interrupts are disabled so we can access the nesting count directly.  If the 
  21.  * nesting is found to be 0 (no nesting) then we are leaving the critical  
  22.  * section and interrupts can be re-enabled. 
  23.  */  
  24. #define  portEXIT_CRITICAL()                                    \  
  25. {                                                               \  
  26.     extern volatile unsigned portBASE_TYPE uxCriticalNesting;   \  
  27.                                                                 \  
  28.     uxCriticalNesting--;                                        \  
  29.     if( uxCriticalNesting == 0 )                                \  
  30.     {                                                           \  
  31.         portENABLE_INTERRUPTS();                                \  
  32.     }                                                           \  
  33. }  
  34. /*-----------------------------------------------------------*/  

上面的代码是FreeRTOS 给出的官方移植代码,这段代码中进出临界区是通过关中断和开中断操作来实现的,相当与 uC/OS-II 中OS_CRITICAL_METHOD == 1 的情况。不过,通过全局变量 uxCriticalNesting来记录临界区的嵌套层数以此来实现临界区的嵌套操作。uxCriticalNesting虽然是全局变量,但是后面可以看到在任务切换时会将uxCriticalNesting的值存到当期任务的堆栈中,完成任务切换后从新的任务的堆栈中取出uxCriticalNesting的值。通过这种操作,每个任务就都维护这自己的uxCriticalNesting 了。实际上,FreeRTOS和uC/OS-II中临界区的概念是相同的,因此在uC/OS-II 中可用的临界区保护的方法都可以拿到FreeRTOS中来。

 

然后是任务切换相关功能的宏定义:


[cpp]  view plain  copy
  1. /* Task utilities. */  
  2.   
  3. /*  
  4.  * These macros are very simple as the processor automatically saves and  
  5.  * restores its registers as interrupts are entered and exited.  In 
  6.  * addition to the (automatically stacked) registers we also stack the  
  7.  * critical nesting count.  Each task maintains its own critical nesting 
  8.  * count as it is legitimate for a task to yield from within a critical 
  9.  * section.  If the banked memory model is being used then the PPAGE 
  10.  * register is also stored as part of the tasks context. 
  11.  */  
  12.   
  13. #ifdef BANKED_MODEL  
  14.     /*  
  15.      * Load the stack pointer for the task, then pull the critical nesting 
  16.      * count and PPAGE register from the stack.  The remains of the  
  17.      * context are restored by the RTI instruction. 
  18.      */  
  19.     #define portRESTORE_CONTEXT()                                   \  
  20.     {                                                               \  
  21.         extern volatile void * pxCurrentTCB;                        \  
  22.         extern volatile unsigned portBASE_TYPE uxCriticalNesting;   \  
  23.                                                                     \  
  24.         __asm( "ldx pxCurrentTCB" );                                \  
  25.         __asm( "lds 0, x" );                                        \  
  26.         __asm( "pula" );                                            \  
  27.         __asm( "staa uxCriticalNesting" );                          \  
  28.         __asm( "pula" );                                            \  
  29.         __asm( "staa 0x30" ); /* 0x30 = PPAGE */                    \  
  30.     }  
  31.   
  32.     /*  
  33.      * By the time this macro is called the processor has already stacked the 
  34.      * registers.  Simply stack the nesting count and PPAGE value, then save  
  35.      * the task stack pointer. 
  36.      */  
  37.     #define portSAVE_CONTEXT()                                      \  
  38.     {                                                               \  
  39.         extern volatile void * pxCurrentTCB;                        \  
  40.         extern volatile unsigned portBASE_TYPE uxCriticalNesting;   \  
  41.                                                                     \  
  42.         __asm( "ldaa 0x30" );  /* 0x30 = PPAGE */                   \  
  43.         __asm( "psha" );                                            \  
  44.         __asm( "ldaa uxCriticalNesting" );                          \  
  45.         __asm( "psha" );                                            \  
  46.         __asm( "ldx pxCurrentTCB" );                                \  
  47.         __asm( "sts 0, x" );                                        \  
  48.     }  
  49. #else  
  50.   
  51.     /*  
  52.      * These macros are as per the BANKED versions above, but without saving 
  53.      * and restoring the PPAGE register. 
  54.      */  
  55.   
  56.     #define portRESTORE_CONTEXT()                                   \  
  57.     {                                                               \  
  58.         extern volatile void * pxCurrentTCB;                        \  
  59.         extern volatile unsigned portBASE_TYPE uxCriticalNesting;   \  
  60.                                                                     \  
  61.         __asm( "ldx pxCurrentTCB" );                                \  
  62.         __asm( "lds 0, x" );                                        \  
  63.         __asm( "pula" );                                            \  
  64.         __asm( "staa uxCriticalNesting" );                          \  
  65.     }  
  66.   
  67.     #define portSAVE_CONTEXT()                                      \  
  68.     {                                                               \  
  69.         extern volatile void * pxCurrentTCB;                        \  
  70.         extern volatile unsigned portBASE_TYPE uxCriticalNesting;   \  
  71.                                                                     \  
  72.         __asm( "ldaa uxCriticalNesting" );                          \  
  73.         __asm( "psha" );                                            \  
  74.         __asm( "ldx pxCurrentTCB" );                                \  
  75.         __asm( "sts 0, x" );                                        \  
  76.     }  
  77. #endif  

这部分的代码挺长的,不过其实是对 Small Memery Model 和Banked Memery Model 分别提供了如下两个宏定义:

 

portRESTORE_CONTEXT()

portSAVE_CONTEXT()

 

使用哪一套宏定义是通过BANKED_MODEL 这个宏是否被定义来确定的。其实,CodeWarrior Development Studio V5.9.0 中提供的官方代码中给出了一种更正规的判断方法:

[cpp]  view plain  copy
  1. #if defined( __BANKED__) || defined(__LARGE__) || defined(__PPAGE__)  
  2.  //这里表示采用的 Banked Model  
  3. #else  
  4.  //这里表示没有采用 Banked Model  
  5. #endif  

FreeRTOS中保存和恢复任务上下文环境的代码与uC/OS-II中的大同小异,唯一有点区别的就是要保存uxCriticalNesting 的值。原因上面已经介绍过了。

 

[cpp]  view plain  copy
  1. /* 
  2.  * Utility macro to call macros above in correct order in order to perform a 
  3.  * task switch from within a standard ISR.  This macro can only be used if 
  4.  * the ISR does not use any local (stack) variables.  If the ISR uses stack 
  5.  * variables portYIELD() should be used in it's place. 
  6.  */  
  7. #define portTASK_SWITCH_FROM_ISR()                              \  
  8.     portSAVE_CONTEXT();                                         \  
  9.     vTaskSwitchContext();                                       \  
  10.     portRESTORE_CONTEXT();  

这段代码是用来在中断处理函数中完成任务切换的,非常简单,就不多介绍了。

最后是对任务原型的定义:

[cpp]  view plain  copy
  1. /* Task function macros as described on the FreeRTOS.org WEB site. */  
  2. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )  
  3. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )  

之所以要搞这两个宏,是为了利用某些C编译器的扩展功能对任务函数进行更好的优化。CodeWarrior 并不提供相关的功能,所以在这里任务就是普通的函数。


今天就先写到这里吧,明天继续。



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值