2013年7月30日火曜日

websocket RX63N その2

とりあえず、勝手にRX63Nからechoクライアントへデータ送信できるようにしてみた。

2013.07.30

2013年7月29日月曜日

websocket RX63N

7/25 発売のCQ出版のInterface 誌は HTML5の特集です。
で、これをRX63Nで動作させられないかと記事を読んだのですが、わからん。

cwebsocketを発見したので、これで動作確認してみる。
http://code.google.com/p/cwebsocket/

解凍してbase64_enc.c, sha1.c, websocket.c x86_server/main.c を RX63Nのプロジェクトに組み込みます。
RX63Nのコンパイルオプションでは C99を選択します。
 tcp_main::  
 void tcp_start(void)  
 {  
      W     size;  
      ER     ercd;  
      char     ver[128];  
      printk("%s", (char*)R_t4_version.library);  
      /* start LAN controller */  
      ercd = lan_open();  
      if (ercd != E_OK)  
      {  
           printk("lan_open %08x\n",ercd);  
           while (1);  
      }  
      /* initialize TCP/IP */  
      size = tcpudp_get_ramsize();  
      if (size > (sizeof(tcpudp_work)))  
      {  
           while (1);  
      }  
      ercd = tcpudp_open(tcpudp_work);  
      if (ercd != E_OK)  
      {  
           while (1);  
      }  
 ...     略  
 config_tcpudp.c::  
 /*** Definition of TCP reception point (only port number needs to be set) ***/  
 const T_TCP_CREP tcp_crep[] =  
 {  
      /* { attribute of reception point, {local IP address, local port number}} */  
      { 0x0000, { 0, 8088 }},  
 };  
 /* Total number of TCP reception points */  
 const H __tcprepn = sizeof(tcp_crep) / sizeof(T_TCP_CREP);  
 /*** Definition of TCP communication end point  
    (only receive window size needs to be set) ***/  
 const T_TCP_CCEP tcp_ccep[] =  
 {  
      /* { attribute of TCP communication end point,  
         top address of transmit window buffer, size of transmit window buffer,  
         top address of receive window buffer, size of receive window buffer,  
         address of callback routine }  
      */  
      { 0, 0, 0, 0, 1460, 0 },  
 };  
 ...     略  
x86_server/main.c -> echo_srv.c::  
 #include "websocket.h"  
 #define BUF_LEN 512  
 void clientWorker(ID cepid);  
 /******************************************************************************  
 Exported global variables and functions (to be accessed by other files)  
 ******************************************************************************/  
 void error(const char *msg)  
 {  
   //エラー処理省き  
   return;  
 }  
 int safeSend(ID cepid, const uint8_t *buffer, size_t bufferSize)  
 {  
   ER written;  
      written = tcp_snd_dat(cepid, buffer, bufferSize, TMO_FEVR);  
   if (written < 0) {  
           tcp_sht_cep(cepid);  
           tcp_cls_cep(cepid, TMO_FEVR);  
     return EXIT_FAILURE;  
   }  
   return EXIT_SUCCESS;  
 }  
 void clientWorker(ID cepid)  
 {  
   uint8_t buffer[BUF_LEN];  
   memset(buffer, 0, BUF_LEN);  
   size_t readedLength = 0;  
   size_t frameSize = BUF_LEN;  
   enum wsState state = WS_STATE_OPENING;  
   uint8_t *data = NULL;  
   size_t dataSize = 0;  
   enum wsFrameType frameType = WS_INCOMPLETE_FRAME;  
   struct handshake hs;  
   nullHandshake(&hs);  
   #define prepareBuffer frameSize = BUF_LEN; memset(buffer, 0, BUF_LEN);  
   #define initNewFrame frameType = WS_INCOMPLETE_FRAME; readedLength = 0; memset(buffer, 0, BUF_LEN);  
   printk(" connect websocket srv\n");  
   while (frameType == WS_INCOMPLETE_FRAME) {  
     ER readed = tcp_rcv_dat(cepid, buffer+readedLength, BUF_LEN-readedLength, TMO_FEVR);  
     if (readed <= 0) {  
                tcp_sht_cep(cepid);  
                tcp_cls_cep(cepid, TMO_FEVR);  
          return;  
     }  
     readedLength+= readed;  
     //assert(readedLength <= BUF_LEN);  
           printk("readed=%d\n",readed);  
     if (state == WS_STATE_OPENING) {  
       frameType = wsParseHandshake(buffer, readedLength, &hs);  
     } else {  
       frameType = wsParseInputFrame(buffer, readedLength, &data, &dataSize);  
     }  
     if ((frameType == WS_INCOMPLETE_FRAME && readedLength == BUF_LEN) || frameType == WS_ERROR_FRAME) {  
       if (frameType == WS_INCOMPLETE_FRAME)  
         printk("buffer too small\n");  
       else  
         printk("error in incoming frame\n");  
       if (state == WS_STATE_OPENING) {  
         prepareBuffer;  
         frameSize = sprintf((char *)buffer,  
                   "HTTP/1.1 400 Bad Request\r\n"  
                   "%s%s\r\n\r\n",  
                   versionField,  
                   version);  
         safeSend(cepid, buffer, frameSize);  
         break;  
       } else {  
         prepareBuffer;  
         wsMakeFrame(NULL, 0, buffer, &frameSize, WS_CLOSING_FRAME);  
         if (safeSend(cepid, buffer, frameSize) == EXIT_FAILURE)  
           break;  
         state = WS_STATE_CLOSING;  
         initNewFrame;  
       }  
     }  
     if (state == WS_STATE_OPENING) {  
       //assert(frameType == WS_OPENING_FRAME);  
       if (frameType == WS_OPENING_FRAME) {  
         // if resource is right, generate answer handshake and send it  
         if (strcmp(hs.resource, "/echo") != 0) {  
           frameSize = sprintf((char *)buffer, "HTTP/1.1 404 Not Found\r\n\r\n");  
           if (safeSend(cepid, buffer, frameSize) == EXIT_FAILURE)  
             break;  
         }  
         prepareBuffer;  
         wsGetHandshakeAnswer(&hs, buffer, &frameSize);  
         if (safeSend(cepid, buffer, frameSize) == EXIT_FAILURE)  
           break;  
         state = WS_STATE_NORMAL;  
         initNewFrame;  
       }  
     } else {  
       if (frameType == WS_CLOSING_FRAME) {  
         if (state == WS_STATE_CLOSING) {  
           break;  
         } else {  
           prepareBuffer;  
           wsMakeFrame(NULL, 0, buffer, &frameSize, WS_CLOSING_FRAME);  
           safeSend(cepid, buffer, frameSize);  
           break;  
         }  
       } else if (frameType == WS_TEXT_FRAME) {  
         uint8_t *recievedString = NULL;  
         recievedString = malloc(dataSize+1);  
         //assert(recievedString);  
         memcpy(recievedString, data, dataSize);  
         recievedString[ dataSize ] = 0;  
         prepareBuffer;  
         wsMakeFrame(recievedString, dataSize, buffer, &frameSize, WS_TEXT_FRAME);  
         if (safeSend(cepid, buffer, frameSize) == EXIT_FAILURE)  
           break;  
         initNewFrame;  
       }  
     }  
   } // read/write cycle  
      tcp_sht_cep(cepid);  
      tcp_cls_cep(cepid, TMO_FEVR);  
 }  
 void websocket_srv(ID id)  
 {  
      ID cepid, repid;  
      ER ercd;  
   T_IPV4EP dst_addr;  
      /* setting of reception point and communucation end point */  
      cepid = id;  
      repid = id;  
      /* TCP wait connection */  
      ercd = tcp_acp_cep(cepid, repid, &dst_addr, TMO_FEVR);  
      if (ercd == E_OK)  
      {  
           clientWorker(cepid);  
      }  
      /* shutdown */  
      tcp_sht_cep(cepid);  
      tcp_cls_cep(cepid, TMO_FEVR);  
      return;  
 }  
あとは htons() がないといわれたら、
 #include <machine.h>  
 #define htons(x) rev(x)  
とか

ネットで参考になった記事は
http://d.hatena.ne.jp/mzp/20110123/websokcet

2013.07.29



2013年7月26日金曜日

RX600シリーズ シリアルデバッグモニタ

http://japan.renesas.com/support/seminar/sample_program/sample_rx600/index.jsp
でシリアルデバッグモニタが公開されている。
Htermとか懐かしいな~。
2013.07.25

2013年7月24日水曜日

FreeRTOS RX63N その2

手元のGR-SAKURA に搭載されている Ethernet PHYは LAN8720 です。
このチップの LED1:LINKSTA端子は LINK/ACTIVE 表示です。
この意味は、LINK状態なら、High->LowまたはLow->High になり
ACTIVE状態(キャリア信号を検知)で、点滅となります。

ところで、 M4S-T4 Tiny のコード r_ether.c では LINK表示のみを期待してます。
したがって、以下の修正が必要とおもわれます。


 r_ether.c::  
 -  EDMAC.EESIPR.BIT.ECIIP  = 1;  
 +  EDMAC.EESIPR.BIT.ECIIP  = 0;  
 r_ether.c::R_ETHER_LinkProcess()::  
 {  
 +  uint16_t link_status=0;  
 +  link_status=Phy_GetLinkStatus();  
 +  if (link_status == R_PHY_OK && g_ether_TransferEnableFlag == ETHER_FLAG_OFF)  
 +  {  
 +       g_ether_LchngFlag=ETHER_FLAG_ON_LINK_ON;  
 +  }   
 +  if (link_status == R_PHY_ERROR && g_ether_TransferEnableFlag == ETHER_FLAG_ON)  
 +  {  
 +       g_ether_LchngFlag=ETHER_FLAG_ON_LINK_OFF;  
 +  }  
2013.07.24

2013年7月23日火曜日

FreeRTOS RX63N動作確認

FreeRTOS/RX63N に M3S-T4 Tiny TCP/IP プロトコルスタックを組み合わせた例が
http://www.renesasrtossolutions.org/jpn/
にありました。

M3S-T4 Tiny TCP/IPプロトコル・スタックについては
http://www.kumikomi.net/archives/2004/12/21tcpip1.php
に紹介されているものに類似しているように感じます。

GR-SAKURAボードで動作確認する場合の
主な変更箇所をかいてみます

hwsetup.c::ConfigurePortPins()::
    /* ==== RMII Pins setting ==== */
       /*
    Pin Functions : Port
    --------------------
    ET_MDC        : PA3
    ET_MDIO       : PA4
    ET_LINKSTA    : PA5(not use)
    RMII_RXD1     : PB0
    RMII_RXD0     : PB1
    REF50CK       : PB2
    RMII_RX_ER    : PB3
    RMII_TXD_EN   : PB4
    RMII_TXD0     : PB5
    RMII_TXD1     : PB6
    RMII_CRS_DV   : PB7
    */

    /* Clear PDR and PMR */
    PORTA.PDR.BIT.B3 = 0;
    PORTA.PDR.BIT.B4 = 0;
    PORTA.PDR.BIT.B5 = 0;
    
    PORTB.PDR.BIT.B0 = 0;
    PORTB.PDR.BIT.B1 = 0;
    PORTB.PDR.BIT.B2 = 0;
    PORTB.PDR.BIT.B3 = 0;
    PORTB.PDR.BIT.B4 = 0;
    PORTB.PDR.BIT.B5 = 0;
    PORTB.PDR.BIT.B6 = 0;
    PORTB.PDR.BIT.B7 = 0;
    
    PORTA.PMR.BIT.B3 = 0;
    PORTA.PMR.BIT.B4 = 0;
    PORTA.PMR.BIT.B5 = 0;
    
    PORTB.PMR.BIT.B0 = 0;
    PORTB.PMR.BIT.B1 = 0;
    PORTB.PMR.BIT.B2 = 0;
    PORTB.PMR.BIT.B3 = 0;
    PORTB.PMR.BIT.B4 = 0;
    PORTB.PMR.BIT.B5 = 0;
    PORTB.PMR.BIT.B6 = 0;
    PORTB.PMR.BIT.B7 = 0;

    /* Write protect off */
    MPC.PWPR.BYTE = 0x00;       /* PWPR.PFSWE write protect off */
    MPC.PWPR.BYTE = 0x40;       /* PFS register write protect off */

    /* Select pin function */
    MPC.PA3PFS.BYTE = 0x11;
    MPC.PA4PFS.BYTE = 0x11;
    MPC.PA5PFS.BYTE = 0x11;
    
    MPC.PB0PFS.BYTE = 0x12;
    MPC.PB1PFS.BYTE = 0x12;
    MPC.PB2PFS.BYTE = 0x12;
    MPC.PB3PFS.BYTE = 0x12;
    MPC.PB4PFS.BYTE = 0x12;
    MPC.PB5PFS.BYTE = 0x12;
    MPC.PB6PFS.BYTE = 0x12;
    MPC.PB7PFS.BYTE = 0x12;

    /* Write protect on */
    MPC.PWPR.BYTE = 0x80;       /* PFS register write protect on */

    /* Select ethernet mode */
    MPC.PFENET.BIT.PHYMODE = 0; /* RMII mode */

    /* Switch to the selected input/output function */
    PORTA.PMR.BIT.B3 = 1;
    PORTA.PMR.BIT.B4 = 1;
    PORTA.PMR.BIT.B5 = 1;
    
    PORTB.PMR.BIT.B0 = 1;
    PORTB.PMR.BIT.B1 = 1;
    PORTB.PMR.BIT.B2 = 1;
    PORTB.PMR.BIT.B3 = 1;
    PORTB.PMR.BIT.B4 = 1;
    PORTB.PMR.BIT.B5 = 1;
    PORTB.PMR.BIT.B6 = 1;
    PORTB.PMR.BIT.B7 = 1;
    
phy.c::
#define PHY_MII_ADDR         (0x00)

r_ether.h::
#define ETH_MODE_SEL         ETH_RMII_MODE
 here.
2013.07.23

2013年7月22日月曜日

TNKernel-RX63N Hew版

https://github.com/msalov/TNKernel-RX にあるTNKernel -RX62Nを HEWでコンパイルできないかやってみた。

HEW環境では割り込みベクタの設定を #pragma interruptで指定出来ます。
GCC では
_isr_vectors[VECT(RTC,PRD)] = rtc_irq_handler;
としたものが #pragma interrupt (rtc_irq_handler(vect=VECT(RTC,PRD)))
と出来ます。またアセンブラでは .rvector指令で
    .rvector TRAP_SAVE_SR, _tn_trap_cpu_save_sr

_tn_trap_cpu_save_sr:

    push.l    r2
とするこで C$VECTに割り込み処理先を登録してくれます。

1) tn_port_rx_gcc.S -> tn_port_rx_rxc.src
; -----------------------------------------------------------------------------
;
; TNKernel RTOS port for RX core
;
; Copyright ゥ 2011, 2012 Maxim Salov
; All rights reserved.
;
; Assembler:   GCC
;
; Permission to use, copy, modify, and distribute this software in source
; and binary forms and its documentation for any purpose and without fee
; is hereby granted, provided that the above copyright notice appear
; in all copies and that both that copyright notice and this permission
; notice appear in supporting documentation.
;
; THIS SOFTWARE IS PROVIDED BY THE YURI TIOMKIN AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; ARE DISCLAIMED. IN NO EVENT SHALL YURI TIOMKIN OR CONTRIBUTORS BE LIABLE
; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
; SUCH DAMAGE.
; -----------------------------------------------------------------------------

;    .section .text

    ;; External references
    
    .glb   _tn_curr_run_task
    .glb   _tn_next_task_to_run
    .glb   _tn_system_state

    ;; Public functions declared in this file

    .glb  _tn_startup_hardware_init
    .glb  _tn_startup
    .glb  _tn_switch_context
    .glb  _tn_switch_context_exit
    .glb  _tn_switch_context_irq_handler
    .glb  _tn_trap_cpu_save_sr
    .glb  _tn_trap_cpu_restore_sr
    .glb  _tn_icpu_save_sr
    .glb  _tn_icpu_restore_sr
    .glb  _tn_cpu_save_sr
    .glb  _tn_cpu_restore_sr
    .glb  _tn_start_exe
    .glb  _tn_chk_irq_disabled
    .glb  _tn_int_exit
    .glb  _tn_cpu_int_enable
    .glb  _tn_inside_int

    ;; Addresses and bits for SWINT
    
SWINTR        .equ    000872E0h
SWINT        .equ    1
IPR03        .equ    00087303h
SWINTPR        .equ    1
IER03        .equ    00087203h
IEN3        .equ    3
IR027        .equ    0008701Bh

__stack     .equ     20000h

TN_ST_STATE_RUNNING    .equ        1
    
    ;; Bit definitions for PSW register
    
I_BIT_N        .equ    16
I_BIT        .equ    (1 << I_BIT_N)

U_BIT_N        .equ    17
U_BIT        .equ    (1 << U_BIT_N)

PM_BIT_N    .equ    20
PM_BIT    .equ        (1 << PM_BIT_N)

IPL_BITS    .equ    0F000000h

    ;; Trap numbers
    
TRAP_SAVE_SR    .equ    10
TRAP_RESTORE_SR    .equ    11

;/* ------------------------------------------------------------------------ */
    .section P,CODE
;/* ------------------------------------------------------------------------ */

;/* ------------------------------------------------------------------------ */

_tn_startup_hardware_init:

    pop    r1        ; save return address
    
    mov.l    #__stack, r2    ; get top of RAM _\stack
    mvtc    r2, isp        ; set ISP to top of RAM
    add    #-512, r2    ; reserve some space for ISP
    mvtc    r2, usp        ; set USP
    setpsw    U        ; switch to USP

    push.l    r1        ; restore return address
    
    rts
;/* ------------------------------------------------------------------------ */

_tn_startup:    
        ;; Enable SWINT (software interrupt), which is used to switch context
    mov.l    #IER03, r1
    mov.l    #IPR03, r2
     mov.l    #IR027, r3
    bclr    #IEN3, [r1].b    ; Disable SWINT
    mov.b    #SWINTPR, [r2]    ; Set SWINT priority to lowest
    mov.b    #0, [r3]    ; Reset interrupt request
    bset    #IEN3, [r1].b    ; Enable SWINT

    rts
;/* ------------------------------------------------------------------------ */

_tn_start_exe:

    ;; Interrupts must be disabled for now, but we should not trust user :/
    clrpsw    I

    ;; Set system state to TN_ST_STATE_RUNNING
    mov.l    #_tn_system_state, r1
    mov.l    #TN_ST_STATE_RUNNING, [r1]

    ;; Make request for context switch
    ;; IMHO this step is unnecessary, since we initially switch to idle task, and additional context switch will not lead us to timer task
    ;; bsr    _tn_switch_context

    ;; Reset interrupt stack pointer into initial state
    mov.l    #__stack, r1
    mvtc    r1, isp

    ;; Prepare for first context switch
    ;; Read current task pointer and write it to USP (user stack pointer)
    mov.l    #_tn_curr_run_task, r1
    mov.l    [r1], r1
    mov.l    [r1], r1
    mvtc    r1, usp
    
    ;; Restore PC and PSW from user stack and place it into interrupt stack
    setpsw    U
    popm    r1-r2
    clrpsw    U
    pushm    r1-r2

    ;; Read task context
    setpsw    U
    popm    r1-r15                        ; Restore context of new task
    ;; Switch back to interrupt stack
    clrpsw    U

    ;; "Return" from interrupt. This will fetch task's address and PSW value with already enabled interrupts, user mode and user stack pointer
    rte
    ;; We should not reach this point
?:
    bra.b  ?-

;/* ------------------------------------------------------------------------ */

_tn_switch_context_exit:

    ;; Enable interrupts
    mov.l    #1, r1
    bsr        _tn_cpu_restore_sr
    ;; Make request for context switch
    bsr        _tn_switch_context
    ;; We should not reach this point
?:
    bra.b      ?-

;/* ------------------------------------------------------------------------ */

_tn_switch_context_irq_handler_rte:

    popm    r1-r2
    rte
    
    .rvector 27, _tn_switch_context_irq_handler
    
_tn_switch_context_irq_handler:

    pushm    r1-r2
    ;; The context switching - to do or not to do, that is the question :)
    mov.l    #_tn_curr_run_task, r1
    mov.l    #_tn_next_task_to_run, r2
    mov.l    [r2], r2
    mov.l    [r1], r1                    ; If both pointers reference one task - not to do

    cmp        r1, r2
    beq.b    _tn_switch_context_irq_handler_rte
    ;; to do
    popm    r1-r2
    
    ;/* -------------------------------------- */

    ;; Save task context
    setpsw    U                        ; Switch to the stack of previous task
    pushm    r1-r15                        ; Save context

    ;; Save PC and PSW of current task (located on top of isp) into task stack (usp)
    clrpsw    U
    popm    r1-r2
    setpsw    U
    pushm    r1-r2

    ;; Prepare to save/switch/restore stack pointers
    mov.l    #_tn_curr_run_task, r1
    mov.l    [r1], r5                    ; Read TCB address of current task
    mov.l    #_tn_next_task_to_run, r2
    mov.l    [r2], r6                    ; Read TCB address of next task
    ;; Save current stack pointer
    mvfc    usp, r3
    mov.l    r3, [r5]
    ;; Set next task as current one
    mov.l    r6, [r1]
    ;; Restore stack pointer of next task
    mov.l    [r6], r3
    mvtc    r3, usp

    ;; Restore PC and PSW from user stack and place it into interrupt stack
    setpsw    U
    popm    r1-r2
    clrpsw    U
    pushm    r1-r2

    ;; Read task context
    setpsw    U
    popm    r1-r15                        ; Restore context of new task
    ;; Switch back to interrupt stack
    clrpsw    U

    ;/* -------------------------------------- */

    rte

