mirror of
https://github.com/UzixLS/picocom.git
synced 2025-07-19 07:21:18 +03:00
Merge pull request #62 from JoeMerten/osx-custom-baudrate
OSX custom baudrates: fixed and refactored
This commit is contained in:
5
Makefile
5
Makefile
@ -44,7 +44,7 @@ linenoise-1.0/linenoise.o : linenoise-1.0/linenoise.c linenoise-1.0/linenoise.h
|
||||
#CPPFLAGS += -DNO_HELP
|
||||
|
||||
|
||||
OBJS += picocom.o term.o fdio.o split.o termios2.o
|
||||
OBJS += picocom.o term.o fdio.o split.o termios2.o custbaud_osx.o
|
||||
picocom : $(OBJS)
|
||||
$(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)
|
||||
|
||||
@ -53,6 +53,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
|
||||
|
||||
.c.o :
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -o $@ -c $<
|
||||
@ -80,7 +81,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
|
||||
rm -f termios2.o custbaud_osx.o
|
||||
rm -f *~
|
||||
rm -f \#*\#
|
||||
|
||||
|
137
custbaud_osx.c
Normal file
137
custbaud_osx.c
Normal file
@ -0,0 +1,137 @@
|
||||
/*
|
||||
* custbaud_osx.c
|
||||
*
|
||||
* Custom baud rate support for OSX.
|
||||
*
|
||||
* by Joe Merten (https://github.com/Joe-Merten www.jme.de)
|
||||
*
|
||||
* ATTENTION: OSX 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
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA
|
||||
*/
|
||||
|
||||
/***************************************************************************/
|
||||
/* Known issues:
|
||||
* - FT232H 12MBaud not working
|
||||
* - using OSX El Capitan 10.11.6, FTDIUSBSerialDriver_v2_3.dmg
|
||||
* - tried with 2 different chips (FT232H and FT2232H)
|
||||
* Testing with Ftdi FT232H, which is capable to use up to 12MBaud, only
|
||||
* line speed up to 3MBaud were accepted. For higher baudrates we earn
|
||||
* a failure in the ioctl(IOSSIOSPEED) call.
|
||||
* But as `python -m serial.tools.miniterm` shows the same behaviour, it
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#if defined(__APPLE__) && defined(USE_CUSTOM_BAUD)
|
||||
|
||||
#include "custbaud_osx.h"
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <IOKit/serial/ioss.h>
|
||||
#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,
|
||||
* like B115200=115200. There is no need for any integer <-> code translation.
|
||||
* So we can pass any baudrate we want directly cfsetospeed() & co.
|
||||
*/
|
||||
int cfsetospeed_custom(struct termios *tiop, int speed) {
|
||||
return cfsetospeed(tiop, speed);
|
||||
}
|
||||
|
||||
int cfsetispeed_custom(struct termios *tiop, int speed) {
|
||||
return cfsetispeed(tiop, speed);
|
||||
}
|
||||
|
||||
int cfgetospeed_custom(struct termios *tiop) {
|
||||
return cfgetospeed(tiop);
|
||||
}
|
||||
|
||||
int cfgetispeed_custom(struct termios *tiop) {
|
||||
return cfgetispeed(tiop);
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
/* 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
|
||||
* 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)
|
||||
* - call tcsetattr() to apply all the rest termios data to the fd
|
||||
* - and then applying the real desired baudrate to the fd by calling ioctl(IOSSIOSPEED)
|
||||
* Note, that in case of failed ioctl(IOSSIOSPEED), our 9600 staying
|
||||
* configured at the fd.
|
||||
*/
|
||||
int tcsetattr_custom(int fd, int optional_actions, const struct termios *tiop) {
|
||||
int r;
|
||||
int workaround = 0;
|
||||
int baudrate;
|
||||
struct termios tios = *tiop;
|
||||
struct termios tio0;
|
||||
int baudrate0;
|
||||
|
||||
if ( fd >= 3 ) { /* don't apply this workaround for stdin/stdout/stderr */
|
||||
baudrate = cfgetospeed(&tios);
|
||||
if (baudrate > 460800 || !term_baud_std(baudrate)) {
|
||||
/* save fd's current termios to recover in case of later falure */
|
||||
r = tcgetattr(fd, &tio0);
|
||||
if ( r < 0 ) return -1;
|
||||
baudrate0 = cfgetospeed(&tio0);
|
||||
/* now temporarily switching baudrate back to 9600 */
|
||||
r = cfsetspeed(&tios, B9600);
|
||||
if ( r < 0 ) return -1;
|
||||
workaround = 1;
|
||||
}
|
||||
}
|
||||
|
||||
r = tcsetattr(fd, optional_actions, &tios);
|
||||
if ( r < 0 ) return -1;
|
||||
|
||||
if ( workaround ) {
|
||||
r = ioctl(fd, IOSSIOSPEED, &baudrate);
|
||||
/*if ( r < 0 ) fprintf(stderr, "%s: ioctl(%d, %d) = %d, optional_actions = %d, %s\r\n", __FUNCTION__, fd, baudrate, r, optional_actions, strerror(errno));*/
|
||||
if ( r < 0 ) {
|
||||
/* ioctl() failed, so we try to restore the fd to the old termios data */
|
||||
r = cfsetspeed(&tio0, B9600);
|
||||
/*if ( r < 0 ) fprintf(stderr, "%s: cfsetspeed() = %d, %s\r\n", __FUNCTION__, r, strerror(errno));*/
|
||||
if ( r < 0 ) return -1;
|
||||
r = tcsetattr(fd, optional_actions, &tio0);
|
||||
/*if ( r < 0 ) fprintf(stderr, "%s: tcsetattr() = %d, %s\r\n", __FUNCTION__, r, strerror(errno));*/
|
||||
if ( r < 0 ) return -1;
|
||||
r = ioctl(fd, IOSSIOSPEED, &baudrate0);
|
||||
/*if ( r < 0 ) fprintf(stderr, "%s: ioctl(%d) = %d, %s\r\n", __FUNCTION__, baudrate0, r, strerror(errno));*/
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#endif /* __APPLE__ && USE_CUSTOM_BAUD */
|
96
custbaud_osx.h
Normal file
96
custbaud_osx.h
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* custbaud_osx.h
|
||||
*
|
||||
* Custom baud rate support for OSX.
|
||||
*
|
||||
* by Joe Merten (https://github.com/Joe-Merten www.jme.de)
|
||||
*
|
||||
* ATTENTION: OSX 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
|
||||
* published by the Free Software Foundation; either version 2 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
||||
* USA
|
||||
*/
|
||||
|
||||
#ifndef CUSTBAUD_OSX_H
|
||||
#define CUSTBAUD_OSX_H
|
||||
|
||||
#include <termios.h>
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
/* OSX 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().
|
||||
*/
|
||||
|
||||
#if defined(HIGH_BAUD)
|
||||
|
||||
#ifndef B460800
|
||||
#define B460800 460800
|
||||
#endif
|
||||
#ifndef B500000
|
||||
#define B500000 500000
|
||||
#endif
|
||||
#ifndef B576000
|
||||
#define B576000 576000
|
||||
#endif
|
||||
#ifndef B921600
|
||||
#define B921600 921600
|
||||
#endif
|
||||
#ifndef B1000000
|
||||
#define B1000000 1000000
|
||||
#endif
|
||||
#ifndef B1152000
|
||||
#define B1152000 1152000
|
||||
#endif
|
||||
#ifndef B1500000
|
||||
#define B1500000 1500000
|
||||
#endif
|
||||
#ifndef B2000000
|
||||
#define B2000000 2000000
|
||||
#endif
|
||||
#ifndef B2500000
|
||||
#define B2500000 2500000
|
||||
#endif
|
||||
#ifndef B3000000
|
||||
#define B3000000 3000000
|
||||
#endif
|
||||
#ifndef B3500000
|
||||
#define B3500000 3500000
|
||||
#endif
|
||||
#ifndef B4000000
|
||||
#define B4000000 4000000
|
||||
#endif
|
||||
|
||||
#endif /* HIGH_BAUD */
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
int cfsetospeed_custom(struct termios *tiop, int speed);
|
||||
int cfsetispeed_custom(struct termios *tiop, int speed);
|
||||
int cfgetospeed_custom(struct termios *tiop);
|
||||
int cfgetispeed_custom(struct termios *tiop);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
/* Replace tcsetattr function with our osx specific one */
|
||||
#define tcsetattr tcsetattr_custom
|
||||
int tcsetattr_custom(int fd, int optional_actions, const struct termios *tiop);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#endif /* CUSTBAUD_OSX_H */
|
65
term.c
65
term.c
@ -61,6 +61,11 @@
|
||||
#include "termios2.h"
|
||||
#endif
|
||||
|
||||
#if defined(__APPLE__) && defined(USE_CUSTOM_BAUD)
|
||||
/* only for OSX, Tiger and above */
|
||||
#include "custbaud_osx.h"
|
||||
#endif
|
||||
|
||||
#include "term.h"
|
||||
|
||||
/***************************************************************************/
|
||||
@ -170,68 +175,14 @@ term_perror (const char *prefix)
|
||||
#ifndef B460800
|
||||
#define B460800 460800
|
||||
#endif
|
||||
#ifndef B500000
|
||||
#define B500000 500000
|
||||
#endif
|
||||
#ifndef B576000
|
||||
#define B576000 576000
|
||||
#endif
|
||||
#ifndef B921600
|
||||
#define B921600 921600
|
||||
#endif
|
||||
#ifndef B1000000
|
||||
#define B1000000 1000000
|
||||
#endif
|
||||
#ifndef B1152000
|
||||
#define B1152000 1152000
|
||||
#endif
|
||||
#ifndef B1500000
|
||||
#define B1500000 1500000
|
||||
#endif
|
||||
#ifndef B2000000
|
||||
#define B2000000 2000000
|
||||
#endif
|
||||
#ifndef B2500000
|
||||
#define B2500000 2500000
|
||||
#endif
|
||||
#ifndef B3000000
|
||||
#define B3000000 3000000
|
||||
#endif
|
||||
#ifndef B3500000
|
||||
#define B3500000 3500000
|
||||
#endif
|
||||
#ifndef B4000000
|
||||
#define B4000000 4000000
|
||||
#endif
|
||||
|
||||
#endif /* __APPLE__ */
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
/* Custom baudrates support for OSX */
|
||||
|
||||
#if defined(__APPLE__) && defined (USE_CUSTOM_BAUD)
|
||||
|
||||
int cfsetospeed_custom(struct termios *tiop, int speed) {
|
||||
return cfsetospeed(tiop, speed);
|
||||
}
|
||||
|
||||
int cfsetispeed_custom(struct termios *tiop, int speed) {
|
||||
return cfsetispeed(tiop, speed);
|
||||
}
|
||||
|
||||
int cfgetospeed_custom(struct termios *tiop) {
|
||||
return cfgetospeed(tiop);
|
||||
}
|
||||
|
||||
int cfgetispeed_custom(struct termios *tiop) {
|
||||
return cfgetispeed(tiop);
|
||||
}
|
||||
|
||||
#endif /* __APPLE__ && USE_CUSTOM_BAUD */
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#define BNONE 0xFFFFFFFF
|
||||
|
||||
struct baud_codes {
|
||||
@ -374,6 +325,12 @@ term_baud_ok(int baud)
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
term_baud_std(int baud)
|
||||
{
|
||||
return (Bcode(baud) != BNONE) ? 1 : 0;
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
|
||||
static int
|
||||
|
7
term.h
7
term.h
@ -69,6 +69,7 @@
|
||||
* F term_baud_up - return next higher baudrate
|
||||
* F term_baud_down - return next lower baudrate
|
||||
* F term_baud_ok - check if baudrate is valid
|
||||
* F term_baud_std - check if baudrate is on of our listed standard baudrates
|
||||
* F term_strerror - return a string describing current error condition
|
||||
* F term_perror - print a string describing the current error condition
|
||||
* G term_errno - current error condition of the library
|
||||
@ -694,6 +695,12 @@ int term_baud_down (int baud);
|
||||
*/
|
||||
int term_baud_ok(int baud);
|
||||
|
||||
/* F term_baud_std
|
||||
*
|
||||
* Returns non-zero if "baud" is a standard baudrate, zero otherwise.
|
||||
*/
|
||||
int term_baud_std(int baud);
|
||||
|
||||
/***************************************************************************/
|
||||
|
||||
#endif /* of TERM_H */
|
||||
|
Reference in New Issue
Block a user