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

📄 download.c

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 C
📖 第 1 页 / 共 2 页
字号:
//****************************************************************************//// DOWNLOAD.C - Automates the download of code into the NOR FLASH on the//              EP72XX and EP73XX boards.//// Copyright (c) 1999,2000,2001 Cirrus Logic, Inc.////****************************************************************************#include <stdio.h>#include "flasher.h"#ifdef _WIN32#include <windows.h>#endif#ifdef __linux__#include <sys/ioctl.h>#include <sys/timeb.h>#include <fcntl.h>#include <termios.h>#endif//****************************************************************************//// lSerialPort is the serial port which is being used.////****************************************************************************#ifdef _WIN32HANDLE hSerialPort;#endif#ifdef __linux__long lSerialPort;#endif//****************************************************************************//// OpenPort opens the specified serial port.////****************************************************************************intOpenPort(long lPort){#ifdef _WIN32    char pcName[16];    //    // Create the device name for the given serial port.    //    sprintf(pcName, "COM%d", lPort);    //    // Open the serial port.    //    hSerialPort = CreateFile(pcName, GENERIC_READ | GENERIC_WRITE, 0, 0,                             OPEN_EXISTING, 0, 0);    if(hSerialPort == INVALID_HANDLE_VALUE)    {        fprintf(stderr, "Could not open serial port %s.\n", pcName);        return(0);    }    //    // Success.    //    return(1);#elif defined(__linux__)    struct termios buf;    char pcName[16];    //    // Create the device name for the given serial port.    //    sprintf(pcName, "/dev/ttyS%d", lPort - 1);    //    // Open the serial port.    //    lSerialPort = open(pcName, O_RDWR | O_NONBLOCK);    if(!lSerialPort)    {        fprintf(stderr, "Could not open serial port %s.\n", pcName);        return(0);    }    //    // Get the attributes of the serial port.  This will fail if the serial    // port does not exist (even though the open will succeed if the serial    // port does not exist).    //    if(tcgetattr(lSerialPort, &buf))    {        fprintf(stderr, "Could not open serial port %s.\n", pcName);        return(0);    }    //    // Success.    //    return(1);#else    //    // There is no serial port support for the target environment, so return    // an error.    //    fprintf(stderr, "No serial port support!\n");    return(0);#endif}//****************************************************************************//// SetBaud sets the baud rate and data format of the serial port.////****************************************************************************voidSetBaud(long lRate){#ifdef _WIN32    DCB dcb;    //    // Purge any pending characters in the serial port.    //    PurgeComm(hSerialPort, (PURGE_TXABORT | PURGE_RXABORT |                            PURGE_TXCLEAR | PURGE_RXCLEAR));    //    // Fill in the device control block.    //    dcb.DCBlength = sizeof(DCB);    dcb.BaudRate = lRate;    dcb.fBinary = TRUE;    dcb.fParity = FALSE;    dcb.fOutxCtsFlow = FALSE;    dcb.fOutxDsrFlow = FALSE;    dcb.fDtrControl = DTR_CONTROL_DISABLE;    dcb.fDsrSensitivity = FALSE;    dcb.fTXContinueOnXoff = TRUE;    dcb.fOutX = FALSE;    dcb.fInX = FALSE;    dcb.fErrorChar = FALSE;    dcb.fNull = FALSE;    dcb.fRtsControl = RTS_CONTROL_DISABLE;    dcb.fAbortOnError = FALSE;    dcb.XonLim = 0;    dcb.XoffLim = 0;    dcb.ByteSize = 8;    dcb.Parity = NOPARITY;    dcb.StopBits = ONESTOPBIT;    dcb.XonChar = 17;    dcb.XoffChar = 19;    dcb.ErrorChar = 0;    dcb.EofChar = 0;    dcb.EvtChar = 0;    dcb.wReserved = 0;    //    // Set the new serial port configuration.    //    SetCommState(hSerialPort, &dcb);#endif#ifdef __linux__    struct termios buf;    int rate;    //    // Get the current settings for the serial port.    //    if(tcgetattr(lSerialPort, &buf))    {        return;    }    //    // Reset to the serial port to raw mode.    //    buf.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |                     INLCR | IGNCR | ICRNL | IXON);    buf.c_oflag &= ~OPOST;    buf.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);    buf.c_cflag &= ~(CSIZE | PARENB);    buf.c_cflag |= CS8;    //    // Get the baud rate.    //    switch(lRate)    {        case 9600:        {            rate = B9600;            break;        }        case 19200:        {            rate = B19200;            break;        }        case 38400:        {            rate = B38400;            break;        }        case 57600:        {            rate = B57600;            break;        }        case 115200:        {            rate = B115200;            break;        }    }    //    // Set the input and output baud rate of the serial port.    //    cfsetispeed(&buf, rate);    cfsetospeed(&buf, rate);    //    // Set data bits to 8.    //    buf.c_cflag &= ~CSIZE;    buf.c_cflag |= CS8;    //    // Set stop bits to one.    //    buf.c_cflag &= ~CSTOPB;    //    // Disable parity.    //    buf.c_cflag &= ~(PARENB | PARODD);    //    // Set the new settings for the serial port.    //    if(tcsetattr(lSerialPort, TCSADRAIN, &buf))    {	return;    }    //    // Wait until the output buffer is empty.    //    tcdrain(lSerialPort);#endif}//****************************************************************************//// SendChar sends a character to the serial port.////****************************************************************************voidSendChar(char cChar){#ifdef _WIN32    DWORD dwLen;    //    // Send this character to the serial port.    //    WriteFile(hSerialPort, &cChar, 1, &dwLen, NULL);#endif#ifdef __linux__    //    // Send this character to the serial port.    //    write(lSerialPort, &cChar, 1);#endif}//****************************************************************************//// ReceiveChar reads a character from the serial port.////****************************************************************************charReceiveChar(long lTimeout){#ifdef _WIN32    COMMTIMEOUTS sTimeouts;    char cChar;    DWORD dwLen;    //    // Fill in the timeout structure based on the timeout requested for this    // read.    //    sTimeouts.ReadIntervalTimeout = 0;    sTimeouts.ReadTotalTimeoutMultiplier = 0;    sTimeouts.ReadTotalTimeoutConstant = lTimeout;    sTimeouts.WriteTotalTimeoutMultiplier = 0;    sTimeouts.WriteTotalTimeoutConstant = 0;    //    // Set the timeout for this read.    //    SetCommTimeouts(hSerialPort, &sTimeouts);    //    // Read a character.    //    if(!ReadFile(hSerialPort, &cChar, 1, &dwLen, NULL))    {        //        // The read failed, so set the read character to a NULL.        //        cChar = 0;    }    //    // If we did not read a character, then set the character to NULL.    //    if(dwLen != 1)    {        cChar = 0;    }    //    // Return the character we read.    //    return(cChar);#endif#ifdef __linux__    struct timeb sTime;    char cChar;    int res;    time_t tStart, tNow;    //    // Get the current time.    //    ftime(&sTime);    tStart = (sTime.time * 1000) + sTime.millitm;    //    // Read the next character from the serial port.    //    while(1)    {        //        // Try to read a character from the serial port.        //        res = read(lSerialPort, &cChar, 1);        if(res == 1)        {            //            // We read a character, so break out of the loop.            //            break;        }        //        // Get the current time.        //        ftime(&sTime);        tNow = (sTime.time * 1000) + sTime.millitm;        //        // See if the timeout has expired.        //        if(lTimeout && ((tStart + lTimeout) < tNow))        {            cChar = 0;            break;        }    }    //    // Return the character read from the serial port.    //    return(cChar);#endif}//****************************************************************************//// WaitTillEmpty waits until the serial port's output buffer is empty.////****************************************************************************voidWaitTillEmpty(void){#ifdef _WIN32    //    // Wait for 10ms so the output buffer can drain.    //    Sleep(10);#endif#ifdef __linux__    //    // Wait until the output buffer is empty.    //    tcdrain(lSerialPort);#endif}//****************************************************************************//// WaitFor waits until a specific character is read from the serial port.////****************************************************************************voidWaitFor(char cWaitChar){    char cChar;    //    // Wait until we read a specific character from the serial port.    //    while(1)    {        //        // Read a character.        //        cChar = ReceiveChar(0);        //        // Stop waiting if we received the character.        //        if(cChar == cWaitChar)        {            break;        }    }}//****************************************************************************//// Prints out a usage message for the program.////****************************************************************************voidUsage(void){    fprintf(stderr, "Usage: download {-h} {-v} {-p<port>} {-b<baud>} "                    "<filename>\n\n");    fprintf(stderr, "Downloads a program image into the NOR FLASH of a Cirrus "                    "Logic EP72xx or EP73xx\n");    fprintf(stderr, "board.  The Intel B3 (sizes 512KB through 4MB), C3 "                    "(sizes 1MB through 8MB), and\n");    fprintf(stderr, "J3(Strata) (sizes 4MB through 16MB) FLASH devices are "                    "supported in either 16 or\n");    fprintf(stderr, "32 bit wide configurations.\n\n");    fprintf(stderr, "  -p <port>             Use the specified serial port "                    "(default is \"1\").  Valid\n");#ifdef _WIN32    fprintf(stderr, "                        values are 1 (COM1), 2 (COM2), 3 "                    "(COM3), and 4 (COM4).\n\n");#endif#ifdef __linux__    fprintf(stderr, "                        values are 1 (/dev/ttyS0), 2 "                    "(/dev/ttyS1), 3\n");    fprintf(stderr, "                        (/dev/ttyS2), and 4 "                    "(/dev/ttyS3).\n\n");#endif    fprintf(stderr, "  -b <baud>             Use the specified baud rate "                    "(default is \"115200\").\n");    fprintf(stderr, "                        Valid values are 9600, 19200, "                    "38400, 57600, and 115200.\n\n");    fprintf(stderr, "  -v                    Prints the version of this "                    "program.\n\n");    fprintf(stderr, "  -h                    Prints this usage message.\n");}//****************************************************************************//// This program waits for the '<' character from the boot ROM, sends the boot// code, waits for the '>' from the boot ROM, waits for the '?' from the boot// code, changes the serial port rate (preferably to 115200), downloads the// user data file, and then prints out progress status as the boot code writes// the user data file to the NOR FLASH.////****************************************************************************intmain(int argc, char *argv[]){    long lPort = 1, lRate = 115200, lFileSize, lIdx, lLoop, lSum;    char cChar, cFirstChar, cRateChar, cBuffer[1024];    int bError = 0;    FILE *pFile;    //    // First, set stdout to be unbuffered, so that our status messages are    // always displayed immediately.    //    setbuf(stdout, NULL);    //    // See if there are any flags specified on the command line.    //    while(1)    {        //        // If we are out of arguments, or this argument does not start with a        // '-', then stop looking at command line arguments.        //        if((argc == 1) || (argv[1][0] != '-'))        {            break;        }        //        // See if this argument is "-p".        //        if(argv[1][1] == 'p')        {            //            // Get the port number from the command line.            //            lPort = atoi(argv[1] + 2);            //            // Make sure that the specified port number is valid.            //            if((lPort != 1) && (lPort != 2) && (lPort != 3) && (lPort != 4))            {                //                // Tell the user that the port number is invalid.                //                fprintf(stderr, "Invalid serial port '%s'.\n\n", argv[1] + 2);                //                // Print the usage message.                //                Usage();                //                // We're done.                //

⌨️ 快捷键说明

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