Revision 241
implementing uart
proj/include/uart.h | ||
---|---|---|
12 | 12 |
uint8_t stop_bits; |
13 | 13 |
uint8_t parity; |
14 | 14 |
uint8_t break_control; |
15 |
uint8_t dlab; |
|
15 | 16 |
uint16_t divisor_latch; |
16 | 17 |
} uart_config; |
17 | 18 |
|
proj/include/uart_macros.h | ||
---|---|---|
10 | 10 |
#define COM1_VECTOR 0x0C |
11 | 11 |
#define COM2_VECTOR 0x0B |
12 | 12 |
|
13 |
#define UART_RBR 0 |
|
14 |
#define UART_THR 0 |
|
15 |
#define UART_IER 1 |
|
16 |
#define UART_IIR 2 |
|
17 |
#define UART_FCR 2 |
|
18 |
#define UART_LCR 3 |
|
19 |
#define UART_MCR 4 |
|
20 |
#define UART_LSR 5 |
|
21 |
#define UART_MSR 6 |
|
22 |
#define UART_SR 7 |
|
23 |
|
|
24 |
#define UART_DLL 0 |
|
25 |
#define UART_DLM 1 |
|
26 |
|
|
27 |
#define UART_BITS_PER_CHAR_POS 0 |
|
28 |
#define UART_STOP_BITS_POS 2 |
|
29 |
#define UART_PARITY_POS 3 |
|
30 |
#define UART_BREAK_CONTROL_POS 6 |
|
31 |
#define UART_DLAB_POS 7 |
|
32 |
|
|
33 |
#define UART_BITS_PER_CHAR (BIT(0) | BIT(1)) |
|
34 |
#define UART_STOP_BITS (BIT(2)) |
|
35 |
#define UART_PARITY (BIT(3) | BIT(4) | BIT(5)) |
|
36 |
#define UART_BREAK_CONTROL (BIT(6)) |
|
37 |
#define UART_DLAB (BIT(7)) |
|
38 |
|
|
39 |
#define UART_GET_BITS_PER_CHAR(n) ((n)&UART_BITS_PER_CHAR + 5) |
|
40 |
#define UART_GET_STOP_BITS(n) ((n)&UART_STOP_BITS? 2 : 1) |
|
41 |
#define UART_GET_PARITY(n) (((n)&UART_PARITY )>>UART_PARITY_POS ) |
|
42 |
#define UART_GET_BREAK_CONTROL(n) (((n)&UART_BREAK_CONTROL)>>UART_BREAK_CONTROL_POS) |
|
43 |
#define UART_GET_DLAB(n) (((n)&UART_DLAB )>>UART_DLAB_POS ) |
|
44 |
|
|
45 |
|
|
46 | 13 |
typedef enum { |
47 | 14 |
uart_parity_none = 0x0, |
48 | 15 |
uart_parity_odd = 0x1, |
proj/src/proj.c | ||
---|---|---|
53 | 53 |
|
54 | 54 |
int r; |
55 | 55 |
|
56 |
#ifdef DIOGO |
|
57 |
uint8_t conf; |
|
58 |
if((r = util_sys_inb(0x3F8+3, &conf))) return 1; printf("0x%02X\n", conf); |
|
59 |
conf = 0x19; //00011001 |
|
60 |
printf("0x%02X\n", conf); |
|
61 |
if((r = sys_outb(0x3F8+3, conf))) return 1; |
|
62 |
uint8_t s; |
|
63 |
if((r = util_sys_inb(0x3F8+3, &s))) return 1; printf("S: 0x%02X\n", s); |
|
64 |
return 0; |
|
65 |
|
|
66 |
#endif |
|
67 |
|
|
68 | 56 |
font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2"); |
69 | 57 |
if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; } |
70 | 58 |
|
... | ... | |
168 | 156 |
printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]); |
169 | 157 |
*/ |
170 | 158 |
//UART |
171 |
/* |
|
172 |
printf("got %d\n", ser_test_conf(COM1_ADDR)); |
|
173 |
printf("\n"); |
|
174 |
printf("got %d\n", ser_test_set(COM1_ADDR, 6, 1, 0, 9800)); |
|
175 |
tickdelay(micros_to_ticks(1000000)); |
|
176 |
printf("got %d\n", ser_test_conf(COM1_ADDR)); |
|
177 |
*/ |
|
178 |
|
|
179 |
|
|
159 |
if((r = ser_test_conf(COM1_ADDR))) return r; |
|
160 |
if((r = ser_test_set(COM1_ADDR, 6, 2, 0, 9800))) return r; |
|
161 |
if((r = ser_test_conf(COM1_ADDR))) return r; |
|
180 | 162 |
#endif |
181 | 163 |
|
182 | 164 |
#ifdef TELMO |
proj/src/test7.c | ||
---|---|---|
22 | 22 |
default: return INVALID_ARG; |
23 | 23 |
} |
24 | 24 |
int ret = SUCCESS; |
25 |
|
|
26 |
uint8_t conf = 0; |
|
27 |
conf |= (bits-5)<<UART_BITS_PER_CHAR_POS; |
|
28 |
conf |= (stop-1)<<UART_STOP_BITS_POS; |
|
29 |
conf |= par <<UART_PARITY_POS; |
|
30 |
|
|
31 |
//if((ret = uart_set_bits_per_character(base_addr, bits))) return ret; tickdelay(micros_to_ticks(100000)); |
|
32 |
//if((ret = uart_set_stop_bits (base_addr, stop))) return ret; tickdelay(micros_to_ticks(100000)); |
|
33 |
//if((ret = uart_set_parity (base_addr, par ))) return ret; tickdelay(micros_to_ticks(100000)); |
|
34 |
if((ret = uart_write_config(base_addr, conf))) return ret; |
|
25 |
if((ret = uart_set_bits_per_character(base_addr, bits))) return ret; tickdelay(micros_to_ticks(100000)); |
|
26 |
if((ret = uart_set_stop_bits (base_addr, stop))) return ret; tickdelay(micros_to_ticks(100000)); |
|
27 |
if((ret = uart_set_parity (base_addr, par ))) return ret; tickdelay(micros_to_ticks(100000)); |
|
35 | 28 |
if((ret = uart_set_bit_rate (base_addr, rate))) return ret; tickdelay(micros_to_ticks(100000)); |
36 | 29 |
|
37 | 30 |
return SUCCESS; |
proj/src/uart.c | ||
---|---|---|
4 | 4 |
|
5 | 5 |
#include "errors.h" |
6 | 6 |
|
7 |
#define UART_RBR 0 |
|
8 |
#define UART_THR 0 |
|
9 |
#define UART_IER 1 |
|
10 |
#define UART_IIR 2 |
|
11 |
#define UART_FCR 2 |
|
12 |
#define UART_LCR 3 |
|
13 |
#define UART_MCR 4 |
|
14 |
#define UART_LSR 5 |
|
15 |
#define UART_MSR 6 |
|
16 |
#define UART_SR 7 |
|
17 |
|
|
18 |
#define UART_DLL 0 |
|
19 |
#define UART_DLM 1 |
|
20 |
|
|
21 |
#define UART_BITS_PER_CHAR_POS 0 |
|
22 |
#define UART_STOP_BITS_POS 2 |
|
23 |
#define UART_PARITY_POS 3 |
|
24 |
#define UART_BREAK_CONTROL_POS 6 |
|
25 |
#define UART_DLAB_POS 7 |
|
26 |
|
|
27 |
#define UART_BITS_PER_CHAR (BIT(0) | BIT(1)) |
|
28 |
#define UART_STOP_BITS (BIT(2)) |
|
29 |
#define UART_PARITY (BIT(3) | BIT(4) | BIT(5)) |
|
30 |
#define UART_BREAK_CONTROL (BIT(6)) |
|
31 |
#define UART_DLAB (BIT(7)) |
|
32 |
|
|
33 |
#define UART_GET_BITS_PER_CHAR(n) (((n)&UART_BITS_PER_CHAR) + 5) |
|
34 |
#define UART_GET_STOP_BITS(n) (((n)&UART_STOP_BITS)? 2 : 1) |
|
35 |
#define UART_GET_PARITY(n) (((n)&UART_PARITY )>>UART_PARITY_POS ) |
|
36 |
#define UART_GET_BREAK_CONTROL(n) (((n)&UART_BREAK_CONTROL)>>UART_BREAK_CONTROL_POS) |
|
37 |
#define UART_GET_DLAB(n) (((n)&UART_DLAB )>>UART_DLAB_POS ) |
|
38 |
#define UART_GET_DIV_LATCH(m,l) ((m)<<8 | (l)) |
|
39 |
#define UART_GET_DLL(n) ((n)&0xFF) |
|
40 |
#define UART_GET_DLM(n) (((n)>>8)&0xFF) |
|
41 |
|
|
7 | 42 |
int uart_get_config(int base_addr, uart_config *config){ |
8 | 43 |
int ret = SUCCESS; |
9 | 44 |
|
... | ... | |
23 | 58 |
config->stop_bits = UART_GET_STOP_BITS (config->config); |
24 | 59 |
config->parity = UART_GET_PARITY (config->config); |
25 | 60 |
config->break_control = UART_GET_BREAK_CONTROL(config->config); |
26 |
config->divisor_latch = config->dlm<<8 | config->dll; |
|
61 |
config->dlab = UART_GET_DLAB (config->config); |
|
62 |
config->divisor_latch = UART_GET_DIV_LATCH (config->dlm, config->dll); |
|
27 | 63 |
} |
28 | 64 |
void uart_print_config(uart_config config){ |
29 | 65 |
printf("Base address: 0x%X\n", config.base_addr); |
... | ... | |
41 | 77 |
} |
42 | 78 |
printf("Break control: %d\n", config.break_control); |
43 | 79 |
printf("Divisor latch: %d\n", config.divisor_latch); |
44 |
printf("Bit rate (x1000): %d\n", 1000*(uint32_t)UART_BITRATE/config.divisor_latch);
|
|
80 |
printf("Bit rate: %d\n", UART_BITRATE/config.divisor_latch);
|
|
45 | 81 |
} |
46 | 82 |
|
47 | 83 |
int uart_enable_divisor_latch(int base_addr){ |
... | ... | |
56 | 92 |
} |
57 | 93 |
|
58 | 94 |
int uart_write_config(int base_addr, uint8_t config){ |
59 |
printf("WRITING CONFIG 0x%02X TO 0x%X\n", config, base_addr); |
|
60 | 95 |
if(sys_outb(base_addr+UART_LCR, config)) return WRITE_ERROR; |
61 | 96 |
return SUCCESS; |
62 | 97 |
} |
... | ... | |
84 | 119 |
conf = (conf & (~UART_PARITY)) | parity; |
85 | 120 |
return uart_write_config(base_addr, conf); |
86 | 121 |
} |
87 |
int uart_set_bit_rate(int base_addr, float bit_rate){ printf("%s, L82\n", __func__);
|
|
122 |
int uart_set_bit_rate(int base_addr, float bit_rate){ |
|
88 | 123 |
int ret = SUCCESS; |
89 | 124 |
uint16_t latch = UART_BITRATE/bit_rate; |
90 |
uint8_t dll = latch&0xFF;
|
|
91 |
uint8_t dlm = (latch>>8)&0xFF;
|
|
125 |
uint8_t dll = UART_GET_DLL(latch);
|
|
126 |
uint8_t dlm = UART_GET_DLM(latch);
|
|
92 | 127 |
if((ret = uart_enable_divisor_latch(base_addr))) return ret; |
93 |
printf("dlm,dll=0x%02X%02X\n", dlm, dll); |
|
94 | 128 |
if(sys_outb(base_addr+UART_DLL, dll)) return WRITE_ERROR; |
95 | 129 |
if(sys_outb(base_addr+UART_DLM, dlm)) return WRITE_ERROR; |
96 | 130 |
if((ret = util_sys_inb(base_addr+UART_DLM, &dlm))) return ret; |
97 |
printf("dlm=0x%02X\n", dlm); |
|
98 |
printf("%s, L91\n", __func__); |
|
99 | 131 |
return SUCCESS; |
100 | 132 |
} |
Also available in: Unified diff