forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c_master.ino
319 lines (259 loc) · 7.62 KB
/
i2c_master.ino
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
I2C Master Test for
*/
#include <Arduino.h>
#include <unity.h>
#include <Wire.h>
#include <vector>
#include <algorithm>
#include <WiFi.h>
#include "sdkconfig.h"
/* DS1307 functions */
const uint8_t DS1307_ADDR = 0x68;
const uint8_t start_sec = 1;
const uint8_t start_min = 2;
const uint8_t start_hour = 3;
const uint8_t start_day = 4;
const uint8_t start_month = 5;
const uint16_t start_year = 2020;
static uint8_t read_sec = 0;
static uint8_t read_min = 0;
static uint8_t read_hour = 0;
static uint8_t read_day = 0;
static uint8_t read_month = 0;
static uint16_t read_year = 0;
static int peek_data = -1;
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const auto BCD2DEC = [](uint8_t num) -> uint8_t {
return ((num / 16 * 10) + (num % 16));
};
const auto DEC2BCD = [](uint8_t num) -> uint8_t {
return ((num / 10 * 16) + (num % 10));
};
void reset_read_values() {
read_sec = 0;
read_min = 0;
read_hour = 0;
read_day = 0;
read_month = 0;
read_year = 0;
}
void ds1307_start(void) {
uint8_t sec;
//Get seconds
Wire.beginTransmission(DS1307_ADDR);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDR, 1);
sec = Wire.read() & 0x7F; //Seconds without halt bit
//Set seconds and start clock
Wire.beginTransmission(DS1307_ADDR);
Wire.write(0x00);
Wire.write(sec);
Wire.endTransmission();
}
void ds1307_stop(void) {
uint8_t sec;
//Get seconds
Wire.beginTransmission(DS1307_ADDR);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDR, 1);
sec = Wire.read() | 0x80; //Seconds with halt bit
//Set seconds and halt clock
Wire.beginTransmission(DS1307_ADDR);
Wire.write(0x00);
Wire.write(sec);
Wire.endTransmission();
}
void ds1307_get_time(uint8_t *sec, uint8_t *min, uint8_t *hour, uint8_t *day, uint8_t *month, uint16_t *year) {
//Get time
Wire.beginTransmission(DS1307_ADDR);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDR, 7);
TEST_ASSERT_EQUAL(7, Wire.available());
if (peek_data == -1 && Wire.peek() != -1) {
peek_data = Wire.peek();
}
*sec = BCD2DEC(Wire.read() & 0x7F); //Seconds without halt bit
*min = BCD2DEC(Wire.read());
*hour = BCD2DEC(Wire.read() & 0x3F);
Wire.read(); //Ignore day of week
*day = BCD2DEC(Wire.read());
*month = BCD2DEC(Wire.read());
*year = BCD2DEC(Wire.read()) + 2000;
}
void ds1307_set_time(uint8_t sec, uint8_t min, uint8_t hour, uint8_t day, uint8_t month, uint16_t year) {
Wire.beginTransmission(DS1307_ADDR);
Wire.write(0x00);
Wire.write(DEC2BCD(sec));
Wire.write(DEC2BCD(min));
Wire.write(DEC2BCD(hour));
Wire.write(DEC2BCD(0)); //Ignore day of week
Wire.write(DEC2BCD(day));
Wire.write(DEC2BCD(month));
Wire.write(DEC2BCD(year - 2000));
Wire.endTransmission();
}
/* Unity functions */
// This function is automatically called by unity before each test is run
void setUp(void) {
reset_read_values();
Wire.begin();
}
// This function is automatically called by unity after each test is run
void tearDown(void) {
//Reset time
ds1307_set_time(start_sec, start_min, start_hour, start_day, start_month, start_year);
Wire.end();
}
void rtc_set_time() {
//Set time
ds1307_set_time(start_sec, start_min, start_hour, start_day, start_month, start_year);
//Get time
ds1307_get_time(&read_sec, &read_min, &read_hour, &read_day, &read_month, &read_year);
//Check time
TEST_ASSERT_EQUAL(start_sec, read_sec);
TEST_ASSERT_EQUAL(start_min, read_min);
TEST_ASSERT_EQUAL(start_hour, read_hour);
TEST_ASSERT_EQUAL(start_day, read_day);
TEST_ASSERT_EQUAL(start_month, read_month);
TEST_ASSERT_EQUAL(start_year, read_year);
}
void rtc_run_clock() {
uint8_t old_sec = 0;
//Run clock for 5 seconds
ds1307_start();
delay(5000);
ds1307_stop();
//Get time
ds1307_get_time(&read_sec, &read_min, &read_hour, &read_day, &read_month, &read_year);
//Check time
TEST_ASSERT_NOT_EQUAL(start_sec, read_sec); //Seconds should have changed
TEST_ASSERT_EQUAL(start_min, read_min);
TEST_ASSERT_EQUAL(start_hour, read_hour);
TEST_ASSERT_EQUAL(start_day, read_day);
TEST_ASSERT_EQUAL(start_month, read_month);
TEST_ASSERT_EQUAL(start_year, read_year);
old_sec = read_sec;
reset_read_values();
//Get time again to check that clock is stopped
delay(2000);
ds1307_get_time(&read_sec, &read_min, &read_hour, &read_day, &read_month, &read_year);
//Check time
TEST_ASSERT_EQUAL(old_sec, read_sec);
TEST_ASSERT_EQUAL(start_min, read_min);
TEST_ASSERT_EQUAL(start_hour, read_hour);
TEST_ASSERT_EQUAL(start_day, read_day);
TEST_ASSERT_EQUAL(start_month, read_month);
TEST_ASSERT_EQUAL(start_year, read_year);
}
void change_clock() {
//Get time
ds1307_get_time(&read_sec, &read_min, &read_hour, &read_day, &read_month, &read_year);
//Check time
TEST_ASSERT_EQUAL(start_sec, read_sec);
TEST_ASSERT_EQUAL(start_min, read_min);
TEST_ASSERT_EQUAL(start_hour, read_hour);
TEST_ASSERT_EQUAL(start_day, read_day);
TEST_ASSERT_EQUAL(start_month, read_month);
TEST_ASSERT_EQUAL(start_year, read_year);
Wire.setClock(400000);
reset_read_values();
TEST_ASSERT_EQUAL(400000, Wire.getClock());
//Get time
ds1307_get_time(&read_sec, &read_min, &read_hour, &read_day, &read_month, &read_year);
//Check time
TEST_ASSERT_EQUAL(start_sec, read_sec);
TEST_ASSERT_EQUAL(start_min, read_min);
TEST_ASSERT_EQUAL(start_hour, read_hour);
TEST_ASSERT_EQUAL(start_day, read_day);
TEST_ASSERT_EQUAL(start_month, read_month);
TEST_ASSERT_EQUAL(start_year, read_year);
}
void swap_pins() {
Wire.setPins(SCL, SDA);
Wire.begin();
//Set time
ds1307_set_time(start_sec, start_min, start_hour, start_day, start_month, start_year);
//Get time
ds1307_get_time(&read_sec, &read_min, &read_hour, &read_day, &read_month, &read_year);
//Check time
TEST_ASSERT_EQUAL(start_sec, read_sec);
TEST_ASSERT_EQUAL(start_min, read_min);
TEST_ASSERT_EQUAL(start_hour, read_hour);
TEST_ASSERT_EQUAL(start_day, read_day);
TEST_ASSERT_EQUAL(start_month, read_month);
TEST_ASSERT_EQUAL(start_year, read_year);
Wire.setPins(SDA, SCL);
}
void test_api() {
int integer_ret;
// Set Buffer Size
integer_ret = Wire.setBufferSize(32);
TEST_ASSERT_EQUAL(32, integer_ret);
integer_ret = Wire.setBufferSize(I2C_BUFFER_LENGTH);
TEST_ASSERT_EQUAL(I2C_BUFFER_LENGTH, integer_ret);
// Set TimeOut
Wire.setTimeOut(100);
TEST_ASSERT_EQUAL(100, Wire.getTimeOut());
// Check if buffer can be peeked
TEST_ASSERT_GREATER_THAN(-1, peek_data);
Wire.flush();
}
bool device_found() {
uint8_t err;
for (uint8_t address = 1; address < 127; ++address) {
Wire.beginTransmission(address);
err = Wire.endTransmission();
log_d("Address: 0x%02X, Error: %d", address, err);
if (err == 0) {
log_i("Found device at address: 0x%02X", address);
} else if (address == DS1307_ADDR) {
log_e("Failed to find DS1307");
return false;
}
}
return true;
}
void scan_bus() {
TEST_ASSERT_TRUE(device_found());
}
#if SOC_WIFI_SUPPORTED
void scan_bus_with_wifi() {
// delete old config
WiFi.disconnect(true, true, 1000);
delay(1000);
WiFi.begin(ssid, password);
delay(5000);
bool found = device_found();
WiFi.disconnect(true, true, 1000);
TEST_ASSERT_TRUE(found);
}
#endif
/* Main */
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(10);
}
log_d("Starting I2C Master");
Wire.begin();
log_d("Starting tests");
UNITY_BEGIN();
RUN_TEST(scan_bus);
#if SOC_WIFI_SUPPORTED
RUN_TEST(scan_bus_with_wifi);
#endif
RUN_TEST(rtc_set_time);
RUN_TEST(rtc_run_clock);
RUN_TEST(change_clock);
RUN_TEST(swap_pins);
RUN_TEST(test_api);
UNITY_END();
}
void loop() {
vTaskDelete(NULL);
}