README.txt for FXS Interface
David Rowe 4 Feb 2009

CPLD Logic
----------

The goal of the CPLD logic is to transfer samples between the router
115,000 baud RS232 UART and the FXS chip-set 2.048 MHz TDM bus.

PCLK is the 2.048 MHz master clock for the system.  We divide PCLK by
256 to obtain a 8 KHz Frame Sync FS.  When FS is asserted the transfer
of 8 bit transmit and receive samples starts (Fig 2).  The samples are
transmitted on the TDM bus at 2.048 MHz.

To obtain a 115,000 baud clock for the receive path we divide 2.048 MHz
by 18.  This is not exact (8.79 uS compared to 8.70 uS).  To minimise
the chance of a bit error we sample the RS232 waveform at the middle
of each data bit.  The sampling instant is measured from the falling
edge of the start bit.  The error in the sampling point for the first
bit is 1.5*0.09 uS, the second bit 2.5*0.09 uS, and the 8th data bit
8.5*0.09 = 0.765 uS, which is much less than the half a bit offset
(4.4uS) that would cause a bit error.

Two designs have been considered:

1) Eight Byte FIFO: Using router UART FIFOs filled to a nominal depth
of 8 bytes.  When the router UART receives 8 RS232 words, an interrupt
is generated.  As we have a sampling rate of 8 kHz this means an ISR
rate of 1 kHz, or one interrupt every ms.  The disadvantage of this
scheme is that a FIFO is required in the trasnmit path, to cope with
the burst of transmit samples after each interrupt (see below).

2) Single Byte: No use of router FIFOs, the router ISR runs after
every received RS232 word.  This gives a high interrupt rate of 8kHz,
or one interrupt every 125us.  The advantage is simpler CPLD logic.

The difference in the two designs is the FIFO depth required for (1),
and the ISR code.  Common elements of both designs are descibed in
the remainder of this section.

Design (1) - Eight Byte FIFO
----------------------------

Fig 1A is an overview of the entire process. A receive sample on the
TDM bus (DRX) is converted to a 115,000 baud RS232 10-bit word on the
UART bus (RX).  After 8 samples are received by the router UART, and
interrupt is generated an the UART Interrupt Service Routine (ISR)
executes.  This ISR reads the 8 samples in the UART RX FIFO and writes
8 new samples to the UART TX FIFO.

The router UART starts transmitting the TX samples immediately after
(or during) the ISR at 155,000 baud.  Note that the UART TX side send
the samples as fast as possible until the router FIFO empties.  The
sample rate at 115,000 baud is slightly faster than 8 kHz so all of
the TX samples are sent by around 697uS into the 1ms cycle.

At 115,000 baud the UART bit period is 8.7uS.  A 10-bit UART word
takes 87uS, and 8 words 696uS.  This suggests that the transmit path
requires at a 3 byte FIFO.

Design 2 - Single Byte
----------------------

Figure 1B illustrate the timing of the single byte design.  A FS pulse
on the TDM bus starts the whole sequence.  When FS is asserted a TDM
byte appears on DRX, receive.v then retransmits this as a 10 bit RS232
word to the RX input of the router. When this word is received by the
router an ISR is generated.

The ISR stores the received byte n and transmits the next TX byte n to
the CPLD.  The txuart.v module receives this word and buffers it
(doublebuf.v).  The data in this word is buffered by doublebuf.v and
is sent on the next FS pulse to DTX on the TDM bus.

Note that TDM bus data sample n-1 is sent while we are still receiving
the next transmit byte n from the UART.  Thus two seperate registers
are required to avoid over-writing. This is acheived by 2-byte deep
buffering (doublebuf.v).

Prototype Tests and Design Choice
---------------------------------

Design 2 (single byte) was tested first but problems were encountered
in the form of clicks in the tx side audio (audio heard on analog
phone).  These were traced to jitter in the time that the tx RS232
byte was sent, probably due to the SoC processing other interrupts.

Design 1 was therefore chosen.  The transmit FIFO was implemented in
software on an Atmel ATmega microcontroller.  The ATmega UART was used
to receive the RS232 samples, and the ATmega SPI port was used to
transmit the sample onto the TDM bus (SPI and TDM waveforms are very
similar).  Some CPLD logic (genss.v) was used to generate the nSS
signal (not SS, asserted low) required by the the ATmega SPI Port.

This design produced click free transmit and receive audio.

References 
----------

[1] sch/fxsinterface.sch for timing diagrams.
[2] cpld directory *.v files
