Example #1
0
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{

    /* Init system, IP clock and multi-function I/O */
    SYS_Init();

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

    /* Init GPIO P2.0 (output) and P3.2 (EINT0) */
    GPIO_Init();

    /* Init SPI0 and LCD */
    LCD_Init();
    LCD_EnableBackLight();
    LCD_ClearScreen();

    LCD_Print(0, "Boot from LDROM");
    LCD_Print(1, "Press SW_INT   ");

    while (1)
    {
        if (g_u8IsPress)
        {
            g_u8IsPress = FALSE;
            
            /* Switch to boot from APROM */
            SYS_UnlockReg();
            FMC->ISPCON = FMC_ISPCON_BS_APROM;
            _SYS_RESET_CPU();
            while (1);
        }
        else
        {
            /* LED blanking for 1000ms */
            P2->DOUT ^= 1;
            SYS_SysTickDelay(300000);
            SYS_SysTickDelay(200000);
        }
    }
}
Example #2
0
/*---------------------------------------------------------------------------------------------------------*/
int32_t main(void)
{
    int32_t  i32Err = 0;
    uint32_t u32Data, u32ImageSize, i, j, *pu32Loader;

    /* Init system, IP clock and multi-function I/O */
    SYS_Init();

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

    /* Init GPIO P2.0 (output) and P3.2 (EINT0) */
    GPIO_Init();

    /* Init SPI0 and LCD */
    LCD_Init();
    LCD_EnableBackLight();
    LCD_ClearScreen();


    /* 
        This sample shows how to switch between APROM and LDROM. 
        Target: 
            Smpl_FMC
                Smpl_FMC is the code for APROM. It will program the firmware to LDROM and
                user can press SW_INT to change to boot from LDROM. 
                In APROM, the LED blanking interval is 200ms.
            Smpl_LDROM
                Smpl_LDROM is the code for LDROM. User can press SW_INT to change to boot from APROM.
                In LDROM, the LED blanking interval is 1000ms.
    */
    LCD_Print(0, "Boot from APROM");
    LCD_Print(1, "Press SW_INT   ");
    while (1)
    {
        if(g_u8IsPress)
        {
            g_u8IsPress = FALSE;

            /* Unlock protected registers to write ISP Control Register (ISPCON) */
            SYS_UnlockReg();

            /* Program sample LD code to LDROM */

            /* Enable ISP LDROM update function */
            FMC->ISPCON = FMC_ISPCON_LDUEN_Msk | FMC_ISPCON_ISPEN_Msk;

            /* Page Erase LDROM */
            for (i = 0;i < 4096;i += PAGE_SIZE)
                FMC_Erase(LDROM_BASE + i);


            /* Erase Verify */
            i32Err = 0;
            for (i = LDROM_BASE; i < (LDROM_BASE + 4096); i += 4)
            {
                u32Data = FMC_Read(i);

                if (u32Data != 0xFFFFFFFF)
                {
                    i32Err = 1;
                }
            }


            u32ImageSize = (uint32_t)&g_u32LoaderImageLimit - (uint32_t)&g_u32LoaderImageBase;

            pu32Loader = (uint32_t *)&g_u32LoaderImageBase;
            for (i = 0;i < u32ImageSize;i += PAGE_SIZE)
            {
                FMC_Erase(LDROM_BASE + i);
                for (j = 0;j < PAGE_SIZE;j += 4)
                {
                    FMC_Write(LDROM_BASE + i + j, pu32Loader[(i + j) / 4]);
                }
            }

            /* Verify loader */
            i32Err = 0;
            for (i = 0;i < u32ImageSize;i += PAGE_SIZE)
            {
                for (j = 0;j < PAGE_SIZE;j += 4)
                {
                    u32Data = FMC_Read(LDROM_BASE + i + j);
                    if (u32Data != pu32Loader[(i+j)/4])
                        i32Err = 1;

                    if (i + j >= u32ImageSize)
                        break;
                }


            }

            if(i32Err)
            {
                LCD_ClearScreen();
                LCD_Print(0,"LDROM write fail");
            }
            else
            {
                /* Switch to boot from LDROM */
                FMC->ISPCON = FMC_ISPCON_BS_LDROM;
                _SYS_RESET_CPU();
            }

            while (1);
        }
        else
        {
            /* LED blanking for 200ms */
            P2->DOUT ^= 1;
            SYS_SysTickDelay(100000);
        }

        

    }
}
Example #3
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();
}
Example #4
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();
    }
}
Example #5
0
void maintask ( void )
{
    static unsigned char checknum = 0xff, checked = 0;
    ModuLenNumStruct *p_modu;
    p_modu = &ModuTemp;

    if ( AdcTimeTask >= AdcTimeTaskFlg )
        {
            ModuCheckFlg += 1;
            AdcTimeTask = 0;
            AdcTask();
#ifdef vhe01

            if ( P40 )
                {
                    P40 = 0;
                }

            else
                {
                    P40 = 1;
                }

#else

            if ( P15 )
                {
                    P15 = 0;
                }

            else
                {
                    P15 = 1;
                }

#endif
        }

    if ( Uart1TimeTask >= Uart1TimeTaskFlg )
        {
            Uart1TimeTask = 0;

            if ( indexmk > 0x0a )
                {
                    indexmk = 0x01;
                }

            Inquire ( indexmk );
            *p_modu = I2cSwitchModu ( indexmk );

            if ( UartRcvFrame.len == 0 && p_modu->Online == 1 ) //ʧ°Ü
                {
                    checknum = indexmk;
                    checked++;

                    if ( checked > 3 )
                        {
                            p_modu->Online = 0;
                            MainModu.Inserted &= ~ ( 0x01 << ( indexmk - 1 ) );
                            checked = 0;
                            checknum = 0xff;
                            indexmk++;
                        }
                }

            else
                {
                    if ( checknum == indexmk )
                        {
                            checked = 0;
                        }

                    indexmk++;
                }
        }

    if ( I2cTimeTask >= I2cTimeTaskFlg )
        {
            _SYS_RESET_CPU();
            I2cTimeTask = 0;
        }
}
Example #6
0
/*************************************************************
*函数名称:void I2cFramePro(I2CRcvFrame *frame)
*函数功能:接收帧处理
*参    数:数据帧
*返    回:null
*************************************************************/
void I2cFramePro ( void )
{
    unsigned char MODUDATACS = 0;
    ModuLenNumStruct *p_modu;
    unsigned char i = 0;
    p_modu = &ModuTemp;

    /*检查是否是升级命令*/

    if ( I2Cuse[0] == 0x55 && I2Cuse[2] == 0x04 && I2Cuse[5] == 0xaa )
        {
            I2Csend[0] = 0x55;
            I2Csend[1] = 0x00;
            I2Csend[2] = 0x06;
            I2Csend[3] = 0x40;
            I2Csend[4] = 0x13;
            I2Csend[5] = 0xaa;
            ClearDim ( I2Cuse, sizeof ( I2Cuse ) / sizeof ( char ) );
            return;
        }

    if ( I2Cuse[0] == 0x55 && I2Cuse[5] == 0xAA && I2Cuse[2] == 0x00 )
        {
            SYS_UnlockReg();
            FMC->ISPCON = FMC_ISPCON_BS_LDROM;
            _SYS_RESET_CPU();//从LD 启动
        }

    if ( I2Cuse[0] != FRAME_HEAD1 || I2Cuse[I2Cuse[2] - 1] != FRAME_REAR ) //针头和针尾
        {
            g_I2Cupdate = 0;
            return;
        }

    I2Cchkxor = 0;

    for ( i = 0; i < I2Cuse[2] - 2; i++ )			//cs 为针头到CS前的异或校验
        {
            I2Cchkxor ^= I2Cuse[i];
        }

    if ( I2Cchkxor != I2Cuse[I2Cuse[2] - 2] )
        {
            I2Cchkxor = 0;
            return;
        }

    I2Cchksum = 0;

    for ( i = 0; i < I2Cuse[2] - 2; i++ )			//cs 为针头到CS前的异或校验
        {
            I2Cchksum += I2Cuse[i];
        }

    if ( I2Cuse[2] != 0x05 )			//设置模块任务
        {
            I2Cuse[I2Cuse[2] - 2] = I2Cchksum;
            RS485_Send ( I2Cuse, I2Cuse[2] );
            DelayMs ( 10 );
            Inquire ( I2Cuse[1] );
            DelayMs ( 10 );
            ClearDim ( I2Cuse, ( sizeof ( I2Cuse ) / sizeof ( char ) ) );
        }

    else if ( I2Cuse[2]  == 5 ) //选择模块(0-10)
        {
            I2Cchoosed_M = I2Cuse[1];
            *p_modu = I2cSwitchModu ( I2Cchoosed_M );

            if ( I2Cchoosed_M != 0x00 )
                {
                    I2Csend[0] = 0xf5;
                    I2Csend[1] = I2Cchoosed_M;
                    I2Csend[2] = 6 + p_modu ->DataLen;
                    I2Csend[3] = p_modu->ModuType;

                    for ( i = 0; i < p_modu->DataLen; i++ )
                        {
                            I2Csend[4 + i] = p_modu->ModuData[i];
                        }

                    I2Csend[4 + p_modu->DataLen] = 0;

                    for ( i = 0; i < 4 + p_modu->DataLen; i++ )
                        {
                            MODUDATACS ^= I2Csend[i];
                        }

                    I2Csend[4 + p_modu->DataLen] = MODUDATACS;
                    I2Csend[5 + p_modu->DataLen] = 0XAA;
                }

            else
                {
                    I2Csend[0] = 0xf5; //主模块
                    I2Csend[1] = 0x00;
                    I2Csend[2] = 11;
                    MainModu.POWERA = ADC_CH ( FourDataAdc ( ADCtempA ) );
                    MainModu.POWERB = ADC_CH ( FourDataAdc ( ADCtempB ) );
                    I2Csend[5] = MainModu.POWERA >> 8;
                    I2Csend[6] = MainModu.POWERA;
                    I2Csend[3] = MainModu.POWERB >> 8;
                    I2Csend[4] = MainModu.POWERB;
                    I2Csend[7] = MainModu.Inserted >> 8;
                    I2Csend[8] = MainModu.Inserted;
                    I2Csend[9] = 0;

                    for ( i = 0; i < 9; i++ )
                        {
                            I2Csend[9] ^= I2Csend[i];
                        }

                    I2Csend[10] = 0xAA;
                }

            ClearDim ( I2Cuse, ( sizeof ( I2Cuse ) / sizeof ( char ) ) );
        }