Skip to content

Commit d8977ad

Browse files
rlehchris-durand
authored andcommitted
[examples] Add Nucleo-G474RE CAN example
1 parent f3c6be9 commit d8977ad

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2020, Raphael Lehmann
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+
#include <modm/board.hpp>
12+
#include <modm/debug/logger.hpp>
13+
#include <modm/board.hpp>
14+
15+
using namespace modm::literals;
16+
17+
// Set the log level
18+
#undef MODM_LOG_LEVEL
19+
#define MODM_LOG_LEVEL modm::log::INFO
20+
21+
int
22+
main()
23+
{
24+
Board::initialize();
25+
26+
MODM_LOG_INFO << "CAN Test Program" << modm::endl;
27+
28+
MODM_LOG_INFO << "Initializing Fdcan1..." << modm::endl;
29+
// Initialize Fdcan1
30+
Fdcan1::connect<GpioB8::Rx, GpioB9::Tx>(Gpio::InputType::PullUp);
31+
Fdcan1::initialize<Board::SystemClock, 125_kbps, 1_pct, 500_kbps>(9);
32+
33+
MODM_LOG_INFO << "Setting up Filter for Fdcan1..." << modm::endl;
34+
// Receive every extended id message
35+
Fdcan1::setExtendedFilter(0, Fdcan1::FilterConfig::Fifo0,
36+
modm::can::ExtendedIdentifier(0),
37+
modm::can::ExtendedMask(0));
38+
39+
MODM_LOG_INFO << "Initializing Fdcan2..." << modm::endl;
40+
// Initialize Fdcan2
41+
Fdcan2::connect<GpioB5::Rx, GpioB6::Tx>(Gpio::InputType::PullUp);
42+
Fdcan2::initialize<Board::SystemClock, 125_kbps, 1_pct, 500_kbps>(12);
43+
44+
MODM_LOG_INFO << "Setting up Filter for Fdcan2..." << modm::endl;
45+
// Receive every message
46+
Fdcan2::setExtendedFilter(0, Fdcan2::FilterConfig::Fifo0,
47+
modm::can::ExtendedIdentifier(0),
48+
modm::can::ExtendedMask(0));
49+
50+
// Send a message
51+
MODM_LOG_INFO << "Sending message on Fdcan1..." << modm::endl;
52+
modm::can::Message msg1(1, 1);
53+
msg1.setExtended(true);
54+
msg1.data[0] = 0x11;
55+
Fdcan1::sendMessage(msg1);
56+
57+
// Send a message
58+
MODM_LOG_INFO << "Sending message on Fdcan2..." << modm::endl;
59+
msg1.data[0] = 0x22;
60+
Fdcan2::sendMessage(msg1);
61+
62+
63+
while (true)
64+
{
65+
if (Fdcan1::isMessageAvailable())
66+
{
67+
MODM_LOG_INFO << "Fdcan1: Message is available..." << modm::endl;
68+
modm::can::Message message;
69+
Fdcan1::getMessage(message);
70+
MODM_LOG_INFO << message << modm::endl;
71+
}
72+
if (Fdcan2::isMessageAvailable())
73+
{
74+
MODM_LOG_INFO << "Fdcan2: Message is available..." << modm::endl;
75+
modm::can::Message message;
76+
Fdcan2::getMessage(message);
77+
MODM_LOG_INFO << message << modm::endl;
78+
}
79+
}
80+
81+
return 0;
82+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<library>
2+
<extends>modm:nucleo-g474re</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g474re/can</option>
5+
</options>
6+
<modules>
7+
<module>modm:debug</module>
8+
<module>modm:platform:can:1</module>
9+
<module>modm:platform:can:2</module>
10+
<module>modm:platform:can:3</module>
11+
<module>modm:platform:gpio</module>
12+
<module>modm:platform:uart:2</module>
13+
<module>modm:build:scons</module>
14+
</modules>
15+
</library>

src/modm/board/nucleo_g474re/board.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ struct SystemClock {
112112
// update frequencies for busy-wait delay functions
113113
Rcc::updateCoreFrequency<Frequency>();
114114

115+
Rcc::setCanClockSource(Rcc::CanClockSource::Pclk);
115116
return true;
116117
}
117118
};

0 commit comments

Comments
 (0)