xref: /aosp_15_r20/external/pigweed/pw_spi/public/pw_spi/responder.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 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 #pragma once
16 
17 #include <utility>
18 
19 #include "pw_bytes/span.h"
20 #include "pw_function/function.h"
21 #include "pw_status/status.h"
22 
23 namespace pw::spi {
24 
25 // The Responder class provides an abstract interface used to receive and
26 // transmit data on the responder side of a SPI bus.
27 class Responder {
28  public:
29   virtual ~Responder() = default;
30 
31   // Set `callback` to be called when SPI transaction completes. `callback` can
32   // be called in an interrupt context. `callback` should not be changed during
33   // execution of a completion.
34   //
35   // A value of CANCELLED for the Status parameter indicates Cancel() was
36   // called. Partially transferred data may be passed in that case as well.
37   // Other Status values are implementer defined.
SetCompletionHandler(Function<void (ByteSpan,Status)> callback)38   void SetCompletionHandler(Function<void(ByteSpan, Status)> callback) {
39     DoSetCompletionHandler(std::move(callback));
40   }
41 
42   // `tx_data` is queued for tx when called, but only transmitted when
43   //   the initiator starts the next transaction. It's up to the implementer to
44   //   define how stuffing bytes are handled.
45   // `rx_data` is populated as the initiator transfers data. A slice of
46   //   `rx_data` is passed as a span to the completion callback.
47   //
48   // Only one outstanding request should be active. UNAVAILABLE will be returned
49   // if a transaction is already established.
50   //
51   // The completion handler will always be invoked, even in the case of an
52   // Cancel(). In that case a Status value of CANCELLED will be passed.
WriteReadAsync(ConstByteSpan tx_data,ByteSpan rx_data)53   Status WriteReadAsync(ConstByteSpan tx_data, ByteSpan rx_data) {
54     return DoWriteReadAsync(tx_data, rx_data);
55   }
56 
57   // Cancel the outstanding `WriteReadAsync` call. The completion handler will
58   // be called with a Status of CANCELLED after this is called.
Cancel()59   void Cancel() { DoCancel(); }
60 
61  private:
62   virtual void DoSetCompletionHandler(
63       Function<void(ByteSpan, Status)> callback) = 0;
64   virtual Status DoWriteReadAsync(ConstByteSpan tx_data, ByteSpan rx_data) = 0;
65   virtual void DoCancel() = 0;
66 };
67 
68 }  // namespace pw::spi
69