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