BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
rifastseriallib.c File Reference
#include "blitzlib.h"

Macros

#define SERDATR_HI   (*(volatile UBYTE *)(BL_CUSTOM + 0x018)) /* TBE = bit 5, RBF = bit 6 */
#define SERDATR_LO   (*(volatile UBYTE *)(BL_CUSTOM + 0x019)) /* the data byte */
#define SERDAT   BL_CUSTOMW(BL_CUSTOM, 0x030)
#define SERPER   BL_CUSTOMW(BL_CUSTOM, 0x032)
#define INTENA   BL_CUSTOMW(BL_CUSTOM, 0x09a)
#define INTREQ   BL_CUSTOMW(BL_CUSTOM, 0x09c)
#define INTENAR   BL_CUSTOMW(BL_CUSTOM, 0x01c)
#define TBE   0x20
#define RBF   0x40
#define INTF_RBF   0x0800
#define SERCLOCK   3546895UL
#define NOBYTES   0xFFFF
#define BAUD_OK(b)

Functions

void rifastseriallib_codeinit (void)
void rifastseriallib_codefinit (void)
void initserialio (void)
void finitserialio (void)
void setserialmode (void)
void endserialio (void)
void putserial (void)
void putserial2 (void)
void getserial (void)
void getserial2 (void)
void flushserialio (void)
void syncserial (void)
void doserialIO2 (void)
void doserialIO (void)
void setiobuf (void)
void setiospeed (void)
void setiosize (void)
void checkserial (void)
void syncput (void)
void syncget (void)
static void writebyte (UBYTE b)
static UBYTE readbyte (void)
static ULONG udiv (ULONG n, ULONG d)
static void setbaud (ULONG baud)
void c_codeinit (void)
 init: the state as at the load: slave, not initialised, no buffers.
void c_finitserialio (void)
 FinitSerialIO (and finit): end the serial I/O, switch TBE/RBF back on.
void c_doserialIO (UWORD n)
 DoSerialIO numbytes-1: exchange the buffers byte by byte.
void c_doserialIO2 (void)
 DoSerialIO: c_doserialIO() with the count of InitSerialIO/SetIOSize.
ULONG c_initserialio (ULONG baud, UWORD numbytes, UBYTE *inbuf, UBYTE *outbuf)
 InitSerialIO baud,numbytes,inbuf,outbuf: set up the serial port for the transfers.
void c_setserialmode (UBYTE mode)
 SetSerialMode mode: master (<> 0) or slave (0) for DoSerialIO.
void c_endserialio (void)
 EndSerialIO: "disable serial read/write" - does nothing (an RTS).
void c_putserial (UBYTE b)
 PutSerial byte: send one byte (through bytebuf).
void c_putserial2 (const UBYTE *p, ULONG n)
 PutSerial address,bytes: send a block of bytes.
ULONG c_checkserial (void)
 CheckSerial: the received byte, without waiting.
ULONG c_getserial (void)
 GetSerial: wait for a byte and return it (through bytebuf).
ULONG c_getserial2 (UBYTE *p, ULONG n)
 GetSerial address,bytes: receive a block of bytes.
void c_flushserialio (void)
 FlushSerialIO: "make sure serial port is empty" - acknowledge RBF in INTREQ.
void c_syncput (const UBYTE *p, ULONG n)
 SyncPut address,bytes: send bytes, each paced by the other side's answer.
void c_syncget (UBYTE *p, ULONG n)
 SyncGet address,bytes: receive bytes, answering each one.
BB_BOOL c_syncserial (UBYTE sync)
 SyncSerial syncbyte: synchronise with the other machine, deciding master and slave.
void c_setiospeed (UWORD baud)
 SetIOSpeed baudrate: a new baud rate (SERPER) if initialised.
void c_setiosize (UWORD n)
 SetIOSize bytes: the count of DoSerialIO without argument.
void c_setiobuf (UBYTE *in, UBYTE *out)
 SetIOBuffers inbuf,outbuf: the buffers of DoSerialIO.

Variables

struct { 
   UBYTE   mode 
   UBYTE   mode_lo 
   UWORD   numbytes 
   UBYTE *   inbuf 
   UBYTE *   outbuf 
   UWORD   baud 
   UWORD   inten 
   UBYTE   bytebuf [2] 
   UBYTE   ready 
} fs

Macro Definition Documentation

◆ BAUD_OK

#define BAUD_OK ( b)
Value:
((b) >= 109 && (b) <= SERCLOCK)
#define SERCLOCK
Definition rifastseriallib.c:219

◆ INTENA

#define INTENA   BL_CUSTOMW(BL_CUSTOM, 0x09a)

◆ INTENAR

#define INTENAR   BL_CUSTOMW(BL_CUSTOM, 0x01c)

◆ INTF_RBF

#define INTF_RBF   0x0800

◆ INTREQ

#define INTREQ   BL_CUSTOMW(BL_CUSTOM, 0x09c)

◆ NOBYTES

#define NOBYTES   0xFFFF

◆ RBF

#define RBF   0x40

◆ SERCLOCK

