Subversion Repositories Code-Repo

Rev

Rev 315 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
315 Kevin 1
#include "defines.h"
2
#include "SPI.h"
3
 
4
static SPI_DATA *spi_data_p;
5
 
6
void SPI_Init(SPI_DATA *data, uint8_t speed) {
7
    spi_data_p = data;
8
 
9
    SPI_MOSI_TRIS = 0;
10
    SPI_CLK_TRIS = 0;
11
 
12
#ifndef SPI_WRITE_ONLY
13
    SPI_MISO_TRIS = 1;
14
#endif
15
 
16
//    SPI_SLAVE_SELECT_TRIS = 0; // SPI2 slave select
17
//    SPI_SLAVE_SELECT_LAT = 1; // SPI2 SS high (Idle)
18
 
19
//    SPI_RESET_TRIS = 0; // SPI2 reset
20
//    SPI_RESET_LAT = 1; // SPI2 reset active low
21
 
22
    SPI_DC_SELECT_TRIS = 0; // SPI2 D/C select
23
    SPI_DC_SELECT_LAT = 0;
24
 
25
    SSP1STATbits.SMP = 0; // Input is sampled in the middle of data output time
26
    SSP1STATbits.CKE = 0; // Transmit occurs on transition from Idle to active clock state
27
 
28
    SSP1CON1bits.SSPM = speed;
29
 
30
    SSP1CON1bits.CKP = 1;   // Idle state for clock is a high level
31
    SSP1CON1bits.SSPEN = 1; // Enable MSSP module
32
 
33
    PIE1bits.SSP1IE = 0;    // Disable MSSP interrupt
34
 
35
#ifndef SPI_WRITE_ONLY
36
    spi_data_p->buffer_in_len = 0;
37
    spi_data_p->buffer_in_read_ind = 0;
38
    spi_data_p->buffer_in_write_ind = 0;
39
#endif
40
    spi_data_p->buffer_out_ind = 0;
41
    spi_data_p->buffer_out_len = 0;
42
}
43
 
44
void SPI_Write(uint8_t *msg, uint16_t length) {
45
    uint16_t i = 0;
46
 
47
//    SPI_SLAVE_SELECT_LAT = 0;
48
    while (i != length) {
49
        SSP1BUF = msg[i];
50
        i++;
51
        while (!SSP1STATbits.BF);
52
 
53
#ifndef SPI_WRITE_ONLY
54
        spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
55
        if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
56
            spi_data_p->buffer_in_write_ind = 0;
57
        } else {
58
            spi_data_p->buffer_in_write_ind++;
59
        }
60
        spi_data_p->buffer_in_len++;
61
#else
62
        // Read data in buffer to clear it
63
        uint8_t tmp = SSP1BUF;
64
#endif
65
    }
66
//    SPI_SLAVE_SELECT_LAT = 1;
67
}
68
 
69
void SPI2_Write_Repeat(uint8_t c, uint16_t length) {
70
    uint16_t i = 0;
71
//    SPI_SLAVE_SELECT_LAT = 0;
72
    while (i != length) {
73
        SSP1BUF = c;
74
        i++;
75
        while (!SSP1STATbits.BF);
76
 
77
#ifndef SPI_WRITE_ONLY
78
        spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
79
        if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
80
            spi_data_p->buffer_in_write_ind = 0;
81
        } else {
82
            spi_data_p->buffer_in_write_ind++;
83
        }
84
        spi_data_p->buffer_in_len++;
85
#else
86
        // Read data in buffer to clear it
87
        uint8_t tmp = SSP1BUF;
88
#endif
89
    }
90
//    SPI_SLAVE_SELECT_LAT = 1;
91
}
92
 
93
#ifndef SPI_WRITE_ONLY
94
void SPI_Recv_Interrupt_Handler() {
95
    uint8_t c;
96
 
97
    if (SSP1STATbits.BF) { // Check if data receive flag is set
98
        if (spi_data_p->buffer_in_len == MAX_SPI_BUFFER - 1) {
99
            c = SSP1BUF; // Read SSP2BUF to clear it
100
        } else {
101
            // Save received data into buffer
102
            spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP1BUF;
103
            if (spi_data_p->buffer_in_write_ind == MAX_SPI_BUFFER - 1) {
104
                spi_data_p->buffer_in_write_ind = 0;
105
            } else {
106
                spi_data_p->buffer_in_write_ind++;
107
            }
108
            spi_data_p->buffer_in_len++;
109
 
110
            // Put next byte in SSP2BUF for transmit
111
            if (spi_data_p->buffer_out_ind != spi_data_p->buffer_out_len) {
112
                SSP1BUF = spi_data_p->buffer_out[spi_data_p->buffer_out_ind];
113
                spi_data_p->buffer_out_ind++;
114
            } else {
115
//                SPI_SLAVE_SELECT_LAT = 1; // Bring SS line high
116
                spi_data_p->buffer_out_ind = 0;
117
                spi_data_p->buffer_out_len = 0;
118
            }
119
        }
120
    }
121
}
122
 
123
void SPI_Read(char length) {
124
//    SPI_SLAVE_SELECT_LAT = 0;
125
 
126
    for (uint8_t i = 0; i < length; i++) {
127
        SSP1BUF = 0x0;
128
        while (!SSP1STATbits.BF);
129
 
130
        spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP1BUF;
131
        if (spi_data_p->buffer_in_write_ind == MAX_SPI_BUFFER - 1) {
132
            spi_data_p->buffer_in_write_ind = 0;
133
        } else {
134
            spi_data_p->buffer_in_write_ind++;
135
        }
136
        spi_data_p->buffer_in_len++;
137
    }
138
//    SPI_SLAVE_SELECT_LAT = 1;
139
}
140
 
141
uint8_t SPI_Buffer_Len() {
142
    return spi_data_p->buffer_in_len;
143
}
144
 
145
uint8_t SPI_Read_Buffer(uint8_t* buffer) {
146
    uint8_t i = 0;
147
    while (spi_data_p->buffer_in_len != 0) {
148
        buffer[i] = spi_data_p->buffer_in[spi_data_p->buffer_in_read_ind];
149
        i++;
150
        if (spi_data_p->buffer_in_read_ind == MAX_SPI_BUFFER - 1) {
151
            spi_data_p->buffer_in_read_ind = 0;
152
        } else {
153
            spi_data_p->buffer_in_read_ind++;
154
        }
155
        spi_data_p->buffer_in_len--;
156
    }
157
    return i;
158
}
159
#endif