チョコ様のCC-RLのUARTサンプルプログラム「RL78コード生成へのリングバッファ追加」にputchar/getcharを追加してprintf/scanfが出来るようにしてみた

以前にチョコ様が投稿されたCC-RLのUARTサンプルプログラム「RL78コード生成へのリングバッファ追加」に以下のputchar/getcharを追加して標準入出力関数のprintf/scanfが出来るようにしてみました。(なお、もともとチョコ様が想定されていなかったことだと思いますので、割り込み処理中に今回の標準入出力関数は実行しない方が良さそうです。)

今回、ダウンロードしたサンプルプログラムを以下のように修正して、秋月電子さんのR5F100LGAFB変換モジュールで動かしてみました。また、この際なので、シミュレータでも少し動かしてみました。(なお、私のツール環境はCS+ for CC V5.00.00とCC-RL V1.02.00です。)

・ CS+ for CCで「RL78コード生成へのリングバッファ追加」のUART_3プロジェクトを開く
・ マイコン変更(R5F100LE→R5F100LG)
・ 再度I/Oヘッダファイル生成
・ 再度コード生成
・ ソース変更(変更はr_main.cのみ 変更前変更後)
・ リビルド(リビルド後のmapファイル)

追加したputchar/getcharは以下の通りです。

/* Start user code for adding. Do not edit comment generated here */
/***********************************************************************************************************************
* Function Name: putchar
* Description  : Low level layer for printf and puts
***********************************************************************************************************************/
int putchar(int c)
{
    while( 0x00 == g_tx_ready_flag )        /* 送信完了待ち             */
    {
        NOP();
    }
    g_tx_ready_flag = 0x00;                 /* 送信完了フラグをクリア   */
    R_UART0_Send( (uint8_t *)&c, 1 );       /* データ送信処理開始 */
    return c;
}

/***********************************************************************************************************************
* Function Name: getchar
* Description  : Low level layer for scanf and gets
***********************************************************************************************************************/
int getchar(void)
{
    while ( 0x00 == chk_status() );
    NOP();
    return get_data();
}
/* End user code. Do not edit comment generated here */

そして、以下のコードを書いて試してみたところ動作してくれました。

/***********************************************************************************************************************
* Function Name: main
* Description  : This function implements main function.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{
    R_MAIN_UserInit();
    /* Start user code. Do not edit comment generated here */
    {
#if 1
        char buff[80];
            
        for(;;)
        {
            printf( "Hello!\r\nPlease enter a word or phrase:\n" );
            scanf( "%[^\n]", (char __far *)buff );
            printf( "Echo back:\n%s\n\n", (char __far *)buff );
        }
#elif 0
        char buff[80];
            
        for(;;)
        {
            puts( "Hello!\r\nPlease enter a word or phrase:" );
            gets( buff );
            puts( "Echo back:" );
            puts( buff );
            puts( "\n" );
        }
#elif 0
       略(もともとのチョコ様のプログラム)
#endif
    }
    /* End user code. Do not edit comment generated here */
}

R5F100LGAFB変換モジュールの場合(ちなみにIDE for GRのシリアルモニタウィンドウを使っています)


CS+ for CCのシミュレータの場合(以下はシミュレータGUIウィンドウの画面です)


[関連リンク]

FAQ 1011547 : RL78コンパイラCC-RLを使用していますが、printf文の出力先をUARTに変更することは可能でしょうか?

CC-RL 標準入出力関数 - CS+ V5.00.00 オンラインヘルプ

printf - Wikiペディア
scanf - Wikiペディア

  • Ayrです。

    NoMay様

    この内容はRL78Forumの「printf出力について」に関連した投稿になますよね?
    後でわかる様にリンクを張っておきます。
    japan.renesasrulz.com/.../printf
  • Dear NoMay san:
    I got error in chk_status() and get_data(), Could you kindly share your functions, thank you very much!!

    Will
  • Hi Will san,

    Please let me remind this issue for a while. Moreover, I'm sorry, please wait for a day because of doing my another issue regarding e2 studio's INDEXER and CODAN in this forum.

    By the way, since it seems that you did check a return value of chk_status() and get_data(), it seems that you can use an emulator such as E1 or E2Lite, is it right?

    Regards,
    NoMaY

  • Hi NoMaY san:

    Thanks for your reply. I just modify source code as your suggestion and try to compile it.
    but undefined symbol error occurred.

    Following is message in CS+

    (E) E3405 RA78K0R error E3405: Undefined symbol '_chk_status' in file 'DefaultBuild\r_main.rel' SAMPLE_CAN_RL78_F13.mtpj
    (E) E3405 RA78K0R error E3405: Undefined symbol '_get_data' in file 'DefaultBuild\r_main.rel' SAMPLE_CAN_RL78_F13.mtpj
    (E) E7001 VF78K0R error E7001: The link error was found. SAMPLE_CAN_RL78_F13.mtpj

    It's not a urgent case and very expecting your information.
    thank you very much!!
    本当にありがとうございました

    B.R
    Will
  • 6180.UART_3_CA.zip

    Hi Will-san, I'm チョコ and I made original UART project with Ring buffer that

    NoMaY san modified it.

    "chk_status" and "get_data" are additional functions for code generator's UART function.

    To use these functions, UART0.c  and UART0.h must be linked.

    These files are in the following page. Clicking "Download", you can get zip file for project.

    https://japan.renesasrulz.com/cafe_rene/m/sample_program/306

     

    Unfortunately, the project is for CC-RL, you must modify to use with CA78K0R.

    So, I port it to CA78K0R and build without error.

    Please refer attached zip file.

    Best regards,

    チョコ

  • Hi チョコ san,

    Thank you very much for your kind help.

    Regards,
    NoMaY