Micro template library A library for building device drivers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 6 години
преди 6 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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) { } // imaginary SCL pin functionality
  34. bool SDA (SDAMode mode, bool x) {
  35. // always read 0 back (which means always ACK)
  36. return 0;
  37. }
  38. void delay (uint32_t u) {
  39. // Pseudo-delay
  40. for (int i =0 ; i<10 ; ++i)
  41. ;
  42. }
  43. public:
  44. I2C (uint32_t clk =100000) noexcept
  45. : i2c_bb_i<I2C>(clk) {
  46. start();
  47. }
  48. };
  49. TEST(Test_i2c_impl, TestConcept) {
  50. I2C i2c {};
  51. EXPECT_EQ(I2c_i<I2C>, true);
  52. }
  53. TEST(Test_i2c_impl, TestConstruction) {
  54. I2C i2c {};
  55. EXPECT_EQ(i2c.clock(), 100000UL);
  56. }
  57. TEST(Test_i2c_impl, TestFunctionality) {
  58. I2C i2c {};
  59. uint8_t b = 42;
  60. i2c.clock(200000UL);
  61. EXPECT_EQ(i2c.clock(), 200000UL);
  62. EXPECT_EQ(i2c.tx_data(b), true);
  63. EXPECT_EQ(i2c.rx_data(true), 0x00);
  64. EXPECT_EQ(i2c.rx_data(false), 0x00);
  65. }
  66. }