忘備録-備忘録

技術的な備忘録

広告

※このエリアは、60日間投稿が無い場合に表示されます。記事を投稿すると、表示されなくなります。

RS232C通信プログラム2

2015-03-27 08:58:53 | RX62N
RX62N用のRS232C通信プログラムです。割込みとバッファリングを使用しています。環境はkpitです。
/***********************************************************************/
/*                                                                     */
/*  FILE        :rs232ctest02.c                                        */
/*  DATE        :Thu, Dec 06, 2012                                     */
/*  DESCRIPTION :Main Program                                          */
/*  CPU TYPE    :RX62N                                                 */
/*                                                                     */
/*  This file is generated by KPIT GNU Project Generator.              */
/*                                                                     */
/***********************************************************************/
																						
#include <stdio.h>																				
#include "iodefine.h"
#include "interrupt_handlers.h"

#define PCLK 48			//PクロックMHz
#define TX_ACTIVE   1	//送信動作中
#define TX_INACTIVE 0	//送信停止中

#define TX_RING_BUFF_SIZE	32	//リングバッファのサイズ
#define RX_RING_BUFF_SIZE	32	//リングバッファのサイズ

//リングバッファ
unsigned char rx_buff[RX_RING_BUFF_SIZE];	//受信用
unsigned char tx_buff[TX_RING_BUFF_SIZE];	//送信用

//データポインタ
volatile int ptr_rx_top,ptr_rx_bottom;
volatile int ptr_tx_top,ptr_tx_bottom;
//送信フラグ データ送信中かどうか
volatile int tx_flag = TX_INACTIVE;


//受信エラー処理
void  INT_Excep_SCI0_ERI0(void)
{ 
	SCI0.SSR.BYTE &= ~0x38;	//エラーフラグクリア
}

//受信完了割込み
void  INT_Excep_SCI0_RXI0(void)
{ 
    
	/* Read data */
	rx_buff[ptr_rx_top] = SCI0.RDR;
	ptr_rx_top++;
	ptr_rx_top = ptr_rx_top % RX_RING_BUFF_SIZE;
}

//送信完了割込み
void  INT_Excep_SCI0_TXI0(void)
{

	/* Write the character out */
	SCI0.TDR = tx_buff[ptr_tx_bottom];
	ptr_tx_bottom++;
	ptr_tx_bottom = ptr_tx_bottom % TX_RING_BUFF_SIZE;
	//すべてのデータを送信したら割込み禁止
	if( ptr_tx_bottom == ptr_tx_top ) {
		IEN(SCI0, TXI0) = 0;
		tx_flag = TX_INACTIVE;
	}    
}

/* SCI初期化 引数はボーレート */
void InitSCI(int bps)
{    
	/* Enable SCI0 */
	MSTP(SCI0) = 0;
   
	/* RxD0 is input */
	PORT2.DDR.BIT.B1 = 0;
	/* Enable Input Buffer on RxD0 */
	PORT2.ICR.BIT.B1 = 1;
	/* TxD0 is output */
 	PORT2.DDR.BIT.B0 = 1;

	/* Disable Tx/Rx */
	SCI0.SCR.BYTE = 0;
    
	/*  Set mode register
		-Asynchronous Mode
		-8 bits
		-no parity
		-1 stop bit
		-PCLK clock (n = 0) */
	SCI0.SMR.BYTE = 0x00;
	//SCI0.SEMR.BIT.ABCS = 1;
	/* 送受信で割込み発生 */
	SCI0.SCR.BIT.RIE = 1;
	SCI0.SCR.BIT.TIE = 1;
    /* 割込み優先度 */
	IPR(SCI0,RXI0) = 0x04;
	IPR(SCI0,TXI0) = 0x04;
	/* Clear IR bits for TIE and RIE */
	IR(SCI0, RXI0) = 0;
	IR(SCI0, TXI0) = 0;
   
	/* 受信・送信割込み許可 */
	IEN(SCI0, RXI0) = 1;
	IEN(SCI0, TXI0) = 1;
   
	/*  Set baud rate to 115200
		N = (PCLK Frequency) / (64 * 2^(2*n - 1) * Bit Rate) - 1
		N = (48,000,000) / (64 * 2^(2*0 - 1) * 115200) - 1
		115200bps N = 12
		 38400bps N=38 
	*/
	//SCI0.BRR = 38;
	SCI0.BRR = PCLK *1000000 / 32 / bps - 1;

	/* Enable Tx/Rx */
	SCI0.SCR.BYTE |= 0x30;
	
	/*リングバッファ初期化*/
	ptr_rx_top = ptr_rx_bottom = 0;
	ptr_tx_top = ptr_tx_bottom = 0;
}

/* データの送信 */
void charput(unsigned char output_char)
{
	int tmp;
	tmp = ptr_tx_top + 1;
	tmp = tmp % TX_RING_BUFF_SIZE;
	while(tmp == ptr_tx_bottom) ;	//バッファに空きができるまで待つ
	
	tx_buff[ptr_tx_top] = output_char;
	ptr_tx_top++;
	ptr_tx_top = ptr_tx_top % TX_RING_BUFF_SIZE;
	if(tx_flag == TX_INACTIVE) {
		tx_flag = TX_ACTIVE;
		IEN(SCI0, TXI0) = 1;
		SCI0.SCR.BYTE |= 0xA0; //送信割込み発生
	}
}

/* データの受信 バッファに受信したデータがなければ受信するまで待つ */
unsigned char charget(void)
{
	unsigned char c;
	
	while(ptr_rx_bottom == ptr_rx_top); //データを受信するまで待つ
	c = rx_buff[ptr_rx_bottom];
	ptr_rx_bottom++;
	ptr_rx_bottom = ptr_rx_bottom % RX_RING_BUFF_SIZE;
	return c;
}

/* 文字列の出力 */
void printString(char *s)
{
	while( *s != 0 ) {
		if(( *s == '\r' ) && ( *(s+1) != '\n')) {
			charput('\r');charput('\n');
		} else if( (*s == '\n') && ( *(s-1) != '\r') ) {
			charput('\r');charput('\n');
		} else {
			charput(*s);
		}
		s++;
	}
}

/* printf関数で使用 */
int _write(int file,char *ptr,int len)
{
	int i;
	
	for(i=0;i<len;i++) {
		if( (ptr[i]=='r') || (ptr[i]=='n')) {
			charput('r');charput('n');
		} else {
			charput(ptr[i]);
		}
	}

    return len;
}

/* scanf関数で使用 */
int _read (int file, char *ptr, int len)
{
	*ptr = charget();
     return 1;
}


int main(void)
{
	char c;
    // TODO: add application code here

	SYSTEM.SCKCR.BIT.ICK = 0x00;		//システムクロック x16 96MHz
	SYSTEM.SCKCR.BIT.PCK = 0x01;		//周辺クロック   x4  48MHz
	SYSTEM.SCKCR.BIT.BCK = 0x01;		//外部バスクロック x4  48MHz
	
	InitSCI(38400);	//38400bpsで初期化
	
	printf("Startn");
    while (1) {
		c = charget();
		printf("%c",c);
    }
  return 0;
}

コメント (2)

RS232C通信プログラム1

2015-03-27 08:53:50 | RX62N
RX62N用のRS232C通信プログラムです。割り込みを利用しません。
/***********************************************************************/
/*                                                                     */
/*  FILE        :RS232Ctest.c                                          */
/*  DATE        :Thu, Jul 12, 2012                                     */
/*  DESCRIPTION :Main Program                                          */
/*  CPU TYPE    :RX62N                                                 */
/*                                                                     */
/*  This file is generated by KPIT GNU Project Generator.              */
/*                                                                     */
/***********************************************************************/
                    
																							
																							
#include "iodefine.h"

/******************************************************************************
* Function Name: InitSCI
* Description  : Initializes SCI channel for asychronous operation
* Arguments    : none
* Return Value : none
******************************************************************************/
void InitSCI(void)
{    
	/* Enable SCI0 */
	MSTP(SCI0) = 0;
   
	/* RxD0 is input */
	PORT2.DDR.BIT.B1 = 0;
	/* Enable Input Buffer on RxD1 */
	PORT2.ICR.BIT.B1 = 1;
	/* TxD0 is output */
 	PORT2.DDR.BIT.B0 = 1;

	/* Disable Tx/Rx */
	SCI0.SCR.BYTE = 0;
    
	/*  Set mode register
		-Asynchronous Mode
		-8 bits
		-no parity
		-1 stop bit
		-PCLK clock (n = 0) */
	SCI0.SMR.BYTE = 0x00;
	//SCI0.SEMR.BIT.ABCS = 1;
	/* Enable RXI and TXI interrupts, even though we are not
		using the interrupts, we will be checking the IR bits
		as flags */
	//SCI0.SCR.BIT.RIE = 1;
	//SCI0.SCR.BIT.TIE = 1;
    
	/* Clear IR bits for TIE and RIE */
	//IR(SCI0, RXI0) = 0;
	//IR(SCI0, TXI0) = 0;
   
	/* Disable RXI and TXI interrupts in ICU because we are polling */
	//IEN(SCI0, RXI0) = 0;
	//IEN(SCI0, TXI0) = 0;
   
	/*  Set baud rate to 115200
		N = (PCLK Frequency) / (64 * 2^(2*n - 1) * Bit Rate) - 1
		N = (48,000,000) / (64 * 2^(2*0 - 1) * 115200) - 1
		N = 12
		38400bps N=38 */
	SCI0.BRR = 38;

	/* Enable Tx/Rx */
	SCI0.SCR.BYTE |= 0x30;
}

