goo blog サービス終了のお知らせ 

忘備録-備忘録

技術的な備忘録

RX210でFreeRTOSを動かす その3

2015-10-13 17:09:37 | RX210
秋月電子通商で販売しているAE-RX210ボードでリアルタイムOSのFreeRTOSを動かしてみました。 環境はe2 studio上でRXCです。
OS部を分離すると新しいプロジェクトに簡単に組み込むことができます。

いつも通りにプロジェクトを作成します。
[各種スタック領域を設定し、サポートファイルを追加]の場所で[ユーザー・スタックの使用]と[ヒープ・メモリーの使用]と[ベクター定義ファイル]のチェックを外しプロジェクトを作成します。


新しくできたプロジェクトに前に作成した[FreeRTOS]のフォルダをドラッグ&ドロップでコピーします。



これでFreeRTOSが使えるようになります。
ここにe2studio用のプロジェクトを置いておきます。

RX210でFreeRTOSを動かす その2

2015-10-09 08:29:46 | RX210
秋月電子通商で販売しているAE-RX210ボードでリアルタイムOSのFreeRTOSを動かしてみました。 環境はe2 studio上でRXCです。
今回はOS部分をきれいに分離し新しプロジェクトに組み込み安くしました。ファイルとフォルダの構成を次に示します。
[src]
 +---- iodefine.h       <-- ウイザードで自動作成
 +---- typedefine.h      <-- ウイザードで自動作成
 +---- dbsct.c         <-- ウイザードで自動作成
 +---- main.c         <-- ウイザードで自動作成
 |
 +-----[FreeRTOS]
       +------ croutine.h        
       +------ deprecated_definitions.h 
       +------ event_groups.h      
       +------ FreeRTOS.h        
       +------ list.h          
       +------ mpu_wrappers.h      
       +------ portable.h        
       +------ projdefs.h        
       +------ queue.h          
       +------ semphr.h         
       +------ StackMacros.h      
       +------ task.h          
       +------ timers.h         
       +------ croutine.c        
       +------ event_groups.c      
       +------ list.c          
       +------ queue.c          
       +------ tasks.c          
       +------ timers.c         
       |
       +------ [portable]
             +------ FreeRTOSConfig.h
             +------ portmacro.h
             +------ stacksct.h      <-- 移動
             +------ vect.h        <-- 移動
             +------ ApplicationHook.c  <-- 新規作成
             +------ heap_2.c
             +------ hwsetup.c       <-- 変更
             +------ interrupt_handlers.c <-- 移動
             +------ port.c
             +------ reset_program.c
             +------ vector_table.c


画面上ではこのようになります。

変更・新規作成したファイルを次に示します。
ApplicationHook.c(新規作成)
/*
* タスク切り替え用のタイマのスタートと各種フック関数
*
* サンプルではmain関数と同じファイルに記述してあったがこのファイルに移動させた
*/

/* Hardware specific includes. */
#include "../../iodefine.h"

/* Kernel includes. */
#include "../FreeRTOS.h"
#include "../task.h"
#include "../queue.h"

/* This variable is not used by this simple Blinky example.  It is defined
purely to allow the project to link as it is used by the full build
configuration. */
volatile unsigned long ulHighFrequencyTickCount = 0UL;

/*-----------------------------------------------------------*/


/* A callback function named vApplicationSetupTimerInterrupt() must be defined
to configure a tick interrupt source, and configTICK_VECTOR set in
FreeRTOSConfig.h to install the tick interrupt handler in the correct position
in the vector table.  This example uses a compare match timer.  It can be
into any FreeRTOS project, provided the same compare match timer is available. */
void vApplicationSetupTimerInterrupt( void )
{
     /* Enable compare match timer 0. */
     SYSTEM.PRCR.WORD = 0x0A502;
     MSTP( CMT0 ) = 0;
     SYSTEM.PRCR.WORD = 0x0A500;

     /* Interrupt on compare match. */
     CMT0.CMCR.BIT.CMIE = 1;
     CMT0.CMCR.BIT.CKS = 0;                         // CLK = PCLK/8

     /* Set the compare match value. */
     CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );

     /* Divide the PCLK by 8. */
     CMT0.CMCR.BIT.CKS = 0;

     /* Enable the interrupt... */
     _IEN( _CMT0_CMI0 ) = 1;
     ICU.IER[IER_CMT0_CMI0].BIT.IEN_CMT0_CMI0 = 1;     //Enable CMIE
     /* ...and set its priority to the application defined kernel priority. */
     _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
     ICU.IPR[IPR_CMT0_CMI0].BYTE = configKERNEL_INTERRUPT_PRIORITY;          // Set interrupt priority level
     /* Start the timer. */
     CMT.CMSTR0.BIT.STR0 = 1;
}
/*-----------------------------------------------------------*/



