Revision 377
added fifos
proj/src/libs/uart/src/uart.c | ||
---|---|---|
81 | 81 |
|
82 | 82 |
/// IIR |
83 | 83 |
#define UART_INT_PEND_POS 1 |
84 |
|
|
85 | 84 |
#define UART_INT_PEND (BIT(3)|BIT(2)|BIT(1)) |
86 | 85 |
|
86 |
/// FCR |
|
87 |
#define UART_EN_FIFOS_POS 0 |
|
88 |
#define UART_CLEAR_RCVR_POS 1 |
|
89 |
#define UART_CLEAR_XMIT_POS 2 |
|
90 |
#define UART_FIFO_TRIGGER_POS 6 |
|
91 |
|
|
92 |
#define UART_EN_FIFOS (BIT(0)) |
|
93 |
#define UART_CLEAR_RCVR (BIT(1)) |
|
94 |
#define UART_CLEAR_XMIT (BIT(2)) |
|
95 |
#define UART_FIFO_TRIGGER (BIT(6)|BIT(7)) |
|
96 |
|
|
97 |
#define UART_TRIGGER_LEVEL01 (0<<UART_FIFO_TRIGGER_POS) |
|
98 |
#define UART_TRIGGER_LEVEL04 (1<<UART_FIFO_TRIGGER_POS) |
|
99 |
#define UART_TRIGGER_LEVEL08 (2<<UART_FIFO_TRIGGER_POS) |
|
100 |
#define UART_TRIGGER_LEVEL14 (3<<UART_FIFO_TRIGGER_POS) |
|
101 |
|
|
87 | 102 |
#define UART_GET_IF_INT_PEND(n) (!((n)&1)) |
88 | 103 |
typedef enum { |
89 | 104 |
uart_int_receiver_line_stat = ( BIT(1) | BIT(0)), |
... | ... | |
276 | 291 |
return uart_set_ier(base_addr, ier); |
277 | 292 |
} |
278 | 293 |
|
294 |
static int uart_set_fcr(int base_addr, uint8_t n){ |
|
295 |
if(sys_outb(base_addr+UART_FCR, n)) return WRITE_ERROR; |
|
296 |
return SUCCESS; |
|
297 |
} |
|
298 |
static int uart_enable_fifos(int base_addr, uint8_t trigger_lvl){ |
|
299 |
uint8_t fcr = UART_EN_FIFOS | UART_CLEAR_RCVR | UART_CLEAR_XMIT; |
|
300 |
switch(trigger_lvl){ |
|
301 |
case 1: fcr |= UART_TRIGGER_LEVEL01; break; |
|
302 |
case 4: fcr |= UART_TRIGGER_LEVEL04; break; |
|
303 |
case 8: fcr |= UART_TRIGGER_LEVEL08; break; |
|
304 |
case 14: fcr |= UART_TRIGGER_LEVEL14; break; |
|
305 |
default: return INVALID_ARG; |
|
306 |
} |
|
307 |
return uart_set_fcr(base_addr, fcr); |
|
308 |
} |
|
309 |
static int uart_disable_fifos(int base_addr){ |
|
310 |
return uart_set_fcr(base_addr, UART_CLEAR_RCVR | UART_CLEAR_XMIT); |
|
311 |
} |
|
312 |
|
|
279 | 313 |
/// NCTP |
280 | 314 |
|
281 | 315 |
//#define NCTP_START 0x80 |
282 | 316 |
//#define NCTP_END 0xFF |
283 | 317 |
#define NCTP_OK 0xFF |
284 | 318 |
#define NCTP_NOK 0x00 |
319 |
#define NCTP_ALIGN 8 |
|
320 |
#define NCTP_FILLER 0x4E |
|
285 | 321 |
|
286 | 322 |
static queue_t *out = NULL; |
287 | 323 |
static queue_t *in = NULL; |
288 | 324 |
static void (*process)(const uint8_t*, const size_t) = NULL; |
289 | 325 |
|
290 | 326 |
int nctp_init(void){ |
291 |
//int r; |
|
292 | 327 |
out = queue_ctor(); if(out == NULL) return NULL_PTR; |
293 | 328 |
in = queue_ctor(); if(in == NULL) return NULL_PTR; |
294 |
//if((r = uart_enable_fifos(COM1_ADDR))) return r; |
|
295 |
//if((r = uart_set_rcvr_trigger_level(COM1_ADDR, 8))) return r; |
|
296 |
return SUCCESS; |
|
329 |
//return SUCCESS; |
|
330 |
return uart_enable_fifos(COM1_ADDR, NCTP_ALIGN); |
|
297 | 331 |
} |
298 | 332 |
int nctp_dump(void){ |
299 | 333 |
int ret; |
... | ... | |
305 | 339 |
return SUCCESS; |
306 | 340 |
} |
307 | 341 |
int nctp_free(void){ |
342 |
int ret = SUCCESS; int r; |
|
308 | 343 |
while(!queue_empty(out)){ |
309 | 344 |
free(queue_top(out)); |
310 | 345 |
queue_pop(out); |
311 |
} |
|
346 |
} queue_dtor(out);
|
|
312 | 347 |
while(!queue_empty(in)){ |
313 | 348 |
free(queue_top(in)); |
314 | 349 |
queue_pop(in); |
315 |
} |
|
316 |
return SUCCESS; |
|
350 |
} queue_dtor(in); |
|
351 |
if((r = uart_disable_fifos(COM1_ADDR))) ret = r; |
|
352 |
return ret; |
|
317 | 353 |
} |
318 | 354 |
|
319 | 355 |
int nctp_send(size_t num, const uint8_t *const *ptr, const size_t *const sz){ |
... | ... | |
331 | 367 |
tmp = malloc(sizeof(uint8_t)); *tmp = *p; queue_push(out, tmp); |
332 | 368 |
} |
333 | 369 |
} |
370 |
uint32_t total_message = sz_total+2; |
|
371 |
uint32_t num_fillers = (NCTP_ALIGN - total_message%NCTP_ALIGN)%NCTP_ALIGN; |
|
372 |
for(size_t i = 0; i < num_fillers; ++i){ |
|
373 |
tmp = malloc(sizeof(uint8_t)); *tmp = NCTP_FILLER; queue_push(out, tmp); |
|
374 |
} |
|
334 | 375 |
|
335 | 376 |
if(uart_transmitter_empty(COM1_ADDR)){ |
336 | 377 |
if((ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out)))) return ret; |
... | ... | |
339 | 380 |
return SUCCESS; |
340 | 381 |
} |
341 | 382 |
static int nctp_transmit(void){ |
342 |
if(!queue_empty(out)){
|
|
383 |
while(!queue_empty(out) && uart_transmitter_empty(COM1_ADDR)){
|
|
343 | 384 |
int ret = uart_send_char(COM1_ADDR, *(uint8_t*)queue_top(out)); |
344 | 385 |
queue_pop(out); |
345 |
return ret; |
|
346 |
}else return SUCCESS; |
|
386 |
if(ret) return ret; |
|
387 |
//return ret; |
|
388 |
}/*else*/ return SUCCESS; |
|
347 | 389 |
} |
348 | 390 |
|
349 | 391 |
static void nctp_process_received(){ |
... | ... | |
365 | 407 |
static int num_bytes_to_receive = 0; |
366 | 408 |
static uint16_t szbytes_to_receive = 0; |
367 | 409 |
static uint8_t size0 = 0; |
410 |
static uint8_t received_so_far = 0; |
|
368 | 411 |
static int nctp_receive(void){ |
369 | 412 |
int ret; |
370 | 413 |
uint8_t c; |
... | ... | |
374 | 417 |
if((ret = uart_get_char(COM1_ADDR, &c))) return ret; |
375 | 418 |
uint8_t *tmp = malloc(sizeof(uint8_t)); *tmp = c; |
376 | 419 |
|
377 |
queue_push(in, tmp); |
|
378 |
|
|
379 | 420 |
if (szbytes_to_receive){ // gotta receive 2nd size byte and update num_bytes |
380 | 421 |
*((uint8_t*)(&num_bytes_to_receive)+0) = size0; |
381 | 422 |
*((uint8_t*)(&num_bytes_to_receive)+1) = c; |
382 | 423 |
szbytes_to_receive = 0; |
383 | 424 |
} else if(num_bytes_to_receive > 0){ |
384 | 425 |
/* Now I know there are no more size bytes to receive. |
385 |
* If there are normal bytes to receive*/ |
|
426 |
* If there are normal bytes to receive, do this*/
|
|
386 | 427 |
--num_bytes_to_receive; |
387 | 428 |
if(num_bytes_to_receive == 0) ++counter_to_process; |
388 | 429 |
} else { |
389 | 430 |
/* Now I know I am not expecting anything. |
390 |
* The fact I received something means it is the 1st size byte */ |
|
391 |
size0 = c; |
|
392 |
szbytes_to_receive = 1; |
|
431 |
* The fact I received something means it is a filler, or the 1st size byte. |
|
432 |
* If received_so_far == 0, then it is not a filler, but rather the 1st size byte. */ |
|
433 |
if(received_so_far == 0){ |
|
434 |
size0 = c; |
|
435 |
szbytes_to_receive = 1; |
|
436 |
}else{ |
|
437 |
received_so_far = (received_so_far+1)%NCTP_ALIGN; |
|
438 |
continue; |
|
439 |
} |
|
393 | 440 |
} |
441 |
queue_push(in, tmp); |
|
442 |
received_so_far = (received_so_far+1)%NCTP_ALIGN; |
|
394 | 443 |
} |
395 | 444 |
while(counter_to_process-- > 0) nctp_process_received(); |
396 | 445 |
return SUCCESS; |
proj/src/project/src/chat.c | ||
---|---|---|
20 | 20 |
static rectangle_t *r_text = NULL; |
21 | 21 |
|
22 | 22 |
static void chat_process(const uint8_t *p, const size_t sz){ |
23 |
char buffer2[CHAT_MAX_NUM+3];
|
|
23 |
char buffer2[CHAT_MAX_SIZE+3];
|
|
24 | 24 |
void *dest = NULL; |
25 | 25 |
hltp_type tp = hltp_interpret(p, sz, &dest); |
26 | 26 |
switch(tp){ |
proj/src/project/src/interrupts_func.c | ||
---|---|---|
115 | 115 |
uart_set_bits_per_character(COM1_ADDR, 8); |
116 | 116 |
uart_set_stop_bits (COM1_ADDR, 2); |
117 | 117 |
uart_set_parity (COM1_ADDR, uart_parity_even); |
118 |
uart_set_bit_rate (COM1_ADDR, 115200);
|
|
118 |
uart_set_bit_rate (COM1_ADDR, 19200);
|
|
119 | 119 |
uart_enable_int_rx (COM1_ADDR); |
120 | 120 |
uart_enable_int_tx(COM1_ADDR); |
121 | 121 |
if(subscribe_uart_interrupt(COM1_IRQ, &uart_id)) { |
Also available in: Unified diff