FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
i2c.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://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,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
16
21#ifndef I2C_HH
22#define I2C_HH
23
24#include <stdint.h>
25#include "flash.h"
26#include "ios.h"
27
28//NOTE only Master operation is supported for the moment
50namespace i2c
51{
66 enum class Status : uint8_t
67 {
69 OK = 0x00,
71 START_TRANSMITTED = 0x08,
86 ARBITRATION_LOST = 0x38,
92 DATA_RECEIVED_ACK = 0x50,
94 DATA_RECEIVED_NACK = 0x58,
95 };
96
98 template<typename OSTREAM> OSTREAM& operator<<(OSTREAM& out, Status s)
99 {
100 switch (s)
101 {
102 case Status::OK:
103 out << F("OK");
104 break;
105
107 out << F("START_TRANSMITTED");
108 break;
109
111 out << F("REPEAT_START_TRANSMITTED");
112 break;
113
115 out << F("SLA_W_TRANSMITTED_ACK");
116 break;
117
119 out << F("SLA_W_TRANSMITTED_NACK");
120 break;
121
123 out << F("DATA_TRANSMITTED_ACK");
124 break;
125
127 out << F("DATA_TRANSMITTED_NACK");
128 break;
129
131 out << F("ARBITRATION_LOST");
132 break;
133
135 out << F("SLA_R_TRANSMITTED_ACK");
136 break;
137
139 out << F("SLA_R_TRANSMITTED_NACK");
140 break;
141
143 out << F("DATA_RECEIVED_ACK");
144 break;
145
147 out << F("DATA_RECEIVED_NACK");
148 break;
149
150 default:
151 out << F("UNKNOWN[");
153 out << uint8_t(s) << ']';
154 break;
155 }
156 return out;
157 }
159
167 enum class I2CMode : uint8_t
168 {
170 STANDARD,
172 FAST
173 };
174};
175
176#endif /* I2C_HH */
static constexpr fmtflags basefield
Bitmask constant used with setf(fmtflags, fmtflags) when changing the output base format.
Definition: ios.h:227
static constexpr fmtflags hex
Read or write integral values using hexadecimal (0..9,A..F) base format.
Definition: ios.h:218
Flash memory utilities.
#define F(ptr)
Force string constant to be stored as flash storage.
Definition: flash.h:150
C++-like std::iostream facilities.
Define API to define and manage I2C devices.
Definition: i2c.h:51
I2CMode
I2C available transmission modes.
Definition: i2c.h:168
@ STANDARD
I2C Standard mode, less than 100KHz.
@ FAST
I2C Fast mode, less than 400KHz.
Status
Transmission status codes.
Definition: i2c.h:67
@ START_TRANSMITTED
[Transmitter/Receiver modes] A START condition has been transmitted.
@ ARBITRATION_LOST
[Transmitter mode] Abitration lost in SLA+W or data bytes.
@ REPEAT_START_TRANSMITTED
[Transmitter/Receiver modes] A repeated START condition has been transmitted.
@ DATA_TRANSMITTED_ACK
[Transmitter mode] Data byte has been transmitted; ACK has been received.
@ SLA_W_TRANSMITTED_ACK
[Transmitter mode] SLA+W has been transmitted; ACK has been received.
@ DATA_TRANSMITTED_NACK
[Transmitter mode] Data byte has been transmitted; NOT ACK has been received.
@ DATA_RECEIVED_ACK
[Receiver mode] Data byte has been transmitted; ACK has been returned.
@ SLA_W_TRANSMITTED_NACK
[Transmitter mode] SLA+W has been transmitted; NOT ACK has been received.
@ DATA_RECEIVED_NACK
[Receiver mode] Data byte has been transmitted; NOT ACK has been returned.
@ SLA_R_TRANSMITTED_NACK
[Receiver mode] SLA+R has been transmitted; NOT ACK has been received.
@ SLA_R_TRANSMITTED_ACK
[Receiver mode] SLA+R has been transmitted; ACK has been received.
@ OK
Code indicating the last called method executed as expected without any issue.