blob: efab8f73beef58db00fee07d3ccc39a16acfe598 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
#include "uni_avr_rpi.h"
#ifdef __SWSPI
#include "swspi.h"
#ifdef __RPI
//#define swspi_miso 9
#define swspi_miso 10
#define swspi_mosi 10
#define swspi_sck 11
#define SWSPI_CS 7
#endif //__RPI
#define SWSPI_DEL 0x0f //delay mask (0-3. bit, delay = 1 << DEL [us])
#define SWSPI_POL 0x10 //polarity mask (4. bit, 1=inverted)
#define SWSPI_PHA 0x20 //phase mask (5. bit)
#define SWSPI_DOR 0x40 //data order mask (6. bit, 0=MSB first, 1=LSB first)
#define SWSPI_SCK_UP if (swspi_cfg & SWSPI_POL) GPIO_CLR(swspi_sck); else GPIO_SET(swspi_sck);
#define SWSPI_SCK_DN if (swspi_cfg & SWSPI_POL) GPIO_SET(swspi_sck); else GPIO_CLR(swspi_sck);
unsigned char swspi_miso = 0;
unsigned char swspi_mosi = 0;
unsigned char swspi_sck = 0;
unsigned char swspi_cfg = 0;
void swspi_init(unsigned char miso, unsigned char mosi, unsigned char sck, unsigned char cfg)
{
swspi_miso = miso;
swspi_mosi = mosi;
swspi_sck = sck;
swspi_cfg = cfg;
GPIO_INP(swspi_miso);
GPIO_OUT(swspi_mosi);
GPIO_OUT(swspi_sck);
GPIO_CLR(swspi_mosi);
SWSPI_SCK_DN;
}
void swspi_tx(unsigned char tx)
{
int delay = 1 << (swspi_cfg & SWSPI_DEL));
if (swspi_miso == swspi_mosi) GPIO_OUT(swspi_mosi);
unsigned char i = 0; for (; i < 8; i++)
{
if (tx & 0x80) GPIO_SET(swspi_mosi);
else GPIO_CLR(swspi_mosi);
DELAY(delay);
SWSPI_SCK_UP;
DELAY(delay);
SWSPI_SCK_DN;
tx <<= 1;
}
}
unsigned char swspi_rx()
{
int delay = 1 << (swspi_cfg & SWSPI_DEL));
if (swspi_miso == swspi_mosi) GPIO_OUT(swspi_mosi);
unsigned char rx = 0;
unsigned char i = 0; for (; i < 8; i++)
{
rx <<= 1;
DELAY(delay);
SWSPI_SCK_UP;
DELAY(delay);
rx |= GPIO_GET(swspi_miso)?1:0;
SWSPI_SCK_DN;
}
return rx;
}
unsigned char swspi_txrx(unsigned char tx)
{
int delay = 1 << (swspi_cfg & SWSPI_DEL));
unsigned char rx = 0;
unsigned char i = 0; for (; i < 8; i++)
{
rx <<= 1;
if (tx & 0x80) GPIO_SET(swspi_mosi);
else GPIO_CLR(swspi_mosi);
DELAY(delay);
SWSPI_SCK_UP;
DELAY(delay);
rx |= GPIO_GET(swspi_miso)?1:0;
SWSPI_SCK_DN;
tx <<= 1;
}
return rx;
}
#endif //__SWSPI
|