#define SERCLOCK   3546895UL

◆ SERDAT

#define SERDAT   BL_CUSTOMW(BL_CUSTOM, 0x030)

◆ SERDATR_HI

#define SERDATR_HI   (*(volatile UBYTE *)(BL_CUSTOM + 0x018)) /* TBE = bit 5, RBF = bit 6 */

◆ SERDATR_LO

#define SERDATR_LO   (*(volatile UBYTE *)(BL_CUSTOM + 0x019)) /* the data byte */

◆ SERPER

#define SERPER   BL_CUSTOMW(BL_CUSTOM, 0x032)

◆ TBE

#define TBE   0x20

Function Documentation

◆ c_checkserial()

ULONG c_checkserial ( void )

CheckSerial: the received byte, without waiting.

If RBF is set: the byte of SERDATR, RBF acknowledged in INTREQ. -1 if not initialised (fixed (#652)).

Returns
d0: the byte, -1 if there is none

◆ c_codeinit()

void c_codeinit ( void )

init: the state as at the load: slave, not initialised, no buffers.

Fixed (#1159): the original's init was empty, so a second run of a program with the library still loaded (from the editor) started with the mode, count, buffers and saved INTENAR of the run before (a program that never calls SetSerialMode was the master in its second run if the first ended as master).

◆ c_doserialIO()

void c_doserialIO ( UWORD n)

DoSerialIO numbytes-1: exchange the buffers byte by byte.

Master (SetSerialMode <> 0): writes a byte of outbuf, then reads one into inbuf; slave: the other way round. n + 1 bytes (DBRA). Nothing if not initialised (fixed (#652): the original ran with the buffers 0, reading into and sending from address 0).

Parameters
nd0: the number of bytes - 1

◆ c_doserialIO2()

void c_doserialIO2 ( void )

DoSerialIO: c_doserialIO() with the count of InitSerialIO/SetIOSize.

Nothing for a count of 0 (numbytes NOBYTES). Fixed (#1164): the original ran its DBRA loop over $FFFF, 65536 bytes into and out of buffers sized for none.

◆ c_endserialio()

void c_endserialio ( void )

EndSerialIO: "disable serial read/write" - does nothing (an RTS).

◆ c_finitserialio()

void c_finitserialio ( void )

FinitSerialIO (and finit): end the serial I/O, switch TBE/RBF back on.

Nothing if not initialised; else baud = 0 and the RBF (bit 11) and TBE (bit 0) interrupts that were on at InitSerialIO (the saved inten) are switched on again in INTENA. Fixed (#651, #648): the original saved the address of INTENAR ($F01C, bits 11 and 0 clear), so it never wrote, and its write went to the absolute address $9A instead of INTENA.

◆ c_flushserialio()

void c_flushserialio ( void )

FlushSerialIO: "make sure serial port is empty" - acknowledge RBF in INTREQ.

◆ c_getserial()

ULONG c_getserial ( void )

GetSerial: wait for a byte and return it (through bytebuf).

0 if not initialised (fixed (#652): the original waited for a byte).

Returns
d0: the byte

◆ c_getserial2()

ULONG c_getserial2 ( UBYTE * p,
ULONG n )

GetSerial address,bytes: receive a block of bytes.

Fixed (#646): 0 bytes receive nothing (the original: 2^32). Fixed (#652): nothing if not initialised.

Parameters
pd0: the buffer
nd1: the number of bytes
Returns
d0: -1 when it received the block, 0 when it did nothing (a byte result; fixed (#649): the original returned its address argument)

◆ c_initserialio()

ULONG c_initserialio ( ULONG baud,
UWORD numbytes,
UBYTE * inbuf,
UBYTE * outbuf )

InitSerialIO baud,numbytes,inbuf,outbuf: set up the serial port for the transfers.

Nothing if already initialised or the baud rate is not between 109 and 3546895 (fixed (#653): the original divided by 0 for baud 0, a rate above 3546895 gave SERPER $FFFF, one below 109 one in the 16th bit, the LONG flag). Switches the TBE and RBF interrupts off (INTENA $0801), sets SERPER = 3546895 / baud - 1 (fixed (#647): a division that works on a 68000, the original's DIVU.L is a 68020 instruction), and stores numbytes - 1 and the buffers (numbytes 0 gives NOBYTES: DoSerialIO without argument transfers nothing, fixed (#1164)). Fixed (#651): inten gets INTENAR's value, the original the immediate $F01C (the address of INTENAR). Fixed (#653): the library is initialised also with SERPER 0 (3546895 baud), which the original took for "not initialised".

Parameters
baudd0: the baud rate
numbytesd1: the bytes of a DoSerialIO
inbufd2: the receive buffer
outbufd3: the send buffer
Returns
d0: -1 when it set the port up, 0 when it did nothing (fixed (#650): the original returned the baud argument in both cases)

◆ c_putserial()

void c_putserial ( UBYTE b)

PutSerial byte: send one byte (through bytebuf).

Waits for TBE and writes the byte with a stop bit to SERDAT; nothing if not initialised.

Parameters
bd0: the byte

◆ c_putserial2()

void c_putserial2 ( const UBYTE * p,
ULONG n )

PutSerial address,bytes: send a block of bytes.

Nothing if not initialised. Fixed (#646): 0 bytes send nothing (the original sent one byte and counted on with SUBQ.l: BNE, 2^32 bytes).

Parameters
pd0: the address of the data
nd1: the number of bytes

◆ c_setiobuf()

void c_setiobuf ( UBYTE * in,
UBYTE * out )

SetIOBuffers inbuf,outbuf: the buffers of DoSerialIO.

Parameters
ind0: the receive buffer
outd1: the send buffer

◆ c_setiosize()

void c_setiosize ( UWORD n)

SetIOSize bytes: the count of DoSerialIO without argument.

Stored as count - 1 for DoSerialIO's DBRA loop, as InitSerialIO stores its numbytes. Fixed (#1158): the original stored the count as it is, so DoSerialIO transferred one byte more than SetIOSize said. A count of 0 gives NOBYTES: DoSerialIO without argument transfers nothing (fixed, #1164).

Parameters
nd0: the count

◆ c_setiospeed()

void c_setiospeed ( UWORD baud)

SetIOSpeed baudrate: a new baud rate (SERPER) if initialised.

Fixed (#653): nothing for a rate below 109 (the original wrote a SERPER with the LONG flag, or divided by 0), and the library is initialised also after SERPER 0. The division as in InitSerialIO (fixed, #647). Fixed (#654): it divides by the word argument alone, the original by all of d0, also its undefined upper word.

Parameters
baudd0.w: the baud rate

◆ c_setserialmode()

void c_setserialmode ( UBYTE mode)

SetSerialMode mode: master (<> 0) or slave (0) for DoSerialIO.

Parameters
moded0: the mode (a byte; the first byte of the word mode)

◆ c_syncget()

void c_syncget ( UBYTE * p,
ULONG n )

SyncGet address,bytes: receive bytes, answering each one.

For every byte received it sends the byte in bytebuf (whatever was last written there: no defect, the answer only paces the sender). Fixed (#646): 0 bytes receive nothing (the original: 2^32). Fixed (#652): nothing if not initialised.

Parameters
pd0: the buffer
nd1: the number of bytes

◆ c_syncput()

void c_syncput ( const UBYTE * p,
ULONG n )

SyncPut address,bytes: send bytes, each paced by the other side's answer.

For every byte: send it, wait for a byte back (into bytebuf). Fixed (#646): 0 bytes send nothing (the original: 2^32). Fixed (#652): nothing if not initialised.

Parameters
pd0: the data
nd1: the number of bytes

◆ c_syncserial()

BB_BOOL c_syncserial ( UBYTE sync)

SyncSerial syncbyte: synchronise with the other machine, deciding master and slave.

A byte already waiting makes this side the slave: it waits for the sync byte, echoes it and reads until a 0 arrives. Otherwise it is the master: it sends the sync byte until it comes back, then sends 0 and flushes. BUG(orig) (#655), wontfix: two machines starting at the same time both become master (each takes the other's sync byte for the echo); see the file's head. BB_FALSE if not initialised (fixed (#652): the original ran on the port).

Parameters
syncd0: the sync byte
Returns
d0: BB_TRUE = master, BB_FALSE = slave (a word result)

◆ checkserial()

void checkserial ( void )

◆ doserialIO()

void doserialIO ( void )

◆ doserialIO2()

void doserialIO2 ( void )

◆ endserialio()

void endserialio ( void )

◆ finitserialio()

void finitserialio ( void )

◆ flushserialio()

void flushserialio ( void )

◆ getserial()

void getserial ( void )

◆ getserial2()

void getserial2 ( void )

◆ initserialio()

void initserialio ( void )

◆ putserial()

void putserial ( void )

◆ putserial2()

void putserial2 ( void )

◆ readbyte()

UBYTE readbyte ( void )
static

◆ rifastseriallib_codefinit()

void rifastseriallib_codefinit ( void )

◆ rifastseriallib_codeinit()

void rifastseriallib_codeinit ( void )

◆ setbaud()

void setbaud ( ULONG baud)
static

◆ setiobuf()

void setiobuf ( void )

◆ setiosize()

void setiosize ( void )

◆ setiospeed()

void setiospeed ( void )

◆ setserialmode()

void setserialmode ( void )

◆ syncget()

void syncget ( void )

◆ syncput()

void syncput ( void )

◆ syncserial()

void syncserial ( void )

◆ udiv()

ULONG udiv ( ULONG n,
ULONG d )
static

◆ writebyte()

void writebyte ( UBYTE b)
static

Variable Documentation

◆ baud

UWORD baud

◆ bytebuf

UBYTE bytebuf[2]

◆ [struct]

struct { ... } fs

◆ inbuf

UBYTE* inbuf

◆ inten

UWORD inten

◆ mode

UBYTE mode

◆ mode_lo

UBYTE mode_lo

◆ numbytes

UWORD numbytes

◆ outbuf

UBYTE* outbuf

◆ ready

UBYTE ready