Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
25namespace pw::i2c {
26
49class Initiator {
50 public:
52 enum class Feature : int {
53 // Initiator does not support extended features.
54 kStandard = 0,
55
56 // Initiator supports 10-bit addressing mode
57 kTenBit = (1 << 0),
58
59 // Initiator supports sending bytes without a start condition or address.
60 kNoStart = (1 << 1),
61 };
62
67 [[deprecated("Use the Initiator(Feature) constructor")]]
68 constexpr Initiator()
69 : Initiator(kCompatibilityFeatures) {}
70
86 explicit constexpr Initiator(Feature supported_features)
87 : supported_features_(supported_features) {}
88
89 virtual ~Initiator() = default;
90
134 ConstByteSpan tx_buffer,
135 ByteSpan rx_buffer,
137 return DoWriteReadFor(device_address, tx_buffer, rx_buffer, timeout);
138 }
139
201 Status TransferFor(span<const Message> messages,
203 return DoValidateAndTransferFor(messages, timeout);
204 }
205
210 const void* tx_buffer,
211 size_t tx_size_bytes,
212 void* rx_buffer,
213 size_t rx_size_bytes,
215 return WriteReadFor(
216 device_address,
217 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
218 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
219 timeout);
220 }
221
260 Status WriteFor(Address device_address,
261 ConstByteSpan tx_buffer,
263 return WriteReadFor(device_address, tx_buffer, ByteSpan(), timeout);
264 }
267 Status WriteFor(Address device_address,
268 const void* tx_buffer,
269 size_t tx_size_bytes,
271 return WriteFor(
272 device_address,
273 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
274 timeout);
275 }
276
315 Status ReadFor(Address device_address,
316 ByteSpan rx_buffer,
318 return WriteReadFor(device_address, ConstByteSpan(), rx_buffer, timeout);
319 }
322 Status ReadFor(Address device_address,
323 void* rx_buffer,
324 size_t rx_size_bytes,
326 return ReadFor(device_address,
327 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
328 timeout);
329 }
330
370 std::byte ignored_buffer[1] = {}; // Read a byte to probe.
371 return WriteReadFor(
372 device_address, ConstByteSpan(), ignored_buffer, timeout);
373 }
374
375 private:
377 static constexpr Feature kCompatibilityFeatures = Feature::kTenBit;
378
379 // This function should not be overridden by future implementations of
380 // Initiator unless dealing with an underlying interface that prefers
381 // this format.
382 // Implement DoTransferFor(Messages) as a preferred course of action.
383 //
384 // The default implementation of this function will forward calls to
385 // TransferFor().
386 //
387 // Both the read and write parameters should be transmitted in one bus
388 // operation using a repeated start condition.
389 // If both parameters are present, the write operation is performed first.
390 virtual Status DoWriteReadFor(Address,
391 ConstByteSpan,
392 ByteSpan,
394
395 // This method should be overridden by implementations of Initiator.
396 // All messages in one call to DoTransferFor() should be executed
397 // as one bus transaction with repeated starts.
398 //
399 // The default implementation returns Status::Unimplemented().
400 virtual Status DoTransferFor(span<const Message> messages,
402
403 // Function to wrap calls to DoTransferFor() and Validate message vector's
404 // contents.
405 [[nodiscard]] Status DoValidateAndTransferFor(
406 span<const Message> messages, chrono::SystemClock::duration timeout);
407
411 Status ValidateMessages(span<const Message> messages) const;
412
417 [[nodiscard]] Status ValidateMessageFeatures(const Message& msg) const;
418
422 bool IsSupported(Feature feature) const;
423
424 // Features supported by this Initiator.
425 Feature supported_features_ = Feature::kStandard;
426};
427
428inline constexpr Initiator::Feature operator|(Initiator::Feature a,
430 return static_cast<Initiator::Feature>(static_cast<int>(a) |
431 static_cast<int>(b));
432}
433
434} // namespace pw::i2c
Definition: status.h:85
Definition: address.h:39
The common, base driver interface for initiating thread-safe transactions with devices on an I2C bus....
Definition: initiator.h:49
Status WriteFor(Address device_address, ConstByteSpan tx_buffer, chrono::SystemClock::duration timeout)
Definition: initiator.h:260
constexpr Initiator(Feature supported_features)
Definition: initiator.h:86
constexpr Initiator()
Definition: initiator.h:68
Status ReadFor(Address device_address, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
Definition: initiator.h:315
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:209
Status ProbeDeviceFor(Address device_address, chrono::SystemClock::duration timeout)
Definition: initiator.h:368
Feature
Defined set of supported i2c features.
Definition: initiator.h:52
Status TransferFor(span< const Message > messages, chrono::SystemClock::duration timeout)
Definition: initiator.h:201
Status ReadFor(Address device_address, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:322
Status WriteReadFor(Address device_address, ConstByteSpan tx_buffer, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
Definition: initiator.h:133
Status WriteFor(Address device_address, const void *tx_buffer, size_t tx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:267
Definition: message.h:49
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:86