Index: proj/include/uart_macros.h
===================================================================
--- proj/include/uart_macros.h	(revision 248)
+++ proj/include/uart_macros.h	(nonexistent)
@@ -1,21 +0,0 @@
-#ifndef UART_MACROS_H_INCLUDED
-#define UART_MACROS_H_INCLUDED
-
-#define UART_BITRATE        115200
-
-#define COM1_ADDR           0x3F8
-#define COM2_ADDR           0x2F8
-#define COM1_IRQ            4
-#define COM2_IRQ            3
-#define COM1_VECTOR         0x0C
-#define COM2_VECTOR         0x0B
-
-typedef enum {
-    uart_parity_none = 0x0,
-    uart_parity_odd  = 0x1,
-    uart_parity_even = 0x3,
-    uart_parity_par1 = 0x5,
-    uart_parity_par0 = 0x7
-} uart_parity;
-
-#endif //UART_MACROS_H_INCLUDED

Property changes on: proj/include/uart_macros.h
___________________________________________________________________
Deleted: svn:executable
## -1 +0,0 ##
-*
\ No newline at end of property
Index: proj/include/test7.h
===================================================================
--- proj/include/test7.h	(revision 248)
+++ proj/include/test7.h	(revision 249)
@@ -1,3 +1,8 @@
+#ifndef TEST7_H_INCLUDED
+#define TEST7_H_INCLUDED
+
+#include "uart.h"
+
 int ser_test_conf(unsigned short base_addr);
 
 int ser_test_set(unsigned short base_addr, unsigned long bits, unsigned long stop,
@@ -9,3 +14,5 @@
 int ser_test_int(/* details to be provided */) ;
 
 int ser_test_fifo(/* details to be provided */);
+
+#endif //TEST7_H_INCLUDED
Index: proj/include/uart.h
===================================================================
--- proj/include/uart.h	(revision 248)
+++ proj/include/uart.h	(revision 249)
@@ -1,8 +1,21 @@
 #ifndef UART_H_INCLUDED
 #define UART_H_INCLUDED
 
-#include "uart_macros.h"
+#define COM1_ADDR           0x3F8
+#define COM2_ADDR           0x2F8
+#define COM1_IRQ            4
+#define COM2_IRQ            3
+#define COM1_VECTOR         0x0C
+#define COM2_VECTOR         0x0B
 
+typedef enum {
+    uart_parity_none = 0x0,
+    uart_parity_odd  = 0x1,
+    uart_parity_even = 0x3,
+    uart_parity_par1 = 0x5,
+    uart_parity_par0 = 0x7
+} uart_parity;
+
 typedef struct {
     int     base_addr               ;
     uint8_t lcr                     ;
@@ -10,7 +23,7 @@
     uint8_t dlm                     ;
     uint8_t bits_per_char           ;
     uint8_t stop_bits               ;
-    uint8_t parity                  ;
+    uart_parity parity              ;
     uint8_t break_control         :1;
     uint8_t dlab                  :1;
     uint16_t divisor_latch          ;
@@ -34,4 +47,8 @@
 int uart_set_parity            (int base_addr, uart_parity par          );
 int uart_set_bit_rate          (int base_addr, float       bit_rate     );
 
+int uart_get_char_poll   (int base_addr, uint8_t *p);
+int uart_send_char_poll  (int base_addr, uint8_t  c);
+int uart_send_memory_poll(int base_addr, void *str, size_t n);
+
 #endif //UART_H_INCLUDED
Index: proj/src/proj.c
===================================================================
--- proj/src/proj.c	(revision 248)
+++ proj/src/proj.c	(revision 249)
@@ -30,7 +30,6 @@
 #include "list.h"
 
 #ifdef DIOGO
-    #include "uart_macros.h"
     #include "test7.h"
 #endif
 
@@ -159,14 +158,15 @@
         //UART
         unsigned long bits = 8;
         unsigned long stop = 2;
-        long parity = -1;
+        long parity = 0;
         unsigned long rate = 9600;
-        unsigned char tx = 0;
+        unsigned char tx = 1;
         int stringc = 2;
-        char *strings[] = {"Hello", "world"};
-        if((r = ser_test_conf(COM1_ADDR))) return r;
-        if((r = ser_test_set(COM1_ADDR, bits, stop, parity, rate))) return r;
+        char *strings[] = {"Hello", "world"}; (void)strings;
+        //if((r = ser_test_conf(COM1_ADDR))) return r;
+        //if((r = ser_test_set(COM1_ADDR, bits, stop, parity, rate))) return r;
         if((r = ser_test_poll(COM1_ADDR, tx, bits, stop, parity, rate, stringc, strings))) return r;
+        printf("DONE\n");
     #endif
     #ifndef DIOGO
         ent_set_scale(DEFAULT_SCALE);
Index: proj/src/test7.c
===================================================================
--- proj/src/test7.c	(revision 248)
+++ proj/src/test7.c	(revision 249)
@@ -1,7 +1,6 @@
 #include <lcom/lcf.h>
 
 #include "errors.h"
-#include "uart_macros.h"
 #include "uart.h"
 
 int ser_test_conf(unsigned short base_addr) {
@@ -36,24 +35,20 @@
 	int ret = SUCCESS;
     if((ret = ser_test_set(base_addr, bits, stop, parity, rate))) return ret;
 	if(tx == 0){
-		/*
-		char c;
-		if((ret = uart_get_char(base_addr, &c))) return ret;
-		while(c != '.'){
-			printf("%c", c);
-			if((ret = uart_get_char(base_addr, &c))) return ret;
+		uint8_t c;
+		if((ret = uart_get_char_poll(base_addr, &c))) return ret;
+		while((char)c != '.'){
+			printf("%c", (char)c);
+			if((ret = uart_get_char_poll(base_addr, &c))) return ret;
 		}
-		*/
+		printf("%c\n", (char)c);
 	}else{
-		/*
 		for(int i = 0; i < stringc; ++i){
-			int j = 0;
-			while(stringc[i][j] != 0)
-				if((ret = uart_send_char(base_addr, stringc[i][j]))) return ret;
-			if(i+1 != stringc) if((ret = uart_send_char(base_addr, ' '))) return ret;
+			size_t sz = strlen(strings[i]);
+			if((ret = uart_send_memory_poll(base_addr, strings[i], sz))) return ret;
+			if(i+1 != stringc) if((ret = uart_send_memory_poll(base_addr, " ", 1))) return ret;
 		}
-		if((ret = uart_send_char(base_addr, '.'))) return ret;
-		*/
+		if((ret = uart_send_memory_poll(base_addr, ".", 1))) return ret;
 	}
 	return SUCCESS;
 }
Index: proj/src/uart.c
===================================================================
--- proj/src/uart.c	(revision 248)
+++ proj/src/uart.c	(revision 249)
@@ -4,56 +4,71 @@
 
 #include "errors.h"
 
-#define UART_RBR            0
-#define UART_THR            0
-#define UART_IER            1
-#define UART_IIR            2
-#define UART_FCR            2
-#define UART_LCR            3
-#define UART_MCR            4
-#define UART_LSR            5
-#define UART_MSR            6
-#define UART_SR             7
+#define UART_BITRATE                            115200
+#define UART_WAIT                               20 //microseconds
 
-#define UART_DLL            0
-#define UART_DLM            1
+#define UART_RBR                                0
+#define UART_THR                                0
+#define UART_IER                                1
+#define UART_IIR                                2
+#define UART_FCR                                2
+#define UART_LCR                                3
+#define UART_MCR                                4
+#define UART_LSR                                5
+#define UART_MSR                                6
+#define UART_SR                                 7
 
-#define UART_BITS_PER_CHAR_POS 0
-#define UART_STOP_BITS_POS     2
-#define UART_PARITY_POS        3
-#define UART_BREAK_CONTROL_POS 6
-#define UART_DLAB_POS          7
+#define UART_DLL                                0
+#define UART_DLM                                1
 
-#define UART_BITS_PER_CHAR  (BIT(0) | BIT(1))
-#define UART_STOP_BITS      (BIT(2))
-#define UART_PARITY         (BIT(3) | BIT(4) | BIT(5))
-#define UART_BREAK_CONTROL  (BIT(6))
-#define UART_DLAB           (BIT(7))
+/// LCR
+#define UART_BITS_PER_CHAR_POS                  0
+#define UART_STOP_BITS_POS                      2
+#define UART_PARITY_POS                         3
+#define UART_BREAK_CONTROL_POS                  6
+#define UART_DLAB_POS                           7
 
-#define UART_GET_BITS_PER_CHAR(n) (((n)&UART_BITS_PER_CHAR) + 5)
-#define UART_GET_STOP_BITS(n)     (((n)&UART_STOP_BITS)? 2 : 1)
-#define UART_GET_PARITY(n)        (((n)&UART_PARITY       )>>UART_PARITY_POS       )
-#define UART_GET_BREAK_CONTROL(n) (((n)&UART_BREAK_CONTROL)>>UART_BREAK_CONTROL_POS)
-#define UART_GET_DLAB(n)          (((n)&UART_DLAB         )>>UART_DLAB_POS         )
-#define UART_GET_DIV_LATCH(m,l)   ((m)<<8 | (l))
-#define UART_GET_DLL(n)           ((n)&0xFF)
-#define UART_GET_DLM(n)           (((n)>>8)&0xFF)
+#define UART_BITS_PER_CHAR                      (BIT(0) | BIT(1))
+#define UART_STOP_BITS                          (BIT(2))
+#define UART_PARITY                             (BIT(3) | BIT(4) | BIT(5))
+#define UART_BREAK_CONTROL                      (BIT(6))
+#define UART_DLAB                               (BIT(7))
 
-#define UART_RECEIVED_DATA_POS      0
-#define UART_TRANSMITTER_EMPTY_POS  1
-#define UART_RECEIVER_LINE_STAT_POS 2
-#define UART_MODEM_STAT_POS         3
+#define UART_GET_BITS_PER_CHAR(n)               (((n)&UART_BITS_PER_CHAR) + 5)
+#define UART_GET_STOP_BITS(n)                   (((n)&UART_STOP_BITS)? 2 : 1)
+#define UART_GET_PARITY(n)                      (((n)&UART_PARITY       )>>UART_PARITY_POS       )
+#define UART_GET_BREAK_CONTROL(n)               (((n)&UART_BREAK_CONTROL)>>UART_BREAK_CONTROL_POS)
+#define UART_GET_DLAB(n)                        (((n)&UART_DLAB         )>>UART_DLAB_POS         )
+#define UART_GET_DIV_LATCH(m,l)                 ((m)<<8 | (l))
+#define UART_GET_DLL(n)                         ((n)&0xFF)
+#define UART_GET_DLM(n)                         (((n)>>8)&0xFF)
 
-#define UART_RECEIVED_DATA          (BIT(0))
-#define UART_TRANSMITTER_EMPTY      (BIT(1))
-#define UART_RECEIVER_LINE_STAT     (BIT(2))
-#define UART_MODEM_STAT             (BIT(3))
+/// IER
+#define UART_INT_EN_RECEIVED_DATA_POS           0
+#define UART_INT_EN_TRANSMITTER_EMPTY_POS       1
+#define UART_INT_EN_RECEIVER_LINE_STAT_POS      2
+#define UART_INT_EN_MODEM_STAT_POS              3
 
-#define UART_GET_RECEIVED_DATA(n)       (((n)&UART_RECEIVED_DATA     )>>UART_RECEIVED_DATA_POS     )
-#define UART_GET_TRANSMITTER_EMPTY(n)   (((n)&UART_TRANSMITTER_EMPTY )>>UART_TRANSMITTER_EMPTY_POS )
-#define UART_GET_RECEIVER_LINE_STAT(n)  (((n)&UART_RECEIVER_LINE_STAT)>>UART_RECEIVER_LINE_STAT_POS)
-#define UART_GET_MODEM_STAT(n)          (((n)&UART_MODEM_STAT        )>>UART_MODEM_STAT_POS        )
+#define UART_INT_EN_RECEIVED_DATA               (BIT(0))
+#define UART_INT_EN_TRANSMITTER_EMPTY           (BIT(1))
+#define UART_INT_EN_RECEIVER_LINE_STAT          (BIT(2))
+#define UART_INT_EN_MODEM_STAT                  (BIT(3))
 
+#define UART_INT_EN_GET_RECEIVED_DATA(n)        (((n)&UART_INT_EN_RECEIVED_DATA     )>>UART_INT_EN_RECEIVED_DATA_POS     )
+#define UART_INT_EN_GET_TRANSMITTER_EMPTY(n)    (((n)&UART_INT_EN_TRANSMITTER_EMPTY )>>UART_INT_EN_TRANSMITTER_EMPTY_POS )
+#define UART_INT_EN_GET_RECEIVER_LINE_STAT(n)   (((n)&UART_INT_EN_RECEIVER_LINE_STAT)>>UART_INT_EN_RECEIVER_LINE_STAT_POS)
+#define UART_INT_EN_GET_MODEM_STAT(n)           (((n)&UART_INT_EN_MODEM_STAT        )>>UART_INT_EN_MODEM_STAT_POS        )
+
+/// LSR
+#define UART_RECEIVER_READY_POS                 0
+#define UART_TRANSMITTER_EMPTY_POS              5
+
+#define UART_RECEIVER_READY                     (BIT(0))
+#define UART_TRANSMITTER_EMPTY                  (BIT(5))
+
+#define UART_GET_RECEIVER_READY(n)              (((n)&UART_RECEIVER_READY           )>>UART_RECEIVER_READY_POS           )
+#define UART_GET_TRANSMITTER_EMPTY(n)           (((n)&UART_TRANSMITTER_EMPTY        )>>UART_TRANSMITTER_EMPTY_POS        )
+
 int uart_get_config(int base_addr, uart_config *config){
     int ret = SUCCESS;
 
@@ -75,42 +90,33 @@
     /// LCR
     config->bits_per_char          = UART_GET_BITS_PER_CHAR     (config->lcr);
     config->stop_bits              = UART_GET_STOP_BITS         (config->lcr);
-    config->parity                 = UART_GET_PARITY            (config->lcr);
+    config->parity                 = UART_GET_PARITY            (config->lcr); if((config->parity & BIT(0)) == 0) config->parity = uart_parity_none;
     config->break_control          = UART_GET_BREAK_CONTROL     (config->lcr);
     config->dlab                   = UART_GET_DLAB              (config->lcr);
     /// IER
-    config->received_data_int      = UART_GET_RECEIVED_DATA     (config->ier);
-    config->transmitter_empty_int  = UART_GET_TRANSMITTER_EMPTY (config->ier);
-    config->receiver_line_stat_int = UART_GET_RECEIVER_LINE_STAT(config->ier);
-    config->modem_stat_int         = UART_GET_MODEM_STAT        (config->ier);
+    config->received_data_int      = UART_INT_EN_GET_RECEIVED_DATA     (config->ier);
+    config->transmitter_empty_int  = UART_INT_EN_GET_TRANSMITTER_EMPTY (config->ier);
+    config->receiver_line_stat_int = UART_INT_EN_GET_RECEIVER_LINE_STAT(config->ier);
+    config->modem_stat_int         = UART_INT_EN_GET_MODEM_STAT        (config->ier);
     /// DIV LATCH
     config->divisor_latch          = UART_GET_DIV_LATCH(config->dlm, config->dll);
 }
 void uart_print_config(uart_config config){
-    printf("Base address                                   : 0x%X\n", config.base_addr);
-    printf("    LCR configuration                          : 0x%02X\n", config.lcr);
-    printf("        Number of bits per char                : %d\n", config.bits_per_char);
-    printf("        Number of stop bits                    : %d\n", config.stop_bits);
-    printf("        Parity                                 : ");
-    if((config.parity&1) == 0) printf("no parity\n");
+
+    printf("%s configuration:\n", (config.base_addr == COM1_ADDR ? "COM1" : "COM2"));
+    printf("\tLCR = 0x%X: %d bits per char\t %d stop bits\t", config.lcr, config.bits_per_char, config.stop_bits);
+    if((config.parity&BIT(0)) == 0) printf("NO parity\n");
     else switch(config.parity){
-        case uart_parity_odd : printf("odd parity\n"     ); break;
-        case uart_parity_even: printf("even parity\n"    ); break;
+        case uart_parity_odd : printf("ODD parity\n"     ); break;
+        case uart_parity_even: printf("EVEN parity\n"    ); break;
         case uart_parity_par1: printf("parity bit is 1\n"); break;
         case uart_parity_par0: printf("parity bit is 0\n"); break;
         default              : printf("invalid\n"        ); break;
     }
-    printf("    Break control                              : %d\n", config.break_control);
-    printf("    DLAB                                       : %d\n", config.dlab);
-
-    printf("    IER configuration                          : 0x%02X\n", config.ier);
-    printf("        Received data interrupts enabled       : %d\n", config.received_data_int);
-    printf("        Transmitter empty interrupts enabled   : %d\n", config.transmitter_empty_int);
-    printf("        Receiver line status interrupts enabled: %d\n", config.receiver_line_stat_int);
-    printf("        Modem status interrupts enabled        : %d\n", config.modem_stat_int);
-
-    printf("    Divisor latch (DLM & DLL)                  : %d\n", config.divisor_latch);
-    printf("        Bit rate                               : %d\n", UART_BITRATE/config.divisor_latch);
+    printf("\tDLM = 0x%02X DLL=0x%02X: bitrate = %d bps\n", config.dlm, config.dll, UART_BITRATE/config.divisor_latch);
+    printf("\tIER = 0x%02X: Rx interrupts: %s\tTx interrupts: %s\n", config.ier,
+        (config.received_data_int     ? "ENABLED":"DISABLED"),
+        (config.transmitter_empty_int ? "ENABLED":"DISABLED"));
 }
 
 int uart_enable_divisor_latch(int base_addr){
@@ -160,6 +166,56 @@
     if((ret = uart_enable_divisor_latch(base_addr))) return ret;
     if(sys_outb(base_addr+UART_DLL, dll)) return WRITE_ERROR;
     if(sys_outb(base_addr+UART_DLM, dlm)) return WRITE_ERROR;
-    if((ret = util_sys_inb(base_addr+UART_DLM, &dlm))) return ret;
+    if((ret = uart_disable_divisor_latch(base_addr))) return ret;
     return SUCCESS;
 }
+
+/// PRIVATE
+int uart_get_lsr(int base_addr, uint8_t *p){
+    return util_sys_inb(base_addr+UART_LSR, p);
+}
+/**
+ * @brief Get char from RBR.
+ */
+int uart_get_char(int base_addr, uint8_t *p){
+    int ret;
+    if((ret = uart_disable_divisor_latch(base_addr))) return ret;
+    return util_sys_inb(base_addr+UART_RBR, p);
+}
+int uart_send_char(int base_addr, uint8_t c){
+    int ret;
+    if((ret = uart_disable_divisor_latch(base_addr))) return ret;
+    if(sys_outb(base_addr, c)) return WRITE_ERROR;
+    return SUCCESS;
+}
+int uart_receiver_ready(int base_addr){
+    uint8_t lsr;
+    if(uart_get_lsr(base_addr, &lsr)) return false;
+    return UART_GET_RECEIVER_READY(lsr);
+}
+int uart_transmitter_empty(int base_addr){
+    uint8_t lsr;
+    if(uart_get_lsr(base_addr, &lsr)) return false;
+    return UART_GET_TRANSMITTER_EMPTY(lsr);
+}
+///PUBLIC
+int uart_get_char_poll(int base_addr, uint8_t *p){
+    int ret;
+    while(!uart_receiver_ready(base_addr)){}
+    if((ret = uart_get_char(base_addr, p))) return ret;
+    return SUCCESS;
+}
+int uart_send_char_poll(int base_addr, uint8_t  c){
+    int ret;
+    while(!uart_transmitter_empty(base_addr)){}
+    if((ret = uart_send_char(base_addr, c))) return ret;
+    return SUCCESS;
+}
+int uart_send_memory_poll(int base_addr, void *str, size_t n){
+    int ret;
+    uint8_t *p = str;
+    for(size_t i = 0; i < n; ++i, ++p){
+        if((ret = uart_send_char_poll(base_addr, *p))) return ret;
+    }
+    return SUCCESS;
+}
Index: proj/protocol.md
===================================================================
--- proj/protocol.md	(nonexistent)
+++ proj/protocol.md	(revision 249)
@@ -0,0 +1,45 @@
+# Serial port protocol
+
+Transmission/reception is processed at 38400 bps, with chars of 8 bits and even parity.
+
+## Transmission
+
+Transmission is performed on request by the program. It is done in a synchronous fashion. When starting a transmission, reception interrupt notifications are suspended. On ending, reception interrupt notifications are re-activated.
+
+### Transmission of a char
+
+1. 1 polls the readiness of THR in intervals of 50 microseconds, one at the beginning and up to 10 retries (up to 11 polling operations, and 500 microseconds). If after all the retries 1 has not yet sent the char, transmission is aborted.
+
+2. If the char is transmitted, 1 expects 2 to answer the same char for confirmation. 1 polls the fullness of RBR in intervals of 50 microseconds, one at the beginning and up to 10 retries. If after all the retries 1 has not yet received the answer, transmission is aborted.
+
+3. If 1 gets the answer, it compares it with the char it sent. If they are different, go back to step 2.
+
+4. If they are equal, 1 sends OK symbol 0xFF using only step 1., and char transmission is complete.
+
+### Transmission of a string
+
+If the string has more than 1024 chars, transmission is not even started.
+
+1. For each char in the string, transmit them, plus a start symbol 0x80 and an end symbol 0xEF. If transmission fails for a given char, transmission of the string fails, and none of the following chars in the same transmission request are transmitted.
+
+2. If all symbols are transmitted correctly, transmission is successfully terminated.
+
+## Reception
+
+Reception is made through interrupts. On a reception interrupt, 2 must handle the interrupt. On a reception interrupt, reception interrupt notifications are suspended, and on ending a reception notifications are re-activated.
+
+### Reception of a char
+
+1. Upon getting a reception interrupt, 2 grabs the char, and then polls the readiness of the THR in intervals of 50 microseconds, one at the beginning and up to 10 retries. If after 10 retries 2 has not sent the char, transmission is aborted.
+
+2. If transmission is successful, 2 expects 1 to answer the OK symbol 0xFF, using only step 1. to get the OK. If the received symbol has less or equal to 4 bits set to 1, transmission is aborted.
+
+3. If the received OK has at least 4 bits set to 1 then it is accepted.
+
+### Reception of a string
+
+1. 2 expects the first char to be the start char. If it is not, transmission is aborted.
+
+2. If the first char is correct, all chars are received. If one of the char receptions fails (timeout), reception fails. If after receiving 1024 chars the end symbol is not received, transmission is aborted.
+
+3. If all receptions are successful and the end symbol is received, transmission is successful.

Property changes on: proj/protocol.md
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
