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

📄 bl_main.c.bak

📁 LM3S_BOOTLOADER程序
💻 BAK
📖 第 1 页 / 共 2 页
字号:
    #else
    ulProcRatio = UART_BAUD_RATIO(UART_FIXED_BAUDRATE);
    #endif

    //
    // Set GPIO A0 and A1 as UART pins.
    //
    HWREG(GPIO_PORTA_BASE + GPIO_O_AFSEL) = UART_PINS;

    //
    // Set the pin type.
    //
    HWREG(GPIO_PORTA_BASE + GPIO_O_DEN) = UART_PINS;

    //
    // Set the baud rate.
    //
    HWREG(UART0_BASE + UART_O_IBRD) = ulProcRatio >> 6;
    HWREG(UART0_BASE + UART_O_FBRD) = ulProcRatio & UART_FBRD_DIVFRAC_M;

    //
    // Set data length, parity, and number of stop bits to 8-N-1.
    //
    HWREG(UART0_BASE + UART_O_LCRH) = UART_LCRH_WLEN_8 | UART_LCRH_FEN;

    //
    // Enable RX, TX, and the UART.
    //
    HWREG(UART0_BASE + UART_O_CTL) = (UART_CTL_UARTEN | UART_CTL_TXE |
    UART_CTL_RXE);

    #ifdef UART_AUTOBAUD
    //
    // Need to ack in the UART case to hold it up while we get things set up.
    //
    AckPacket();
    #endif
    #endif
}
unsigned short CRC(unsigned short oldCRC,unsigned char ch)
{
    static const unsigned short crcPoly = 0x8005;

    int n;
    unsigned long m;

    m = ((unsigned long)oldCRC << 8) | ch;
    for (n = 0; n < 8; n++)
    if ((m <<= 1) & 0x1000000)
    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;
    
    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();
                        address/=0x1000;
                       
                        // Erase this block.
                        EraseSector(address,address);

                        break;

                       // 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;
                            EraseSector(address/0x1000,address/0x1000);
                            
                            WriteFlash(address,q,size);
    
                            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();
                            
                            return ;
                        }

                        // 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 + -