Skip to content

Commit 52f20bf

Browse files
committed
[mbed][tests] Add CAN loopback test case
Add test case that configures a MUT for in CAN self-test mode and sends a series of test messages to verify that it works.
1 parent c233c88 commit 52f20bf

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "mbed.h"
2+
#include "test_env.h"
3+
4+
#if defined(TARGET_LPC1549)
5+
CAN can1(D9, D8);
6+
#elif defined(TARGET_LPC1768) || defined(TARGET_LPC4088)
7+
CAN can1(p9, p10);
8+
#endif
9+
10+
#define TEST_ITERATIONS 127
11+
12+
int main() {
13+
MBED_HOSTTEST_TIMEOUT(20);
14+
MBED_HOSTTEST_SELECT(dev_null);
15+
MBED_HOSTTEST_DESCRIPTION(CAN Loopback);
16+
MBED_HOSTTEST_START("MBED_A27");
17+
18+
can1.mode(CAN::Reset);
19+
20+
if (!can1.mode(CAN::LocalTest)) {
21+
printf("Mode change failed\n");
22+
}
23+
24+
char success_count = 0;
25+
for (char i=0; i < TEST_ITERATIONS; i++) {
26+
unsigned int id = 1337;
27+
CANMessage tx_msg(id, &i, sizeof(i));
28+
bool sent = false;
29+
if (can1.write(tx_msg)) {
30+
printf("Sent %u: %d\n", id, i);
31+
sent = true;
32+
}
33+
wait_ms(50);
34+
35+
bool read = false;
36+
CANMessage rx_msg;
37+
if (can1.read(rx_msg)) {
38+
printf("Read %u: %d\n", rx_msg.id, rx_msg.data[0]);
39+
read = (rx_msg.id == id) && (rx_msg.data[0] == i);
40+
}
41+
42+
bool success = sent && read;
43+
44+
if (success) {
45+
success_count++;
46+
}
47+
}
48+
49+
MBED_HOSTTEST_RESULT(success_count == TEST_ITERATIONS);
50+
}

workspace_tools/tests.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@
8080
* i2c_eeprom:
8181
* LPC1*: (SDA=p28 , SCL=p27)
8282
* KL25Z: (SDA=PTE0, SCL=PTE1)
83-
83+
84+
* can_transceiver:
85+
* LPC1768: (RX=p9, TX=p10)
86+
* LPC1549: (RX=D9, TX=D8)
87+
* LPC4088: (RX=p9, TX=p10)
88+
8489
"""
8590
TESTS = [
8691
# Automated MBED tests
@@ -270,6 +275,15 @@
270275
"automated": True,
271276
"duration": 10,
272277
},
278+
{
279+
"id": "MBED_A27", "description": "CAN loopback test",
280+
"source_dir": join(TEST_DIR, "mbed", "can_loopback"),
281+
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
282+
"automated": True,
283+
"duration": 20,
284+
"peripherals": ["can_transceiver"],
285+
"mcu": ["LPC1549", "LPC1768"],
286+
},
273287
{
274288
"id": "MBED_BLINKY", "description": "Blinky",
275289
"source_dir": join(TEST_DIR, "mbed", "blinky"),

0 commit comments

Comments
 (0)