I have an XC-3 development kit; and I'm trying to get the UART running, but I can't.
This is the code I wrote:
Code: Select all
#include <xs1.h>
#include <print.h>
#include <platform.h>
#define UART_BIT_RATE 115200
#define BIT_LENGTH (XS1_TIMER_HZ / UART_BIT_RATE)
#define STOP_BIT_LENGTH (BIT_LENGTH *2)
#define FALSE 0
#define TRUE 1
void uart_transmit_init(out port txd);
void uart_transmit(out port txd, char bytes[], int numBytes);
void uart_receive(in port txr, char bytes[], int numBytes);
on stdcore[0] : out port txd = PORT_UART_TX;
on stdcore[0] : in port txr = PORT_UART_RX;
int main(void)
{
char transmit[] = { 11 , 242, 57 };
char receive[] = { 0, 0, 0 };
/* Initialize the transmitter */
uart_transmit_init(txd);
/* Run the transmitter and receiver in parallel */
par {
uart_transmit(txd, transmit, 3);
uart_receive(txr, receive, 3);
}
/* Print out the received values *//*
for (int i=0;i<3;i++) {
printIntln(receive[i]);
}*/
return 0;
}
void uart_transmit_init(out port txd)
{
/* Set pin output to 1 initially to indicate no transmission */
txd <: 1;
}
void uart_transmit(out port txd, char bytes[], int numBytes)
{
unsigned time;
for (int i = 0; i < numBytes; i += 1)
{
int byte = bytes[i];
// Start bit
txd <: 0 @ time;
// Data bits
for (int j = 0; j < 8; j += 1)
{
time += BIT_LENGTH;
txd @ time <: >> byte;
}
// Stop bit
time += BIT_LENGTH;
txd @ time <: 1;
time += STOP_BIT_LENGTH;
txd @ time <: 1;
}
}
void uart_receive(in port txr, char bytes[], int numBytes)
{
for (int i = 0; i < numBytes; i += 1)
{
unsigned time; // Tracks previous port event time
unsigned startBitTime; // Time at beginning of the start bit
int input_val = 0; // The current value being inputted
int got_start_bit = FALSE; // Whether start bit has been received
// Wait for start bit of the required length
while (!got_start_bit) {
txr when pinseq(0) :> int x @ startBitTime;
// Wait until 0 returned for half of BIT TIME
// or revert to a 1 on the port
got_start_bit = TRUE;
while (got_start_bit && time < (startBitTime + BIT_LENGTH/2)) {
int x;
txr :> x @ time;
if (x==1)
got_start_bit = FALSE;
}
}
// Data bits
for (int j = 0; j < 8; j += 1)
{
time += BIT_LENGTH;
txr @ time :> >> input_val;
}
// Input will by in the high byte of input val
// need to shift it down into the low byte
bytes[i] = (unsigned char) (input_val >> 24);
}
}
Greetz,
Ganux_