• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "pw_spi/chip_selector.h"
15 #include "pw_spi/device.h"
16 #include "pw_spi/initiator.h"
17 #include "pw_spi/responder.h"
18 #include "pw_status/status.h"
19 #include "pw_sync/borrow.h"
20 #include "pw_sync/mutex.h"
21 #include "pw_unit_test/framework.h"
22 
23 namespace pw::spi {
24 namespace {
25 
26 const pw::spi::Config kConfig = {.polarity = ClockPolarity::kActiveHigh,
27                                  .phase = ClockPhase::kFallingEdge,
28                                  .bits_per_word = BitsPerWord(8),
29                                  .bit_order = BitOrder::kMsbFirst};
30 
31 class SpiTestDevice : public ::testing::Test {
32  public:
SpiTestDevice()33   SpiTestDevice()
34       : initiator_(),
35         chip_selector_(),
36         initiator_lock_(),
37         borrowable_initiator_(initiator_, initiator_lock_),
38         device_(borrowable_initiator_, kConfig, chip_selector_) {}
39 
40  private:
41   // Stub SPI Initiator/ChipSelect objects, used to exercise public API surface.
42   class TestInitiator : public Initiator {
43    public:
Configure(const Config &)44     Status Configure(const Config& /*config */) override { return OkStatus(); }
WriteRead(ConstByteSpan,ByteSpan)45     Status WriteRead(ConstByteSpan /* write_buffer */,
46                      ByteSpan /* read_buffer */) override {
47       return OkStatus();
48     }
49   };
50 
51   class TestChipSelector : public ChipSelector {
52    public:
SetActive(bool)53     Status SetActive(bool /*active*/) override { return OkStatus(); }
54   };
55 
56   TestInitiator initiator_;
57   TestChipSelector chip_selector_;
58   sync::VirtualMutex initiator_lock_;
59   sync::Borrowable<Initiator> borrowable_initiator_;
60   Device device_;
61 };
62 
63 class SpiResponderTestDevice : public ::testing::Test {
64  public:
SpiResponderTestDevice()65   SpiResponderTestDevice() : responder_() {}
66 
67  private:
68   // Stub SPI Responder, used to exercise public API surface.
69   class TestResponder : public Responder {
70    private:
DoSetCompletionHandler(Function<void (ByteSpan,Status)>)71     void DoSetCompletionHandler(
72         Function<void(ByteSpan, Status)> /* callback */) override {}
DoWriteReadAsync(ConstByteSpan,ByteSpan)73     Status DoWriteReadAsync(ConstByteSpan /* tx_data */,
74                             ByteSpan /* rx_data */) override {
75       return OkStatus();
76     }
DoCancel()77     void DoCancel() override {}
78   };
79 
80   TestResponder responder_;
81 };
82 
83 // Simple test ensuring the SPI HAL compiles
TEST_F(SpiTestDevice,CompilationSucceeds)84 TEST_F(SpiTestDevice, CompilationSucceeds) {
85   // arrange
86   // act
87   // assert
88   EXPECT_TRUE(true);
89 }
90 
91 // Simple test ensuring the SPI Responder HAL compiles
TEST_F(SpiResponderTestDevice,CompilationSucceeds)92 TEST_F(SpiResponderTestDevice, CompilationSucceeds) { EXPECT_TRUE(true); }
93 
94 }  // namespace
95 }  // namespace pw::spi
96