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 #include "pw_i2c/i2c_service.h"
15
16 #include <algorithm>
17 #include <chrono>
18
19 #include "pw_assert/check.h"
20 #include "pw_chrono/system_clock.h"
21 #include "pw_containers/vector.h"
22 #include "pw_i2c/address.h"
23 #include "pw_rpc/pwpb/server_reader_writer.h"
24 #include "pw_status/status.h"
25
26 namespace pw::i2c {
27 namespace {
28
29 constexpr auto kI2cTimeout =
30 chrono::SystemClock::for_at_least(std::chrono::milliseconds(100));
31
32 } // namespace
33
I2cWrite(const pwpb::I2cWriteRequest::Message & request,rpc::PwpbUnaryResponder<pwpb::I2cWriteResponse::Message> & responder)34 void I2cService::I2cWrite(
35 const pwpb::I2cWriteRequest::Message& request,
36 rpc::PwpbUnaryResponder<pwpb::I2cWriteResponse::Message>& responder) {
37 Initiator* initiator = initiator_selector_(request.bus_index);
38 if (initiator == nullptr) {
39 responder.Finish({}, Status::InvalidArgument()).IgnoreError();
40 return;
41 }
42
43 constexpr auto kMaxWriteSize =
44 pwpb::I2cWriteRequest::kRegisterAddressMaxSize +
45 pwpb::I2cWriteRequest::kValueMaxSize;
46 Vector<std::byte, kMaxWriteSize> write_buffer{};
47 write_buffer.assign(std::begin(request.register_address),
48 std::end(request.register_address));
49 std::copy(std::begin(request.value),
50 std::end(request.value),
51 std::back_inserter(write_buffer));
52 auto result = initiator->WriteFor(
53 Address{static_cast<uint16_t>(request.target_address)},
54 write_buffer,
55 kI2cTimeout);
56 responder.Finish({}, result).IgnoreError();
57 }
58
I2cRead(const pwpb::I2cReadRequest::Message & request,rpc::PwpbUnaryResponder<pwpb::I2cReadResponse::Message> & responder)59 void I2cService::I2cRead(
60 const pwpb::I2cReadRequest::Message& request,
61 rpc::PwpbUnaryResponder<pwpb::I2cReadResponse::Message>& responder) {
62 constexpr auto kMaxReadSize = pwpb::I2cReadResponse::kValueMaxSize;
63
64 Initiator* initiator = initiator_selector_(request.bus_index);
65 if (initiator == nullptr || request.read_size > kMaxReadSize) {
66 responder.Finish({}, Status::InvalidArgument()).IgnoreError();
67 return;
68 }
69 Vector<std::byte, kMaxReadSize> value{};
70 value.resize(request.read_size);
71 auto result = initiator->WriteReadFor(
72 Address{static_cast<uint16_t>(request.target_address)},
73 request.register_address,
74 {value.data(), value.size()},
75 kI2cTimeout);
76
77 if (result.ok()) {
78 responder.Finish({value}, OkStatus()).IgnoreError();
79 } else {
80 responder.Finish({}, result).IgnoreError();
81 }
82 }
83
84 } // namespace pw::i2c
85