-
Notifications
You must be signed in to change notification settings - Fork 11
/
RTD2660_SPI.h
146 lines (140 loc) · 3.97 KB
/
RTD2660_SPI.h
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef RTD2660_SPI_H
#define RTD2660_SPI_H
//-----------------------------------------------------------------------------
#include "avr/pgmspace.h"
#include "config.h"
//-----------------------------------------------------------------------------
#define RTD_READ 1
#define RTD_WRITE 0
#define RTD_NO_AUTOINC 1
#define RTD_AUTOINC 0
#define RTD_ID_ADDRESS 0x00
#define RTD_ID_MAGIC 0xF1
#define RTD_RX_DATA ((RTD_RX_PIN & _bit(RTD_RX_BIT)) >> RTD_RX_BIT)
#define RTD_enable() clr_bit(RTD_CS_PORT, RTD_CS_BIT)
#define RTD_disable() set_bit(RTD_CS_PORT, RTD_CS_BIT)
#define RTD_toggle_clk() RTD_CLK_PORT = RTD_CLK_PORT ^ _bit(RTD_CLK_BIT)
//-----------------------------------------------------------------------------
__inline void RTD2660_SPI_init()
{
set_bit(RTD_CLK_DDR, RTD_CLK_BIT);
set_bit(RTD_TX_DDR, RTD_TX_BIT);
set_bit(RTD_CS_PORT, RTD_CS_BIT);
set_bit(RTD_CS_DDR, RTD_CS_BIT);
}
//-----------------------------------------------------------------------------
__inline void RTD2660_SPI_send_bit(BYTE value)
{
if(value)
{
set_bit(RTD_TX_PORT, RTD_TX_BIT);
}
else
{
clr_bit(RTD_TX_PORT, RTD_TX_BIT);
}
RTD_toggle_clk();
}
//-----------------------------------------------------------------------------
__inline BYTE RTD2660_SPI_get_bit()
{
static BYTE read;
read = RTD_RX_DATA;
RTD_toggle_clk();
return read;
}
//-----------------------------------------------------------------------------
__inline void RTD2660_SPI_read(BYTE address, BYTE count, BYTE *data, bool auto_inc)
{
register BYTE tmp = address;
set_bit(RTD_CLK_PORT, RTD_CLK_BIT);
RTD_enable();
// address
for(BYTE i = 0; i < 8; i++)
{
RTD2660_SPI_send_bit(tmp & 0x1);
tmp >>= 1;
}
// command
RTD2660_SPI_send_bit(RTD_READ);
if(auto_inc && count>1)
RTD2660_SPI_send_bit(RTD_AUTOINC);
else
RTD2660_SPI_send_bit(RTD_NO_AUTOINC);
RTD2660_SPI_send_bit(1);
// reply
BYTE pos = 0;
while(count--)
{
for(BYTE i = 0; i < 8; i++)
{
tmp = (tmp >> 1) | (RTD2660_SPI_get_bit() << 7);
}
data[pos++] = tmp;
}
RTD_disable();
}
//-----------------------------------------------------------------------------
__inline void RTD2660_SPI_write(const BYTE address, const BYTE count, const BYTE *data, const bool auto_inc)
{
register BYTE tmp = address;
register BYTE cnt = count;
set_bit(RTD_CLK_PORT, RTD_CLK_BIT);
RTD_enable();
// address and command
for(BYTE i = 0; i < 8; i++)
{
RTD2660_SPI_send_bit(tmp & 0x1);
tmp >>= 1;
}
RTD2660_SPI_send_bit(RTD_WRITE);
if(auto_inc && count > 1)
RTD2660_SPI_send_bit(RTD_AUTOINC);
else
RTD2660_SPI_send_bit(RTD_NO_AUTOINC);
// data
BYTE pos = 0;
while(cnt--)
{
tmp = data[pos++];
for(BYTE i = 0; i < 8; i++)
{
RTD2660_SPI_send_bit(tmp & 0x1);
tmp >>= 1;
}
}
RTD_disable();
}
//-----------------------------------------------------------------------------
__inline void RTD2660_SPI_write_pgm(const BYTE address, const BYTE count, const BYTE *data, const bool auto_inc)
{
register BYTE tmp = address;
register BYTE cnt = count;
set_bit(RTD_CLK_PORT, RTD_CLK_BIT);
RTD_enable();
// address and command
for(BYTE i = 0; i < 8; i++)
{
RTD2660_SPI_send_bit(tmp & 0x1);
tmp >>= 1;
}
RTD2660_SPI_send_bit(RTD_WRITE);
if(auto_inc && count > 1)
RTD2660_SPI_send_bit(RTD_AUTOINC);
else
RTD2660_SPI_send_bit(RTD_NO_AUTOINC);
// data
BYTE pos = 0;
while(cnt--)
{
tmp = pgm_read_byte(data + pos++);
for(BYTE i = 0; i < 8; i++)
{
RTD2660_SPI_send_bit(tmp & 0x1);
tmp >>= 1;
}
}
RTD_disable();
}
//-----------------------------------------------------------------------------
#endif