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

📄 dsploadder10046.cpp

📁 TDA10046驱动源代码.TDA10046是PHILIPS的一款DVB-T TUNER
💻 CPP
字号:
void CDownloadDlg::OnButtonStart() 
{
    CString cppStrFileName;

    // get the file name
    m_EditFileName.GetWindowText(cppStrFileName);

    //--------------
   	// open the file
    //--------------
    if (m_BinFile.Open(cppStrFileName, CFile::modeRead) == FALSE)
    {
        MessageBox("File can't be opened",NULL,MB_ICONSTOP|MB_OK);
        return;
    }

    //------------
    // file format
    //------------
    // Code length in word
    // Data length in word
    // Number in parentheses is the field size in byte
    //--------------------------------------------- - ------------------------- - ---------
    //|(3)Rom Only|(2)Code length|(1)0x41|(1)0x80| ... |(2)CRC|(2)Data length| ... |(2)CRC|
    //--------------------------------------------- - ------------------------- - ---------
    //<                         CODE AREA                    ><         DATA AREA         >

    //--------------------
    // get the code length
    //--------------------
    UInt32 uCodeSize = 0;
    UInt8 pbBuffer[3];

    // read the size of code to download
	if (m_ButtonRom.GetCheck())
	{
        // skip three bytes if rom file
		m_BinFile.Read(pbBuffer, 3);
		uCodeSize += 3; 
    }
    m_BinFile.Read(pbBuffer, 2);

    // convert size of word into bytes
    uCodeSize += (UInt32)((pbBuffer[0] << 8) | pbBuffer[1]) * 2;

    // add length of the code length field
    uCodeSize += 2;
    
    // add CRC length
    uCodeSize += 2;

    //----------------------
    // check the file format
    //----------------------
    // test the file - the bytes must be set to 0x4180      
	m_BinFile.Read(pbBuffer, 2);
	if ((pbBuffer[0] != 0x41) || (pbBuffer[1] != 0x80))
	{
       	MessageBox("The file format is not valid", "Bad bytes");
        m_BinFile.Close();
        return;
	}

    //--------------------
    // get the data length
    //--------------------
    UInt32 uDataSize = 0;

    // skip the code area from the beginning of the file
	m_BinFile.Seek(uCodeSize, CFile::begin);

    // read the size of data to download
    m_BinFile.Read(pbBuffer, 2);

    // convert size of word into bytes
    uDataSize = (UInt32)((pbBuffer[0] << 8) | pbBuffer[1]) * 2;

    // add length of the data length field
    uDataSize += 2;

    // add CRC length
    uDataSize += 2;

    //--------------------
    // check the file size
    //--------------------
    m_uTotalSize = uCodeSize + uDataSize;

    // up to 32Kbytes for the total size
    if (m_uTotalSize > 0x8000)
    {
       	MessageBox("The file format is not valid", "Bad length");
        m_BinFile.Close();
        return;
    }

    // set the file pointer to the beginning
    m_BinFile.SeekToBegin();

    //-----------------
    // start the thread
    //-----------------
    // test if the Ram is used or not
	if (m_ButtonRom.GetCheck())
        m_pThread = AfxBeginThread(DownloadRomFile, NULL);
    else
        m_pThread = AfxBeginThread(DownloadRamFile, NULL);

    //-----------------
    // initialize items
    //-----------------
    // buttons
    m_ButtonAbort.EnableWindow(TRUE);
    m_ButtonStart.EnableWindow(FALSE);

    // progress
    m_ProgressCtrl.SetRange32(0, m_uTotalSize);

    // start timer for refreshing the display during the download
    SetTimer(TIMER_ID, 100, NULL);
}

