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 "SPI1.h"
2
#include "SPI1.h"
5
 
3
 
6
static SPI1_DATA *spi_data_ptr;
4
static SPI1_DATA *spi_data_ptr;
7
 
5
 
8
void SPI1_Init(SPI1_DATA *data, void (*rx_callback)(char)) {
6
void SPI1_Init(SPI1_DATA *data, void (*rx_callback)(uint8_t)) {
9
    spi_data_ptr = data;
7
    spi_data_ptr = data;
10
    spi_data_ptr->buffer_out_ind = 0;
8
    spi_data_ptr->buffer_out_ind = 0;
11
    spi_data_ptr->buffer_out_len = 0;
9
    spi_data_ptr->buffer_out_len = 0;
12
    spi_data_ptr->rx_callback = rx_callback;
10
    spi_data_ptr->rx_callback = rx_callback;
13
    
11
    
Line 27... Line 25...
27
//    INTClearFlag(INT_SPI1RX);
25
//    INTClearFlag(INT_SPI1RX);
28
 
26
 
29
    // FSCK = FPB / (2 * (SPIxBRG + 1))
27
    // FSCK = FPB / (2 * (SPIxBRG + 1))
30
    IEC0CLR     = 0x03800000;   // Disable all SPI interrupts
28
    IEC0CLR     = 0x03800000;   // Disable all SPI interrupts
31
    SPI1CON     = 0;            // Stops and resets the SPI1.
29
    SPI1CON     = 0;            // Stops and resets the SPI1.
32
    int tmp     = SPI1BUF;      // Clears the receive buffer
30
    uint32_t tmp     = SPI1BUF;      // Clears the receive buffer
33
    IFS0CLR     = 0x03800000;   // Clear any existing event
31
    IFS0CLR     = 0x03800000;   // Clear any existing event
34
    IPC5CLR     = 0x1F000000;   // Clear the priority
32
    IPC5CLR     = 0x1F000000;   // Clear the priority
35
    IPC5SET     = 0x19000000;   // Set IPL=6, Subpriority 1
33
    IPC5SET     = 0x19000000;   // Set IPL=6, Subpriority 1
36
    SPI1BRG     = 0x1;          // Use FPB/4 clock frequency
34
    SPI1BRG     = 0x1;          // Use FPB/4 clock frequency
37
    SPI1STATCLR = 0x40;         // Clear the Overflow
35
    SPI1STATCLR = 0x40;         // Clear the Overflow
Line 43... Line 41...
43
    SPI1CON     = 0x18225;
41
    SPI1CON     = 0x18225;
44
 
42
 
45
    INTEnableInterrupts();
43
    INTEnableInterrupts();
46
}
44
}
47
 
45
 
48
int SPI1_Write(char *array, int length, void (*tx_callback)(void)) {
46
uint8_t SPI1_Write(uint8_t *array, uint32_t length, void (*tx_callback)(void)) {
49
    spi_data_ptr->tx_callback = tx_callback;
47
    spi_data_ptr->tx_callback = tx_callback;
50
 
48
 
51
    if (length > SPI1_BUFFER_OUT_SIZE)
49
    if (length > SPI1_BUFFER_OUT_SIZE)
52
        return 0;
50
        return 0;
53
    if (spi_data_ptr->buffer_out_len != 0)
51
    if (spi_data_ptr->buffer_out_len != 0)
54
        return 0;
52
        return 0;
55
 
53
 
56
    // Put the data to send into the outbound buffer
54
    // Put the data to send into the outbound buffer
57
    spi_data_ptr->buffer_out_len = length;
55
    spi_data_ptr->buffer_out_len = length;
58
    spi_data_ptr->buffer_out_ind = length-1;
56
    spi_data_ptr->buffer_out_ind = length-1;
59
    int i;
57
    int32_t i;
60
    for (i = 0; i < length; i++) {
58
    for (i = 0; i < length; i++) {
61
        spi_data_ptr->buffer_out[i] = array[i];
59
        spi_data_ptr->buffer_out[i] = array[i];
62
    }
60
    }
63
    IEC0SET = 0x02000000; // Enable TX interrupt
61
    IEC0SET = 0x02000000; // Enable TX interrupt
64
    return 1;
62
    return 1;
Line 75... Line 73...
75
        IFS0CLR = 0x00800000; // Clear the error flag
73
        IFS0CLR = 0x00800000; // Clear the error flag
76
    }
74
    }
77
 
75
 
78
    // Process SPI1 receive flag
76
    // Process SPI1 receive flag
79
    if (IFS0bits.SPI1RXIF) {
77
    if (IFS0bits.SPI1RXIF) {
80
        int i;
78
        int32_t i;
81
        // Read the data received from the last transfer
79
        // Read the data received from the last transfer
82
        int rxBufferCount = SPI1STATbits.RXBUFELM;
80
        int32_t rxBufferCount = SPI1STATbits.RXBUFELM;
83
        for (i = 0; i < rxBufferCount; i++) {
81
        for (i = 0; i < rxBufferCount; i++) {
84
            char c = SPI1BUF;
82
            int8_t c = SPI1BUF;
85
            // Call the RX callback function on the received data
83
            // Call the RX callback function on the received data
86
            if (spi_data_ptr->rx_callback != NULL)
84
            if (spi_data_ptr->rx_callback != NULL)
87
                (*spi_data_ptr->rx_callback)(c);
85
                (*spi_data_ptr->rx_callback)(c);
88
        }
86
        }
89
        IFS0CLR = 0x01000000; // Clear the RX flag
87
        IFS0CLR = 0x01000000; // Clear the RX flag
90
    }
88
    }
91
#endif
89
#endif
92
 
90
 
93
    // Process SPI1 transmit flag
91
    // Process SPI1 transmit flag
94
    if (IFS0bits.SPI1TXIF && IEC0bits.SPI1TXIE) {
92
    if (IFS0bits.SPI1TXIF && IEC0bits.SPI1TXIE) {
95
        int i;
93
        int32_t i;
96
        // Disable the transmit interrupt if all data has been sent
94
        // Disable the transmit interrupt if all data has been sent
97
        if (spi_data_ptr->buffer_out_len == 0) {
95
        if (spi_data_ptr->buffer_out_len == 0) {
98
            IEC0CLR=0x02000000;
96
            IEC0CLR=0x02000000;
99
            if (spi_data_ptr->tx_callback != NULL)
97
            if (spi_data_ptr->tx_callback != NULL)
100
                (*spi_data_ptr->tx_callback)();
98
                (*spi_data_ptr->tx_callback)();
101
        } else {
99
        } else {
102
            // Start transmitting the data in the buffer
100
            // Start transmitting the data in the buffer
103
            int txBufferFree = 16 - SPI1STATbits.TXBUFELM;
101
            int32_t txBufferFree = 16 - SPI1STATbits.TXBUFELM;
104
            if (spi_data_ptr->buffer_out_len > txBufferFree) {
102
            if (spi_data_ptr->buffer_out_len > txBufferFree) {
105
                for (i = 0; i < txBufferFree; i++) {
103
                for (i = 0; i < txBufferFree; i++) {
106
                    SPI1BUF = spi_data_ptr->buffer_out[spi_data_ptr->buffer_out_ind];
104
                    SPI1BUF = spi_data_ptr->buffer_out[spi_data_ptr->buffer_out_ind];
107
                    spi_data_ptr->buffer_out_ind--;
105
                    spi_data_ptr->buffer_out_ind--;
108
                }
106
                }