Skip to content

Commit 2d2199b

Browse files
committed
[test] Add FDCAN hardware unit test for Nucleo G474RE
1 parent d8977ad commit 2d2199b

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed

test/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ run-nucleo-l432:
6363
$(call run-test,nucleo-l432,size)
6464

6565

66+
compile-nucleo-g474:
67+
$(call compile-test,nucleo-g474,size)
68+
run-nucleo-g474:
69+
$(call run-test,nucleo-g474,size)
70+
71+
6672
compile-nucleo-f103_A:
6773
$(call compile-test,nucleo-f103_A,size)
6874
run-nucleo-f103_A:

test/config/nucleo-g474.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<library>
3+
<extends>modm:nucleo-g474re</extends>
4+
<options>
5+
<option name="modm:build:build.path">../../build/generated-unittest/nucleo-g474/</option>
6+
<option name="modm:build:unittest.source">../../build/generated-unittest/nucleo-g474/modm-test</option>
7+
</options>
8+
<modules>
9+
<module>modm:platform:heap</module>
10+
<module>modm-test:test:**</module>
11+
</modules>
12+
</library>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include "fdcan_test.hpp"
13+
14+
#include <modm/platform.hpp>
15+
#include <modm/board.hpp>
16+
17+
using namespace modm::platform;
18+
19+
void
20+
FdcanTest::setUp()
21+
{
22+
Fdcan1::initialize<Board::SystemClock, 500_kbps, 1_pct>(9, Fdcan1::Mode::TestInternalLoopback);
23+
24+
// receive all extended messages
25+
Fdcan1::setExtendedFilter(0, Fdcan1::FilterConfig::Fifo1,
26+
modm::can::ExtendedIdentifier(0),
27+
modm::can::ExtendedMask(0));
28+
}
29+
30+
void
31+
FdcanTest::testSendReceive()
32+
{
33+
modm::can::Message message{0x12345678, 7};
34+
constexpr std::string_view data = "\xDE\xAD\xBE\xEF\x12\x34\x56";
35+
std::copy(std::begin(data), std::begin(data) + 7, message.data);
36+
37+
TEST_ASSERT_FALSE(Fdcan1::isMessageAvailable());
38+
TEST_ASSERT_TRUE(Fdcan1::sendMessage(message));
39+
40+
modm::delay_ms(1);
41+
42+
modm::can::Message receivedMessage;
43+
TEST_ASSERT_TRUE(Fdcan1::getMessage(receivedMessage));
44+
TEST_ASSERT_EQUALS(receivedMessage.getIdentifier(), 0x12345678u);
45+
TEST_ASSERT_EQUALS(receivedMessage.getLength(), 7);
46+
TEST_ASSERT_TRUE(receivedMessage.isExtended());
47+
TEST_ASSERT_FALSE(receivedMessage.isRemoteTransmitRequest());
48+
TEST_ASSERT_TRUE(std::equal(std::begin(data), std::begin(data) + 7, message.data));
49+
}
50+
51+
void
52+
FdcanTest::testFilters()
53+
{
54+
Fdcan1::setStandardFilter(27, Fdcan1::FilterConfig::Fifo0,
55+
modm::can::StandardIdentifier(0x108),
56+
modm::can::StandardMask(0x1F8));
57+
58+
modm::can::Message message{0x188, 0};
59+
message.setExtended(false);
60+
Fdcan1::sendMessage(message);
61+
modm::delay_ms(1);
62+
TEST_ASSERT_FALSE(Fdcan1::isMessageAvailable());
63+
64+
message.setIdentifier(0xF09);
65+
Fdcan1::sendMessage(message);
66+
modm::delay_ms(1);
67+
TEST_ASSERT_TRUE(Fdcan1::isMessageAvailable());
68+
TEST_ASSERT_TRUE(Fdcan1::getMessage(message));
69+
TEST_ASSERT_FALSE(message.isExtended());
70+
}
71+
72+
void
73+
FdcanTest::testBuffers()
74+
{
75+
modm::can::Message message{0x4711, 0};
76+
// send 8 messages, exceeds internal peripheral queue size
77+
for (uint_fast8_t i = 0; i <= 8; ++i) {
78+
message.setLength(i);
79+
for (uint_fast8_t dataIndex = 0; dataIndex < i; ++dataIndex) {
80+
message.data[dataIndex] = i;
81+
}
82+
Fdcan1::sendMessage(message);
83+
}
84+
85+
modm::delay_ms(10);
86+
87+
// try to receive same messages
88+
modm::can::Message receivedMessage;
89+
for (uint_fast8_t i = 0; i <= 8; ++i) {
90+
TEST_ASSERT_TRUE(Fdcan1::getMessage(receivedMessage));
91+
92+
TEST_ASSERT_EQUALS(receivedMessage.getIdentifier(), 0x4711u);
93+
TEST_ASSERT_EQUALS(receivedMessage.getLength(), i);
94+
95+
for (uint_fast8_t dataIndex = 0; dataIndex < i; ++dataIndex) {
96+
TEST_ASSERT_EQUALS(receivedMessage.data[dataIndex], i);
97+
}
98+
}
99+
TEST_ASSERT_FALSE(Fdcan1::isMessageAvailable());
100+
TEST_ASSERT_FALSE(Fdcan1::getMessage(message));
101+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (c) 2021, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <unittest/testsuite.hpp>
13+
14+
/// @ingroup modm_test_test_platform_fdcan
15+
class FdcanTest : public unittest::TestSuite
16+
{
17+
public:
18+
void
19+
setUp() override;
20+
21+
void
22+
testSendReceive();
23+
24+
void
25+
testFilters();
26+
27+
void
28+
testBuffers();
29+
};

test/modm/platform/fdcan/module.lb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021, Christopher Durand
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
12+
13+
def init(module):
14+
module.name = ":test:platform:fdcan"
15+
16+
def prepare(module, options):
17+
target = options[":target"]
18+
19+
identifier = target.identifier
20+
if identifier.platform != "stm32" or identifier.family != "g4":
21+
return False
22+
23+
module.depends(":architecture:delay", ":platform:can:1")
24+
return True
25+
26+
def build(env):
27+
# env.substitutions = properties
28+
env.outbasepath = "modm-test/src/modm-test/platform/fdcan_test"
29+
env.copy("fdcan_test.hpp")
30+
env.copy("fdcan_test.cpp")

0 commit comments

Comments
 (0)