⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bl_main.c

📁 LM3S_BOOTLOADER程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    m ^= ((unsigned int)crcPoly << 8);

    return (unsigned short)(m >> 8);
}
//*****************************************************************************
//
//! This function performs the update on the selected port.
//!
//! This function is called directly by the boot loader or it is called as a
//! result of an update request from the application.
//!
//! This function is contained in <tt>bl_main.c</tt>.
//!
//! \return Never returns.
//
//*****************************************************************************
union {
    #if KEY_COUNT > 0
    struct {
        unsigned char m_tempbuf[256]; // Temp buffer for aesInit.
    } part2;
    #endif
} sharedbufs;

//#define rxBuffer   sharedbufs.part1.m_rxBuffer
//#define pageBuffer 
#if KEY_COUNT > 0
#define tempbuf    sharedbufs.part2.m_tempbuf
#endif

#define frameindex_t unsigned long

void Updater(void)
{
    //unsigned long ulSize, ulTemp, ulFlashSize;
    
    SysCtlPeriEnable(LED_PERIPH);                               //  使能LED所在的GPIO端口
    GPIOPinTypeOut(LED_PORT, LED_PIN);                          //  设置LED所在管脚为输出
    GPIOPinTypeOut(LED_PORT, WDT_PIN);                          //  设置LED所在管脚为输出
    ledOn();                                                   //  熄灭LED

    #if KEY_COUNT > 0
    unsigned char chainCipherBlock[16];		// Buffer for Cipher Block
    // Unchaining

    // Copy Initial Vector to start the cipher block unchaining
    {
        unsigned char *p = chainCipherBlock;
        unsigned char const *q = initialVector;
        unsigned char n = 16;

        do
        {
            *p++ = *q++;
        }
        while (--n);
    }

    aesInit( tempbuf ); // Init AES algorithm.
    #endif

    //
    // This ensures proper alignment of the global buffer so that the one byte
    // size parameter used by the packetized format is easily skipped for data
    // transfers.
    //
    g_pucDataBuffer = ((unsigned char *)g_pulDataBuffer);

    //
    // Insure that the COMMAND_SEND_DATA cannot be sent to erase the boot
    // loader before the application is erased.
    //

    //
    // Read any data from the serial port in use.
    //
    //busReplyByte(ERROR_OK);
   // while(1)
    {
     //   unsigned char c =busReceiveByte();
    //    ledOff();
    //    if(c ==0xaa)
    //    {
    //        busReplyByte(0xaa);
     //       break;
     //   }
    }
    for (;;)
    {
        frameindex_t frameSize;
        unsigned int crc;
        ledOn();

        // Get the frame size
        frameSize =	((frameindex_t)busReceiveByte() << 8) |	busReceiveByte();

        // Receive a frame of data from communication interface and calculate
        // its CRC
        {
            unsigned char *p = g_pucDataBuffer;
            frameindex_t n = frameSize;

            crc = 0;
            do
            {
                unsigned char ch;

                ch = busReceiveByte();

                *p++ = ch;
                crc = CRC(crc, ch);

                //__watchdog_reset();
            }
            while (--n);

            busBusy();
        }
        //busReplyByte(frameSize/256);
        //busReplyByte(frameSize%256);
        //busReplyByte(crc/256);
        //busReplyByte(crc%256);
        ledOff();

        // CRC is OK?
        if (crc == 0x0000)
        {

            // Decrypt 16 bytes, CRC-bytes are ignored.
            #if KEY_COUNT > 0
            {
                unsigned char *p = g_pucDataBuffer;

                frameSize -= 2;
                do
                {
                    //__watchdog_reset();
                    aesDecrypt(p, chainCipherBlock);
                    p += 16;
                }
                while (frameSize -= 16);
            }
            #endif // KEY_COUNT > 0
            
            unsigned char *p = g_pucDataBuffer;
            

            // Check that the signature is correct
            if ((*p++ == (unsigned char)(SIGNATURE >> 24)) &&
            (*p++ == (unsigned char)(SIGNATURE >> 16)) &&
            (*p++ == (unsigned char)(SIGNATURE >>  8)) &&
            (*p++ == (unsigned char)(SIGNATURE >>  0)))
            {
                unsigned long address;
                unsigned char bits;
                frameindex_t size;
                unsigned char type;

                // Continue parsing the frames until the 'End Of Frame' is
                // received
                while ((type = *p++) != TYPE_EOF)
                {
                    //__watchdog_reset();

                    // Receive Lock Bits (used in TYPE_LOCKBITS)
                    bits = *p++;

                    // Destination Address (note: 'bits' and 'address' overlap)
                    address =((unsigned long)bits << 16);
                    address = (address | ((unsigned long)*p++ << 8));
                    address =(address | *p++);

                    size = ((unsigned int)*p++ << 8);
                    size |= *p++;
                    p++;
                    p++;

                    switch (type)
                    {
                        // Erase page
                        case TYPE_ERASE:
                        ledToggle();
                        //
                        // Clear the flash access interrupt.
                        //
                        HWREG(FLASH_FCMISC) = FLASH_FCMISC_AMISC;
                        //
                        // Erase this block.
                        //
                        HWREG(FLASH_FMA) = address;
                        HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_ERASE;

                        //
                        // Wait until this block has been erased.
                        //
                        while(HWREG(FLASH_FMC) & FLASH_FMC_ERASE)
                        {
                        }
                        //ledToggle();
                        break;

                        //spmErasePage(address);
                        // Fall-through!

                        // Prepare for incoming data chunks by copying the page
                        // contents into the page buffer
                        case TYPE_PREPARE:
                        {
                            //unsigned char *q = pageBuffer;
                            //unsigned char APPFLASH *r = address;

                           // do
                            {
                           //     *q++ = *r++;
                            }
                           // while (--size);
                            break;
                        }

                        // Chunk of data is written into the page buffer
                        case TYPE_DATA://program
                        {
                            unsigned long *q = (unsigned long *)p;
                            //unsigned char APPFLASH *r = address;
                            
                            HWREG(FLASH_FCMISC) = FLASH_FCMISC_AMISC;
                            //
                            // Erase this block.
                            //
                            HWREG(FLASH_FMA) = address;
                            HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_ERASE;
    
                            //
                            // Wait until this block has been erased.
                            //
                            while(HWREG(FLASH_FMC) & FLASH_FMC_ERASE)
                            {
                            }
                            size/=4;

                            do
                            {
                                //__watchdog_reset();

                                HWREG(FLASH_FMA) = address;//Addr
                                HWREG(FLASH_FMD) = *q++; //data
                                HWREG(FLASH_FMC) = FLASH_FMC_WRKEY | FLASH_FMC_WRITE;

                                //
                                // Wait until the word has been programmed.
                                //
                                while(HWREG(FLASH_FMC) & FLASH_FMC_WRITE)
                                {
                                }
                                address+=4;
                                p+=4;
                            }
                            while (--size);

                            //spmErasePage(address);
                            //spmProgramPage(address);
                            break;
                        }
                        // Program page buffer into flash page
                        case TYPE_PROGRAM:  //not use
                        {
                            //unsigned char *r =
                            //&pageBuffer[(unsigned long)(address) & 0xffff];

                            //do
                            //{
                            //    *r++ = *p++;
                            //}
                            //while (--size);
                            //break;
                        }



                        // Write a chunk of data into EEPROM
                        case TYPE_EEPROM:
                        do
                        {
                            //__watchdog_reset();
                            //spmEEWriteByte(
                            //(unsigned long)address++ & 0xffff, *p++
                            //);
                        }
                        while (--size);
                        break;

                        // Write Lock Bits
                        case TYPE_LOCKBITS:
                        //spmWriteLockBits(bits);
                        break;

                        case TYPE_RESET:
                        {
                            busReplyByte(ERROR_OK);
                            busReplyByte(ERROR_OK);
                            busReplyByte(ERROR_OK);
                            busReceiveByte();
                            //Delay(5242880);
                            return ;
                            //break;
                            /*
                            busReplyByte(ERROR_OK);
                            busReplyByte(ERROR_OK);
                            busReplyByte(ERROR_OK);
                            busReplyByte(ERROR_OK);
                            Delay(5242880);

                            //
                            // Reset and disable the peripherals used by the boot loader.
                            //
                            
                            HWREG(SYSCTL_RCGC1) = 0;
                            HWREG(SYSCTL_RCGC2) = 0;
                            HWREG(SYSCTL_SRCR1) = (SYSCTL_SRCR1_I2C0 | SYSCTL_SRCR1_SSI0 |
                            SYSCTL_SRCR1_UART0);
                            HWREG(SYSCTL_SRCR2) = SYSCTL_SRCR2_GPIOA | SYSCTL_SRCR2_GPIOB;
                            HWREG(SYSCTL_SRCR1) = 0;
                            HWREG(SYSCTL_SRCR2) = 0;

                            //
                            // Branch to the specified address.  This should never return.
                            // If it does, very bad things will likely happen since it is
                            // likely that the copy of the boot loader in SRAM will have
                            // been overwritten.
                            //
                            ((void (*)(void))0x1000)();

                            //
                            // In case this ever does return and the boot loader is still
                            // intact, simply reset the device.
                            //
                            HWREG(NVIC_APINT) = (NVIC_APINT_VECTKEY |
                            NVIC_APINT_SYSRESETREQ);

                            //
                            // The microcontroller should have reset, so this should
                            // never be reached.  Just in case, loop forever.
                            //
                            while(1)
                            {
                            }
                            
                            for(;;);
                            */

                        }

                        // Nonsense frames, one byte long
                        default:
                        p -= 5;
                        break;
                    }
                }
            }

            busReplyByte(ERROR_OK);
        }
        else
        {
            busReplyByte(ERROR_CRC);
            //busReplyByte(ERROR_CRC);
        }
    }
    //busReplyByte(ERROR_OK);
    //Delay(5242880);

}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -