OpenCores
URL https://opencores.org/ocsvn/hf-risc/hf-risc/trunk

Subversion Repositories hf-risc

[/] [hf-risc/] [trunk/] [software/] [lib/] [spi.c] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 serginhofr
/* file:          spi.c
2
 * description:   low level SPI driver
3
 * date:          09/2015
4
 * author:        Sergio Johann Filho <sergio.filho@pucrs.br>
5
 */
6
 
7
#include <hf-risc.h>
8
 
9
/* SPI mode: 0 ~ 3 */
10
#define SPI_MODE        0
11
 
12
/* SPI interface - EXTIO_OUT pins 0 (chip select), 1 (clock), 2 (master output) and 3 (chip select 2) */
13
#define SPI_CS          0x01
14
#define SPI_SCK         0x02
15
#define SPI_MOSI        0x04
16
#define SPI_CS2         0x08
17
 
18
/* SPI interface - EXIO_IN pin 3 (master input) */
19
#define SPI_MISO        0x08
20
 
21
void spi_setup(void){
22
// setup IO pins ...
23
        EXTIO_OUT |= SPI_CS | SPI_CS2;
24
 
25
#if SPI_MODE == 0 || SPI_MODE == 1
26
        EXTIO_OUT &= ~SPI_SCK;
27
#else
28
        EXTIO_OUT |= SPI_SCK;
29
#endif
30
}
31
 
32
void spi_start(void){
33
// set CS low
34
        EXTIO_OUT &= ~SPI_CS;
35
}
36
 
37
void spi_stop(void){
38
// set CS high
39
        EXTIO_OUT |= SPI_CS;
40
}
41
 
42
void spi_start2(void){
43
// set CS low
44
        EXTIO_OUT &= ~SPI_CS2;
45
}
46
 
47
void spi_stop2(void){
48
// set CS high
49
        EXTIO_OUT |= SPI_CS2;
50
}
51
 
52
int8_t spi_sendrecv(int8_t data){
53
        int32_t i;
54
        int8_t newdata = 0;
55
 
56
#if SPI_MODE == 0       
57
        for (i = 0; i < 8; i++){
58
                if (data & 0x80){
59
                        EXTIO_OUT |= SPI_MOSI;
60
                }else{
61
                        EXTIO_OUT &= ~SPI_MOSI;
62
                }
63
                data <<= 1;
64
                EXTIO_OUT |= SPI_SCK;
65
                newdata <<= 1;
66
                newdata |= ((EXTIO_IN & SPI_MISO) ? 1 : 0);
67
                EXTIO_OUT &= ~SPI_SCK;
68
        }
69
        return newdata;
70
#endif
71
#if SPI_MODE == 1
72
        for (i = 0; i < 8; i++){
73
                EXTIO_OUT |= SPI_SCK;
74
                if (data & 0x80){
75
                        EXTIO_OUT |= SPI_MOSI;
76
                }else{
77
                        EXTIO_OUT &= ~SPI_MOSI;
78
                }
79
                data <<= 1;
80
                EXTIO_OUT &= ~SPI_SCK;
81
                newdata <<= 1;
82
                newdata |= ((EXTIO_IN & SPI_MISO) ? 1 : 0);
83
        }
84
        return newdata;
85
#endif
86
#if SPI_MODE == 2
87
        for (i = 0; i < 8; i++){
88
                if (data & 0x80){
89
                        EXTIO_OUT |= SPI_MOSI;
90
                }else{
91
                        EXTIO_OUT &= ~SPI_MOSI;
92
                }
93
                data <<= 1;
94
                EXTIO_OUT &= ~SPI_SCK;
95
                newdata <<= 1;
96
                newdata |= ((EXTIO_IN & SPI_MISO) ? 1 : 0);
97
                EXTIO_OUT |= SPI_SCK;
98
        }
99
        return newdata;
100
#endif
101
#if SPI_MODE == 3
102
        for (i = 0; i < 8; i++){
103
                EXTIO_OUT &= ~SPI_SCK;
104
                if (data & 0x80){
105
                        EXTIO_OUT |= SPI_MOSI;
106
                }else{
107
                        EXTIO_OUT &= ~SPI_MOSI;
108
                }
109
                data <<= 1;
110
                EXTIO_OUT |= SPI_SCK;
111
                newdata <<= 1;
112
                newdata |= ((EXTIO_IN & SPI_MISO) ? 1 : 0);
113
        }
114
        return newdata;
115
#endif
116
}
117
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.