1
0
mirror of https://github.com/UzixLS/picocom.git synced 2025-07-19 07:21:18 +03:00

#77: added custom baudrate support for FreeBSD

This commit is contained in:
Joe Merten
2017-12-31 07:32:23 +01:00
parent 78abf75112
commit b78410aa04
4 changed files with 43 additions and 47 deletions

View File

@ -38,14 +38,14 @@ OBJS += linenoise-1.0/linenoise.o
linenoise-1.0/linenoise.o : linenoise-1.0/linenoise.c linenoise-1.0/linenoise.h
## Comment this in to enable custom baudrate support.
## Works with: Linux (kernels > 2.6), OSX (Tiger and above)
## Works with: Linux (kernels > 2.6), FreeBSD, macOS (Tiger and above)
#CPPFLAGS += -DUSE_CUSTOM_BAUD
## Comment this IN to remove help strings (saves ~ 4-6 Kb).
#CPPFLAGS += -DNO_HELP
OBJS += picocom.o term.o fdio.o split.o termios2.o custbaud_osx.o
OBJS += picocom.o term.o fdio.o split.o termios2.o custbaud_bsd.o
picocom : $(OBJS)
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
@ -54,7 +54,7 @@ term.o : term.c term.h
split.o : split.c split.h
fdio.o : fdio.c fdio.h
termios2.o : termios2.c termios2.h termbits2.h
custbaud_osx.o : custbaud_osx.c custbaud_osx.h
custbaud_bsd.o : custbaud_bsd.c custbaud_bsd.h
.c.o :
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
@ -86,7 +86,7 @@ picocom.1.pdf : picocom.1
clean:
rm -f picocom.o term.o fdio.o split.o
rm -f linenoise-1.0/linenoise.o
rm -f termios2.o custbaud_osx.o
rm -f termios2.o custbaud_bsd.o
rm -f *~
rm -f \#*\#

View File

