Revision 277
added queue. implemented working queues
proj/include/queue.h | ||
---|---|---|
1 |
#ifndef QUEUE_H_INCLUDED |
|
2 |
#define QUEUE_H_INCLUDED |
|
3 |
|
|
4 |
struct queue; |
|
5 |
typedef struct queue queue_t; |
|
6 |
|
|
7 |
const size_t queue_max_size; |
|
8 |
|
|
9 |
queue_t* (queue_ctor )(void); |
|
10 |
int (queue_dtor )(queue_t *q); |
|
11 |
size_t (queue_size )(const queue_t *q); |
|
12 |
int (queue_empty)(const queue_t *q); |
|
13 |
void (queue_push )(queue_t *q, void *val); |
|
14 |
void* (queue_top )(const queue_t *q); |
|
15 |
void (queue_pop )(queue_t *q); |
|
16 |
|
|
17 |
#endif //QUEUE_H_INCLUDED |
|
0 | 18 |
proj/src/queue.c | ||
---|---|---|
1 |
#include <lcom/lcf.h> |
|
2 |
|
|
3 |
#include "queue.h" |
|
4 |
|
|
5 |
#include "list.h" |
|
6 |
#include "errors.h" |
|
7 |
|
|
8 |
const size_t queue_max_size = UINT_MAX; |
|
9 |
|
|
10 |
struct queue{ |
|
11 |
list_t *l; |
|
12 |
}; |
|
13 |
|
|
14 |
queue_t* (queue_ctor )(void){ |
|
15 |
queue_t *ret = malloc(sizeof(queue_t)); |
|
16 |
if(ret == NULL) return NULL; |
|
17 |
ret->l = list_ctor(); |
|
18 |
if(ret->l == NULL){ |
|
19 |
queue_dtor(ret); |
|
20 |
return NULL; |
|
21 |
} |
|
22 |
return ret; |
|
23 |
} |
|
24 |
int (queue_dtor )(queue_t *q){ |
|
25 |
int r; |
|
26 |
if((r = list_dtor(q->l))) return r; |
|
27 |
free(q); |
|
28 |
return SUCCESS; |
|
29 |
} |
|
30 |
size_t (queue_size )(const queue_t *q ){ return list_size (q->l); } |
|
31 |
int (queue_empty)(const queue_t *q ){ return list_empty(q->l); } |
|
32 |
void (queue_push )( queue_t *q, void *val){ list_push_back(q->l, val); } |
|
33 |
void* (queue_top )(const queue_t *q ){ return *list_front(q->l); } |
|
34 |
void (queue_pop )( queue_t *q ){ list_pop_front(q->l); } |
|
0 | 35 |
proj/src/uart.c | ||
---|---|---|
23 | 23 |
#define UART_DLM 1 |
24 | 24 |
|
25 | 25 |
/// LCR |
26 |
//#define UART_BITS_PER_CHAR_POS 0
|
|
26 |
#define UART_BITS_PER_CHAR_POS 0 |
|
27 | 27 |
#define UART_STOP_BITS_POS 2 |
28 | 28 |
#define UART_PARITY_POS 3 |
29 | 29 |
#define UART_BREAK_CONTROL_POS 6 |
... | ... | |
80 | 80 |
#define UART_GET_TRANSMITTER_EMPTY(n) (((n)&UART_TRANSMITTER_EMPTY )>>UART_TRANSMITTER_EMPTY_POS ) |
81 | 81 |
|
82 | 82 |
/// IIR |
83 |
#define UART_INT_PEND_POS 1 |
|
84 |
|
|
85 |
#define UART_INT_PEND (BIT(3)|BIT(2)|BIT(1)) |
|
86 |
|
|
83 | 87 |
#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 |
|
88 |
typedef enum { |
|
89 |
uart_int_receiver_line_stat = ( BIT(1) | BIT(0)), |
|
90 |
uart_int_rx = ( BIT(1) ), |
|
91 |
uart_int_char_timeout_fifo = (BIT(2) | BIT(1) ), |
|
92 |
uart_int_tx = ( BIT(0)), |
|
93 |
uart_int_modem_stat = (0) |
|
94 |
} uart_int_code; |
|
95 |
#define UART_GET_INT_PEND(n) ((uart_int_code)(((n)&UART_INT_PEND)>>UART_INT_PEND_POS)) |
|
87 | 96 |
|
88 | 97 |
int (subscribe_uart_interrupt)(uint8_t interrupt_bit, int *interrupt_id) { |
89 | 98 |
if (interrupt_id == NULL) return 1; |
... | ... | |
272 | 281 |
#define NCTP_END 0xFF |
273 | 282 |
#define NCTP_OK 0xFF |
274 | 283 |
#define NCTP_NOK 0x00 |
275 |
#define NCTP_MAX_SIZE 1024 //in bytes |
|
276 | 284 |
|
277 | 285 |
queue_t *out = NULL; |
278 | 286 |
queue_t *in = NULL; |
... | ... | |
296 | 304 |
|
297 | 305 |
int nctp_send(size_t num, uint8_t* ptr[], size_t sz[]){ |
298 | 306 |
{ |
299 |
int cnt = 0;
|
|
307 |
size_t cnt = 0;
|
|
300 | 308 |
for(size_t i = 0; i < num; ++i){ |
301 | 309 |
cnt += sz[i]; |
302 |
if(cnt > NCTP_MAX_SIZE) return TRANS_REFUSED;
|
|
310 |
if(cnt > queue_max_size) return TRANS_REFUSED;
|
|
303 | 311 |
} |
304 | 312 |
} |
305 | 313 |
int ret; |
... | ... | |
337 | 345 |
static int nctp_receive(void){ |
338 | 346 |
int ret; |
339 | 347 |
uint8_t c; |
348 |
int num_ends = 0; |
|
340 | 349 |
while(uart_receiver_ready(COM1_ADDR)){ |
341 | 350 |
if((ret = uart_get_char(COM1_ADDR, &c))) return ret; |
342 | 351 |
uint8_t *tmp = malloc(sizeof(uint8_t)); *tmp = c; |
343 | 352 |
queue_push(in, tmp); |
344 |
if(c == NCTP_END) process();
|
|
353 |
if(c == NCTP_END) ++num_ends;
|
|
345 | 354 |
} |
355 |
while(num_ends-- > 0) process(); |
|
346 | 356 |
return SUCCESS; |
347 | 357 |
} |
348 | 358 |
|
... | ... | |
352 | 362 |
if((nctp_ih_err = uart_get_iir(COM1_ADDR, &iir))) return; |
353 | 363 |
if(UART_GET_IF_INT_PEND(iir)){ |
354 | 364 |
switch(UART_GET_INT_PEND(iir)){ |
355 |
case UART_INT_RX: nctp_receive (); break;
|
|
356 |
case UART_INT_TX: nctp_transmit(); break;
|
|
365 |
case uart_int_rx: nctp_receive (); break;
|
|
366 |
case uart_int_tx: nctp_transmit(); break;
|
|
357 | 367 |
default: break; |
358 | 368 |
} |
359 | 369 |
} |
Also available in: Unified diff