C/C++ API Reference
Loading...
Searching...
No Matches
initiator.h
1// Copyright 2021 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 <cstdint>
18#include <type_traits>
19
20#include "pw_assert/assert.h"
21#include "pw_bytes/span.h"
22#include "pw_status/status.h"
23
24namespace pw::spi {
25
27
30enum class ClockPolarity : uint8_t {
31 kActiveHigh, // Corresponds to CPOL = 0
32 kActiveLow, // Corresponds to CPOL = 1
33};
34
37enum class ClockPhase : uint8_t {
38 kRisingEdge, // Corresponds to CPHA = 0
39 kFallingEdge, // Corresponds to CPHA = 1
40};
41
44enum class BitOrder : uint8_t {
45 kLsbFirst,
46 kMsbFirst,
47};
48
54 public:
55 constexpr BitsPerWord(uint8_t data_bits) : data_bits_(data_bits) {
56 PW_ASSERT(data_bits_ >= 3 && data_bits_ <= 32);
57 }
58
59 constexpr uint8_t operator()() const { return data_bits_; }
60
61 private:
62 uint8_t data_bits_;
63};
64
67struct Config {
68 ClockPolarity polarity;
69 ClockPhase phase;
70 BitsPerWord bits_per_word;
71 BitOrder bit_order;
72
73 constexpr bool operator==(const Config& rhs) const {
74 return polarity == rhs.polarity && phase == rhs.phase &&
75 bits_per_word() == rhs.bits_per_word() && bit_order == rhs.bit_order;
76 }
77
78 constexpr bool operator!=(const Config& rhs) const { return !(*this == rhs); }
79};
80static_assert(sizeof(Config) == sizeof(uint32_t),
81 "Ensure that the config struct fits in 32-bits");
82
83// inclusive-language: disable
105// inclusive-language: enable
107 public:
108 virtual ~Initiator() = default;
109
116 Status Configure(const Config& config) { return DoConfigure(config); }
117
129 Status WriteRead(ConstByteSpan write_buffer, ByteSpan read_buffer) {
130 return DoWriteRead(write_buffer, read_buffer);
131 }
132
133 private:
134 // Subclass API:
135 virtual Status DoConfigure(const Config& config) = 0;
136 virtual Status DoWriteRead(ConstByteSpan write_buffer,
137 ByteSpan read_buffer) = 0;
138};
139
141
142} // namespace pw::spi
Definition: status.h:109
Definition: initiator.h:53
Definition: initiator.h:106
Status Configure(const Config &config)
Definition: initiator.h:116
Status WriteRead(ConstByteSpan write_buffer, ByteSpan read_buffer)
Definition: initiator.h:129
ClockPhase
Definition: initiator.h:37
BitOrder
Definition: initiator.h:44
ClockPolarity
Definition: initiator.h:30
Definition: initiator.h:67