[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:
Georgii Surkov 2023-03-22 17:54:06 +03:00 committed by GitHub
parent 1f236ede0e
commit 8b224ecb15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 397 additions and 227 deletions

View File

@ -19,9 +19,12 @@
#include <one_wire/maxim_crc.h>
#include <one_wire/one_wire_host.h>
#include <furi_hal_power.h>
#define UPDATE_PERIOD_MS 1000UL
#define TEXT_STORE_SIZE 64U
#define DS18B20_CMD_SKIP_ROM 0xccU
#define DS18B20_CMD_CONVERT 0x44U
#define DS18B20_CMD_READ_SCRATCHPAD 0xbeU
@ -92,7 +95,7 @@ static void example_thermo_request_temperature(ExampleThermoContext* context) {
/* After the reset, a ROM operation must follow.
If there is only one device connected, the "Skip ROM" command is most appropriate
(it can also be used to address all of the connected devices in some cases).*/
onewire_host_skip(onewire);
onewire_host_write(onewire, DS18B20_CMD_SKIP_ROM);
/* After the ROM operation, a device-specific command is issued.
In this case, it's a request to start measuring the temperature. */
onewire_host_write(onewire, DS18B20_CMD_CONVERT);
@ -133,7 +136,7 @@ static void example_thermo_read_temperature(ExampleThermoContext* context) {
/* After the reset, a ROM operation must follow.
If there is only one device connected, the "Skip ROM" command is most appropriate
(it can also be used to address all of the connected devices in some cases).*/
onewire_host_skip(onewire);
onewire_host_write(onewire, DS18B20_CMD_SKIP_ROM);
/* After the ROM operation, a device-specific command is issued.
This time, it will be the "Read Scratchpad" command which will
@ -267,6 +270,9 @@ static void example_thermo_input_callback(InputEvent* event, void* ctx) {
/* Starts the reader thread and handles the input */
static void example_thermo_run(ExampleThermoContext* context) {
/* Enable power on external pins */
furi_hal_power_enable_otg();
/* Configure the hardware in host mode */
onewire_host_start(context->onewire);
@ -299,6 +305,9 @@ static void example_thermo_run(ExampleThermoContext* context) {
/* Reset the hardware */
onewire_host_stop(context->onewire);
/* Disable power on external pins */
furi_hal_power_disable_otg();
}
/******************** Initialisation & startup *****************************/

View File

@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,19.0,,
Version,+,20.0,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
@ -153,7 +153,6 @@ Header,+,lib/mlib/m-tuple.h,,
Header,+,lib/mlib/m-variant.h,,
Header,+,lib/one_wire/maxim_crc.h,,
Header,+,lib/one_wire/one_wire_host.h,,
Header,+,lib/one_wire/one_wire_host_timing.h,,
Header,+,lib/one_wire/one_wire_slave.h,,
Header,+,lib/print/wrappers.h,,
Header,+,lib/toolbox/args.h,,
@ -1481,8 +1480,8 @@ Function,+,onewire_host_read_bit,_Bool,OneWireHost*
Function,+,onewire_host_read_bytes,void,"OneWireHost*, uint8_t*, uint16_t"
Function,+,onewire_host_reset,_Bool,OneWireHost*
Function,+,onewire_host_reset_search,void,OneWireHost*
Function,+,onewire_host_search,uint8_t,"OneWireHost*, uint8_t*, OneWireHostSearchMode"
Function,+,onewire_host_skip,void,OneWireHost*
Function,+,onewire_host_search,_Bool,"OneWireHost*, uint8_t*, OneWireHostSearchMode"
Function,+,onewire_host_set_overdrive,void,"OneWireHost*, _Bool"
Function,+,onewire_host_start,void,OneWireHost*
Function,+,onewire_host_stop,void,OneWireHost*
Function,+,onewire_host_target_search,void,"OneWireHost*, uint8_t"
@ -1496,6 +1495,7 @@ Function,+,onewire_slave_receive_bit,_Bool,OneWireSlave*
Function,+,onewire_slave_send,_Bool,"OneWireSlave*, const uint8_t*, size_t"
Function,+,onewire_slave_send_bit,_Bool,"OneWireSlave*, _Bool"
Function,+,onewire_slave_set_command_callback,void,"OneWireSlave*, OneWireSlaveCommandCallback, void*"
Function,+,onewire_slave_set_overdrive,void,"OneWireSlave*, _Bool"
Function,+,onewire_slave_set_reset_callback,void,"OneWireSlave*, OneWireSlaveResetCallback, void*"
Function,+,onewire_slave_set_result_callback,void,"OneWireSlave*, OneWireSlaveResultCallback, void*"
Function,+,onewire_slave_start,void,OneWireSlave*

1 entry status name type params
2 Version + 19.0 20.0
3 Header + applications/services/bt/bt_service/bt.h
4 Header + applications/services/cli/cli.h
5 Header + applications/services/cli/cli_vcp.h
153 Header + lib/mlib/m-variant.h
154 Header + lib/one_wire/maxim_crc.h
155 Header + lib/one_wire/one_wire_host.h
Header + lib/one_wire/one_wire_host_timing.h
156 Header + lib/one_wire/one_wire_slave.h
157 Header + lib/print/wrappers.h
158 Header + lib/toolbox/args.h
1480 Function + onewire_host_read_bytes void OneWireHost*, uint8_t*, uint16_t
1481 Function + onewire_host_reset _Bool OneWireHost*
1482 Function + onewire_host_reset_search void OneWireHost*
1483 Function + onewire_host_search uint8_t _Bool OneWireHost*, uint8_t*, OneWireHostSearchMode
1484 Function + onewire_host_skip onewire_host_set_overdrive void OneWireHost* OneWireHost*, _Bool
1485 Function + onewire_host_start void OneWireHost*
1486 Function + onewire_host_stop void OneWireHost*
1487 Function + onewire_host_target_search void OneWireHost*, uint8_t
1495 Function + onewire_slave_send _Bool OneWireSlave*, const uint8_t*, size_t
1496 Function + onewire_slave_send_bit _Bool OneWireSlave*, _Bool
1497 Function + onewire_slave_set_command_callback void OneWireSlave*, OneWireSlaveCommandCallback, void*
1498 Function + onewire_slave_set_overdrive void OneWireSlave*, _Bool
1499 Function + onewire_slave_set_reset_callback void OneWireSlave*, OneWireSlaveResetCallback, void*
1500 Function + onewire_slave_set_result_callback void OneWireSlave*, OneWireSlaveResultCallback, void*
1501 Function + onewire_slave_start void OneWireSlave*

View File

@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,19.0,,
Version,+,20.0,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
@ -172,7 +172,6 @@ Header,+,lib/mlib/m-variant.h,,
Header,+,lib/nfc/nfc_device.h,,
Header,+,lib/one_wire/maxim_crc.h,,
Header,+,lib/one_wire/one_wire_host.h,,
Header,+,lib/one_wire/one_wire_host_timing.h,,
Header,+,lib/one_wire/one_wire_slave.h,,
Header,+,lib/print/wrappers.h,,
Header,+,lib/subghz/blocks/const.h,,
@ -2062,8 +2061,8 @@ Function,+,onewire_host_read_bit,_Bool,OneWireHost*
Function,+,onewire_host_read_bytes,void,"OneWireHost*, uint8_t*, uint16_t"
Function,+,onewire_host_reset,_Bool,OneWireHost*
Function,+,onewire_host_reset_search,void,OneWireHost*
Function,+,onewire_host_search,uint8_t,"OneWireHost*, uint8_t*, OneWireHostSearchMode"
Function,+,onewire_host_skip,void,OneWireHost*
Function,+,onewire_host_search,_Bool,"OneWireHost*, uint8_t*, OneWireHostSearchMode"
Function,+,onewire_host_set_overdrive,void,"OneWireHost*, _Bool"
Function,+,onewire_host_start,void,OneWireHost*
Function,+,onewire_host_stop,void,OneWireHost*
Function,+,onewire_host_target_search,void,"OneWireHost*, uint8_t"
@ -2077,6 +2076,7 @@ Function,+,onewire_slave_receive_bit,_Bool,OneWireSlave*
Function,+,onewire_slave_send,_Bool,"OneWireSlave*, const uint8_t*, size_t"
Function,+,onewire_slave_send_bit,_Bool,"OneWireSlave*, _Bool"
Function,+,onewire_slave_set_command_callback,void,"OneWireSlave*, OneWireSlaveCommandCallback, void*"
Function,+,onewire_slave_set_overdrive,void,"OneWireSlave*, _Bool"
Function,+,onewire_slave_set_reset_callback,void,"OneWireSlave*, OneWireSlaveResetCallback, void*"
Function,+,onewire_slave_set_result_callback,void,"OneWireSlave*, OneWireSlaveResultCallback, void*"
Function,+,onewire_slave_start,void,OneWireSlave*

1 entry status name type params
2 Version + 19.0 20.0
3 Header + applications/services/bt/bt_service/bt.h
4 Header + applications/services/cli/cli.h
5 Header + applications/services/cli/cli_vcp.h
172 Header + lib/nfc/nfc_device.h
173 Header + lib/one_wire/maxim_crc.h
174 Header + lib/one_wire/one_wire_host.h
Header + lib/one_wire/one_wire_host_timing.h
175 Header + lib/one_wire/one_wire_slave.h
176 Header + lib/print/wrappers.h
177 Header + lib/subghz/blocks/const.h
2061 Function + onewire_host_read_bytes void OneWireHost*, uint8_t*, uint16_t
2062 Function + onewire_host_reset _Bool OneWireHost*
2063 Function + onewire_host_reset_search void OneWireHost*
2064 Function + onewire_host_search uint8_t _Bool OneWireHost*, uint8_t*, OneWireHostSearchMode
2065 Function + onewire_host_skip onewire_host_set_overdrive void OneWireHost* OneWireHost*, _Bool
2066 Function + onewire_host_start void OneWireHost*
2067 Function + onewire_host_stop void OneWireHost*
2068 Function + onewire_host_target_search void OneWireHost*, uint8_t
2076 Function + onewire_slave_send _Bool OneWireSlave*, const uint8_t*, size_t
2077 Function + onewire_slave_send_bit _Bool OneWireSlave*, _Bool
2078 Function + onewire_slave_set_command_callback void OneWireSlave*, OneWireSlaveCommandCallback, void*
2079 Function + onewire_slave_set_overdrive void OneWireSlave*, _Bool
2080 Function + onewire_slave_set_reset_callback void OneWireSlave*, OneWireSlaveResetCallback, void*
2081 Function + onewire_slave_set_result_callback void OneWireSlave*, OneWireSlaveResultCallback, void*
2082 Function + onewire_slave_start void OneWireSlave*

View File

@ -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

View File

@ -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*);

View File

@ -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) {

View File

@ -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);
}

View File

@ -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) {

View File

@ -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;
}

