Pigweed
 
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
199 Status TransferFor(span<const Message> messages,
201 return DoValidateAndTransferFor(messages, timeout);
202 }
203
208 const void* tx_buffer,
209 size_t tx_size_bytes,
210 void* rx_buffer,
211 size_t rx_size_bytes,
213 return WriteReadFor(
214 device_address,
215 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
216 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
217 timeout);
218 }
219
258 Status WriteFor(Address device_address,
259 ConstByteSpan tx_buffer,
261 return WriteReadFor(device_address, tx_buffer, ByteSpan(), timeout);
262 }
265 Status WriteFor(Address device_address,
266 const void* tx_buffer,
267 size_t tx_size_bytes,
269 return WriteFor(
270 device_address,
271 span(static_cast<const std::byte*>(tx_buffer), tx_size_bytes),
272 timeout);
273 }
274
313 Status ReadFor(Address device_address,
314 ByteSpan rx_buffer,
316 return WriteReadFor(device_address, ConstByteSpan(), rx_buffer, timeout);
317 }
320 Status ReadFor(Address device_address,
321 void* rx_buffer,
322 size_t rx_size_bytes,
324 return ReadFor(device_address,
325 span(static_cast<std::byte*>(rx_buffer), rx_size_bytes),
326 timeout);
327 }
328
368 std::byte ignored_buffer[1] = {}; // Read a byte to probe.
369 return WriteReadFor(
370 device_address, ConstByteSpan(), ignored_buffer, timeout);
371 }
372
373 private:
375 static constexpr Feature kCompatibilityFeatures = Feature::kTenBit;
376
377 // This function should not be overridden by future implementations of
378 // Initiator unless dealing with an underlying interface that prefers
379 // this format.
380 // Implement DoTransferFor(Messages) as a preferred course of action.
381 //
382 // Both the read and write parameters should be transmitted in one bus
383 // operation using a repeated start condition.
384 // If both parameters are present, the write operation is performed first.
385 virtual Status DoWriteReadFor(Address,
386 ConstByteSpan,
387 ByteSpan,
389 return Status::Unimplemented();
390 }
391
392 // This method should be overridden by implementations of Initiator.
393 // All messages in one call to DoTransferFor() should be executed
394 // as one transaction.
395 virtual Status DoTransferFor(span<const Message> messages,
397
398 // Function to wrap calls to DoTransferFor() and Validate message vector's
399 // contents.
400 [[nodiscard]] Status DoValidateAndTransferFor(
401 span<const Message> messages, chrono::SystemClock::duration timeout);
402
406 Status ValidateMessages(span<const Message> messages) const;
407
412 [[nodiscard]] Status ValidateMessageFeatures(const Message& msg) const;
413
417 bool IsSupported(Feature feature) const;
418
419 // Features supported by this Initiator.
420 Feature supported_features_ = Feature::kStandard;
421};
422
423inline constexpr Initiator::Feature operator|(Initiator::Feature a,
425 return static_cast<Initiator::Feature>(static_cast<int>(a) |
426 static_cast<int>(b));
427}
428
429} // 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:258
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:313
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:207
Status ProbeDeviceFor(Address device_address, chrono::SystemClock::duration timeout)
Definition: initiator.h:366
Feature
Defined set of supported i2c features.
Definition: initiator.h:52
Status TransferFor(span< const Message > messages, chrono::SystemClock::duration timeout)
Definition: initiator.h:199
Status ReadFor(Address device_address, void *rx_buffer, size_t rx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:320
Status WriteReadFor(Address device_address, ConstByteSpan tx_buffer, ByteSpan rx_buffer, chrono::SystemClock::duration timeout)
Status WriteFor(Address device_address, const void *tx_buffer, size_t tx_size_bytes, chrono::SystemClock::duration timeout)
Definition: initiator.h:265
std::chrono::duration< rep, period > duration
Alias for durations representable with this clock.
Definition: system_clock.h:86