OSDN Git Service

[UART] Load via S-Record is okay. Setting is 57600bps, 8N1, but some of noise (bit7...
[openi2cradio/OpenI2CRadio.git] / euart.c
diff --git a/euart.c b/euart.c
index 247a4f3..8941051 100644 (file)
--- a/euart.c
+++ b/euart.c
@@ -49,6 +49,7 @@ static unsigned char uart_rx_xon;
 static unsigned char uart_tx_xon;
 
 static unsigned char uart_rx_wakeup;
+#define UART_BAUD_FACTOR 34 // 57600bps on 8MHz,BRGH=1,BRG16=1
 
 void uart_sleep(void)
 {
@@ -57,13 +58,13 @@ void uart_sleep(void)
     IPR1bits.TX1IP = 0;  // Low
     IPR1bits.RC1IP = 1; // High
     RCSTA = 0b10010000; //SPEN, 8bit, ASYNC, CREN = 0
-    BAUDCON = 0b00001010; // IDLE High, BRG16, -ABDEN, WUE
-    TXSTA = 0b00100000; //8bit, ASYNC, TXEN, Break
+    BAUDCON = 0b00001010; // IDLE High, DTRXP=0(Positive), BRG16, -ABDEN, WUE
+    TXSTA = 0b00100100; //8bit, ASYNC, TXEN, Break, BRGH=1
     PIE1bits.TX1IE = 0;
     PIE1bits.RC1IE = 1;
     uart_rx_wakeup = 0;
-    SPBRGH = 51 / 256;
-    SPBRG = 51 % 256;
+    SPBRGH = UART_BAUD_FACTOR / 256;
+    SPBRG = UART_BAUD_FACTOR % 256;
 }
 
 void uart_wakeup(void)
@@ -73,13 +74,13 @@ void uart_wakeup(void)
     IPR1bits.TX1IP = 0;  // Low
     IPR1bits.RC1IP = 1; // High
     RCSTA = 0b10010000; //SPEN, 8bit, ASYNC, CREN
-    BAUDCON = 0b00001010; // IDLE High, BRG16, -ABDEN, WUE
-    TXSTA = 0b00100000; //8bit, ASYNC, TXEN, Break
+    BAUDCON = 0b00001010; // IDLE High,  DTRXP=0(Positive),BRG16, -ABDEN, WUE
+    TXSTA = 0b00100100; //8bit, ASYNC, TXEN, Break, BRGH=1
     PIE1bits.TX1IE = 0;
     PIE1bits.RC1IE = 1;
 //   uart_rx_wakeup = 0xff;
-   SPBRGH = 51 / 256;
-   SPBRG = 51 % 256;
+   SPBRGH = UART_BAUD_FACTOR / 256;
+   SPBRG = UART_BAUD_FACTOR % 256;
 }
 
 void uart_init(void)
@@ -108,21 +109,30 @@ void uart_inthdr_rx(void)
         goto _l0; // Exit
     }
 #endif
+    c = RCREG;
     if(RCSTAbits.OERR == 0) {
         if(RCSTAbits.FERR == 0) {
             uart_rx_sts = 0; // Clear error
-            c = RCREG;
+//            c = RCREG;
             if(c == UART_CH_XOFF) {
                 uart_tx_xon = 0;  // XOFF Sequence for TX
              } else if(c == UART_CH_XON) { // XON Sequence for TX
                  uart_tx_xon = 0xff;
-             }  else { //if(uart_rx_bytes < UART_BUF_SIZE) { //
+             }  else if(uart_rx_bytes < UART_BUF_SIZE) { //
                         uart_rfifo[uart_rx_wptr++] = c;
                         if(uart_rx_wptr >= UART_BUF_SIZE) uart_rx_wptr = 0;
                         uart_rx_bytes++;
                         if(uart_rx_bytes >= UART_BUF_SIZE) uart_rx_bytes = UART_BUF_SIZE;
+                       if((uart_rx_xon != 0) && (uart_rx_bytes >= ((UART_BUF_SIZE * 7) / 10))) {
+                          uart_pushxoff(100);
+                          uart_rx_xon = 0;
+                       }
+               
 //             uart_pushchar(c, 0x0000);
-             }
+             }// else {
+               // If overflow, discard.
+            //}
+          
         } else { // Frame Error
          uart_rx_sts != UART_FRAMEERROR;
         }
@@ -150,6 +160,10 @@ unsigned char uart_pullchar(void)
     if(uart_rx_rptr > UART_BUF_SIZE) uart_rx_rptr = 0;
     uart_rx_bytes--;
     if(uart_rx_bytes <= 0) uart_rx_bytes = 0;
+    if((uart_rx_xon == 0) && (uart_rx_bytes < ((UART_BUF_SIZE * 7) / 10))) {
+       uart_pushxon(100);
+       uart_rx_xon = 0xff;
+    }
     return c;
 }