34 m_lastOperationWasWrite =
false;
35 m_acknowledgeError =
false;
65 SEMF_INFO(
"write data %p with size %u", data, size);
66 m_data =
const_cast<uint8_t*
>(data);
70 m_acknowledgeError =
false;
75 m_writingAddress =
true;
76 startConditionResetSda();
82 m_lastFrame =
frame();
83 m_lastOperationWasWrite =
true;
88 SEMF_INFO(
"read data %p with size %u", buffer, bufferSize);
90 m_dataSize = bufferSize;
93 m_acknowledgeError =
false;
97 m_activeByte =
static_cast<uint8_t
>((
address() << 1) | 0x01);
98 m_writingAddress =
true;
99 startConditionResetSda();
103 m_activeByte =
static_cast<uint8_t
>((
address() << 1) | 0x01);
104 m_writingAddress =
true;
105 resartConditionSetSdaResetScl();
111 m_lastFrame =
frame();
112 m_lastOperationWasWrite =
false;
115void SoftI2cMaster::resartConditionSetSdaResetScl()
117 SEMF_INFO(
"do restart condition (followed by start condition)");
120 m_timemoutSlot.setFunction(
SEMF_SLOT_FUNC(resartConditionSetScl));
123void SoftI2cMaster::resartConditionSetScl()
126 m_timemoutSlot.setFunction(
SEMF_SLOT_FUNC(startConditionResetSda));
129void SoftI2cMaster::startConditionResetSda()
133 m_timemoutSlot.setFunction(
SEMF_SLOT_FUNC(startConditionResetScl));
136void SoftI2cMaster::startConditionResetScl()
142void SoftI2cMaster::writeByte()
144 if (m_writingAddress)
146 SEMF_INFO(
"start write address: %u", m_activeByte);
150 m_activeByte = m_data[m_dataIndex];
151 SEMF_INFO(
"start write byte: %u", m_activeByte);
153 writeByteSetSdaDataBit();
156void SoftI2cMaster::writeByteSetSdaDataBit()
159 if (m_activeByte & (1 << m_bitIndex))
166void SoftI2cMaster::writeByteSetScl()
172void SoftI2cMaster::writeByteResetScl()
180 writeByteSetSdaDataBit();
186 checkAcknowledgeSetSdaInput();
190void SoftI2cMaster::readByte()
196 readByteReadSdaDataBit();
199void SoftI2cMaster::readByteReadSdaDataBit()
204 m_activeByte =
static_cast<uint8_t
>(m_activeByte | (1 << m_bitIndex));
209void SoftI2cMaster::readByteSetScl()
215void SoftI2cMaster::readByteResetScl()
224 m_activeByte =
static_cast<uint8_t
>(m_activeByte | (1 << m_bitIndex));
225 readByteReadSdaDataBit();
231 setAcknowledgeSetSdaOutput();
235void SoftI2cMaster::checkAcknowledgeSetSdaInput()
239 m_timemoutSlot.setFunction(
SEMF_SLOT_FUNC(checkAcknowledgeSetScl));
242void SoftI2cMaster::checkAcknowledgeSetScl()
245 m_acknowledgeBit = m_sda.
state();
246 m_timemoutSlot.setFunction(
SEMF_SLOT_FUNC(checkAcknowledgeResetScl));
249void SoftI2cMaster::checkAcknowledgeResetScl()
257void SoftI2cMaster::setAcknowledgeSetSdaOutput()
260 if (m_dataIndex + 1 < m_dataSize)
265void SoftI2cMaster::setAcknowledgeSetScl()
268 m_timemoutSlot.setFunction(
SEMF_SLOT_FUNC(setAcknowledgeResetScl));
271void SoftI2cMaster::setAcknowledgeResetScl()
278void SoftI2cMaster::finishAcknowledge()
280 if (m_writingAddress)
282 m_writingAddress =
false;
287 m_data[m_dataIndex] = m_activeByte;
292 finishAcknowledgeReadOperation();
294 finishAcknowledgeWriteOperation();
297void SoftI2cMaster::finishAcknowledgeWriteOperation()
300 if (m_acknowledgeBit)
302 m_acknowledgeError =
true;
304 stopConditionResetSda();
309 if (m_dataIndex == m_dataSize)
313 stopConditionResetSda();
328void SoftI2cMaster::finishAcknowledgeReadOperation()
331 if (m_dataIndex == m_dataSize)
337 stopConditionResetSda();
352void SoftI2cMaster::stopConditionResetSda()
359void SoftI2cMaster::stopConditionSetScl()
366void SoftI2cMaster::stopConditionSetSda()
390void SoftI2cMaster::setFrequency(uint32_t hz)
@ FirstAndLast
start AND stop condition
@ Next
no start AND no stop condition
@ First
start condition, but no stop condition
Interface class for using a GPIO pin of the microcontroller.
virtual bool state() const =0
Returns the current state of the pin.
virtual void setDirection(Direction direction)=0
Sets the direction of a GPIO.
bool isBusyReading() const override
Communication hardware is busy reading at the moment.
CommunicationHardware::Frame frame() const
Returns the actual frame mode setting.
uint8_t address() const override
Returns the I2C slave device address.
void onError(Error thrown)
Is called if an error occurred by hardware read or write access. Will emit error signal.
bool isBusyWriting() const override
Communication hardware is busy writing at the moment.
void clear()
Disconnect all functions or methods.
void connect(SlotBase< Arguments... > &slot)
Connect a method to the signal.
void stopWrite() override
SoftI2cMaster(Gpio &scl, Gpio &sda, app::Timer &timer)
Constructor.
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.
@ StopConditionSetSda_NackError
Class for using timer hardware.
#define SEMF_SLOT_FUNC(function)
Creates a binding to a given member function.