/* If configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h, then this
function will be called if pvPortMalloc() returns NULL because it has exhausted
the available FreeRTOS heap space.  See http://www.freertos.org/a00111.html. */
void vApplicationMallocFailedHook( void )
{
     for( ;; );
}
/*-----------------------------------------------------------*/

/* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 in
FreeRTOSConfig.h, then this function will be called if a task overflows its
stack space.  See
http://www.freertos.org/Stacks-and-stack-overflow-checking.html. */
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
     for( ;; );
}
/*-----------------------------------------------------------*/

/* If configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h, then this function
will be called on each iteration of the idle task.  See
http://www.freertos.org/a00016.html */
void vApplicationIdleHook( void )
{
     /* If this is being executed then the kernel has been started.  Start the high
     frequency timer test as described at the top of this file.  This is only
     included in the optimised build configuration - otherwise it takes up too much
     CPU time and can disrupt other tests. */
}
/*-----------------------------------------------------------*/


reset_program.c
/***************************************************************/
/*                                                             */
/*      PROJECT NAME :  FreeRTOS02                             */
/*      FILE         :  reset_program.c                        */
/*      DESCRIPTION  :  Reset program                          */
/*      CPU SERIES   :  RX200                                  */
/*      CPU TYPE     :  RX210                                  */
/*                                                             */
/*      This file is generated by e2 studio.                   */
/*                                                             */
/***************************************************************/                               
                                                                          
                                                                          
                                                                          
                                                                         

/*********************************************************************
*
* Device     : RX/RX200
*
* File Name  : resetprg.c
*
* Abstract   : Reset Program.
*
* History    : 1.00  (2010-12-17)
*            : 1.10  (2011-02-21)
*            : 1.11  (2011-06-20)
*            : 1.20  (2014-09-18)
*            : 1.21  (2014-10-22)
*
* NOTE       : THIS IS A TYPICAL EXAMPLE.
*
* Copyright (C) 2010 (2011-2014) Renesas Electronics Corporation. and
* Renesas Solutions Corp. All rights reserved.
*
*********************************************************************/

#include     <machine.h>
#include     <_h_c_lib.h>
//#include     <stddef.h>                         // Remove the comment when you use errno
//#include      <stdlib.h>                         // Remove the comment when you use rand()
#include     "../../typedefine.h"          // Define Types
#include "stacksct.h"          // Stack Sizes (Interrupt and User)

extern void HardwareSetup( void );

#ifdef __cplusplus
extern "C" {
#endif
void PowerON_Reset_PC(void);
void main(void);
#ifdef __cplusplus
}
#endif

//#ifdef __cplusplus                    // Use SIM I/O
//extern "C" {
//#endif
//extern void _INIT_IOLIB(void);
//extern void _CLOSEALL(void);
//#ifdef __cplusplus
//}
//#endif

#define PSW_init  0x00010000     // PSW bit pattern
#define FPSW_init 0x00000000     // FPSW bit base pattern

//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 HardwareSetup(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

#pragma section ResetPRG          // output PowerON_Reset to PResetPRG section

#pragma entry PowerON_Reset_PC

void PowerON_Reset_PC(void)
{
#ifdef __RXV2
     set_extb(__sectop("EXCEPTVECT"));
#endif
     set_intb(__sectop("C$VECT"));

#ifdef __FPU
#ifdef __ROZ                         // Initialize FPSW
#define _ROUND 0x00000001               // Let FPSW RMbits=01 (round to zero)
#else
#define _ROUND 0x00000000               // Let FPSW RMbits=00 (round to nearest)
#endif
#ifdef __DOFF
#define _DENOM 0x00000100               // Let FPSW DNbit=1 (denormal as zero)
#else
#define _DENOM 0x00000000               // Let FPSW DNbit=0 (denormal as is)
#endif
     set_fpsw(FPSW_init | _ROUND | _DENOM);
#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()
//     _s1ptr=NULL;                         // Remove the comment when you use strtok()


     HardwareSetup();                    // Use Hardware Setup
    nop();

//     _CALL_INIT();                         // Remove the comment when you use global class object

     set_psw(PSW_init);                    // Set Ubit & Ibit for PSW
//     chg_pmusr();                         // Remove the comment when you need to change PSW PMbit (SuperVisor->User)

     main();

//     _CLOSEALL();                         // Use SIM I/O
    
//     _CALL_END();                         // Remove the comment when you use global class object

     brk();
}


main.c
/* Hardware specific includes. */
#include "iodefine.h"

/* Kernel includes. */
#include "FreeRTOS/FreeRTOS.h"
#include "FreeRTOS/task.h"
#include "FreeRTOS/queue.h"



void vTask1(void *pvParameters)
{
     while(1) {
          PORTC.PODR.BIT.B1 = ~PORTC.PODR.BIT.B1;
          vTaskDelay(100/portTICK_PERIOD_MS);
     }
}

void vTask2(void *pvParameters)
{
     while(1) {
          PORTC.PODR.BIT.B2 = ~PORTC.PODR.BIT.B2;
          vTaskDelay(200/portTICK_PERIOD_MS);
     }
}

void vTask3(void *pvParameters)
{
     while(1) {
          PORTC.PODR.BIT.B3 = ~PORTC.PODR.BIT.B3;
          vTaskDelay(300/portTICK_PERIOD_MS);
     }
}

void main(void)
{

     /* Turn all LEDs off. */
     PORTC.PDR.BYTE = 0xFF;     //ポートC出力
     PORTC.PODR.BYTE = 0xFF;     //初期値


     xTaskCreate(vTask1,"Task1",100,NULL,1,NULL);
     xTaskCreate(vTask2,"Task2",100,NULL,1,NULL);
     xTaskCreate(vTask3,"Task3",100,NULL,1,NULL);


     /* Create the queue. */
     vTaskStartScheduler();

     /* If all is well the next line of code will not be reached as the scheduler
     will be     running.  If the next line is reached then it is likely that there was
     insufficient heap available for the idle task to be created. */
     for( ;; );
}

RX210でFreeRTOSを動かす

2015-10-08 16:35:41 | RX210

秋月電子通商で販売しているAE-RX210ボードでリアルタイムOSのFreeRTOSを動かしてみました。 環境はe2 studio上でRXCです。

FreeRTOSのサイトからソースコードをダウンロードして展開します。ソースコードには各種CPUアーキテクチャ用のファイルが混在しているので必要なものを e2 studioに登録していきます。どのフォルダからコピーしたのかを示します。

[src]
 +---- iodefine.h      --- ウイザードで自動作成
 +---- stacksct.h      --- ウイザードで自動作成
 +---- typedefine.h     --- ウイザードで自動作成
 +---- vect.h        --- ウイザードで自動作成したものを書き換え
 +---- dbsct.c        --- ウイザードで自動作成
 +---- interrupt_handlers.c --- ウイザードで自動作成
 +---- main.c        --- ウイザードで自動作成
 +---- reset_program.c    --- ウイザードで自動作成
 +---- vector_table.c    --- ウイザードで自動作成
 |
 +-----[FreeRTOS]
       +------ croutine.h        --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ deprecated_definitions.h --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ event_groups.h      --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ FreeRTOS.h        --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ list.h          --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ mpu_wrappers.h      --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ portable.h        --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ projdefs.h        --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ queue.h          --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ semphr.h         --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ StackMacros.h       --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ task.h          --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ timers.h         --- FreeRTOSv8.2.2\FreeRTOS\Source\include からコピー
       +------ croutine.c        --- FreeRTOSv8.2.2\FreeRTOS\Source からコピー
       +------ event_groups.c      --- FreeRTOSv8.2.2\FreeRTOS\Source からコピー
       +------ list.c          --- FreeRTOSv8.2.2\FreeRTOS\Source からコピー
       +------ queue.c          --- FreeRTOSv8.2.2\FreeRTOS\Source からコピー
       +------ tasks.c          --- FreeRTOSv8.2.2\FreeRTOS\Source からコピー
       +------ timers.c         --- FreeRTOSv8.2.2\FreeRTOS\Source からコピー
       |
       +------ [portable]
             +------ FreeRTOSConfig.h --- FreeRTOSv8.2.2\FreeRTOS\Demo\RX200_RX210-RSK_Renesas\RTOSDemo からコピーし書き換え
             +------ heap_2.c     --- FreeRTOSv8.2.2\FreeRTOS\Source\portable\MemMang からコピー
             +------ hwsetup.c     --- FreeRTOSv8.2.2\FreeRTOS\Demo\RX200_RX210-RSK_Renesas\RTOSDemo\Renesas-Files からコピーし書き換え
             +------ port.c      --- FreeRTOSv8.2.2\FreeRTOS\Source\portable\Renesas\RX200 からコピー
             +------ portmacro.h    --- FreeRTOSv8.2.2\FreeRTOS\Source\portable\Renesas\RX200 からコピー
             +------ port_asm.src   --- FreeRTOSv8.2.2\FreeRTOS\Source\portable\Renesas\RX200 からコピー