;/* ------------------------------------------------------------------------ */

_tn_switch_context:
_tn_int_exit:

    mov.l    #SWINTR, r1
    mov.b    #SWINT, [r1]                    ; Trigger software interrupt
    rts

;/* ------------------------------------------------------------------------ */
    .rvector TRAP_SAVE_SR, _tn_trap_cpu_save_sr

_tn_trap_cpu_save_sr:

    push.l    r2

    mov.l    8[r0], r2                    ; Read PSW value from stack
    mov.l    r2, r1
    bclr    #I_BIT_N, r2                ; Reset I bit in saved PSW
    mov.l    r2, 8[r0]                    ; Store PSW value to stack

    and        #I_BIT, r1                    ; Isolate I bit

    pop        r2
    rte

;/* ------------------------------------------------------------------------ */
    .rvector TRAP_RESTORE_SR, _tn_trap_cpu_restore_sr

_tn_trap_cpu_restore_sr:

    push.l    r2

    mov.l    8[r0], r2                    ; Read PSW value from stack
    cmp        #0, r1                        ; Check if we need to set I bit
    bmnz    #I_BIT_N, r2                ; Enable/disable interrupts in saved PSW
    mov.l    r2, 8[r0]                    ; Store PSW value to stack

    pop        r2
    rte

