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
128 ConstByteSpan tx_buffer,
129 ByteSpan rx_buffer,
131 return DoWriteReadFor(device_address, tx_buffer, rx_buffer, timeout);
132 }
133
187 return DoValidateAndTransferFor(messages, timeout);
188 }
189
194 const void* tx_buffer,
195 size_t tx_size_bytes,
196 void* rx_buffer,
197 size_t rx_size_bytes,
199 return WriteReadFor(
200 device_address,
201 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
202 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
203 timeout);
204 }
205
235 Status WriteFor(Address device_address,
236 ConstByteSpan tx_buffer,
238 return WriteReadFor(device_address, tx_buffer, ByteSpan(), timeout);
239 }
242 Status WriteFor(Address device_address,
243 const void* tx_buffer,
244 size_t tx_size_bytes,
246 return WriteFor(
247 device_address,
248 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
249 timeout);
250 }
251
281 Status ReadFor(Address device_address,
282 ByteSpan rx_buffer,
284 return WriteReadFor(device_address, ConstByteSpan(), rx_buffer, timeout);
285 }
288 Status ReadFor(Address device_address,
289 void* rx_buffer,
290 size_t rx_size_bytes,
292 return ReadFor(device_address,
293 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
294 timeout);
295 }
296
327 std::byte ignored_buffer[1] = {}; // Read a byte to probe.
328 return WriteReadFor(
329 device_address, ConstByteSpan(), ignored_buffer, timeout);
330 }
331
332 private:
334 static constexpr Feature kCompatibilityFeatures = Feature::kTenBit;
335
336 // This function should not be overridden by future implementations of
337 // Initiator unless dealing with an underlying interface that prefers
338 // this format.
339 // Implement DoTransferFor(Messages) as a preferred course of action.
340 //
341 // The default implementation of this function will forward calls to
342 // TransferFor().
343 //
344 // Both the read and write parameters should be transmitted in one bus
345 // operation using a repeated start condition.
346 // If both parameters are present, the write operation is performed first.
347 virtual Status DoWriteReadFor(Address,
349 ByteSpan,
351
352 // This method should be overridden by implementations of Initiator.
353 // All messages in one call to DoTransferFor() should be executed
354 // as one bus transaction with repeated starts.
355 //
356 // The default implementation returns Status::Unimplemented().
357 virtual Status DoTransferFor(span<const Message> messages,
359
360 // Function to wrap calls to DoTransferFor() and Validate message vector's
361 // contents.
362 [[nodiscard]] Status DoValidateAndTransferFor(
364
368 Status ValidateMessages(span<const Message> messages) const;
369
374 [[nodiscard]] Status ValidateMessageFeatures(const Message& msg) const;
375
379 bool IsSupported(Feature feature) const;
380
381 // Features supported by this Initiator.
382 Feature supported_features_ = Feature::kStandard;
383};
384
385inline constexpr Initiator::Feature operator|(Initiator::Feature a,
387 return static_cast<Initiator::Feature>(static_cast<int>(a) |
388 static_cast<int>(b));
389}
390
391} // namespace pw::i2c
Definition: status.h:120
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:235
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:281
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:193
Status ProbeDeviceFor(Address device_address, chrono::SystemClock::duration timeout)
Definition: initiator.h:325
Feature
Defined set of supported i2c features.
Definition: initiator.h:55
Status TransferFor(span< const Message > messages, chrono::SystemClock::duration timeout)
Definition: initiator.h:185
Status ReadFor(Address device_address, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:288
Status WriteReadFor(Address device_address, ConstByteSpan tx_buffer, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
Definition: initiator.h:127
Status WriteFor(Address device_address, const void *tx_buffer, size_t tx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:242
Cross-platform I2C library.
Definition: address.h:19