mirror of
https://github.com/TomHarte/CLK.git
synced 2025-02-27 00:30:26 +00:00
Attempt some inspection.
This commit is contained in:
parent
47e9279bd4
commit
2712d50e05
@ -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) {
|
||||
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user