忘備録-備忘録

技術的な備忘録

RX210のリアルタイムクロック機能を使う

2016-02-15 18:01:40 | RX210
RX210のRTC機能を使えるようにプログラムしてみました。時刻と日付はGPSより取得するようにしました。

  1. /*
  2.  * RX Family RTC Module Using Firmware Integrated Technology
  3.  * http://japan.renesas.com/support/downloads/download_results/C1000000-C9999999/mpumcu/rx/an_r01an1817eu_rx_rtc.jsp
  4.  */
  5.                                                                            
  6. #include <stdio.h>
  7. #include <machine.h>
  8. #include "iodefine.h"
  9. #include <string.h>
  10. #include "gps.h"
  11. /*
  12.  * RTCの初期化
  13.  */
  14. void initRTC(void)
  15. {
  16.     /* Set subclock drive capacity */
  17.     RTC.RCR3.BIT.RTCDV = 0x06;    //標準CL用ドライブ能力
  18.     while(RTC.RCR3.BIT.RTCDV != 0x06); //Confirm that it has changed, it's slow.
  19.     /* Enable the subclock for RTC */
  20.     RTC.RCR3.BIT.RTCEN = 1; // enable subclock
  21.     while(RTC.RCR3.BIT.RTCEN != 1);
  22.     /* Disable RTC interrupts */
  23.     RTC.RCR1.BYTE = 0x00u;
  24.     /* Stop RTC counter */
  25.     RTC.RCR2.BIT.START = 0;
  26.     while(RTC.RCR2.BIT.RESET != 0) ;
  27.     /*RCR2レジスタのb7を“0”にする*/
  28.     RTC.RCR2.BYTE &= 0x7F;
  29.     /* RTCは24時間モードで動作 */
  30.     RTC.RCR2.BIT.HR24 = 1;
  31.     /* Clear alarms, capture regs, adjustment regs, and output enable */
  32.     RTC.RCR2.BIT.RESET = 1;
  33.     /* Wait for reset to happen before continuing.*/
  34.     while(RTC.RCR2.BIT.RESET != 0) ;
  35.     /* Insure ICU RTC interrupts disabled */
  36.     IEN(RTC,PRD) = 0;
  37.     IEN(RTC,ALM) = 0;
  38.     IEN(RTC,CUP) = 0;
  39.     /* Enable RTC interrupts (PIE, CIE and AIE), not ICU yet */
  40.     RTC.RCR1.BYTE = 0x07u;
  41.     while (RTC.RCR1.BYTE != 0x07u) ;
  42. }
  43. /*
  44.  * BCDコードへの変換
  45.  */
  46. unsigned char dec2bcd(unsigned char data)
  47. {
  48.     unsigned char ans = 0;
  49.     ans = ((data / 10) & 0x0F) << 4 | ((data % 10) & 0x0F);
  50.     return ans;
  51. }
  52. /*
  53.  * BINコードへの変換
  54.  */
  55. unsigned char bcd2dec(unsigned char data)
  56. {
  57.     unsigned char ans = 0;
  58.     ans = ((data >> 4) & 0x0F) * 10 + (data & 0x0F);
  59.     return ans;
  60. }
  61. /*
  62.  * 曜日の計算
  63.  */
  64. int Zeller(int y,int mo, int d)
  65. {
  66.     int C,Y,G;
  67.     C=y/100;
  68.     Y= y % 100;
  69.     G=5*C+C/4;
  70.     return (d+((26*(mo+1))/10)+Y+Y/4+G + 6) % 7;
  71. }
  72. /*
  73.  * RTCへの時間の設定
  74.  */
  75. void RTCsetTime (int *y,int *mo,int *d,int *w,int *h,int *m,int *s)
  76. {
  77.     /* Stop RTC counter */
  78.     RTC.RCR2.BIT.START = 0;
  79.     while(RTC.RCR2.BIT.START != 0);
  80.     /* Set time */
  81.     /* Set seconds. (0-59) */
  82.     RTC.RSECCNT.BYTE = dec2bcd(*s);
  83.     /* Set minutes (0-59) */
  84.     RTC.RMINCNT.BYTE = dec2bcd(*m);
  85.     /* Set hours. (0-23) */
  86.     RTC.RHRCNT.BYTE = dec2bcd(*h);
  87.     /* Set the date */
  88.     /* Day of the week (0-6, 0=Sunday) */
  89.     RTC.RWKCNT.BYTE = dec2bcd(*w);
  90.     /* Day of the month (1-31) */
  91.     RTC.RDAYCNT.BYTE = dec2bcd(*d);
  92.     /* Month. (1-12, 1=January) */
  93.     RTC.RMONCNT.BYTE = dec2bcd(*mo);
  94.     /* Year. (00-99) */
  95.     RTC.RYRCNT.WORD = (unsigned short)(dec2bcd(*y));
  96.     /* start RTC counter */
  97.     RTC.RCR2.BIT.START = 1;
  98.     while(RTC.RCR2.BIT.START != 1);
  99. }
  100. /*
  101.  * RTCのデータを読み出す
  102.  */
  103. void RTCread(int *y,int *mo,int *d,int *w,int *h,int *m,int *s)
  104. {
  105.     unsigned short bcd_years; // Used for converting year.
  106.     do
  107.     {
  108.         /* Clear carry flag in ICU */
  109.     IR(RTC,CUP) = 0;
  110.         /* Read and convert RTC registers; mask off unknown bits and hour am/pm. */
  111.         /* Seconds. (0-59) */
  112.         *s = bcd2dec((RTC.RSECCNT.BYTE & 0x7fu));
  113.         /* Minutes. (0-59) */
  114.         *m = bcd2dec((RTC.RMINCNT.BYTE & 0x7fu));
  115.         /* Hours. (0-23) */
  116.         *h = bcd2dec((RTC.RHRCNT.BYTE & 0x3fu));
  117.         /* Day of the month (1-31) */
  118.         *d = bcd2dec(RTC.RDAYCNT.BYTE);
  119.         /* Months since January (0-11) */
  120.         *mo = bcd2dec(RTC.RMONCNT.BYTE);
  121.         /* Years since 2000 */
  122.         bcd_years = (unsigned short)RTC.RYRCNT.WORD;
  123.         /* years years since 1900 */
  124.         *y = bcd2dec((bcd_years & 0xFF));
  125.         /* Days since Sunday (0-6) */
  126.         *w = RTC.RWKCNT.BYTE & 0x07u;
  127.     }
  128.     while (IR(RTC,CUP) == 1); //Reread if carry occurs during read
  129. }
  130. void main(void)
  131. {
  132.     int h,m,s,d,mo,y,w,flag=0;
  133.     volatile unsigned int i;
  134.     change_oscillation_PLL();            //クロックソースPLL
  135.     SCI6_Init (9600);                    // SCI6-->GPSユニット
  136.     initRTC();
  137.     setpsw_i();                            // 割込み許可 clrpsw_i()割込み禁止
  138.     printf("\nhello world!\n");
  139.     while(flag==0) {
  140.         if(getJST(&y,&mo,&d,&h,&m,&s)) {
  141.             printf("\n\n\n\ny=%d m=%d d=%d h=%d m=%d s=%d\n",y,mo,d,h,m,s);
  142.             w = Zeller(y,mo,d);
  143.             RTCsetTime (&y,&mo,&d,&w,&h,&m,&s);
  144.             flag =1;
  145.         }
  146.     }
  147.     while(1) {
  148.         RTCread(&y,&mo,&d,&w,&h,&m,&s);
  149.         printf("y=%d m=%d d=%d h=%d m=%d s=%d w=%d\n",y,mo,d,h,m,s,w);
  150.         for(i=0;i<9000000;i++)nop();
  151.     }
  152.             //printf("> %s\n",buf);
  153. }

e2 studioのデバック仮想コンソールを使う

2016-02-01 18:00:16 | RX210
RXシリーズにはE1デバッガ経由でデータを交換できる機能があるようです。この機能を使ってprintf(),scanf()などの標準入出力を動作させることができます。
e2 studio上で[Renesas デバッグ仮想コンソール]をクリックするとコンソールが開きターゲットと通信できます。


ターゲット側にも仕掛けが必要です。次のファイルをルネサスエレクトロニクス社のサンプルプログラムから手に入れます。ファイルが入っているのは「RX ファミリ ボードサポートパッケージモジュール Firmware Integration Technology」です。
  • lowlvl.c
  • lowsrc.c

各ファイは少し書き直さないとビルドできません。
lowlvl.c抜粋
/***********************************************************************************************************************
Includes   <System Includes> , "Project Includes"
***********************************************************************************************************************/
/* r_bsp access. */
//#include "platform.h" コメントアウト

typedef unsigned int uint32_t; //追加

/***********************************************************************************************************************
Macro definitions
***********************************************************************************************************************/
#define E1_DBG_PORT (*(volatile struct st_dbg     __evenaccess *)0x84080)
#define TXFL0EN     0x00000100          // debug tx flow control bit
#define RXFL0EN     0x00001000          // debug RX flow control bit

lowsrc.c抜粋
/***********************************************************************************************************************
Includes   <System Includes> , "Project Includes"
***********************************************************************************************************************/
//#include "r_bsp_config.h"   コメントアウト
#define BSP_CFG_IO_LIB_ENABLE 1 //追加
/* Do not include this file if stdio is disabled in r_bsp_config. */
#if (BSP_CFG_IO_LIB_ENABLE == 1)

reset_program.c抜粋
       略	(下の行のコメントを外す)
#ifdef __cplusplus				// Use SIM I/O
extern "C" {
#endif
extern void _INIT_IOLIB(void);
//extern void _CLOSEALL(void);
#ifdef __cplusplus
}
#endif
       略
	_INITSCT();

	_INIT_IOLIB();			// Use SIM I/O コメントを外す

//	errno=0;			// Remove the comment when you use errno
//	srand((_UINT)1);		// Remove the comment when you use rand()
       略