Subversion Repositories Code-Repo

Rev

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

Rev Author Line No. Line
333 Kevin 1
#include "DEFINES.h"
2
#include "EUSART.h"
3
#include "INTERRUPTS.h"
4
 
5
static volatile uint8_t txBuffer[UART_TX_BUFFER_SIZE];
6
static volatile uint8_t txBufferSize = 0;
7
static volatile uint8_t txBufferIndex = 0;
8
 
9
static volatile uint8_t rxBuffer[UART_RX_BUFFER_SIZE];
10
static volatile uint8_t rxBufferSize = 0;
11
static volatile uint8_t rxBufferIndex = 0;
12
 
13
void UART_Init() {
14
    UART_TX_TRIS = 0;
15
    UART_RX_TRIS = 1;
16
 
17
    BAUDCONbits.BRG16 = 0;      // 8-bit baud rate generation
18
    SPBRG = 64;                 // Baud rate of 19.2k
19
    TXSTAbits.BRGH = 1;         // High speed mode
20
 
21
    TXSTAbits.SYNC = 0;         // Async mode
22
    RCSTAbits.SPEN = 1;         // Serial port enable
23
 
24
    TXSTAbits.TX9 = 0;          // 8 bit transmission
25
    RCSTAbits.RX9 = 0;          // 8 bit reception
26
 
27
    TXSTAbits.TXEN = 1;         // Transmission enabled
28
    RCSTAbits.CREN = 1;         // Reception enabled
29
 
30
    PIE1bits.TXIE = 0;          // TX interrupt starts disabled
31
    PIE1bits.RCIE = 1;          // RX interrupt starts enabled
32
}
33
 
34
void UART_Write(uint8_t *msg, uint8_t length) {
35
    // Check to make sure there is enough space in buffer for message
36
    length = (length > UART_TX_BUFFER_SIZE) ? UART_TX_BUFFER_SIZE : length;
37
 
38
    // Wait for previous message to finish sending
39
    while (PIE1bits.TXIE);
40
 
41
    txBufferSize = length;
42
    txBufferIndex = 1;
43
    for (uint8_t i = 0; i < length; i++) {
44
        txBuffer[i] = msg[i];
45
    }
46
    TXREG = txBuffer[0];
47
    PIE1bits.TXIE = 1;
48
}
49
 
50
void UART_TX_Interrupt_Handler() {
51
    if (txBufferIndex != txBufferSize) {
52
        // Transmit next byte in the buffer
53
        TXREG = txBuffer[txBufferIndex];
54
        txBufferIndex++;
55
    } else {
56
        // Wait for last byte to finish sending
57
        while (!TXSTAbits.TRMT);
58
        PIE1bits.TXIE = 0;
59
        txBufferSize = 0;
60
        txBufferIndex = 0;
61
    }
62
}
63
 
64
void UART_RX_Interrupt_Handler() {
65
    if (PIR1bits.RCIF) {
66
        uint8_t c = RCREG;
67
 
68
        // Store received byte into buffer and increment write location
69
        rxBuffer[rxBufferIndex] = c;
70
        if (rxBufferIndex == UART_RX_BUFFER_SIZE - 1) {
71
            rxBufferIndex = 0;
72
        } else {
73
            rxBufferIndex++;
74
        }
75
 
76
        // Increment received byte count
77
        if (rxBufferSize < UART_RX_BUFFER_SIZE) {
78
            rxBufferSize++;
79
        }
80
    }
81
 
82
    // If UART overrun is detected, reset module
83
    if (RCSTAbits.OERR) {
84
        TXSTAbits.TXEN = 0;
85
        RCSTAbits.CREN = 0;
86
        RCSTAbits.CREN = 1;
87
    }
88
}
89
 
90
uint8_t UART_Read(uint8_t *buffer) {
91
    // Return values in RX buffer
92
    uint8_t size = rxBufferSize;
93
    for (uint8_t i = 0; i < size; i++) {
94
        buffer[i] = rxBuffer[i];
95
    }
96
 
97
    return size;
98
}
99
 
100
void UART_Reset_RX() {
101
    rxBufferIndex = 0;
102
    rxBufferSize = 0;
103
}