Ejemplo n.º 1
0
/*---------------------------------------------------------------------------------------------------------*/
void SYS_Example (void)
{
    uint32_t u32data;

    /* Lock protected registers */
    if(SYS->REGWRPROT == 1) // In end of main function, program issued CPU reset and write-protection will be disabled.
        SYS_LockReg();

    /* Init System, IP clock and multi-function I/O */
    SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register.

    /* Init UART0 for printf */
    UART0_Init();

    printf("\n\nCPU @ %dHz\n", SystemCoreClock);

    /*
        This sample code will show some function about system manager controller and clock controller:
        1. Read PDID
        2. Get and clear reset source
        3. Setting about BOD
        4. Change system clock depended on different PLL settings
        5. Outout system clock from CKO pin, and the output frequency = system clock / 4
    */

    printf("+---------------------------------------+\n");
    printf("|    NUC200 System Driver Sample Code   |\n");
    printf("+---------------------------------------+\n");

    if (M32(FLAG_ADDR) == SIGNATURE)
    {
        printf("  CPU Reset success!\n");
        M32(FLAG_ADDR) = 0;
        printf("  Press any key to continue ...\n");
        uart_getchar();
    }

    /*---------------------------------------------------------------------------------------------------------*/
    /* Misc system function test                                                                               */
    /*---------------------------------------------------------------------------------------------------------*/

    /* Read Part Device ID */
    printf("Product ID 0x%x\n", SYS->PDID);

    /* Get reset source from last operation */
    u32data = SYS->RSTSRC;
    printf("Reset Source 0x%x\n", u32data);

    /* Clear reset source */
    SYS->RSTSRC = u32data;

    /* Unlock protected registers for Brown-Out Detector settings */
    SYS_UnlockReg();

    /* Check if the write-protected registers are unlocked before BOD setting and CPU Reset */
    if (SYS->REGWRPROT != 0)
    {
        printf("Protected Address is Unlocked\n");
    }

    /* Enable Brown-Out Detector and Low Voltage Reset function, and set Brown-Out Detector voltage 2.7V */
    SYS->BODCR =SYS_BODCR_BOD_EN_Msk | SYS_BODCR_BOD_VL_2_7V | SYS_BODCR_LVR_EN_Msk;

    /* Enable Brown-Out Interrupt function */
    SYS->BODCR &= ~SYS_BODCR_BOD_RSTEN_Msk;
    NVIC_EnableIRQ(BOD_IRQn);

    /* Get system clock frequency and PLL clock frequency */
    printf("  Change system clock to %d Hz and PLL clock is %d Hz\n", SystemCoreClock, PllClock);

    /* Run PLL Test */
    SYS_PLL_Test();

    /* Write a signature work to SRAM to check if it is reset by software */
    M32(FLAG_ADDR) = SIGNATURE;
    printf("\n\n  >>> Reset CPU <<<\n");

    /* Waiting for message send out */
    _UART_WAIT_TX_EMPTY(UART0);

    /* Switch HCLK clock source to Internal 22MHz */
    SYSCLK->CLKSEL0 = SYSCLK_CLKSEL0_HCLK_IRC22M;

    /* Set PLL to Power down mode and HW will also clear PLL_STB bit in CLKSTATUS register */
    SYSCLK->PLLCON |= SYSCLK_PLLCON_PD_Msk;

    /* Reset CPU */
    _SYS_RESET_CPU();
}
Ejemplo n.º 2
0
void FMC_LDROM_Test(void)
{
    int32_t  i32Err;
    uint32_t u32Data, i, j, *pu32Loader;

    /* Enable LDROM Update */
    _FMC_ENABLE_LD_UPDATE();

    printf("  Erase LD ROM ............................... ");
    
    /* Page Erase LDROM */
    for (i = 0; i < FMC_LDROM_SIZE; i += FMC_FLASH_PAGE_SIZE)
        FMC_Erase(FMC_LDROM_BASE + i);
    
    /* Erase Verify */
    i32Err = 0;

    for (i = FMC_LDROM_BASE; i < (FMC_LDROM_BASE+FMC_LDROM_SIZE); i += 4) 
    {     
        u32Data = FMC_Read(i);
        
        if (u32Data != 0xFFFFFFFF)
        { 
            i32Err = 1;
        }
    }
    
    if (i32Err)
        printf("[FAIL]\n");
    else
        printf("[OK]\n");
    
    printf("  Program LD ROM test ........................ ");
    
    /* Program LDROM and read out data to compare it */
    for(i = FMC_LDROM_BASE; i < (FMC_LDROM_BASE+FMC_LDROM_SIZE); i += 4) 
    {
        FMC_Write(i, i);
    }

    i32Err = 0;
    
    for(i = FMC_LDROM_BASE; i < (FMC_LDROM_BASE+FMC_LDROM_SIZE); i += 4) 
    {
        u32Data = FMC_Read(i);
        
        if (u32Data != i)
        { 
           i32Err = 1;
        }        
    }
    
    if (i32Err)
        printf("[FAIL]\n");
    else
        printf("[OK]\n");

    printf("  Program Simple LD Code ..................... ");
    pu32Loader = (uint32_t *)&loaderImageBase;
    
    for (i=0;i<g_u32ImageSize;i+=FMC_FLASH_PAGE_SIZE)
    {
        FMC_Erase(FMC_LDROM_BASE + i);
        
        for (j=0;j<FMC_FLASH_PAGE_SIZE;j+=4)
        {
            FMC_Write(FMC_LDROM_BASE + i + j, pu32Loader[(i + j) / 4]);
        }
    }

    /* Verify loader */
    i32Err = 0;
    
    for (i=0;i<g_u32ImageSize;i+=FMC_FLASH_PAGE_SIZE)
    {
        for (j=0;j<FMC_FLASH_PAGE_SIZE;j+=4)
        {
            u32Data = FMC_Read(FMC_LDROM_BASE + i + j);
            if (u32Data != pu32Loader[(i+j)/4])
                i32Err = 1;
            
            if (i + j >= g_u32ImageSize)
                break;
        }
    }

    if (i32Err)
    {
        printf("[FAIL]\n");
    }
    else
    {
        printf("[OK]\n");
        
        /* Reset CPU to boot to LD mode */
        printf("\n  >>> Reset to LD mode <<<\n");
        
        /* Make sure message has printed out */
        _UART_WAIT_TX_EMPTY(UART0);

        FMC->ISPCON |= FMC_ISPCON_BS_LDROM;
        
        /* CPU Reset */
        _SYS_RESET_CPU();
    }
}