Commit 607df88f authored by Roberto Hexsel's avatar Roberto Hexsel

UART tested OK in loop-back mode

parent 6b81cccb
......@@ -57,6 +57,7 @@ extern int stopCount(void);
extern int readCount(void);
// LCD display (Macnica board)
#define LCDprint(n) LCDint((n))
extern void LCDinit(void);
extern int LCDprobe(void);
extern int LCDset(int);
......@@ -66,7 +67,9 @@ extern void LCDtopLine(void);
extern void LCDbotLine(void);
extern void LCDgotoxy(int, int);
extern void LCDputc(char);
extern void LCDprint(unsigned int);
extern void LCDint(unsigned int);
extern void LCDshort(unsigned short);
extern void LCDchar(unsigned char);
// 7-segment display and keyboard (Macnica board)
extern void DSP7SEGput(int, int, int, int, int);
......
......@@ -353,7 +353,7 @@ void LCDputc(char c) {
#define conv(c) ((c<10)?((c)+0x30):((c)+('a'-10)))
// write an integer to the display
void LCDprint(unsigned int n) {
void LCDint(unsigned int n) {
int k;
k = (n >>28);
......@@ -376,6 +376,32 @@ void LCDprint(unsigned int n) {
k = (n<<28)>>28;
LCDput( conv(k) );
}
// write a short to the display
void LCDshort(unsigned short n) {
int k;
k = (n >>12);
LCDput( conv(k) );
k = (n<< 4)>>12;
LCDput( conv(k) );
k = (n<< 8)>>12;
LCDput( conv(k) );
k = (n<<12)>>12;
LCDput( conv(k) );
}
// write a char to the display
void LCDchar(unsigned char n) {
int k;
k = (n >>4);
LCDput( conv(k) );
k = (n<< 4)>>4;
LCDput( conv(k) );
}
//-----------------------------------------------------------------------
......
//-------------------------------------------------------------------
// loop-back test
// UART's input must be connected to UART's output for this to work
//
// clear LCD screen, light up BLUE led, wait for 2 seconds, then
// send a string ('0'..'9') through the UART in loop-back mode
// write received characters to the LCD display
// 7-segment displays the last UART's status (must be 0x40)
//
// on 30march2017 tested OK with speeds 2,4,7
//
//-------------------------------------------------------------------
#include "cMIPS.h"
typedef struct control { // control register fields (uses only ls byte)
int ign : 24, // ignore uppermost bits
rts : 1, // Request to Send
ign2 : 2, // bits 6,5 ignored
intTX : 1, // interrupt on TX buffer empty (bit 4)
intRX : 1, // interrupt on RX buffer full (bit 3)
speed : 3; // 4,8,16..256 tx-rx clock data rates (bits 0..2)
} Tcontrol;
typedef struct status { // status register fields (uses only ls byte)
int s;
// int ign : 24, // ignore uppermost bits
// ign7 : 1, // ignored (bit 7)
// txEmpty : 1, // TX register is empty (bit 6)
// rxFull : 1, // octet available from RX register (bit 5)
// int_TX_empt: 1, // interrupt pending on TX empty (bit 4)
// int_RX_full: 1, // interrupt pending on RX full (bit 3)
// ign2 : 1, // ignored (bit 2)
// framing : 1, // framing error (bit 1)
// overun : 1; // overun error (bit 0)
} Tstatus;
#define RXfull 0x00000020
#define TXempty 0x00000040
typedef union ctlStat { // control + status on same address
Tcontrol ctl; // write-only
Tstatus stat; // read-only
} TctlStat;
typedef union data { // data registers on same address
int tx; // write-only
int rx; // read-only
} Tdata;
typedef struct serial {
TctlStat cs;
Tdata d;
} Tserial;
#include "uart_defs.c"
#if 0
int strcopy(const char *y, char *x) {
int i=0;
while ( (*x++ = *y++) != '\0' ) // copy and check end-of-string
......@@ -55,22 +24,28 @@ int strcopy(const char *y, char *x) {
*x = '\0';
return(i+1);
}
#endif
// to remove code not needed for debugging at the simulator -> SYNTHESIS=0
#define SYNTHESIS 1
int main(void) { // receive a string through the UART, in loop back
// and write it to the LCD display
int main(void) {
volatile Tserial *uart; // tell GCC not to optimize away code
volatile Tstatus status;
Tcontrol ctrl;
int i,n;
int i,n,r,s;
int state;
char s,r;
int *addr;
#if SYNTHESIS
LCDinit();
LCDclr();
DSP7SEGput( 0, 0, 0, 0, 1); // light up BLUE led
delay_ms(2000); // wait 2 seconds
LCDtopLine();
LCDput(' ');
......@@ -85,8 +60,6 @@ int main(void) { // receive a string through the UART, in loop back
LCDput('r');
LCDput('l');
LCDput('d');
LCDbotLine();
#else
to_stdout('\n');
#endif
......@@ -98,42 +71,55 @@ int main(void) { // receive a string through the UART, in loop back
ctrl.ign2 = 0;
ctrl.intTX = 0;
ctrl.intRX = 0;
ctrl.speed = 2;
ctrl.speed = 7; // on 30march2017 tested for speed = {2,4,7}
uart->cs.ctl = ctrl;
s = '0';
do {
while ( ( (state = uart->cs.stat.s) & TXempty ) == 0 )
{ };
uart->d.tx = (int)s;
while ( ( (state = uart->cs.stat.s) & RXfull ) == 0 )
{ };
r = (char)uart->d.rx;
// let us see the state
state = uart->cs.stat.rxFull;
LCDgotoxy(14,1);
LCDchar((unsigned char)state);
DSP7SEGput( state>>4 , 0, state & 0xf, 0, 4);
s = '0'; // start transmission from '0'
do {
i = 0;
while ( ( state = uart->cs.stat.txEmpty ) == 0 ) {
i = i+1;
LCDgotoxy(14,1);
LCDchar((unsigned char)state);
DSP7SEGput( state>>4 , 0, state & 0xf, 0, i & 0x07);
};
uart->d.tx = (int)s; // send out char
if (s == '0')
LCDgotoxy(1,2); // put cursor on second line
i = 0;
while ( ( state = uart->cs.stat.rxFull ) == 0 ) {
i = i+1;
DSP7SEGput( state>>4 , 0, state & 0xf, 0, i & 0x07);
};
r = uart->d.rx; // get char from UART
#if SYNTHESIS
LCDput( r );
DSP7SEGput( state>>4 , 0, state & 0xf, 0);
cmips_delay(25000000);
LCDputc((unsigned char)r); // send it to LCD display
DSP7SEGput( state>>4 , 0, state & 0xf, 0, r & 0x07);
#else
to_stdout(r);
#endif
s = (char)((int)s +1);
s = (char)((int)s + 1); // next ASCII algarism
} while (s != ':');
#if SYNTHESIS
LCDput( ' ' );
LCDput( 'z' );
addr = (int *)IO_UART_ADDR; // get last state
state = *addr; // must show 0x40 = TXempty, no errors
DSP7SEGput( state>>4 , 0, state & 0xf, 0, 0x02); // light up GREEN
do { } while (1 == 1);
do { delay_us(1) ; } while (1 == 1); // and wait forever
#else
to_stdout('\n');
to_stdout('\n');
to_stdout('\n');
to_stdout('\n');
#endif
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment