[FL-3179] 1-Wire Overdrive Mode (#2522)
* Separate ibutton to its own module, add one_wire to f18 * Move onewire cli to a separate app * Add definitions for normal and overdrive timings * Update api definitions * Add rough overdrive timings definition for onewire emulation * Remove one_wire_host_timing.h * Add rough overdrive timings for onewire host * Improve overdrive mode * Working overdrive mode from flipper to flipper * Update thermometer example app * Turn on otg power when running thermometer example app * Implement reset overdrive switching * Always exit out of overdrive mode * Improve overdrive timings * Fix typos * Fix reset behaviour * Use overdrive mode everywhere in DS1996 * Improve comments * Bump API version Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <flipper_format.h>
|
||||
|
||||
#include <one_wire/one_wire_host.h>
|
||||
#include <one_wire/one_wire_slave.h>
|
||||
|
||||
#include <flipper_format/flipper_format.h>
|
||||
|
||||
#define DALLAS_COMMON_MANUFACTURER_NAME "Dallas"
|
||||
|
||||
#define DALLAS_COMMON_CMD_READ_ROM 0x33U
|
||||
|
@@ -2,11 +2,11 @@
|
||||
|
||||
#include "../protocol_common_i.h"
|
||||
|
||||
#include <flipper_format.h>
|
||||
|
||||
#include <one_wire/one_wire_host.h>
|
||||
#include <one_wire/one_wire_slave.h>
|
||||
|
||||
#include <flipper_format/flipper_format.h>
|
||||
|
||||
typedef bool (*iButtonProtocolDallasReadWriteFunc)(OneWireHost*, iButtonProtocolData*);
|
||||
typedef void (*iButtonProtocolDallasEmulateFunc)(OneWireSlave*, iButtonProtocolData*);
|
||||
typedef bool (*iButtonProtocolDallasSaveFunc)(FlipperFormat*, const iButtonProtocolData*);
|
||||
|
@@ -53,7 +53,7 @@ const iButtonProtocolDallasBase ibutton_protocol_ds1971 = {
|
||||
.name = DS1971_FAMILY_NAME,
|
||||
|
||||
.read = dallas_ds1971_read,
|
||||
.write_blank = NULL, /* No data to write a blank */
|
||||
.write_blank = NULL, // TODO: Implement writing to blank
|
||||
.write_copy = dallas_ds1971_write_copy,
|
||||
.emulate = dallas_ds1971_emulate,
|
||||
.save = dallas_ds1971_save,
|
||||
@@ -76,7 +76,7 @@ bool dallas_ds1971_write_copy(OneWireHost* host, iButtonProtocolData* protocol_d
|
||||
DS1971ProtocolData* data = protocol_data;
|
||||
|
||||
onewire_host_reset(host);
|
||||
onewire_host_skip(host);
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
|
||||
// Starting writing from address 0x0000
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_WRITE_SCRATCH);
|
||||
onewire_host_write(host, 0x00);
|
||||
@@ -87,7 +87,7 @@ bool dallas_ds1971_write_copy(OneWireHost* host, iButtonProtocolData* protocol_d
|
||||
bool pad_valid = false;
|
||||
if(onewire_host_reset(host)) {
|
||||
pad_valid = true;
|
||||
onewire_host_skip(host);
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_READ_SCRATCH);
|
||||
onewire_host_write(host, 0x00);
|
||||
|
||||
@@ -103,7 +103,7 @@ bool dallas_ds1971_write_copy(OneWireHost* host, iButtonProtocolData* protocol_d
|
||||
// Copy scratchpad to memory and confirm
|
||||
if(pad_valid) {
|
||||
if(onewire_host_reset(host)) {
|
||||
onewire_host_skip(host);
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_SKIP_ROM);
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_COPY_SCRATCH);
|
||||
onewire_host_write(host, DS1971_CMD_FINALIZATION);
|
||||
|
||||
@@ -114,10 +114,16 @@ bool dallas_ds1971_write_copy(OneWireHost* host, iButtonProtocolData* protocol_d
|
||||
return pad_valid;
|
||||
}
|
||||
|
||||
static void dallas_ds1971_reset_callback(void* context) {
|
||||
static bool dallas_ds1971_reset_callback(bool is_short, void* context) {
|
||||
furi_assert(context);
|
||||
DS1971ProtocolData* data = context;
|
||||
data->state.command_state = DallasCommonCommandStateIdle;
|
||||
|
||||
if(!is_short) {
|
||||
data->state.command_state = DallasCommonCommandStateIdle;
|
||||
onewire_slave_set_overdrive(data->state.bus, is_short);
|
||||
}
|
||||
|
||||
return !is_short;
|
||||
}
|
||||
|
||||
static bool dallas_ds1971_command_callback(uint8_t command, void* context) {
|
||||
|
@@ -67,6 +67,14 @@ bool dallas_ds1990_write_blank(OneWireHost* host, iButtonProtocolData* protocol_
|
||||
tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
|
||||
}
|
||||
|
||||
static bool dallas_ds1990_reset_callback(bool is_short, void* context) {
|
||||
DS1990ProtocolData* data = context;
|
||||
if(!is_short) {
|
||||
onewire_slave_set_overdrive(data->state.bus, is_short);
|
||||
}
|
||||
return !is_short;
|
||||
}
|
||||
|
||||
static bool dallas_ds1990_command_callback(uint8_t command, void* context) {
|
||||
furi_assert(context);
|
||||
DS1990ProtocolData* data = context;
|
||||
@@ -92,7 +100,7 @@ void dallas_ds1990_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data
|
||||
DS1990ProtocolData* data = protocol_data;
|
||||
data->state.bus = bus;
|
||||
|
||||
onewire_slave_set_reset_callback(bus, NULL, NULL);
|
||||
onewire_slave_set_reset_callback(bus, dallas_ds1990_reset_callback, protocol_data);
|
||||
onewire_slave_set_command_callback(bus, dallas_ds1990_command_callback, protocol_data);
|
||||
}
|
||||
|
||||
|
@@ -87,10 +87,16 @@ bool dallas_ds1992_write_copy(OneWireHost* host, iButtonProtocolData* protocol_d
|
||||
DS1992_SRAM_DATA_SIZE);
|
||||
}
|
||||
|
||||
static void dallas_ds1992_reset_callback(void* context) {
|
||||
static bool dallas_ds1992_reset_callback(bool is_short, void* context) {
|
||||
furi_assert(context);
|
||||
DS1992ProtocolData* data = context;
|
||||
data->state.command_state = DallasCommonCommandStateIdle;
|
||||
|
||||
if(!is_short) {
|
||||
data->state.command_state = DallasCommonCommandStateIdle;
|
||||
onewire_slave_set_overdrive(data->state.bus, is_short);
|
||||
}
|
||||
|
||||
return !is_short;
|
||||
}
|
||||
|
||||
static bool dallas_ds1992_command_callback(uint8_t command, void* context) {
|
||||
|
@@ -63,24 +63,54 @@ const iButtonProtocolDallasBase ibutton_protocol_ds1996 = {
|
||||
|
||||
bool dallas_ds1996_read(OneWireHost* host, iButtonProtocolData* protocol_data) {
|
||||
DS1996ProtocolData* data = protocol_data;
|
||||
return onewire_host_reset(host) && dallas_common_read_rom(host, &data->rom_data) &&
|
||||
dallas_common_read_mem(host, 0, data->sram_data, DS1996_SRAM_DATA_SIZE);
|
||||
bool success = false;
|
||||
|
||||
do {
|
||||
if(!onewire_host_reset(host)) break;
|
||||
if(!dallas_common_read_rom(host, &data->rom_data)) break;
|
||||
if(!onewire_host_reset(host)) break;
|
||||
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_OVERDRIVE_SKIP_ROM);
|
||||
onewire_host_set_overdrive(host, true);
|
||||
|
||||
if(!dallas_common_read_mem(host, 0, data->sram_data, DS1996_SRAM_DATA_SIZE)) break;
|
||||
success = true;
|
||||
} while(false);
|
||||
|
||||
onewire_host_set_overdrive(host, false);
|
||||
return success;
|
||||
}
|
||||
|
||||
bool dallas_ds1996_write_copy(OneWireHost* host, iButtonProtocolData* protocol_data) {
|
||||
DS1996ProtocolData* data = protocol_data;
|
||||
return dallas_common_write_mem(
|
||||
host,
|
||||
DS1996_COPY_SCRATCH_TIMEOUT_US,
|
||||
DS1996_SRAM_PAGE_SIZE,
|
||||
data->sram_data,
|
||||
DS1996_SRAM_DATA_SIZE);
|
||||
bool success = false;
|
||||
|
||||
do {
|
||||
if(!onewire_host_reset(host)) break;
|
||||
|
||||
onewire_host_write(host, DALLAS_COMMON_CMD_OVERDRIVE_SKIP_ROM);
|
||||
onewire_host_set_overdrive(host, true);
|
||||
|
||||
if(!dallas_common_write_mem(
|
||||
host,
|
||||
DS1996_COPY_SCRATCH_TIMEOUT_US,
|
||||
DS1996_SRAM_PAGE_SIZE,
|
||||
data->sram_data,
|
||||
DS1996_SRAM_DATA_SIZE))
|
||||
break;
|
||||
success = true;
|
||||
} while(false);
|
||||
|
||||
onewire_host_set_overdrive(host, false);
|
||||
return success;
|
||||
}
|
||||
|
||||
static void dallas_ds1996_reset_callback(void* context) {
|
||||
static bool dallas_ds1996_reset_callback(bool is_short, void* context) {
|
||||
furi_assert(context);
|
||||
DS1996ProtocolData* data = context;
|
||||
data->state.command_state = DallasCommonCommandStateIdle;
|
||||
onewire_slave_set_overdrive(data->state.bus, is_short);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool dallas_ds1996_command_callback(uint8_t command, void* context) {
|
||||
@@ -96,8 +126,7 @@ static bool dallas_ds1996_command_callback(uint8_t command, void* context) {
|
||||
|
||||
} else if(data->state.command_state == DallasCommonCommandStateRomCmd) {
|
||||
data->state.command_state = DallasCommonCommandStateMemCmd;
|
||||
dallas_common_emulate_read_mem(bus, data->sram_data, DS1996_SRAM_DATA_SIZE);
|
||||
return false;
|
||||
return dallas_common_emulate_read_mem(bus, data->sram_data, DS1996_SRAM_DATA_SIZE);
|
||||
|
||||
} else {
|
||||
return false;
|
||||
@@ -120,8 +149,17 @@ static bool dallas_ds1996_command_callback(uint8_t command, void* context) {
|
||||
}
|
||||
|
||||
case DALLAS_COMMON_CMD_OVERDRIVE_SKIP_ROM:
|
||||
if(data->state.command_state == DallasCommonCommandStateIdle) {
|
||||
data->state.command_state = DallasCommonCommandStateRomCmd;
|
||||
onewire_slave_set_overdrive(bus, true);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
case DALLAS_COMMON_CMD_MATCH_ROM:
|
||||
case DALLAS_COMMON_CMD_OVERDRIVE_MATCH_ROM:
|
||||
/* TODO: Overdrive mode support */
|
||||
/* TODO: Match ROM command support */
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
@@ -61,6 +61,14 @@ bool ds_generic_write_blank(OneWireHost* host, iButtonProtocolData* protocol_dat
|
||||
return tm2004_write(host, data->rom_data.bytes, sizeof(DallasCommonRomData));
|
||||
}
|
||||
|
||||
static bool ds_generic_reset_callback(bool is_short, void* context) {
|
||||
DallasGenericProtocolData* data = context;
|
||||
if(!is_short) {
|
||||
onewire_slave_set_overdrive(data->state.bus, is_short);
|
||||
}
|
||||
return !is_short;
|
||||
}
|
||||
|
||||
static bool ds_generic_command_callback(uint8_t command, void* context) {
|
||||
furi_assert(context);
|
||||
DallasGenericProtocolData* data = context;
|
||||
@@ -85,7 +93,7 @@ void ds_generic_emulate(OneWireSlave* bus, iButtonProtocolData* protocol_data) {
|
||||
DallasGenericProtocolData* data = protocol_data;
|
||||
data->state.bus = bus;
|
||||
|
||||
onewire_slave_set_reset_callback(bus, NULL, NULL);
|
||||
onewire_slave_set_reset_callback(bus, ds_generic_reset_callback, NULL);
|
||||
onewire_slave_set_command_callback(bus, ds_generic_command_callback, protocol_data);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user