View File

@ -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);
}

View File

@ -8,7 +8,6 @@ env.Append(
"#/lib/one_wire",
],
SDK_HEADERS=[
File("one_wire_host_timing.h"),
File("one_wire_host.h"),
File("one_wire_slave.h"),
File("maxim_crc.h"),

View File

@ -1,10 +1,54 @@
#include <furi.h>
/**
* Timings based on Application Note 126:
* https://www.analog.com/media/en/technical-documentation/tech-articles/1wire-communication-through-software--maxim-integrated.pdf
*/
#include "one_wire_host.h"
#include "one_wire_host_timing.h"
typedef struct {
uint16_t a;
uint16_t b;
uint16_t c;
uint16_t d;
uint16_t e;
uint16_t f;
uint16_t g;
uint16_t h;
uint16_t i;
uint16_t j;
} OneWireHostTimings;
static const OneWireHostTimings onewire_host_timings_normal = {
.a = 9,
.b = 64,
.c = 64,
.d = 14,
.e = 9,
.f = 55,
.g = 0,
.h = 480,
.i = 70,
.j = 410,
};
static const OneWireHostTimings onewire_host_timings_overdrive = {
.a = 1,
.b = 8,
.c = 8,
.d = 3,
.e = 1,
.f = 7,
.g = 3,
.h = 70,
.i = 9,
.j = 40,
};
struct OneWireHost {
const GpioPin* gpio_pin;
const OneWireHostTimings* timings;
unsigned char saved_rom[8]; /** < global search state */
uint8_t last_discrepancy;
uint8_t last_family_discrepancy;
@ -15,6 +59,7 @@ OneWireHost* onewire_host_alloc(const GpioPin* gpio_pin) {
OneWireHost* host = malloc(sizeof(OneWireHost));
host->gpio_pin = gpio_pin;
onewire_host_reset_search(host);
onewire_host_set_overdrive(host, false);
return host;
}
@ -27,6 +72,8 @@ bool onewire_host_reset(OneWireHost* host) {
uint8_t r;
uint8_t retries = 125;
const OneWireHostTimings* timings = host->timings;
// wait until the gpio is high
furi_hal_gpio_write(host->gpio_pin, true);
do {
@ -35,19 +82,19 @@ bool onewire_host_reset(OneWireHost* host) {
} while(!furi_hal_gpio_read(host->gpio_pin));
// pre delay
furi_delay_us(OWH_RESET_DELAY_PRE);
furi_delay_us(timings->g);
// drive low
furi_hal_gpio_write(host->gpio_pin, false);
furi_delay_us(OWH_RESET_DRIVE);
furi_delay_us(timings->h);
// release
furi_hal_gpio_write(host->gpio_pin, true);
furi_delay_us(OWH_RESET_RELEASE);
furi_delay_us(timings->i);
// read and post delay
r = !furi_hal_gpio_read(host->gpio_pin);
furi_delay_us(OWH_RESET_DELAY_POST);
furi_delay_us(timings->j);
return r;
}
@ -55,17 +102,19 @@ bool onewire_host_reset(OneWireHost* host) {
bool onewire_host_read_bit(OneWireHost* host) {
bool result;
const OneWireHostTimings* timings = host->timings;
// drive low
furi_hal_gpio_write(host->gpio_pin, false);
furi_delay_us(OWH_READ_DRIVE);
furi_delay_us(timings->a);
// release
furi_hal_gpio_write(host->gpio_pin, true);
furi_delay_us(OWH_READ_RELEASE);
furi_delay_us(timings->e);
// read and post delay
result = furi_hal_gpio_read(host->gpio_pin);
furi_delay_us(OWH_READ_DELAY_POST);
furi_delay_us(timings->f);
return result;
}
@ -89,22 +138,24 @@ void onewire_host_read_bytes(OneWireHost* host, uint8_t* buffer, uint16_t count)
}
void onewire_host_write_bit(OneWireHost* host, bool value) {
const OneWireHostTimings* timings = host->timings;
if(value) {
// drive low
furi_hal_gpio_write(host->gpio_pin, false);
furi_delay_us(OWH_WRITE_1_DRIVE);
furi_delay_us(timings->a);
// release
furi_hal_gpio_write(host->gpio_pin, true);
furi_delay_us(OWH_WRITE_1_RELEASE);
furi_delay_us(timings->b);
} else {
// drive low
furi_hal_gpio_write(host->gpio_pin, false);
furi_delay_us(OWH_WRITE_0_DRIVE);
furi_delay_us(timings->c);
// release
furi_hal_gpio_write(host->gpio_pin, true);
furi_delay_us(OWH_WRITE_0_RELEASE);
furi_delay_us(timings->d);
}
}
@ -122,10 +173,6 @@ void onewire_host_write_bytes(OneWireHost* host, const uint8_t* buffer, uint16_t
}
}
void onewire_host_skip(OneWireHost* host) {
onewire_host_write(host, 0xCC);
}
void onewire_host_start(OneWireHost* host) {
furi_hal_gpio_write(host->gpio_pin, true);
furi_hal_gpio_init(host->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
@ -154,7 +201,7 @@ void onewire_host_target_search(OneWireHost* host, uint8_t family_code) {
host->last_device_flag = false;
}
uint8_t onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSearchMode mode) {
bool onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSearchMode mode) {
uint8_t id_bit_number;
uint8_t last_zero, rom_byte_number, search_result;
uint8_t id_bit, cmp_id_bit;
@ -268,3 +315,7 @@ uint8_t onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSea
return search_result;
}
void onewire_host_set_overdrive(OneWireHost* host, bool set) {
host->timings = set ? &onewire_host_timings_overdrive : &onewire_host_timings_normal;
}

View File

@ -15,114 +15,115 @@ extern "C" {
typedef enum {
OneWireHostSearchModeConditional = 0, /**< Search for alarmed device */
OneWireHostSearchModeNormal = 1, /**< Search all devices */
OneWireHostSearchModeNormal = 1, /**< Search for all devices */
} OneWireHostSearchMode;
typedef struct OneWireHost OneWireHost;
/**
* Allocate onewire host bus
* @param pin
* @return OneWireHost*
* Allocate OneWireHost instance
* @param [in] gpio_pin connection pin
* @return pointer to OneWireHost instance
*/
OneWireHost* onewire_host_alloc(const GpioPin* gpio_pin);
/**
* Deallocate onewire host bus
* @param host
* Destroy OneWireHost instance, free resources
* @param [in] host pointer to OneWireHost instance
*/
void onewire_host_free(OneWireHost* host);
/**
* Reset bus
* @param host
* @return bool
* Reset the 1-Wire bus
* @param [in] host pointer to OneWireHost instance
* @return true if presence was detected, false otherwise
*/
bool onewire_host_reset(OneWireHost* host);
/**
* Read one bit
* @param host
* @return bool
* @param [in] host pointer to OneWireHost instance
* @return received bit value
*/
bool onewire_host_read_bit(OneWireHost* host);
/**
* Read one byte
* @param host
* @return uint8_t
* @param [in] host pointer to OneWireHost instance
* @return received byte value
*/
uint8_t onewire_host_read(OneWireHost* host);
/**
* Read many bytes
* @param host
* @param buffer
* @param count
* Read one or more bytes
* @param [in] host pointer to OneWireHost instance
* @param [out] buffer received data buffer
* @param [in] count number of bytes to read
*/
void onewire_host_read_bytes(OneWireHost* host, uint8_t* buffer, uint16_t count);
/**
* Write one bit
* @param host
* @param value
* @param [in] host pointer to OneWireHost instance
* @param value bit value to write
*/
void onewire_host_write_bit(OneWireHost* host, bool value);
/**
* Write one byte
* @param host
* @param value
* @param [in] host pointer to OneWireHost instance
* @param value byte value to write
*/
void onewire_host_write(OneWireHost* host, uint8_t value);
/**
* Write many bytes
* @param host
* @param buffer
* @param count
* Write one or more bytes
* @param [in] host pointer to OneWireHost instance
* @param [in] buffer pointer to the data to write
* @param [in] count size of the data to write
*/
void onewire_host_write_bytes(OneWireHost* host, const uint8_t* buffer, uint16_t count);
/**
* Skip ROM command
* @param host
*/
void onewire_host_skip(OneWireHost* host);
/**
* Start working with the bus
* @param host
* @param [in] host pointer to OneWireHost instance
*/
void onewire_host_start(OneWireHost* host);
/**
* Stop working with the bus
* @param host
* @param [in] host pointer to OneWireHost instance
*/
void onewire_host_stop(OneWireHost* host);
/**
*
* @param host
* Reset previous search results
* @param [in] host pointer to OneWireHost instance
*/
void onewire_host_reset_search(OneWireHost* host);
/**
*
* @param host
* @param family_code
* Set the family code to search for
* @param [in] host pointer to OneWireHost instance
* @param [in] family_code device family code
*/
void onewire_host_target_search(OneWireHost* host, uint8_t family_code);
/**
*
* @param host
* @param newAddr
* @param mode
* @return uint8_t
* Search for devices on the 1-Wire bus
* @param [in] host pointer to OneWireHost instance
* @param [out] new_addr pointer to the buffer to contain the unique ROM of the found device
* @param [in] mode search mode
* @return true on success, false otherwise
*/
uint8_t onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSearchMode mode);
bool onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSearchMode mode);
/**
* Enable overdrive mode
* @param [in] host pointer to OneWireHost instance
* @param [in] set true to turn overdrive on, false to turn it off
*/
void onewire_host_set_overdrive(OneWireHost* host, bool set);
#ifdef __cplusplus
}

View File

@ -1,30 +0,0 @@
/**
* @file one_wire_host_timing.h
*
* 1-Wire library, timing list
*/
#pragma once
#define OWH_TIMING_A 9
#define OWH_TIMING_B 64
#define OWH_TIMING_C 64
#define OWH_TIMING_D 14
#define OWH_TIMING_E 9
#define OWH_TIMING_F 55
#define OWH_TIMING_G 0
#define OWH_TIMING_H 480
#define OWH_TIMING_I 70
#define OWH_TIMING_J 410
#define OWH_WRITE_1_DRIVE OWH_TIMING_A
#define OWH_WRITE_1_RELEASE OWH_TIMING_B
#define OWH_WRITE_0_DRIVE OWH_TIMING_C
#define OWH_WRITE_0_RELEASE OWH_TIMING_D
#define OWH_READ_DRIVE 3
#define OWH_READ_RELEASE OWH_TIMING_E
#define OWH_READ_DELAY_POST OWH_TIMING_F
#define OWH_RESET_DELAY_PRE OWH_TIMING_G
#define OWH_RESET_DRIVE OWH_TIMING_H
#define OWH_RESET_RELEASE OWH_TIMING_I
#define OWH_RESET_DELAY_POST OWH_TIMING_J

View File

@ -3,20 +3,7 @@
#include <furi.h>
#include <furi_hal.h>
#define ONEWIRE_TRSTL_MIN 270 /* Minimum Reset Low time */
#define ONEWIRE_TRSTL_MAX 1200 /* Maximum Reset Low time */
#define ONEWIRE_TPDH_TYP 20 /* Typical Presence Detect High time */
#define ONEWIRE_TPDL_MIN 100 /* Minimum Presence Detect Low time */
#define ONEWIRE_TPDL_MAX 480 /* Maximum Presence Detect Low time */
#define ONEWIRE_TSLOT_MIN 60 /* Minimum Read/Write Slot time */
#define ONEWIRE_TSLOT_MAX 135 /* Maximum Read/Write Slot time */
#define ONEWIRE_TW1L_MAX 20 /* Maximum Master Write 1 time */
#define ONEWIRE_TRL_TMSR_MAX 30 /* Maximum Master Read Low + Read Sample time */
#define ONEWIRE_TH_TIMEOUT 15000 /* Maximum time before general timeout */
#define TH_TIMEOUT_MAX 15000 /* Maximum time before general timeout */
typedef enum {
OneWireSlaveErrorNone = 0,
@ -26,10 +13,29 @@ typedef enum {
OneWireSlaveErrorTimeout,
} OneWireSlaveError;
typedef struct {
uint16_t trstl_min; /* Minimum Reset Low time */
uint16_t trstl_max; /* Maximum Reset Low time */
uint16_t tpdh_typ; /* Typical Presence Detect High time */
uint16_t tpdl_min; /* Minimum Presence Detect Low time */
uint16_t tpdl_max; /* Maximum Presence Detect Low time */
uint16_t tslot_min; /* Minimum Read/Write Slot time */
uint16_t tslot_max; /* Maximum Read/Write Slot time */
uint16_t tw1l_max; /* Maximum Master Write 1 time */
uint16_t trl_tmsr_max; /* Maximum Master Read Low + Read Sample time */
} OneWireSlaveTimings;
struct OneWireSlave {
const GpioPin* gpio_pin;
const OneWireSlaveTimings* timings;
OneWireSlaveError error;
bool is_first_reset;
bool is_short_reset;
OneWireSlaveResetCallback reset_callback;
OneWireSlaveCommandCallback command_callback;
OneWireSlaveResultCallback result_callback;
@ -39,42 +45,72 @@ struct OneWireSlave {
void* command_callback_context;
};
static const OneWireSlaveTimings onewire_slave_timings_normal = {
.trstl_min = 270,
.trstl_max = 1200,
.tpdh_typ = 20,
.tpdl_min = 100,
.tpdl_max = 480,
.tslot_min = 60,
.tslot_max = 135,
.tw1l_max = 20,
.trl_tmsr_max = 30,
};
static const OneWireSlaveTimings onewire_slave_timings_overdrive = {
.trstl_min = 48,
.trstl_max = 80,
.tpdh_typ = 0,
.tpdl_min = 8,
.tpdl_max = 24,
.tslot_min = 6,
.tslot_max = 16,
.tw1l_max = 2,
.trl_tmsr_max = 3,
};
/*********************** PRIVATE ***********************/
static uint32_t
onewire_slave_wait_while_gpio_is(OneWireSlave* bus, uint32_t time, const bool pin_value) {
uint32_t start = DWT->CYCCNT;
uint32_t time_ticks = time * furi_hal_cortex_instructions_per_microsecond();
uint32_t time_captured;
static bool
onewire_slave_wait_while_gpio_is(OneWireSlave* bus, uint32_t time_us, const bool pin_value) {
const uint32_t time_start = DWT->CYCCNT;
const uint32_t time_ticks = time_us * furi_hal_cortex_instructions_per_microsecond();
uint32_t time_elapsed;
do { //-V1044
time_captured = DWT->CYCCNT;
time_elapsed = DWT->CYCCNT - time_start;
if(furi_hal_gpio_read(bus->gpio_pin) != pin_value) {
uint32_t remaining_time = time_ticks - (time_captured - start);
remaining_time /= furi_hal_cortex_instructions_per_microsecond();
return remaining_time;
return time_ticks >= time_elapsed;
}
} while((time_captured - start) < time_ticks);
} while(time_elapsed < time_ticks);
return 0;
return false;
}
static bool onewire_slave_show_presence(OneWireSlave* bus) {
static inline bool onewire_slave_show_presence(OneWireSlave* bus) {
const OneWireSlaveTimings* timings = bus->timings;
// wait until the bus is high (might return immediately)
onewire_slave_wait_while_gpio_is(bus, ONEWIRE_TRSTL_MAX, false);
onewire_slave_wait_while_gpio_is(bus, timings->trstl_max, false);
// wait while master delay presence check
furi_delay_us(ONEWIRE_TPDH_TYP);
furi_delay_us(timings->tpdh_typ);
// show presence
furi_hal_gpio_write(bus->gpio_pin, false);
furi_delay_us(ONEWIRE_TPDL_MIN);
furi_delay_us(timings->tpdl_min);
furi_hal_gpio_write(bus->gpio_pin, true);
// somebody also can show presence
const uint32_t wait_low_time = ONEWIRE_TPDL_MAX - ONEWIRE_TPDL_MIN;
const uint32_t wait_low_time = timings->tpdl_max - timings->tpdl_min;
// so we will wait
if(onewire_slave_wait_while_gpio_is(bus, wait_low_time, false) == 0) {
if(!onewire_slave_wait_while_gpio_is(bus, wait_low_time, false)) {
bus->error = OneWireSlaveErrorPresenceConflict;
return false;
}
@ -85,27 +121,36 @@ static bool onewire_slave_show_presence(OneWireSlave* bus) {
static inline bool onewire_slave_receive_and_process_command(OneWireSlave* bus) {
/* Reset condition detected, send a presence pulse and reset protocol state */
if(bus->error == OneWireSlaveErrorResetInProgress) {
if(onewire_slave_show_presence(bus)) {
bus->error = OneWireSlaveErrorNone;
if(!bus->is_first_reset) {
/* Guess the reset type */
bus->is_short_reset = onewire_slave_wait_while_gpio_is(
bus,
onewire_slave_timings_overdrive.trstl_max -
onewire_slave_timings_overdrive.tslot_max,
false);
} else {
bus->is_first_reset = false;
}
if(bus->reset_callback != NULL) {
bus->reset_callback(bus->reset_callback_context);
furi_assert(bus->reset_callback);
if(bus->reset_callback(bus->is_short_reset, bus->reset_callback_context)) {
if(onewire_slave_show_presence(bus)) {
bus->error = OneWireSlaveErrorNone;
return true;
}
return true;
}
} else if(bus->error == OneWireSlaveErrorNone) {
uint8_t command;
if(!onewire_slave_receive(bus, &command, 1)) {
/* Upon failure, request an additional iteration to
choose the appropriate action by checking bus->error */
return true;
} else if(bus->command_callback) {
return bus->command_callback(command, bus->command_callback_context);
} else {
bus->error = OneWireSlaveErrorInvalidCommand;
if(onewire_slave_receive(bus, &command, sizeof(command))) {
furi_assert(bus->command_callback);
if(bus->command_callback(command, bus->command_callback_context)) {
return true;
}
}
return (bus->error == OneWireSlaveErrorResetInProgress);
}
return false;
@ -115,9 +160,6 @@ static inline bool onewire_slave_bus_start(OneWireSlave* bus) {
FURI_CRITICAL_ENTER();
furi_hal_gpio_init(bus->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
/* Start in Reset state in order to send a presence pulse immediately */
bus->error = OneWireSlaveErrorResetInProgress;
while(onewire_slave_receive_and_process_command(bus))
;
@ -139,7 +181,15 @@ static void onewire_slave_exti_callback(void* context) {
const uint32_t pulse_length =
(DWT->CYCCNT - pulse_start) / furi_hal_cortex_instructions_per_microsecond();
if((pulse_length >= ONEWIRE_TRSTL_MIN) && pulse_length <= (ONEWIRE_TRSTL_MAX)) {
if((pulse_length >= onewire_slave_timings_overdrive.trstl_min) &&
(pulse_length <= onewire_slave_timings_normal.trstl_max)) {
/* Start in reset state in order to send a presence pulse immediately */
bus->error = OneWireSlaveErrorResetInProgress;
/* Determine reset type (chooses speed mode if supported by the emulated device) */
bus->is_short_reset = pulse_length <= onewire_slave_timings_overdrive.trstl_max;
/* Initial reset allows going directly into overdrive mode */
bus->is_first_reset = true;
const bool result = onewire_slave_bus_start(bus);
if(result && bus->result_callback != NULL) {
@ -158,6 +208,7 @@ OneWireSlave* onewire_slave_alloc(const GpioPin* gpio_pin) {
OneWireSlave* bus = malloc(sizeof(OneWireSlave));
bus->gpio_pin = gpio_pin;
bus->timings = &onewire_slave_timings_normal;
bus->error = OneWireSlaveErrorNone;
return bus;
@ -205,52 +256,45 @@ void onewire_slave_set_result_callback(
}
bool onewire_slave_receive_bit(OneWireSlave* bus) {
const OneWireSlaveTimings* timings = bus->timings;
// wait while bus is low
uint32_t time = ONEWIRE_TSLOT_MAX;
time = onewire_slave_wait_while_gpio_is(bus, time, false);
if(time == 0) {
if(!onewire_slave_wait_while_gpio_is(bus, timings->tslot_max, false)) {
bus->error = OneWireSlaveErrorResetInProgress;
return false;
}
// wait while bus is high
time = ONEWIRE_TH_TIMEOUT;
time = onewire_slave_wait_while_gpio_is(bus, time, true);
if(time == 0) {
if(!onewire_slave_wait_while_gpio_is(bus, TH_TIMEOUT_MAX, true)) {
bus->error = OneWireSlaveErrorTimeout;
return false;
}
// wait a time of zero
time = ONEWIRE_TW1L_MAX;
time = onewire_slave_wait_while_gpio_is(bus, time, false);
return (time > 0);
return onewire_slave_wait_while_gpio_is(bus, timings->tw1l_max, false);
}
bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
const OneWireSlaveTimings* timings = bus->timings;
// wait while bus is low
uint32_t time = ONEWIRE_TSLOT_MAX;
time = onewire_slave_wait_while_gpio_is(bus, time, false);
if(time == 0) {
if(!onewire_slave_wait_while_gpio_is(bus, timings->tslot_max, false)) {
bus->error = OneWireSlaveErrorResetInProgress;
return false;
}
// wait while bus is high
time = ONEWIRE_TH_TIMEOUT;
time = onewire_slave_wait_while_gpio_is(bus, time, true);
if(time == 0) {
if(!onewire_slave_wait_while_gpio_is(bus, TH_TIMEOUT_MAX, true)) {
bus->error = OneWireSlaveErrorTimeout;
return false;
}
// choose write time
uint32_t time;
if(!value) {
furi_hal_gpio_write(bus->gpio_pin, false);
time = ONEWIRE_TRL_TMSR_MAX;
time = timings->trl_tmsr_max;
} else {
time = ONEWIRE_TSLOT_MIN;
time = timings->tslot_min;
}
// hold line for ZERO or ONE time
@ -301,3 +345,13 @@ bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, size_t data_size) {
}
return true;
}
void onewire_slave_set_overdrive(OneWireSlave* bus, bool set) {
const OneWireSlaveTimings* new_timings = set ? &onewire_slave_timings_overdrive :
&onewire_slave_timings_normal;
if(bus->timings != new_timings) {
/* Prevent erroneous reset by waiting for the previous time slot to finish */
onewire_slave_wait_while_gpio_is(bus, bus->timings->tslot_max, false);
bus->timings = new_timings;
}
}

View File

@ -18,68 +18,85 @@ extern "C" {
typedef struct OneWireDevice OneWireDevice;
typedef struct OneWireSlave OneWireSlave;
typedef void (*OneWireSlaveResetCallback)(void* context);
typedef void (*OneWireSlaveResultCallback)(void* context);
typedef bool (*OneWireSlaveResetCallback)(bool is_short, void* context);
typedef bool (*OneWireSlaveCommandCallback)(uint8_t command, void* context);
typedef void (*OneWireSlaveResultCallback)(void* context);
/**
* Allocate onewire slave
* @param gpio_pin
* @return OneWireSlave*
* Allocate OneWireSlave instance
* @param [in] gpio_pin connection pin
* @return pointer to OneWireSlave instance
*/
OneWireSlave* onewire_slave_alloc(const GpioPin* gpio_pin);
/**
* Free onewire slave
* @param bus
* Destroy OneWireSlave instance, free resources
* @param [in] bus pointer to OneWireSlave instance
*/
void onewire_slave_free(OneWireSlave* bus);
/**
* Start working with the bus
* @param bus
* @param [in] bus pointer to OneWireSlave instance
*/
void onewire_slave_start(OneWireSlave* bus);
/**
* Stop working with the bus
* @param bus
* @param [in] bus pointer to OneWireSlave instance
*/
void onewire_slave_stop(OneWireSlave* bus);
/**
* TODO: description comment
* Receive one bit
* @param [in] bus pointer to OneWireSlave instance
* @return received bit value
*/
bool onewire_slave_receive_bit(OneWireSlave* bus);
/**
* TODO: description comment
* Send one bit
* @param [in] bus pointer to OneWireSlave instance
* @param [in] value bit value to send
* @return true on success, false on failure
*/
bool onewire_slave_send_bit(OneWireSlave* bus, bool value);
/**
* Send data
* @param bus
* @param data
* @param data_size
* @return bool
* Send one or more bytes of data
* @param [in] bus pointer to OneWireSlave instance
* @param [in] data pointer to the data to send
* @param [in] data_size size of the data to send
* @return true on success, false on failure
*/
bool onewire_slave_send(OneWireSlave* bus, const uint8_t* data, size_t data_size);
/**
* Receive data
* @param bus
* @param data
* @param data_size
* @return bool
* Receive one or more bytes of data
* @param [in] bus pointer to OneWireSlave instance
* @param [out] data pointer to the receive buffer
* @param [in] data_size number of bytes to receive
* @return true on success, false on failure
*/
bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, size_t data_size);
/**
* Set a callback to be called on each reset
* @param bus
* @param callback
* @param context
* Enable overdrive mode
* @param [in] bus pointer to OneWireSlave instance
* @param [in] set true to turn overdrive on, false to turn it off
*/
void onewire_slave_set_overdrive(OneWireSlave* bus, bool set);
/**
* Set a callback function to be called on each reset.
* The return value of the callback determines whether the emulated device
* supports the short reset (passed as the is_short parameter).
* In most applications, it should also call onewire_slave_set_overdrive()
* to set the appropriate speed mode.
*
* @param [in] bus pointer to OneWireSlave instance
* @param [in] callback pointer to a callback function
* @param [in] context additional parameter to be passed to the callback
*/
void onewire_slave_set_reset_callback(
OneWireSlave* bus,
@ -87,10 +104,13 @@ void onewire_slave_set_reset_callback(
void* context);
/**
* Set a callback to be called on each command
* @param bus
* @param callback
* @param context
* Set a callback function to be called on each command.
* The return value of the callback determines whether further operation
* is possible. As a rule of thumb, return true unless a critical error happened.
*
* @param [in] bus pointer to OneWireSlave instance
* @param [in] callback pointer to a callback function
* @param [in] context additional parameter to be passed to the callback
*/
void onewire_slave_set_command_callback(
OneWireSlave* bus,
@ -99,9 +119,9 @@ void onewire_slave_set_command_callback(
/**
* Set a callback to report emulation success
* @param bus
* @param result_cb
* @param context
* @param [in] bus pointer to OneWireSlave instance
* @param [in] result_cb pointer to a callback function
* @param [in] context additional parameter to be passed to the callback
*/
void onewire_slave_set_result_callback(
OneWireSlave* bus,