here's my code:
Code: Select all
#include <platform.h>
#include <xs1.h>
#include <stdio.h>
/* - The I/O ports are clocked at 100Mhz
- UART enables a UART server that interfaces with the UART-to-USB converter
on the XMOS USB-to-JTAG adapters. The converter operates at a rate of
115200 bits/sec.
- UART: 115200,8,n,1*/
#define BIT_RATE 115200
#define BIT_TIME 100000000 / BIT_RATE
void txByte(unsigned char);
unsigned char rxByte(void);
out port TXD = PORT_UART_TX;
in port RXD = PORT_UART_RX;
out port led = PORT_LED;
int main()
{
int i;
unsigned char array[10]={"HolaAmigo"};
while(1){
led <:1;
for (i = 0; i < 10; i++){
array[i] = rxByte();
}
led <: 0;
for (i = 0; i < 10; i++){
printf("%c",array[i]);
printf("\n");
txByte(array[i]);
}
}
return 0;
}
unsigned char rxByte(void)
{
unsigned data = 0, time;
int i;
timer t;
unsigned char c;
// Wait for stop bit
// RXD when pinseq (1) :> int _;
// wait for start bit
RXD when pinseq (0) :> void;
t :> time;
time += BIT_TIME/2;
// sample each bit in the middle.
for (i = 0; i < 8; i += 1)
{
time +=BIT_TIME;
t when timerafter(time) :> void;
RXD :> >> data;
}
// input stop it
time +=BIT_TIME;
t when timerafter(time) :> void;
RXD :> void;
// reshuffle the data.
c = (unsigned char) (data >> 24);
return c;
}
void txByte(unsigned char c)
{
unsigned time, data;
data = c;
// 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;
}
// one stop bit
time += BIT_TIME;
TXD @ time <: 1;
}