Project

General

Profile

Revision 235

trying to implement uart. not working

View differences:

proj/include/errors.h
17 17
    OUT_OF_RANGE,       /** @brief Accessing area out of range of memory */
18 18
    ALLOC_ERROR,        /** @brief Memory allocation error */
19 19
    LOGIC_ERROR,        /** @brief Logic error */
20
    INVALID_ARG,        /** @brief Invalid argument */
20 21
    OTHER_ERROR         /** @brief Unspecified error */
21 22
};
22 23

  
proj/include/test7.h
1
int ser_test_conf(unsigned short base_addr);
2

  
3
int ser_test_set(unsigned short base_addr, unsigned long bits, unsigned long stop,
4
	     long parity, /* -1: none, 0: even, 1: odd */
5
	     unsigned long rate);
6

  
7
int ser_test_poll(unsigned short base_addr, unsigned char tx, unsigned long bits, 	unsigned long stop, long parity, unsigned long rate, int stringc, char *strings[]);
8

  
9
int ser_test_int(/* details to be provided */) ;
10

  
11
int ser_test_fifo(/* details to be provided */);
0 12

  
proj/include/uart.h
1
#ifndef UART_H_INCLUDED
2
#define UART_H_INCLUDED
3

  
4
#include "uart_macros.h"
5

  
6
typedef struct {
7
    int     base_addr;
8
    uint8_t config;
9
    uint8_t dll;
10
    uint8_t dlm;
11
    uint8_t bits_per_char;
12
    uint8_t stop_bits;
13
    uint8_t parity;
14
    uint8_t break_control;
15
    uint16_t divisor_latch;
16
} uart_config;
17

  
18
int uart_get_config(int base_addr, uart_config *config);
19
void uart_parse_config(uart_config *config);
20
void uart_print_config(uart_config config);
21

  
22
int uart_enable_divisor_latch (int base_addr);
23
int uart_disable_divisor_latch(int base_addr);
24

  
25
int uart_write_config          (int base_addr, uint8_t     config       );
26
int uart_set_bits_per_character(int base_addr, uint8_t     bits_per_char);
27
int uart_set_stop_bits         (int base_addr, uint8_t     stop         );
28
int uart_set_parity            (int base_addr, uart_parity par          );
29
int uart_set_bit_rate          (int base_addr, float       bit_rate     );
30

  
31
#endif //UART_H_INCLUDED
0 32

  
proj/include/uart_macros.h
1
#ifndef UART_MACROS_H_INCLUDED
2
#define UART_MACROS_H_INCLUDED
3

  
4
#define UART_BITRATE        115200
5

  
6
#define COM1_ADDR           0x3F8
7
#define COM2_ADDR           0x2F8
8
#define COM1_IRQ            4
9
#define COM2_IRQ            3
10
#define COM1_VECTOR         0x0C
11
#define COM2_VECTOR         0x0B
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
typedef enum {
47
    uart_parity_none = 0x0,
48
    uart_parity_odd  = 0x1,
49
    uart_parity_even = 0x3,
50
    uart_parity_par1 = 0x5,
51
    uart_parity_par0 = 0x7
52
} uart_parity;
53

  
54
#endif //UART_MACROS_H_INCLUDED
0 55

  
proj/src/proj.c
29 29

  
30 30
#include "list.h"
31 31

  
32
#ifdef DIOGO
33
    #include "uart_macros.h"
34
    #include "test7.h"
35
#endif
36

  
32 37
int main(int argc, char* argv[]) {
33 38

  
34 39
    lcf_set_language("EN-US");
35 40

  
36
    //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
41
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
37 42

  
38 43
    lcf_log_output("/home/lcom/labs/proj/output.txt");
39 44

  
......
48 53

  
49 54
    int r;
50 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
        if((r = util_sys_inb(0x3F8+3, &conf))) return 1; //printf("0x%02X\n", conf);
63
    #endif
64

  
51 65
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
52 66
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
53 67

  
......
137 151
        tickdelay(micros_to_ticks(1000000));*/
138 152

  
139 153
        // RTC
154
        /*
140 155
        uint8_t date[4], time[3];
141 156

  
142 157
        if (rtc_read_time(time)) return 1;
......
146 161
        printf("Hour: %02d:%02d:%02d\n", time[2], time[1], time[0]);
147 162

  
148 163
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
164
        */
165
        //UART
166
        /*
167
        printf("got %d\n", ser_test_conf(COM1_ADDR));
168
        printf("\n");
169
        printf("got %d\n", ser_test_set(COM1_ADDR, 6, 1, 0, 9800));
170
        tickdelay(micros_to_ticks(1000000));
171
        printf("got %d\n", ser_test_conf(COM1_ADDR));
172
        */
173

  
174

  
149 175
    #endif
150 176

  
151 177
    #ifdef TELMO
proj/src/test7.c
1
#include <lcom/lcf.h>
2

  
3
#include "errors.h"
4
#include "uart_macros.h"
5
#include "uart.h"
6

  
7
int ser_test_conf(unsigned short base_addr) {
8
	int ret = SUCCESS;
9
	uart_config conf;
10
	if((ret = uart_get_config(base_addr, &conf))) return ret;
11
	uart_print_config(conf);
12
	return SUCCESS;
13
}
14

  
15
int ser_test_set(unsigned short base_addr, unsigned long bits, unsigned long stop,
16
	           long parity, unsigned long rate) {
17
    int par;
18
   	switch(parity){
19
		case -1: par = uart_parity_none; break;
20
		case  0: par = uart_parity_even; break;
21
		case +1: par = uart_parity_odd ; break;
22
		default: return INVALID_ARG;
23
   	}
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;
35
	if((ret = uart_set_bit_rate          (base_addr, rate))) return ret; tickdelay(micros_to_ticks(100000));
36

  
37
	return SUCCESS;
38
}
39

  
40
int ser_test_poll(unsigned short base_addr, unsigned char tx, unsigned long bits,
41
                    unsigned long stop, long parity, unsigned long rate,
42
                    int stringc, char *strings[]) {
43
    /* To be completed */
44
	return 1;
45
}
46

  
47
int ser_test_int(/* details to be provided */) {
48
    /* To be completed */
49
	return 1;
50
}
51

  
52
int ser_test_fifo(/* details to be provided */) {
53
    /* To be completed */
54
	return 1;
55
}
0 56

  
proj/src/uart.c
1
#include <lcom/lcf.h>
2

  
3
#include "uart.h"
4

  
5
#include "errors.h"
6

  
7
int uart_get_config(int base_addr, uart_config *config){
8
    int ret = SUCCESS;
9

  
10
    config->base_addr = base_addr;
11

  
12
    if((ret = util_sys_inb(base_addr+UART_LCR, &config->config))) return ret;
13

  
14
    if((ret = uart_enable_divisor_latch(base_addr))) return ret;
15
    if((ret = util_sys_inb(base_addr+UART_DLL, &config->dll   ))) return ret;
16
    if((ret = util_sys_inb(base_addr+UART_DLM, &config->dlm   ))) return ret;
17

  
18
    uart_parse_config(config);
19
    return ret;
20
}
21
void uart_parse_config(uart_config *config){
22
    config->bits_per_char        = UART_GET_BITS_PER_CHAR(config->config);
23
    config->stop_bits            = UART_GET_STOP_BITS    (config->config);
24
    config->parity               = UART_GET_PARITY       (config->config);
25
    config->break_control        = UART_GET_BREAK_CONTROL(config->config);
26
    config->divisor_latch        = config->dlm<<8 | config->dll;
27
}
28
void uart_print_config(uart_config config){
29
    printf("Base address: 0x%X\n", config.base_addr);
30
    printf("Configuration: 0x%02X\n", config.config);
31
    printf("Number of bits per char: %d\n", config.bits_per_char);
32
    printf("Number of stop bits: %d\n", config.stop_bits);
33
    printf("Parity: ");
34
    if((config.parity&1) == 0) printf("no parity\n");
35
    else switch(config.parity){
36
        case uart_parity_odd : printf("odd parity\n"     ); break;
37
        case uart_parity_even: printf("even parity\n"    ); break;
38
        case uart_parity_par1: printf("parity bit is 1\n"); break;
39
        case uart_parity_par0: printf("parity bit is 0\n"); break;
40
        default              : printf("invalid\n"        ); break;
41
    }
42
    printf("Break control: %d\n", config.break_control);
43
    printf("Divisor latch: %d\n", config.divisor_latch);
44
    printf("Bit rate (x1000): %d\n", 1000*(uint32_t)UART_BITRATE/config.divisor_latch);
45
}
46

  
47
int uart_enable_divisor_latch(int base_addr){
48
    int ret = SUCCESS;
49
    uint8_t conf; if((ret = util_sys_inb(base_addr+UART_LCR, &conf))) return ret;
50
    return uart_write_config(base_addr, conf | UART_DLAB);
51
}
52
int uart_disable_divisor_latch(int base_addr){
53
    int ret = SUCCESS;
54
    uint8_t conf; if((ret = util_sys_inb(base_addr+UART_LCR, &conf))) return ret;
55
    return uart_write_config(base_addr, conf & (~UART_DLAB));
56
}
57

  
58
int uart_write_config(int base_addr, uint8_t config){
59
    printf("WRITING CONFIG 0x%02X TO 0x%X\n", config, base_addr);
60
    if(sys_outb(base_addr+UART_LCR, config)) return WRITE_ERROR;
61
    return SUCCESS;
62
}
63
int uart_set_bits_per_character(int base_addr, uint8_t bits_per_char){
64
    if(bits_per_char < 5 || bits_per_char > 8) return INVALID_ARG;
65
    int ret = SUCCESS;
66
    bits_per_char = (bits_per_char-5)&0x3;
67
    uint8_t conf; if((ret = util_sys_inb(base_addr+UART_LCR, &conf))) return ret;
68
    conf = (conf & (~UART_BITS_PER_CHAR)) | bits_per_char;
69
    return uart_write_config(base_addr, conf);
70
}
71
int uart_set_stop_bits(int base_addr, uint8_t stop){
72
    if(stop != 1 && stop != 2) return INVALID_ARG;
73
    int ret = SUCCESS;
74
    stop -= 1;
75
    stop = (stop&1)<<2;
76
    uint8_t conf; if((ret = util_sys_inb(base_addr+UART_LCR, &conf))) return ret;
77
    conf = (conf & (~UART_STOP_BITS)) | stop;
78
    return uart_write_config(base_addr, conf);
79
}
80
int uart_set_parity(int base_addr, uart_parity par){
81
    int ret = SUCCESS;
82
    uint8_t parity = par << 3;
83
    uint8_t conf; if((ret = util_sys_inb(base_addr+UART_LCR, &conf))) return ret;
84
    conf = (conf & (~UART_PARITY)) | parity;
85
    return uart_write_config(base_addr, conf);
86
}
87
int uart_set_bit_rate(int base_addr, float bit_rate){ printf("%s, L82\n", __func__);
88
    int ret = SUCCESS;
89
    uint16_t latch = UART_BITRATE/bit_rate;
90
    uint8_t dll = latch&0xFF;
91
    uint8_t dlm = (latch>>8)&0xFF;
92
    if((ret = uart_enable_divisor_latch(base_addr))) return ret;
93
    printf("dlm,dll=0x%02X%02X\n", dlm, dll);
94
    if(sys_outb(base_addr+UART_DLL, dll)) return WRITE_ERROR;
95
    if(sys_outb(base_addr+UART_DLM, dlm)) return WRITE_ERROR;
96
    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
    return SUCCESS;
100
}
0 101

  
proj/DR.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c test7.c uart.c
6 6

  
7
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D DIOGO -D __LCOM_OPTIMIZED_
7
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D DIOGO #-D __LCOM_OPTIMIZED_
8 8

  
9 9
DPADD += ${LIBLCF}
10 10
LDADD += -llcf
proj/Makefile
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c test7.c uart.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D __LCOM_OPTIMIZED_
8 8

  
proj/TB.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c
5
SRCS= proj.c list.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c rtc.c test7.c uart.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./maps -I./media/xpm -D LCOM_MACRO -D TELMO #-D __LCOM_OPTIMIZED_
8 8

  

Also available in: Unified diff