[FL-84] iButton app, dallas emulate + cyfral read, cyfral emulate (#253)

* maxim crc function
* one wire template device and ds1990 classes
* 3 fields for addr
* cyfral emulator lib
* add cyfral read mode, refract rendering and events
* add ADC1_IN14, add adc interrupt
* cyfral read mode
* rename and move api-hal includes folder
* build onewire libs only if we build app
* start in mode 0
This commit is contained in:
DrZlo13
2020-11-25 10:25:13 +03:00
committed by GitHub
parent 758e37e294
commit 1f761d7fbb
37 changed files with 1996 additions and 821 deletions

View File

@@ -0,0 +1,95 @@
#pragma once
#include "flipper.h"
#include "flipper_v2.h"
class CyfralTiming {
public:
constexpr static const uint8_t ZERO_HIGH = 50;
constexpr static const uint8_t ZERO_LOW = 70;
constexpr static const uint8_t ONE_HIGH = 100;
constexpr static const uint8_t ONE_LOW = 70;
};
class CyfralEmulator {
private:
void send_nibble(uint8_t nibble);
void send_byte(uint8_t data);
inline void send_bit(bool bit);
const GpioPin* emulate_pin_record;
public:
CyfralEmulator(const GpioPin* emulate_pin);
~CyfralEmulator();
void send(uint8_t* data, uint8_t count = 1, uint8_t repeat = 1);
void start(void);
void stop(void);
};
// 7 = 0 1 1 1
// B = 1 0 1 1
// D = 1 1 0 1
// E = 1 1 1 0
void CyfralEmulator::send_nibble(uint8_t nibble) {
for(uint8_t i = 0; i < 4; i++) {
bool bit = nibble & (0b1000 >> i);
send_bit(bit);
}
}
void CyfralEmulator::send_byte(uint8_t data) {
for(uint8_t i = 0; i < 8; i++) {
bool bit = data & (0b10000000 >> i);
send_bit(bit);
}
}
void CyfralEmulator::send_bit(bool bit) {
if(!bit) {
gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ZERO_LOW);
gpio_write(&ibutton_gpio, true);
delay_us(CyfralTiming::ZERO_HIGH);
gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ZERO_LOW);
} else {
gpio_write(&ibutton_gpio, true);
delay_us(CyfralTiming::ONE_HIGH);
gpio_write(&ibutton_gpio, false);
delay_us(CyfralTiming::ONE_LOW);
}
}
CyfralEmulator::CyfralEmulator(const GpioPin* emulate_pin) {
emulate_pin_record = emulate_pin;
}
CyfralEmulator::~CyfralEmulator() {
}
void CyfralEmulator::send(uint8_t* data, uint8_t count, uint8_t repeat) {
osKernelLock();
__disable_irq();
for(uint8_t i = 0; i < repeat; i++) {
// start sequence
send_nibble(0x01);
// send data
for(uint8_t i = 0; i < count; i++) {
send_byte(data[i]);
}
}
__enable_irq();
osKernelUnlock();
}
void CyfralEmulator::start(void) {
gpio_init(emulate_pin_record, GpioModeOutputOpenDrain);
gpio_write(emulate_pin_record, false);
}
void CyfralEmulator::stop(void) {
gpio_init(emulate_pin_record, GpioModeAnalog);
}

273
lib/cyfral/cyfral_reader.h Normal file
View File

@@ -0,0 +1,273 @@
#pragma once
#include "flipper.h"
#include "flipper_v2.h"
enum class CyfralReaderError : uint8_t {
NO_ERROR = 0,
UNABLE_TO_DETECT = 1,
RAW_DATA_SIZE_ERROR = 2,
UNKNOWN_NIBBLE_VALUE = 3,
NO_START_NIBBLE = 4,
};
class CyfralReader {
private:
ADC_HandleTypeDef adc_config;
ADC_TypeDef* adc_instance;
uint32_t adc_channel;
void get_line_minmax(uint16_t times, uint32_t* min_level, uint32_t* max_level);
void capture_data(bool* data, uint16_t capture_size, uint32_t line_min, uint32_t line_max);
bool parse_data(bool* raw_data, uint16_t capture_size, uint8_t* data, uint8_t count);
uint32_t search_array_in_array(
const bool* haystack,
const uint32_t haystack_size,
const bool* needle,
const uint32_t needle_size);
// key is 9 nibbles
static const uint16_t bits_in_nibble = 4;
static const uint16_t key_length = 9;
static const uint32_t capture_size = key_length * bits_in_nibble * 2;
CyfralReaderError error;
public:
CyfralReader(ADC_TypeDef* adc, uint32_t Channel);
~CyfralReader();
void start(void);
void stop(void);
bool read(uint8_t* data, uint8_t count);
};
void CyfralReader::get_line_minmax(uint16_t times, uint32_t* min_level, uint32_t* max_level) {
uint32_t in = 0;
uint32_t min = UINT_MAX;
uint32_t max = 0;
for(uint32_t i = 0; i < 256; i++) {
HAL_ADC_Start(&adc_config);
HAL_ADC_PollForConversion(&adc_config, 100);
in = HAL_ADC_GetValue(&adc_config);
if(in < min) min = in;
if(in > max) max = in;
}
*min_level = min;
*max_level = max;
}
void CyfralReader::capture_data(
bool* data,
uint16_t capture_size,
uint32_t line_min,
uint32_t line_max) {
uint32_t input_value = 0;
bool last_input_value = 0;
uint32_t diff = line_max - line_min;
uint32_t mid = line_min + diff / 2;
uint32_t low_threshold = mid - (diff / 4);
uint32_t high_threshold = mid - (diff / 4);
uint16_t capture_position = 0;
uint32_t instructions_per_us = (SystemCoreClock / 1000000.0f);
uint32_t time_threshold = 75 * instructions_per_us;
uint32_t capture_max_time = 140 * (capture_size * 2) * instructions_per_us;
uint32_t start = DWT->CYCCNT;
uint32_t end = DWT->CYCCNT;
memset(data, 0, capture_size);
osKernelLock();
uint32_t capture_start = DWT->CYCCNT;
while((capture_position < capture_size) &&
((DWT->CYCCNT - capture_start) < capture_max_time)) {
// read adc
HAL_ADC_Start(&adc_config);
HAL_ADC_PollForConversion(&adc_config, 100);
input_value = HAL_ADC_GetValue(&adc_config);
// low to high transition
if((input_value > high_threshold) && last_input_value == 0) {
last_input_value = 1;
start = DWT->CYCCNT;
}
// high to low transition
if((input_value < low_threshold) && last_input_value == 1) {
last_input_value = 0;
end = DWT->CYCCNT;
// check transition time
if(end - start < time_threshold) {
data[capture_position] = 1;
capture_position++;
} else {
data[capture_position] = 0;
capture_position++;
}
}
}
osKernelUnlock();
}
uint32_t CyfralReader::search_array_in_array(
const bool* haystack,
const uint32_t haystack_size,
const bool* needle,
const uint32_t needle_size) {
uint32_t haystack_index = 0, needle_index = 0;
while(haystack_index < haystack_size && needle_index < needle_size) {
if(haystack[haystack_index] == needle[needle_index]) {
haystack_index++;
needle_index++;
if(needle_index == needle_size) {
return (haystack_index - needle_size);
};
} else {
haystack_index = haystack_index - needle_index + 1;
needle_index = 0;
}
}
return haystack_index;
}
bool CyfralReader::parse_data(bool* raw_data, uint16_t capture_size, uint8_t* data, uint8_t count) {
const bool start_nibble[bits_in_nibble] = {1, 1, 1, 0};
uint32_t start_position =
search_array_in_array(raw_data, capture_size, start_nibble, bits_in_nibble);
uint32_t end_position = 0;
memset(data, 0, count);
if(start_position < capture_size) {
start_position = start_position + bits_in_nibble;
end_position = start_position + count * 2 * bits_in_nibble;
if(end_position >= capture_size) {
error = CyfralReaderError::RAW_DATA_SIZE_ERROR;
return false;
}
bool first_nibble = true;
uint8_t data_position = 0;
uint8_t nibble_value = 0;
while(data_position < count) {
nibble_value = !raw_data[start_position] << 3 | !raw_data[start_position + 1] << 2 |
!raw_data[start_position + 2] << 1 | !raw_data[start_position + 3];
switch(nibble_value) {
case(0x7):
case(0xB):
case(0xD):
case(0xE):
break;
default:
error = CyfralReaderError::UNKNOWN_NIBBLE_VALUE;
return false;
break;
}
if(first_nibble) {
data[data_position] |= nibble_value << 4;
} else {
data[data_position] |= nibble_value;
}
first_nibble = !first_nibble;
if(first_nibble) {
data_position++;
}
start_position = start_position + bits_in_nibble;
}
error = CyfralReaderError::NO_ERROR;
return true;
}
error = CyfralReaderError::NO_START_NIBBLE;
return false;
}
CyfralReader::CyfralReader(ADC_TypeDef* adc, uint32_t channel) {
adc_instance = adc;
adc_channel = channel;
}
CyfralReader::~CyfralReader() {
}
void CyfralReader::start(void) {
ADC_ChannelConfTypeDef sConfig = {0};
// init ADC
adc_config.Instance = adc_instance;
adc_config.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
adc_config.Init.Resolution = ADC_RESOLUTION_12B;
adc_config.Init.DataAlign = ADC_DATAALIGN_RIGHT;
adc_config.Init.ScanConvMode = ADC_SCAN_DISABLE;
adc_config.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
adc_config.Init.LowPowerAutoWait = DISABLE;
adc_config.Init.ContinuousConvMode = DISABLE;
adc_config.Init.NbrOfConversion = 1;
adc_config.Init.DiscontinuousConvMode = DISABLE;
adc_config.Init.ExternalTrigConv = ADC_SOFTWARE_START;
adc_config.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
adc_config.Init.DMAContinuousRequests = DISABLE;
adc_config.Init.Overrun = ADC_OVR_DATA_PRESERVED;
adc_config.Init.OversamplingMode = DISABLE;
if(HAL_ADC_Init(&adc_config) != HAL_OK) {
Error_Handler();
}
// init channel
sConfig.Channel = adc_channel;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if(HAL_ADC_ConfigChannel(&adc_config, &sConfig) != HAL_OK) {
Error_Handler();
}
}
void CyfralReader::stop(void) {
HAL_ADC_DeInit(&adc_config);
}
bool CyfralReader::read(uint8_t* data, uint8_t count) {
uint32_t line_level_min, line_level_max;
bool raw_data[capture_size];
bool result = false;
error = CyfralReaderError::NO_ERROR;
// calibrate
get_line_minmax(256, &line_level_min, &line_level_max);
// TODO think about other detection method
// key not on line
if(line_level_max > 2000) {
error = CyfralReaderError::UNABLE_TO_DETECT;
return false;
}
// capturing raw data consisting of bits
capture_data(raw_data, capture_size, line_level_min, line_level_max);
// parse captured data
if(parse_data(raw_data, capture_size, data, count)) {
result = true;
}
return result;
}

View File

@@ -54,4 +54,18 @@ CFLAGS += -I$(LIB_DIR)/app-template
# fnv1a hash library
CFLAGS += -I$(LIB_DIR)/fnv1a-hash
C_SOURCES += $(LIB_DIR)/fnv1a-hash/fnv1a-hash.c
C_SOURCES += $(LIB_DIR)/fnv1a-hash/fnv1a-hash.c
# build onewire/cyfral library only if
# we build iButton application
ifeq ($(BUILD_IBUTTON), 1)
# onewire library
ONEWIRE_DIR = $(LIB_DIR)/onewire
CFLAGS += -I$(ONEWIRE_DIR)
CPP_SOURCES += $(wildcard $(ONEWIRE_DIR)/*.cpp)
# cyfral library
CYFRAL_DIR = $(LIB_DIR)/cyfral
CFLAGS += -I$(CYFRAL_DIR)
CPP_SOURCES += $(wildcard $(CYFRAL_DIR)/*.cpp)
endif

48
lib/onewire/maxim_crc.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include "maxim_crc.h"
uint8_t maxim_crc8(const uint8_t* data, const uint8_t data_size, const uint8_t crc_init) {
uint8_t crc = crc_init;
for(uint8_t index = 0; index < data_size; ++index) {
uint8_t input_byte = data[index];
for(uint8_t bit_position = 0; bit_position < 8; ++bit_position) {
const uint8_t mix = (crc ^ input_byte) & static_cast<uint8_t>(0x01);
crc >>= 1;
if(mix != 0) crc ^= 0x8C;
input_byte >>= 1;
}
}
return crc;
}
uint16_t maxim_crc16(const uint8_t* address, const uint8_t length, const uint16_t init) {
uint16_t crc = init;
static const uint8_t odd_parity[16] = {0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0};
for(uint8_t i = 0; i < length; ++i) {
uint16_t cdata = address[i];
cdata = (cdata ^ crc) & static_cast<uint16_t>(0xff);
crc >>= 8;
if((odd_parity[cdata & 0x0F] ^ odd_parity[cdata >> 4]) != 0) crc ^= 0xC001;
cdata <<= 6;
crc ^= cdata;
cdata <<= 1;
crc ^= cdata;
}
return crc;
}
uint16_t maxim_crc16(uint8_t value, uint16_t crc) {
static const uint8_t odd_parity[16] = {0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0};
value = (value ^ static_cast<uint8_t>(crc));
crc >>= 8;
if((odd_parity[value & 0x0F] ^ odd_parity[value >> 4]) != 0) crc ^= 0xC001;
uint16_t cdata = (static_cast<uint16_t>(value) << 6);
crc ^= cdata;
crc ^= (static_cast<uint16_t>(cdata) << 1);
return crc;
}

6
lib/onewire/maxim_crc.h Normal file
View File

@@ -0,0 +1,6 @@
#pragma once
#include <stdint.h>
uint8_t maxim_crc8(const uint8_t* data, const uint8_t data_size, const uint8_t crc_init = 0);
uint16_t maxim_crc16(const uint8_t* address, const uint8_t length, const uint16_t init = 0);
uint16_t maxim_crc16(uint8_t value, uint16_t crc);

View File

@@ -0,0 +1,26 @@
#include "one_wire_device.h"
// TODO fix GPL compability
// currently we use rework of OneWireHub
OneWireDevice::OneWireDevice(
uint8_t id_1,
uint8_t id_2,
uint8_t id_3,
uint8_t id_4,
uint8_t id_5,
uint8_t id_6,
uint8_t id_7) {
id_storage[0] = id_1;
id_storage[1] = id_2;
id_storage[2] = id_3;
id_storage[3] = id_4;
id_storage[4] = id_5;
id_storage[5] = id_6;
id_storage[6] = id_7;
id_storage[7] = maxim_crc8(id_storage, 7);
}
void OneWireDevice::send_id(OneWireGpioSlave* owner) const {
owner->send(id_storage, 8);
}

View File

@@ -0,0 +1,34 @@
#pragma once
#include <stdint.h>
#include "maxim_crc.h"
#include "one_wire_slave_gpio.h"
// TODO fix GPL compability
// currently we use rework of OneWireHub
class OneWireDevice {
public:
OneWireDevice(
uint8_t id_1,
uint8_t id_2,
uint8_t id_3,
uint8_t id_4,
uint8_t id_5,
uint8_t id_6,
uint8_t id_7);
~OneWireDevice() = default; // TODO: detach if deleted before hub
// allow only move constructor
OneWireDevice(OneWireDevice&& one_wire_device) = default;
OneWireDevice(const OneWireDevice& one_wire_device) = delete;
OneWireDevice& operator=(OneWireDevice& one_wire_device) = delete;
OneWireDevice& operator=(const OneWireDevice& one_wire_device) = delete;
OneWireDevice& operator=(OneWireDevice&& one_wire_device) = delete;
uint8_t id_storage[8];
void send_id(OneWireGpioSlave* owner) const;
virtual void do_work(OneWireGpioSlave* owner) = 0;
};

View File

@@ -0,0 +1,27 @@
#include "one_wire_device_ds_1990.h"
// TODO fix GPL compability
// currently we use rework of OneWireHub
DS1990::DS1990(
uint8_t ID1,
uint8_t ID2,
uint8_t ID3,
uint8_t ID4,
uint8_t ID5,
uint8_t ID6,
uint8_t ID7)
: OneWireDevice(ID1, ID2, ID3, ID4, ID5, ID6, ID7) {
}
void DS1990::do_work(OneWireGpioSlave* owner) {
uint8_t cmd;
if(owner->receive(&cmd)) return;
switch(cmd) {
default:
return;
//owner->raiseSlaveError(cmd);
}
}

View File

@@ -0,0 +1,21 @@
#pragma once
#include "one_wire_device.h"
// TODO fix GPL compability
// currently we use rework of OneWireHub
class DS1990 : public OneWireDevice {
public:
static constexpr uint8_t family_code{0x01};
DS1990(
uint8_t ID1,
uint8_t ID2,
uint8_t ID3,
uint8_t ID4,
uint8_t ID5,
uint8_t ID6,
uint8_t ID7);
void do_work(OneWireGpioSlave* owner) final;
};

130
lib/onewire/one_wire_gpio.h Normal file
View File

@@ -0,0 +1,130 @@
#pragma once
#include "flipper.h"
#include "flipper_v2.h"
#include "one_wire_timings.h"
class OneWireGpio {
private:
const GpioPin* gpio;
public:
OneWireGpio(const GpioPin* one_wire_gpio);
~OneWireGpio();
bool reset(void);
bool read_bit(void);
uint8_t read(void);
void read_bytes(uint8_t* buf, uint16_t count);
void write_bit(bool value);
void write(uint8_t value);
void start(void);
void stop(void);
};
OneWireGpio::OneWireGpio(const GpioPin* one_wire_gpio) {
gpio = one_wire_gpio;
}
OneWireGpio::~OneWireGpio() {
stop();
}
void OneWireGpio::start(void) {
gpio_init(gpio, GpioModeOutputOpenDrain);
}
void OneWireGpio::stop(void) {
gpio_init(gpio, GpioModeAnalog);
}
bool OneWireGpio::reset(void) {
uint8_t r;
uint8_t retries = 125;
// wait until the gpio is high
gpio_write(gpio, true);
do {
if(--retries == 0) return 0;
delay_us(2);
} while(!gpio_read(gpio));
// pre delay
delay_us(OneWireTiming::RESET_DELAY_PRE);
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::RESET_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::RESET_RELEASE);
// read and post delay
r = !gpio_read(gpio);
delay_us(OneWireTiming::RESET_DELAY_POST);
return r;
}
bool OneWireGpio::read_bit(void) {
bool result;
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::READ_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::READ_RELEASE);
// read and post delay
result = gpio_read(gpio);
delay_us(OneWireTiming::READ_DELAY_POST);
return result;
}
void OneWireGpio::write_bit(bool value) {
if(value) {
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_1_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_1_RELEASE);
} else {
// drive low
gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_0_DRIVE);
// release
gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_0_RELEASE);
}
}
uint8_t OneWireGpio::read(void) {
uint8_t result = 0;
for(uint8_t bitMask = 0x01; bitMask; bitMask <<= 1) {
if(read_bit()) {
result |= bitMask;
}
}
return result;
}
void OneWireGpio::read_bytes(uint8_t* buffer, uint16_t count) {
for(uint16_t i = 0; i < count; i++) {
buffer[i] = read();
}
}
void OneWireGpio::write(uint8_t value) {
uint8_t bitMask;
for(bitMask = 0x01; bitMask; bitMask <<= 1) {
write_bit((bitMask & value) ? 1 : 0);
}
}

View File

@@ -0,0 +1,511 @@
#include "one_wire_slave_gpio.h"
#include "one_wire_device.h"
#include "one_wire_device_ds_1990.h"
// TODO fix GPL compability
// currently we use rework of OneWireHub
static uint32_t __instructions_per_us = 0;
OneWireGpioSlave::OneWireGpioSlave(const GpioPin* one_wire_gpio) {
gpio = one_wire_gpio;
error = OneWireGpioSlaveError::NO_ERROR;
devices_count = 0;
device_selected = nullptr;
for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
devices[i] = nullptr;
}
__instructions_per_us = (SystemCoreClock / 1000000.0f);
}
OneWireGpioSlave::~OneWireGpioSlave() {
stop();
}
void OneWireGpioSlave::start(void) {
gpio_init(gpio, GpioModeOutputOpenDrain);
}
void OneWireGpioSlave::stop(void) {
gpio_init(gpio, GpioModeAnalog);
}
bool OneWireGpioSlave::emulate() {
error = OneWireGpioSlaveError::NO_ERROR;
while(1) {
if(devices_count == 0) return false;
if(!check_reset()) {
return false;
} else {
}
// OK, we receive reset
osKernelLock();
if(!show_presence()) {
return false;
} else {
}
// and we succefully show our presence on bus
__disable_irq();
if(!receive_and_process_cmd()) {
__enable_irq();
osKernelUnlock();
return false;
} else {
__enable_irq();
osKernelUnlock();
return (error == OneWireGpioSlaveError::NO_ERROR);
}
}
}
OneWiteTimeType OneWireGpioSlave::wait_while_gpio_is(OneWiteTimeType time, const bool pin_value) {
uint32_t start = DWT->CYCCNT;
uint32_t time_ticks = time * __instructions_per_us;
uint32_t time_captured;
do {
time_captured = DWT->CYCCNT;
if(gpio_read(gpio) != pin_value) {
OneWiteTimeType remaining_time = time_ticks - (time_captured - start);
remaining_time /= __instructions_per_us;
return remaining_time;
}
} while((time_captured - start) < time_ticks);
return 0;
}
void OneWireGpioSlave::pin_set_float() {
gpio_write(gpio, true);
}
void OneWireGpioSlave::pin_set_low() {
gpio_write(gpio, false);
}
const char* OneWireGpioSlave::decode_error() {
const char* error_text[16] = {
"NO_ERROR",
"READ_TIMESLOT_TIMEOUT",
"WRITE_TIMESLOT_TIMEOUT",
"WAIT_RESET_TIMEOUT",
"VERY_LONG_RESET",
"VERY_SHORT_RESET",
"PRESENCE_LOW_ON_LINE",
"READ_TIMESLOT_TIMEOUT_LOW",
"AWAIT_TIMESLOT_TIMEOUT_HIGH",
"PRESENCE_HIGH_ON_LINE",
"INCORRECT_ONEWIRE_CMD",
"INCORRECT_SLAVE_USAGE",
"TRIED_INCORRECT_WRITE",
"FIRST_TIMESLOT_TIMEOUT",
"FIRST_BIT_OF_BYTE_TIMEOUT",
"RESET_IN_PROGRESS"};
return error_text[static_cast<uint8_t>(error)];
}
uint8_t OneWireGpioSlave::attach(OneWireDevice& device) {
if(devices_count >= ONE_WIRE_MAX_DEVICES) return 255; // hub is full
uint8_t position = 255;
for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
if(devices[i] == &device) {
return i;
}
if((position > ONE_WIRE_MAX_DEVICES) && (devices[i] == nullptr)) {
position = i;
}
}
if(position == 255) return 255;
devices[position] = &device;
devices_count++;
build_id_tree();
return position;
}
bool OneWireGpioSlave::detach(const OneWireDevice& device) {
uint8_t position = 255;
for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
if(devices[i] == &device) {
position = i;
break;
}
}
if(position != 255) return detach(position);
return false;
}
bool OneWireGpioSlave::detach(uint8_t device_number) {
if(devices[device_number] == nullptr) return false;
if(devices_count == 0) return false;
if(device_number >= ONE_WIRE_MAX_DEVICES) return false;
devices[device_number] = nullptr;
devices_count--;
build_id_tree();
return true;
}
uint8_t OneWireGpioSlave::get_next_device_index(const uint8_t index_start) const {
for(uint8_t i = index_start; i < ONE_WIRE_MAX_DEVICES; ++i) {
if(devices[i] != nullptr) return i;
}
return 0;
}
uint8_t OneWireGpioSlave::build_id_tree(void) {
uint32_t device_mask = 0;
uint32_t bit_mask = 0x01;
// build mask
for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
if(devices[i] != nullptr) device_mask |= bit_mask;
bit_mask <<= 1;
}
for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
id_tree[i].id_position = 255;
}
// begin with root-element
build_id_tree(0, device_mask); // goto branch
return 0;
}
uint8_t OneWireGpioSlave::build_id_tree(uint8_t id_bit_position, uint32_t device_mask) {
if(device_mask == 0) return (255);
while(id_bit_position < 64) {
uint32_t mask_pos{0};
uint32_t mask_neg{0};
const uint8_t pos_byte{static_cast<uint8_t>(id_bit_position >> 3)};
const uint8_t mask_bit{static_cast<uint8_t>(1 << (id_bit_position & 7))};
uint32_t mask_id{1};
// searchid_tree through all active slaves
for(uint8_t id = 0; id < ONE_WIRE_MAX_DEVICES; ++id) {
if((device_mask & mask_id) != 0) {
// if slave is in mask differentiate the bitValue
if((devices[id]->id_storage[pos_byte] & mask_bit) != 0)
mask_pos |= mask_id;
else
mask_neg |= mask_id;
}
mask_id <<= 1;
}
if((mask_neg != 0) && (mask_pos != 0)) {
// there was found a junction
const uint8_t active_element = get_first_id_tree_el_position();
id_tree[active_element].id_position = id_bit_position;
id_tree[active_element].device_selected = get_first_bit_set_position(device_mask);
id_bit_position++;
id_tree[active_element].got_one = build_id_tree(id_bit_position, mask_pos);
id_tree[active_element].got_zero = build_id_tree(id_bit_position, mask_neg);
return active_element;
}
id_bit_position++;
}
// gone through the address, store this result
uint8_t active_element = get_first_id_tree_el_position();
id_tree[active_element].id_position = 128;
id_tree[active_element].device_selected = get_first_bit_set_position(device_mask);
id_tree[active_element].got_one = 255;
id_tree[active_element].got_zero = 255;
return active_element;
}
uint8_t OneWireGpioSlave::get_first_bit_set_position(uint32_t mask) const {
uint32_t _mask = mask;
for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
if((_mask & 1) != 0) return i;
_mask >>= 1;
}
return 0;
}
uint8_t OneWireGpioSlave::get_first_id_tree_el_position(void) const {
for(uint8_t i = 0; i < ONE_WIRE_MAX_DEVICES; ++i) {
if(id_tree[i].id_position == 255) return i;
}
return 0;
}
void OneWireGpioSlave::cmd_search_rom(void) {
uint8_t id_bit_position = 0;
uint8_t trigger_position = 0;
uint8_t active_slave = id_tree[trigger_position].device_selected;
uint8_t trigger_bit = id_tree[trigger_position].id_position;
while(id_bit_position < 64) {
// if junction is reached, act different
if(id_bit_position == trigger_bit) {
if(!send_bit(false)) return;
if(!send_bit(false)) return;
const bool bit_recv = receive_bit();
if(error != OneWireGpioSlaveError::NO_ERROR) return;
// switch to next junction
trigger_position = bit_recv ? id_tree[trigger_position].got_one :
id_tree[trigger_position].got_zero;
active_slave = id_tree[trigger_position].device_selected;
trigger_bit = (trigger_position == 255) ? uint8_t(255) :
id_tree[trigger_position].id_position;
} else {
const uint8_t pos_byte = (id_bit_position >> 3);
const uint8_t mask_bit = (static_cast<uint8_t>(1) << (id_bit_position & (7)));
bool bit_send;
if((devices[active_slave]->id_storage[pos_byte] & mask_bit) != 0) {
bit_send = true;
if(!send_bit(true)) return;
if(!send_bit(false)) return;
} else {
bit_send = false;
if(!send_bit(false)) return;
if(!send_bit(true)) return;
}
const bool bit_recv = receive_bit();
if(error != OneWireGpioSlaveError::NO_ERROR) return;
if(bit_send != bit_recv) return;
}
id_bit_position++;
}
device_selected = devices[active_slave];
}
bool OneWireGpioSlave::check_reset(void) {
pin_set_float();
if(error == OneWireGpioSlaveError::RESET_IN_PROGRESS) {
error = OneWireGpioSlaveError::NO_ERROR;
if(wait_while_gpio_is(
OWET::RESET_MIN[overdrive_mode] - OWET::SLOT_MAX[overdrive_mode] -
OWET::READ_MAX[overdrive_mode],
false) == 0) {
// we want to show_presence on high, so wait for it
const OneWiteTimeType time_remaining = wait_while_gpio_is(OWET::RESET_MAX[0], false);
if(overdrive_mode &&
((OWET::RESET_MAX[0] - OWET::RESET_MIN[overdrive_mode]) > time_remaining)) {
overdrive_mode = false;
};
return true;
}
}
// if line is low, then just leave
if(gpio_read(gpio) == 0) return false;
// wait while gpio is high
if(wait_while_gpio_is(OWET::RESET_TIMEOUT, true) == 0) {
return false;
}
// store low time
const OneWiteTimeType time_remaining = wait_while_gpio_is(OWET::RESET_MAX[0], false);
// low time more than RESET_MAX time
if(time_remaining == 0) {
error = OneWireGpioSlaveError::VERY_LONG_RESET;
return false;
}
// if time, while bus was low, fit in standart reset timings
if(overdrive_mode && ((OWET::RESET_MAX[0] - OWET::RESET_MIN[0]) <= time_remaining)) {
// normal reset detected
overdrive_mode = false;
};
bool result = (time_remaining <= OWET::RESET_MAX[0]) &&
time_remaining >= OWET::RESET_MIN[overdrive_mode];
return result;
}
bool OneWireGpioSlave::show_presence(void) {
// wait while master delay presence check
wait_while_gpio_is(OWET::PRESENCE_TIMEOUT, true);
// show presence
pin_set_low();
delay_us(OWET::PRESENCE_MIN[overdrive_mode]);
pin_set_float();
// somebody also can show presence
const OneWiteTimeType wait_low_time =
OWET::PRESENCE_MAX[overdrive_mode] - OWET::PRESENCE_MIN[overdrive_mode];
// so we will wait
if(wait_while_gpio_is(wait_low_time, false) == 0) {
error = OneWireGpioSlaveError::PRESENCE_LOW_ON_LINE;
return false;
}
return true;
}
bool OneWireGpioSlave::receive_and_process_cmd(void) {
receive(&cmd);
if(error == OneWireGpioSlaveError::RESET_IN_PROGRESS) return true;
if(error != OneWireGpioSlaveError::NO_ERROR) return false;
switch(cmd) {
case 0xF0:
// SEARCH ROM
device_selected = nullptr;
cmd_search_rom();
// trigger reinit
return true;
case 0x33:
// READ ROM
// work only when one slave on the bus
if((device_selected == nullptr) && (devices_count == 1)) {
device_selected = devices[get_next_device_index()];
}
if(device_selected != nullptr) {
device_selected->send_id(this);
}
return false;
default: // Unknown command
error = OneWireGpioSlaveError::INCORRECT_ONEWIRE_CMD;
//error_cmd = cmd;
}
if(error == OneWireGpioSlaveError::RESET_IN_PROGRESS) return true;
return (error == OneWireGpioSlaveError::NO_ERROR);
}
bool OneWireGpioSlave::receive_bit(void) {
// wait while bus is low
OneWiteTimeType time = OWET::SLOT_MAX[overdrive_mode];
time = wait_while_gpio_is(time, false);
if(time == 0) {
error = OneWireGpioSlaveError::RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWET::MSG_HIGH_TIMEOUT;
time = wait_while_gpio_is(time, true);
if(time == 0) {
error = OneWireGpioSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
error_place = 1;
return false;
}
// wait a time of zero
time = OWET::READ_MIN[overdrive_mode];
time = wait_while_gpio_is(time, false);
return (time > 0);
}
bool OneWireGpioSlave::send_bit(bool value) {
const bool write_zero = !value;
// wait while bus is low
OneWiteTimeType time = OWET::SLOT_MAX[overdrive_mode];
time = wait_while_gpio_is(time, false);
if(time == 0) {
error = OneWireGpioSlaveError::RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWET::MSG_HIGH_TIMEOUT;
time = wait_while_gpio_is(time, true);
if(time == 0) {
error = OneWireGpioSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
error_place = 2;
return false;
}
// choose write time
if(write_zero) {
pin_set_low();
time = OWET::WRITE_ZERO[overdrive_mode];
} else {
time = OWET::READ_MAX[overdrive_mode];
}
// hold line for ZERO or ONE time
delay_us(time);
pin_set_float();
return true;
}
bool OneWireGpioSlave::send(const uint8_t* address, const uint8_t data_length) {
uint8_t bytes_sent = 0;
pin_set_float();
// bytes loop
for(; bytes_sent < data_length; ++bytes_sent) {
const uint8_t data_byte = address[bytes_sent];
// bit loop
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(!send_bit(static_cast<bool>(bit_mask & data_byte))) {
// if we cannot send first bit
if((bit_mask == 0x01) &&
(error == OneWireGpioSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH))
error = OneWireGpioSlaveError::FIRST_BIT_OF_BYTE_TIMEOUT;
return false;
}
}
}
return true;
}
bool OneWireGpioSlave::receive(uint8_t* data, const uint8_t data_length) {
uint8_t bytes_received = 0;
pin_set_float();
for(; bytes_received < data_length; ++bytes_received) {
uint8_t value = 0;
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(receive_bit()) value |= bit_mask;
}
data[bytes_received] = value;
}
return (bytes_received != data_length);
}

View File

@@ -0,0 +1,93 @@
#pragma once
#include "flipper.h"
#include "flipper_v2.h"
#include "one_wire_timings.h"
// TODO fix GPL compability
// currently we use rework of OneWireHub
#define ONE_WIRE_MAX_DEVICES 1
#define ONE_WIRE_TREE_SIZE ((2 * ONE_WIRE_MAX_DEVICES) - 1)
#define OWET OneWireEmulateTiming
class OneWireDevice;
enum class OneWireGpioSlaveError : uint8_t {
NO_ERROR = 0,
READ_TIMESLOT_TIMEOUT = 1,
WRITE_TIMESLOT_TIMEOUT = 2,
WAIT_RESET_TIMEOUT = 3,
VERY_LONG_RESET = 4,
VERY_SHORT_RESET = 5,
PRESENCE_LOW_ON_LINE = 6,
READ_TIMESLOT_TIMEOUT_LOW = 7,
AWAIT_TIMESLOT_TIMEOUT_HIGH = 8,
PRESENCE_HIGH_ON_LINE = 9,
INCORRECT_ONEWIRE_CMD = 10,
INCORRECT_SLAVE_USAGE = 11,
TRIED_INCORRECT_WRITE = 12,
FIRST_TIMESLOT_TIMEOUT = 13,
FIRST_BIT_OF_BYTE_TIMEOUT = 14,
RESET_IN_PROGRESS = 15
};
class OneWireGpioSlave {
private:
const GpioPin* gpio;
bool overdrive_mode = false;
uint8_t cmd;
OneWireGpioSlaveError error;
uint8_t error_place;
uint8_t devices_count;
OneWireDevice* devices[ONE_WIRE_MAX_DEVICES];
OneWireDevice* device_selected;
struct IDTree {
uint8_t device_selected; // for which slave is this jump-command relevant
uint8_t id_position; // where does the algorithm has to look for a junction
uint8_t got_zero; // if 0 switch to which tree branch
uint8_t got_one; // if 1 switch to which tree branch
} id_tree[ONE_WIRE_TREE_SIZE];
public:
OneWireGpioSlave(const GpioPin* one_wire_gpio);
~OneWireGpioSlave();
void start(void);
void stop(void);
bool emulate();
bool check_reset(void);
bool show_presence(void);
bool receive_and_process_cmd(void);
bool receive(uint8_t* data, const uint8_t data_length = 1);
bool receive_bit(void);
bool send_bit(bool value);
bool send(const uint8_t* address, const uint8_t data_length = 1);
OneWiteTimeType wait_while_gpio_is(volatile OneWiteTimeType retries, const bool pin_value);
// set pin state
inline void pin_set_float();
inline void pin_set_low();
// get error text
const char* decode_error();
// devices managment
uint8_t attach(OneWireDevice& device);
bool detach(const OneWireDevice& device);
bool detach(uint8_t device_number);
uint8_t get_next_device_index(const uint8_t index_start = 0) const;
// id tree managment
uint8_t build_id_tree(void);
uint8_t build_id_tree(uint8_t id_bit_position, uint32_t device_mask);
uint8_t get_first_bit_set_position(uint32_t mask) const;
uint8_t get_first_id_tree_el_position(void) const;
// commands
void cmd_search_rom(void);
};

View File

@@ -0,0 +1,17 @@
#include "one_wire_timings.h"
// fix pre C++17 "undefined reference" errors
constexpr const OneWiteTimeType OneWireEmulateTiming::RESET_TIMEOUT;
constexpr const OneWiteTimeType OneWireEmulateTiming::RESET_MIN[2];
constexpr const OneWiteTimeType OneWireEmulateTiming::RESET_MAX[2];
constexpr const OneWiteTimeType OneWireEmulateTiming::PRESENCE_TIMEOUT;
constexpr const OneWiteTimeType OneWireEmulateTiming::PRESENCE_MIN[2];
constexpr const OneWiteTimeType OneWireEmulateTiming::PRESENCE_MAX[2];
constexpr const OneWiteTimeType OneWireEmulateTiming::MSG_HIGH_TIMEOUT;
constexpr const OneWiteTimeType OneWireEmulateTiming::SLOT_MAX[2];
constexpr const OneWiteTimeType OneWireEmulateTiming::READ_MIN[2];
constexpr const OneWiteTimeType OneWireEmulateTiming::READ_MAX[2];
constexpr const OneWiteTimeType OneWireEmulateTiming::WRITE_ZERO[2];

View File

@@ -0,0 +1,54 @@
#pragma once
#include <stdint.h>
class __OneWireTiming {
public:
constexpr static const uint16_t TIMING_A = 6;
constexpr static const uint16_t TIMING_B = 64;
constexpr static const uint16_t TIMING_C = 60;
constexpr static const uint16_t TIMING_D = 10;
constexpr static const uint16_t TIMING_E = 9;
constexpr static const uint16_t TIMING_F = 55;
constexpr static const uint16_t TIMING_G = 0;
constexpr static const uint16_t TIMING_H = 480;
constexpr static const uint16_t TIMING_I = 70;
constexpr static const uint16_t TIMING_J = 410;
};
class OneWireTiming {
public:
constexpr static const uint16_t WRITE_1_DRIVE = __OneWireTiming::TIMING_A;
constexpr static const uint16_t WRITE_1_RELEASE = __OneWireTiming::TIMING_B;
constexpr static const uint16_t WRITE_0_DRIVE = __OneWireTiming::TIMING_C;
constexpr static const uint16_t WRITE_0_RELEASE = __OneWireTiming::TIMING_D;
constexpr static const uint16_t READ_DRIVE = __OneWireTiming::TIMING_A;
constexpr static const uint16_t READ_RELEASE = __OneWireTiming::TIMING_E;
constexpr static const uint16_t READ_DELAY_POST = __OneWireTiming::TIMING_F;
constexpr static const uint16_t RESET_DELAY_PRE = __OneWireTiming::TIMING_G;
constexpr static const uint16_t RESET_DRIVE = __OneWireTiming::TIMING_H;
constexpr static const uint16_t RESET_RELEASE = __OneWireTiming::TIMING_I;
constexpr static const uint16_t RESET_DELAY_POST = __OneWireTiming::TIMING_J;
};
typedef uint32_t OneWiteTimeType;
class OneWireEmulateTiming {
public:
constexpr static const OneWiteTimeType RESET_TIMEOUT = {5000};
constexpr static const OneWiteTimeType RESET_MIN[2] = {430, 48};
constexpr static const OneWiteTimeType RESET_MAX[2] = {960, 80};
constexpr static const OneWiteTimeType PRESENCE_TIMEOUT = {20};
constexpr static const OneWiteTimeType PRESENCE_MIN[2] = {160, 8};
constexpr static const OneWiteTimeType PRESENCE_MAX[2] = {480, 32};
constexpr static const OneWiteTimeType MSG_HIGH_TIMEOUT = {15000};
constexpr static const OneWiteTimeType SLOT_MAX[2] = {135, 30};
constexpr static const OneWiteTimeType READ_MIN[2] = {20, 4};
constexpr static const OneWiteTimeType READ_MAX[2] = {60, 10};
constexpr static const OneWiteTimeType WRITE_ZERO[2] = {30, 8};
};