Subversion Repositories Code-Repo

Rev

Go to most recent revision | 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"
158 Kevin 5
#include "base_UART.h"
152 Kevin 6
 
155 Kevin 7
static UART_DATA *uart_1_data_p;
152 Kevin 8
 
155 Kevin 9
void UART1_Init(UART_DATA *data) {
10
    uart_1_data_p = data;
152 Kevin 11
 
12
    UART1_TX_TRIS = 0; // Tx pin set to output
13
    UART1_RX_TRIS = 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
 
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
29
    PIE1bits.TX1IE = 1; // Enable TX interrupt
30
#endif
31
 
32
    PIE1bits.RC1IE = 1; // Enable RX interrupt
33
 
34
    // Initialize the buffer that holds UART messages
35
    uart_1_data_p->buffer_in_read_ind = 0;
36
    uart_1_data_p->buffer_in_write_ind = 0;
37
    uart_1_data_p->buffer_in_len = 0;
38
    uart_1_data_p->buffer_in_len_tmp = 0;
39
}
40
 
155 Kevin 41
#pragma interrupt_level 0
152 Kevin 42
void UART1_Recv_Interrupt_Handler() {
155 Kevin 43
    char c;
44
 
152 Kevin 45
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
46
        c = RCREG1;
47
#ifdef UART1_RX_TO_BUFFER
48
        // Save received data into buffer
49
        uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_write_ind] = c;
50
        if (uart_1_data_p->buffer_in_write_ind == MAXUARTBUF - 1) {
51
            uart_1_data_p->buffer_in_write_ind = 0;
52
        } else {
53
            uart_1_data_p->buffer_in_write_ind++;
54
        }
55
 
56
        // Store the last MAXUARTBUF values entered
57
        if (uart_1_data_p->buffer_in_len_tmp < MAXUARTBUF) {
58
            uart_1_data_p->buffer_in_len_tmp++;
59
        } else {
60
            if (uart_1_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
61
                uart_1_data_p->buffer_in_read_ind = 0;
62
            } else {
63
                uart_1_data_p->buffer_in_read_ind++;
64
            }
65
        }
66
 
67
        // Update buffer size upon receiving newline (0x0D)
68
        if (c == UART1_BREAK_CHAR) {
69
            uart_1_data_p->buffer_in_len = uart_1_data_p->buffer_in_len_tmp;
70
            uart_1_data_p->buffer_in_len_tmp = 0;
71
        }
72
#endif
73
#ifdef UART1_RX_TO_XBEE
74
        XBee_Serial_In(c);
75
#endif
76
    }
77
 
78
    if (RCSTA1bits.OERR == 1) {
79
        // We've overrun the USART and must reset
80
        RCSTA1bits.CREN = 0; // Reset UART1
81
        RCSTA1bits.CREN = 1;
155 Kevin 82
        char output[64];
83
        sprintf(output, "UART1: (ERROR) Overrun Occurred\r\n");
84
        DBG_PRINT_UART(output, strlen(output));
152 Kevin 85
        TXSTA1bits.TXEN = 0; // Kill anything currently sending
86
    }
87
}
88
 
89
void UART1_Send_Interrupt_Handler() {
90
    // Put remaining data in TSR for transmit
91
    if (uart_1_data_p->buffer_out_ind != uart_1_data_p->buffer_out_len) {
92
        TXREG1 = uart_1_data_p->buffer_out[uart_1_data_p->buffer_out_ind];
93
        uart_1_data_p->buffer_out_ind++;
94
    } else {
95
        while (!TXSTA1bits.TRMT); // Wait for last byte to finish sending
96
        TXSTA1bits.TXEN = 0; // End transmission and disable TX interrupt
97
        uart_1_data_p->buffer_out_ind = 0;
98
        uart_1_data_p->buffer_out_len = 0;
99
    }
100
}
101
 
155 Kevin 102
//void UART1_WriteS(const char *fmt, ...) {
103
//#ifdef _DEBUG
104
//    char i;
105
//    va_list args;
106
//    va_start(args, fmt);
152 Kevin 107
//    vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
155 Kevin 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
//}
152 Kevin 128
 
155 Kevin 129
//void UART1_WriteF(float f, char m) {
130
//    long whole = 0;
131
//    unsigned long decimal = 0;
132
//    unsigned int multiplier = 1;
133
//    char i;
134
//
135
//    for (i = 0; i < m; i++)
136
//        multiplier *= 10;
137
//
138
//    whole = (long)((float)f);
139
//    decimal = (long)((float)f*multiplier) - whole*multiplier;
140
//    // Round up if necessary
141
//    if ((long)((float)f*multiplier*10) % 10 >= 5)
142
//        decimal += 1;
143
//#ifdef _DEBUG
144
//    sprintf((char *) uart_1_data_p->buffer_out, "%ld.%ld", whole, decimal);
145
//    uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
146
//    uart_1_data_p->buffer_out_ind = 1;
147
//    for (i = 0; i < uart_1_data_p->buffer_out_len; i++) {
148
//        TXREG1 = uart_1_data_p->buffer_out[i];
149
//        Nop();
150
//        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
151
//    }
152
//#else
153
//    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
154
//    sprintf((char *) uart_1_data_p->buffer_out, "%ld.%ld", whole, decimal);
155
//    uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
156
//    uart_1_data_p->buffer_out_ind = 1;
157
//    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
158
//    TXSTA1bits.TXEN = 1; // Begin transmission
159
//#endif
160
//}
161
 
162
void UART1_WriteS(char *string, char length) {
163
    char i;
152 Kevin 164
#ifdef _DEBUG
165
    for (i = 0; i < length; i++) {
155 Kevin 166
        TXREG1 = string[i];
152 Kevin 167
        Nop();
168
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
169
    }
170
#else
171
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
172
    uart_1_data_p->buffer_out_len = length;
173
    uart_1_data_p->buffer_out_ind = 1;
174
    for (i = 0; i < length; i++) {
155 Kevin 175
        uart_1_data_p->buffer_out[i] = string[i];
152 Kevin 176
    }
177
    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
178
    TXSTA1bits.TXEN = 1; // Begin transmission
179
#endif
180
}
181
 
155 Kevin 182
void UART1_WriteC(const char c) {
152 Kevin 183
#ifdef _DEBUG
184
    TXREG1 = c;
185
    Nop();
186
    while (!PIR1bits.TX1IF);
187
#else
188
    while (TXSTA1bits.TXEN);
189
    uart_1_data_p->buffer_out_len = 1;
190
    uart_1_data_p->buffer_out_ind = 1;
191
    TXREG1 = c;
192
    TXSTA1bits.TXEN = 1;
193
#endif
194
 
195
}
196
 
155 Kevin 197
char UART1_Buffer_Len() {
152 Kevin 198
    return uart_1_data_p->buffer_in_len;
199
}
200
 
201
/* Reader interface to the UART buffer, returns the number of bytes read */
155 Kevin 202
char UART1_Read_Buffer(char *buffer) {
203
    char i = 0;
152 Kevin 204
    while (uart_1_data_p->buffer_in_len != 0) {
205
        buffer[i] = uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_read_ind];
206
        i++;
207
        if (uart_1_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
208
            uart_1_data_p->buffer_in_read_ind = 0;
209
        } else {
210
            uart_1_data_p->buffer_in_read_ind++;
211
        }
212
        uart_1_data_p->buffer_in_len--;
213
    }
214
    return i;
215
}