Subversion Repositories Code-Repo

Rev

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

Rev 152 Rev 155
Line 2... Line 2...
2
#include <string.h>
2
#include <string.h>
3
#include <stdio.h>
3
#include <stdio.h>
4
#include "defines.h"
4
#include "defines.h"
5
#include "uart.h"
5
#include "uart.h"
6
 
6
 
7
static UART_DATA uart_1_data;
7
static UART_DATA *uart_1_data_p;
8
static UART_DATA *uart_1_data_p = &uart_1_data;
-
 
9
 
8
 
10
void UART1_Init() {
9
void UART1_Init(UART_DATA *data) {
11
    // Configure the hardware USART device
-
 
12
    // UART1 TX RC6
-
 
13
    // UART1 RX RC7
10
    uart_1_data_p = data;
14
 
11
 
15
    UART1_TX_TRIS = 0; // Tx pin set to output
12
    UART1_TX_TRIS = 0; // Tx pin set to output
16
    UART1_RX_TRIS = 1; // Rx pin set to input
13
    UART1_RX_TRIS = 1; // Rx pin set to input
17
 
14
 
18
    BAUDCON1bits.BRG16 = 0; // 8-bit baud rate generator
15
    BAUDCON1bits.BRG16 = 0; // 8-bit baud rate generator
Line 39... Line 36...
39
    uart_1_data_p->buffer_in_write_ind = 0;
36
    uart_1_data_p->buffer_in_write_ind = 0;
40
    uart_1_data_p->buffer_in_len = 0;
37
    uart_1_data_p->buffer_in_len = 0;
41
    uart_1_data_p->buffer_in_len_tmp = 0;
38
    uart_1_data_p->buffer_in_len_tmp = 0;
42
}
39
}
43
 
40
 
-
 
41
#pragma interrupt_level 0
44
void UART1_Recv_Interrupt_Handler() {
42
void UART1_Recv_Interrupt_Handler() {
45
    unsigned char c;
43
    char c;
-
 
44
   
46
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
45
    if (PIR1bits.RC1IF) { // Check if data receive flag is set
47
        c = RCREG1;
46
        c = RCREG1;
48
#ifdef UART1_RX_TO_BUFFER
47
#ifdef UART1_RX_TO_BUFFER
49
        // Save received data into buffer
48
        // Save received data into buffer
50
        uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_write_ind] = c;
49
        uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_write_ind] = c;
Line 78... Line 77...
78
 
77
 
79
    if (RCSTA1bits.OERR == 1) {
78
    if (RCSTA1bits.OERR == 1) {
80
        // We've overrun the USART and must reset
79
        // We've overrun the USART and must reset
81
        RCSTA1bits.CREN = 0; // Reset UART1
80
        RCSTA1bits.CREN = 0; // Reset UART1
82
        RCSTA1bits.CREN = 1;
81
        RCSTA1bits.CREN = 1;
-
 
82
        char output[64];
83
        DBG_PRINT_UART("UART1: (ERROR) overrun\r\n");
83
        sprintf(output, "UART1: (ERROR) Overrun Occurred\r\n");
-
 
84
        DBG_PRINT_UART(output, strlen(output));
84
        TXSTA1bits.TXEN = 0; // Kill anything currently sending
85
        TXSTA1bits.TXEN = 0; // Kill anything currently sending
85
    }
86
    }
86
}
87
}
87
 
88
 
88
void UART1_Send_Interrupt_Handler() {
89
void UART1_Send_Interrupt_Handler() {
Line 96... Line 97...
96
        uart_1_data_p->buffer_out_ind = 0;
97
        uart_1_data_p->buffer_out_ind = 0;
97
        uart_1_data_p->buffer_out_len = 0;
98
        uart_1_data_p->buffer_out_len = 0;
98
    }
99
    }
99
}
100
}
100
 
101
 
