semf
softi2cmaster.h
Go to the documentation of this file.
1
10#ifndef SEMF_COMMUNICATION_SOFTI2CMASTER_H_
11#define SEMF_COMMUNICATION_SOFTI2CMASTER_H_
12
15#include <semf/system/gpio.h>
17
18namespace semf
19{
30{
31public:
35 enum class ErrorCode : uint8_t
36 {
37 StopConditionSetSda_NackError = 0
38 };
46 SoftI2cMaster(Gpio& scl, Gpio& sda, app::Timer& timer);
47 explicit SoftI2cMaster(const SoftI2cMaster& other) = delete;
48 virtual ~SoftI2cMaster() = default;
49
50 void init() override;
51 void deinit() override;
52 void stopWrite() override;
53 void stopRead() override;
54
55protected:
56 void writeHardware(const uint8_t data[], size_t size) override;
57 void readHardware(uint8_t buffer[], size_t bufferSize) override;
58
59private:
60 // RESTART CONDITION
62 void resartConditionSetSdaResetScl();
64 void resartConditionSetScl();
65
66 // START CONDITION
68 void startConditionResetSda();
70 void startConditionResetScl();
71
72 // WRITE BYTE
74 void writeByte();
76 void writeByteSetSdaDataBit();
78 void writeByteSetScl();
83 void writeByteResetScl();
84
85 // READ BYTE
87 void readByte();
89 void readByteReadSdaDataBit();
91 void readByteSetScl();
96 void readByteResetScl();
97
98 // ACKNOLAGE
100 void checkAcknowledgeSetSdaInput();
102 void checkAcknowledgeSetScl();
104 void checkAcknowledgeResetScl();
106 void setAcknowledgeSetSdaOutput();
108 void setAcknowledgeSetScl();
110 void setAcknowledgeResetScl();
112 void finishAcknowledge();
114 void finishAcknowledgeWriteOperation();
116 void finishAcknowledgeReadOperation();
117
118 // STOP CONDITION
120 void stopConditionResetSda();
122 void stopConditionSetScl();
127 void stopConditionSetSda();
128 void setFrequency(uint32_t hz);
129
131 Gpio& m_scl;
133 Gpio& m_sda;
135 app::Timer& m_timer;
137 uint8_t* m_data = nullptr;
139 size_t m_dataSize = 0;
141 size_t m_dataIndex = 0;
143 uint8_t m_activeByte = 0;
145 bool m_writingAddress = false;
147 int8_t m_bitIndex = 7;
154 bool m_lastOperationWasWrite = false;
156 bool m_acknowledgeBit = false;
158 bool m_acknowledgeError = false;
160 SEMF_SLOT(m_timemoutSlot, SoftI2cMaster, *this, resartConditionSetScl);
162 static constexpr Error::ClassID kSemfClassId = Error::ClassID::SoftI2cMaster;
163};
164} /* namespace semf */
165#endif /* SEMF_COMMUNICATION_SOFTI2CMASTER_H_ */
@ FirstAndLast
start AND stop condition
ClassID
Semf class IDs.
Definition: error.h:28
Interface class for using a GPIO pin of the microcontroller.
Definition: gpio.h:23
For using the I2C in master mode.
This class used two GPIOs (SCL and SDA) and a Timer and implements a software I2C Master interface.
Definition: softi2cmaster.h:30
void stopWrite() override
virtual ~SoftI2cMaster()=default
SoftI2cMaster(Gpio &scl, Gpio &sda, app::Timer &timer)
Constructor.
void stopRead() override
void deinit() override
SoftI2cMaster(const SoftI2cMaster &other)=delete
void writeHardware(const uint8_t data[], size_t size) override
Hardware will write data. After finished write operation, onDataWritten() function will be called.
void readHardware(uint8_t buffer[], size_t bufferSize) override
Hardware will read data. After finished read operation, onDataAvailable() function will be called.
void init() override
ErrorCode
Error IDs for this class. Error ID identify a unique error() / onError call (excluding transferring).
Definition: softi2cmaster.h:36
Class for using timer hardware.
Definition: timer.h:24