Micro template library A library for building device drivers
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

78 satır
2.2 KiB

  1. /*!
  2. * \file test_i2c_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/i2c_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_i2c {
  29. using namespace utl;
  30. // implementer class stub
  31. class I2C : public i2c_bb_i<I2C> {
  32. friend i2c_bb_i<I2C>;
  33. void SCL (bool x) { (void)x; } // imaginary SCL pin functionality
  34. bool SDA (SDAMode mode, bool x) {
  35. (void)mode;
  36. (void)x;
  37. // always read 0 back (which means always ACK)
  38. return 0;
  39. }
  40. void delay (uint32_t u) {
  41. (void)u;
  42. // Pseudo-delay
  43. for (int i =0 ; i<10 ; ++i)
  44. ;
  45. }
  46. public:
  47. I2C (uint32_t clk =100000) noexcept
  48. : i2c_bb_i<I2C>(clk) {
  49. start();
  50. }
  51. };
  52. TEST(Test_i2c_impl, TestConcept) {
  53. I2C i2c {};
  54. // EXPECT_EQ(I2c_i<I2C>, true);
  55. }
  56. TEST(Test_i2c_impl, TestConstruction) {
  57. I2C i2c {};
  58. EXPECT_EQ(i2c.clock(), 100000UL);
  59. }
  60. TEST(Test_i2c_impl, TestFunctionality) {
  61. I2C i2c {};
  62. uint8_t b = 42;
  63. i2c.clock(200000UL);
  64. //EXPECT_EQ(i2c.clock(), 200000UL);
  65. EXPECT_EQ(i2c.tx_data(b), true);
  66. EXPECT_EQ(i2c.rx_data(true), 0x00);
  67. EXPECT_EQ(i2c.rx_data(false), 0x00);
  68. }
  69. }