101
void UART1_WriteS(const char *fmt, ...) {
102
//void UART1_WriteS(const char *fmt, ...) {
102
#ifdef _DEBUG
103
//#ifdef _DEBUG
103
    unsigned char i;
104
//    char i;
104
    va_list args;
105
//    va_list args;
105
    va_start(args, fmt);
106
//    va_start(args, fmt);
106
//    vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
107
//    vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
107
    vprintf(fmt, args);
-
 
108
    va_end(args);
108
//    va_end(args);
109
    uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
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;
110
//    uart_1_data_p->buffer_out_ind = 1;
111
    for (i = 0; i < uart_1_data_p->buffer_out_len; i++) {
111
//    for (i = 0; i < uart_1_data_p->buffer_out_len; i++) {
112
        TXREG1 = uart_1_data_p->buffer_out[i];
112
//        TXREG1 = uart_1_data_p->buffer_out[i];
113
        Nop();
113
//        Nop();
114
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
114
//        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
115
    }
115
//    }
116
#else
116
//#else
117
    va_list args;
117
//    va_list args;
118
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
118
//    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
119
    va_start(args, fmt);
119
//    va_start(args, fmt);
120
    vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
120
//    vsprintf((char *) uart_1_data_p->buffer_out, fmt, args);
121
    va_end(args);
121
//    va_end(args);
122
    uart_1_data_p->buffer_out_len = strlen((char *) uart_1_data_p->buffer_out);
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;
123
//    uart_1_data_p->buffer_out_ind = 1;
124
    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
124
//    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
125
    TXSTA1bits.TXEN = 1; // Begin transmission
125
//    TXSTA1bits.TXEN = 1; // Begin transmission
126
#endif
126
//#endif
-
 
127
//}
127
}
128
 
-
 
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
//}
128
 
161
 
129
void UART1_WriteB(const char *msg, unsigned char length) {
162
void UART1_WriteS(char *string, char length) {
130
    unsigned char i;
163
    char i;
131
#ifdef _DEBUG
164
#ifdef _DEBUG
132
    for (i = 0; i < length; i++) {
165
    for (i = 0; i < length; i++) {
133
        TXREG1 = msg[i];
166
        TXREG1 = string[i];
134
        Nop();
167
        Nop();
135
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
168
        while (!PIR1bits.TX1IF); // Wait for byte to be transmitted
136
    }
169
    }
137
#else
170
#else
138
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
171
    while (TXSTA1bits.TXEN); // Wait for previous message to finish sending
139
    uart_1_data_p->buffer_out_len = length;
172
    uart_1_data_p->buffer_out_len = length;
140
    uart_1_data_p->buffer_out_ind = 1;
173
    uart_1_data_p->buffer_out_ind = 1;
141
    for (i = 0; i < length; i++) {
174
    for (i = 0; i < length; i++) {
142
        uart_1_data_p->buffer_out[i] = msg[i];
175
        uart_1_data_p->buffer_out[i] = string[i];
143
    }
176
    }
144
    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
177
    TXREG1 = uart_1_data_p->buffer_out[0]; // Put first byte in TSR
145
    TXSTA1bits.TXEN = 1; // Begin transmission
178
    TXSTA1bits.TXEN = 1; // Begin transmission
146
#endif
179
#endif
147
}
180
}
148
 
181
 
149
void UART1_WriteC(const unsigned char c) {
182
void UART1_WriteC(const char c) {
150
#ifdef _DEBUG
183
#ifdef _DEBUG
151
    TXREG1 = c;
184
    TXREG1 = c;
152
    Nop();
185
    Nop();
153
    while (!PIR1bits.TX1IF);
186
    while (!PIR1bits.TX1IF);
154
#else
187
#else
Line 159... Line 192...
159
    TXSTA1bits.TXEN = 1;
192
    TXSTA1bits.TXEN = 1;
160
#endif
193
#endif
161
 
194
 
162
}
195
}
163
 
196
 
164
unsigned char UART1_Buffer_Len() {
197
char UART1_Buffer_Len() {
165
    return uart_1_data_p->buffer_in_len;
198
    return uart_1_data_p->buffer_in_len;
166
}
199
}
167
 
200
 
168
/* Reader interface to the UART buffer, returns the number of bytes read */
201
/* Reader interface to the UART buffer, returns the number of bytes read */
169
unsigned char UART1_Read_Buffer(unsigned char *buffer) {
202
char UART1_Read_Buffer(char *buffer) {
170
    unsigned char i = 0;
203
    char i = 0;
171
    while (uart_1_data_p->buffer_in_len != 0) {
204
    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];
205
        buffer[i] = uart_1_data_p->buffer_in[uart_1_data_p->buffer_in_read_ind];
173
        i++;
206
        i++;
174
        if (uart_1_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
207
        if (uart_1_data_p->buffer_in_read_ind == MAXUARTBUF - 1) {
175
            uart_1_data_p->buffer_in_read_ind = 0;
208
            uart_1_data_p->buffer_in_read_ind = 0;