FreeRTOS 移植要点(2)
port.c port.c 中主要实现了几个函数: pxPortInitialiseStack() xPortStartScheduler() vPortEndScheduler() vPortYield() vPortTickInterrupt() 还定义了个全局变量:uxCriticalNesting uxCriticalNesting 定义全局变量uxCriticalNesting 的代码如下。 /*
port.c
port.c 中主要实现了几个函数:
pxPortInitialiseStack()
xPortStartScheduler()
vPortEndScheduler()
vPortYield()
vPortTickInterrupt()
还定义了个全局变量:uxCriticalNesting
uxCriticalNesting
定义全局变量uxCriticalNesting 的代码如下。
/* Calls to portENTER_CRITICAL() can be nested. When they are nested the critical div should not be left (i.e. interrupts should not be re-enabled) until the nesting depth reaches 0. This variable simply tracks the nesting depth. Each task maintains it's own critical nesting depth variable so uxCriticalNesting is saved and restored from the task stack during a context switch. */ volatile unsigned portBASE_TYPE uxCriticalNesting = 0xff;
uxCriticalNesting 的初始值并不重要,因为每个任务的堆栈中存了uxCriticalNesting 各自的初始值0。
pxPortInitialiseStack
第一个介绍的是pxPortInitialiseStack()。这个函数的作用与uC/OS-II 中 OS_STK*OSTaskStkInit (void (*task)(void *pd), void *p_arg, OS_STK *ptos, INT16U opt) 函数的作用是相同的,实现代码也大同小异。
portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters ) { /* Place a few bytes of known values on the bottom of the stack. This can be uncommented to provide useful stack markers when debugging. *pxTopOfStack = ( portSTACK_TYPE ) 0x11; pxTopOfStack--; *pxTopOfStack = ( portSTACK_TYPE ) 0x22; pxTopOfStack--; *pxTopOfStack = ( portSTACK_TYPE ) 0x33; pxTopOfStack--; */ /* Setup the initial stack of the task. The stack is set exactly as expected by the portRESTORE_CONTEXT() macro. In this case the stack as expected by the HCS12 RTI instruction. */ /* The address of the task function is placed in the stack byte at a time. */ *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 ); pxTopOfStack--; *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 ); pxTopOfStack--; /* Next are all the registers that form part of the task context. */ /* Y register */ *pxTopOfStack = ( portSTACK_TYPE ) 0xff; pxTopOfStack--; *pxTopOfStack = ( portSTACK_TYPE ) 0xee; pxTopOfStack--; /* X register */ *pxTopOfStack = ( portSTACK_TYPE ) 0xdd; pxTopOfStack--; *pxTopOfStack = ( portSTACK_TYPE ) 0xcc; pxTopOfStack--; /* A register contains parameter high byte. */ *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 ); pxTopOfStack--; /* B register contains parameter low byte. */ *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 ); pxTopOfStack--; /* CCR: Note that when the task starts interrupts will be enabled since "I" bit of CCR is cleared */ *pxTopOfStack = ( portSTACK_TYPE ) 0x00; pxTopOfStack--; #ifdef BANKED_MODEL /* The page of the task. */ *pxTopOfStack = ( portSTACK_TYPE ) ( ( int ) pxCode ); pxTopOfStack--; #endif /* Finally the critical nesting depth is initialised with 0 (not within a critical div). */ *pxTopOfStack = ( portSTACK_TYPE ) 0x00; return pxTopOfStack; }
xPortStartScheduler
xPortStartScheduler()函数对应于uC/OS-II 中的OSStartHighRdy() 函数。FreeRTOS的移植代码中并没有直接在xPortStartScheduler() 函数中实现具体功能,而是将真正的工作放到了xBankedStartScheduler()函数中,xPortStartScheduler()函数只是简单的调用xBankedStartScheduler()函数。之所以这样处理是因为相应的代码需放到64K以内的地址空间中。具体可以参看下面代码中的注释部分。
#pragma CODE_SEG __NEAR_SEG NON_BANKED /* Simply called by xPortStartScheduler(). xPortStartScheduler() does not start the scheduler directly because the header file containing the xPortStartScheduler() prototype is part of the common kernel code, and therefore cannot use the CODE_SEG pragma. */ static portBASE_TYPE xBankedStartScheduler( void ); #pragma CODE_SEG DEFAULT portBASE_TYPE xPortStartScheduler( void ) { /* xPortStartScheduler() does not start the scheduler directly because the header file containing the xPortStartScheduler() prototype is part of the common kernel code, and therefore cannot use the CODE_SEG pragma. Instead it simply calls the locally defined xBankedStartScheduler() - which does use the CODE_SEG pragma. */ return xBankedStartScheduler(); } /*-----------------------------------------------------------*/ #pragma CODE_SEG __NEAR_SEG NON_BANKED static portBASE_TYPE xBankedStartScheduler( void ) { /* Configure the timer that will generate the RTOS tick. Interrupts are disabled when this function is called. */ prvSetupTimerInterrupt(); /* Restore the context of the first task. */ portRESTORE_CONTEXT(); /* Simulate the end of an interrupt to start the scheduler off. */ __asm( "rti" ); /* Should not get here! */ return pdFALSE; }
上面代码中调用了prvSetupTimerInterrupt() 函数,这在 uC/OS-II中是没有对应代码的。prvSetupTimerInterrupt()函数的功能是设置定时中断的频率。在这里放这个函数从程序逻辑上来看并不是太好。我本人还是倾向于uC/OS-II 作者的做法,应该将prvSetupTimerInterrupt() 函数放到第一个运行的任务的代码中,虽然这里的做法也没错误。
vPortEndScheduler
这个函数在uC/OS-II 没有对应的函数,因为uC/OS-II 不允许退出。这个移植代码中也没有实现什么具体的功能,就是个空函数。
void vPortEndScheduler( void ) { /* It is unlikely that the HCS12 port will get stopped. */ }
vPortYield
vPortYield()函数等价于uC/OS-II 中的OSCtxSw()函数。具体代码如下:
/* * Manual context switch forced by calling portYIELD(). This is the SWI * handler. */ void interrupt vPortYield( void ) { portSAVE_CONTEXT(); vTaskSwitchContext(); portRESTORE_CONTEXT(); }
FreeRTOS中少了与 uC/OS-II 中 OSIntCtxSw()函数对应的函数,这时因为FreeRTOS 中相应的功能用一个宏定义来实现了:portRESTORE_CONTEXT() ,因此就不需要这个函数了。
vPortTickInterrupt()
最后一个函数是vPortTickInterrupt() 这个函数是定时中断处理函数,等价于uC/OS-II 移植代码中的:interrupt VectorNumber_Vrti void OSTickISR (void)
由于 FreeRTOS既支持抢占式多任务,也支持协作式多任务,所以vPortTickInterrupt()函数相对uC/OS-II 移植代码中的OSTickISR()来说要复杂些。
/* * RTOS tick interrupt service routine. If the cooperative scheduler is * being used then this simply increments the tick count. If the * preemptive scheduler is being used a context switch can occur. */ void interrupt vPortTickInterrupt( void ) { #if configUSE_PREEMPTION == 1 { /* A context switch might happen so save the context. */ portSAVE_CONTEXT(); /* Increment the tick ... */ vTaskIncrementTick(); /* ... then see if the new tick value has necessitated a context switch. */ vTaskSwitchContext(); TFLG1 = 1; /* Restore the context of a task - which may be a different task to that interrupted. */ portRESTORE_CONTEXT(); } #else { vTaskIncrementTick(); TFLG1 = 1; } #endif }
至此,所有移植代码就都分析完了。
实时操作系统内核其实都大同小异,掌握了一种再学习其余的很容易就能入门。从入门难度来说,uC/OS-II无疑是入门学习的首选。之所以这么说并不是因为uC/OS-II本身很简单,而是国内介绍uC/OS-II的资料非常多。相比起来,介绍FreeRTOS的资料就少的可怜了。我建议想要学习FreeRTOS的人还是应该先学习uC/OS-II,学懂了uC/OS-II,然后对比着学习FreeRTOS,这样会事半功倍。这也是我学习FreeRTOS的路径。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

華為手機如何實現雙微信登入?隨著社群媒體的興起,微信已成為人們日常生活中不可或缺的溝通工具之一。然而,許多人可能會遇到一個問題:在同一部手機上同時登入多個微信帳號。對於華為手機用戶來說,實現雙微信登入並不困難,本文將介紹華為手機如何實現雙微信登入的方法。首先,華為手機自帶的EMUI系統提供了一個很方便的功能-應用程式雙開。透過應用程式雙開功能,用戶可以在手機上同

程式語言PHP是一種用於Web開發的強大工具,能夠支援多種不同的程式設計邏輯和演算法。其中,實作斐波那契數列是一個常見且經典的程式設計問題。在這篇文章中,將介紹如何使用PHP程式語言來實作斐波那契數列的方法,並附上具體的程式碼範例。斐波那契數列是一個數學上的序列,其定義如下:數列的第一個和第二個元素為1,從第三個元素開始,每個元素的值等於前兩個元素的和。數列的前幾元

如何在華為手機上實現微信分身功能隨著社群軟體的普及和人們對隱私安全的日益重視,微信分身功能逐漸成為人們關注的焦點。微信分身功能可以幫助使用者在同一台手機上同時登入多個微信帳號,方便管理和使用。在華為手機上實現微信分身功能並不困難,只需要按照以下步驟操作即可。第一步:確保手機系統版本和微信版本符合要求首先,確保你的華為手機系統版本已更新至最新版本,以及微信App

在現今的軟體開發領域中,Golang(Go語言)作為一種高效、簡潔、並發性強的程式語言,越來越受到開發者的青睞。其豐富的標準庫和高效的並發特性使它成為遊戲開發領域的一個備受關注的選擇。本文將探討如何利用Golang來實現遊戲開發,並透過具體的程式碼範例來展示其強大的可能性。 1.Golang在遊戲開發中的優勢作為靜態類型語言,Golang正在建構大型遊戲系統

PHP遊戲需求實現指南隨著網路的普及和發展,網頁遊戲的市場也越來越火爆。許多開發者希望利用PHP語言來開發自己的網頁遊戲,而實現遊戲需求是其中一個關鍵步驟。本文將介紹如何利用PHP語言來實現常見的遊戲需求,並提供具體的程式碼範例。 1.創造遊戲角色在網頁遊戲中,遊戲角色是非常重要的元素。我們需要定義遊戲角色的屬性,例如姓名、等級、經驗值等,並提供方法來操作這些

在Golang中實現精確除法運算是一個常見的需求,特別是在涉及金融計算或其它需要高精度計算的場景中。 Golang的內建的除法運算子「/」是針對浮點數計算的,並且有時會出現精度遺失的問題。為了解決這個問題,我們可以藉助第三方函式庫或自訂函數來實現精確除法運算。一種常見的方法是使用math/big套件中的Rat類型,它提供了分數的表示形式,可以用來實現精確的除法運算

實在抱歉,我無法提供即時的程式設計指導,但我可以為你提供一篇程式碼範例,讓你更能理解如何使用PHP實作SaaS。以下是一篇1500字以內的文章,標題為《使用PHP實作SaaS:全面解析》。在當今資訊時代,SaaS(SoftwareasaService)已經成為了企業和個人使用軟體的主流方式,它提供了更靈活、更便利的軟體存取方式。透過SaaS,用戶無需在本地

標題:利用Golang實現資料匯出功能詳解隨著資訊化程度的提升,許多企業和組織需要將儲存在資料庫中的資料匯出到不同的格式中,以便進行資料分析、報表產生等用途。本文將介紹如何利用Golang程式語言實作資料匯出功能,包括連接資料庫、查詢資料和匯出資料到檔案的詳細步驟,並提供具體的程式碼範例。連接資料庫首先,我們需要使用Golang中提供的資料庫驅動程序,例如da
