Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
119 Kevin 1
#include "maindefs.h"
2
#include "uart.h"
3
#include <string.h>
4
 
5
static UART_DATA uart_1_data;
6
 
7
void UART1_Init() {
8
    // Configure the hardware USART device
9
    // UART1 TX RC6
10
    // UART1 RX RC7
11
 
12
    TRISCbits.TRISC6 = 0;   // Tx pin set to output
13
    TRISCbits.TRISC7 = 1;   // Rx pin set to input
14
 
15
    BAUDCON1bits.BRG16 = 0; // 8-bit baud rate generator
16
    SPBRG1 = 25;            // Set UART speed to 115200 baud
17
    TXSTA1bits.BRGH = 1;    // High speed mode
18
    TXSTA1bits.SYNC = 0;    // Async mode
19
    RCSTA1bits.SPEN = 1;    // Serial port enable
20
    TXSTA1bits.TX9 = 0;     // 8 bit transmission
21
    RCSTA1bits.RX9 = 0;     // 8 bit reception
22
    TXSTA1bits.TXEN = 0;    // Enable transmission
23
    RCSTA1bits.CREN = 1;    // Continuous receive mode
24
 
25
    PIE1bits.TX1IE = 1;     // Enable TX interrupt
26
    PIE1bits.RC1IE = 1;     // Enable RX interrupt
27
 
28
    // Initialize the buffer that holds UART messages
29
    uart_1_data.buffer_in_read_ind = 0;
30
    uart_1_data.buffer_in_write_ind = 0;
31
    uart_1_data.buffer_in_len = 0;
32
}
33
 
34
//void uart_2_init() {
35
//    // Configure the PPS USART ports
36
//
37
//    // UART2 RX Pin RP5
38
//    RPINR16 = 5;    // 5 is PPS RP5
39
//    // UART2 TX Pin RP6
40
//    RPOR6 = 6;  // 6 is TX2/CK2 (EUSART2 Asynchronous Transmit/Asynchronous Clock Output)
41
//
42
//    Open2USART(USART_TX_INT_OFF &   // Interrupt on TX off
43
//            USART_RX_INT_ON &      // Interrupt on RX on
44
//            USART_ASYNCH_MODE &     // Operate in async mode
45
//            USART_EIGHT_BIT &       // Operate in 8-bit mode
46
//            USART_CONT_RX &         // Continuously recieve messages
47
//            USART_BRGH_HIGH, 25);  // Set UART speed to 115200 baud
48
//}
49
 
50
void UART1_Recv_Interrupt_Handler() {
51
    unsigned char c;
52
    if (PIR1bits.RC1IF) {   // Check if data receive flag is set
53
        if (uart_1_data.buffer_in_len == MAXUARTBUF-1) {
54
            TXSTA1bits.TXEN = 0;    // Kill anything currently sending
55
            DBG_PRINT_UART("UART1 Buffer Overflow!\r\n");
56
            c = RCREG1; // Read RCREG to clear it
57
        } else {
58
            // Save received data into buffer
59
            uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = RCREG1;
60
            if (uart_1_data.buffer_in_write_ind == MAXUARTBUF-1) {
61
                uart_1_data.buffer_in_write_ind = 0;
62
            } else {
63
                uart_1_data.buffer_in_write_ind++;
64
            }
65
            uart_1_data.buffer_in_len++;
66
        }
67
    }
68
 
69
    if (RCSTAbits.OERR == 1) {
70
        // We've overrun the USART and must reset
71
        RCSTA1bits.CREN = 0;    // Reset UART1
72
        RCSTA1bits.CREN = 1;
73
        TXSTA1bits.TXEN = 0;    // Kill anything currently sending
74
        DBG_PRINT_UART("UART1 Overrun!\r\n");
75
    }
76
}
77
 
78
//void uart_2_recv_interrupt_handler() {
79
//    if (DataRdy2USART()) {
80
////        xbee_read_serial(Read2USART());
81
//    }
82
//
83
//    if (USART2_Status.OVERRUN_ERROR == 1) {
84
//        // We've overrun the USART and must reset
85
//        RCSTA2bits.CREN = 0;    // Reset UART2
86
//        RCSTA2bits.CREN = 1;
87
//    }
88
//}
89
 
90
void UART1_Send_Interrupt_Handler() {
91
    // Put remaining data in TSR for transmit
92
    if (uart_1_data.buffer_out_ind != uart_1_data.buffer_out_len) {
93
        TXREG1 = uart_1_data.buffer_out[uart_1_data.buffer_out_ind];
94
        uart_1_data.buffer_out_ind++;
95
    } else {
96
        while (!TXSTA1bits.TRMT);   // Wait for last byte to finish sending
97
        TXSTA1bits.TXEN = 0;    // End transmission and disable TX interrupt
98
        uart_1_data.buffer_out_ind = 0;
99
        uart_1_data.buffer_out_len = 0;
100
    }
101
}
102
 
103
void UART1_WriteS(const rom char *fmt, ...) {
104
    va_list args;
105
    while (TXSTA1bits.TXEN);    // Wait for previous message to finish sending
106
    va_start(args, fmt);
107
    vsprintf((char *)uart_1_data.buffer_out, fmt, args);
108
    va_end(args);
109
    uart_1_data.buffer_out_len = strlen((char *)uart_1_data.buffer_out);
110
    uart_1_data.buffer_out_ind = 1;
111
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
112
    TXSTA1bits.TXEN = 1;    // Begin transmission
113
}
114
 
115
void UART1_WriteB(const char *msg, unsigned char length) {
116
    unsigned char i;
117
    while (TXSTA1bits.TXEN);    // Wait for previous message to finish sending
118
    uart_1_data.buffer_out_len = length;
119
    uart_1_data.buffer_out_ind = 1;
120
    for (i = 0; i <  length; i++) {
121
        uart_1_data.buffer_out[i] = msg[i];
122
    }
123
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
124
    TXSTA1bits.TXEN = 1;    // Begin transmission
125
}
126
 
127
/* Reader interface to the UART buffer, returns the number of bytes read */
128
unsigned char UART1_Read(char *buffer) {
129
    unsigned char i = 0;
130
    while (uart_1_data.buffer_in_len != 0) {
131
        buffer[i] = uart_1_data.buffer_in[uart_1_data.buffer_in_read_ind];
132
        i++;
133
        if (uart_1_data.buffer_in_read_ind == MAXUARTBUF-1) {
134
            uart_1_data.buffer_in_read_ind = 0;
135
        } else {
136
            uart_1_data.buffer_in_read_ind++;
137
        }
138
        uart_1_data.buffer_in_len--;
139
    }
140
    return i;
141
}