Add a simple program to control the pipeline by using the iCEstick UART

parent f298b2b7
IDIR =./
CFLAGS=-Wall -I$(IDIR)
LDFLAGS=
LIBS=
SOURCES=micropipeline-control.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=micropipeline-control
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(CFLAGS) $(LIBS)
.c.o:
$(CC) -c $(CFLAGS) $< -o $@
clean:
rm -f $(EXECUTABLE) *.o
.PHONY: clean
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
int configure_uart(int fd, int baudrate)
{
struct termios tty;
if (tcgetattr(fd, &tty) < 0) {
printf("Error from tcgetattr: %s\n", strerror(errno));
return -1;
}
cfsetospeed(&tty, (speed_t)baudrate);
cfsetispeed(&tty, (speed_t)baudrate);
tty.c_cflag |= (CLOCAL | CREAD);
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~PARENB;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
tty.c_oflag &= ~OPOST;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 1;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error from tcsetattr: %s\n", strerror(errno));
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
int fd;
int wlen;
char value;
if(argc != 3) {
fprintf(stderr, "\nUsage:\t%s portname data\n"
"\tportname : serial port device\n"
"\tdata : data to be written\n\n",
argv[0]);
exit(1);
}
char *portname = argv[1];
value = (char)strtoul(argv[2],0,0);
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0) {
printf("Error opening %s: %s\n", portname, strerror(errno));
return -1;
}
configure_uart(fd, B115200);
char command_write[1] = {value};
wlen = write(fd, command_write, 1);
if (wlen != 1) {
printf("Error from write: %d, %d\n", wlen, errno);
}
tcdrain(fd);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment