Skip to content

Commit 686b996

Browse files
committed
Asynch Unit tests (Serial, SPI, I2C, low power ticker)
Peripheral unit tests require target pins definitions. I included K64F there as a show case, although currently k64f does not support asynch methods.
1 parent 3ee5f86 commit 686b996

File tree

5 files changed

+891
-0
lines changed

5 files changed

+891
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <stdio.h>
17+
#include "TestHarness.h"
18+
#include "mbed.h"
19+
20+
/* EEPROM 24LC256 Test Unit, to test I2C asynchronous communication.
21+
*/
22+
23+
#if !DEVICE_I2C || !DEVICE_I2C_ASYNCH
24+
#error i2c_master_eeprom_asynch requires asynch I2C
25+
#endif
26+
27+
#if defined(TARGET_K64F)
28+
#define TEST_SDA_PIN PTE25
29+
#define TEST_SCL_PIN PTE24
30+
#else
31+
#error Target not supported
32+
#endif
33+
34+
#define PATTERN_MASK 0x66, ~0x66, 0x00, 0xFF, 0xA5, 0x5A, 0xF0, 0x0F
35+
36+
volatile int why;
37+
volatile bool complete;
38+
void cbdone(int event) {
39+
complete = true;
40+
why = event;
41+
}
42+
43+
const unsigned char pattern[] = { PATTERN_MASK };
44+
45+
TEST_GROUP(I2C_Master_EEPROM_Asynchronous)
46+
{
47+
I2C *obj;
48+
const int eeprom_address = 0xA0;
49+
event_callback_t callback;
50+
51+
void setup() {
52+
obj = new I2C(TEST_SDA_PIN, TEST_SCL_PIN);
53+
obj->frequency(400000);
54+
complete = false;
55+
why = 0;
56+
callback.attach(cbdone);
57+
}
58+
59+
void teardown() {
60+
delete obj;
61+
obj = NULL;
62+
}
63+
64+
};
65+
66+
TEST(I2C_Master_EEPROM_Asynchronous, tx_rx_one_byte_separate_transactions)
67+
{
68+
int rc;
69+
char data[] = { 0, 0, 0x66};
70+
71+
rc = obj->transfer(eeprom_address, data, sizeof(data), NULL, 0, callback, I2C_EVENT_ALL, false);
72+
CHECK_EQUAL(0, rc);
73+
while (!complete) {
74+
sleep();
75+
}
76+
77+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
78+
79+
// wait until slave is ready
80+
do {
81+
complete = 0;
82+
why = 0;
83+
obj->transfer(eeprom_address, NULL, 0, NULL, 0, callback, I2C_EVENT_ALL, false);
84+
while (!complete) {
85+
sleep();
86+
}
87+
} while (why != I2C_EVENT_TRANSFER_COMPLETE);
88+
89+
90+
// write the address for reading (0,0) then start reading data
91+
data[0] = 0;
92+
data[1] = 0;
93+
data[2] = 0;
94+
why = 0;
95+
complete = 0;
96+
obj->transfer(eeprom_address, data, 2, NULL, 0, callback, I2C_EVENT_ALL, true);
97+
while (!complete) {
98+
sleep();
99+
}
100+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
101+
102+
data[0] = 0;
103+
data[1] = 0;
104+
data[2] = 0;
105+
why = 0;
106+
complete = 0;
107+
rc = obj->transfer(eeprom_address, NULL, 0, data, 1, callback, I2C_EVENT_ALL, false);
108+
CHECK_EQUAL(0, rc);
109+
while (!complete) {
110+
sleep();
111+
}
112+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
113+
CHECK_EQUAL(data[0], 0x66);
114+
}
115+
116+
TEST(I2C_Master_EEPROM_Asynchronous, tx_rx_one_byte_one_transactions)
117+
{
118+
int rc;
119+
char send_data[] = { 0, 0, 0x66};
120+
rc = obj->transfer(eeprom_address, send_data, sizeof(send_data), NULL, 0, callback, I2C_EVENT_ALL, false);
121+
CHECK_EQUAL(0, rc)
122+
123+
while (!complete) {
124+
sleep();
125+
}
126+
127+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
128+
129+
// wait until slave is ready
130+
do {
131+
complete = 0;
132+
why = 0;
133+
obj->transfer(eeprom_address, NULL, 0, NULL, 0, callback, I2C_EVENT_ALL, false);
134+
while (!complete) {
135+
sleep();
136+
}
137+
} while (why != I2C_EVENT_TRANSFER_COMPLETE);
138+
139+
140+
send_data[0] = 0;
141+
send_data[1] = 0;
142+
send_data[2] = 0;
143+
char receive_data[1] = {0};
144+
why = 0;
145+
complete = 0;
146+
rc = obj->transfer(eeprom_address, send_data, 2, receive_data, 1, callback, I2C_EVENT_ALL, false);
147+
CHECK_EQUAL(0, rc);
148+
while (!complete) {
149+
sleep();
150+
}
151+
152+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
153+
CHECK_EQUAL(receive_data[0], 0x66);
154+
}
155+
156+
TEST(I2C_Master_EEPROM_Asynchronous, tx_rx_pattern)
157+
{
158+
int rc;
159+
char data[] = { 0, 0, PATTERN_MASK};
160+
// write 8 bytes to 0x0, then read them
161+
rc = obj->transfer(eeprom_address, data, sizeof(data), NULL, 0, callback, I2C_EVENT_ALL, false);
162+
CHECK_EQUAL(0, rc);
163+
164+
while (!complete) {
165+
sleep();
166+
}
167+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
168+
169+
// wait until slave is ready
170+
do {
171+
complete = 0;
172+
why = 0;
173+
obj->transfer(eeprom_address, NULL, 0, NULL, 0, callback, I2C_EVENT_ALL, false);
174+
while (!complete) {
175+
sleep();
176+
}
177+
} while (why != I2C_EVENT_TRANSFER_COMPLETE);
178+
179+
complete = 0;
180+
why = 0;
181+
char rec_data[8] = {0};
182+
rc = obj->transfer(eeprom_address, rec_data, 2, NULL, 0, callback, I2C_EVENT_ALL, true);
183+
CHECK_EQUAL(0, rc);
184+
while (!complete) {
185+
sleep();
186+
}
187+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
188+
189+
complete = 0;
190+
why = 0;
191+
rc = obj->transfer(eeprom_address, NULL, 0, rec_data, 8, callback, I2C_EVENT_ALL, false);
192+
CHECK_EQUAL(0, rc);
193+
while (!complete) {
194+
sleep();
195+
}
196+
CHECK_EQUAL(why, I2C_EVENT_TRANSFER_COMPLETE);
197+
198+
// received buffer match with pattern
199+
rc = memcmp(pattern, rec_data, sizeof(rec_data));
200+
CHECK_EQUAL(0, rc);
201+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include <stdio.h>
17+
#include "TestHarness.h"
18+
#include "mbed.h"
19+
#include "us_ticker_api.h"
20+
21+
/* Low power timer test.
22+
*/
23+
24+
#if !DEVICE_LOWPOWERTIMER
25+
#error This test unit requires low power to be defined for a target
26+
#endif
27+
28+
volatile bool complete;
29+
30+
void cbdone() {
31+
complete = true;
32+
}
33+
34+
TEST_GROUP(LowPowerTimerTest)
35+
{
36+
LowPowerTimeout *obj;
37+
38+
void setup() {
39+
obj = new LowPowerTimeout;
40+
complete = false;
41+
}
42+
43+
void teardown() {
44+
delete obj;
45+
obj = NULL;
46+
}
47+
48+
};
49+
50+
// 2 second timeout using lp ticker, time is measured using us ticker
51+
TEST(LowPowerTimerTest, lp_ticker_callback_2sec_timeout)
52+
{
53+
timestamp_t start = us_ticker_read();
54+
obj->attach(&cbdone, 2.0f);
55+
while (!complete);
56+
timestamp_t end = us_ticker_read();
57+
58+
// Not accurate for longer delays, thus +/-
59+
if ((end - start) > 2100000) {
60+
printf("Error! Start: %u, end: %u. It took longer than 2.1 sec.", start, end);
61+
CHECK_EQUAL(0, 1);
62+
}
63+
CHECK_EQUAL(complete, 1);
64+
}
65+
66+
// 50 microsecond timeout
67+
TEST(LowPowerTimerTest, lp_ticker_callback_50us_timeout)
68+
{
69+
timestamp_t start = us_ticker_read();
70+
obj->attach_us(&cbdone, 50);
71+
while(!complete);
72+
timestamp_t end = us_ticker_read();
73+
// roughly should be around 50us +/- 500us, for example with 32kHz, it can be 32us the lowest
74+
CHECK_EQUAL(((end - start) > 1) && ((end - start) < 500) ,1);
75+
CHECK_EQUAL(complete, 1);
76+
}
77+
78+
// 1 milisecond timeout
79+
TEST(LowPowerTimerTest, lp_ticker_callback_1ms_timeout)
80+
{
81+
timestamp_t start = us_ticker_read();
82+
obj->attach_us(&cbdone, 1000);
83+
while(!complete);
84+
timestamp_t end = us_ticker_read();
85+
86+
CHECK_EQUAL(((end - start) > 800) && ((end - start) < 1600) ,1);
87+
CHECK_EQUAL(complete, 1);
88+
}
89+
90+
// 5 second wake up from deep sleep
91+
TEST(LowPowerTimerTest, lp_ticker_deepsleep_wakeup_5sec_timeout)
92+
{
93+
timestamp_t start = lp_ticker_read();
94+
obj->attach(&cbdone, 5.0f);
95+
deepsleep();
96+
while (!complete);
97+
timestamp_t end = lp_ticker_read();
98+
// roughly should be around 5seconds +/- 100ms
99+
CHECK_EQUAL(((end - start) > 4900000) && ((end - start) < 5100000) ,1);
100+
CHECK_EQUAL(complete, 1);
101+
}
102+
103+
// 1ms wake up from deep sleep
104+
TEST(LowPowerTimerTest, lp_ticker_deepsleep_wakeup_1ms_timeout)
105+
{
106+
timestamp_t start = lp_ticker_read();
107+
obj->attach(&cbdone, 0.001f);
108+
deepsleep();
109+
while (!complete);
110+
timestamp_t end = lp_ticker_read();
111+
// 1ms timeout +/- 600us
112+
CHECK_EQUAL(((end - start) > 400) && ((end - start) < 1600) ,1);
113+
CHECK_EQUAL(complete, 1);
114+
}

0 commit comments

Comments
 (0)