Renesas


Renesas Electronics Corporation (TSE: 6723), the world's number one supplier of microcontrollers, is a premier supplier of advanced semiconductor solutions including microcontrollers, SoC solutions and a broad range of analog and power devices.

You are viewing an older revision! See the latest version

GR PEACH knowhow database

Information

Japanese version is available in lower part of this page.
このページの後半に日本語版が用意されています.

[OS] The way to adjust the total stack size of threads

The way to adjust the total stack size of threads for Cortex-A9 CMSIS-RTOS RTX is show below.
In the current setting, you have 32Kbyte total stack size of thread. Normally, you can use it without adjustments.
However, in some cases, you need to adjust the size.

Cases you need to adjust the stack size


  • Fail to generate the thread
    If return value of "osThreadCreate()" is "NULL", your program fails to generate the thread. One possible cause is a lack of the total stack size of thread. You would be able to solve the issue by increasing the total stack size of thread.

  • User RAM size is not enough.
    When user RAM is not enough, you can reduce the total stack size of the thread.

  • Thread stack overflow
    If an error in RTOS occurs during thread execution, os_error() => mbed_die() is called.
    By setting the following macro to "1", you can see it happens and LED1 ~ LED4 to blink.
    <<code title = (mbed-src Top)\libraries\mbed\targets\hal\TARGET_RENESAS\TARGET_RZ_A1H\device.h  line 64>>
          
    1. define DEVICE_ERROR_PATTERN 0      ⇒ #define DEVICE_ERROR_PATTERN 1 <</code>>
      It is possible that thread has a stack overflow. In this case, check if the issue is solved when you increase the stack size set in osThreadDef(). Then you need to restore the stack size in osTreadDef() and add the increased size to the total stack size of thread.


How to adjust the total stack size of thread

Adjust target macro

The total stack size of the thread is defined as the macro in the configurator file of the OS as below.

(mbed-rtos Top)\rtos\rtx\TARGET_CORTEX_A\RTX_Conf_CA.c line 78

     #define  OS_PRIVSTKSIZE 8192


In Cortex-A9 CMSIS-RTOS RTX, the total stack size is defined in word units(4 bytes), actual total stack size of the thread is 4 times of OS_PRIVSTKSIZE.
In the above case, the total stack size is as follows.

  Total stack size = OS_PRIVSTKSIZE * 4 = 8192 * 4 = 32Kbyte



Relation of the total number of threads and total stack size

If you want to adjust the total number of threads, you can modify the settings of OS_PRIVCNT macro.
If the total number of threads and the total stack size are defined as below,

example of setting

#define  OS_PRIVCNT   10
#define  OS_PRIVSTKSIZE 10240

Note:mbed-rtos as of 2015/1/12, "OS_PRIVCNT" is set to "8" and "OS_PRIVSTKSIZE" is set to "8192".

The total stack size is;

  Total stack size = OS_PRIVSTKSIZE * 4 = 10240 * 4 = 40Kbyte

The above total stack size, 40Kbyte, is shared by threads defined in OS_PRIVCNT.
So the average stack size per thread can be estimated as follows.

  Total stack size / OS_PRIVCNT = 40Kbyte / 10 = 4Kbyte

However, the actual stack size of each thread can be freely set by the user. In concrete, you can specify the stack size of the thread in the third argument of osThreadDef().

stack size settings

osThreadDef(sample_thread, osPriorityNormal, 1024);
                                             ^^^^

For example, you can also set a 10 thread's stack as follows.

 Thread 1:   1 Kbyte
 Thread 2:   1 Kbyte
 Thread 3:   2 Kbyte
 Thread 4:   5 Kbyte
 Thread 5:   10 Kbyte
 Thread 6:   10 Kbyte
 Thread 7:   1 Kbyte
 Thread 8:   3 Kbyte
 Thread 9:   2 Kbyte
 Thread 10:   5 Kbyte
 --------------------------------
 sum       40 Kbyte


Plan for the method of setting total thread stack size

There's a difference between Cortex-A9 and Cortex-M regarding the method.
User program can obtain the total stack size of the thread dinamically on CMSIS-RTOS RTX of Cortex-M, on the other hand user program obtain it statically.
Renesas will migrate the dinamic method from Cortex-M to Cortex-A OS, you need to adjust it by your self until then.

The following is the information about the OS difference.

