XMOS chips are programmed in XC (a version of C for parallel processing), standard C or assembler. A UART is a few lines of XC:
// Simple UART demo
#include <platform.h>
#define BIT_RATE 115200
#define BIT_TIME XS1_TIMER_HZ / BIT_RATE
void txByte(unsigned char);
unsigned char rxByte(void);
out port TXD = PORT_UART_TX;
in port RXD = PORT_UART_RX;
unsigned char array[10];
int main()
{
int i;
while (1)
{
for (i = 0; i < 10; i++)
array[i] = rxByte();
for (i = 0; i < 10; i++)
txByte(array[i]);
}
return 0;
}
unsigned char rxByte(void)
{
unsigned data = 0, time;
int i;
unsigned char c;
// Wait for stop bit
RXD when pinseq (1) :> int _;
// wait for start bit
RXD when pinseq (0) :> int _ @ time;
time += BIT_TIME + (BIT_TIME >> 1);
// sample each bit in the middle.
for (i = 0; i < 8; i += 1)
{
RXD @ time :> >> data;
time += BIT_TIME;
}
// reshuffle the data.
c = (unsigned char) (data >> 24);
return {c};
}
void txByte(unsigned char c)
{
unsigned time, data;
// get current time from port with force out.
TXD <: 1 @ time;
// Start bit.
TXD <: 0;
// Data bits.
for (int i = 0; i < 8; i += 1)
{
time += BIT_TIME;
TXD @ time <: >> data;
}
// two stop bits
time += BIT_TIME;
TXD @ time <: 1;
time += BIT_TIME;
TXD @ time <: 1;
}
That code produces a UART running on a single thread. It can easily be replicated across several threads and cores using the par construct.