1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-10-02 04:55:56 +00:00
CLK/Components/I2C/I2C.cpp

130 lines
2.9 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_) {
result |= !(peripheral_response_ & 0x80);
}
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-18 01:55:19 +00:00
const bool prior_clock = clock_;
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-18 15:09:29 +00:00
logger.info().append("C:%d D:%d", clock_, data_);
2024-03-18 01:55:19 +00:00
// Advance peripheral input from peripheral if clock
// transitions from high to low.
if(peripheral_bits_ && prior_clock && !clock_) {
--peripheral_bits_;
peripheral_response_ <<= 1;
}
2024-03-17 02:02:16 +00:00
2024-03-18 01:55:19 +00:00
const auto capture_bit = [&]() {
if(prior_clock && !clock_) {
2024-03-17 02:02:16 +00:00
input_ = uint16_t((input_ << 1) | (data_pulled ? 0 : 1));
++input_count_;
}
2024-03-18 01:55:19 +00:00
};
2024-03-18 15:09:29 +00:00
const auto await_byte = [&] {
peripheral_response_ = 0;
peripheral_bits_ = 2;
phase_ = Phase::AwaitingByte;
logger.info().append("Waiting for byte");
};
2024-03-18 01:55:19 +00:00
// Check for stop condition at any time.
// "A LOW-to-HIGH transition of the data line
// while the clock is HIGH is defined as the STOP condition".
if(prior_data && !data_ && !clock_) {
2024-03-18 15:09:29 +00:00
logger.info().append("Stopped");
2024-03-18 01:55:19 +00:00
phase_ = Phase::AwaitingStart;
}
switch(phase_) {
case Phase::AwaitingStart:
// "A HIGH-to-LOW transition of the data line while
// the clock is HIGH is defined as the START condition"
if(!prior_data && data_ && !clock_) {
phase_ = Phase::CollectingAddress;
input_count_ = 0;
input_ = 0;
2024-03-18 15:09:29 +00:00
logger.info().append("Waiting for [remainder of] address");
2024-03-18 01:55:19 +00:00
}
break;
case Phase::CollectingAddress:
capture_bit();
if(input_count_ == 8) {
2024-03-18 15:09:29 +00:00
logger.info().append("Addressed %02x with %s?", uint8_t(input_) & 0xfe, input_ & 1 ? "read" : "write");
2024-03-17 02:02:16 +00:00
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-17 02:02:16 +00:00
2024-03-18 15:09:29 +00:00
await_byte();
2024-03-18 01:55:19 +00:00
} else {
phase_ = Phase::AwaitingStart;
2024-03-18 15:09:29 +00:00
logger.info().append("No device; not acknowledging");
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-18 01:55:19 +00:00
case Phase::AwaitingByte:
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
logger.info().append("Beginning byte");
phase_ = Phase::CollectingByte;
input_count_ = 0;
[[fallthrough]];
2024-03-18 01:55:19 +00:00
case Phase::CollectingByte:
capture_bit();
if(input_count_ == 8) {
2024-03-18 15:09:29 +00:00
logger.info().append("Got byte %02x?", uint8_t(input_));
await_byte();
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;
}