Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
212 Kevin 1
#include <xc.h>
2
#include <plib.h>
3
#include "defines.h"
4
#include "UART1.h"
5
 
6
static UART1_DATA *uart_data_ptr;
7
 
215 Kevin 8
/* Note: BRGH values are different from PIC18!
9
 *
10
 * Baud Rate Calculation (BRGH = 0):
11
 * Baud Rate = PerfBusFreq / (16 * (BRG + 1))
12
 * BRG = PerfBusFreq / (16 * Baud Rate) - 1
13
 *
14
 * Baud Rate Calculation (BRGH = 1):
15
 * Baud Rate = PerfBusFreq / (4 * (BRG + 1))
16
 * BRG = PerfBusFreq / (4 * Baud Rate) - 1
17
 */
18
 
212 Kevin 19
void UART1_Init(UART1_DATA *data, void (*rx_callback)(char)) {
20
    uart_data_ptr = data;
21
    uart_data_ptr->rx_callback = rx_callback;
22
    uart_data_ptr->buffer_out_len = 0;
23
    uart_data_ptr->buffer_out_ind = 0;
24
 
25
    INTDisableInterrupts();
26
 
27
    IEC0CLR   = 0x1C000000; // Disable all UART1 interrupts
28
    IFS0CLR   = 0x1C000000; // Clear any existing events
29
    IPC6SET   = 0x00000009; // Set Priority = 2, Subpriority = 1
30
 
216 Kevin 31
    U1MODE    = 0x00008008; // UART enabled, BRGH = 1
212 Kevin 32
    U1STA     = 0x00009400; // TX interrupt on buffer empty, RX interrupt on buffer not empty
216 Kevin 33
 
34
//    U1BRG     = 173;        // Set baud rate to 115200 @ 80MHz (0.22% error)
35
    U1BRG     = 86;         // Set baud rate to 230400 @ 80MHz (0.22% error)
36
//    U1BRG     = 77;         // Set baud rate to 256000 @ 80MHz (0.12% error)
37
//    U1BRG     = 42;         // Set baud rate to 460800 @ 80MHz (0.94% error)
38
//    U1BRG     = 21;         // Set baud rate to 921600 @ 80MHz (1.36% error)
39
 
212 Kevin 40
    IEC0SET   = 0x0C000000; // Enable the RX and Error interrupts
41
 
42
    INTEnableInterrupts();
43
 
44
    TRISDbits.TRISD14 = 0;
45
    PORTDbits.RD14 = 0;
46
}
47
 
48
int UART1_Write(char *string, int length) {
49
    if (length > UART1_BUFFER_SIZE)
50
        return 0;
51
    if (uart_data_ptr->buffer_out_len != 0)
52
        return 0;
53
 
54
    // Put the data to send into the outbound buffer
55
    uart_data_ptr->buffer_out_len = length;
56
    uart_data_ptr->buffer_out_ind = 0;
57
    int i;
58
    for (i = 0; i < length; i++) {
59
        uart_data_ptr->buffer_out[i] = string[i];
60
    }
61
    IEC0SET = 0x10000000; // Enable TX interrupt
62
    return 1;
63
}
64
 
65
void __ISR(_UART_1_VECTOR, ipl2) __UART_1_Interrupt_Handler(void) {
66
    PORTDbits.RD14 = 1;
67
    // Process UART1 error flag
68
    if (IFS0bits.U1EIF) {
69
        if (U1STAbits.PERR) { // Process parity error
70
 
71
        }
72
        if (U1STAbits.FERR) { // Process frame error
73
 
74
        }
75
        if (U1STAbits.OERR) { // Process receive buffer overrun error
76
            U1STAbits.OERR = 0; // Clear the overrun error if set
77
        }
78
        IFS0CLR = 0x04000000; // Clear the error flag
79
    }
80
 
81
    // Process UART1 recieve flag
82
    if (IFS0bits.U1RXIF) {
83
        // Read the data received from the last transfer
84
        while (U1STAbits.URXDA) {
85
            char c = U1RXREG;
86
            // Call the RX callback function on each received data
87
            if (uart_data_ptr->rx_callback != NULL) {
88
                (*uart_data_ptr->rx_callback)(c);
89
            }
90
        }
91
        IFS0CLR = 0x08000000; // Clear the recieve flag
92
    }
93
 
94
    // Process UART1 transmit flag
215 Kevin 95
    if (IFS0bits.U1TXIF && IEC0bits.U1TXIE) {
212 Kevin 96
        // Disable the transmit interrupt if all data has been sent
97
        if (uart_data_ptr->buffer_out_ind == uart_data_ptr->buffer_out_len) {
98
            IEC0CLR = 0x10000000;
99
            uart_data_ptr->buffer_out_len = 0;
100
        } else {
101
            // Start filling the transmit buffer
102
            while (!U1STAbits.UTXBF) {
103
                U1TXREG = uart_data_ptr->buffer_out[uart_data_ptr->buffer_out_ind];
104
                uart_data_ptr->buffer_out_ind++;
105
                if (uart_data_ptr->buffer_out_ind == uart_data_ptr->buffer_out_len)
106
                    break;
107
            }
108
        }
109
        IFS0CLR = 0x10000000; // Clear the transmit flag
110
    }
111
    PORTDbits.RD14 = 0;
112
}