登録後はこのようになります。



ファイルのフォルダ構成を変更したのでソースコード内の
#includde "filename.h"
行のファイルの位置を相対パスで指定しなおします。

次にコードを一部書き直します。
main.c
/* Hardware specific includes. */
#include "iodefine.h"

/* Kernel includes. */
#include "FreeRTOS/FreeRTOS.h"
#include "FreeRTOS/task.h"
#include "FreeRTOS/queue.h"


/* This variable is not used by this simple Blinky example.  It is defined
purely to allow the project to link as it is used by the full build
configuration. */
volatile unsigned long ulHighFrequencyTickCount = 0UL;

/*-----------------------------------------------------------*/

void vTask1(void *pvParameters)
{
     while(1) {
          PORTC.PODR.BIT.B1 = ~PORTC.PODR.BIT.B1;
          vTaskDelay(100/portTICK_PERIOD_MS);
     }
}

void vTask2(void *pvParameters)
{
     while(1) {
          PORTC.PODR.BIT.B2 = ~PORTC.PODR.BIT.B2;
          vTaskDelay(200/portTICK_PERIOD_MS);
     }
}

void vTask3(void *pvParameters)
{
     while(1) {
          PORTC.PODR.BIT.B3 = ~PORTC.PODR.BIT.B3;
          vTaskDelay(300/portTICK_PERIOD_MS);
     }
}

void main(void)
{
extern void HardwareSetup( void );

     /* Renesas provided CPU configuration routine.  The clocks are configured in
     here. */
     HardwareSetup();

     /* Turn all LEDs off. */
     PORTC.PDR.BYTE = 0xFF;     //ポートC出力
     PORTC.PODR.BYTE = 0xFF;     //初期値


     xTaskCreate(vTask1,"Task1",100,NULL,1,NULL);
     xTaskCreate(vTask2,"Task2",100,NULL,1,NULL);
     xTaskCreate(vTask3,"Task3",100,NULL,1,NULL);


     /* Create the queue. */
     vTaskStartScheduler();

     /* If all is well the next line of code will not be reached as the scheduler
     will be     running.  If the next line is reached then it is likely that there was
     insufficient heap available for the idle task to be created. */
     for( ;; );
}
/*-----------------------------------------------------------*/


/* A callback function named vApplicationSetupTimerInterrupt() must be defined
to configure a tick interrupt source, and configTICK_VECTOR set in
FreeRTOSConfig.h to install the tick interrupt handler in the correct position
in the vector table.  This example uses a compare match timer.  It can be
into any FreeRTOS project, provided the same compare match timer is available. */
void vApplicationSetupTimerInterrupt( void )
{
     /* Enable compare match timer 0. */
     SYSTEM.PRCR.WORD = 0x0A502;
     MSTP( CMT0 ) = 0;
     SYSTEM.PRCR.WORD = 0x0A500;

     /* Interrupt on compare match. */
     CMT0.CMCR.BIT.CMIE = 1;
     CMT0.CMCR.BIT.CKS = 0;                         // CLK = PCLK/8

     /* Set the compare match value. */
     CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );

     /* Divide the PCLK by 8. */
     CMT0.CMCR.BIT.CKS = 0;

     /* Enable the interrupt... */
     _IEN( _CMT0_CMI0 ) = 1;
     ICU.IER[IER_CMT0_CMI0].BIT.IEN_CMT0_CMI0 = 1;     //Enable CMIE
     /* ...and set its priority to the application defined kernel priority. */
     _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
     ICU.IPR[IPR_CMT0_CMI0].BYTE = configKERNEL_INTERRUPT_PRIORITY;          // Set interrupt priority level
     /* Start the timer. */
     CMT.CMSTR0.BIT.STR0 = 1;
}
/*-----------------------------------------------------------*/

/* If configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h, then this
function will be called if pvPortMalloc() returns NULL because it has exhausted
the available FreeRTOS heap space.  See http://www.freertos.org/a00111.html. */
void vApplicationMallocFailedHook( void )
{
     for( ;; );
}
/*-----------------------------------------------------------*/

