2024-03-16 19:00:23 +00:00
|
|
|
//
|
|
|
|
// I2C.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 16/03/2024.
|
|
|
|
// Copyright © 2024 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "I2C.hpp"
|
|
|
|
|
2024-03-18 15:09:29 +00:00
|
|
|
#include "../../Outputs/Log.hpp"
|
|
|
|
|
2024-03-16 19:00:23 +00:00
|
|
|
using namespace I2C;
|
|
|
|
|
2024-03-18 15:09:29 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
Log::Logger<Log::Source::I2C> logger;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-16 19:00:23 +00:00
|
|
|
void Bus::set_data(bool pulled) {
|
|
|
|
set_clock_data(clock_, pulled);
|
|
|
|
}
|
|
|
|
bool Bus::data() {
|
2024-03-18 01:55:19 +00:00
|
|
|
bool result = data_;
|
|
|
|
if(peripheral_bits_) {
|
2024-03-26 16:24:24 +00:00
|
|
|
result |= !(peripheral_response_ & 0x200);
|
2024-03-18 01:55:19 +00:00
|
|
|
}
|
|
|
|
return result;
|
2024-03-16 19:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::set_clock(bool pulled) {
|
|
|
|
set_clock_data(pulled, data_);
|
|
|
|
}
|
|
|
|
bool Bus::clock() {
|
|
|
|
return clock_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::set_clock_data(bool clock_pulled, bool data_pulled) {
|
2024-03-26 02:10:52 +00:00
|
|
|
if(clock_pulled == clock_ && data_pulled == data_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-18 01:55:19 +00:00
|
|
|
const bool prior_data = data_;
|
2024-03-16 19:00:23 +00:00
|
|
|
clock_ = clock_pulled;
|
|
|
|
data_ = data_pulled;
|
2024-03-17 02:02:16 +00:00
|
|
|
|
2024-03-26 02:10:52 +00:00
|
|
|
if(clock_) {
|
|
|
|
return;
|
|
|
|
}
|
2024-03-18 15:09:29 +00:00
|
|
|
|
2024-03-26 02:10:52 +00:00
|
|
|
if(prior_data != data_) {
|
|
|
|
if(data_) {
|
2024-03-26 16:27:37 +00:00
|
|
|
// logger.info().append("S");
|
2024-03-26 02:10:52 +00:00
|
|
|
signal(Event::Start);
|
|
|
|
} else {
|
2024-03-26 16:27:37 +00:00
|
|
|
// logger.info().append("P");
|
2024-03-26 02:10:52 +00:00
|
|
|
signal(Event::Stop);
|
|
|
|
}
|
|
|
|
} else {
|
2024-03-26 16:24:24 +00:00
|
|
|
if(peripheral_bits_) {
|
|
|
|
--peripheral_bits_;
|
|
|
|
peripheral_response_ <<= 1;
|
|
|
|
}
|
2024-03-26 02:10:52 +00:00
|
|
|
|
|
|
|
if(data_) {
|
2024-03-26 16:27:37 +00:00
|
|
|
// logger.info().append("0");
|
2024-03-26 02:10:52 +00:00
|
|
|
signal(Event::Zero);
|
|
|
|
} else {
|
2024-03-26 16:27:37 +00:00
|
|
|
// logger.info().append("1");
|
2024-03-26 02:10:52 +00:00
|
|
|
signal(Event::One);
|
|
|
|
}
|
2024-03-18 01:55:19 +00:00
|
|
|
}
|
2024-03-26 02:10:52 +00:00
|
|
|
}
|
2024-03-17 02:02:16 +00:00
|
|
|
|
2024-03-26 02:10:52 +00:00
|
|
|
void Bus::signal(Event event) {
|
2024-03-18 01:55:19 +00:00
|
|
|
const auto capture_bit = [&]() {
|
2024-03-26 16:24:24 +00:00
|
|
|
input_ = uint16_t((input_ << 1) | (event == Event::Zero ? 0 : 1));
|
|
|
|
++input_count_;
|
2024-03-18 01:55:19 +00:00
|
|
|
};
|
|
|
|
|
2024-03-26 16:24:24 +00:00
|
|
|
const auto acknowledge = [&]() {
|
|
|
|
// Post an acknowledgement bit.
|
2024-03-18 15:09:29 +00:00
|
|
|
peripheral_response_ = 0;
|
|
|
|
peripheral_bits_ = 2;
|
|
|
|
};
|
|
|
|
|
2024-03-26 16:24:24 +00:00
|
|
|
const auto set_state = [&](State state) {
|
|
|
|
state_ = state;
|
|
|
|
input_count_ = 0;
|
|
|
|
input_ = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto enqueue = [&](std::optional<uint8_t> next) {
|
|
|
|
if(next) {
|
|
|
|
peripheral_response_ = *next;
|
|
|
|
peripheral_bits_ = 9;
|
|
|
|
set_state(State::PostingByte);
|
|
|
|
} else {
|
|
|
|
acknowledge();
|
|
|
|
set_state(State::AwaitingAddress);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Allow start and stop conditions at any time.
|
|
|
|
if(event == Event::Start) {
|
|
|
|
set_state(State::CollectingAddress);
|
|
|
|
active_peripheral_ = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-26 02:10:52 +00:00
|
|
|
if(event == Event::Stop) {
|
2024-03-26 16:24:24 +00:00
|
|
|
set_state(State::AwaitingAddress);
|
|
|
|
if(active_peripheral_) {
|
|
|
|
active_peripheral_->stop();
|
|
|
|
}
|
|
|
|
active_peripheral_ = nullptr;
|
|
|
|
return;
|
2024-03-18 01:55:19 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 16:24:24 +00:00
|
|
|
switch(state_) {
|
|
|
|
case State::AwaitingAddress: break;
|
2024-03-18 01:55:19 +00:00
|
|
|
|
2024-03-26 16:24:24 +00:00
|
|
|
case State::CollectingAddress:
|
2024-03-18 01:55:19 +00:00
|
|
|
capture_bit();
|
2024-03-17 02:02:16 +00:00
|
|
|
|
2024-03-26 16:24:24 +00:00
|
|
|
if(input_count_ == 8) {
|
2024-03-18 15:09:29 +00:00
|
|
|
auto pair = peripherals_.find(uint8_t(input_) & 0xfe);
|
2024-03-18 01:55:19 +00:00
|
|
|
if(pair != peripherals_.end()) {
|
|
|
|
active_peripheral_ = pair->second;
|
2024-03-26 16:24:24 +00:00
|
|
|
active_peripheral_->start(input_ & 1);
|
|
|
|
|
|
|
|
if(input_&1) {
|
|
|
|
enqueue(active_peripheral_->read());
|
|
|
|
} else {
|
|
|
|
acknowledge();
|
|
|
|
set_state(State::ReceivingByte);
|
|
|
|
}
|
2024-03-18 01:55:19 +00:00
|
|
|
} else {
|
2024-03-26 16:24:24 +00:00
|
|
|
state_ = State::AwaitingAddress;
|
2024-03-17 02:02:16 +00:00
|
|
|
}
|
2024-03-18 01:55:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2024-03-17 02:02:16 +00:00
|
|
|
|
2024-03-26 16:24:24 +00:00
|
|
|
case State::ReceivingByte:
|
2024-03-18 02:14:07 +00:00
|
|
|
// Run down the clock on the acknowledge bit.
|
2024-03-18 15:09:29 +00:00
|
|
|
if(peripheral_bits_) {
|
|
|
|
return;
|
2024-03-18 01:55:19 +00:00
|
|
|
}
|
2024-03-18 15:09:29 +00:00
|
|
|
|
2024-03-18 01:55:19 +00:00
|
|
|
capture_bit();
|
|
|
|
if(input_count_ == 8) {
|
2024-03-26 16:24:24 +00:00
|
|
|
active_peripheral_->write(static_cast<uint8_t>(input_));
|
|
|
|
acknowledge();
|
|
|
|
set_state(State::ReceivingByte);
|
|
|
|
}
|
|
|
|
break;
|
2024-03-18 15:09:29 +00:00
|
|
|
|
2024-03-26 16:24:24 +00:00
|
|
|
case State::PostingByte:
|
|
|
|
// Finish whatever is enqueued.
|
|
|
|
if(peripheral_bits_) {
|
|
|
|
return;
|
2024-03-18 01:55:19 +00:00
|
|
|
}
|
2024-03-26 16:24:24 +00:00
|
|
|
|
|
|
|
enqueue(active_peripheral_->read());
|
2024-03-18 01:55:19 +00:00
|
|
|
break;
|
2024-03-17 02:02:16 +00:00
|
|
|
}
|
2024-03-16 19:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Bus::add_peripheral(Peripheral *peripheral, int address) {
|
|
|
|
peripherals_[address] = peripheral;
|
|
|
|
}
|