;/* ------------------------------------------------------------------------ */

_tn_cpu_save_sr:

    int    #TRAP_SAVE_SR
    rts

;/* ------------------------------------------------------------------------ */

_tn_cpu_restore_sr:

    int    #TRAP_RESTORE_SR
    rts

;/* ------------------------------------------------------------------------ */

_tn_icpu_save_sr:

    mvfc    psw, r1
    clrpsw    I
    and        #I_BIT, r1
    rts

;/* ------------------------------------------------------------------------ */

_tn_icpu_restore_sr:

    mvfc    psw, r2
    cmp        #0, r1
    bmnz    #I_BIT_N, r2
    mvtc    r2, psw
    rts

;/* ------------------------------------------------------------------------ */

_tn_chk_irq_disabled:

    mvfc    psw, r1
    and        #I_BIT, r1                    ; Isolate I bit
    bnot    #I_BIT_N, r1                ; Invert I bit
    rts

;/* ------------------------------------------------------------------------ */

_tn_cpu_int_enable:
    ;; This function is invoked as a starting point of tn_timer_task_func.
    ;; But interrupts are already enabled, because the only way there -- to
    ;; perform context switch, that is done in SWINT.

    ;; But lets implement it any way
    mov.l    #1, r1
    bsr        _tn_cpu_restore_sr
    rts