/******************************************************************************
* Function Name: charput
* Description  : Outputs a character on a serial port
* Arguments    : character to output
* Return Value : none
******************************************************************************/
void charput(unsigned char output_char)
{
	
	/* Wait for transmit buffer to be empty */
	//while(IR(SCI1, TXI1) == 0);
	while(SCI0.SSR.BIT.TDRE == 0);

	/* Clear TXI IR bit */
	//IR(SCI0, TXI0) = 0;

	/* Write the character out */
	SCI0.TDR = output_char;    
}

/******************************************************************************
* Function Name: charget
* Description  : Gets a character on a serial port
* Arguments    : character to output
* Return Value : none
******************************************************************************/
unsigned char charget(void)
{
	unsigned char temp;
    
	/* エラーチェック */
	if (SCI0.SSR.BYTE & 0x38) {
		SCI0.SSR.BYTE &= ~0x38;	//エラーフラグクリア
		temp = SCI0.RDR;
		return 0;
	}
	/* Read any 'junk' out of receive buffer */
	//temp = SCI0.RDR;
    
	/* Clear flag to receive next byte */
	//IR(SCI0, RXI0) = 0;
    
	/* Wait for next receive data */
	//while(IR(SCI1, RXI1) == 0);
	while(SCI0.SSR.BIT.RDRF == 0);
    
	/* Read data */
	temp = SCI0.RDR;
    
/*	// Echo data back out 
	if(temp == 0x0d)
	{
		// Enter was pressed, output newline 
		charput('\r');
		charput('\n');
	}
	else
	{
		// Echo back character 
		charput(temp);
	}
*/  
	// Receive data acquired, send back up 
	return temp;         
}

/* 文字列の出力 */
void printString(char *s)
{
	while( *s != 0 ) {
		charput(*s);
		s++;
	}
}

int main(void)
{
    // TODO: add application code here

	SYSTEM.SCKCR.BIT.ICK = 0x00;		//システムクロック x16 96MHz
	SYSTEM.SCKCR.BIT.PCK = 0x01;		//周辺クロック   x4  48MHz
	SYSTEM.SCKCR.BIT.BCK = 0x01;		//外部バスクロック x4  48MHz
	InitSCI();
	
	printString("Start\r\n");
    while (1) {
		charput(charget());
    }
  return 0;
}
コメント

i2c通信関数

