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
    RCSTA1bits.CREN = 1;    // Continuous receive mode
23
 
121 Kevin 24
#ifdef _DEBUG // In debug mode we want to have TXEN constantly enabled
25
    TXSTA1bits.TXEN = 1;    // TX is always enabled
26
    PIE1bits.TX1IE = 0;     // Disable TX interrupt
27
#else
28
    TXSTA1bits.TXEN = 0;    // Enable transmission
119 Kevin 29
    PIE1bits.TX1IE = 1;     // Enable TX interrupt
121 Kevin 30
#endif
31
 
119 Kevin 32
    PIE1bits.RC1IE = 1;     // Enable RX interrupt
33
 
34
    // Initialize the buffer that holds UART messages
35
    uart_1_data.buffer_in_read_ind = 0;
36
    uart_1_data.buffer_in_write_ind = 0;
37
    uart_1_data.buffer_in_len = 0;
38
}
39
 
40
//void uart_2_init() {
41
//    // Configure the PPS USART ports
42
//
43
//    // UART2 RX Pin RP5
44
//    RPINR16 = 5;    // 5 is PPS RP5
45
//    // UART2 TX Pin RP6
46
//    RPOR6 = 6;  // 6 is TX2/CK2 (EUSART2 Asynchronous Transmit/Asynchronous Clock Output)
47
//
48
//    Open2USART(USART_TX_INT_OFF &   // Interrupt on TX off
49
//            USART_RX_INT_ON &      // Interrupt on RX on
50
//            USART_ASYNCH_MODE &     // Operate in async mode
51
//            USART_EIGHT_BIT &       // Operate in 8-bit mode
52
//            USART_CONT_RX &         // Continuously recieve messages
53
//            USART_BRGH_HIGH, 25);  // Set UART speed to 115200 baud
54
//}
55
 
56
void UART1_Recv_Interrupt_Handler() {
57
    unsigned char c;
58
    if (PIR1bits.RC1IF) {   // Check if data receive flag is set
59
        if (uart_1_data.buffer_in_len == MAXUARTBUF-1) {
60
            TXSTA1bits.TXEN = 0;    // Kill anything currently sending
120 Kevin 61
            DBG_PRINT_UART("UART1: buffer overflow\r\n");
119 Kevin 62
            c = RCREG1; // Read RCREG to clear it
63
        } else {
64
            // Save received data into buffer
65
            uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = RCREG1;
66
            if (uart_1_data.buffer_in_write_ind == MAXUARTBUF-1) {
67
                uart_1_data.buffer_in_write_ind = 0;
68
            } else {
69
                uart_1_data.buffer_in_write_ind++;
70
            }
71
            uart_1_data.buffer_in_len++;
72
        }
73
    }
74
 
75
    if (RCSTAbits.OERR == 1) {
76
        // We've overrun the USART and must reset
77
        RCSTA1bits.CREN = 0;    // Reset UART1
78
        RCSTA1bits.CREN = 1;
79
        TXSTA1bits.TXEN = 0;    // Kill anything currently sending
120 Kevin 80
        DBG_PRINT_UART("UART1: overrun\r\n");
119 Kevin 81
    }
82
}
83
 
84
//void uart_2_recv_interrupt_handler() {
85
//    if (DataRdy2USART()) {
86
////        xbee_read_serial(Read2USART());
87
//    }
88
//
89
//    if (USART2_Status.OVERRUN_ERROR == 1) {
90
//        // We've overrun the USART and must reset
91
//        RCSTA2bits.CREN = 0;    // Reset UART2
92
//        RCSTA2bits.CREN = 1;
93
//    }
94
//}
95
 
96
void UART1_Send_Interrupt_Handler() {
97
    // Put remaining data in TSR for transmit
98
    if (uart_1_data.buffer_out_ind != uart_1_data.buffer_out_len) {
99
        TXREG1 = uart_1_data.buffer_out[uart_1_data.buffer_out_ind];
100
        uart_1_data.buffer_out_ind++;
101
    } else {
102
        while (!TXSTA1bits.TRMT);   // Wait for last byte to finish sending
103
        TXSTA1bits.TXEN = 0;    // End transmission and disable TX interrupt
104
        uart_1_data.buffer_out_ind = 0;
105
        uart_1_data.buffer_out_len = 0;
106
    }
107
}
108
 
109
void UART1_WriteS(const rom char *fmt, ...) {
110
    va_list args;
111
    while (TXSTA1bits.TXEN);    // Wait for previous message to finish sending
112
    va_start(args, fmt);
113
    vsprintf((char *)uart_1_data.buffer_out, fmt, args);
114
    va_end(args);
115
    uart_1_data.buffer_out_len = strlen((char *)uart_1_data.buffer_out);
116
    uart_1_data.buffer_out_ind = 1;
117
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
118
    TXSTA1bits.TXEN = 1;    // Begin transmission
119
}
120
 
121
void UART1_WriteB(const char *msg, unsigned char length) {
122
    unsigned char i;
123
    while (TXSTA1bits.TXEN);    // Wait for previous message to finish sending
124
    uart_1_data.buffer_out_len = length;
125
    uart_1_data.buffer_out_ind = 1;
126
    for (i = 0; i <  length; i++) {
127
        uart_1_data.buffer_out[i] = msg[i];
128
    }
129
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
130
    TXSTA1bits.TXEN = 1;    // Begin transmission
131
}
132
 
133
/* Reader interface to the UART buffer, returns the number of bytes read */
134
unsigned char UART1_Read(char *buffer) {
135
    unsigned char i = 0;
136
    while (uart_1_data.buffer_in_len != 0) {
137
        buffer[i] = uart_1_data.buffer_in[uart_1_data.buffer_in_read_ind];
138
        i++;
139
        if (uart_1_data.buffer_in_read_ind == MAXUARTBUF-1) {
140
            uart_1_data.buffer_in_read_ind = 0;
141
        } else {
142
            uart_1_data.buffer_in_read_ind++;
143
        }
144
        uart_1_data.buffer_in_len--;
145
    }
146
    return i;
147
}