/* If configCHECK_FOR_STACK_OVERFLOW is set to either 1 or 2 in
FreeRTOSConfig.h, then this function will be called if a task overflows its
stack space.  See
http://www.freertos.org/Stacks-and-stack-overflow-checking.html. */
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
     for( ;; );
}
/*-----------------------------------------------------------*/

/* If configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h, then this function
will be called on each iteration of the idle task.  See
http://www.freertos.org/a00016.html */
void vApplicationIdleHook( void )
{
     /* If this is being executed then the kernel has been started.  Start the high
     frequency timer test as described at the top of this file.  This is only
     included in the optimised build configuration - otherwise it takes up too much
     CPU time and can disrupt other tests. */
     #ifdef INCLUDE_HIGH_FREQUENCY_TIMER_TEST
     static portBASE_TYPE xTimerTestStarted = pdFALSE;
     extern void vSetupHighFrequencyTimer( void );
          if( xTimerTestStarted == pdFALSE )
          {
               vSetupHighFrequencyTimer();
               xTimerTestStarted = pdTRUE;
          }
     #endif
}
/*-----------------------------------------------------------*/


vect.h
vect.h
の70行目から76行目をコメントアウト
// FCU FIFERR
#pragma interrupt (Excep_FCU_FIFERR(vect=21))
void Excep_FCU_FIFERR( void);

// FCU FRDYI
#pragma interrupt (Excep_FCU_FRDYI(vect=23))
void Excep_FCU_FRDYI( void);

// ICU SWINT
//#pragma interrupt (Excep_ICU_SWINT(vect =27))  --- コメントアウト
//void Excep_ICU_SWINT(void);           --- コメントアウト

// CMT0 CMI0
//#pragma interrupt (Excep_CMT0_CMI0(vect =28))  --- コメントアウト
//void Excep_CMT0_CMI0(void);           --- コメントアウト

// CMT1 CMI1
#pragma interrupt (Excep_CMT1_CMI1(vect=29))
void Excep_CMT1_CMI1( void);


FreeRTOSConfig.h
/*
    FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
    All rights reserved

    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.

    This file is part of the FreeRTOS distribution.

    FreeRTOS is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License (version 2) as published by the
    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.

    ***************************************************************************
    >>!   NOTE: The modification to the GPL is included to allow you to     !<<
    >>!   distribute a combined work that includes FreeRTOS without being   !<<
    >>!   obliged to provide the source code for proprietary components     !<<
    >>!   outside of the FreeRTOS kernel.                                   !<<
    ***************************************************************************

    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  Full license text is available on the following
    link: http://www.freertos.org/a00114.html

    ***************************************************************************
     *                                                                       *
     *    FreeRTOS provides completely free yet professionally developed,    *
     *    robust, strictly quality controlled, supported, and cross          *
     *    platform software that is more than just the market leader, it     *
     *    is the industry's de facto standard.                               *
     *                                                                       *
     *    Help yourself get started quickly while simultaneously helping     *
     *    to support the FreeRTOS project by purchasing a FreeRTOS           *
     *    tutorial book, reference manual, or both:                          *
     *    http://www.FreeRTOS.org/Documentation                              *
     *                                                                       *
    ***************************************************************************

    http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
    the FAQ page "My application does not run, what could be wrong?".  Have you
    defined configASSERT()?

    http://www.FreeRTOS.org/support - In return for receiving this top quality
    embedded software for free we request you assist our global community by
    participating in the support forum.

    http://www.FreeRTOS.org/training - Investing in training allows your team to
    be as productive as possible as early as possible.  Now you can receive
    FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
    Ltd, and the world's leading authority on the world's leading RTOS.

    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
    including FreeRTOS+Trace - an indispensable productivity tool, a DOS
    compatible FAT file system, and our tiny thread aware UDP/IP stack.

    http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
    Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.

    http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
    Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
    licenses offer ticketed support, indemnification and commercial middleware.

    http://www.SafeRTOS.com - High Integrity Systems also provide a safety
    engineered and independently SIL3 certified version for use in safety and
    mission critical applications that require provable dependability.

    1 tab == 4 spaces!
*/

#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H

/* Board specifics. */
//#include "rskrx210def.h"


/* System Clock Settings */
/* ボードのクロック設定
* CLK_SRC_HOCO=0 で 20MHz xtal
* CLK_SRC_HOCO=1 で 50MHz 内臓発振器
*/
#define          CLK_SRC_HOCO     0

/* DETAIL THIS LATER !!!! */
#if (CLK_SRC_HOCO == 0)
/* External xtal and PLL circuit */
#define     XTAL_FREQUENCY  (20000000L)
#define     PLL_MUL         (10)
#define     PLL_INPUT_FREQ_DIV         (2)
#define     ICLK_DIV        (2)
#define     PCLK_DIV        (4)
#define     BCLK_DIV        (4)
#define     PLL_FREQUENCY   (XTAL_FREQUENCY * (PLL_MUL / PLL_INPUT_FREQ_DIV))
#define     ICLK_FREQUENCY  (PLL_FREQUENCY / ICLK_DIV)
#define     PCLK_FREQUENCY  (PLL_FREQUENCY / PCLK_DIV)
#define     BCLK_FREQUENCY  (PLL_FREQUENCY / BCLK_DIV)
#else
/* Internal high speed on-chip oscillator (HOCO) */
#define     XTAL_FREQUENCY  (50000000L)
#define     PLL_MUL         (1)
#define     PLL_INPUT_FREQ_DIV        (1)
#define     ICLK_DIV        (1)
#define     PCLK_DIV        (2)
#define     BCLK_DIV        (2)
#define     PLL_FREQUENCY   (XTAL_FREQUENCY * (PLL_MUL / PLL_INPUT_FREQ_DIV))
#define     ICLK_FREQUENCY  (PLL_FREQUENCY / ICLK_DIV)
#define     PCLK_FREQUENCY  (PLL_FREQUENCY / PCLK_DIV)
#define     BCLK_FREQUENCY  (PLL_FREQUENCY / BCLK_DIV)
#endif

/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html.
*----------------------------------------------------------*/

#define configUSE_PREEMPTION               1
#define configUSE_IDLE_HOOK                    1
#define configUSE_TICK_HOOK                    0
#define configCPU_CLOCK_HZ                    ( ICLK_FREQUENCY ) /* Set in rskrx210def.h. */
#define configPERIPHERAL_CLOCK_HZ          ( PCLK_FREQUENCY ) /* Set in rskrx210def.h. */
#define configTICK_RATE_HZ                    ( ( TickType_t ) 1000 )
#define configMINIMAL_STACK_SIZE          ( ( unsigned short ) 140 )
#define configTOTAL_HEAP_SIZE               ( ( size_t ) ( 50 * 1024 ) )
#define configMAX_TASK_NAME_LEN               ( 12 )
#define configUSE_TRACE_FACILITY          1
#define configUSE_16_BIT_TICKS               0
#define configIDLE_SHOULD_YIELD               1
#define configUSE_CO_ROUTINES                0
#define configUSE_MUTEXES                    1
#define configGENERATE_RUN_TIME_STATS     1
#define configCHECK_FOR_STACK_OVERFLOW     2
#define configUSE_RECURSIVE_MUTEXES          1
#define configQUEUE_REGISTRY_SIZE          0
#define configUSE_MALLOC_FAILED_HOOK     1
#define configUSE_APPLICATION_TASK_TAG     0

#define configMAX_PRIORITIES               ( 7 )
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )

/* Software timer definitions. */
#define configUSE_TIMERS                    1
#define configTIMER_TASK_PRIORITY          ( 3 )
#define configTIMER_QUEUE_LENGTH          5
#define configTIMER_TASK_STACK_DEPTH     ( configMINIMAL_STACK_SIZE )

/* The interrupt priority used by the kernel itself for the tick interrupt and
the pended interrupt.  This would normally be the lowest priority. */
#define configKERNEL_INTERRUPT_PRIORITY         1

/* The maximum interrupt priority from which FreeRTOS API calls can be made.
Interrupts that use a priority above this will not be effected by anything the
kernel is doing. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY    4

/* The peripheral used to generate the tick interrupt is configured as part of
the application code.  This constant should be set to the vector number of the
peripheral chosen.  As supplied this is CMT0. */
#define configTICK_VECTOR                              _CMT0_CMI0

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */

#define INCLUDE_vTaskPrioritySet               1
#define INCLUDE_uxTaskPriorityGet               1
#define INCLUDE_vTaskDelete                         1
#define INCLUDE_vTaskCleanUpResources          0
#define INCLUDE_vTaskSuspend                    1
#define INCLUDE_vTaskDelayUntil                    1
#define INCLUDE_vTaskDelay                         1
#define INCLUDE_uxTaskGetStackHighWaterMark     1
#define INCLUDE_xTaskGetSchedulerState          1
#define INCLUDE_eTaskGetState                    1

