Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
155 Kevin 1
#include <xc.h>
2
#include <stdio.h>
3
#include <string.h>
4
#include "defines.h"
158 Kevin 5
#include "base_SPI.h"
6
#include "base_UART.h"
155 Kevin 7
 
8
static SPI_DATA *spi_data_p;
9
 
10
void SPI2_Init(SPI_DATA *data, char speed) {
11
    spi_data_p = data;
12
 
13
    // Set up SPI2 with specified pins
14
    RPINR22 = PPS_SPI2_CLK_IN; // SPI2 CLK Input
15
    PPS_SPI2_CLK_OUT = 11; // SPI2 CLK Output
16
 
17
#ifndef SPI2_WRITE_ONLY
18
    RPINR21 = PPS_SPI2_MISO; // SPI2 Data Input
19
    SPI_MISO_TRIS = 1; // SPI2 data in pin (MISO)
20
#endif
21
 
22
    SPI_CLK_TRIS = 0; // SPI2 clock pin
23
    PPS_SPI2_MOSI = 10; // SPI2 Data Output (MOSI)
24
    SPI_MOSI_TRIS = 0; // SPI2 data out pin (MOSI)
25
 
26
    SPI_SLAVE_SELECT_TRIS = 0; // SPI2 slave select
27
    SPI_SLAVE_SELECT_LAT = 1; // SPI2 SS high (Idle)
28
 
29
    SPI_RESET_TRIS = 0; // SPI2 reset
30
    SPI_RESET_LAT = 1; // SPI2 reset active low
31
 
32
    SPI_DC_SELECT_TRIS = 0; // SPI2 D/C select
33
    SPI_DC_SELECT_LAT = 0;
34
 
35
    SSP2STATbits.SMP = 0; // Input is sampled in the middle of data output time
36
    SSP2STATbits.CKE = 0; // Transmit occurs on transition from Idle to active clock state
37
 
38
    SSP2CON1bits.SSPM = speed;
39
 
40
    SSP2CON1bits.CKP = 1; // Idle state for clock is a high level
41
    SSP2CON1bits.SSPEN = 1; // Enable MSSP module
42
 
43
#ifdef SPI2_USE_INTERRUPT
44
    PIE3bits.SSP2IE = 1; // Enable MSSP2 interrupt
45
#else
46
    PIE3bits.SSP2IE = 0;
47
#endif
48
 
49
#ifndef SPI2_WRITE_ONLY
50
    spi_data_p->buffer_in_len = 0;
51
    spi_data_p->buffer_in_read_ind = 0;
52
    spi_data_p->buffer_in_write_ind = 0;
53
#endif
54
    spi_data_p->buffer_out_ind = 0;
55
    spi_data_p->buffer_out_len = 0;
56
}
57
 
58
void SPI2_Write(char *msg, unsigned int length) {
59
    unsigned int i = 0;
60
#ifdef SPI2_USE_INTERRUPT
61
    spi_data_p->buffer_out_len = length;
62
    spi_data_p->buffer_out_ind = 1;
63
    for (i = 0; i < length; i++) {
64
        spi_data_p->buffer_out[i] = msg[i];
65
    }
66
    SPI_SLAVE_SELECT_LAT = 0; // Bring SS line low
67
    SSP2BUF = spi_data_p->buffer_out[0]; // Transmit first byte
68
#else
69
    SPI_SLAVE_SELECT_LAT = 0;
70
    while (i != length) {
71
        SSP2BUF = msg[i];
72
        i++;
73
        while (!SSP2STATbits.BF);
74
 
75
#ifndef SPI2_WRITE_ONLY
76
        spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
77
        if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
78
            spi_data_p->buffer_in_write_ind = 0;
79
        } else {
80
            spi_data_p->buffer_in_write_ind++;
81
        }
82
        spi_data_p->buffer_in_len++;
83
#else
84
        // Read data in buffer to clear it
85
        char tmp = SSP2BUF;
86
#endif
87
    }
88
    SPI_SLAVE_SELECT_LAT = 1;
89
#endif
90
}
91
 
92
void SPI2_Write_Repeat(char c, unsigned int length) {
93
#ifdef SPI2_USE_INTERRUPT
94
    // TODO Implement interrupts for SPI2
95
#else
96
    unsigned int i = 0;
97
    SPI_SLAVE_SELECT_LAT = 0;
98
    while (i != length) {
99
        SSP2BUF = c;
100
        i++;
101
        while (!SSP2STATbits.BF);
102
 
103
#ifndef SPI2_WRITE_ONLY
104
        spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
105
        if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
106
            spi_data_p->buffer_in_write_ind = 0;
107
        } else {
108
            spi_data_p->buffer_in_write_ind++;
109
        }
110
        spi_data_p->buffer_in_len++;
111
#else
112
        // Read data in buffer to clear it
113
        char tmp = SSP2BUF;
114
#endif
115
    }
116
    SPI_SLAVE_SELECT_LAT = 1;
117
#endif
118
}
119
 
