忘備録-備忘録

技術的な備忘録

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. }


最新の画像もっと見る

コメントを投稿