📄 unformat.c
字号:
//****************************************************************************//// UNFORMAT.C - Erases the contents of the NAND FLASH on the EP7209 board.//// Copyright (c) 1999,2000,2001 Cirrus Logic, Inc.////****************************************************************************#include <stdio.h>#include "bootcode.h"#ifdef _WIN32#include <windows.h>#endif#ifdef __linux__#include <sys/ioctl.h>#include <sys/timeb.h>#include <fcntl.h>#include <termios.h>#endif//****************************************************************************//// Define a string which describes the board on which this unformat program// will work.////****************************************************************************#ifdef EP7209_Eval_Board#define BOARD "EP7209 Evaluation Board"#endif#ifdef EP7212_Reference_Board_Rev_A#define BOARD "EP7212 Reference Design, Rev. A"#endif#ifdef EP7212_Reference_Board_Rev_B#define BOARD "EP7212 Reference Design, Rev. B"#endif#ifdef EP7309_Reference_Board_Rev_B#define BOARD "EP7309 Reference Design, Rev. B"#endif#ifdef EP7212_Reference_Board_Rev_C#define BOARD "EP7212 Reference Design, Rev. C"#endif#ifdef EP7309_Reference_Board_Rev_C#define BOARD "EP7309 Reference Design, Rev. C"#endif#ifdef EP73XX_Eval_Board#define BOARD "EP73XX Evaluation Board"#endif#ifdef EP7312_Eval_Board#define BOARD "EP7312 Evaluation Board"#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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -