C/C++ API Reference
Loading...
Searching...
No Matches
responder.h
1// Copyright 2025 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
15#pragma once
16
17#include <zephyr/drivers/i2c.h>
18#include <zephyr/kernel.h>
19
20#include "pw_function/function.h"
21#include "pw_i2c/address.h"
22#include "pw_i2c/responder.h"
23#include "pw_log/log.h"
24#include "pw_status/status.h"
25
26namespace pw::i2c {
27
29
35class ZephyrResponder final : public Responder {
36 public:
37 using OnStartReadCallback = pw::Function<Status()>;
38 using OnStartWriteCallback = pw::Function<Status()>;
39 using OnWriteCallback = pw::Function<Status(ConstByteSpan)>;
40 using OnReadCallback = pw::Function<Result<ByteSpan>()>;
41 using OnStopCallback = pw::Function<Status()>;
42
43 // Constructs a ZephyrResponder.
44 //
45 // @param zephyr_i2c_device Pointer to the Zephyr I2C device struct.
46 // @param address The I2C address this responder will listen on.
47 // @param events Event handler.
48 ZephyrResponder(const ::device* zephyr_i2c_device,
50 ResponderEvents& events);
51
53
54 private:
57 Status DoEnable() override;
58 Status DoDisable() override;
60
61 // Zephyr I2C target callback functions. These are static and use
62 // CONTAINER_OF to get the ZephyrResponder instance.
63 static int ZephyrWriteRequestedCb(::i2c_target_config* cfg);
64 static int ZephyrWriteReceivedCb(::i2c_target_config* cfg, uint8_t val);
65 static int ZephyrReadRequestedCb(::i2c_target_config* cfg, uint8_t* val);
66 static int ZephyrReadProcessedCb(::i2c_target_config* cfg, uint8_t* val);
67 static int ZephyrStopCb(::i2c_target_config* cfg);
68
69#ifdef CONFIG_I2C_TARGET_BUFFER_MODE
70 static void ZephyrBufWriteReceivedCb(::i2c_target_config* cfg,
71 uint8_t* ptr,
72 uint32_t len);
73 static int ZephyrBufReadRequestedCb(::i2c_target_config* cfg,
74 uint8_t** ptr,
75 uint32_t* len);
76#endif
77
78 const ::device* zephyr_i2c_device_;
79
80 struct ZephyrTarget {
81 ZephyrResponder* self;
82 ::i2c_target_config config;
83 ::i2c_target_callbacks callbacks;
84 } zephyr_target_ = {};
85
88 ConstByteSpan current_read_data_span_;
89};
90
92
93} // namespace pw::i2c
Definition: status.h:120
Definition: address.h:42
Definition: responder.h:27
Definition: responder.h:87
const Address & address() const
Definition: responder.h:162
Definition: responder.h:35
Status DoDisable() override
Implementation of Disable()
Status DoEnable() override
Implementation of Enable()
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73
Cross-platform I2C library.
Definition: address.h:19