Project

General

Profile

Revision 275

implementing transmission with queues

View differences:

proj/include/test7.h
1
#ifndef TEST7_H_INCLUDED
2
#define TEST7_H_INCLUDED
3

  
4
#include "uart.h"
5

  
6
int ser_test_conf(unsigned short base_addr);
7

  
8
int ser_test_set(unsigned short base_addr, unsigned long bits, unsigned long stop,
9
	     long parity, /* -1: none, 0: even, 1: odd */
10
	     unsigned long rate);
11

  
12
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[]);
13

  
14
int ser_test_int(/* details to be provided */) ;
15

  
16
int ser_test_fifo(/* details to be provided */);
17

  
18
#endif //TEST7_H_INCLUDED
19 0

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

  
3
#include "errors.h"
4
#include "nctp.h"
5

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

  
14
int ser_test_set(unsigned short base_addr, unsigned long bits, unsigned long stop,
15
	           long parity, unsigned long rate) {
16
    int par;
17
   	switch(parity){
18
		case -1: par = uart_parity_none; break;
19
		case  0: par = uart_parity_even; break;
20
		case +1: par = uart_parity_odd ; break;
21
		default: return INVALID_ARG;
22
   	}
23
	int ret = SUCCESS;
24
	if((ret = uart_set_bits_per_character(base_addr, bits))) return ret; tickdelay(micros_to_ticks(100000));
25
	if((ret = uart_set_stop_bits         (base_addr, stop))) return ret; tickdelay(micros_to_ticks(100000));
26
	if((ret = uart_set_parity            (base_addr, par ))) return ret; tickdelay(micros_to_ticks(100000));
27
	if((ret = uart_set_bit_rate          (base_addr, rate))) return ret; tickdelay(micros_to_ticks(100000));
28

  
29
	return SUCCESS;
30
}
31

  
32
int ser_test_poll(unsigned short base_addr, unsigned char tx, unsigned long bits,
33
                    unsigned long stop, long parity, unsigned long rate,
34
                    int stringc, char *strings[]) {
35
	int ret = SUCCESS;
36
    if((ret = ser_test_set(base_addr, bits, stop, parity, rate))) return ret;
37
	if((ret = uart_disable_int_rx(base_addr))) return ret;
38
	if((ret = uart_disable_int_tx(base_addr))) return ret;
39
	if(tx == 0){
40
		uint8_t c = ' ';
41
		int r;
42
		while(c != '.'){
43
			r = nctp_get_char_poll(base_addr, &c);
44
			if(r == TIMEOUT_ERROR) continue;
45
			else if(r != SUCCESS ) return r;
46
			printf("%c", c);
47
		}
48
	}else{
49
		for(int i = 0; i < stringc; ++i){
50
			for(int j = 0; strings[i][j] != 0; ++j)
51
				if((ret = nctp_send_char_poll(base_addr, strings[i][j])))
52
					return ret;
53
			if(i+1 != stringc)
54
				if((ret = nctp_send_char_poll(base_addr, ' '))) return ret;
55
		}
56
		if((ret = nctp_send_char_poll(base_addr, '.'))) return ret;
57
	}
58
	return SUCCESS;
59
}
60

  
61
int ser_test_int(/* details to be provided */) {
62
    /* To be completed */
63
	return 1;
64
}
65

  
66
int ser_test_fifo(/* details to be provided */) {
67
    /* To be completed */
68
	return 1;
69
}
70 0

  
proj/include/hltp.h
3 3

  
4 4
#include "nctp.h"
5 5

  
6
int hltp_send_string(int base_addr, const char *p);
6
int hltp_send_string(const char *p);
7 7

  
8
int hltp_get_string(int base_addr, char **p);
9

  
10 8
#endif //HLTP_H_INCLUDED
proj/include/list.h
18 18
list_node_t* (list_begin )(list_t *l);
19 19
list_node_t* (list_end   )(list_t *l);
20 20
size_t       (list_size  )(const list_t *l);
21
int          (list_empty )(const list_t *l);
21 22
list_node_t* (list_insert)(list_t *l, list_node_t *position, void *val);
22 23
void*        (list_erase )(list_t *l, list_node_t *position);
24
void         (list_push_back)(list_t *l, void *val);
25
void**       (list_front)(list_t *l);
26
void         (list_pop_front)(list_t *l);
23 27

  
24 28
#endif //LIST_H_INCLUDED
proj/include/nctp.h
3 3

  
4 4
#include "uart.h"
5 5

  
6
int nctp_get_char_poll   (int base_addr, uint8_t *p);
7
int nctp_send_char_poll  (int base_addr, uint8_t  c);
6
int nctp_init(void);
7
int nctp_free(void);
8 8

  
9
/**
10
 * @brief Send stuff to COM #port.
11
 * @param   port    Port to send message to
12
 * @param   num     Number of pairs of address/numbytes to send
13
 */
14
int nctp_send(int base_addr, size_t num, uint8_t* ptr[], size_t sz[]);
9
int nctp_send(size_t num, uint8_t* ptr[], size_t sz[]);
15 10

  
16
int nctp_get (int base_addr, uint8_t **dest);
11
int nctp_ih_err;
12
void nctp_ih(void);
17 13

  
18 14
#endif //NCTP_H_INCLUDED
proj/src/hltp.c
3 3
#include "hltp.h"
4 4

  
5 5
#include "nctp.h"
6

  
7
int hltp_send_string(int base_addr, const char *p){
8
    uint8_t* ptr[1]; ptr[0] = (uint8_t*)p;
9
    size_t    sz[1]; sz[0] = strlen(p)+1;
10
    return nctp_send(base_addr, 1, ptr, sz);
11
}
12

  
13
int hltp_get_string(int base_addr, char **p){
14
    return nctp_get(base_addr, (uint8_t**)p);
15
}
proj/src/interrupts_func.c
5 5
#include "i8254.h"
6 6
#include "keyboard.h"
7 7
#include "mouse.h"
8
#include "uart.h"
8
#include "nctp.h"
9 9
#include "utils.h"
10 10
#include "errors.h"
11 11

  
......
106 106
    uart_set_parity            (COM1_ADDR, uart_parity_even);
107 107
    uart_set_bit_rate          (COM1_ADDR, 38400);
108 108
    uart_enable_int_rx (COM1_ADDR);
109
    uart_disable_int_tx(COM1_ADDR);
109
    uart_enable_int_tx(COM1_ADDR);
110 110
    if(subscribe_uart_interrupt(COM1_IRQ, &uart_id)) {
111 111
        printf("%s: failed to subscribe UART interrupts.\n", __func__);
112 112
        return SBCR_ERROR;
113 113
    }
114 114
    uart_subscribed = 1;
115
    nctp_init();
115 116

  
116 117
    return SUCCESS;
117 118
}
......
168 169
        uart_enable_int_tx(COM1_ADDR);
169 170
        uart_subscribed = 0;
170 171
    }
172
    nctp_free();
171 173

  
172 174
    return r;
173 175
}
proj/src/list.c
52 52
list_node_t* (list_begin )(      list_t *l){ return l->begin_->n; }
53 53
list_node_t* (list_end   )(      list_t *l){ return l->end_; }
54 54
size_t       (list_size  )(const list_t *l){ return l->sz; }
55
int          (list_empty )(const list_t *l){ return !(l->sz); }
55 56
list_node_t* (list_insert)(list_t *l, list_node_t *position, void *val){
56 57
    list_node_t *node = list_node_ctor(position->p, position, val);
57 58
    position->p->n = node;
......
67 68
    --l->sz;
68 69
    return ret;
69 70
}
71
void         (list_push_back)(list_t *l, void *val){
72
    list_insert(l, list_end(l), val);
73
}
74
void**       (list_front)(list_t *l){
75
    return list_node_val(list_begin(l));
76
}
77
void         (list_pop_front)(list_t *l){
78
    list_erase(l, list_begin(l));
79
}
proj/src/proj.c
209 209
                                        buffer[buffer_pos] = '\0';
