Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

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