Subversion Repositories Code-Repo

Rev

Rev 126 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 126 Rev 127
Line 1... Line 1...
1
#include "maindefs.h"
1
#include "maindefs.h"
2
#include "uart.h"
2
#include "uart.h"
-
 
3
#include "xbee.h"
3
#include <string.h>
4
#include <string.h>
4
#include <stdio.h>
5
#include <stdio.h>
5
 
6
 
6
static UART_DATA uart_1_data;
7
static UART_DATA uart_1_data;
7
 
8
 
Line 57... Line 58...
57
 
58
 
58
void UART1_Recv_Interrupt_Handler() {
59
void UART1_Recv_Interrupt_Handler() {
59
    unsigned char c;
60
    unsigned char c;
60
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
61
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
61
        c = RCREG1;
62
        c = RCREG1;
62
 
-
 
63
#ifdef UART1_RECV_BUFFER
63
#ifdef UART1_RX_TO_BUFFER
64
        // Save received data into buffer
64
        // Save received data into buffer
65
        uart_1_data.buffer_in[uart_1_data.buffer_in_write_ind] = c;
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) {
66
        if (uart_1_data.buffer_in_write_ind == MAXUARTBUF - 1) {
67
            uart_1_data.buffer_in_write_ind = 0;
67
            uart_1_data.buffer_in_write_ind = 0;
68
        } else {
68
        } else {
Line 84... Line 84...
84
        if (c == UART1_BREAK_CHAR) {
84
        if (c == UART1_BREAK_CHAR) {
85
            uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp;
85
            uart_1_data.buffer_in_len = uart_1_data.buffer_in_len_tmp;
86
            uart_1_data.buffer_in_len_tmp = 0;
86
            uart_1_data.buffer_in_len_tmp = 0;
87
        }
87
        }
88
#endif
88
#endif
89
#ifdef UART1_RECV_XBEE
89
#ifdef UART1_RX_TO_XBEE
90
 
-
 
-
 
90
        XBee_Serial_In(c);
91
#endif
91
#endif
92
    }
92
    }
93
 
93
 
94
    if (RCSTAbits.OERR == 1) {
94
    if (RCSTA1bits.OERR == 1) {
95
        // We've overrun the USART and must reset
95
        // We've overrun the USART and must reset
96
        RCSTA1bits.CREN = 0; // Reset UART1
96
        RCSTA1bits.CREN = 0; // Reset UART1
97
        RCSTA1bits.CREN = 1;
97
        RCSTA1bits.CREN = 1;
98
        DBG_PRINT_UART("UART1: (ERROR) overrun\r\n");
98
        DBG_PRINT_UART("UART1: (ERROR) overrun\r\n");
99
        TXSTA1bits.TXEN = 0; // Kill anything currently sending
99
        TXSTA1bits.TXEN = 0; // Kill anything currently sending
Line 135... Line 135...
135
    uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
135
    uart_1_data.buffer_out_len = strlen((char *) uart_1_data.buffer_out);
136
    uart_1_data.buffer_out_ind = 1;
136
    uart_1_data.buffer_out_ind = 1;
137
    for (i = 0; i < uart_1_data.buffer_out_len; i++) {
137
    for (i = 0; i < uart_1_data.buffer_out_len; i++) {
138
        TXREG1 = uart_1_data.buffer_out[i];
138
        TXREG1 = uart_1_data.buffer_out[i];
139
        Nop();
139
        Nop();
140
        while(!PIR1bits.TX1IF); // Wait for byte to be transmitted
140
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
141
    }
141
    }
142
#else
142
#else
143
    va_list args;
143
    va_list args;
144
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
144
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
145
    va_start(args, fmt);
145
    va_start(args, fmt);
Line 156... Line 156...
156
    unsigned char i;
156
    unsigned char i;
157
#ifdef _DEBUG
157
#ifdef _DEBUG
158
    for (i = 0; i < length; i++) {
158
    for (i = 0; i < length; i++) {
159
        TXREG1 = msg[i];
159
        TXREG1 = msg[i];
160
        Nop();
160
        Nop();
161
        while(!PIR1bits.TX1IF); // Wait for byte to be transmitted
161
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
162
    }
162
    }
163
#else
163
#else
164
    unsigned char i;
-
 
165
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
164
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
166
    uart_1_data.buffer_out_len = length;
165
    uart_1_data.buffer_out_len = length;
167
    uart_1_data.buffer_out_ind = 1;
166
    uart_1_data.buffer_out_ind = 1;
168
    for (i = 0; i < length; i++) {
167
    for (i = 0; i < length; i++) {
169
        uart_1_data.buffer_out[i] = msg[i];
168
        uart_1_data.buffer_out[i] = msg[i];
Line 171... Line 170...
171
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
170
    TXREG1 = uart_1_data.buffer_out[0]; // Put first byte in TSR
172
    TXSTA1bits.TXEN = 1; // Begin transmission
171
    TXSTA1bits.TXEN = 1; // Begin transmission
173
#endif
172
#endif
174
}
173
}
175
 
174
 
-
 
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
 
176
unsigned char UART1_Buffer_Len() {
190
unsigned char UART1_Buffer_Len() {
177
    return uart_1_data.buffer_in_len;
191
    return uart_1_data.buffer_in_len;
178
}
192
}
179
 
193
 
180
/* Reader interface to the UART buffer, returns the number of bytes read */
194
/* Reader interface to the UART buffer, returns the number of bytes read */