Subversion Repositories Code-Repo

Rev

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

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