2022-03-29 13:01:56 +00:00
|
|
|
#include "one_wire_slave.h"
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
#include <furi.h>
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <furi_hal.h>
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
#define TH_TIMEOUT_MAX 15000 /* Maximum time before general timeout */
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
typedef enum {
|
2023-03-02 13:23:33 +00:00
|
|
|
OneWireSlaveErrorNone = 0,
|
|
|
|
OneWireSlaveErrorResetInProgress,
|
|
|
|
OneWireSlaveErrorPresenceConflict,
|
|
|
|
OneWireSlaveErrorInvalidCommand,
|
|
|
|
OneWireSlaveErrorTimeout,
|
2022-03-29 13:01:56 +00:00
|
|
|
} OneWireSlaveError;
|
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
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;
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
struct OneWireSlave {
|
2023-02-08 05:40:44 +00:00
|
|
|
const GpioPin* gpio_pin;
|
2023-03-22 14:54:06 +00:00
|
|
|
const OneWireSlaveTimings* timings;
|
2022-03-29 13:01:56 +00:00
|
|
|
OneWireSlaveError error;
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
bool is_first_reset;
|
|
|
|
bool is_short_reset;
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
OneWireSlaveResetCallback reset_callback;
|
|
|
|
OneWireSlaveCommandCallback command_callback;
|
|
|
|
OneWireSlaveResultCallback result_callback;
|
|
|
|
|
|
|
|
void* reset_callback_context;
|
|
|
|
void* result_callback_context;
|
|
|
|
void* command_callback_context;
|
2022-03-29 13:01:56 +00:00
|
|
|
};
|
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
/*********************** PRIVATE ***********************/
|
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
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;
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
do { //-V1044
|
2023-03-22 14:54:06 +00:00
|
|
|
time_elapsed = DWT->CYCCNT - time_start;
|
2023-02-08 05:40:44 +00:00
|
|
|
if(furi_hal_gpio_read(bus->gpio_pin) != pin_value) {
|
2023-03-22 14:54:06 +00:00
|
|
|
return time_ticks >= time_elapsed;
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
2023-03-22 14:54:06 +00:00
|
|
|
} while(time_elapsed < time_ticks);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
return false;
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
static inline bool onewire_slave_show_presence(OneWireSlave* bus) {
|
|
|
|
const OneWireSlaveTimings* timings = bus->timings;
|
2023-03-02 13:23:33 +00:00
|
|
|
// wait until the bus is high (might return immediately)
|
2023-03-22 14:54:06 +00:00
|
|
|
onewire_slave_wait_while_gpio_is(bus, timings->trstl_max, false);
|
2022-03-29 13:01:56 +00:00
|
|
|
// wait while master delay presence check
|
2023-03-22 14:54:06 +00:00
|
|
|
furi_delay_us(timings->tpdh_typ);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
// show presence
|
2023-02-08 05:40:44 +00:00
|
|
|
furi_hal_gpio_write(bus->gpio_pin, false);
|
2023-03-22 14:54:06 +00:00
|
|
|
furi_delay_us(timings->tpdl_min);
|
2023-02-08 05:40:44 +00:00
|
|
|
furi_hal_gpio_write(bus->gpio_pin, true);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
// somebody also can show presence
|
2023-03-22 14:54:06 +00:00
|
|
|
const uint32_t wait_low_time = timings->tpdl_max - timings->tpdl_min;
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
// so we will wait
|
2023-03-22 14:54:06 +00:00
|
|
|
if(!onewire_slave_wait_while_gpio_is(bus, wait_low_time, false)) {
|
2023-03-02 13:23:33 +00:00
|
|
|
bus->error = OneWireSlaveErrorPresenceConflict;
|
2022-03-29 13:01:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
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) {
|
2023-03-22 14:54:06 +00:00
|
|
|
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;
|
|
|
|
}
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
furi_assert(bus->reset_callback);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
if(bus->reset_callback(bus->is_short_reset, bus->reset_callback_context)) {
|
|
|
|
if(onewire_slave_show_presence(bus)) {
|
|
|
|
bus->error = OneWireSlaveErrorNone;
|
|
|
|
return true;
|
|
|
|
}
|
2023-03-02 13:23:33 +00:00
|
|
|
}
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
} else if(bus->error == OneWireSlaveErrorNone) {
|
|
|
|
uint8_t command;
|
2023-03-22 14:54:06 +00:00
|
|
|
if(onewire_slave_receive(bus, &command, sizeof(command))) {
|
|
|
|
furi_assert(bus->command_callback);
|
|
|
|
if(bus->command_callback(command, bus->command_callback_context)) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
2023-03-22 14:54:06 +00:00
|
|
|
|
|
|
|
return (bus->error == OneWireSlaveErrorResetInProgress);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
return false;
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
static inline bool onewire_slave_bus_start(OneWireSlave* bus) {
|
|
|
|
FURI_CRITICAL_ENTER();
|
|
|
|
furi_hal_gpio_init(bus->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
while(onewire_slave_receive_and_process_command(bus))
|
|
|
|
;
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
const bool result = (bus->error == OneWireSlaveErrorNone);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
furi_hal_gpio_init(bus->gpio_pin, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
|
|
|
|
FURI_CRITICAL_EXIT();
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
static void onewire_slave_exti_callback(void* context) {
|
2022-03-29 13:01:56 +00:00
|
|
|
OneWireSlave* bus = context;
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
const volatile bool input_state = furi_hal_gpio_read(bus->gpio_pin);
|
2022-03-29 13:01:56 +00:00
|
|
|
static uint32_t pulse_start = 0;
|
|
|
|
|
|
|
|
if(input_state) {
|
2023-03-02 13:23:33 +00:00
|
|
|
const uint32_t pulse_length =
|
2022-07-20 10:56:33 +00:00
|
|
|
(DWT->CYCCNT - pulse_start) / furi_hal_cortex_instructions_per_microsecond();
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2023-03-22 14:54:06 +00:00
|
|
|
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;
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
const bool result = onewire_slave_bus_start(bus);
|
|
|
|
|
|
|
|
if(result && bus->result_callback != NULL) {
|
|
|
|
bus->result_callback(bus->result_callback_context);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
}
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
} else {
|
|
|
|
pulse_start = DWT->CYCCNT;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*********************** PUBLIC ***********************/
|
|
|
|
|
2023-02-08 05:40:44 +00:00
|
|
|
OneWireSlave* onewire_slave_alloc(const GpioPin* gpio_pin) {
|
2022-03-29 13:01:56 +00:00
|
|
|
OneWireSlave* bus = malloc(sizeof(OneWireSlave));
|
2023-03-02 13:23:33 +00:00
|
|
|
|
2023-02-08 05:40:44 +00:00
|
|
|
bus->gpio_pin = gpio_pin;
|
2023-03-22 14:54:06 +00:00
|
|
|
bus->timings = &onewire_slave_timings_normal;
|
2023-03-02 13:23:33 +00:00
|
|
|
bus->error = OneWireSlaveErrorNone;
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
return bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onewire_slave_free(OneWireSlave* bus) {
|
|
|
|
onewire_slave_stop(bus);
|
|
|
|
free(bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onewire_slave_start(OneWireSlave* bus) {
|
2023-03-02 13:23:33 +00:00
|
|
|
furi_hal_gpio_add_int_callback(bus->gpio_pin, onewire_slave_exti_callback, bus);
|
2023-02-08 05:40:44 +00:00
|
|
|
furi_hal_gpio_write(bus->gpio_pin, true);
|
|
|
|
furi_hal_gpio_init(bus->gpio_pin, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onewire_slave_stop(OneWireSlave* bus) {
|
2023-02-08 05:40:44 +00:00
|
|
|
furi_hal_gpio_write(bus->gpio_pin, true);
|
|
|
|
furi_hal_gpio_init(bus->gpio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
|
|
|
furi_hal_gpio_remove_int_callback(bus->gpio_pin);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
void onewire_slave_set_reset_callback(
|
|
|
|
OneWireSlave* bus,
|
|
|
|
OneWireSlaveResetCallback callback,
|
|
|
|
void* context) {
|
|
|
|
bus->reset_callback = callback;
|
|
|
|
bus->reset_callback_context = context;
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
void onewire_slave_set_command_callback(
|
|
|
|
OneWireSlave* bus,
|
|
|
|
OneWireSlaveCommandCallback callback,
|
|
|
|
void* context) {
|
|
|
|
bus->command_callback = callback;
|
|
|
|
bus->command_callback_context = context;
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onewire_slave_set_result_callback(
|
|
|
|
OneWireSlave* bus,
|
|
|
|
OneWireSlaveResultCallback result_cb,
|
|
|
|
void* context) {
|
2023-03-02 13:23:33 +00:00
|
|
|
bus->result_callback = result_cb;
|
|
|
|
bus->result_callback_context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onewire_slave_receive_bit(OneWireSlave* bus) {
|
2023-03-22 14:54:06 +00:00
|
|
|
const OneWireSlaveTimings* timings = bus->timings;
|
2023-03-02 13:23:33 +00:00
|
|
|
// wait while bus is low
|
2023-03-22 14:54:06 +00:00
|
|
|
if(!onewire_slave_wait_while_gpio_is(bus, timings->tslot_max, false)) {
|
2023-03-02 13:23:33 +00:00
|
|
|
bus->error = OneWireSlaveErrorResetInProgress;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait while bus is high
|
2023-03-22 14:54:06 +00:00
|
|
|
if(!onewire_slave_wait_while_gpio_is(bus, TH_TIMEOUT_MAX, true)) {
|
2023-03-02 13:23:33 +00:00
|
|
|
bus->error = OneWireSlaveErrorTimeout;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait a time of zero
|
2023-03-22 14:54:06 +00:00
|
|
|
return onewire_slave_wait_while_gpio_is(bus, timings->tw1l_max, false);
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
|
2023-03-22 14:54:06 +00:00
|
|
|
const OneWireSlaveTimings* timings = bus->timings;
|
2023-03-02 13:23:33 +00:00
|
|
|
// wait while bus is low
|
2023-03-22 14:54:06 +00:00
|
|
|
if(!onewire_slave_wait_while_gpio_is(bus, timings->tslot_max, false)) {
|
2023-03-02 13:23:33 +00:00
|
|
|
bus->error = OneWireSlaveErrorResetInProgress;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait while bus is high
|
2023-03-22 14:54:06 +00:00
|
|
|
if(!onewire_slave_wait_while_gpio_is(bus, TH_TIMEOUT_MAX, true)) {
|
2023-03-02 13:23:33 +00:00
|
|
|
bus->error = OneWireSlaveErrorTimeout;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// choose write time
|
2023-03-22 14:54:06 +00:00
|
|
|
uint32_t time;
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
if(!value) {
|
|
|
|
furi_hal_gpio_write(bus->gpio_pin, false);
|
2023-03-22 14:54:06 +00:00
|
|
|
time = timings->trl_tmsr_max;
|
2023-03-02 13:23:33 +00:00
|
|
|
} else {
|
2023-03-22 14:54:06 +00:00
|
|
|
time = timings->tslot_min;
|
2023-03-02 13:23:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// hold line for ZERO or ONE time
|
|
|
|
furi_delay_us(time);
|
|
|
|
furi_hal_gpio_write(bus->gpio_pin, true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
bool onewire_slave_send(OneWireSlave* bus, const uint8_t* data, size_t data_size) {
|
2023-02-08 05:40:44 +00:00
|
|
|
furi_hal_gpio_write(bus->gpio_pin, true);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
size_t bytes_sent = 0;
|
|
|
|
|
2022-03-29 13:01:56 +00:00
|
|
|
// bytes loop
|
2023-03-02 13:23:33 +00:00
|
|
|
for(; bytes_sent < data_size; ++bytes_sent) {
|
|
|
|
const uint8_t data_byte = data[bytes_sent];
|
2022-03-29 13:01:56 +00:00
|
|
|
|
|
|
|
// bit loop
|
|
|
|
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
|
|
|
|
if(!onewire_slave_send_bit(bus, bit_mask & data_byte)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, size_t data_size) {
|
2023-02-08 05:40:44 +00:00
|
|
|
furi_hal_gpio_write(bus->gpio_pin, true);
|
2022-03-29 13:01:56 +00:00
|
|
|
|
2023-03-02 13:23:33 +00:00
|
|
|
size_t bytes_received = 0;
|
|
|
|
|
|
|
|
for(; bytes_received < data_size; ++bytes_received) {
|
2022-03-29 13:01:56 +00:00
|
|
|
uint8_t value = 0;
|
|
|
|
|
|
|
|
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
|
2023-03-02 13:23:33 +00:00
|
|
|
if(onewire_slave_receive_bit(bus)) {
|
|
|
|
value |= bit_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bus->error != OneWireSlaveErrorNone) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data[bytes_received] = value;
|
|
|
|
}
|
2023-03-02 13:23:33 +00:00
|
|
|
return true;
|
2022-03-29 13:01:56 +00:00
|
|
|
}
|
2023-03-22 14:54:06 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|