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