C/C++ API Reference
Loading...
Searching...
No Matches
initiator.h
1// Copyright 2020 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#include <utility>
18
19#include "pw_bytes/span.h"
20#include "pw_chrono/system_clock.h"
21#include "pw_i2c/address.h"
22#include "pw_i2c/message.h"
23#include "pw_status/status.h"
24
26namespace pw::i2c {
27
29
52class Initiator {
53 public:
55 enum class Feature : int {
56 // Initiator does not support extended features.
57 kStandard = 0,
58
59 // Initiator supports 10-bit addressing mode
60 kTenBit = (1 << 0),
61
62 // Initiator supports sending bytes without a start condition or address.
63 kNoStart = (1 << 1),
64 };
65
70 [[deprecated("Use the Initiator(Feature) constructor")]]
71 constexpr Initiator()
72 : Initiator(kCompatibilityFeatures) {}
73
89 explicit constexpr Initiator(Feature supported_features)
90 : supported_features_(supported_features) {}
91
92 virtual ~Initiator() = default;
93
137 ConstByteSpan tx_buffer,
138 ByteSpan rx_buffer,
140 return DoWriteReadFor(device_address, tx_buffer, rx_buffer, timeout);
141 }
142
206 return DoValidateAndTransferFor(messages, timeout);
207 }
208
213 const void* tx_buffer,
214 size_t tx_size_bytes,
215 void* rx_buffer,
216 size_t rx_size_bytes,
218 return WriteReadFor(
219 device_address,
220 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
221 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
222 timeout);
223 }
224
263 Status WriteFor(Address device_address,
264 ConstByteSpan tx_buffer,
266 return WriteReadFor(device_address, tx_buffer, ByteSpan(), timeout);
267 }
270 Status WriteFor(Address device_address,
271 const void* tx_buffer,
272 size_t tx_size_bytes,
274 return WriteFor(
275 device_address,
276 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
277 timeout);
278 }
279
318 Status ReadFor(Address device_address,
319 ByteSpan rx_buffer,
321 return WriteReadFor(device_address, ConstByteSpan(), rx_buffer, timeout);
322 }
325 Status ReadFor(Address device_address,
326 void* rx_buffer,
327 size_t rx_size_bytes,
329 return ReadFor(device_address,
330 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
331 timeout);
332 }
333
373 std::byte ignored_buffer[1] = {}; // Read a byte to probe.
374 return WriteReadFor(
375 device_address, ConstByteSpan(), ignored_buffer, timeout);
376 }
377
378 private:
380 static constexpr Feature kCompatibilityFeatures = Feature::kTenBit;
381
382 // This function should not be overridden by future implementations of
383 // Initiator unless dealing with an underlying interface that prefers
384 // this format.
385 // Implement DoTransferFor(Messages) as a preferred course of action.
386 //
387 // The default implementation of this function will forward calls to
388 // TransferFor().
389 //
390 // Both the read and write parameters should be transmitted in one bus
391 // operation using a repeated start condition.
392 // If both parameters are present, the write operation is performed first.
393 virtual Status DoWriteReadFor(Address,
395 ByteSpan,
397
398 // This method should be overridden by implementations of Initiator.
399 // All messages in one call to DoTransferFor() should be executed
400 // as one bus transaction with repeated starts.
401 //
402 // The default implementation returns Status::Unimplemented().
403 virtual Status DoTransferFor(span<const Message> messages,
405
406 // Function to wrap calls to DoTransferFor() and Validate message vector's
407 // contents.
408 [[nodiscard]] Status DoValidateAndTransferFor(
410
414 Status ValidateMessages(span<const Message> messages) const;
415
420 [[nodiscard]] Status ValidateMessageFeatures(const Message& msg) const;
421
425 bool IsSupported(Feature feature) const;
426
427 // Features supported by this Initiator.
428 Feature supported_features_ = Feature::kStandard;
429};
430
431inline constexpr Initiator::Feature operator|(Initiator::Feature a,
433 return static_cast<Initiator::Feature>(static_cast<int>(a) |
434 static_cast<int>(b));
435}
436
437} // namespace pw::i2c
Definition: status.h:86
Definition: address.h:42
The common, base driver interface for initiating thread-safe transactions with devices on an I2C bus....
Definition: initiator.h:52
Definition: message.h:51
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:90
Status WriteFor(Address device_address, ConstByteSpan tx_buffer, chrono::SystemClock::duration timeout)
Definition: initiator.h:263
constexpr Initiator(Feature supported_features)
Definition: initiator.h:89
constexpr Initiator()
Definition: initiator.h:71
Status ReadFor(Address device_address, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
Definition: initiator.h:318
Status WriteReadFor(Address device_address, const void *tx_buffer, size_t tx_size_bytes, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:212
Status ProbeDeviceFor(Address device_address, chrono::SystemClock::duration timeout)
Definition: initiator.h:371
Feature
Defined set of supported i2c features.
Definition: initiator.h:55
Status TransferFor(span< const Message > messages, chrono::SystemClock::duration timeout)
Definition: initiator.h:204
Status ReadFor(Address device_address, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:325
Status WriteReadFor(Address device_address, ConstByteSpan tx_buffer, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
Definition: initiator.h:136
Status WriteFor(Address device_address, const void *tx_buffer, size_t tx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:270
Cross-platform I2C library.
Definition: address.h:19