Micro template library A library for building device drivers
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

test_spi_impl.cpp 2.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*!
  2. * \file test_spi_impl.cpp
  3. *
  4. * Copyright (C) 2018 Christos Choutouridis
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation, either version 3
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <gtest/gtest.h>
  21. #include <utl/com/spi_bb.h>
  22. /*!
  23. * \warning
  24. * This is not right way of testing communication interfaces. We have to
  25. * implement a Mock object to simulate the slave's behavior. Until then
  26. * we have the following.
  27. */
  28. namespace test_spi {
  29. using namespace utl;
  30. // implementer class stub
  31. class SPI : public spi_bb_i<SPI, spi::cpol::LOW, spi::cpha::LOW> {
  32. friend spi_bb_i<SPI, spi::cpol::LOW, spi::cpha::LOW>;
  33. void MOSI (bool st) { } // Imaginary pin functionality
  34. bool MISO () { return true; } // All reads become 0xFF
  35. void SCLK (bool st) { } // Imaginary pin functionality
  36. void delay (uint32_t nsec) { }// Imaginary delay functionality
  37. public:
  38. SPI (uint32_t clk =100000) noexcept : // expect 100000 default constructed clock
  39. spi_bb_i<SPI, spi::cpol::LOW, spi::cpha::LOW>(clk) {
  40. }
  41. };
  42. // fixture
  43. class Test_spi_impl : public ::testing::Test {
  44. protected:
  45. //void SetUp() override { }
  46. //void TearDown() override { }
  47. SPI spi {};
  48. };
  49. TEST_F(Test_spi_impl, TestConcept) {
  50. EXPECT_EQ(Spi_i<SPI>, true);
  51. }
  52. TEST_F(Test_spi_impl, TestConstruction) {
  53. EXPECT_EQ(spi.clock(), 100000UL);
  54. }
  55. TEST_F (Test_spi_impl, TestFunctionality) {
  56. uint8_t b = 42;
  57. uint8_t bb[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  58. uint8_t bbb[sizeof bb];
  59. spi.clock(500000UL);
  60. EXPECT_EQ(spi.clock(), 500000UL);
  61. EXPECT_EQ(spi.tx_data(b), 0xFF);
  62. EXPECT_EQ(spi.tx_data(bb, bbb, sizeof(bb)), sizeof (bb));
  63. EXPECT_EQ(spi.rx_data(), 0xFF);
  64. spi.rx_data(bbb, sizeof bb);
  65. for (unsigned int i=0 ; i<sizeof bb ; ++i) {
  66. EXPECT_EQ (bbb[i], 0xFF);
  67. }
  68. }
  69. }