210 210
                                        printf("\nSending string -%s-", buffer);
211 211
                                        printf(" (output: %d)\n",
212
                                            hltp_send_string(COM1_ADDR, buffer));
212
                                            hltp_send_string(buffer));
213 213
                                        buffer_pos = 0;
214 214
                                    }
215 215
                                    else {
......
262 262
                            #endif
263 263
                            #ifdef DIOGO
264 264
                            case COM1_IRQ:
265
                                {
266
                                    printf("You've got mail");
267
                                    int r = hltp_get_string(COM1_ADDR, &s);
268
                                    printf(" (return code %d)", r);
269
                                    printf(": -%s-\n", s);
270
                                }break;
265
                                nctp_ih();
266
                                break;
271 267
                            #endif
272 268
                            }
273 269
                        }
proj/src/uart.c
2 2

  
3 3
#include "uart.h"
4 4

  
5
#include "queue.h"
5 6
#include "errors.h"
6 7

  
7 8
#define UART_BITRATE                            115200
......
78 79
#define UART_GET_FRAMING_ERROR                  (((n)&UART_FRAMING_ERROR            )>>UART_FRAMING_ERROR_POS            )
79 80
#define UART_GET_TRANSMITTER_EMPTY(n)           (((n)&UART_TRANSMITTER_EMPTY        )>>UART_TRANSMITTER_EMPTY_POS        )
80 81

  
82
/// IIR
83
#define UART_GET_IF_INT_PEND(n)                 (!((n)&1))
84
#define UART_GET_INT_PEND(n)                    (((n)&0xE)>>1)
85
#define UART_INT_RX                             0x2 //0b010
86
#define UART_INT_TX                             0x1 //0b001
87

  
81 88
int (subscribe_uart_interrupt)(uint8_t interrupt_bit, int *interrupt_id) {
82 89
    if (interrupt_id == NULL) return 1;
83 90
    *interrupt_id = interrupt_bit;
......
110 117
static int uart_get_lsr(int base_addr, uint8_t *p){
111 118
    return util_sys_inb(base_addr+UART_LSR, p);
112 119
}
120
static int uart_get_iir(int base_addr, uint8_t *p){
121
    return util_sys_inb(base_addr+UART_IIR, p);
122
}
113 123

  
114 124
static int uart_enable_divisor_latch(int base_addr){
115 125
    int ret = SUCCESS;
......
256 266
    return uart_set_ier(base_addr, ier);
257 267
}
258 268

  
259
/*static*/ int uart_has_communication_error(int base_addr){
260
    int ret;
261
    uint8_t lsr;
262
    if((ret = uart_get_lsr(base_addr, &lsr))) return 1;
263
    lsr &= (UART_OVERRUN_ERROR &
264
            UART_PARITY_ERROR  &
265
            UART_FRAMING_ERROR);
266
    return (lsr ? 1 : 0);
267
}
268

  
269 269
#include "nctp.h"
270 270

  
271 271
#define NCTP_START      0x80
272 272
#define NCTP_END        0xFF
273
#define NCTP_TRIES      3
274 273
#define NCTP_OK         0xFF
275 274
#define NCTP_NOK        0x00
276 275
#define NCTP_MAX_SIZE   1024 //in bytes
277 276

  
278
int nctp_send_char_poll(int base_addr, uint8_t  c){
279
    for(int i = 0; i < NCTP_TRIES; ++i)
280
        if(uart_transmitter_empty(base_addr))
281
            return uart_send_char(base_addr, c);
282
    return TIMEOUT_ERROR;
283
}
284
int nctp_get_char_poll (int base_addr, uint8_t *p){
285
    for(int i = 0; i < NCTP_TRIES; ++i)
286
        if(uart_receiver_ready(base_addr))
287
            return uart_get_char(base_addr, p);
288
    return TIMEOUT_ERROR;
289
}
277
queue_t *out = NULL;
278
queue_t *in  = NULL;
290 279

  
291
int nctp_expect_ok(int base_addr){
292
    int ret;
293
    uint8_t ok;
294
    if((ret = nctp_get_char_poll(base_addr, &ok))) return ret;
295
    int cnt = 0;
296
    while(ok){
297
        cnt += ok&1;
298
        ok >>= 1;
299
    }
300
    if(cnt > 4) return SUCCESS;
301
    else        return NOK;
280
int nctp_init(void){
281
    out = queue_ctor(); if(out == NULL) return NULL_PTR;
282
    in  = queue_ctor(); if(in  == NULL) return NULL_PTR;
283
    return SUCCESS;
302 284
}
303

  
304
int nctp_send_char_try(int base_addr, uint8_t  c){
305
    int ret;
306
    for(size_t k = 0; k < NCTP_TRIES; ++k){
307
        if((ret = nctp_send_char_poll(base_addr, c))) return ret;
308
        return SUCCESS;
309
        //if(nctp_expect_ok(base_addr) == SUCCESS) return SUCCESS;
285
int nctp_free(void){
286
    while(!queue_empty(out)){
287
        free(queue_top(out));
288
        queue_pop(out);
310 289
    }
311
    return TRANS_FAILED;
312
}
313
int nctp_get_char_try (int base_addr, uint8_t *p){
314
    int ret;
315
    for(size_t k = 0; k < NCTP_TRIES; ++k){
316
        if((ret = nctp_get_char_poll (base_addr, p))) return ret;
317
        return SUCCESS;
318
        //if(!uart_has_communication_error(base_addr))
319
            //return nctp_send_char_try(base_addr, NCTP_OK ); //If it does not have any errors
290
    while(!queue_empty(in)){
291
        free(queue_top(in));
292
        queue_pop(in);
320 293
    }
321
    if((ret = nctp_send_char_poll(base_addr, NCTP_NOK))) return ret;
322
    return TRANS_FAILED;
294
    return SUCCESS;
323 295
}
324 296

  
325
static int nctp_send_inner(int base_addr, size_t num, uint8_t* ptr[], size_t sz[]){
297
int nctp_send(size_t num, uint8_t* ptr[], size_t sz[]){
326 298
    {
327 299
        int cnt = 0;
328 300
        for(size_t i = 0; i < num; ++i){
......
330 302
            if(cnt > NCTP_MAX_SIZE) return TRANS_REFUSED;
331 303
        }
332 304
    }
333

  
334 305
    int ret;
335

  
336
    if((ret = nctp_send_char_try(base_addr, NCTP_START))) return ret;
306
    uint8_t *tmp;
307
    tmp = malloc(sizeof(uint8_t)); *tmp = NCTP_START; queue_push(out, tmp);
337 308
    for(size_t i = 0; i < num; ++i){
338 309
        uint8_t *p = ptr[i]; size_t s = sz[i];
339
        for(size_t j = 0; j < s; ++j, ++p)
340
            if((ret = nctp_send_char_try(base_addr, *p)))
341
                return ret;
310
        for(size_t j = 0; j < s; ++j, ++p){
311
            tmp = malloc(sizeof(uint8_t)); *tmp = *p; queue_push(out, tmp);
312
        }
342 313
    }
343
    if((ret = nctp_send_char_try(base_addr, NCTP_END))) return ret;
344

  
314
    tmp = malloc(sizeof(uint8_t)); *tmp = NCTP_END; queue_push(out, tmp);
315
    if(uart_transmitter_empty(COM1_ADDR)){
316
        if((ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out)))) return ret;
317
        queue_pop(out);
318
    }
345 319
    return SUCCESS;
346 320
}
347
int nctp_send(int base_addr, size_t num, uint8_t* ptr[], size_t sz[]){
348
    int ret;
349
    if((ret = uart_disable_int_rx(base_addr))) return ret;
350
    if((ret = uart_disable_int_tx(base_addr))) return ret;
351
    int r = nctp_send_inner(base_addr, num, ptr, sz);
352
    if((ret = uart_enable_int_rx(base_addr))) return ret;
353
    if((ret = uart_disable_int_tx(base_addr))) return ret;
354
    return r;
321

  
322
static int nctp_transmit(void){
323
    if(!queue_empty(out)){
324
        int ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out));
325
        queue_pop(out);
326
        return ret;
327
    }else return SUCCESS;
355 328
}
356

  
357
static int nctp_get_inner(int base_addr, uint8_t **dest){
329
static void process(){
330
    free(queue_top(in)); queue_pop(in);
331
    while(*(uint8_t*)queue_top(in) != NCTP_END){
332
        printf("%c", *(uint8_t*)queue_top(in));
333
        free(queue_top(in)); queue_pop(in);
334
    }
335
    free(queue_top(in)); queue_pop(in);
336
}
337
static int nctp_receive(void){
358 338
    int ret;
359
    free(*dest);
360
    *dest = malloc(NCTP_MAX_SIZE*sizeof(uint8_t)); size_t i = 0;
361
    if(*dest == NULL) return NULL_PTR;
362 339
    uint8_t c;
363
    if((ret = nctp_get_char_try (base_addr, &c     ))) return ret;
364
    while(true){
365
        if(i >= NCTP_MAX_SIZE) return TRANS_REFUSED;
366
        if((ret = nctp_get_char_try (base_addr, &c))) return ret;
367
        if(c == NCTP_END) return SUCCESS;
368
        else              (*dest)[i++] = c;
340
    while(uart_receiver_ready(COM1_ADDR)){
341
        if((ret = uart_get_char(COM1_ADDR, &c))) return ret;
342
        uint8_t *tmp = malloc(sizeof(uint8_t)); *tmp = c;
343
        queue_push(in, tmp);
344
        if(c == NCTP_END) process();
369 345
    }
346
    return SUCCESS;
370 347
}
371
int nctp_get(int base_addr, uint8_t **dest){
372
    int ret;
373
    if((ret = uart_disable_int_rx(base_addr))) return ret;
374
    if((ret = uart_disable_int_tx(base_addr))) return ret;
375
    int r = nctp_get_inner(base_addr, dest);
376
    if((ret = uart_enable_int_rx(base_addr))) return ret;
377
    if((ret = uart_disable_int_tx(base_addr))) return ret;
378
    return r;
348

  
349
int nctp_ih_err = SUCCESS;
350
void nctp_ih(void){
351
    uint8_t iir;
352
    if((nctp_ih_err = uart_get_iir(COM1_ADDR, &iir))) return;
353
    if(UART_GET_IF_INT_PEND(iir)){
354
        switch(UART_GET_INT_PEND(iir)){
355
            case UART_INT_RX: nctp_receive (); break;
356
            case UART_INT_TX: nctp_transmit(); break;
357
            default: break;
358
        }
359
    }
379 360
}
361

  
362
/// HLTP
363
int hltp_send_string(const char *p){
364
    uint8_t* ptr[1]; ptr[0] = (uint8_t*)p;
365
    size_t    sz[1]; sz[0] = strlen(p)+1;
366
    return nctp_send(1, ptr, sz);
367
}
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 test7.c uart.c hltp.c makecode_map.c
5
SRCS= proj.c list.c queue.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 uart.c hltp.c makecode_map.c
6 6

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

  
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 test7.c uart.c hltp.c makecode_map.c
5
SRCS= proj.c list.c queue.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 uart.c hltp.c makecode_map.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 test7.c uart.c hltp.c makecode_map.c
5
SRCS= proj.c list.c queue.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 uart.c hltp.c makecode_map.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