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"
127 Kevin 3
#include "xbee.h"
119 Kevin 4
#include <string.h>
126 Kevin 5
#include <stdio.h>
119 Kevin 6
 
7
static UART_DATA uart_1_data;
8
 
9
void UART1_Init() {
10
    // Configure the hardware USART device
11
    // UART1 TX RC6
12
    // UART1 RX RC7
13
 
122 Kevin 14
    TRISCbits.TRISC6 = 0; // Tx pin set to output
15
    TRISCbits.TRISC7 = 1; // Rx pin set to input
119 Kevin 16
 
17
    BAUDCON1bits.BRG16 = 0; // 8-bit baud rate generator
122 Kevin 18
    SPBRG1 = 25; // Set UART speed to 115200 baud
19
    TXSTA1bits.BRGH = 1; // High speed mode
20
    TXSTA1bits.SYNC = 0; // Async mode
21
    RCSTA1bits.SPEN = 1; // Serial port enable
22
    TXSTA1bits.TX9 = 0; // 8 bit transmission
23
    RCSTA1bits.RX9 = 0; // 8 bit reception
24
    RCSTA1bits.CREN = 1; // Continuous receive mode
25
 
121 Kevin 26
#ifdef _DEBUG // In debug mode we want to have TXEN constantly enabled
122 Kevin 27
    TXSTA1bits.TXEN = 1; // TX is always enabled
28
    PIE1bits.TX1IE = 0; // Disable TX interrupt
121 Kevin 29
#else
122 Kevin 30
    TXSTA1bits.TXEN = 0; // Enable transmission
31
    PIE1bits.TX1IE = 1; // Enable TX interrupt
121 Kevin 32
#endif
33
 
122 Kevin 34
    PIE1bits.RC1IE = 1; // Enable RX interrupt
119 Kevin 35
 
36
    // Initialize the buffer that holds UART messages
37
    uart_1_data.buffer_in_read_ind = 0;
38
    uart_1_data.buffer_in_write_ind = 0;
39
    uart_1_data.buffer_in_len = 0;
122 Kevin 40
    uart_1_data.buffer_in_len_tmp = 0;
119 Kevin 41
}
42
 
43
//void uart_2_init() {
44
//    // Configure the PPS USART ports
45
//
46
//    // UART2 RX Pin RP5
47
//    RPINR16 = 5;    // 5 is PPS RP5
48
//    // UART2 TX Pin RP6
49
//    RPOR6 = 6;  // 6 is TX2/CK2 (EUSART2 Asynchronous Transmit/Asynchronous Clock Output)
50
//
51
//    Open2USART(USART_TX_INT_OFF &   // Interrupt on TX off
52
//            USART_RX_INT_ON &      // Interrupt on RX on
53
//            USART_ASYNCH_MODE &     // Operate in async mode
54
//            USART_EIGHT_BIT &       // Operate in 8-bit mode
55
//            USART_CONT_RX &         // Continuously recieve messages
56
//            USART_BRGH_HIGH, 25);  // Set UART speed to 115200 baud
57
//}
58
 
59
void UART1_Recv_Interrupt_Handler() {
60
    unsigned char c;
122 Kevin 61
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
62
        c = RCREG1;
127 Kevin 63
#ifdef UART1_RX_TO_BUFFER
122 Kevin 64
        // Save received data into buffer
65
        uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = c;
66
        if (uart_1_data.buffer_in_write_ind == MAXUARTBUF - 1) {
67
            uart_1_data.buffer_in_write_ind = 0;
119 Kevin 68
        } else {
122 Kevin 69
            uart_1_data.buffer_in_write_ind++;
70
        }
71
 
72
        // Store the last MAXUARTBUF values entered
73
        if (uart_1_data.buffer_in_len_tmp < MAXUARTBUF) {
74
            uart_1_data.buffer_in_len_tmp++;
75
        } else {
76
            if (uart_1_data.buffer_in_read_ind == MAXUARTBUF - 1) {
77
                uart_1_data.buffer_in_read_ind = 0;
119 Kevin 78
            } else {
122 Kevin 79
                uart_1_data.buffer_in_read_ind++;
119 Kevin 80
            }
81
        }
122 Kevin 82
 
83
        // Update buffer size upon receiving newline (0x0D)
84
        if (c == UART1_BREAK_CHAR) {
85
            uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp;
86
            uart_1_data.buffer_in_len_tmp = 0;
87
        }
126 Kevin 88
#endif
127 Kevin 89
#ifdef UART1_RX_TO_XBEE
90
        XBee_Serial_In(c);
126 Kevin 91
#endif
119 Kevin 92
    }
122 Kevin 93
 
