-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathmain.cpp
133 lines (111 loc) · 3.61 KB
/
main.cpp
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
/*
* Copyright (c) 2013-2014, Sascha Schade
* Copyright (c) 2013, 2016, Kevin Läufer
* Copyright (c) 2013-2018, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------
#include <modm/board.hpp>
#include <modm/processing/timer.hpp>
#include <modm/debug/logger.hpp>
/**
* Example of CAN Hardware on STM32 F0 Discovery Board.
*
* Connect PB8 / PB9 to a CAN transceiver which is connected to a CAN bus.
*
*/
// Create an IODeviceWrapper around the Uart Peripheral we want to use
using Usart1 = BufferedUart<UsartHal1>;
modm::IODeviceWrapper< Usart1, modm::IOBuffer::BlockIfFull > loggerDevice;
// Set all four logger streams to use the UART
modm::log::Logger modm::log::debug(loggerDevice);
modm::log::Logger modm::log::info(loggerDevice);
modm::log::Logger modm::log::warning(loggerDevice);
modm::log::Logger modm::log::error(loggerDevice);
// Set the log level
#undef MODM_LOG_LEVEL
#define MODM_LOG_LEVEL modm::log::DEBUG
static void
displayMessage(const modm::can::Message& message)
{
static uint32_t receiveCounter = 0;
receiveCounter++;
MODM_LOG_INFO<< "id =" << message.getIdentifier();
if (message.isExtended()) {
MODM_LOG_INFO<< " extended";
}
else {
MODM_LOG_INFO<< " standard";
}
if (message.isRemoteTransmitRequest()) {
MODM_LOG_INFO<< ", rtr";
}
MODM_LOG_INFO<< modm::endl;
MODM_LOG_INFO<< "dlc =" << message.getDataLengthCode() << modm::endl;
MODM_LOG_INFO<< "length =" << message.getLength() << modm::endl;
if (!message.isRemoteTransmitRequest())
{
MODM_LOG_INFO << "data=";
for (uint32_t i = 0; i < message.getLength(); ++i) {
MODM_LOG_INFO<< modm::hex << message.data[i] << modm::ascii << ' ';
}
MODM_LOG_INFO<< modm::endl;
}
MODM_LOG_INFO<< "# received=" << receiveCounter << modm::endl;
}
// ----------------------------------------------------------------------------
int
main()
{
Board::initialize();
Board::LedUp::set();
// Initialize Usart
Usart1::connect<GpioOutputA9::Tx>();
Usart1::initialize<Board::SystemClock, 115200_Bd>();
MODM_LOG_INFO << "CAN Test Program" << modm::endl;
MODM_LOG_INFO << "Initializing Can ..." << modm::endl;
// Initialize Can
Can::connect<GpioInputB8::Rx, GpioOutputB9::Tx>(Gpio::InputType::PullUp);
Can::initialize<Board::SystemClock, 125_kbps>(9);
MODM_LOG_INFO << "Setting up Filter for Can ..." << modm::endl;
// Receive every message
CanFilter::setFilter(0, CanFilter::FIFO0,
CanFilter::ExtendedIdentifier(0),
CanFilter::ExtendedFilterMask(0));
// Send a message
MODM_LOG_INFO << "Sending message on Can ..." << modm::endl;
modm::can::Message msg1(1, 1);
msg1.setExtended(true);
msg1.data[0] = 0x11;
Can::sendMessage(msg1);
modm::ShortPeriodicTimer pTimer(1s);
const auto silent = static_cast<bool>(CAN->BTR & CAN_BTR_SILM);
const auto loop_back = static_cast<bool>(CAN->BTR & CAN_BTR_LBKM);
MODM_LOG_INFO << "Can silent mode: " << silent << modm::endl;
MODM_LOG_INFO << "Can loop back mode: " << loop_back << modm::endl;
while (true)
{
if (Can::isMessageAvailable())
{
MODM_LOG_INFO << "Can: Message is available..." << modm::endl;
modm::can::Message message;
Can::getMessage(message);
displayMessage(message);
}
if (pTimer.execute()) {
Board::LedUp::toggle();
static uint8_t idx = 0;
modm::can::Message msg1(1, 1);
msg1.setExtended(true);
msg1.data[0] = idx;
Can::sendMessage(msg1);
++idx;
}
}
return 0;
}