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 #pragma once
15 
16 #include <cstdint>
17 
18 #include "fsl_mu.h"
19 #include "pw_bytes/span.h"
20 #include "pw_status/status.h"
21 #include "pw_stream/stream.h"
22 #include "pw_sync/binary_semaphore.h"
23 
24 namespace pw::stream {
25 
26 // Stream for reading/writing between processor cores using the Mcuxpresso SDK.
27 //
28 // It uses the MU module from the SDK for signaling data readiness. MU channels
29 // 0 and 1 are claimed for exclusive use. Each core should have an instance of
30 // this class with shared buffers pointing at the same physical memory locations
31 // that is uncached on both sides.
32 //
33 // Interrupt setup is different between cores, so that is left to the user. An
34 // example can be found in the docs. In the MU interrupt handler on each core,
35 // the `HandleInterrupt` function on the stream should be called.
36 class ShmemMcuxpressoStream : public NonSeekableReaderWriter {
37  public:
ShmemMcuxpressoStream(MU_Type * base,ByteSpan shared_read_buffer,ByteSpan shared_write_buffer)38   ShmemMcuxpressoStream(MU_Type* base,
39                         ByteSpan shared_read_buffer,
40                         ByteSpan shared_write_buffer)
41       : base_(base),
42         shared_read_buffer_(shared_read_buffer),
43         shared_write_buffer_(shared_write_buffer) {}
44   ~ShmemMcuxpressoStream();
45 
46   void Enable();
47   void Disable();
48 
49   // To be called when MU interrupt fires.
50   void HandleInterrupt();
51 
52  private:
53   StatusWithSize DoRead(ByteSpan) override;
54   Status DoWrite(ConstByteSpan) override;
55 
56   MU_Type* const base_;
57   ByteSpan shared_read_buffer_;
58   ByteSpan shared_write_buffer_;
59   sync::BinarySemaphore read_semaphore_;
60   sync::BinarySemaphore write_semaphore_;
61   sync::BinarySemaphore write_done_semaphore_;
62 };
63 
64 }  // namespace pw::stream
65