/examples/stm32f3_discovery/can/main.cpp

https://github.com/modm-io/modm · C++ · 127 lines · 79 code · 21 blank · 27 comment · 8 complexity · 43f4f96d68dac526088e9cc12b849fb4 MD5 · raw file

  1. /*
  2. * Copyright (c) 2013, Kevin Läufer
  3. * Copyright (c) 2013-2014, Sascha Schade
  4. * Copyright (c) 2013-2018, Niklas Hauser
  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. #include <modm/board.hpp>
  14. #include <modm/processing/timer.hpp>
  15. #include <modm/debug/logger.hpp>
  16. /**
  17. * Example of CAN Hardware on STM32 F3 Discovery Board.
  18. *
  19. * Connect PB8 / PB9 to a CAN transceiver which is connected to a CAN bus.
  20. *
  21. * Tested in hardware on 2013-12-17 by Sascha Schade.
  22. */
  23. // Create an IODeviceWrapper around the Uart Peripheral we want to use
  24. modm::IODeviceWrapper< Usart2, modm::IOBuffer::BlockIfFull > loggerDevice;
  25. // Set all four logger streams to use the UART
  26. modm::log::Logger modm::log::debug(loggerDevice);
  27. modm::log::Logger modm::log::info(loggerDevice);
  28. modm::log::Logger modm::log::warning(loggerDevice);
  29. modm::log::Logger modm::log::error(loggerDevice);
  30. // Set the log level
  31. #undef MODM_LOG_LEVEL
  32. #define MODM_LOG_LEVEL modm::log::DEBUG
  33. static void
  34. displayMessage(const modm::can::Message& message)
  35. {
  36. static uint32_t receiveCounter = 0;
  37. receiveCounter++;
  38. MODM_LOG_INFO<< "id =" << message.getIdentifier();
  39. if (message.isExtended()) {
  40. MODM_LOG_INFO<< " extended";
  41. }
  42. else {
  43. MODM_LOG_INFO<< " standard";
  44. }
  45. if (message.isRemoteTransmitRequest()) {
  46. MODM_LOG_INFO<< ", rtr";
  47. }
  48. MODM_LOG_INFO<< modm::endl;
  49. MODM_LOG_INFO<< "dlc =" << message.getLength() << modm::endl;
  50. if (!message.isRemoteTransmitRequest())
  51. {
  52. MODM_LOG_INFO << "data=";
  53. for (uint32_t i = 0; i < message.getLength(); ++i) {
  54. MODM_LOG_INFO<< modm::hex << message.data[i] << modm::ascii << ' ';
  55. }
  56. MODM_LOG_INFO<< modm::endl;
  57. }
  58. MODM_LOG_INFO<< "# received=" << receiveCounter << modm::endl;
  59. }
  60. // ----------------------------------------------------------------------------
  61. int
  62. main()
  63. {
  64. Board::initialize();
  65. Board::LedNorth::set();
  66. // Initialize Usart
  67. Usart2::connect<GpioOutputA2::Tx>();
  68. Usart2::initialize<Board::SystemClock, 115200_Bd>();
  69. MODM_LOG_INFO << "CAN Test Program" << modm::endl;
  70. MODM_LOG_INFO << "Initializing Can ..." << modm::endl;
  71. // Initialize Can
  72. Can::connect<GpioInputB8::Rx, GpioOutputB9::Tx>(Gpio::InputType::PullUp);
  73. Can::initialize<Board::SystemClock, 125_kbps>(9);
  74. MODM_LOG_INFO << "Setting up Filter for Can ..." << modm::endl;
  75. // Receive every message
  76. CanFilter::setFilter(0, CanFilter::FIFO0,
  77. CanFilter::ExtendedIdentifier(0),
  78. CanFilter::ExtendedFilterMask(0));
  79. // Send a message
  80. MODM_LOG_INFO << "Sending message on Can ..." << modm::endl;
  81. modm::can::Message msg1(1, 1);
  82. msg1.setExtended(true);
  83. msg1.data[0] = 0x11;
  84. Can::sendMessage(msg1);
  85. modm::ShortPeriodicTimer pTimer(100ms);
  86. while (true)
  87. {
  88. if (Can::isMessageAvailable())
  89. {
  90. MODM_LOG_INFO << "Can: Message is available..." << modm::endl;
  91. modm::can::Message message;
  92. Can::getMessage(message);
  93. displayMessage(message);
  94. }
  95. if (pTimer.execute()) {
  96. Board::LedNorth::toggle();
  97. static uint8_t idx = 0;
  98. modm::can::Message msg1(1, 1);
  99. msg1.setExtended(true);
  100. msg1.data[0] = idx;
  101. Can::sendMessage(msg1);
  102. ++idx;
  103. }
  104. }
  105. return 0;
  106. }