projects/echo/r_bsp/board/grsakurarx63n/resetprg.c
static void clock_source_select (void)
{
    /* Declared volatile for software delay purposes. */
    volatile uint32_t i;

    /* The delay loops used below have been measured to take 86us per iteration. This has been verified using the 
       Renesas RX Toolchain with optimizations set to 2, size priority. The same result was obtained using 2, speed 
       priority. At this time the MCU is still running on the 125kHz LOCO. */

#if (BSP_CFG_CLOCK_SOURCE == 1)
    /* HOCO is chosen. Start it operating. */
    SYSTEM.HOCOCR.BYTE = 0x00;          

    /* The delay period needed is to make sure that the HOCO has stabilized. According to Rev.1.00 of the RX63N's HW 
       manual the delay period is tHOCOWT2 which is 2ms. A delay of 2.4ms has been used below to account for variations 
       in the LOCO. 
       2.4ms / 86us (per iteration) = 28 iterations */
    for(i = 0; i < 28; i++)             
    {
        /* Wait 2.4ms. See comment above for why. */
        nop() ;
    }
#else
    /* HOCO is not chosen. */
    /* Stop the HOCO. */
    SYSTEM.HOCOCR.BYTE = 0x01;          

    /* Turn off power to HOCO. */
    SYSTEM.HOCOPCR.BYTE = 0x01;
#endif

#if (BSP_CFG_CLOCK_SOURCE == 2)
    /* Main clock oscillator is chosen. Start it operating. */
    /* Wait 65,536 cycles * 12 MHz = 5.46 ms */
    SYSTEM.MOSCWTCR.BYTE = 0x0C;        

    /* Set the main clock to operating. */
    SYSTEM.MOSCCR.BYTE = 0x00;          

    /* The delay period needed is to make sure that the main clock has stabilized. This delay period is tMAINOSCWT in 
       the HW manual and according to the Renesas Technical Update document TN-RX*-A021A/E this is defined as:
       n = Wait time selected by MOSCWTCR.MSTS[] bits
       tMAINOSC = Main clock oscillator start-up time. From referring to various vendors, a start-up time of 4ms appears
                  to be a common maximum. To be safe we will use 5ms.
       tMAINOSCWT = tMAINOSC + ((n+16384)/fMAIN)
       tMAINOSCWT = 5ms + ((65536 + 16384)/12MHz)
       tMAINOSCWT = 11.82ms
       A delay of 12ms has been used below to account for variations in the LOCO. 
       12ms / 86us (per iteration) = 140 iterations */
    for(i = 0; i < 140; i++)             
    {
        /* Wait 12ms. See comment above for why. */
        nop() ;
    }
#else
    /* Set the main clock to stopped. */
    SYSTEM.MOSCCR.BYTE = 0x01;          
#endif

#if (BSP_CFG_CLOCK_SOURCE == 3)
    /* Sub-clock oscillator is chosen. Start it operating. */
    /* Wait 65,536 cycles * 32768Hz = 2s. This meets the timing requirement tSUBOSC which is 2 seconds. */
    SYSTEM.SOSCWTCR.BYTE = 0x0C;        

    /* Set the sub-clock to operating. */
    SYSTEM.SOSCCR.BYTE = 0x00;          

    /* The delay period needed is to make sure that the sub-clock has stabilized. According to Rev.1.00 of the RX63N's 
       HW manual the delay period is tSUBOSCWT0 which is at minimum 1.8s and at maximum 2.6 seconds. We will use the
       maximum value to be safe.       
       2.6s / 86us (per iteration) = 30233 iterations */
    for(i = 0; i < 30233; i++)             
    {
        /* Wait 2.6s. See comment above for why. */
        nop() ;
    }
#else
    /* Set the sub-clock to stopped. */
    SYSTEM.SOSCCR.BYTE = 0x01;          
#endif

#if (BSP_CFG_CLOCK_SOURCE == 4)
    /* PLL is chosen. Start it operating. Must start main clock as well since PLL uses it. */
    /* Wait 65,536 cycles * 12 MHz = 5.46 ms */
    SYSTEM.MOSCWTCR.BYTE = 0x0C;        

    /* Set the main clock to operating. */
    SYSTEM.MOSCCR.BYTE = 0x00;          

    /* PLL wait is 1,048,576 cycles * 192 MHz (12 MHz * 16) = 5.46 ms*/
    SYSTEM.PLLWTCR.BYTE = 0x0D;         

    /* Set PLL Input Divisor. */
    SYSTEM.PLLCR.BIT.PLIDIV = BSP_CFG_PLL_DIV >> 1;

    /* Set PLL Multiplier. */
    SYSTEM.PLLCR.BIT.STC = BSP_CFG_PLL_MUL - 1;

    /* Set the PLL to operating. */
    SYSTEM.PLLCR2.BYTE = 0x00;          

    /* The delay period needed is to make sure that the main clock and PLL have stabilized. This delay period is 
       tPLLWT2 when the main clock has not been previously stabilized. According to the Renesas Technical Update 
       document TN-RX*-A021A/E this delay is defined as:
       n = Wait time selected by PLLWTCR.PSTS[] bits
       tMAINOSC = Main clock oscillator start-up time. From referring to various vendors, a start-up time of 4ms appears
                  to be a common maximum. To be safe we will use 5ms.
       tPLLWT2 = tMAINOSC + tPLL1 + ((n + 131072)/fPLL)
       tPLLWT2 = 5ms + 500us + ((1048576 + 131072)/192MHz)
       tPLLWT2 = 11.64ms
       A delay of 12ms has been used below to account for variations in the LOCO. 
       12ms / 86us (per iteration) = 140 iterations */
    for(i = 0; i < 140; i++)             
    {
        /* Wait 12ms. See comment above for why. */
        nop() ;
    }
#else
    /* Set the PLL to stopped. */
    SYSTEM.PLLCR2.BYTE = 0x01;          
#endif

    /* LOCO is saved for last since it is what is running by default out of reset. This means you do not want to turn
       it off until another clock has been enabled and is ready to use. */
#if (BSP_CFG_CLOCK_SOURCE == 0)
    /* LOCO is chosen. This is the default out of reset. */
    SYSTEM.LOCOCR.BYTE = 0x00;
#else
    /* LOCO is not chosen but it cannot be turned off yet since it is still being used. */
#endif

    /* Make sure a valid clock was chosen. */
#if (BSP_CFG_CLOCK_SOURCE > 4) || (BSP_CFG_CLOCK_SOURCE < 0)
    #error "ERROR - Valid clock source must be chosen in r_bsp_config.h using BSP_CFG_CLOCK_SOURCE macro."
#endif 
}
/***********************************************************************************************************************
* Function name: clock_source_select
* Description  : Enables and disables clocks as chosen by the user. This function also implements the software delays
*                needed for the clocks to stabilize.
* Arguments    : none
* Return value : none
***********************************************************************************************************************/
Function name: clock_source_select
Description  : Enables and disables clocks as chosen by the user. This function also implements the software delays
needed for the clocks to stabilize.
Arguments    : none
Return value : none
Function name : clock_source_select Description : Enables and disables clocks as chosen by the user . This function also implements the software delays needed for the clocks to stabilize . Arguments : none Return value : none
static void clock_source_select (void)
{
    volatile uint32_t i;
#if (BSP_CFG_CLOCK_SOURCE == 1)
    SYSTEM.HOCOCR.BYTE = 0x00;          
    for(i = 0; i < 28; i++)             
    {
        nop() ;
    }
#else
    SYSTEM.HOCOCR.BYTE = 0x01;          
    SYSTEM.HOCOPCR.BYTE = 0x01;
#endif
#if (BSP_CFG_CLOCK_SOURCE == 2)
    SYSTEM.MOSCWTCR.BYTE = 0x0C;        
    SYSTEM.MOSCCR.BYTE = 0x00;          
    for(i = 0; i < 140; i++)             
    {
        nop() ;
    }
#else
    SYSTEM.MOSCCR.BYTE = 0x01;          
#endif
#if (BSP_CFG_CLOCK_SOURCE == 3)
    SYSTEM.SOSCWTCR.BYTE = 0x0C;        
    SYSTEM.SOSCCR.BYTE = 0x00;          
    for(i = 0; i < 30233; i++)             
    {
        nop() ;
    }
#else
    SYSTEM.SOSCCR.BYTE = 0x01;          
#endif
#if (BSP_CFG_CLOCK_SOURCE == 4)
    SYSTEM.MOSCWTCR.BYTE = 0x0C;        
    SYSTEM.MOSCCR.BYTE = 0x00;          
    SYSTEM.PLLWTCR.BYTE = 0x0D;         
    SYSTEM.PLLCR.BIT.PLIDIV = BSP_CFG_PLL_DIV >> 1;
    SYSTEM.PLLCR.BIT.STC = BSP_CFG_PLL_MUL - 1;
    SYSTEM.PLLCR2.BYTE = 0x00;          
    for(i = 0; i < 140; i++)             
    {
        nop() ;
    }
#else
    SYSTEM.PLLCR2.BYTE = 0x01;          
#endif
#if (BSP_CFG_CLOCK_SOURCE == 0)
    SYSTEM.LOCOCR.BYTE = 0x00;
#else
#endif
#if (BSP_CFG_CLOCK_SOURCE > 4) || (BSP_CFG_CLOCK_SOURCE < 0)
    #error "ERROR - Valid clock source must be chosen in r_bsp_config.h using BSP_CFG_CLOCK_SOURCE macro."
#endif 
}
static void clock_source_select ( void ) { volatile uint32_t i ; #if ( BSP_CFG_CLOCK_SOURCE == 1 ) SYSTEM . HOCOCR . BYTE = 0x00 ; for ( i = 0 ; i < 28 ; i ++ ) { nop ( ) ; } #else SYSTEM . HOCOCR . BYTE = 0x01 ; SYSTEM . HOCOPCR . BYTE = 0x01 ; #endif #if ( BSP_CFG_CLOCK_SOURCE == 2 ) SYSTEM . MOSCWTCR . BYTE = 0x0C ; SYSTEM . MOSCCR . BYTE = 0x00 ; for ( i = 0 ; i < 140 ; i ++ ) { nop ( ) ; } #else SYSTEM . MOSCCR . BYTE = 0x01 ; #endif #if ( BSP_CFG_CLOCK_SOURCE == 3 ) SYSTEM . SOSCWTCR . BYTE = 0x0C ; SYSTEM . SOSCCR . BYTE = 0x00 ; for ( i = 0 ; i < 30233 ; i ++ ) { nop ( ) ; } #else SYSTEM . SOSCCR . BYTE = 0x01 ; #endif #if ( BSP_CFG_CLOCK_SOURCE == 4 ) SYSTEM . MOSCWTCR . BYTE = 0x0C ; SYSTEM . MOSCCR . BYTE = 0x00 ; SYSTEM . PLLWTCR . BYTE = 0x0D ; SYSTEM . PLLCR . BIT . PLIDIV = BSP_CFG_PLL_DIV >> 1 ; SYSTEM . PLLCR . BIT . STC = BSP_CFG_PLL_MUL - 1 ; SYSTEM . PLLCR2 . BYTE = 0x00 ; for ( i = 0 ; i < 140 ; i ++ ) { nop ( ) ; } #else SYSTEM . PLLCR2 . BYTE = 0x01 ; #endif #if ( BSP_CFG_CLOCK_SOURCE == 0 ) SYSTEM . LOCOCR . BYTE = 0x00 ; #else #endif #if ( BSP_CFG_CLOCK_SOURCE > 4 ) || ( BSP_CFG_CLOCK_SOURCE < 0 ) #error  "ERROR - Valid clock source must be chosen in r_bsp_config.h using BSP_CFG_CLOCK_SOURCE macro."
 #endif }
Function name: clock_source_select
Description  : Enables and disables clocks as chosen by the user.
Function name : clock_source_select Description : Enables and disables clocks as chosen by the user .
/* Declared volatile for software delay purposes. */ /* The delay loops used below have been measured to take 86us per iteration. This has been verified using the 
       Renesas RX Toolchain with optimizations set to 2, size priority. The same result was obtained using 2, speed 
       priority. At this time the MCU is still running on the 125kHz LOCO. */ /* HOCO is chosen. Start it operating. */ /* The delay period needed is to make sure that the HOCO has stabilized. According to Rev.1.00 of the RX63N's HW 
       manual the delay period is tHOCOWT2 which is 2ms. A delay of 2.4ms has been used below to account for variations 
       in the LOCO. 
       2.4ms / 86us (per iteration) = 28 iterations */ /* Wait 2.4ms. See comment above for why. */ /* HOCO is not chosen. */ /* Stop the HOCO. */ /* Turn off power to HOCO. */ /* Main clock oscillator is chosen. Start it operating. */ /* Wait 65,536 cycles * 12 MHz = 5.46 ms */ /* Set the main clock to operating. */ /* The delay period needed is to make sure that the main clock has stabilized. This delay period is tMAINOSCWT in 
       the HW manual and according to the Renesas Technical Update document TN-RX*-A021A/E this is defined as:
       n = Wait time selected by MOSCWTCR.MSTS[] bits
       tMAINOSC = Main clock oscillator start-up time. From referring to various vendors, a start-up time of 4ms appears
                  to be a common maximum. To be safe we will use 5ms.
       tMAINOSCWT = tMAINOSC + ((n+16384)/fMAIN)
       tMAINOSCWT = 5ms + ((65536 + 16384)/12MHz)
       tMAINOSCWT = 11.82ms
       A delay of 12ms has been used below to account for variations in the LOCO. 
       12ms / 86us (per iteration) = 140 iterations */ /* Wait 12ms. See comment above for why. */ /* Set the main clock to stopped. */ /* Sub-clock oscillator is chosen. Start it operating. */ /* Wait 65,536 cycles * 32768Hz = 2s. This meets the timing requirement tSUBOSC which is 2 seconds. */ /* Set the sub-clock to operating. */ /* The delay period needed is to make sure that the sub-clock has stabilized. According to Rev.1.00 of the RX63N's 
       HW manual the delay period is tSUBOSCWT0 which is at minimum 1.8s and at maximum 2.6 seconds. We will use the
       maximum value to be safe.       
       2.6s / 86us (per iteration) = 30233 iterations */ /* Wait 2.6s. See comment above for why. */ /* Set the sub-clock to stopped. */ /* PLL is chosen. Start it operating. Must start main clock as well since PLL uses it. */ /* Wait 65,536 cycles * 12 MHz = 5.46 ms */ /* Set the main clock to operating. */ /* PLL wait is 1,048,576 cycles * 192 MHz (12 MHz * 16) = 5.46 ms*/ /* Set PLL Input Divisor. */ /* Set PLL Multiplier. */ /* Set the PLL to operating. */ /* The delay period needed is to make sure that the main clock and PLL have stabilized. This delay period is 
       tPLLWT2 when the main clock has not been previously stabilized. According to the Renesas Technical Update 
       document TN-RX*-A021A/E this delay is defined as:
       n = Wait time selected by PLLWTCR.PSTS[] bits
       tMAINOSC = Main clock oscillator start-up time. From referring to various vendors, a start-up time of 4ms appears
                  to be a common maximum. To be safe we will use 5ms.
       tPLLWT2 = tMAINOSC + tPLL1 + ((n + 131072)/fPLL)
       tPLLWT2 = 5ms + 500us + ((1048576 + 131072)/192MHz)
       tPLLWT2 = 11.64ms
       A delay of 12ms has been used below to account for variations in the LOCO. 
       12ms / 86us (per iteration) = 140 iterations */ /* Wait 12ms. See comment above for why. */ /* Set the PLL to stopped. */ /* LOCO is saved for last since it is what is running by default out of reset. This means you do not want to turn
       it off until another clock has been enabled and is ready to use. */ /* LOCO is chosen. This is the default out of reset. */ /* LOCO is not chosen but it cannot be turned off yet since it is still being used. */ /* Make sure a valid clock was chosen. */