;/* ------------------------------------------------------------------------ */

_tn_inside_int:

    mvfc    psw, r1
    and        #U_BIT, r1
    bnot    #U_BIT_N, r1
    rts

;/* ------------------------------------------------------------------------ */

    .end


2) tn_user.c
/*
TNKernel real-time kernel

Copyright ゥ 2011 Maxim Salov
All rights reserved.

Permission to use, copy, modify, and distribute this software in source
and binary forms and its documentation for any purpose and without fee
is hereby granted, provided that the above copyright notice appear
in all copies and that both that copyright notice and this permission
notice appear in supporting documentation.

THIS SOFTWARE IS PROVIDED BY THE YURI TIOMKIN AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL YURI TIOMKIN OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/

#include <iodefine.h>
#include <grsakurarx63ndef.h>
#include <tn.h>
#include <tn_user.h>
#include "sci2.h"
//----------------------------------------------------------------------------
// RTC initialization routine
//----------------------------------------------------------------------------

void rtc_init(void)
{
    /* For detailed description please refer to hardware manual */
    SYSTEM.PRCR.WORD = 0xA503;                  
    RTC.RCR3.BYTE           = 0x0c;
    SYSTEM.SOSCCR.BIT.SOSTP = 0;                         /* Enable SubClock circuit */
    SYSTEM.PRCR.WORD = 0xA500;                  

    RTC.RCR3.BIT.RTCEN      = 1;
    RTC.RCR2.BIT.START      = 0;
    while (0 != RTC.RCR2.BIT.START);
    RTC.RCR2.BIT.RESET      = 1;
    while (1 == RTC.RCR2.BIT.RESET);
    /* Set initial time and date */
    RTC.RSECCNT.BYTE        = 0;
    RTC.RMINCNT.BYTE        = 0;
    RTC.RHRCNT.BYTE         = 0;
    RTC.RDAYCNT.BYTE        = 0;
    RTC.RWKCNT.BYTE         = 0;
    RTC.RMONCNT.BYTE        = 0;
    RTC.RYRCNT.WORD         = 0x2012;
    /* Set periodic interrupt */
    RTC.RCR1.BYTE           = 0x64;
