Subversion Repositories Code-Repo

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
199 Kevin 1
#include <xc.h>
2
#include <plib.h>
3
#include "defines.h"
4
#include "SPI1.h"
5
 
201 Kevin 6
static SPI1_DATA *spi_data_ptr;
199 Kevin 7
 
212 Kevin 8
void SPI1_Init(SPI1_DATA *data, void (*rx_callback)(char)) {
201 Kevin 9
    spi_data_ptr = data;
212 Kevin 10
    spi_data_ptr->buffer_out_ind = 0;
11
    spi_data_ptr->buffer_out_len = 0;
12
    spi_data_ptr->rx_callback = rx_callback;
199 Kevin 13
 
14
    INTDisableInterrupts();
15
 
16
    // Note: FIFO enhanced buffer depth is 4/8/16 for 32/16/8 bit widths
17
 
18
    // Alternative Configuration:
19
    // The third value is the SPI bitrate which is 1/2 the frequency of the
20
    //  desired clock frequency. Thus 40Mhz / (20Mhz / 2) = 4.
21
    // Note: SPI_OPEN_TBE_NOT_FULL should only be used at >10Mhz speeds
22
//    SpiChnOpen(SPI_CHANNEL1, SPI_OPEN_MSTEN | SPI_OPEN_ENHBUF | SPI_OPEN_TBE_NOT_FULL | SPI_OPEN_RBF_NOT_EMPTY, 4);
23
//    INTSetVectorPriority(INT_SPI_1_VECTOR, INT_PRIORITY_LEVEL_6);
24
//    INTSetVectorSubPriority(INT_SPI_1_VECTOR, INT_SUB_PRIORITY_LEVEL_1);
25
//    INTClearFlag(INT_SPI1E);
26
//    INTClearFlag(INT_SPI1TX);
27
//    INTClearFlag(INT_SPI1RX);
28
 
29
    // FSCK = FPB / (2 * (SPIxBRG + 1))
30
    IEC0CLR     = 0x03800000;   // Disable all SPI interrupts
31
    SPI1CON     = 0;            // Stops and resets the SPI1.
32
    int tmp     = SPI1BUF;      // Clears the receive buffer
33
    IFS0CLR     = 0x03800000;   // Clear any existing event
34
    IPC5CLR     = 0x1F000000;   // Clear the priority
35
    IPC5SET     = 0x19000000;   // Set IPL=6, Subpriority 1
36
    SPI1BRG     = 0x1;          // Use FPB/4 clock frequency
37
    SPI1STATCLR = 0x40;         // Clear the Overflow
212 Kevin 38
#ifndef SPI_WRITE_ONLY
199 Kevin 39
    IEC0SET = 0x01800000;       // Enable RX and Error interrupts
40
#endif
41
    // Enhanced buffer, SPI on, 8 bits transfer, SMP=1, Master mode
42
    // SPIxTXIF set on buffer empty, SPIxRXIF set on buffer not empty
43
    SPI1CON     = 0x18225;
44
 
45
    INTEnableInterrupts();
46
}
47
 
226 Kevin 48
int SPI1_Write(char *array, int length, void (*tx_callback)(void)) {
212 Kevin 49
    spi_data_ptr->tx_callback = tx_callback;
199 Kevin 50
 
226 Kevin 51
    if (length > SPI1_BUFFER_OUT_SIZE)
199 Kevin 52
        return 0;
212 Kevin 53
    if (spi_data_ptr->buffer_out_len != 0)
199 Kevin 54
        return 0;
55
 
212 Kevin 56
    // Put the data to send into the outbound buffer
226 Kevin 57
    spi_data_ptr->buffer_out_len = length;
58
    spi_data_ptr->buffer_out_ind = length-1;
199 Kevin 59
    int i;
226 Kevin 60
    for (i = 0; i < length; i++) {
212 Kevin 61
        spi_data_ptr->buffer_out[i] = array[i];
199 Kevin 62
    }
63
    IEC0SET = 0x02000000; // Enable TX interrupt
64
    return 1;
65
}
66
 
67
void __ISR(_SPI_1_VECTOR, ipl6) __SPI_1_Interrupt_Handler(void) {
212 Kevin 68
#ifndef SPI_WRITE_ONLY
199 Kevin 69
    // Process SPI1 error flag
70
    if (IFS0bits.SPI1EIF) {
71
        // Clear the receive overflow bit if it is set
72
        if (SPI1STATbits.SPIROV) {
73
            SPI1STATbits.SPIROV = 0;
74
        }
75
        IFS0CLR = 0x00800000; // Clear the error flag
76
    }
77
 
78
    // Process SPI1 receive flag
79
    if (IFS0bits.SPI1RXIF) {
80
        int i;
81
        // Read the data received from the last transfer
82
        int rxBufferCount = SPI1STATbits.RXBUFELM;
212 Kevin 83
        for (i = 0; i < rxBufferCount; i++) {
84
            char c = SPI1BUF;
85
            // Call the RX callback function on the received data
86
            if (spi_data_ptr->rx_callback != NULL)
87
                (*spi_data_ptr->rx_callback)(c);
199 Kevin 88
        }
89
        IFS0CLR = 0x01000000; // Clear the RX flag
90
    }
91
#endif
92
 
93
    // Process SPI1 transmit flag
215 Kevin 94
    if (IFS0bits.SPI1TXIF && IEC0bits.SPI1TXIE) {
199 Kevin 95
        int i;
96
        // Disable the transmit interrupt if all data has been sent
212 Kevin 97
        if (spi_data_ptr->buffer_out_len == 0) {
199 Kevin 98
            IEC0CLR=0x02000000;
212 Kevin 99
            if (spi_data_ptr->tx_callback != NULL)
100
                (*spi_data_ptr->tx_callback)();
199 Kevin 101
        } else {
102
            // Start transmitting the data in the buffer
103
            int txBufferFree = 16 - SPI1STATbits.TXBUFELM;
212 Kevin 104
            if (spi_data_ptr->buffer_out_len > txBufferFree) {
199 Kevin 105
                for (i = 0; i < txBufferFree; i++) {
212 Kevin 106
                    SPI1BUF = spi_data_ptr->buffer_out[spi_data_ptr->buffer_out_ind];
107
                    spi_data_ptr->buffer_out_ind--;
199 Kevin 108
                }
212 Kevin 109
                spi_data_ptr->buffer_out_len -= txBufferFree;
199 Kevin 110
            } else {
212 Kevin 111
                for (i = 0; i < spi_data_ptr->buffer_out_len; i++) {
112
                    SPI1BUF = spi_data_ptr->buffer_out[spi_data_ptr->buffer_out_ind];
113
                    spi_data_ptr->buffer_out_ind--;
199 Kevin 114
                }
212 Kevin 115
                spi_data_ptr->buffer_out_len = 0;
199 Kevin 116
            }
117
        }
118
        IFS0CLR = 0x02000000; // Clear the TX flag
119
    }
120
}