UINT CDownloadDlg::DownloadRomFile(LPVOID)
{
    //-----------------------
    // Initialize the loading
    //-----------------------
    // open the switch for the tuner
    FE_SetDeviceIO ((UInt8)m_iDemodId, (UInt32)tmhalFEI2cSwitch, 1);

    // reset the error counter
    m_uError = 0;

    // reset number of bytes downloaded
    m_uDownloadSize = 0;

    // downloading has started
    m_isDownloading = TRUE;

    //---------------------------------------------
    // Download by using directly the I2C functions
    //---------------------------------------------

    // frame format
    // Number in parentheses is the field size in byte
    //------------------------------ - ------------
    //|(1)Address|(2)Page|(1)byte0| ... |(1)byte63|
    //------------------------------ - ------------
    do
    {
        // start new frame of 64 bytes
        UInt32 uFrameByte = 0;

        // download header
		IIC_Start();
		if (IIC_WriteByte((UInt8)m_iEepromAdd) != 0)
            m_uError++;
        if (IIC_WriteByte((UInt8)(m_uDownloadSize >> 8)) != 0)
            m_uError++;
		if (IIC_WriteByte((UInt8)m_uDownloadSize) != 0)
            m_uError++;

        // download one page of 64 bytes
        UInt8 bByte;
        do
        {
            // read the file
            m_BinFile.Read(&bByte, 1);

            // send a byte
		    if (IIC_WriteByte(bByte) != 0)
                m_uError++;
            
            // next byte
            m_uDownloadSize ++;
            uFrameByte ++;

        } while((m_uDownloadSize <= m_uTotalSize) && (m_uError == 0) && (uFrameByte%64));

        // I2C stop
       	IIC_Stop();

        // wait to enable the EEPROM to complete the writing operation
        Sleep(20);

    }while((m_isDownloading == TRUE) && (m_uDownloadSize <= m_uTotalSize) && (m_uError == 0));

    //----------------------
    // Download is completed
    //----------------------
    // close the switch for the tuner
    FE_SetDeviceIO ((UInt8)m_iDemodId, (UInt32)tmhalFEI2cSwitch, 0);

    // download is finished
	m_isDownloading = FALSE;

    // load the content of the EEPROM in the chip's ram by setting BOOT_EEP
    FE_WriteDeviceBit ((UInt8)m_iDemodId, 0x07, 0x04, 0x04);

    return TRUE;
}

UINT CDownloadDlg::DownloadRamFile(LPVOID)
{
    //-----------------------
    // Initialize the loading
    //-----------------------
    // reset the chip - not needed if the DSP RAM is empty
    FE_WriteDeviceBit ((UInt8)m_iDemodId, 0x3b, 0x01, 0x00);

    // set the boot_host bit
    FE_WriteDeviceBit ((UInt8)m_iDemodId, 0x07, 0x08, 0x08);

    // reset the error counter
    m_uError = 0;

    // reset number of bytes downloaded
    m_uDownloadSize = 0;

    // downloading has started
    m_isDownloading = TRUE;

    //---------------------------------------------
    // Download by using directly the I2C functions
    //---------------------------------------------

    // frame format
    // Number in parentheses is the field size in byte
    //------------------------------- - ---------------
    //|(1)Address|(1)index|(1)byte0| ... |(1)byteXXXXX|
    //------------------------------- - ---------------
    // start
	IIC_Start();

    // send the I2C address
	if (IIC_WriteByte((UInt8)m_iDemodAdd) != 0)
        m_uError++;

    // send the I2C index for DSP_code_in
	if (IIC_WriteByte(0x58) != 0)
        m_uError++;

    // download data
    UInt8 bByte;
    do
    {
        // read one byte from the file
        m_BinFile.Read(&bByte, 1);

        // send one byte
		if (IIC_WriteByte(bByte) != 0)
            m_uError++;

        // next byte
        m_uDownloadSize ++;

    } while((m_isDownloading == TRUE) && (m_uDownloadSize <= m_uTotalSize) && (m_uError == 0));

    // stop
    IIC_Stop();

    //----------------------
    // Download is completed
    //----------------------
	m_isDownloading = FALSE;

    return TRUE;
}

⌨️ 快捷键说明

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