/* 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. * * 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 #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { int port, n; struct termios tty_settings, orig_settings; unsigned char buf[256]; unsigned char baud_change_cmd[2] = {0x6A, 0xB4}; /* first, open the serial port for writing */ port = open(PORT, O_WRONLY | O_NOCTTY); if (port < 0) { fprintf(stderr, "Unable to open \"%s\"\n", PORT); 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)); 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"); 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; }