Mark Slevinsky | c01abaf | 2021-11-19 11:57:48 -0500 | [diff] [blame] | 1 | // Copyright 2021 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | #include "pw_spi/linux_spi.h" |
| 16 | |
| 17 | #include <array> |
| 18 | #include <optional> |
| 19 | |
| 20 | #include "gtest/gtest.h" |
| 21 | #include "pw_spi/chip_selector.h" |
| 22 | #include "pw_spi/device.h" |
| 23 | #include "pw_spi/initiator.h" |
| 24 | #include "pw_status/status.h" |
| 25 | #include "pw_sync/borrow.h" |
| 26 | |
| 27 | namespace pw::spi { |
| 28 | namespace { |
| 29 | |
| 30 | const pw::spi::Config kConfig = {.polarity = ClockPolarity::kActiveHigh, |
| 31 | .phase = ClockPhase::kFallingEdge, |
| 32 | .bits_per_word = BitsPerWord(8), |
| 33 | .bit_order = BitOrder::kMsbFirst}; |
| 34 | |
| 35 | class LinuxSpi : public ::testing::Test { |
| 36 | public: |
| 37 | LinuxSpi() |
| 38 | : initiator_(LinuxInitiator("/dev/spidev0.0", 1000000)), |
| 39 | chip_selector_(), |
| 40 | initiator_lock_(), |
| 41 | borrowable_initiator_(initiator_, initiator_lock_), |
| 42 | device_(borrowable_initiator_, kConfig, chip_selector_) {} |
| 43 | |
| 44 | Device& device() { return device_; } |
| 45 | |
| 46 | private: |
| 47 | LinuxInitiator initiator_; |
| 48 | LinuxChipSelector chip_selector_; |
| 49 | sync::VirtualMutex initiator_lock_; |
| 50 | sync::Borrowable<Initiator> borrowable_initiator_; |
| 51 | [[maybe_unused]] Device device_; |
| 52 | }; |
| 53 | |
| 54 | TEST_F(LinuxSpi, StartTransaction_Succeeds) { |
| 55 | // arrange |
| 56 | std::optional<Device::Transaction> transaction = |
| 57 | device().StartTransaction(ChipSelectBehavior::kPerWriteRead); |
| 58 | |
| 59 | // act |
| 60 | |
| 61 | // assert |
| 62 | EXPECT_TRUE(transaction.has_value()); |
| 63 | } |
| 64 | |
| 65 | TEST_F(LinuxSpi, HalfDuplexTransaction_Succeeds) { |
| 66 | // arrange |
| 67 | std::optional<Device::Transaction> transaction = |
| 68 | device().StartTransaction(ChipSelectBehavior::kPerWriteRead); |
| 69 | |
| 70 | // act |
| 71 | ASSERT_TRUE(transaction.has_value()); |
| 72 | |
| 73 | std::array write_data{std::byte(1), std::byte(2), std::byte(3), std::byte(4)}; |
| 74 | auto write_status = transaction->Write(ConstByteSpan(write_data)); |
| 75 | |
| 76 | std::array read_data{std::byte(1), std::byte(2), std::byte(3), std::byte(4)}; |
| 77 | auto read_status = transaction->Read(read_data); |
| 78 | |
| 79 | // assert |
| 80 | EXPECT_TRUE(write_status.ok()); |
| 81 | EXPECT_TRUE(read_status.ok()); |
| 82 | } |
| 83 | |
| 84 | TEST_F(LinuxSpi, FullDuplexTransaction_Succeeds) { |
| 85 | // arrange |
| 86 | std::optional<Device::Transaction> transaction = |
| 87 | device().StartTransaction(ChipSelectBehavior::kPerWriteRead); |
| 88 | |
| 89 | // act |
| 90 | ASSERT_TRUE(transaction.has_value()); |
| 91 | |
| 92 | std::array write_data{std::byte(1), std::byte(2), std::byte(3), std::byte(4)}; |
| 93 | std::array read_data{std::byte(0), std::byte(0), std::byte(0), std::byte(0)}; |
| 94 | auto wr_status = transaction->WriteRead(ConstByteSpan(write_data), read_data); |
| 95 | |
| 96 | // assert |
| 97 | EXPECT_TRUE(wr_status.ok()); |
| 98 | } |
| 99 | |
| 100 | } // namespace |
| 101 | } // namespace pw::spi |