@ -1,11 +1,11 @@
/*
* custbaud_osx.c
* custbaud_bsd.c
*
* Custom baud rate support for OSX.
* Custom baud rate support for BSD and macOS.
*
* by Joe Merten (https://github.com/Joe-Merten www.jme.de)
*
* ATTENTION: OSX specific stuff!
* ATTENTION: BSD and macOS specific stuff!
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -35,28 +35,28 @@
* looks that this is a bug or limitation in OSX and/or Ftdi driver.
* Trying with PL2303 (driver version PL2303_MacOSX_1.6.1_20160309.zip),
* baudrates up to 6MBaud were accepted.
* - Have not tested with more recent macOS or Ftdi driver until now.
*/
#if defined(__APPLE__) && defined(USE_CUSTOM_BAUD)
/* Note that this code might also work with OpenBSD, NetBSD et cetera, but I have not tested.
* That's why I only check for `defined (__FreeBSD__)` here.
*/
#if (defined (__FreeBSD__) || defined(__APPLE__)) && defined(USE_CUSTOM_BAUD)
#include "custbaud_osx.h"
#include "custbaud_bsd.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/ioctl.h>
#ifdef __APPLE__
#include <IOKit/serial/ioss.h>
#endif
#include "term.h"
/***************************************************************************/
/* Need to undef tcsetattr to get access to the original tcsetattr()
* function inside our module.
*/
#undef tcsetattr
/***************************************************************************/
/* As we can see in OSX termios.h all the baudrate constants are transparent,
/* As we can see in FreeBSD and macOS termios.h all the baudrate constants are transparent,
* like B115200=115200. There is no need for any integer <-> code translation.
* So we can pass any baudrate we want directly cfsetospeed() & co.
* So we can pass any baudrate we want directly to / from cfsetospeed() & co.
*/
int cfsetospeed_custom(struct termios *tiop, int speed) {
return cfsetospeed(tiop, speed);
@ -74,11 +74,18 @@ int cfgetispeed_custom(struct termios *tiop) {
return cfgetispeed(tiop);
}
#ifdef __APPLE__
/***************************************************************************/
/* Need to undef tcsetattr to get access to the original tcsetattr()
* function inside our module.
*/
#undef tcsetattr
/***************************************************************************/
/* The strategy of picocom's terminal handling library is to hold all the
* terminal settings (including baudrate) using termios struct.
* Problem on OSX is, that tcsetattr() will fail if termios contains an
* unusual baudrate (like e.g. 12345 of 12M), The official OSX way to apply
* Problem on macOS is, that tcsetattr() will fail if termios contains an
* unusual baudrate (like e.g. 12345 of 12M), The official macOS way to apply
* those baudrates is to use ioctl(IOSSIOSPEED) instead.
* Our workaround strategy is:
* - set the baudrate stored in termios back to a standard value (e.g. 9600)
@ -131,7 +138,8 @@ int tcsetattr_custom(int fd, int optional_actions, const struct termios *tiop) {
return 0;
}
#endif /*__APPLE__ */
/***************************************************************************/
#endif /* __APPLE__ && USE_CUSTOM_BAUD */
#endif /* FreeBSD || __APPLE__ && USE_CUSTOM_BAUD */

View File

@ -1,11 +1,11 @@
/*
* custbaud_osx.h
* custbaud_bsd.h
*
* Custom baud rate support for OSX.
* Custom baud rate support for BSD and macOS.
*
* by Joe Merten (https://github.com/Joe-Merten www.jme.de)
*
* ATTENTION: OSX specific stuff!
* ATTENTION: BSD and macOS specific stuff!
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
@ -23,18 +23,20 @@
* USA
*/
#ifndef CUSTBAUD_OSX_H
#define CUSTBAUD_OSX_H
#ifndef CUSTBAUD_BSD_H
#define CUSTBAUD_BSD_H
#include <termios.h>
/***************************************************************************/
/* OSX termios.h unfortunately just provides constants for baudrates
/* macOS termios.h unfortunately just provides constants for baudrates
* up to 230k, so we add the missing constants here. Regardless, that
* most of the high baudrates needs special handling (implementation in
* tcsetattr_custom()), we want to provide the values here to have them
* available for term_baud_up()/down().
*
* FreeBSD termios.h has 460k and 921k but misses e.g. 500k and >=1M.
*/
#if defined(HIGH_BAUD)
@ -87,10 +89,12 @@ int cfgetispeed_custom(struct termios *tiop);
/***************************************************************************/
/* Replace tcsetattr function with our osx specific one */
#ifdef __APPLE__
/* Replace tcsetattr function with our macOS specific one */
#define tcsetattr tcsetattr_custom
int tcsetattr_custom(int fd, int optional_actions, const struct termios *tiop);
#endif
/***************************************************************************/
#endif /* CUSTBAUD_OSX_H */
#endif /* CUSTBAUD_BSD_H */

22
term.c
View File

@ -61,9 +61,9 @@
#include "termios2.h"
#endif
#if defined(__APPLE__) && defined(USE_CUSTOM_BAUD)
/* only for OSX, Tiger and above */
#include "custbaud_osx.h"
#if (defined (__FreeBSD__) || defined(__APPLE__)) && defined(USE_CUSTOM_BAUD)
/* only for FreeBSD and macOS (Tiger and above) */
#include "custbaud_bsd.h"
#endif
/* Time to wait for UART to clear after a drain (in usec). */
@ -170,22 +170,6 @@ term_perror (const char *prefix)
/***************************************************************************/
/* OSX termios.h unfortunately just provides constants for baudrates
up to 230k, so we add the missing constants here. */
#if defined(__APPLE__) && defined(HIGH_BAUD)
#ifndef B460800
#define B460800 460800
#endif
#ifndef B921600
#define B921600 921600
#endif
#endif /* __APPLE__ */
/***************************************************************************/
#define BNONE 0xFFFFFFFF
struct baud_codes {