| 1 | #include <pic18fregs.h> |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <my_serial.h> |
|---|
| 4 | |
|---|
| 5 | char uart_txbuf[UART_BUFSIZE]; |
|---|
| 6 | unsigned char uart_txbuf_prod; |
|---|
| 7 | volatile unsigned char uart_txbuf_cons; |
|---|
| 8 | |
|---|
| 9 | char uart_rxbuf[UART_BUFSIZE]; |
|---|
| 10 | volatile unsigned char uart_rxbuf_prod; |
|---|
| 11 | unsigned char uart_rxbuf_cons; |
|---|
| 12 | |
|---|
| 13 | #define LEDR LATAbits.LATA7 |
|---|
| 14 | |
|---|
| 15 | PUTCHAR(c) /* Macro */ |
|---|
| 16 | { |
|---|
| 17 | unsigned char new_uart_txbuf_prod; |
|---|
| 18 | #if 0 |
|---|
| 19 | if (!PORTBbits.RB7) |
|---|
| 20 | return; |
|---|
| 21 | #endif |
|---|
| 22 | new_uart_txbuf_prod = (uart_txbuf_prod + 1) & UART_BUFSIZE_MASK; |
|---|
| 23 | #if 1 |
|---|
| 24 | |
|---|
| 25 | again: |
|---|
| 26 | while (new_uart_txbuf_prod == uart_txbuf_cons) { |
|---|
| 27 | PIE1bits.TX1IE = 1; /* ensure we'll make progress */ |
|---|
| 28 | } |
|---|
| 29 | uart_txbuf[uart_txbuf_prod] = c; |
|---|
| 30 | uart_txbuf_prod = new_uart_txbuf_prod; |
|---|
| 31 | PIE1bits.TX1IE = 1; |
|---|
| 32 | if (c == '\n') { |
|---|
| 33 | c = '\r'; |
|---|
| 34 | new_uart_txbuf_prod = (uart_txbuf_prod + 1) & UART_BUFSIZE_MASK; |
|---|
| 35 | goto again; |
|---|
| 36 | } |
|---|
| 37 | #else |
|---|
| 38 | again: |
|---|
| 39 | while (!PIR1bits.TX1IF) |
|---|
| 40 | ; /* wait */ |
|---|
| 41 | TXREG1 = c; |
|---|
| 42 | if (c == '\n') { |
|---|
| 43 | c = '\r'; |
|---|
| 44 | goto again; |
|---|
| 45 | } |
|---|
| 46 | #endif |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | void |
|---|
| 50 | uart_putchar_raw(char c) |
|---|
| 51 | { |
|---|
| 52 | unsigned char new_uart_txbuf_prod; |
|---|
| 53 | new_uart_txbuf_prod = (uart_txbuf_prod + 1) & UART_BUFSIZE_MASK; |
|---|
| 54 | while (new_uart_txbuf_prod == uart_txbuf_cons) { |
|---|
| 55 | PIE1bits.TX1IE = 1; /* ensure we'll make progress */ |
|---|
| 56 | } |
|---|
| 57 | uart_txbuf[uart_txbuf_prod] = c; |
|---|
| 58 | uart_txbuf_prod = new_uart_txbuf_prod; |
|---|
| 59 | PIE1bits.TX1IE = 1; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | void |
|---|
| 63 | uart_putchar_hard(char c) __wparam { |
|---|
| 64 | while (PIR1bits.TX1IF == 0) |
|---|
| 65 | ; |
|---|
| 66 | TXREG1 = c; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | char |
|---|
| 70 | getchar(void) |
|---|
| 71 | { |
|---|
| 72 | char c; |
|---|
| 73 | char en; |
|---|
| 74 | en = PIE1bits.RC1IE; |
|---|
| 75 | PIE1bits.RC1IE = 0; |
|---|
| 76 | while (!PIR1bits.RC1IF); /* wait for a char */ |
|---|
| 77 | c = RCREG1; |
|---|
| 78 | if (RCSTA1bits.OERR) { |
|---|
| 79 | RCSTA1bits.CREN = 0; |
|---|
| 80 | RCSTA1bits.CREN = 1; |
|---|
| 81 | } |
|---|
| 82 | PIE1bits.RC1IE = en; |
|---|
| 83 | return c; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | char |
|---|
| 87 | uart_getchar() |
|---|
| 88 | { |
|---|
| 89 | register char c; |
|---|
| 90 | LEDR = 1; |
|---|
| 91 | while (uart_rxbuf_cons == uart_rxbuf_prod) |
|---|
| 92 | ; |
|---|
| 93 | |
|---|
| 94 | uart_rxbuf_cons = (uart_rxbuf_cons + 1) & UART_BUFSIZE_MASK; |
|---|
| 95 | c = uart_rxbuf[uart_rxbuf_cons]; |
|---|
| 96 | LEDR = 0; |
|---|
| 97 | return c; |
|---|
| 98 | } |
|---|