Subversion Repositories Code-Repo

Rev

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

Rev 226 Rev 231
Line 1... Line -...
1
#include <xc.h>
-
 
2
#include <plib.h>
-
 
3
#include "defines.h"
1
#include "defines.h"
4
#include "SPI4.h"
2
#include "SPI4.h"
5
 
3
 
6
static SPI4_DATA *spi_data_ptr;
4
static SPI4_DATA *spi_data_ptr;
7
 
5
 
Line 18... Line 16...
18
    // Note: FIFO enhanced buffer depth is 4/8/16 for 32/16/8 bit widths
16
    // Note: FIFO enhanced buffer depth is 4/8/16 for 32/16/8 bit widths
19
 
17
 
20
    // FSCK = FPB / (2 * (SPIxBRG + 1))
18
    // FSCK = FPB / (2 * (SPIxBRG + 1))
21
    IEC1CLR     = 0x00000700;   // Disable all SPI interrupts
19
    IEC1CLR     = 0x00000700;   // Disable all SPI interrupts
22
    SPI4CON     = 0;            // Stops and resets the SPI4.
20
    SPI4CON     = 0;            // Stops and resets the SPI4.
23
    int tmp     = SPI4BUF;      // Clears the receive buffer
21
    uint32_t tmp     = SPI4BUF;      // Clears the receive buffer
24
    IFS1CLR     = 0x00000700;   // Clear any existing event
22
    IFS1CLR     = 0x00000700;   // Clear any existing event
25
    IPC8CLR     = 0x0000001F;   // Clear the priority
23
    IPC8CLR     = 0x0000001F;   // Clear the priority
26
    IPC8SET     = 0x0000001A;   // Set IPL=6, Subpriority 2
24
    IPC8SET     = 0x0000001A;   // Set IPL=6, Subpriority 2
27
    SPI4BRG     = 0x4;          // Use FPB/10 clock frequency
25
    SPI4BRG     = 0x4;          // Use FPB/10 clock frequency
28
    SPI4STATCLR = 0x40;         // Clear the Overflow
26
    SPI4STATCLR = 0x40;         // Clear the Overflow
Line 33... Line 31...
33
    SPI4CON     = 0x18225;
31
    SPI4CON     = 0x18225;
34
 
32
 
35
    INTEnableInterrupts();
33
    INTEnableInterrupts();
36
}
34
}
37
 
35
 
38
int SPI4_Read(int length, void (*rx_callback)(char, char *)) {
36
uint8_t SPI4_Read(uint32_t length, void (*rx_callback)(uint8_t, uint8_t *)) {
39
    spi_data_ptr->rx_callback = rx_callback;
37
    spi_data_ptr->rx_callback = rx_callback;
40
 
38
 
41
    // Ensure that the receiving buffer is large enough
39
    // Ensure that the receiving buffer is large enough
42
    if (length > SPI4_BUFFER_IN_SIZE)
40
    if (length > SPI4_BUFFER_IN_SIZE)
43
        return 0;
41
        return 0;
Line 52... Line 50...
52
    
50
    
53
    SPI4_Write(NULL, length, NULL);
51
    SPI4_Write(NULL, length, NULL);
54
    return 1;
52
    return 1;
55
}
53
}
56
 
54
 
57
int SPI4_Write(char *array, int length, void (*tx_callback)(void)) {
55
uint8_t SPI4_Write(uint8_t *array, uint32_t length, void (*tx_callback)(void)) {
58
    spi_data_ptr->tx_callback = tx_callback;
56
    spi_data_ptr->tx_callback = tx_callback;
59
 
57
 
60
    // We only care about the transmit length if we are sending data
58
    // We only care about the transmit length if we are sending data
61
    if (length > SPI4_BUFFER_OUT_SIZE && !spi_data_ptr->write_blank)
59
    if (length > SPI4_BUFFER_OUT_SIZE && !spi_data_ptr->write_blank)
62
        return 0;
60
        return 0;
Line 69... Line 67...
69
    spi_data_ptr->buffer_out_len = length;
67
    spi_data_ptr->buffer_out_len = length;
70
    spi_data_ptr->buffer_out_ind = 0;
68
    spi_data_ptr->buffer_out_ind = 0;
71
 
69
 
72
    // Copy only if we are actually going to transmit data
70
    // Copy only if we are actually going to transmit data
73
    if (!spi_data_ptr->write_blank) {
71
    if (!spi_data_ptr->write_blank) {
74
        int i;
72
        int32_t i;
75
        for (i = 0; i < length; i++) {
73
        for (i = 0; i < length; i++) {
76
            spi_data_ptr->buffer_out[i] = array[i];
74
            spi_data_ptr->buffer_out[i] = array[i];
77
        }
75
        }
78
    }
76
    }
79
    IEC1SET = 0x00000400; // Enable TX interrupt
77
    IEC1SET = 0x00000400; // Enable TX interrupt
Line 90... Line 88...
90
        IFS1CLR = 0x00000100; // Clear the error flag
88
        IFS1CLR = 0x00000100; // Clear the error flag
91
    }
89
    }
92
 
90
 
93
    // Process SPI4 receive flag
91
    // Process SPI4 receive flag
94
    if (IFS1bits.SPI4RXIF) {
92
    if (IFS1bits.SPI4RXIF) {
95
        int i;
93
        uint32_t i;
96
        // Read the data received from the last transfer
94
        // Read the data received from the last transfer
97
        int rxBufferCount = SPI4STATbits.RXBUFELM;
95
        uint32_t rxBufferCount = SPI4STATbits.RXBUFELM;
98
        for (i = 0; i < rxBufferCount; i++) {
96
        for (i = 0; i < rxBufferCount; i++) {
99
            char c = SPI4BUF;
97
            int8_t c = SPI4BUF;
100
            // Put the received data into the buffer
98
            // Put the received data into the buffer
101
            if (spi_data_ptr->buffer_in_len != 0) {
99
            if (spi_data_ptr->buffer_in_len != 0) {
102
                spi_data_ptr->buffer_in[spi_data_ptr->buffer_in_ind] = c;
100
                spi_data_ptr->buffer_in[spi_data_ptr->buffer_in_ind] = c;
103
                spi_data_ptr->buffer_in_ind++;
101
                spi_data_ptr->buffer_in_ind++;
104
                // If done acquiring requested length, reset
102
                // If done acquiring requested length, reset
Line 113... Line 111...
113
        IFS1CLR = 0x00000200; // Clear the RX flag
111
        IFS1CLR = 0x00000200; // Clear the RX flag
114
    }
112
    }
115
 
113
 
116
    // Process SPI4 transmit flag
114
    // Process SPI4 transmit flag
117
    if (IFS1bits.SPI4TXIF && IEC1bits.SPI4TXIE) {
115
    if (IFS1bits.SPI4TXIF && IEC1bits.SPI4TXIE) {
118
        int i;
116
        int32_t i;
119
        // Disable the transmit interrupt if all data has been sent
117
        // Disable the transmit interrupt if all data has been sent
120
        if (spi_data_ptr->buffer_out_len == 0) {
118
        if (spi_data_ptr->buffer_out_len == 0) {
121
            IEC1CLR=0x00000400;
119
            IEC1CLR=0x00000400;
122
            spi_data_ptr->write_blank = 0;
120
            spi_data_ptr->write_blank = 0;
123
            // Call the TX callback function at end of transmission
121
            // Call the TX callback function at end of transmission
124
            if (spi_data_ptr->tx_callback != NULL)
122
            if (spi_data_ptr->tx_callback != NULL)
125
                (*spi_data_ptr->tx_callback)();
123
                (*spi_data_ptr->tx_callback)();
126
        } else {
124
        } else {
127
            // Start transmitting the data in the buffer
125
            // Start transmitting the data in the buffer
128
            int txBufferFree = 16 - SPI4STATbits.TXBUFELM;
126
            int32_t txBufferFree = 16 - SPI4STATbits.TXBUFELM;
129
            if (spi_data_ptr->buffer_out_len > txBufferFree) {
127
            if (spi_data_ptr->buffer_out_len > txBufferFree) {
130
                for (i = 0; i < txBufferFree; i++) {
128
                for (i = 0; i < txBufferFree; i++) {
131
                    if (spi_data_ptr->write_blank) {
129
                    if (spi_data_ptr->write_blank) {
132
                        SPI4BUF = 0x00;
130
                        SPI4BUF = 0x00;
133
                    } else {
131
                    } else {