📄 tinitalk.c
字号:
TiniWriteAndWait ("\r", 1, '>'); printf ("\n[Load succesfull]\n"); fclose (hexFile); return 1;}intSaveFile (char *path){ printf ("Saving file: %s\n", path); return 1;}/* this is the io part */#if defined(_MSC_VER) || defined(__BORLANDC__)HANDLE tiniHandle;DCB tiniDcb;#elsestatic int tini_fd;static int tini_status;static struct termios tini_options;#endifstatic int initflag = 0;intTiniOpen (char *port, int baud){ if (initflag) { return 1; } printf ("[Opening \"%s\" at \"%d\" baud, escape is 0x%02x]\n", port, baud, escapeChar);#if defined(_MSC_VER) || defined(__BORLANDC__) if ((tiniHandle = CreateFile (port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)#else if ((tini_fd = open (port, O_RDWR | O_NOCTTY | O_NONBLOCK)) < 0)#endif { fprintf (stderr, "%s: unable to open port %s - ", "TiniOpen", port); perror(""); return 0; } // configure the serial port#if defined(_MSC_VER) || defined(__BORLANDC__) tiniDcb.DCBlength = sizeof (DCB); if (GetCommState (tiniHandle, &tiniDcb) != TRUE) { fprintf (stderr, "%s: unable to query port %s\n", "TiniOpen", port); TiniClose (); return 0; } tiniDcb.StopBits = 0; tiniDcb.ByteSize = 8; tiniDcb.Parity = 0; tiniDcb.fDtrControl = DTR_CONTROL_DISABLE; tiniDcb.fRtsControl = RTS_CONTROL_DISABLE; tiniDcb.fOutxCtsFlow = FALSE; tiniDcb.fOutX = FALSE; tiniDcb.fInX = FALSE; if (SetCommState (tiniHandle, &tiniDcb) != TRUE) { fprintf (stderr, "%s: unable to configure port %s\n", "TiniOpen", port); TiniClose (); return 0; } // reset DTR EscapeCommFunction (tiniHandle, CLRDTR);#else if (ioctl (tini_fd, TIOCMGET, &tini_status)) { perror ("0: ioctl(tini_fd, TIOCMGET, &tini_status) - "); TiniClose (); return 0; } // reset DTR tini_status &= ~TIOCM_DTR; if (ioctl (tini_fd, TIOCMSET, &tini_status)) { perror ("2: ioctl(tini_fd, TIOCMSET, &tini_status) - "); TiniClose (); return 0; } tcgetattr (tini_fd, &tini_options); tini_options.c_cflag |= (CLOCAL | CREAD); tini_options.c_lflag &= ~(ISIG | ICANON | ECHO); tini_options.c_iflag |= (IGNCR); tini_options.c_cc[VMIN] = 0; tini_options.c_cc[VTIME] = 0; tcsetattr (tini_fd, TCSANOW, &tini_options);#endif if (!TiniBaudRate (baud)) { TiniClose (); return 0; } initflag = 1; return 1;}#if defined(_MSC_VER) || defined(__BORLANDC__)#define B9600 CBR_9600#define B19200 CBR_19200#define B38400 CBR_38400#define B57600 CBR_57600#define B115200 CBR_115200#endifintTiniBaudRate (int baud){ int baudB; switch (baud) { case 9600: baudB = B9600; break; case 19200: baudB = B19200; break; case 38400: baudB = B38400; break; case 57600: baudB = B57600; break; case 115200: baudB = B115200; break; default: fprintf (stderr, "%s: illegal baudrate: \"%d\"\n", programName, baud); return 0; }#if defined(_MSC_VER) || defined(__BORLANDC__) tiniDcb.BaudRate = baudB; SetCommState (tiniHandle, &tiniDcb);#else cfsetispeed (&tini_options, baudB); cfsetospeed (&tini_options, baudB); tcsetattr (tini_fd, TCSANOW, &tini_options);#endif return 1;}intTiniReset (int toBootLoader){ // set DTR#if defined(_MSC_VER) || defined(__BORLANDC__) EscapeCommFunction (tiniHandle, SETDTR);#else tini_status |= TIOCM_DTR; if (ioctl (tini_fd, TIOCMSET, &tini_status)) { perror ("1: ioctl(tini_fd, TIOCMSET, &tini_status) - "); return 0; }#endif // wait for 100ms usleep (100000); // drain input and output buffers TiniDrain (); // reset DTR#if defined(_MSC_VER) || defined(__BORLANDC__) EscapeCommFunction (tiniHandle, CLRDTR);#else tini_status &= ~TIOCM_DTR; if (ioctl (tini_fd, TIOCMSET, &tini_status)) { perror ("2: ioctl(tini_fd, TIOCMSET, &tini_status) - "); return 0; }#endif // wait for 100ms usleep (100000); if (TiniWrite ("\r", 1) != 1) { fprintf (stderr, "TiniReset: couldn't write to tini\n"); return 0; } // wait for the bootloader prompt // we should build a timeout here TiniWait ('>'); if (toBootLoader) { return 1; } TiniWriteAndWait ("E\r", 2, 'E'); return 1;}#if defined(_MSC_VER) || defined(__BORLANDC__)// read as much character as available, at most nintTiniRead (char *buffer, int n){ int count; int status; COMSTAT tiniComStat; ClearCommError (tiniHandle, &status, &tiniComStat); if (tiniComStat.cbInQue < (unsigned int) n) { n = tiniComStat.cbInQue; } ReadFile (tiniHandle, buffer, n, &count, NULL); return count;}intTiniWrite (char *buffer, int n){ int count; WriteFile (tiniHandle, buffer, n, &count, NULL); return count;}#elseintTiniRead (char *buffer, int n){ return read (tini_fd, buffer, n);}intTiniWrite (char *buffer, int n){ return write (tini_fd, buffer, n);}#endif// wait for the prompCharintTiniWait (char promptChar){ char c; while (1) { switch (TiniRead (&c, 1)) { case 0: // no char available // give up our time slice sleep (0); break; case 1: // one char read putchar (c); fflush (stdout); if (c == promptChar) { return 1; } break; default: // some error perror ("TiniWait: "); return 0; break; } }}// send the buffer and wait for the promptCharintTiniWriteAndWait (char *buffer, int n, char promptChar){ char bytes = TiniWrite (buffer, n); TiniWait (promptChar); return bytes;}// flush input and output buffers (wait for it)voidTiniFlush (){#if defined(_MSC_VER) || defined(__BORLANDC__) FlushFileBuffers (tiniHandle);#else // flush the buffers, isn't there a simpler way? tcsetattr (tini_fd, TCSAFLUSH, &tini_options);#endif}// drain input and output buffers (forget it)voidTiniDrain (){#if defined(_MSC_VER) || defined(__BORLANDC__) PurgeComm (tiniHandle, PURGE_TXCLEAR | PURGE_RXCLEAR);#else // drain the buffers, isn't there a simpler way? tcsetattr (tini_fd, TCSADRAIN, &tini_options);#endif}#if defined(_MSC_VER) || defined(__BORLANDC__)voidTiniConnect (int baud){ char c; if (baud) { TiniBaudRate (baud); } while (1) { if (TiniRead (&c, 1)) { // char from TINI, high priority putchar (c); fflush (stdout); } else if (kbhit ()) { // char from console, low priotity if ((c = getch ()) == escapeChar) { // escape from connect? printf ("<ESC>"); break; } TiniWrite (&c, 1); } else { // nothing to do, so give up our timeslice sleep (0); } }}#elsevoidTiniConnect (int baud){ struct termios options, consoleOptions; int consoleFlags; char c; int fno; if (baud) { TiniBaudRate (baud); } // set stdin to nonblocking IO, noecho fno = fileno (stdin); consoleFlags = fcntl (fno, F_GETFL); fcntl (fno, F_SETFL, consoleFlags | O_NONBLOCK); tcgetattr (fno, &consoleOptions); options = consoleOptions; options.c_lflag &= ~(ISIG | ICANON | ECHO); tcsetattr (fno, TCSANOW, &options); while (1) { if (TiniRead (&c, 1) == 1) { // char from TINI, high priority putchar (c); } else if ((c = getchar ()) != EOF) { // char from console, low priority if (c == escapeChar) { // escape from connect? break; } if (c == '\n') { c = '\r'; } TiniWrite (&c, 1); } else { // nothing to do, so give up our timeslice sleep (0); } } // reset stdin fcntl (fno, F_SETFL, consoleFlags); tcsetattr (fno, TCSANOW, &consoleOptions);}#endifvoidTiniClose (void){ initflag = 0;#if defined(_MSC_VER) || defined(__BORLANDC__) CloseHandle (tiniHandle);#else close (tini_fd);#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -