Welcome to mirror list, hosted at ThFree Co, Russian Federation.

swi2c.cpp « Firmware - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 272ac419247b8a741b15867d2aa8ccdb49b015ea (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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "uni_avr_rpi.h"

#ifdef SWI2C
#include "swi2c.h"

#ifdef __AVR
unsigned char swi2c_sda = 20; // SDA pin
unsigned char swi2c_scl = 21; // SCL pin
#endif //__AVR

#ifdef __RPI
unsigned char swi2c_sda = 2; // SDA pin
unsigned char swi2c_scl = 3; // SCL pin
#endif //__RPI

unsigned char swi2c_cfg = 0xb1; // config
//  bit0..3 = clock delay factor = 1 << 1 = 2 [us]
//  bit4..7 = ack timeout factor = 1 << 11 = 2048 [cycles]

#define SWI2C_SDA    swi2c_sda
#define SWI2C_SCL    swi2c_scl
#define SWI2C_RMSK   0x01 //read mask (bit0 = 1)
#define SWI2C_WMSK   0x00 //write mask (bit0 = 0)
#define SWI2C_ASHF   0x01 //address shift (<< 1)
#define SWI2C_DMSK   0x7f //device address mask


void swi2c_init(unsigned char sda, unsigned char scl, unsigned char cfg)
{
	swi2c_sda = sda;
	swi2c_scl = scl;
	swi2c_cfg = cfg;
	GPIO_OUT(SWI2C_SDA);
	GPIO_OUT(SWI2C_SCL);
	GPIO_SET(SWI2C_SDA);
	GPIO_SET(SWI2C_SCL);
	DELAY(1000);
}

void swi2c_start(int delay)
{
	GPIO_CLR(SWI2C_SDA);
	DELAY(delay);
	GPIO_CLR(SWI2C_SCL);
	DELAY(delay);
}

void swi2c_stop(int delay)
{
	GPIO_SET(SWI2C_SCL);
	DELAY(delay);
	GPIO_SET(SWI2C_SDA);
	DELAY(delay);
}

void swi2c_ack(int delay)
{
	GPIO_CLR(SWI2C_SDA);
	DELAY(delay);
	GPIO_SET(SWI2C_SCL);
	DELAY(delay);
	GPIO_CLR(SWI2C_SCL);
	DELAY(delay);
}

int swi2c_wait_ack(int delay, int ackto)
{
	GPIO_INP(SWI2C_SDA);
	DELAY(delay);
//	GPIO_SET(SWI2C_SDA);
	DELAY(delay);
	GPIO_SET(SWI2C_SCL);
//	DELAY(delay);
	int ack = 0;
	while (!(ack = !GPIO_GET(SWI2C_SDA)) && ackto--) DELAY(delay);
	GPIO_CLR(SWI2C_SCL);
	DELAY(delay);
	GPIO_OUT(SWI2C_SDA);
	DELAY(delay);
	GPIO_CLR(SWI2C_SDA);
	DELAY(delay);
	return ack;
}

unsigned char swi2c_read(int delay)
{
	GPIO_SET(SWI2C_SDA);
	DELAY(delay);
	GPIO_INP(SWI2C_SDA);
	unsigned char data = 0;
	int bit; for (bit = 7; bit >= 0; bit--)
	{
		GPIO_SET(SWI2C_SCL);
		DELAY(delay);
		data |= GPIO_GET(SWI2C_SDA) << bit;
		GPIO_CLR(SWI2C_SCL);
		DELAY(delay);
	}
	GPIO_OUT(SWI2C_SDA);
	return data;
}

void swi2c_write(int delay, unsigned char data)
{
	int bit; for (bit = 7; bit >= 0; bit--)
	{
		if (data & (1 << bit)) GPIO_SET(SWI2C_SDA);
		else GPIO_CLR(SWI2C_SDA);
		DELAY(delay);
		GPIO_SET(SWI2C_SCL);
		DELAY(delay);
		GPIO_CLR(SWI2C_SCL);
		DELAY(delay);
	}
}

int swi2c_check(unsigned char dev_addr)
{
	int delay = 1 << (swi2c_cfg & 0xf);
	int tmout = 1 << (swi2c_cfg >> 4);
	swi2c_start(delay);
	swi2c_write(delay, (dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
	swi2c_stop(delay);
	return 1;
}

#ifdef SWI2C_A8 //8bit address

int swi2c_readByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte)
{
	int delay = 1 << (swi2c_cfg & 0xf);
	int tmout = 1 << (swi2c_cfg >> 4);
	swi2c_start(delay);
	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
	swi2c_write(delay, addr & 0xff);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_stop(delay);
	swi2c_start(delay);
	swi2c_write(delay, SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	unsigned char byte = swi2c_read(delay);
	swi2c_stop(delay);
	if (pbyte) *pbyte = byte;
	return 1;
}

int swi2c_writeByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte)
{
	int delay = 1 << (swi2c_cfg & 0xf);
	int tmout = 1 << (swi2c_cfg >> 4);
	swi2c_start(delay);
	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
	swi2c_write(delay, addr & 0xff);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_write(delay, *pbyte);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_stop(delay);
	return 1;
}

#endif //SWI2C_A8

#ifdef SWI2C_A16 //16bit address

int swi2c_readByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte)
{
	int delay = 1 << (swi2c_cfg & 0xf);
	int tmout = 1 << (swi2c_cfg >> 4);
	swi2c_start(delay);
	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
	swi2c_write(delay, addr >> 8);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_write(delay, addr & 0xff);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_stop(delay);
	swi2c_start(delay);
	swi2c_write(delay, SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	unsigned char byte = swi2c_read(delay);
	swi2c_stop(delay);
	if (pbyte) *pbyte = byte;
	return 1;
}

int swi2c_writeByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte)
{
	int delay = 1 << (swi2c_cfg & 0xf);
	int tmout = 1 << (swi2c_cfg >> 4);
	swi2c_start(delay);
	swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
	if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
	swi2c_write(delay, addr >> 8);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_write(delay, addr & 0xff);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_write(delay, *pbyte);
	if (!swi2c_wait_ack(delay, tmout)) return 0;
	swi2c_stop(delay);
	return 1;
}

#endif //SWI2C_A16


#endif //SWI2C