📄 xmit115200.c
字号:
/* Transmit a fast binary download to the mp3 player at 115200 baud. * This program takes care of switching the baud rate details: * 1: Command the board to begin a fast flash download * 2: Command the board to switch to 115200 * 3: Switch the PC to 115200 * 4: Transmit the data at 115200 * 5: Switch back to original baud rate at end of transfer. * It is assumed that the user is running a terminal emulator program * while this download is in progress. The terminal emulator program * is responsible for establishing the baud rate to 19200 before this * download utility is run. * * The serial port to download to defaults to /dev/ttyS0, the first * serial port. The port can be changed on the command line ie. * xmit115200 /dev/ttyS2 or as an environment variable PJRCMP3_PORT. * * This code uses the unix/posix termios interface, and other unix * APIs, and it also depends on the ability to open the serial port * for writing while it's already in use by the terminal emulation * program. Works great on Linux (tested on RedHat 7.2), but it's * very unlikely to ever be ported to Microsoft Windows. * * Compile with: * gcc -O2 -Wall -o xmit115200 xmit115200.c * * Typical usage: * ./hex2fdl mp3player.hex | ./xmit115200 */#define PORT "/dev/ttyS0"#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/ioctl.h>#include <termios.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <signal.h>void ctrl_c_handler(int num);int port;struct termios orig_settings;int main(int argc, char **argv){ int n; struct termios tty_settings; unsigned char buf[256]; unsigned char baud_change_cmd[2] = {0x6A, 0xB4}; char* portstr = PORT; if (argc == 2) { portstr = argv[1]; } else if (getenv("PJRCMP3_PORT") != 0) { portstr = strdup(getenv("PJRCMP3_PORT")); } /* first, open the serial port for writing */ port = open(portstr, O_WRONLY | O_NOCTTY); if (port < 0) { fprintf(stderr, "Unable to open \"%s\"\n", portstr); exit(1); } /* send the command to start the fast flash download */ write(port, "~", 1); usleep(70000); /* send the command to change the baud rate... */ /* we'll assume there's a terminal emulator running with */ /* this port open and the baud rate configured nicely */ write(port, baud_change_cmd, 2); usleep(20000); /* now switch to 115200 baud */ tcgetattr(port, &tty_settings); memcpy(&orig_settings, &tty_settings, sizeof(struct termios)); signal(SIGINT, ctrl_c_handler); cfsetospeed(&tty_settings, B115200); tcsetattr(port, 0, &tty_settings); /* copy all data from stdin to the serial port, at 115200 :) */ while (1) { n = read(fileno(stdin), buf, sizeof(buf)); if (n == 0) break; if (n < 0) { if (errno == EINTR) continue; fprintf(stderr, "error reading input\n"); tcflush(port, TCIOFLUSH); tcsetattr(port, 0, &orig_settings); write(port, "\033", 1); exit(1); } write(port, buf, n); } /* wait for all this data we're transmitted to actually */ /* leave the port and get into the mp3 player */ tcdrain(port); usleep(20000); /* now restore the baud rate, hopefully before the board */ /* begins printing the download summary! */ tcsetattr(port, 0, &orig_settings); return 0;}void ctrl_c_handler(int num){ tcflush(port, TCIOFLUSH); tcsetattr(port, 0, &orig_settings); usleep(10000); write(port, "\033", 1); printf("xmit115200: abort on ctrl-c, baud rate restored\n"); exit(1);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -