1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-18 13:29:41 +00:00

Attempt some inspection.

This commit is contained in:
Thomas Harte 2024-03-16 22:02:16 -04:00
parent 47e9279bd4
commit 2712d50e05
2 changed files with 47 additions and 1 deletions

View File

@ -25,9 +25,44 @@ bool Bus::clock() {
}
void Bus::set_clock_data(bool clock_pulled, bool data_pulled) {
// TODO: all intelligence.
const bool has_new_bit = clock_pulled && !clock_;
clock_ = clock_pulled;
data_ = data_pulled;
// TODO: "Stop condition: SDA goes high after SCL (?)".
if(has_new_bit) {
// Test for a start bit.
//
// "Start condition: SDA goes low before SCL".
if(input_count_ < 0 && data_) {
input_count_ = 0;
}
// Accumulate if started.
if(input_count_ >= 0) {
input_ = uint16_t((input_ << 1) | (data_pulled ? 0 : 1));
++input_count_;
}
// Test for meaning.
switch(phase_) {
case Phase::AwaitingAddress:
if(input_count_ == 9) {
printf("Calling %02x?", uint8_t(input_));
auto pair = peripherals_.find(uint8_t(input_));
if(pair != peripherals_.end()) {
active_peripheral_ = pair->second;
// TODO: device found; pull data low for the next clock.
}
}
break;
case Phase::CollectingData:
break;
}
}
}
void Bus::add_peripheral(Peripheral *peripheral, int address) {

View File

@ -8,6 +8,7 @@
#pragma once
#include <cstdint>
#include <unordered_map>
namespace I2C {
@ -34,6 +35,16 @@ private:
bool data_ = false;
bool clock_ = false;
std::unordered_map<int, Peripheral *> peripherals_;
uint16_t input_ = 0xffff;
int input_count_ = -1;
Peripheral *active_peripheral_ = nullptr;
enum class Phase {
AwaitingAddress,
CollectingData,
} phase_ = Phase::AwaitingAddress;
};
}