2015-03-26 07:37:56 | RX62N
RX62N用のi2c通信プログラムです。割り込みを利用しません。

  1. /*
  2.    Interface 2011年6月号を参考にして作成したIIC通信プログラム
  3.      割込みを使用しない
  4. */
  5. #include "iodefine.h"
  6. /*
  7.    RIICの初期化
  8.    引数  無し
  9.    戻り値 無し
  10. */
  11. void initRIIC0(void)
  12. {
  13.     MSTP(RIIC0) = 0;            // モジュールスタンバイ解除
  14.     RIIC0.ICCR1.BIT.ICE = 0;
  15.     /* set port direction to input */
  16.     PORT1.DDR.BIT.B3 = 0;            // SDA0
  17.     PORT1.DDR.BIT.B2 = 0;            // SCL0
  18.     /* enable input buffer */
  19.     PORT1.ICR.BIT.B3 = 1;            // SDA0
  20.     PORT1.ICR.BIT.B2 = 1;            // SCL0
  21.     /* オープンドレイン設定 いらない?*/
  22.     PORT1.ODR.BIT.B3 = 1;
  23.     PORT1.ODR.BIT.B2 = 1;
  24.     /* RIIC disable for RIIC initial */
  25.     RIIC0.ICCR1.BIT.ICE = 0;
  26.     RIIC0.ICCR1.BIT.IICRST = 1;        // リセットパルス発生
  27.     RIIC0.ICCR1.BIT.IICRST = 0;
  28.     
  29.     /* Set baud rate */
  30.     RIIC0.ICMR1.BIT.CKS = 4;        // IIC phi = PCLK/8 clock
  31.     RIIC0.ICBRH.BIT.BRH = 25;        // 100kbps (PCLK = 48MHz)
  32.     RIIC0.ICBRL.BIT.BRL = 25;
  33.     /* disable all address detection. Master mode only. */
  34.     RIIC0.ICSER.BYTE = 0x00;
  35.     /* ACKWP is protect bit for ACKBT */
  36.     RIIC0.ICMR3.BIT.ACKWP = 1;        // disable protect for ACKBT
  37.     RIIC0.ICCR1.BIT.ICE = 1;        // enable RIIC
  38. }
  39. /*
  40.    バスフリーの確認
  41.    引数  無し
  42.    戻り値 無し
  43. */
  44. inline void i2cBusFree(void)
  45. {
  46.     while(RIIC0.ICCR2.BIT.BBSY == 1);
  47. }
  48. /*
  49.    バス・スタートを実行
  50.    引数  無し
  51.    戻り値 無し
  52. */
  53. inline void i2cStart(void)
  54. {
  55.     RIIC0.ICCR2.BIT.ST = 1;
  56.     while(RIIC0.ICSR2.BIT.START != 1);
  57.     RIIC0.ICSR2.BIT.START = 0;
  58. }
  59. /*
  60.    バスがBusy中にバス・スタート
  61.    引数  無し
  62.    戻り値 無し
  63. */
  64. inline void i2cReStart(void)
  65. {
  66.     RIIC0.ICCR2.BIT.RS = 1;
  67.     while(RIIC0.ICSR2.BIT.START != 1);
  68.     RIIC0.ICSR2.BIT.START = 0;
  69. }
  70. /*
  71.    バス・ストップを実行
  72.    引数  無し
  73.    戻り値 無し
  74. */
  75. inline void i2cStop(void)
  76. {
  77.     RIIC0.ICSR2.BIT.STOP = 0;
  78.     RIIC0.ICCR2.BIT.SP = 1;
  79.     while(RIIC0.ICSR2.BIT.STOP != 1);
  80.     RIIC0.ICSR2.BIT.STOP = 0;
  81.     RIIC0.ICSR2.BIT.NACKF = 0;
  82. }
  83. /*
  84.    デバイスに1byteデータ転送
  85.    引数  送信データ
  86.    戻り値 無し
  87. */
  88. inline void i2cSendByte(unsigned char data)
  89. {
  90.     while(RIIC0.ICSR2.BIT.TDRE != 1);
  91.     RIIC0.ICDRT = data;
  92. }
  93. /*
  94.    データの転送完了を確認
  95.    引数  無し
  96.    戻り値 無し
  97. */
  98. inline void i2cCheckTEND(void)
  99. {
  100.     while(RIIC0.ICSR2.BIT.TEND != 1);
  101. }
  102. /*
  103.    デバイスから1byte読み取りバスストップを実行
  104.    引数   無し
  105.    戻り値  デバイスから読み取ったデータ
  106. */
  107. unsigned char i2cReceiveByteStop(void)
  108. {
  109.     unsigned char rdt;
  110.     while(RIIC0.ICSR2.BIT.RDRF != 1);
  111.     RIIC0.ICMR3.BIT.WAIT = 1;
  112.     RIIC0.ICMR3.BIT.ACKBT = 1;
  113.     rdt = RIIC0.ICDRR;                //dummy read
  114.     while(RIIC0.ICSR2.BIT.RDRF != 1);
  115.     RIIC0.ICSR2.BIT.STOP = 0;
  116.     RIIC0.ICCR2.BIT.SP = 1;            //stop
  117.     rdt = RIIC0.ICDRR;                //data read
  118.     RIIC0.ICMR3.BIT.WAIT = 0;
  119.     while(RIIC0.ICSR2.BIT.STOP != 1);
  120.     RIIC0.ICSR2.BIT.STOP = 0;
  121.     RIIC0.ICSR2.BIT.NACKF = 0;
  122.     return rdt;
  123. }
  124. /*
  125.    スレーブ・アドレスを指定してデバイスのレジスタ・メモリにデータを書き込む
  126.    引数   sadr : スレーブ・アドレス
  127.         madr : レジスタ・メモリアドレス
  128.         data : 書き込むデータ
  129.    戻り値  無し
  130. */
  131. void i2cDevWrite(unsigned char sadr, unsigned char madr, unsigned char data)
  132. {
  133.     i2cBusFree();
  134.     i2cStart();
  135.     i2cSendByte(sadr);
  136.     i2cSendByte(madr);
  137.     i2cSendByte(data);
  138.     i2cCheckTEND();
  139.     i2cStop();
  140. }
  141. /*
  142.   スレーブアドレスとデバイスのレジスタ・アドレスを指定してデバイスからデータを読み込む
  143.   引数   sadr : スレーブ・アドレス
  144.        madr : レジスタ・メモリアドレス
  145.   戻り値  読み込んだデバイスのデータ
  146. */
  147. unsigned char i2cDevRead(unsigned char sadr, unsigned char madr)
  148. {
  149.     unsigned char rdt;
  150.     i2cBusFree();
  151.     i2cStart();
  152.     i2cSendByte(sadr);
  153.     i2cSendByte(madr);
  154.     i2cCheckTEND();
  155.     i2cReStart();
  156.     i2cSendByte(sadr | 0x01);
  157.     rdt = i2cReceiveByteStop();
  158.     return rdt;
  159. }
  160. /*
  161.    スレーブアドレスを指定して複数byteのデータを送信する
  162.    引数  sadr : スレーブアドレス
  163.        databuff: 送信するデータの入った配列
  164.        datanum : 送信するデータ数
  165.    戻り値 0 : 送信完了  -1 : 送信エラー
  166. */
  167. int i2cSendData(unsigned char sadr, const unsigned char databuff[], int datanum)
  168. {
  169.     int senddata=-1,ret=-1;
  170.     
  171.     i2cBusFree();
  172.     i2cStart();
  173.     while(senddata<datanum) {
  174.         if(RIIC0.ICSR2.BIT.NACKF != 0) goto sendstop;    //送信エラー
  175.         if(RIIC0.ICSR2.BIT.TDRE != 1) continue;
  176.         if(senddata < 0) {
  177.             RIIC0.ICDRT = sadr;            //最初にアドレスを送信
  178.         } else {
  179.             RIIC0.ICDRT = databuff[senddata];
  180.         }
  181.         senddata++;
  182.     }
  183.     i2cCheckTEND();
  184.     ret = 0;        //送信完了
  185. sendstop:
  186.     i2cStop();
  187.     return ret;
  188. }
  189. /*
  190.    スレーブアドレスを指定して複数byteのデータを受信する
  191.    引数  sadr : スレーブアドレス
  192.        databuff: 受信したデータを入れる配列
  193.        datanum : 受信するデータ数
  194.    戻り値 0 : 受信完了  -1 : 受信エラー
  195. */
  196. int i2cReceiveData(unsigned char sadr, unsigned char databuff[], int datanum)
  197. {
  198.     int recvdata=0,ret=-1;
  199.     unsigned char dummy;
  200.     
  201.     i2cBusFree();
  202.     i2cStart();
  203.     i2cSendByte(sadr | 0x01);        // アドレス送信 受信設定(|0x01)
  204.     i2cCheckTEND();
  205.     if(datanum == 1 ) {
  206.         databuff[0] = i2cReceiveByteStop();
  207.         return 0;
  208.     }
  209.     if(RIIC0.ICSR2.BIT.NACKF == 0) {
  210.     /* データ受信処理 */
  211.         dummy = RIIC0.ICDRR;                //dummy read
  212.         while(recvdata < datanum) {
  213.             if(RIIC0.ICSR2.BIT.RDRF != 1) continue;
  214.             if(recvdata == (datanum-2) ) break;
  215.             if(recvdata == (datanum-3) ) {
  216.                 RIIC0.ICMR3.BIT.WAIT = 1;
  217.             }
  218.             databuff[recvdata++] = RIIC0.ICDRR;
  219.         }
  220.         RIIC0.ICMR3.BIT.ACKBT = 1;
  221.         databuff[recvdata++] = RIIC0.ICDRR;
  222.         while(RIIC0.ICSR2.BIT.RDRF != 1);
  223.         RIIC0.ICSR2.BIT.STOP = 0;
  224.         RIIC0.ICCR2.BIT.SP = 1;
  225.         databuff[recvdata++] = RIIC0.ICDRR;
  226.         RIIC0.ICMR3.BIT.WAIT = 0;
  227.         ret = 0;
  228.     } else {
  229.     /* 受信エラー */
  230.         RIIC0.ICSR2.BIT.STOP = 0;
  231.         RIIC0.ICCR2.BIT.SP = 1;
  232.         dummy = RIIC0.ICDRR;        //dummy read
  233.     }    
  234.     while(RIIC0.ICSR2.BIT.STOP != 1);
  235.     RIIC0.ICSR2.BIT.STOP = 0;
  236.     RIIC0.ICSR2.BIT.NACKF = 0;
  237.     
  238.     return ret;
  239. }
