Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

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