#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); }
extern volatile unsigned long ulHighFrequencyTickCount;
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() nop() /* Run time stats use the same timer as the high frequency timer test. */
#define portGET_RUN_TIME_COUNTER_VALUE() ulHighFrequencyTickCount


/* Override some of the priorities set in the common demo tasks.  This is
required to ensure flase positive timing errors are not reported. */
#define bktPRIMARY_PRIORITY          ( configMAX_PRIORITIES - 3 )
#define bktSECONDARY_PRIORITY     ( configMAX_PRIORITIES - 4 )
#define intqHIGHER_PRIORITY          ( configMAX_PRIORITIES - 3 )

#endif /* FREERTOS_CONFIG_H */


hwsetup.c
/******************************************************************************
* DISCLAIMER

* This software is supplied by Renesas Technology Corp. and is only
* intended for use with Renesas products. No other uses are authorized.

* This software is owned by Renesas Technology Corp. 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
* TECHNOLOGY CORP. NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
* FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES
* FOR ANY REASON RELATED TO THE 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) 2008. Renesas Technology Corp., All Rights Reserved.
*******************************************************************************    
* File Name    : hwsetup.c
* Version      : 1.00
* Description  : Power up hardware initializations
******************************************************************************
* History : DD.MM.YYYY Version Description
*         : 15.02.2010 1.00    First Release
******************************************************************************/


/******************************************************************************
Includes   <System Includes> , "Project Includes"
******************************************************************************/
#include <stdint.h>
#include "../../iodefine.h"
#include "../FreeRTOS.h"

/******************************************************************************
Typedef definitions
******************************************************************************/

/******************************************************************************
Macro definitions
******************************************************************************/

/******************************************************************************
Imported global variables and functions (from other files)
******************************************************************************/

/******************************************************************************
Exported global variables and functions (to be accessed by other files)
******************************************************************************/

/******************************************************************************
Private global variables and functions
******************************************************************************/
void io_set_cpg(void);
void EnablePeripheralModules(void);

/******************************************************************************
* Function Name: HardwareSetup
* Description  : This function does initial setting for CPG port pins used in
*              : the Demo including the MII pins of the Ethernet PHY connection.
* Arguments    : none
* Return Value : none
******************************************************************************/
void HardwareSetup(void)
{
     /* CPG setting */
     io_set_cpg();

    /* Enables peripherals */
    EnablePeripheralModules();

}

/******************************************************************************
* Function Name: EnablePeripheralModules
* Description  : Enables Peripheral Modules before use
* Arguments    : none
* Return Value : none
******************************************************************************/
void EnablePeripheralModules(void)
{
     /*  Module standby clear */
     SYSTEM.PRCR.WORD = 0x0A502;
     MSTP( CMT0 ) = 0;
     SYSTEM.PRCR.WORD = 0x0A500;
}


