/*
 * Copyright(C) Paul und Scherer (mct.de/mct.net)
 *
 * This example demonstrates how to...
 *
 *  ... use auxin/auxout.
 */

#include <stdio.h>
#include <sys/io.h>

/*
 * Test COM1 in loopback mode, i.e.
 *
 *  TxD1 (=SIN1  =P4.7) connected to
 *  RxD1 (=SOUT1 =P4.6).
 *
 * COM0 (stdin/stdout/stderr) is already initialized when main
 * is called. COM1 (auxin/auxout) has to be initialized by the
 * user program before any I/O can be done via auxin/auxout.
 */
int
main(void)
{
	/*
	 * Initialize COM1.
	 *
	 * If the baudrate cannot be set within +/-3%,
	 * _sio_init() returns non-zero and sets errno
	 * appropriately.
	 */
	if (_sio_init(19200, 1)) return perror("_sio_init"), 1;

	while (1) {
		int sc, rc;			// send/receive char

		puts("Hit any key...");
		sc = getchar();
		fputc(sc, auxout);
		printf("\nSent 0x%02x, reading... ", sc);
		rc = fgetc(auxin);
		printf("0x%02x\n", rc);
		if (rc != sc) puts("Loopback FAILED!\7");
	}
}