Cortex-MDynamically obtain the stack area to each thread.
⇒ during thread creation, you don't need to care about the total stack size of the thread. You can generate threads while RAM is available.
Cortex-A9Statically obtain the total stack size of the thread in the configurator settings of the OS.
⇒ it is necessary to check if it's enough and adjust it if necessary.


[other] How to run a program stored in the internal SRAM

Access performance to internal SRAM is faster than access performance to serial flash memory. By placing the program in the internal RAM, it is possible to improve the program execution performance of GR-PEACH. The way to run a program stored in the internal SRAM is below:

  1. Delete mbed library.Then Import mbed-src library.
  2. Overwite or add following files of mbed-src.

Note

Program code is stored in address below. Do not allocate variables to these address.

0x20020000 - 0x200FFFFF

How to change sct file in mbed online compiler

You can not edit scr file in mbed online compiler. Please import sct file by the way shown below. /media/uploads/RyoheiHagimoto/import.png

  1. Click "import" button on mbed compiler.
  2. Click "Upload" tag.
  3. Click "browse" button, and select file to upload.
    /media/uploads/RyoheiHagimoto/import2.png
  4. Select file to import on mbed compiler.
  5. Click "Import!" button.

[USB]How to change the USB Speed

You can switch Hi-Speed or Full-speed by the procedure below. (define 1:Hi-Speed, define 0:Full Speed)

USBHost

USBHost\USBHost\TARGET_RENESAS\TARGET_RZ_A1H\usb_host_setting.h

#define USB_HOST_HISPEED                      1
↓
#define USB_HOST_HISPEED                      0


USBDevice

\USBDevice\USBDevice\TARGET_RENESAS\TARGET_RZ_A1H\usb_function_setting.h

#define USB_FUNCTION_HISPEED                  1
↓
#define USB_FUNCTION_HISPEED                  0


[USB] How to use the USB1 function of GR-PEACH rev.C

You can use the USB1 connector by the procedure below.
(define 0:USB0、define 1:USB1)

USBHost

USBHost\USBHost\TARGET_RENESAS\TARGET_RZ_A1H\usb_host_setting.h

#define USB_HOST_CH                           0
↓
#define USB_HOST_CH                           1


USBDevice

\USBDevice\USBDevice\TARGET_RENESAS\TARGET_RZ_A1H\usb_function_setting.h

#define USB_FUNCTION_CH                       0
↓
#define USB_FUNCTION_CH                       1


Usable settings please refer to the following.

NoUSB0USB1usable
1HostDeviceOK
2DeviceHostOK
3HostHostNG
4DeviceDeviceNG


[USB]USBHost Isochronous transfer

In board-dependent part of USBHost, GR-PEACH supports isochronous transfer.
USBHost library by mbed official does not support isochronous transfer.
If you use isochronous transfer, please import following library.

Import libraryUSBHost_AddIso

I added the Isochronous to USBHost library. The Isochronous code based on the following. http://developer.mbed.org/users/va009039/code/USBHostC270_example/


Two samples below are the sample of use of the library above.

  • USB camera
    • This program converts the camera image in JPEG image, and save it to a USB flash memory.
    • Note: USBHostC270 is the C270 dedicated driver.
    • Note: This porgram set the baud rate of serial to 921600bps.
      Target device:C270

      Import programUSBHostC270_example_GR-PEACH

      supported GR-PEACH original: http://developer.mbed.org/users/va009039/code/USBHostC270_example/ The function of Isochronous has moved to USBHost_AddIso library.

  • USB DAC sample
    This program will play the file with the extension "abc" in the root directory of the USB flash memory.
    (This program supports only file with RIFF format, 48kHz, 16bit, and 2ch.)
    Target device:BSHSAU01BK

    Import programUSBHostDac_example

    This sample will play a ".wav" file of the USB root folder. Only RIFF format, 48kHz, 16bit, 2ch.





[OS]スレッドの総スタックサイズ調整方法

Cortex-A9 CMSIS-RTOS RTXの、スレッドの総スタックサイズ調整方法を説明します。
現在の設定では、スレッドの総スタックサイズを32Kbyte確保しています。
過去実績からすると調整なしでお使い頂けると思います。
ただし、調整が必要なケースがありますので、以下をご参照ください。

調整が必要なケース

以下の現象が発生した場合、スレッドの総スタックサイズを調整する必要があります。

  • スレッドの生成に失敗する
    osThreadCreate()戻り値がNULLの場合、スレッドの生成に失敗しています。
    スレッドの総スタックサイズ不足が原因の可能性があります。
    この場合、スレッドの総スタックサイズを増加させる必要があります。

  • RAMが不足し、スタックサイズを削りたい
    現在のmbed-rtosの設定では、スレッドの総スタックサイズ = 32KBとしています。
    システムとしてRAMが不足し、スタックサイズを減らしたい場合はスレッドの総スタックサイズを調整します。

  • スレッドのスタックオーバーフロー
    スレッド実行中にOS内でエラーが発生した場合、os_error()⇒mbed_die()がコールされます。
    以下のマクロを1にすることで、LED1~LED4が点滅し、エラー発生を確認できます。

    <<code title = (mbed-src Top)\libraries\mbed\targets\hal\TARGET_RENESAS\TARGET_RZ_A1H\device.h  64行目>>
          
    1. define DEVICE_ERROR_PATTERN 0      ⇒ #define DEVICE_ERROR_PATTERN 1 <</code>>
      上記現象が発生した場合、スレッドがスタックオーバーフローを起こした可能性があります。
      この場合、osThreadDef()にて指定したスタックサイズを増加させ、現象が改善するか確認ください。
      増加させたスタックサイズに合わせて、総スタックサイズも調整する必要があります。


調整方法

調整対象マクロ

調整対象となるスレッドの総スタックサイズは、OSのコンフィギュレータファイルにマクロで定義されています。
スレッドの総スタックサイズを調整する場合は、下記マクロの設定値を修正します。

(mbed-rtos Top)\rtos\rtx\TARGET_CORTEX_A\RTX_Conf_CA.c 78行目

     #define  OS_PRIVSTKSIZE 8192


Cortex-A9 CMSIS-RTOS RTXでは、ワード単位でスタックサイズを確保する仕様のため、スレッドの総スタックサイズはOS_PRIVSTKSIZE を4倍したサイズが確保されます。
上記の場合、総スタックサイズは、以下のようになります。

  総スタックサイズ = OS_PRIVSTKSIZE * 4 = 8192 * 4 = 32Kbyte



総スレッド数と総スタックサイズの関係

総スレッド数を調整する場合は、OS_PRIVCNTマクロの設定値を修正します。
総スレッド数、および総スタックサイズを以下で定義した場合、

設定例

#define  OS_PRIVCNT   10
#define  OS_PRIVSTKSIZE 10240

(※2015/1/12時点のmbed-rtosでは、それぞれ以下となっていますが、ここでは値を変更して説明します。)

   #define  OS_PRIVCNT   8
   #define  OS_PRIVSTKSIZE 8192


総スタックサイズは、

  総スタックサイズ = OS_PRIVSTKSIZE * 4 = 10240 * 4 = 40Kbyte

となります。
上記総スタックサイズ 40Kbyte を、OS_PRIVCNTにて定義したスレッド数で分け合います。
目安として、1スレッドあたりの平均スタックサイズは、以下のように見積もれます。

  総スタックサイズ / OS_PRIVCNT = 40Kbyte / 10 = 4Kbyte

各スレッドのスタックサイズの内訳はユーザが自由に設定できます。
具体的にはソースコード上で、osThreadDef()を定義した際、第三引数でスレッドのスタックサイズを指定します。

スレッドのスタックサイズの指定

osThreadDef(sample_thread, osPriorityNormal, 1024);
                                             ^^^^

例えば、以下のように10スレッドのスタックを設定することもできます。

 Thread 1:   1 Kbyte
 Thread 2:   1 Kbyte
 Thread 3:   2 Kbyte
 Thread 4:   5 Kbyte
 Thread 5:   10 Kbyte
 Thread 6:   10 Kbyte
 Thread 7:   1 Kbyte
 Thread 8:   3 Kbyte
 Thread 9:   2 Kbyte
 Thread 10:   5 Kbyte
 ――――――――――
合計       40 Kbyte


今後の対応

Cortex-A9とCortex-MとでCMSIS-RTOS RTXの仕様に以下の差分があります。
Cortex-M用 CMSIS-RTOS RTXでは、ユーザがスレッドの総スタックサイズを意識しないでよい仕様となっておりますが、Cortex-A9用 CMSIS-RTOS RTXではユーザにスレッドの総スタックサイズの調整を任せる仕様となっています。
Cortex-A9の仕様をCortex-Mの仕様に合わせる方針(対応調整中)ですが、それまでの間、ユーザ側でのスレッドの総スタックサイズ調整(不足時)が必要になります。

以下、OS差分に関する情報です。

Cortex-Mスタック領域をスレッド別に動的に確保。
⇒スレッド生成時、スレッドの総スタックサイズを意識しないでよい。
RAMが許す限り、スレッドを生成できる。
Cortex-A9スレッドの総スタックサイズをOSのコンフィギュレータ設定にて静的に確保。
⇒総スタックサイズを最初に検討/調整する必要がある。
コンフィギュレータにて設定したスタックサイズ内でスレッドを生成する必要がある。

[other] 内蔵RAMに格納されたプログラムを実行する方法

プログラムを、GR-PEACH標準のシリアルフラッシュメモリよりもアクセス性能の良い内蔵RAMに配置することで、GR-PEACHのプログラム実行性能を向上させることができます。

  1. mbedライブラリを削除し、mbed-srcライブラリをimportしてください
  2. mbed-src内の、以下の各ファイルを上書きまたは追加してください

注意:

RAM実行時、以下のRAM領域はコード領域となります。
変数などのデータは配置しないでください。

0x20020000 - 0x200FFFFF

sctファイルの変更方法

sctファイルはmbedコンパイラ上で編集することができません。以下の手法でインポートしてください。
/media/uploads/RyoheiHagimoto/import.png

  1. mbed compiler 上で 「import」をクリックします
  2. 「Upload」タグをクリックします
  3. 「参照」をクリックし、アップロードするファイルを選択します
    /media/uploads/RyoheiHagimoto/import2.png
  4. mbed compiler上でインポートするファイルを選択します
  5. 「Import!」ボタンをクリックします

[USB]USB Speedの切り替え

以下の手順でHi-SpeedとFull Speedの切り替えができます。
(define 1:Hi-Speed、define 0:Full Speed)

USBHost

USBHost\USBHost\TARGET_RENESAS\TARGET_RZ_A1H\usb_host_setting.h

#define USB_HOST_HISPEED                      1
↓
#define USB_HOST_HISPEED                      0


USBDevice

\USBDevice\USBDevice\TARGET_RENESAS\TARGET_RZ_A1H\usb_function_setting.h

#define USB_FUNCTION_HISPEED                  1
↓
#define USB_FUNCTION_HISPEED                  0


[USB]rev.Cボード USB 2ポート設定方法

以下の手順で使用するUSBのポートを指定できます。
(define 0:USB0、define 1:USB1)
注意:rev.Cボードのみの機能です。rev.B以前のボードではUSBポートの変更はできません。

USBHost

USBHost\USBHost\TARGET_RENESAS\TARGET_RZ_A1H\usb_host_setting.h

#define USB_HOST_CH                           0
↓
#define USB_HOST_CH                           1


USBDevice

\USBDevice\USBDevice\TARGET_RENESAS\TARGET_RZ_A1H\usb_function_setting.h

#define USB_FUNCTION_CH                       0
↓
#define USB_FUNCTION_CH                       1


Host、Deviceの組み合わせは下記を参照ください。

NoUSB0USB1設定可否
1HostDevice可(OK)
2DeviceHost可(OK)
3HostHost不可(NG)
4DeviceDevice不可(NG)

[USB]USBHost アイソクロナス転送

USBHostのボード依存部において、アイソクロナス転送に対応しています。
mbedオフィシャルのUSBHostではアイソクロナス転送のインターフェースが存在しないため、以下のライブラリをご用意しました。

Import libraryUSBHost_AddIso

I added the Isochronous to USBHost library. The Isochronous code based on the following. http://developer.mbed.org/users/va009039/code/USBHostC270_example/


また、上記ライブラリの使用例として、USBカメラとUSB Dacのサンプルを公開しました。

  • USBカメラサンプル
    カメラの画像をjpegに変換し、USBメモリに保存します。
    注意:USBHostC270はc270専用ドライバです。
    注意:printfを表示させるためには、PCとのシリアルポート ボー・レートを921600に変更する必要があります。
    動作確認デバイス:c270

    Import programUSBHostC270_example_GR-PEACH

    supported GR-PEACH original: http://developer.mbed.org/users/va009039/code/USBHostC270_example/ The function of Isochronous has moved to USBHost_AddIso library.

  • USB Dacサンプル
    USBルートフォルダにある拡張子".wav"のファイルを再生します。(RIFF形式、48kHZ、16bit、2chのみ対応)
    動作確認デバイス:BSHSAU01BK

    Import programUSBHostDac_example

    This sample will play a ".wav" file of the USB root folder. Only RIFF format, 48kHz, 16bit, 2ch.


All wikipages