Interfacing with the driver
Once you start the driver, it exposes a path under /dev/ser* (the naming depends on which driver you start). You can use the devctl() command to set configurations on the char driver such as baud rate, parity, flow control, etc. Then, you can utilize the normal open(), read(), write(), and close() commands to communicate with the driver. You can find the full interface in sys/io-char.h and sys/dcmd_chr.h.
To configure the driver, you can either configure it when starting the
driver through the argument options or use the
termios
struct:
// Define termios struct
struct termios tios;
// Open the serial driver
int fd = open("/dev/serusb1", O_RDWR);
// Set control flags
tios.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
tios.c_cflag |= (CS8 | CREAD | CLOCAL);
// Set input flags - disable special handling
tios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP |
INLCR | IGNCR | ICRNL | IXON);
// Set output flags - disable post processing
tios.c_oflag &= ~OPOST;
// Set local flags - disable canonical mode, echo, signals
tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
// Set timeouts
tios.c_cc[VMIN] = 1;
tios.c_cc[VTIME] = 0;
// Set baud rate. Will default to 57600 if not set
cfsetispeed(&tios, baud_rate);
cfsetospeed(&tios, baud_rate);
// Apply the configurations
tcflush( fd, TCIFLUSH );
tcsetattr(fd, TCSANOW, &tios);
This is just an arbitrary example of setting the driver's configuration. Set the configurations based on your specific use case.
You can also utilize the devctl() command to set and get useful information about the driver. Here is an example on how to get the line status information of the device:
int status = 0;
devctl(fd, DCMD_CHR_LINESTATUS, &status, sizeof(status), NULL);
You can also set the path of logging directory and enable logging:
// Set logging directory
devctl(controller->fd, DCMD_CHR_SET_LOGGING_DIR, (void*)log_dir, strlen(log_dir) + 1, NULL);
These are just a few commands to configure the driver using devctl() command. You can find the full list of devctl() commands for character devices in the DCMD_CHR_* chapter of the Devctl and Ioctl Commands guide.
You can also utilize the read or write operations to read and write from or to the device:
// Write data to device
write(fd, data, length);
// Read data from device
read(fd, buf, length)