1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-01 11:49:58 +00:00
CLK/Components/I2C/I2C.cpp

227 lines
5.3 KiB
C++
Raw Normal View History

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-27 01:33:46 +00:00
result |= !(peripheral_response_ & 0x80);
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) {
// Proceed only if changes are evidenced.
2024-03-26 02:10:52 +00:00
if(clock_pulled == clock_ && data_pulled == data_) {
return;
}
2024-03-27 01:33:46 +00:00
const bool prior_clock = clock_;
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
// If currently serialising from a peripheral then shift onwards on
// every clock trailing edge.
2024-03-27 01:33:46 +00:00
if(peripheral_bits_) {
// Trailing edge of clock => bit has been consumed.
if(!prior_clock && clock_) {
logger.info().append("<< %d", (peripheral_response_ >> 7) & 1);
--peripheral_bits_;
peripheral_response_ <<= 1;
if(!peripheral_bits_) {
signal(Event::FinishedOutput);
}
}
2024-03-26 02:10:52 +00:00
return;
}
2024-03-18 15:09:29 +00:00
// Not currently serialising implies listening.
2024-03-27 01:33:46 +00:00
if(!clock_ && prior_data != data_) {
// A data transition outside of a clock cycle implies a start or stop.
2024-03-27 01:33:46 +00:00
in_bit_ = false;
2024-03-26 02:10:52 +00:00
if(data_) {
2024-03-27 01:33:46 +00:00
logger.info().append("S");
2024-03-26 02:10:52 +00:00
signal(Event::Start);
} else {
2024-03-27 01:33:46 +00:00
logger.info().append("W");
2024-03-26 02:10:52 +00:00
signal(Event::Stop);
}
2024-03-27 01:33:46 +00:00
} else if(clock_ != prior_clock) {
// Bits: wait until the falling edge of the cycle.
if(!clock_) {
// Rising edge: clock period begins.
in_bit_ = true;
} else if(in_bit_) {
// Falling edge: clock period ends (assuming it began; otherwise this is a preparatory
// clock transition only, immediately after a start bit).
2024-03-27 01:33:46 +00:00
in_bit_ = false;
if(data_) {
logger.info().append("0");
signal(Event::Zero);
} else {
logger.info().append("1");
signal(Event::One);
}
2024-03-26 02:10:52 +00:00
}
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 = [&]() {
input_ = uint16_t((input_ << 1) | (event == Event::Zero ? 0 : 1));
++input_count_;
2024-03-18 01:55:19 +00:00
};
const auto acknowledge = [&]() {
// Post an acknowledgement bit.
2024-03-18 15:09:29 +00:00
peripheral_response_ = 0;
2024-03-27 01:33:46 +00:00
peripheral_bits_ = 1;
2024-03-18 15:09:29 +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) {
2024-03-27 01:33:46 +00:00
peripheral_response_ = static_cast<uint16_t>(*next);
peripheral_bits_ = 8;
set_state(State::AwaitingByteAcknowledge);
} else {
set_state(State::AwaitingAddress);
}
};
const auto stop = [&]() {
set_state(State::AwaitingAddress);
active_peripheral_ = nullptr;
};
// 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) {
if(active_peripheral_) {
active_peripheral_->stop();
}
stop();
return;
2024-03-18 01:55:19 +00:00
}
switch(state_) {
// While waiting for an address, don't respond to anything other than a
// start bit, which is actually dealt with above.
case State::AwaitingAddress: break;
2024-03-18 01:55:19 +00:00
// To collect an address: shift in eight bits, and if there's a device
// at that address then acknowledge the address and segue into a read
// or write loop.
case State::CollectingAddress:
2024-03-18 01:55:19 +00:00
capture_bit();
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;
active_peripheral_->start(input_ & 1);
if(input_&1) {
2024-03-26 18:06:11 +00:00
acknowledge();
set_state(State::CompletingReadAcknowledge);
} else {
acknowledge();
set_state(State::ReceivingByte);
}
2024-03-18 01:55:19 +00:00
} else {
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
// Receiving byte: wait until a scheduled acknowledgment has
// happened, then collect eight bits, then see whether the
// active peripheral will accept them. If so, acknowledge and repeat.
// Otherwise fall silent.
case State::ReceivingByte:
2024-03-27 01:33:46 +00:00
if(event == Event::FinishedOutput) {
2024-03-18 15:09:29 +00:00
return;
2024-03-18 01:55:19 +00:00
}
capture_bit();
if(input_count_ == 8) {
if(active_peripheral_->write(static_cast<uint8_t>(input_))) {
acknowledge();
set_state(State::ReceivingByte);
} else {
stop();
}
}
break;
2024-03-18 15:09:29 +00:00
// The initial state immediately after a peripheral has been started
// in read mode and the address-select acknowledgement is still
// being serialised.
//
// Once that is completed, enqueues the first byte from the peripheral.
case State::CompletingReadAcknowledge:
2024-03-27 01:33:46 +00:00
if(event != Event::FinishedOutput) {
break;
}
enqueue(active_peripheral_->read());
break;
// Repeating state during reading; waits until the previous byte has
// been fully serialised, and if the host acknowledged it then posts
// the next. If the host didn't acknowledge, stops the connection.
2024-03-27 01:33:46 +00:00
case State::AwaitingByteAcknowledge:
if(event == Event::FinishedOutput) {
break;
}
if(event != Event::Zero) {
stop();
2024-03-27 01:33:46 +00:00
break;
2024-03-18 01:55:19 +00:00
}
2024-03-27 01:33:46 +00:00
// Add a new byte if there is one.
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;
}