//    RTC.RCR1.BYTE           = 0xc4;
//    RTC.RCR1.BIT.PES        = 6;                             /* Set periodic interrupts frequency to 256 Hz */
//    RTC.RCR1.BIT.PIE        = 1;                             /* Enable periodic interrupts */
//    _isr_vectors[VECT(RTC,PRD)] = rtc_irq_handler;
    IR(RTC,PRD)             = 0;                             /* Clear interrupt flag */
    IPR(RTC,PRD)            = 3;                            /* Set priority level */
    IEN(RTC,PRD)            = 1;                             /* Enable interrupt in ICU */
    RTC.RCR2.BIT.START      = 1;                             /* Start RTC */
    while (1 != RTC.RCR2.BIT.START);
}

//----------------------------------------------------------------------------
// RTC periodic interrupt
//----------------------------------------------------------------------------
#pragma interrupt (rtc_irq_handler(vect=VECT(RTC,PRD)))
//interrupt_attr
void rtc_irq_handler(void)
{
    static unsigned char free_counter;
    
    free_counter++;
    if(free_counter > 63) { 
        LEDA6 ^= 1;
        free_counter=0;
    }
    tn_tick_int_processing();
    tn_int_exit();
}

//----------------------------------------------------------------------------


3)test.c
/*

  TNKernel real-time kernel - YRDKRX62N test  (RX core)

  Copyright ゥ 2011 Maxim Salov
  All rights reserved.

  Permission to use, copy, modify, and distribute this software in source
  and binary forms and its documentation for any purpose and without fee
  is hereby granted, provided that the above copyright notice appear
  in all copies and that both that copyright notice and this permission
  notice appear in supporting documentation.

  THIS SOFTWARE IS PROVIDED BY THE YURI TIOMKIN AND CONTRIBUTORS ``AS IS'' AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL YURI TIOMKIN OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  SUCH DAMAGE.
*/