127 Kevin 94
    if (RCSTA1bits.OERR == 1) {
119 Kevin 95
        // We've overrun the USART and must reset
122 Kevin 96
        RCSTA1bits.CREN = 0; // Reset UART1
119 Kevin 97
        RCSTA1bits.CREN = 1;
122 Kevin 98
        DBG_PRINT_UART("UART1: (ERROR) overrun\r\n");
99
        TXSTA1bits.TXEN = 0; // Kill anything currently sending
119 Kevin 100
    }
101
}
102
 
103
//void uart_2_recv_interrupt_handler() {
104
//    if (DataRdy2USART()) {
105
////        xbee_read_serial(Read2USART());
106
//    }
107
//
108
//    if (USART2_Status.OVERRUN_ERROR == 1) {
109
//        // We've overrun the USART and must reset
110
//        RCSTA2bits.CREN = 0;    // Reset UART2
111
//        RCSTA2bits.CREN = 1;
112
//    }
113
//}
114
 
115
void UART1_Send_Interrupt_Handler() {
116
    // Put remaining data in TSR for transmit
117
    if (uart_1_data.buffer_out_ind != uart_1_data.buffer_out_len) {
118
        TXREG1 = uart_1_data.buffer_out[uart_1_data.buffer_out_ind];
119
        uart_1_data.buffer_out_ind++;
120
    } else {
122 Kevin 121
        while (!TXSTA1bits.TRMT); // Wait for last byte to finish sending
122
        TXSTA1bits.TXEN = 0; // End transmission and disable TX interrupt
119 Kevin 123
        uart_1_data.buffer_out_ind = 0;
124
        uart_1_data.buffer_out_len = 0;
125
    }
126
}
127
 
128
void UART1_WriteS(const rom char *fmt, ...) {
126 Kevin 129
#ifdef _DEBUG
130
    unsigned char i;
119 Kevin 131
    va_list args;
126 Kevin 132
    va_start(args, fmt);
133
    vsprintf((char *) uart_1_data.buffer_out, fmt, args);
134
    va_end(args);
135
    uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
136
    uart_1_data.buffer_out_ind = 1;
137
    for (i = 0; i < uart_1_data.buffer_out_len; i++) {
138
        TXREG1 = uart_1_data.buffer_out[i];
139
        Nop();
127 Kevin 140
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
126 Kevin 141
    }
142
#else
143
    va_list args;
122 Kevin 144
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
119 Kevin 145
    va_start(args, fmt);
122 Kevin 146
    vsprintf((char *) uart_1_data.buffer_out, fmt, args);
119 Kevin 147
    va_end(args);
122 Kevin 148
    uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
119 Kevin 149
    uart_1_data.buffer_out_ind = 1;
150
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
122 Kevin 151
    TXSTA1bits.TXEN = 1; // Begin transmission
126 Kevin 152
#endif
119 Kevin 153
}
154
 
155
void UART1_WriteB(const char *msg, unsigned char length) {
156
    unsigned char i;
126 Kevin 157
#ifdef _DEBUG
158
    for (i = 0; i < length; i++) {
159
        TXREG1 = msg[i];
160
        Nop();
127 Kevin 161
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
126 Kevin 162
    }
163
#else
122 Kevin 164
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
119 Kevin 165
    uart_1_data.buffer_out_len = length;
166
    uart_1_data.buffer_out_ind = 1;
122 Kevin 167
    for (i = 0; i < length; i++) {
119 Kevin 168
        uart_1_data.buffer_out[i] = msg[i];
169
    }
170
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
122 Kevin 171
    TXSTA1bits.TXEN = 1; // Begin transmission
126 Kevin 172
#endif
119 Kevin 173
}
174
 
127 Kevin 175
void UART1_WriteC(const unsigned char c) {
176
#ifdef _DEBUG
177
    TXREG1 = c;
178
    Nop();
179
    while (!PIR1bits.TX1IF);
180
#else
181
    while (TXSTA1bits.TXEN);
182
    uart_1_data.buffer_out_len = 1;
183
    uart_1_data.buffer_out_ind = 1;
184
    TXREG1 = c;
185
    TXSTA1bits.TXEN = 1;
186
#endif
187
 
188
}
189
 
122 Kevin 190
unsigned char UART1_Buffer_Len() {
191
    return uart_1_data.buffer_in_len;
192
}
193
 
119 Kevin 194
/* Reader interface to the UART buffer, returns the number of bytes read */
122 Kevin 195
unsigned char UART1_Read_Buffer(unsigned char *buffer) {
119 Kevin 196
    unsigned char i = 0;
197
    while (uart_1_data.buffer_in_len != 0) {
198
        buffer[i] = uart_1_data.buffer_in[uart_1_data.buffer_in_read_ind];
199
        i++;
122 Kevin 200
        if (uart_1_data.buffer_in_read_ind == MAXUARTBUF - 1) {
119 Kevin 201
            uart_1_data.buffer_in_read_ind = 0;
202
        } else {
203
            uart_1_data.buffer_in_read_ind++;
204
        }
205
        uart_1_data.buffer_in_len--;
206
    }
207
    return i;
208
}