Subversion Repositories Code-Repo

Rev

Rev 234 | Details | Compare with Previous | Last modification | View Log | RSS feed

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