There can be few characters in the serial port buffer. Draining
them before attempting synchronization avoids synchronization
issues.
snprintf(freq, 8, "%d\r\n", crystal_freq);
+ /* Flush input (previously buffered garbage) */
+ isp_serial_flush_input();
+
/* Send synchronize request */
if (isp_serial_write(SYNCHRO_START, strlen(SYNCHRO_START)) != strlen(SYNCHRO_START)) {
printf("Unable to send synchronize request.\n");
return count;
}
+void isp_serial_flush_input()
+{
+ int nb;
+ char unused;
+ do {
+ nb = read(serial_fd, &unused, 1);
+ } while (nb > 0);
+ if (nb == 0) {
+ printf("serial_read: end of file !!!!\n");
+ } else if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
+ perror("Serial read error");
+ }
+}
+
static char next_read_char = 0;
void isp_serial_empty_buffer()
{
*/
int isp_serial_open(int baudrate, char* serial_device);
void isp_serial_close(void);
+void isp_serial_flush_input(void);
/* Simple write() wrapper, with trace if enabled */
int isp_serial_write(const char* buf, unsigned int buf_size);