コメント

RX210のクロックをPLLに切り替えるプログラム

2015-03-26 07:17:08 | RX210
RX210のクロックをPLLに切り替えるプログラムです。ルネサスエレクトロニクスのサンプルコードを単純化したものです。水晶振動子の発振周波数が20MHzの時にCPUの動作クロックが50MHzとなり周辺機器のクロックが25MHzとなります。

  1. /* RX210 cpuのクロックをPLLに切り替えるプログラム */
  2. /* 水晶振動子の発振周波数が20MHzの時にCPUの動作クロックが50MHzとなり周辺機器のクロックが25MHzとなる。 */
  3.                                                                           
  4. #include "iodefine.h"
  5. #include <machine.h>
  6. void change_oscillation_PLL(void)
  7. {
  8.     unsigned int i;
  9.     /* ---- Enable write protection ---- */
  10.     /* PRCR - Protect Register
  11.     b15:b8 PRKEY - PRC Key Code - A5h
  12.                   (The write value should be A5h to permission writing PRCi bit)
  13.     b7:b4 Reserved - The write value should be 0.
  14.     b3 PRC3 - Protect Bit 3 - Write disabled
  15.     b2 PRC2 - Protect Bit 2 - Write enabled
  16.     b1 PRC1 - Protect Bit 1 - Write enabled
  17.     b0 PRC0 - Protect Bit 0 - Write enabled */
  18.     SYSTEM.PRCR.WORD = 0xA507;
  19.     /* ---- Set the VRCR register ---- */
  20.     SYSTEM.VRCR = 0x00;
  21.     /* ---- Set the main clock oscillator drive capability ---- */
  22.     /* MOFCR - Main Clock Oscillator Forced Oscillation Control Register
  23.     b7 Reserved - The write value should be 0.
  24.     b6 MOSEL - Main Clock Oscillator Switch - Resonator
  25.     b5:b4 MODRV2 - Main Clock Oscillator Drive Capability Switch 2
  26.                       - 16 MHz to 20 MHz
  27.     b3:b1 MODRV - Main Clock Oscillator Drive Capability Switch
  28.                       - 16 MHz to 20 MHz non-lead type ceramic resonator
  29.     b0 Reserved - The write value should be 0. */
  30.     SYSTEM.MOFCR.BYTE = (0x30);    /* Drive capability : 20 MHz crystal resonator */
  31.     /* ---- Set wait time until the main clock oscillator stabilizes ---- */
  32.     /* MOSCWTCR - Main Clock Oscillator Wait Control Register
  33.     b7:b5 Reserved - The write value should be 0.
  34.     b4:b0 MSTS - Main Clock Oscillator Waiting Time
  35.                       - Wait time is 131072 cycles (approx. 6.55 ms). */
  36.     SYSTEM.MOSCWTCR.BYTE = (0x0D);    /* Wait control register : 131072 cycles (approx. 6.55 ms) */
  37.     /* ---- Operate the main clock oscillator ---- */
  38.     /* MOSCCR - Main Clock Oscillator Control Register
  39.     b7:b1 Reserved - The write value should be 0.
  40.     b0 MOSTP - Main Clock Oscillator Stop - Main clock oscillator is operating. */
  41.     SYSTEM.MOSCCR.BYTE = 0x00;
  42.     while (0x00 != SYSTEM.MOSCCR.BYTE)
  43.     {
  44.         /* Confirm that the written value can be read correctly. */
  45.     }
  46.     /* ---- Wait processing for the clock oscillation stabilization ---- */
  47.     for(i=0;i<100;i++) nop();</li>
  48.     /* ---- Set the PLL division ratio and multiplication factor ---- */
  49.     /* PLLCR - PLL Control Register
  50.     b15:b13 Reserved - The write value should be 0.
  51.     b12:b8 STC - Frequency Multiplication Factor Select
  52.                       - Frequency multiplication factor is multiply-by-10.
  53.     b7:b2 Reserved - The write value should be 0.
  54.     b1:b0 PLIDIV - PLL Input Frequency Division Ratio Select
  55.                       - PLL input division ratio is divide-by-2. */
  56.     SYSTEM.PLLCR.WORD = (0x0901);    /* Division ratio and multiplication factor : divide-by-2, multiply-by-10 */
  57.     /* ---- Set wait time until the PLL clock oscillator stabilizes ---- */
  58.     /* PLLWTCR - PLL Wait Control Register
  59.     b7:b5 Reserved - The write value should be 0.
  60.     b4:b0 PSTS - PLL Waiting Time
  61.                       - Wait time is 65536 cycles (approx. 655.36 us). */
  62.     SYSTEM.PLLWTCR.BYTE = (0x09);    /* Wait control register : 65536 cycles (approx. 655.36 us) */
  63.     /* ---- Operate the PLL clock oscillator ---- */
  64.     /* PLLCR2 - PLL Control Register 2
  65.     b7:b1 Reserved - The write value should be 0.
  66.     b0 PLLEN - PLL Stop Control - PLL is operating. */
  67.     SYSTEM.PLLCR2.BYTE = 0x00;
  68.     /* ---- Wait processing for the clock oscillation stabilization ---- */
  69.     for(i=0;i<100;i++) nop();</li>
  70.     /* ---- Set the operating power control mode ---- */
  71.     /* OPCCR - Operating Power Control Register
  72.     b7:b5 Reserved - The write value should be 0.
  73.     b4 OPCMTSF - Operating Power Control Mode Transition Status Flag
  74.     b3 Reserved - The write value should be 0.
  75.     b2:b0 OPCM - Operating Power Control Mode Select - High-speed operating mode */
  76.     SYSTEM.OPCCR.BYTE = (0x00); /* High-speed operating mode */
  77.     while (0 != SYSTEM.OPCCR.BIT.OPCMTSF)
  78.     {
  79.         /* Confirm that the operation power control mode transition completed. */
  80.     }
  81.     /* ---- Set the internal clock division ratio ---- */
  82.     /* SCKCR - System Clock Control Register
  83.     b31:b28 FCK - FlashIF Clock(FCLK) Select - divide-by-4
  84.     b27:b24 ICK - System Clock (ICLK) Select - divide-by-2
  85.     b23 PSTOP1 - BCLK Pin Output Control - disabled. (Fixed high)
  86.     b22:b20 Reserved - The write value should be 0.
  87.     b19:b16 BCK - External Bus Clock (BCLK) Select - divide-by-4
  88.     b15:b12 Reserved - The write value should be 0001b.
  89.     b10:b8 PCLKB - Peripheral Module Clock B(PCLKB) Select - divide-by-4
  90.     b7:b4 Reserved - The write value should be 0001b.
  91.     b3:b0 PCLKD - Peripheral Module Clock D(PCLKD) Select - divide-by-2 */
  92.     SYSTEM.SCKCR.LONG = 0x21821211;    /* ICLK,PCLKD: divide-by-2 PCLKB,BCLK,FCLK: divide-by-4 */
  93.     while (0x21821211 != SYSTEM.SCKCR.LONG)
  94.     {
  95.          /* Confirm that the written value can be read correctly. */
  96.     }
  97.     /* ---- Set the BCLK pin output ---- */
  98.     /* BCKCR - External Bus Clock Control Register
  99.     b7:b1 Reserved - The write value should be 0.
  100.     b0 BCLKDIV - BCLK Pin Output Select - divide-by-2 */
  101.     SYSTEM.BCKCR.BYTE = 0x01;
  102.     while (0x01 != SYSTEM.BCKCR.BYTE)
  103.     {
  104.         /* Confirm that the written value can be read correctly. */
  105.     }
  106.     /* ---- Set the internal clock source ---- */
  107.     /* SCKCR3 - System Clock Control Register 3
  108.     b15:b11 Reserved - The write value should be 0.
  109.     b10:b8 CKSEL - Clock Source Select - PLL circuit is selected.
  110.     b7:b1 Reserved - The write value should be 0. */
  111.     SYSTEM.SCKCR3.WORD = (0x0400);    /* PLL */
  112.     while ((0x0400) != SYSTEM.SCKCR3.WORD)
  113.     {
  114.         /* Confirm that the written value can be read correctly. */
  115.     }
  116.     /* ---- Disable write protection ---- */
  117.     /* PRCR - Protect Register
  118.     b15:b8 PRKEY - PRC Key Code - A5h
  119.                   (The write value should be A5h to permission writing PRCi bit)
  120.     b2 PRC2 - Protect Bit 2 - Write disabled
  121.     b1 PRC1 - Protect Bit 1 - Write disabled
  122.     b0 PRC0 - Protect Bit 0 - Write disabled */
  123.     SYSTEM.PRCR.WORD = 0xA500;
  124. }
  125. void main(void)
  126. {
  127.     unsigned int i;
  128.     change_oscillation_PLL();            //クロックソースPLL
  129.     PORTC.PDR.BYTE = 0xFF;
  130.     PORTC.PODR.BYTE = 0x00;
  131.     PORTA.PDR.BYTE = 0xFF;
  132.     PORTA.PODR.BYTE =0x00;
  133.     PORTB.PDR.BYTE = 0xFF;
  134.     PORTB.PODR.BYTE = 0xFF;
  135.     while(1) {
  136.         PORTC.PODR.BYTE = ~PORTC.PODR.BYTE;
  137.         PORTA.PODR.BYTE = ~PORTA.PODR.BYTE;
  138.         PORTB.PODR.BYTE = ~PORTB.PODR.BYTE;
  139.         for(i=0;i<500000;i++) nop();</li>
  140.     }
  141. }
コメント

RX210のクロックを内蔵高速クロックに切り替えるプログラム

2015-03-26 07:00:27 | RX210
RX210のクロックを内蔵高速クロックに切り替えるプログラムです。ルネサスエレクトロニクスのサンプルコードを単純化したものです。 change_oscillation_HOCO()関数の引数でクロック周波数を変更できます。

  1. /* RX210 cpuのクロックを内蔵高速クロックに切り替えるプログラム */
  2.                                                                            
  3.                                                                           
  4. #include "iodefine.h"
  5. #include <machine.h>
  6. #define FREQ_32MHz (0x00) /* 32 MHz */
  7. #define FREQ_36MHz (0x01) /* 36.864 MHz */
  8. #define FREQ_40MHz (0x02) /* 40 MHz */
  9. #define FREQ_50MHz (0x03) /* 50 MHz */
  10. void change_oscillation_HOCO(unsigned char cpufreq) // <--引数にOSCの周波数を設定
  11. {
  12.     unsigned char reg_hocwtcr2;
  13.     unsigned int i;
  14.     cpufreq &= 0x03;
  15.     if(cpufreq == FREQ_50MHz) {
  16.         reg_hocwtcr2 = 0x03;        /* 50 MHz : 9216 cycles */
  17.     } else {
  18.         reg_hocwtcr2 = 0x02;        /* 32 MHz, 36.864 MHz, 40 MHz : 7168 cycles */
  19.     }
  20.     /* ---- Enable write protection ---- */
  21.     /* PRCR - Protect Register
  22.     b15:b8 PRKEY - PRC Key Code - A5h
  23.                   (The write value should be A5h to permission writing PRCi bit)
  24.     b7:b4 Reserved - The write value should be 0.
  25.     b3 PRC3 - Protect Bit 3 - Write disabled
  26.     b2 PRC2 - Protect Bit 2 - Write enabled
  27.     b1 PRC1 - Protect Bit 1 - Write enabled
  28.     b0 PRC0 - Protect Bit 0 - Write enabled */
  29.     SYSTEM.PRCR.WORD = 0xA507;
  30.     /* ---- Set the VRCR register ---- */
  31.     SYSTEM.VRCR = 0x00;
  32.     /* ---- Set the HOCO frequency ---- */
  33.     /* HOCOCR2 - High-Speed On-Chip Oscillator Control Register 2
  34.     b7:b2 Reserved - The write value should be 0.
  35.     b1:b0 HCFRQ - HOCO Frequency Setting - 50 MHz */
  36.     SYSTEM.HOCOCR2.BYTE = cpufreq;
  37.     /* ---- Set wait time until the HOCO oscillator stabilizes ---- */
  38.     /* HOCOWTCR2 - HOCO Wait Control Register 2
  39.     b7:b4 Reserved - The write value should be 0.
  40.     b3:b0 HSTS2 - HOCO Wait Time Select 2
  41.                       - Wait time is 9216 cycles (approx. 184.32 us). */
  42.     SYSTEM.HOCOWTCR2.BYTE = reg_hocwtcr2;
  43.     //SYSTEM.SCKCR.BIT.BCK = 1;            //BCK 2分周
  44.     /* ---- Operate the HOCO clock ---- */
  45.     /* HOCOCR - High-Speed On-Chip Oscillator Control Register
  46.     b7:b1 Reserved - The write value should be 0.
  47.     b0 HCSTP - the HOCO is operating. */
  48.     SYSTEM.HOCOCR.BYTE = 0x00;
  49.     /* ---- Wait processing for the clock oscillation stabilization ---- */
  50.     for(i=0;i<100;i++) nop();
  51.     /* ---- Set the internal clock division ratio ---- */
  52.     /* SCKCR - System Clock Control Register
  53.     b31:b28 FCK - FlashIF Clock(FCLK) Select - divide-by-4
  54.     b27:b24 ICK - System Clock (ICLK) Select - divide-by-2
  55.     b23 PSTOP1 - BCLK Pin Output Control - disabled. (Fixed high)
  56.     b22:b20 Reserved - The write value should be 0.
  57.     b19:b16 BCK - External Bus Clock (BCLK) Select - divide-by-4
  58.     b15:b12 Reserved - The write value should be 0001b.
  59.     b10:b8 PCLKB - Peripheral Module Clock B(PCLKB) Select - divide-by-4
  60.     b7:b4 Reserved - The write value should be 0001b.
  61.     b3:b0 PCLKD - Peripheral Module Clock D(PCLKD) Select - divide-by-2 */
  62.     SYSTEM.SCKCR.LONG = 0x10811110;    /* ICLK,PCLKD: no division PCLKB,BCLK,FCLK: divide-by-2 */
  63.     while (0x10811110 != SYSTEM.SCKCR.LONG)
  64.     {
  65.          /* Confirm that the written value can be read correctly. */
  66.     }
  67.     /* ---- Set the BCLK pin output ---- */
  68.     /* BCKCR - External Bus Clock Control Register
  69.     b7:b1 Reserved - The write value should be 0.
  70.     b0 BCLKDIV - BCLK Pin Output Select - divide-by-2 */
  71.     SYSTEM.BCKCR.BYTE = 0x01;
  72.     while (0x01 != SYSTEM.BCKCR.BYTE)
  73.     {
  74.         /* Confirm that the written value can be read correctly. */
  75.     }
  76.     /* ---- Set the internal clock source ---- */
  77.     /* SCKCR3 - System Clock Control Register 3
  78.     b15:b11 Reserved - The write value should be 0.
  79.     b10:b8 CKSEL - Clock Source Select - PLL circuit is selected.
  80.     b7:b1 Reserved - The write value should be 0. */
  81.     SYSTEM.SCKCR3.WORD = 0x0100;
  82.     while (0x0100 != SYSTEM.SCKCR3.WORD)
  83.     {
  84.         /* Confirm that the written value can be read correctly. */
  85.     }
  86.     /* ---- Disable write protection ---- */
  87.     /* PRCR - Protect Register
  88.     b15:b8 PRKEY - PRC Key Code - A5h
  89.                   (The write value should be A5h to permission writing PRCi bit)
  90.     b2 PRC2 - Protect Bit 2 - Write disabled
  91.     b1 PRC1 - Protect Bit 1 - Write disabled
  92.     b0 PRC0 - Protect Bit 0 - Write disabled */
  93.     SYSTEM.PRCR.WORD = 0xA500;
  94. }
  95. void main(void)
  96. {
  97.     unsigned int i;
  98.     change_oscillation_HOCO(FREQ_50MHz);            //クロック変更
  99.     PORTC.PDR.BYTE = 0xFF;
  100.     PORTC.PODR.BYTE = 0x00;
  101.     PORTA.PDR.BYTE = 0xFF;
  102.     PORTA.PODR.BYTE =0x00;
  103.     PORTB.PDR.BYTE = 0xFF;
  104.     PORTB.PODR.BYTE = 0xFF;
  105.     while(1) {
  106.         PORTC.PODR.BYTE = ~PORTC.PODR.BYTE;
  107.         PORTA.PODR.BYTE = ~PORTA.PODR.BYTE;
  108.         PORTB.PODR.BYTE = ~PORTB.PODR.BYTE;
  109.         for(i=0;i<500000;i++) nop();
  110.     }
  111. }
コメント