test_can_timings.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2016 UAVCAN Team
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. *
  22. * Contributors: https://github.com/UAVCAN/libcanard/contributors
  23. */
  24. #include <catch.hpp>
  25. #include <drivers/stm32/canard_stm32.h>
  26. #include <cstdio>
  27. #include <string>
  28. #include <stdexcept>
  29. static std::uint32_t computeBDTR(const std::uint32_t pclk1,
  30. const std::uint32_t target_bitrate)
  31. {
  32. CanardSTM32CANTimings timings = CanardSTM32CANTimings();
  33. const int res = canardSTM32ComputeCANTimings(pclk1, target_bitrate, &timings);
  34. const auto sample_point_permill =
  35. std::uint16_t((1000 * (1 + timings.bit_segment_1) / (1 + timings.bit_segment_1 + timings.bit_segment_2)));
  36. const std::uint32_t bdtr = (((timings.max_resynchronization_jump_width - 1U) & 3U) << 24U) |
  37. (((timings.bit_segment_1 - 1U) & 15U) << 16U) |
  38. (((timings.bit_segment_2 - 1U) & 7U) << 20U) |
  39. (((timings.bit_rate_prescaler - 1U) & 1023U) << 0U);
  40. std::printf("PCLK %9u Target %9u %s (%d) Presc %4u BS %2u/%u %.1f%% BDTR 0x%08x\n",
  41. unsigned(pclk1),
  42. unsigned(target_bitrate),
  43. (res == 0) ? "OK" : "FAIL",
  44. res,
  45. timings.bit_rate_prescaler,
  46. timings.bit_segment_1,
  47. timings.bit_segment_2,
  48. double(sample_point_permill) * 0.1,
  49. unsigned(bdtr));
  50. if (res != 0)
  51. {
  52. throw std::runtime_error("canardSTM32ComputeCANTimings() returned " + std::to_string(res));
  53. }
  54. return bdtr;
  55. }
  56. /*
  57. * Reference values were validated manually with the help of http://www.bittiming.can-wiki.info/
  58. */
  59. TEST_CASE("STM32, CANTimings")
  60. {
  61. CHECK(0x00060003 == computeBDTR(36000000, 1000000));
  62. CHECK(0x00180005 == computeBDTR(36000000, 500000));
  63. CHECK(0x001c0008 == computeBDTR(36000000, 250000));
  64. CHECK(0x001c0011 == computeBDTR(36000000, 125000));
  65. CHECK(0x001b0017 == computeBDTR(36000000, 100000));
  66. CHECK(0x001c00e0 == computeBDTR(36000000, 10000));
  67. CHECK(0x00070008 == computeBDTR(90000000, 1000000));
  68. CHECK(0x001b000b == computeBDTR(90000000, 500000));
  69. CHECK(0x001b0017 == computeBDTR(90000000, 250000));
  70. CHECK(0x001c002c == computeBDTR(90000000, 125000));
  71. CHECK(0x001b003b == computeBDTR(90000000, 100000));
  72. CHECK(0x001b0257 == computeBDTR(90000000, 10000));
  73. }