view target-utils/libcommon/cmd_baud_switch.c @ 465:003e48f8ebe1

rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel I generally don't use NULL and use plain 0 instead, based on a "NULL considered harmful" discussion on the classiccmp mailing list many aeons ago (I couldn't find it, and I reason that it must have been 2005 or earlier), but a recent complaint by a packager sent me searching, and I found this: https://ewontfix.com/11/ While I don't give a @#$% about "modern" systems and code-nazi tools, I realized that passing a plain 0 as a pointer sentinel in execl is wrong because it will break on systems where pointers are longer than the plain int type. Again, I don't give a @#$% about the abomination of x86_64 and the like, but if anyone ever manages to port my code to something like a PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0 as a function argument where a pointer is expected most definitely won't work: if the most natural stack slot and SP alignment unit is 16 bits, fitting an int, with longs and pointers taking up two such slots, then the call stack will be totally wrong with a plain 0 passed for a pointer. Casting the 0 to (char *) ought to be the most kosher solution for the most retro systems possible.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Feb 2019 00:00:19 +0000
parents 2b041d57de1f
children
line wrap: on
line source

/*
 * Baud rate switching command
 */

#include <stdlib.h>
#include "types.h"
#include "ns16550.h"

extern struct ns16550_regs *uart_base;
extern int serial_in_poll();

static const struct tab {
	int baud;
	int divisor;
} rate_table[] = {
	/*
	 * First support the rates and divisors implemented by the
	 * Calypso boot ROM.  Dividing 13 MHz by 7 gives an approximation
	 * of 115200 (x16); the divisors used by the boot ROM code for
	 * the slower baud rates are all 7x the usual PC value.
	 */
	{115200, 7},
	{57600, 7 * 2},
	{38400, 7 * 3},
	{28800, 7 * 4},
	{19200, 7 * 6},
	/*
	 * Going faster than ~115200 baud means using a divisor
	 * less than 7, resulting in a non-standard baud rate.
	 * The /1, /2 and /4 seem like reasonable choices.
	 */
	{812500, 1},
	{406250, 2},
	{203125, 4},
	/* that's all we really need to support */
	{0, 0}
};

/*
 * The following helper function actually switches the UART
 * baud rate divisor.  Call serial_flush() first.  It returns the
 * old divisor value.
 *
 * Note the u8 type for both the new and old divisor values.
 * All supported divisors are well below 255, so we don't bother
 * with the upper byte.
 */
static u8
actually_switch_baud(newdiv)
	u8 newdiv;
{
	volatile struct ns16550_regs *regs;
	u8 save_lcr, save_old_baud;

	regs = uart_base;
	save_lcr = regs->lcr;
	regs->lcr = save_lcr | NS16550_LCR_DLAB;
	save_old_baud = regs->datareg;
	regs->datareg = newdiv;
	regs->lcr = save_lcr;
	return(save_old_baud);
}

void
cmd_baud_switch(argbulk)
	char *argbulk;
{
	char *argv[2];
	int baudarg;
	const struct tab *tp;
	u8 save_old_baud;
	int c;

	if (parse_args(argbulk, 1, 1, argv, 0) < 0)
		return;
	baudarg = atoi(argv[0]);
	for (tp = rate_table; tp->baud; tp++)
		if (tp->baud == baudarg)
			break;
	if (!tp->baud) {
		printf("ERROR: invalid/unimplemented baud rate argument\n");
		return;
	}

	/* do it */
	serial_flush();
	save_old_baud = actually_switch_baud(tp->divisor);

	/*
	 * After getting the echo of this command at the old baud rate
	 * (see the serial flush call just before switching the divisor),
	 * the line will go silent from the user's perspective.
	 * The user should wait just a little bit, then send us a 0x55 ('U')
	 * at the new baud rate - we should be in the below loop waiting
	 * for this character by then.  Receiving that character
	 * correctly (0x55 was chosen for the bit pattern - unlikely to
	 * be received if the sender is sending at a wrong baud rate)
	 * will cause us to conclude this command and return a new '='
	 * prompt at the new baud rate.
	 *
	 * If we get something else, we assume that someone messed up,
	 * switch back to the old baud rate, scribble an error message
	 * and return.
	 */
	do
		c = serial_in_poll();
	while (c < 0);
	if (c != 0x55) {
	  actually_switch_baud(save_old_baud);
	  printf("ERROR: no \'U\' received, switched back to old baud rate\n");
	}
}