192 Kevin 120
void SPI2_DMA_Init(void) {
121
    DMACON1bits.SSCON0 = 0;
122
    DMACON1bits.SSCON1 = 0; // DYLINTEN is software programmable
123
 
124
    DMACON1bits.TXINC = 1; // TXADDR is automatically incremented
125
    DMACON1bits.RXINC = 0; // RXADDR is not automatically incremented
126
 
127
    DMACON1bits.DUPLEX0 = 1;
128
    DMACON1bits.DUPLEX1 = 0; // Half-duplex mode, transmission only
129
 
130
    DMACON1bits.DLYINTEN = 0; // Interrupt is disabled
131
 
132
    DMACON2bits.DLYCYC = 0b0000; // Delay time of 1 cycle between bytes
133
    DMACON2bits.INTLVL = 0b0000; // Interrupt on transfer complete
134
}
135
 
136
void SPI2_DMA_Start(unsigned int length, void* TXADDR, void* RXADDR) {
137
    // Set length of message to transmit
138
    DMABCH = (char)(length-1 >> 8);
139
    DMABCL = (char)(length-1);
140
 
141
    // Set sourcing address
142
    TXADDRH = (char)((int)TXADDR >> 8);
143
    TXADDRL = (char)((int)TXADDR);
144
 
145
    // Set receiving address
146
    RXADDRH = (char)((int)RXADDR >> 8);
147
    RXADDRL = (char)((int)RXADDR);
148
 
149
    DMACON1bits.DMAEN = 1; // Start transmission
150
}
151
 
155 Kevin 152
#ifndef SPI2_WRITE_ONLY
153
void SPI2_Recv_Interrupt_Handler() {
154
    char c;
155
 
156
    if (SSP2STATbits.BF) { // Check if data receive flag is set
157
        if (spi_data_p->buffer_in_len == MAXSPIBUF - 1) {
158
            char output[64];
159
            sprintf(output, "SPI2: (ERROR) buffer overflow\r\n");
160
            DBG_PRINT_SPI(output, strlen(output));
161
            c = SSP2BUF; // Read SSP2BUF to clear it
162
        } else {
163
            // Save received data into buffer
164
            spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
165
            if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
166
                spi_data_p->buffer_in_write_ind = 0;
167
            } else {
168
                spi_data_p->buffer_in_write_ind++;
169
            }
170
            spi_data_p->buffer_in_len++;
171
 
172
            // Put next byte in SSP2BUF for transmit
173
            if (spi_data_p->buffer_out_ind != spi_data_p->buffer_out_len) {
174
                SSP2BUF = spi_data_p->buffer_out[spi_data_p->buffer_out_ind];
175
                spi_data_p->buffer_out_ind++;
176
            } else {
177
                SPI_SLAVE_SELECT_LAT = 1; // Bring SS line high
178
                spi_data_p->buffer_out_ind = 0;
179
                spi_data_p->buffer_out_len = 0;
180
            }
181
        }
182
    }
183
}
184
 
185
void SPI2_Read(char length) {
186
#ifdef SPI2_USE_INTERRUPT
187
    spi_data_p->buffer_out_len = length;
188
    spi_data_p->buffer_out_ind = 1;
189
    for (char i = 0; i < length; i++) {
190
        spi_data_p->buffer_out[i] = 0x0;
191
    }
192
    SPI_SLAVE_SELECT_LAT = 0; // Bring SS line low
193
    SSP2BUF = spi_data_p->buffer_out[0]; // Transmit first byte
194
#else
195
    SPI_SLAVE_SELECT_LAT = 0;
196
 
197
    for (char i = 0; i < length; i++) {
198
        SSP2BUF = 0x0;
199
        while (!SSP2STATbits.BF);
200
 
201
        spi_data_p->buffer_in[spi_data_p->buffer_in_write_ind] = SSP2BUF;
202
        if (spi_data_p->buffer_in_write_ind == MAXSPIBUF - 1) {
203
            spi_data_p->buffer_in_write_ind = 0;
204
        } else {
205
            spi_data_p->buffer_in_write_ind++;
206
        }
207
        spi_data_p->buffer_in_len++;
208
    }
209
    SPI_SLAVE_SELECT_LAT = 1;
210
#endif
211
}
212
 
213
char SPI2_Buffer_Len() {
214
    return spi_data_p->buffer_in_len;
215
}
216
 
217
char SPI2_Read_Buffer(char* buffer) {
218
    char i = 0;
219
    while (spi_data_p->buffer_in_len != 0) {
220
        buffer[i] = spi_data_p->buffer_in[spi_data_p->buffer_in_read_ind];
221
        i++;
222
        if (spi_data_p->buffer_in_read_ind == MAXSPIBUF - 1) {
223
            spi_data_p->buffer_in_read_ind = 0;
224
        } else {
225
            spi_data_p->buffer_in_read_ind++;
226
        }
227
        spi_data_p->buffer_in_len--;
228
    }
229
    return i;
230
}
231
#endif