Skip to content

Commit 01fcdd0

Browse files
committed
Updated
1 parent 6d79ee2 commit 01fcdd0

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

examples/uart_main/uart_main.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,20 @@
2222

2323
//Macros for generating function names
2424
#define CREATE_FAST_UART(uart_name,tx_pin) #uart_name#tx_pin"init()"
25-
#define SEND_FAST_UART(uart_name,tx_pin) uart_name##tx_pin##_tx
25+
#define SEND_FAST_UART(uart_name,tx_pin) uart_name##tx_pin##_tx
2626

2727
//Used for setting the baud rate.
28-
//Currently unimplemented
2928
enum uart_baud baud;
3029
void main(void)
3130
{
32-
baud = BAUD_115200;
31+
baud = BAUD_19200;
3332
uartX_init(baud);
33+
baud = BAUD_19200;
34+
uartX_set_baud(baud);
3435
while (TRUE)
35-
{
36-
printf("Hello");
37-
}
36+
{
37+
printf("Hello");
38+
}
3839
}
3940

4041
void putchar(char c)

lib/uart/soft_uart.c

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
**/
55
#include <fx2regs.h>
66
#include <fx2macros.h>
7-
#include <serial.h>
87
#include <uart/api.h>
8+
#include <assert.h>
99

10-
11-
10+
/*This is set by calling the uartX_set_baud() function.*/
11+
unsigned char load_delay;
1212
BOOL uartX_init(enum uart_baud rate, ...)
1313
{
1414
//All delay values assume a 48MHZ clock.
@@ -48,7 +48,7 @@ void uartX_tx(char c)
4848
//At 12Mhz, it should be about 83.33ns
4949
//But it appears to be about 88ns
5050
//These numbers have been verified using an analyzer
51-
mov r1, #0x20 //(2 cycles)
51+
mov r1, _load_delay //(2 cycles)
5252
0006$:
5353
//1 bit is about 8.6us
5454
djnz r1, 0006$ //DJNZ on Rn takes (3 cycles)
@@ -62,7 +62,7 @@ void uartX_tx(char c)
6262
//Move the carry into the port
6363
mov _TX_PIN, c //(2 cycles)
6464
//Now we need to add delay for the next
65-
mov r1, #0x1F //(2 cycles)
65+
mov r1, _load_delay //(2 cycles)
6666
//31*3 , 93 cycles of delay
6767
0004$:
6868
djnz r1, 0004$ //(3 cycles)
@@ -72,17 +72,46 @@ void uartX_tx(char c)
7272
djnz r0, 0001$ //(3 cycles)
7373
setb _TX_PIN //(2 cycles) This is for stop bit
7474
//We need to delay the stop bit, otherwise we may get errors.
75-
mov r1, #0x20 //(2 cycles)
75+
mov r1, _load_delay//(2 cycles)
7676
0005$:
77-
djnz r1, 0005$ //(3 cycles) for DJNZ , Jump for 32*3 , 96 cycles
77+
djnz r1, 0005$ //(3 cycles) for DJNZ , Jump for 32*3 , 96 cycles
7878
nop //(NOP takes 1 cycle) 97 cycles of delay
7979
setb _EA; //Enable back the interrupts
8080
__endasm;
8181
}
8282

8383
BOOL uartX_set_baud(enum uart_baud rate)
8484
{
85-
return FALSE;
85+
switch(rate)
86+
{
87+
case BAUD_2400:
88+
load_delay = 0xd0;
89+
break;
90+
case BAUD_4800:
91+
break;
92+
case BAUD_9600:
93+
break;
94+
case BAUD_19200:
95+
load_delay = 0xd0;
96+
break;
97+
case BAUD_38400:
98+
load_delay = 0x68;
99+
break;
100+
case BAUD_57600:
101+
load_delay = 0x45;
102+
break;
103+
case BAUD_115200:
104+
load_delay = 0x20;
105+
break;
106+
case BAUD_ANY:
107+
break;
108+
case BAUD_FASTEST:
109+
break;
110+
default:
111+
load_delay = 0x20;
112+
break;
113+
}
114+
return TRUE;
86115
}
87116

88117
enum uart_baud uartX_get_baud()
@@ -98,17 +127,17 @@ BOOL uartX_tx_willblock()
98127
char uartX_rx()
99128
{
100129
//This function should never be called
130+
assert(FALSE);
101131
return 0xFF;
102132
}
103133

104134
BOOL uartX_check_rx_blocking()
105135
{
106-
//Doesnt really matter what we send here
107-
return FALSE;
136+
return TRUE;
108137
}
109138

110139
BYTE uartX_check_receive_buffer()
111140
{
112-
//Read not implemented. Always return a 0.
141+
//Read not implemented.No data is present in the buffer
113142
return 0x00;
114143
}

0 commit comments

Comments
 (0)