uC/OS-2のマニュアルの日本語訳に挑戦

6.00 μC/OS-II Port for the H8/300L
The μC/OS-II port for the H8/300L provided with this application note assumes the HEW environment. However, you can certainly modify this port to work with other C/C++ compilers. In fact, the CPU instructions (i.e. the code) should be identical and all you would have to do is adapt the files to your compiler specifics (compiler/assembler/linker directives and options). We will describe some of these when we cover the contents of the different files.
We assume that you have μC/OS-II V2.62 or higher. We actually used V2.70 to do the tests.


・H8/300LのためのuC/OS-2ポート
H8/300LのためのuC/OS-2ポートはHEW環境を想定してこのアプリケーションノートを提供します。しかし主として、他のCコンパイラでも改良が可能です。事実、そのCPUの取り扱い説明書はほぼ同一で、他のコンパイラアセンブラ、リンカ、機能はほとんど同じです。われわれはそれぞれのファイルをカバーしたものの内容の一部を紹介いたします。
私たちはあなたがuC/OS-2 V2.62かそれ以上を所有していることを前提として説明を行います。われわれのこのテストではuC/OS-2 V2.70を使用しています。




6.01 Directories and Files
The software that goes with this application note is provided on the Micriμm web site (see www.Micrium.com, click on Support and then Application Notes). The file is called EDK38024-uCOS-II.ZIP and will be associated with AN-1100. When you unzip the file, the port files are found in the following directory:
\Micrium\Software\uCOS-II\Ports\H8-300L\H8-38024\EDK38024\HEW\
Like all μC/OS-II ports, the source code for the port is found in the following three files:
os_cpu.h
os_cpu_c.c
os_cpu_a.src

このアプリケーションで説明使用としているこのソフトウェアはMicrium社のWEBサイトより提供されています。(詳しくは www.Micrium.comのサポートアプリケーションをご覧ください)
今回使用するファイルはEDK38024-uCOS-II.ZIPというもので、AN-1100と共同になっている。ポートファイルはEDK38024-uCOS-II.ZIPファイルを解凍後以下のファイルパスにあります。
\Micrium\Software\uCOS-II\Ports\H8-300L\H8-38024\EDK38024\HEW\
ポートのためのソースコードはほとんどすべてのμC/OS-II portsのように、次の3つのファイルが主要なものです。
os_cpu.c
os_cpu_c.c
os_cpu_a.src



6.02 os_cpu.h
os_cpu.h contains processor- and implementation-specific #defines constants, macros, and typedefs. Os_cpu.h for the H8/300L port is shown in Listing 6-1 (shown in multiple sections).
Listing 6-1, os_cpu.h, Globals and Externs
#ifdef OS_CPU_GLOBALS #define OS_CPU_EXT #else #define OS_CPU_EXT extern #endif
OS_CPU_GLOBALS and OS_CPU_EXT allows us to declare global variables that are specific to this port (described later).


os_cpu.hヘッダーファイルはプロセッサーや明確な#definesやマクロやtypedefなどが含まれている。H8/300Lポート用のos_cpu.hヘッダーファイルは"Listing 6-1"で紹介しています。

Listing 6-1(os_cpu.h,Globals and Externs)
#ifdef OS_CPU_GLOBALS #define OS_CPU_EXT #else #define OS_CPU_EXT extern #endif
OS_CPU_GLOBALSやOS_CPU_EXTは私たちにこのポートのさまざまなグローバルの宣言を許可している。





Listing 6-1, os_cpu.h, Data Types
typedef unsigned char BOOLEAN;
typedef unsigned char INT8U;
typedef signed char INT8S;
typedef unsigned short INT16U; // (1)
typedef signed short INT16S;
typedef unsigned int INT32U;
typedef signed int INT32S;
typedef float FP32; // (2)
typedef double FP64;
typedef unsigned short OS_STK; // (3)
typedef unsigned char OS_CPU_SR; // (4)

L6-1(1) If you were to consult the HEW compiler documentation, you would find that an short is 16 bits and an int is 32 bits. Most H8/300L compilers should have the same definitions.

もし、HEWコンパイラドキュメントを見た場合、16ビットや32ビットのビット列を見ることでしょう。たいていのH8/300Lコンパイラは同じ定義を持っているはずです。



L6-1(2) Floating-point data types are included even though μC/OS-II doesn’t make use of floating-point numbers.

L6-1(3) A stack entry for the H8/300L processor is always 16 bits wide; thus, OS_STK is declared accordingly. All task stacks must be declared using OS_STK as its data type.

L6-1(4) The status register (the CCR) on the H8/300L processor is 8 bits wide. The OS_CPU_SR data type is used when OS_CRITICAL_METHOD #3 is used (described below). In fact, this port only supports OS_CRITICAL_METHOD #3 because it’s the preferred method for μC/OS-II ports.

H8/300LプロセッサのStatusレジスタCCRレジスタ)は8ビット幅です。OS_CRITICAL_METHOD #3が使われたとき、OS_CPU_SRデータが使用されます。実際、このポートはOS_CRITICAL_METHOD #3にだけサポートされるだけで、なぜなら、μC/OS-II portsにとって選ばれた方法です。



Listing 6-1, os_cpu.h, OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL()
#if OS_CRITICAL_METHOD == 3 #define OS_ENTER_CRITICAL() (cpu_sr = OS_CPU_SaveSR()) #define OS_EXIT_CRITICAL() (OS_CPU_RestoreSR(cpu_sr)) #endif
μC/OS-II, as with all real-time kernels, needs to disable interrupts in order to access critical sections of code and re-enable interrupts when done. μC/OS-II defines two macros to disable and enable interrupts: OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL(), respectively. μC/OS-II defines three ways to disable interrupts but, you only need to use one of the three methods for disabling and enabling interrupts.


uC/OSに限らずリアルタイムOSには、重要なプログラムにアクセスするための割り込みを禁止したり、実行時に割り込みを許可するというものが必要です。このOSでは割り込みの禁止方法が3種類あり、そのうちの1種類を使えばよいです。



The book (MicroC/OS-II, The Real-Time Kernel) describes the three different methods. The one to choose depends on the processor and compiler. For the H8/300L port, the prefered method is OS_CRITICAL_METHOD #3.
OS_CRITICAL_METHOD #3 implements OS_ENTER_CRITICAL() by writing a function that will save the status register of the CPU in a variable. OS_EXIT_CRITICAL() invokes another function to restore the status register from the variable. In the book, I recommend that you call the functions expected in OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL(): OSCPUSaveSR() and OSCPURestoreSR(), respectively. The code for these two functions is declared in os_cpu_a.src (described later).


このOSは、3種類。その1つはプロセッサーコンパイラに依存します。H8/300Lにとって、選択された方法は







Listing 6-1, os_cpu.h, Stack Growth
#define OS_STK_GROWTH 1
The stacks on the H8/300L grows from high memory to low memory and thus, OS_STK_GROWTH is set to 1 to indicate this to μC/OS-II.
Listing 6-1, os_cpu.h, Task Level Context Switch
#define OS_TASK_SW() OSCtxSw()
Task level context switches are performed when μC/OS-II invokes the macro OS_TASK_SW(). Because context switching is processor specific, OS_TASK_SW() needs to execute an assembly language function. In this case, OSCtxSw() which is declared in os_cpu_a.src.
Listing 6-1, os_cpu.h, Function Prototypes
#if OS_CRITICAL_METHOD == 3 OS_CPU_SR OS_CPU_SaveSR(void); void OS_CPU_RestoreSR(OS_CPU_SR cpu_sr); #endif
These function prototypes are associated with OS_CRITICAL_METHOD #3 described above. The actual implementation code is found in os_cpu_a.src which will be described shortly.






6.03 os_cpu_c.c
A μC/OS-II port requires that you write ten fairly simple C functions:
OSTaskStkInit() MUST be in os_cpu_c.c
OSInitHookBegin()
OSInitHookEnd()
OSTaskCreateHook()
OSTaskDelHook()
OSTaskIdleHook()
OSTaskStatHook()
OSTaskSwHook()
OSTCBInitHook()
OSTimeTickHook()
μC/OS-II requires all these functions to be declared but, OSTaskStkInit() actually needs to contain code to ‘prepare’ the stack frame of each task. OSTaskStkInit() is always placed in os_cpu_c.c along with the other nine functions. However, the nine other functions can be declared in another file if OS_CPU_HOOKS_EN is set to 0 in os_cfg.h. In fact, for the H8/300L port, this is what we did (i.e. set OS_CPU_HOOKS_EN to 0) and, we declared the nine functions (non-bold in the list above) in main.c. Because of this, all these functions in os_cpu_c.c are empty! Declaring these functions ‘outside’ the port file allows us to ‘extend’ μC/OS-II by adding application specific things such as blinking LEDs when the statistic task is executed, toggle ports during a context switch and more. Some of these will be described later.
Listing 6-2, os_cpu_c.c, OSTaskStkInit()
OS_STK *OSTaskStkInit (void (*task)(void *pd), void *p_arg, OS_STK *ptos, INT16U opt) { INT16U *pstk; opt = opt; pstk = (INT16U *)ptos;