Subversion Repositories Code-Repo

Rev

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

Rev 216 Rev 231
Line 1... Line -...
1
#include <xc.h>
-
 
2
#include <plib.h>
-
 
3
#include "defines.h"
1
#include "defines.h"
4
#include "UART1.h"
2
#include "UART1.h"
5
 
3
 
6
static UART1_DATA *uart_data_ptr;
4
static UART1_DATA *uart_data_ptr;
7
 
5
 
Line 14... Line 12...
14
 * Baud Rate Calculation (BRGH = 1):
12
 * Baud Rate Calculation (BRGH = 1):
15
 * Baud Rate = PerfBusFreq / (4 * (BRG + 1))
13
 * Baud Rate = PerfBusFreq / (4 * (BRG + 1))
16
 * BRG = PerfBusFreq / (4 * Baud Rate) - 1
14
 * BRG = PerfBusFreq / (4 * Baud Rate) - 1
17
 */
15
 */
18
 
16
 
19
void UART1_Init(UART1_DATA *data, void (*rx_callback)(char)) {
17
void UART1_Init(UART1_DATA *data, void (*rx_callback)(uint8_t)) {
20
    uart_data_ptr = data;
18
    uart_data_ptr = data;
21
    uart_data_ptr->rx_callback = rx_callback;
19
    uart_data_ptr->rx_callback = rx_callback;
22
    uart_data_ptr->buffer_out_len = 0;
20
    uart_data_ptr->buffer_out_len = 0;
23
    uart_data_ptr->buffer_out_ind = 0;
21
    uart_data_ptr->buffer_out_ind = 0;
24
 
22
 
Line 43... Line 41...
43
 
41
 
44
    TRISDbits.TRISD14 = 0;
42
    TRISDbits.TRISD14 = 0;
45
    PORTDbits.RD14 = 0;
43
    PORTDbits.RD14 = 0;
46
}
44
}
47
 
45
 
48
int UART1_Write(char *string, int length) {
46
uint8_t UART1_Write(uint8_t *string, uint32_t length) {
49
    if (length > UART1_BUFFER_SIZE)
47
    if (length > UART1_BUFFER_SIZE)
50
        return 0;
48
        return 0;
51
    if (uart_data_ptr->buffer_out_len != 0)
49
    if (uart_data_ptr->buffer_out_len != 0)
52
        return 0;
50
        return 0;
53
 
51
 
54
    // Put the data to send into the outbound buffer
52
    // Put the data to send into the outbound buffer
55
    uart_data_ptr->buffer_out_len = length;
53
    uart_data_ptr->buffer_out_len = length;
56
    uart_data_ptr->buffer_out_ind = 0;
54
    uart_data_ptr->buffer_out_ind = 0;
57
    int i;
55
    uint8_t i;
58
    for (i = 0; i < length; i++) {
56
    for (i = 0; i < length; i++) {
59
        uart_data_ptr->buffer_out[i] = string[i];
57
        uart_data_ptr->buffer_out[i] = string[i];
60
    }
58
    }
61
    IEC0SET = 0x10000000; // Enable TX interrupt
59
    IEC0SET = 0x10000000; // Enable TX interrupt
62
    return 1;
60
    return 1;
Line 80... Line 78...
80
 
78
 
81
    // Process UART1 recieve flag
79
    // Process UART1 recieve flag
82
    if (IFS0bits.U1RXIF) {
80
    if (IFS0bits.U1RXIF) {
83
        // Read the data received from the last transfer
81
        // Read the data received from the last transfer
84
        while (U1STAbits.URXDA) {
82
        while (U1STAbits.URXDA) {
85
            char c = U1RXREG;
83
            uint8_t c = U1RXREG;
86
            // Call the RX callback function on each received data
84
            // Call the RX callback function on each received data
87
            if (uart_data_ptr->rx_callback != NULL) {
85
            if (uart_data_ptr->rx_callback != NULL) {
88
                (*uart_data_ptr->rx_callback)(c);
86
                (*uart_data_ptr->rx_callback)(c);
89
            }
87
            }
90
        }
88
        }