view loadtools/sercomm.c @ 38:5b3d7398d2d9

target-utils top level Makefile buglet
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Mon, 13 May 2013 20:40:44 +0000
parents fea204bc7674
children 54392d1ea474
line wrap: on
line source

/*
 * This module handles the establishment of serial communication
 * with the target, i.e., the host-side termios stuff.
 */

#include <sys/types.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

char *target_ttydev;
int target_fd;
struct termios target_termios;

open_target_serial()
{
	target_fd = open(target_ttydev, O_RDWR|O_NONBLOCK);
	if (target_fd < 0) {
		perror(target_ttydev);
		exit(1);
	}
	target_termios.c_iflag = IGNBRK;
	target_termios.c_oflag = 0;
	target_termios.c_cflag = CLOCAL|HUPCL|CREAD|CS8;
	target_termios.c_lflag = 0;
	target_termios.c_cc[VMIN] = 1;
	target_termios.c_cc[VTIME] = 0;
	/* start at B19200, as that's what we'll need to use initially */
	cfsetispeed(&target_termios, B19200);
	cfsetospeed(&target_termios, B19200);
	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
		perror("initial tcsetattr on target");
		exit(1);
	}
	return 0;
}

switch_baud_rate(code)
{
	cfsetispeed(&target_termios, code);
	cfsetospeed(&target_termios, code);
	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
		perror("tcsetattr to switch baud rate");
		exit(1);
	}
}