/*============================================================================
  A basic test - (Hello-LED-World)
*===========================================================================*/

#include <tn.h>
#include <machine.h>
#include <iodefine.h>
#include <grsakurarx63ndef.h>
#include "tn_user.h"
#include "hwsetup.h"
#include "sci2.h"

//----------- Tasks ----------------------------------------------------------

#define  TASK_0_PRIORITY 7
#define  TASK_1_PRIORITY 10
#define  TASK_2_PRIORITY 12

#define  TASK_0_STK_SIZE 128
#define  TASK_1_STK_SIZE 128
#define  TASK_2_STK_SIZE 128

unsigned int task_0_stack[TASK_0_STK_SIZE];
unsigned int task_1_stack[TASK_1_STK_SIZE];
unsigned int task_2_stack[TASK_2_STK_SIZE];

TN_TCB  task_0;
TN_TCB  task_1;
TN_TCB  task_2;

void task_0_func(void * par );
void task_1_func(void * par );
void task_2_func(void * par );

//----------------------------------------------------------------------------
void tcp_main(void);

void main (void)
{  
   
   HardwareSetup();
   sci2_init();
   rtc_init();

   printk("\nStart System\n");
    
   tn_start_system();

   ...以下略

4)Resetprg.c
/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No 
* other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all 
* applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
* THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, 
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM 
* EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES 
* SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS 
* SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of 
* this software. By using this software, you agree to the additional terms and conditions found by accessing the 
* following link:
* http://www.renesas.com/disclaimer 
*
* Copyright (C) 2012 Renesas Electronics Corporation. All rights reserved.    
***********************************************************************************************************************/
/***********************************************************************************************************************
* File Name    : resetprg.c
* Version      : 1.01
* Description  : Power on reset
***********************************************************************************************************************/
/**********************************************************************************************************************
* History : DD.MM.YYYY Version  Description
*         : 22.04.2011 1.00     First Release
*         : 21.08.2012 1.01     Change to supervisor mode
***********************************************************************************************************************/


/***********************************************************************************************************************
Includes   <System Includes> , "Project Includes"
***********************************************************************************************************************/
#include    <machine.h>
#include    <_h_c_lib.h>
#include    <stdint.h>
#include    "typedefine.h"
#include    "iodefine.h"
#include    "sci2.h"
//#include    "isr_vectors.h"

/***********************************************************************************************************************
Macro definitions
***********************************************************************************************************************/
#define PSW_init  0x00010000
#define FPSW_init 0x00000100
/***********************************************************************************************************************
Typedef definitions
***********************************************************************************************************************/

/***********************************************************************************************************************
Imported global variables and functions (from other files)
***********************************************************************************************************************/
//#ifdef __cplusplus                // Use SIM I/O
//extern "C" {
//#endif
//extern void _INIT_IOLIB(void);
//extern void _CLOSEALL(void);
//#ifdef __cplusplus
//}
//#endif

//extern void srand(_UINT);        // Remove the comment when you use rand()
//extern _SBYTE *_s1ptr;                // Remove the comment when you use strtok()
        
#ifdef __cplusplus                // Use Hardware Setup
extern "C" {
#endif
extern void main(void);
extern void HardwareSetup(void);
extern void tn_startup_hardware_init(void);
extern void tn_startup(void);
#ifdef __cplusplus
}
#endif
    
//#ifdef __cplusplus            // Remove the comment when you use global class object
//extern "C" {                    // Sections C$INIT and C$END will be generated
//#endif
//extern void _CALL_INIT(void);
//extern void _CALL_END(void);
//#ifdef __cplusplus
//}
//#endif
#define PSW_init  0x00000000    // PSW bit pattern
#define FPSW_init 0x00000100    // FPSW bit base pattern

/***********************************************************************************************************************
Exported global variables and functions (to be accessed by other files)
***********************************************************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
void PowerON_Reset_PC(void);
#ifdef __cplusplus
}
#endif

/***********************************************************************************************************************
Private global variables and functions
***********************************************************************************************************************/
#pragma section ResetPRG

#pragma entry PowerON_Reset_PC

/***********************************************************************************************************************
* Function Name: PowerON_Reset_PC
* Description  : Executes the CPU initialization processing to register 
*                the power-on reset vector to the exception vector table.
*                This function is executed first after power-on reset.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void PowerON_Reset_PC(void)
{ 

    set_psw(PSW_init);                // Set Ubit & Ibit for PSW

    set_fpsw(FPSW_init);
    
    set_intb(__sectop("C$VECT"));
    
    set_isp (0x400);
    
    tn_startup_hardware_init();

    _INITSCT();

    tn_startup();
    
    main();
    
    brk();
}

/* END of File */


5)intprg.c
省略
6 その他
http://japan.renesas.com/products/tools/middleware/c_communication/a_tcpip/tcpip_t4/index.jsp
にて公開されている無償版TCP/IP プロトコルスタックが便利です。

本家 http://www.tnkernel.com/ に Version 2.7があがっていた。

2013.07.22