/******************************************************************************
* Function Name: io_set_cpg
* Description  : Sets up operating speed
* Arguments    : none
* Return Value : none
******************************************************************************/
void io_set_cpg(void)
{
/* Set CPU PLL operating frequencies. Changes to the peripheral clock will require
changes to the debugger and flash kernel BRR settings. */

     /* ==== CPG setting ==== */

     volatile unsigned int i;

     SYSTEM.PRCR.WORD = 0xA507;                    /* Protect off                               */

#if (CLK_SRC_HOCO == 1)    
     /* ---- Set the VRCR register ---- */
     SYSTEM.VRCR = 0x00;

     SYSTEM.HOCOPCR.BYTE = 0x00;                    /* HOCO power supply on */
     SYSTEM.HOCOCR2.BYTE = 0x03;                    /* Select - 50MHz */
     SYSTEM.HOCOWTCR2.BYTE = 0x03;               /* Set wait time until the HOCO oscillator stabilizes  */
     SYSTEM.HOCOCR.BYTE  = 0x01;                    /* HOCO is operating */

    SYSTEM.SCKCR.LONG = 0x10811110;     /* ICLK,PCLKD: no division PCLKB,BCLK,FCLK: divide-by-2 */
    while (0x10811110 != SYSTEM.SCKCR.LONG)
    {
         /* Confirm that the written value can be read correctly. */
    }

    SYSTEM.BCKCR.BYTE = 0x01;     /* ---- Set the BCLK pin output ---- */

    while (0x01 != SYSTEM.BCKCR.BYTE)
    {
        /* Confirm that the written value can be read correctly. */
    }

     for(i=0; i<10; i++){                         /* wait over 60us */
     }
     //     SYSTEM.SCKCR.LONG = 0x21823333;               /* ICK=PLL/2,FCK,PCK,BCL=PLL/4 */
     /************************************************************************/
     /*  If setting bits individually, rather than a single long write,           */
     /*     set the BCK value before that of ICK                                         */
     /************************************************************************/
     //     SYSTEM.SCKCR.BIT.PCKD      = 3;               /* PLL/8 = 10MHz          */
     //     //SYSTEM.SCKCR.BIT.PCKC      = 3;               /* PLL/8 = 10MHz          */
     //     SYSTEM.SCKCR.BIT.PCKB      = 3;               /* PLL/8 = 10MHz          */
     //     //SYSTEM.SCKCR.BIT.PCKA      = 3;               /* PLL/8 = 10MHz          */
     //     SYSTEM.SCKCR.BIT.BCK      = 3;               /* PLL/8 = 10MHz          */
     //     SYSTEM.SCKCR.BIT.PSTOP1 = 1;               /* BUS CLK OUT Disabled */
     //     SYSTEM.SCKCR.BIT.ICK      = 1;               /* PLL/2 = 40MHz          */
     //     SYSTEM.SCKCR.BIT.FCK      = 2;               /* PLL/4 = 20MHz          */

#else
    /* ---- Set the VRCR register ---- */
    SYSTEM.VRCR = 0x00;
    SYSTEM.MOFCR.BYTE = (0x30);     /* Drive capability : 20 MHz crystal resonator */

     SYSTEM.MOSCWTCR.BYTE = 0x0D;               /* Main Clock Oscillator Wait Control Register */
                                                       /* 131072 cycles (approx. 6.55 ms). */
                                                       /* wait over 2 ms  @20MHz                */

     SYSTEM.PLLWTCR.BYTE = 0x0B;                    /* PLL Wait Control Register           */
                                                       /* 262144 states                          */
                                                       /* wait over 2.1 ms  @PLL = 80Hz     */
                                                       /*                         (20/2x8*8)           */
    
     SYSTEM.MOSCCR.BYTE = 0x00;                    /* EXTAL OFF */
    while (0x00 != SYSTEM.MOSCCR.BYTE)
    {
        /* Confirm that the written value can be read correctly. */
    }
    /* ---- Wait processing for the clock oscillation stabilization ---- */
    for(i=0;i<100;i++) {
    }



    SYSTEM.PLLCR.WORD = (0x0901);     /* Division ratio and multiplication factor : divide-by-2, multiply-by-10 */
                                                       /* Input to PLL (EXTAL in) / 2           */
                                                       /* Therefore:
                                                                 PLL = EXTAL / 2     
                                                                      = 20M / 2
                                                                      = 10MHz                                                                     
                                                            PLL * 8 = 80Mhz
                                                            PLL *10 = 100MHz */
    SYSTEM.PLLWTCR.BYTE = (0x09);     /* Wait control register : 65536 cycles (approx. 655.36 us) */
    SYSTEM.PLLCR2.BYTE = 0x00;
    /* ---- Wait processing for the clock oscillation stabilization ---- */
    for(i=0;i<100;i++) {
    }

    SYSTEM.SCKCR.LONG = 0x21821211;     /* ICLK,PCLKD: divide-by-2 PCLKB,BCLK,FCLK: divide-by-4 */
    while (0x21821211 != SYSTEM.SCKCR.LONG)
    {
         /* Confirm that the written value can be read correctly. */
    }

    SYSTEM.BCKCR.BYTE = 0x01;     /* ---- Set the BCLK pin output ---- */
    while (0x01 != SYSTEM.BCKCR.BYTE)
    {
        /* Confirm that the written value can be read correctly. */
    }
#endif
    

     while(SYSTEM.OPCCR.BIT.OPCMTSF == 1);
     SYSTEM.OPCCR.BIT.OPCMTSF = 0;               /* High-speed operating mode */
     while(SYSTEM.OPCCR.BIT.OPCMTSF == 1);
#if (CLK_SRC_HOCO == 1)    
     SYSTEM.SCKCR3.WORD = 0x0100;               /* LOCO -> HOCO */
#else
     SYSTEM.SCKCR3.WORD = 0x0400;               /* LOCO -> PLL */
#endif
     SYSTEM.PRCR.WORD = 0xA500;                    /* Protect on     */
}


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