[FL-3057] Allow use of any suitable pin for 1-Wire devices (#2350)
* Add 1-wire thermometer example app stub * Working 1-wire thermometer app * Refactor app to use threads * Clean up code, add comments * Add CRC checking * Increase update period * Fix error in fbt * Revert the old update period * Use settable pin in onewire_host * Use settable pin for onewire_slave * Clear EXTI flag after callback, make private methods static in onewire_slave * Do not hardcode GPIO pin number * Remove iButton hal from furi_hal_rfid * Remove most of furi_hal_ibutton * Add some of furi_hal_ibutton back * Slightly neater code * Fix formatting * Fix PVS-studio warnings * Update CODEOWNERS * Add furi_hal_gpio_get_ext_pin_number * Create README.md * FuriHal: move furi_hal_gpio_get_ext_pin_number to resources --------- Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
@@ -25,8 +25,8 @@ iButtonWorker* ibutton_worker_alloc() {
|
||||
iButtonWorker* worker = malloc(sizeof(iButtonWorker));
|
||||
worker->key_p = NULL;
|
||||
worker->key_data = malloc(ibutton_key_get_max_size());
|
||||
worker->host = onewire_host_alloc();
|
||||
worker->slave = onewire_slave_alloc();
|
||||
worker->host = onewire_host_alloc(&ibutton_gpio);
|
||||
worker->slave = onewire_slave_alloc(&ibutton_gpio);
|
||||
worker->writer = ibutton_writer_alloc(worker->host);
|
||||
worker->device = onewire_device_alloc(0, 0, 0, 0, 0, 0, 0, 0);
|
||||
worker->messages = furi_message_queue_alloc(1, sizeof(iButtonMessage));
|
||||
|
@@ -234,16 +234,13 @@ void ibutton_worker_emulate_timer_cb(void* context) {
|
||||
furi_assert(context);
|
||||
iButtonWorker* worker = context;
|
||||
|
||||
LevelDuration level =
|
||||
const LevelDuration level_duration =
|
||||
protocol_dict_encoder_yield(worker->protocols, worker->protocol_to_encode);
|
||||
|
||||
furi_hal_ibutton_emulate_set_next(level_duration_get_duration(level));
|
||||
const bool level = level_duration_get_level(level_duration);
|
||||
|
||||
if(level_duration_get_level(level)) {
|
||||
furi_hal_ibutton_pin_high();
|
||||
} else {
|
||||
furi_hal_ibutton_pin_low();
|
||||
}
|
||||
furi_hal_ibutton_emulate_set_next(level);
|
||||
furi_hal_ibutton_pin_write(level);
|
||||
}
|
||||
|
||||
void ibutton_worker_emulate_timer_start(iButtonWorker* worker) {
|
||||
@@ -266,7 +263,7 @@ void ibutton_worker_emulate_timer_start(iButtonWorker* worker) {
|
||||
protocol_dict_set_data(worker->protocols, worker->protocol_to_encode, key_id, key_size);
|
||||
protocol_dict_encoder_start(worker->protocols, worker->protocol_to_encode);
|
||||
|
||||
furi_hal_ibutton_start_drive();
|
||||
furi_hal_ibutton_pin_configure();
|
||||
furi_hal_ibutton_emulate_start(0, ibutton_worker_emulate_timer_cb, worker);
|
||||
}
|
||||
|
||||
|
@@ -1,18 +1,19 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
#include "one_wire_host.h"
|
||||
#include "one_wire_host_timing.h"
|
||||
|
||||
struct OneWireHost {
|
||||
// global search state
|
||||
unsigned char saved_rom[8];
|
||||
const GpioPin* gpio_pin;
|
||||
unsigned char saved_rom[8]; /** < global search state */
|
||||
uint8_t last_discrepancy;
|
||||
uint8_t last_family_discrepancy;
|
||||
bool last_device_flag;
|
||||
};
|
||||
|
||||
OneWireHost* onewire_host_alloc() {
|
||||
OneWireHost* onewire_host_alloc(const GpioPin* gpio_pin) {
|
||||
OneWireHost* host = malloc(sizeof(OneWireHost));
|
||||
host->gpio_pin = gpio_pin;
|
||||
onewire_host_reset_search(host);
|
||||
return host;
|
||||
}
|
||||
@@ -23,49 +24,47 @@ void onewire_host_free(OneWireHost* host) {
|
||||
}
|
||||
|
||||
bool onewire_host_reset(OneWireHost* host) {
|
||||
UNUSED(host);
|
||||
uint8_t r;
|
||||
uint8_t retries = 125;
|
||||
|
||||
// wait until the gpio is high
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(host->gpio_pin, true);
|
||||
do {
|
||||
if(--retries == 0) return 0;
|
||||
furi_delay_us(2);
|
||||
} while(!furi_hal_ibutton_pin_get_level());
|
||||
} while(!furi_hal_gpio_read(host->gpio_pin));
|
||||
|
||||
// pre delay
|
||||
furi_delay_us(OWH_RESET_DELAY_PRE);
|
||||
|
||||
// drive low
|
||||
furi_hal_ibutton_pin_low();
|
||||
furi_hal_gpio_write(host->gpio_pin, false);
|
||||
furi_delay_us(OWH_RESET_DRIVE);
|
||||
|
||||
// release
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(host->gpio_pin, true);
|
||||
furi_delay_us(OWH_RESET_RELEASE);
|
||||
|
||||
// read and post delay
|
||||
r = !furi_hal_ibutton_pin_get_level();
|
||||
r = !furi_hal_gpio_read(host->gpio_pin);
|
||||
furi_delay_us(OWH_RESET_DELAY_POST);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
bool onewire_host_read_bit(OneWireHost* host) {
|
||||
UNUSED(host);
|
||||
bool result;
|
||||
|
||||
// drive low
|
||||
furi_hal_ibutton_pin_low();
|
||||
furi_hal_gpio_write(host->gpio_pin, false);
|
||||
furi_delay_us(OWH_READ_DRIVE);
|
||||
|
||||
// release
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(host->gpio_pin, true);
|
||||
furi_delay_us(OWH_READ_RELEASE);
|
||||
|
||||
// read and post delay
|
||||
result = furi_hal_ibutton_pin_get_level();
|
||||
result = furi_hal_gpio_read(host->gpio_pin);
|
||||
furi_delay_us(OWH_READ_DELAY_POST);
|
||||
|
||||
return result;
|
||||
@@ -90,22 +89,21 @@ void onewire_host_read_bytes(OneWireHost* host, uint8_t* buffer, uint16_t count)
|
||||
}
|
||||
|
||||
void onewire_host_write_bit(OneWireHost* host, bool value) {
|
||||
UNUSED(host);
|
||||
if(value) {
|
||||
// drive low
|
||||
furi_hal_ibutton_pin_low();
|
||||
furi_hal_gpio_write(host->gpio_pin, false);
|
||||
furi_delay_us(OWH_WRITE_1_DRIVE);
|
||||
|
||||
// release
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(host->gpio_pin, true);
|
||||
furi_delay_us(OWH_WRITE_1_RELEASE);
|
||||
} else {
|
||||
// drive low
|
||||
furi_hal_ibutton_pin_low();
|
||||
furi_hal_gpio_write(host->gpio_pin, false);
|
||||
furi_delay_us(OWH_WRITE_0_DRIVE);
|
||||
|
||||
// release
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(host->gpio_pin, true);
|
||||
furi_delay_us(OWH_WRITE_0_RELEASE);
|
||||
}
|
||||
}
|
||||
@@ -123,13 +121,13 @@ void onewire_host_skip(OneWireHost* host) {
|
||||
}
|
||||
|
||||
void onewire_host_start(OneWireHost* host) {
|
||||
UNUSED(host);
|
||||
furi_hal_ibutton_start_drive();
|
||||
furi_hal_gpio_write(host->gpio_pin, true);
|
||||
furi_hal_gpio_init(host->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
||||
}
|
||||
|
||||
void onewire_host_stop(OneWireHost* host) {
|
||||
UNUSED(host);
|
||||
furi_hal_ibutton_stop();
|
||||
furi_hal_gpio_write(host->gpio_pin, true);
|
||||
furi_hal_gpio_init(host->gpio_pin, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
||||
}
|
||||
|
||||
void onewire_host_reset_search(OneWireHost* host) {
|
||||
@@ -150,7 +148,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* newAddr, OneWireHostSearchMode mode) {
|
||||
uint8_t 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;
|
||||
@@ -259,7 +257,7 @@ uint8_t onewire_host_search(OneWireHost* host, uint8_t* newAddr, OneWireHostSear
|
||||
host->last_family_discrepancy = 0;
|
||||
search_result = false;
|
||||
} else {
|
||||
for(int i = 0; i < 8; i++) newAddr[i] = host->saved_rom[i];
|
||||
for(int i = 0; i < 8; i++) new_addr[i] = host->saved_rom[i];
|
||||
}
|
||||
|
||||
return search_result;
|
||||
|
@@ -22,10 +22,10 @@ typedef struct OneWireHost OneWireHost;
|
||||
|
||||
/**
|
||||
* Allocate onewire host bus
|
||||
* @param gpio
|
||||
* @param pin
|
||||
* @return OneWireHost*
|
||||
*/
|
||||
OneWireHost* onewire_host_alloc();
|
||||
OneWireHost* onewire_host_alloc(const GpioPin* gpio_pin);
|
||||
|
||||
/**
|
||||
* Deallocate onewire host bus
|
||||
@@ -114,7 +114,7 @@ void onewire_host_target_search(OneWireHost* host, uint8_t family_code);
|
||||
* @param mode
|
||||
* @return uint8_t
|
||||
*/
|
||||
uint8_t onewire_host_search(OneWireHost* host, uint8_t* newAddr, OneWireHostSearchMode mode);
|
||||
uint8_t onewire_host_search(OneWireHost* host, uint8_t* new_addr, OneWireHostSearchMode mode);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
@@ -27,6 +27,7 @@ typedef enum {
|
||||
} OneWireSlaveError;
|
||||
|
||||
struct OneWireSlave {
|
||||
const GpioPin* gpio_pin;
|
||||
OneWireSlaveError error;
|
||||
OneWireDevice* device;
|
||||
OneWireSlaveResultCallback result_cb;
|
||||
@@ -35,15 +36,15 @@ struct OneWireSlave {
|
||||
|
||||
/*********************** PRIVATE ***********************/
|
||||
|
||||
uint32_t onewire_slave_wait_while_gpio_is(OneWireSlave* bus, uint32_t time, const bool pin_value) {
|
||||
UNUSED(bus);
|
||||
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;
|
||||
|
||||
do { //-V1044
|
||||
time_captured = DWT->CYCCNT;
|
||||
if(furi_hal_ibutton_pin_get_level() != pin_value) {
|
||||
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;
|
||||
@@ -53,14 +54,14 @@ uint32_t onewire_slave_wait_while_gpio_is(OneWireSlave* bus, uint32_t time, cons
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool onewire_slave_show_presence(OneWireSlave* bus) {
|
||||
static bool onewire_slave_show_presence(OneWireSlave* bus) {
|
||||
// wait while master delay presence check
|
||||
onewire_slave_wait_while_gpio_is(bus, OWS_PRESENCE_TIMEOUT, true);
|
||||
|
||||
// show presence
|
||||
furi_hal_ibutton_pin_low();
|
||||
furi_hal_gpio_write(bus->gpio_pin, false);
|
||||
furi_delay_us(OWS_PRESENCE_MIN);
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(bus->gpio_pin, true);
|
||||
|
||||
// somebody also can show presence
|
||||
const uint32_t wait_low_time = OWS_PRESENCE_MAX - OWS_PRESENCE_MIN;
|
||||
@@ -74,7 +75,7 @@ bool onewire_slave_show_presence(OneWireSlave* bus) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool onewire_slave_receive_bit(OneWireSlave* bus) {
|
||||
static bool onewire_slave_receive_bit(OneWireSlave* bus) {
|
||||
// wait while bus is low
|
||||
uint32_t time = OWS_SLOT_MAX;
|
||||
time = onewire_slave_wait_while_gpio_is(bus, time, false);
|
||||
@@ -98,7 +99,7 @@ bool onewire_slave_receive_bit(OneWireSlave* bus) {
|
||||
return (time > 0);
|
||||
}
|
||||
|
||||
bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
|
||||
static bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
|
||||
const bool write_zero = !value;
|
||||
|
||||
// wait while bus is low
|
||||
@@ -119,7 +120,7 @@ bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
|
||||
|
||||
// choose write time
|
||||
if(write_zero) {
|
||||
furi_hal_ibutton_pin_low();
|
||||
furi_hal_gpio_write(bus->gpio_pin, false);
|
||||
time = OWS_WRITE_ZERO;
|
||||
} else {
|
||||
time = OWS_READ_MAX;
|
||||
@@ -127,12 +128,12 @@ bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
|
||||
|
||||
// hold line for ZERO or ONE time
|
||||
furi_delay_us(time);
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(bus->gpio_pin, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void onewire_slave_cmd_search_rom(OneWireSlave* bus) {
|
||||
static void onewire_slave_cmd_search_rom(OneWireSlave* bus) {
|
||||
const uint8_t key_bytes = 8;
|
||||
uint8_t* key = onewire_device_get_id_p(bus->device);
|
||||
|
||||
@@ -151,7 +152,7 @@ void onewire_slave_cmd_search_rom(OneWireSlave* bus) {
|
||||
}
|
||||
}
|
||||
|
||||
bool onewire_slave_receive_and_process_cmd(OneWireSlave* bus) {
|
||||
static bool onewire_slave_receive_and_process_cmd(OneWireSlave* bus) {
|
||||
uint8_t cmd;
|
||||
onewire_slave_receive(bus, &cmd, 1);
|
||||
|
||||
@@ -178,14 +179,14 @@ bool onewire_slave_receive_and_process_cmd(OneWireSlave* bus) {
|
||||
}
|
||||
}
|
||||
|
||||
bool onewire_slave_bus_start(OneWireSlave* bus) {
|
||||
static bool onewire_slave_bus_start(OneWireSlave* bus) {
|
||||
bool result = true;
|
||||
|
||||
if(bus->device == NULL) {
|
||||
result = false;
|
||||
} else {
|
||||
FURI_CRITICAL_ENTER();
|
||||
furi_hal_ibutton_start_drive_in_isr();
|
||||
furi_hal_gpio_init(bus->gpio_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
||||
bus->error = NO_ERROR;
|
||||
|
||||
if(onewire_slave_show_presence(bus)) {
|
||||
@@ -197,7 +198,7 @@ bool onewire_slave_bus_start(OneWireSlave* bus) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
furi_hal_ibutton_start_interrupt_in_isr();
|
||||
furi_hal_gpio_init(bus->gpio_pin, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
|
||||
FURI_CRITICAL_EXIT();
|
||||
}
|
||||
|
||||
@@ -207,7 +208,7 @@ bool onewire_slave_bus_start(OneWireSlave* bus) {
|
||||
static void exti_cb(void* context) {
|
||||
OneWireSlave* bus = context;
|
||||
|
||||
volatile bool input_state = furi_hal_ibutton_pin_get_level();
|
||||
volatile bool input_state = furi_hal_gpio_read(bus->gpio_pin);
|
||||
static uint32_t pulse_start = 0;
|
||||
|
||||
if(input_state) {
|
||||
@@ -234,8 +235,9 @@ static void exti_cb(void* context) {
|
||||
|
||||
/*********************** PUBLIC ***********************/
|
||||
|
||||
OneWireSlave* onewire_slave_alloc() {
|
||||
OneWireSlave* onewire_slave_alloc(const GpioPin* gpio_pin) {
|
||||
OneWireSlave* bus = malloc(sizeof(OneWireSlave));
|
||||
bus->gpio_pin = gpio_pin;
|
||||
bus->error = NO_ERROR;
|
||||
bus->device = NULL;
|
||||
bus->result_cb = NULL;
|
||||
@@ -249,14 +251,15 @@ void onewire_slave_free(OneWireSlave* bus) {
|
||||
}
|
||||
|
||||
void onewire_slave_start(OneWireSlave* bus) {
|
||||
furi_hal_ibutton_add_interrupt(exti_cb, bus);
|
||||
furi_hal_ibutton_start_interrupt();
|
||||
furi_hal_gpio_add_int_callback(bus->gpio_pin, exti_cb, bus);
|
||||
furi_hal_gpio_write(bus->gpio_pin, true);
|
||||
furi_hal_gpio_init(bus->gpio_pin, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
|
||||
}
|
||||
|
||||
void onewire_slave_stop(OneWireSlave* bus) {
|
||||
UNUSED(bus);
|
||||
furi_hal_ibutton_stop();
|
||||
furi_hal_ibutton_remove_interrupt();
|
||||
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);
|
||||
}
|
||||
|
||||
void onewire_slave_attach(OneWireSlave* bus, OneWireDevice* device) {
|
||||
@@ -282,7 +285,7 @@ void onewire_slave_set_result_callback(
|
||||
bool onewire_slave_send(OneWireSlave* bus, const uint8_t* address, const uint8_t data_length) {
|
||||
uint8_t bytes_sent = 0;
|
||||
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(bus->gpio_pin, true);
|
||||
|
||||
// bytes loop
|
||||
for(; bytes_sent < data_length; ++bytes_sent) {
|
||||
@@ -304,7 +307,7 @@ bool onewire_slave_send(OneWireSlave* bus, const uint8_t* address, const uint8_t
|
||||
bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, const uint8_t data_length) {
|
||||
uint8_t bytes_received = 0;
|
||||
|
||||
furi_hal_ibutton_pin_high();
|
||||
furi_hal_gpio_write(bus->gpio_pin, true);
|
||||
|
||||
for(; bytes_received < data_length; ++bytes_received) {
|
||||
uint8_t value = 0;
|
||||
|
@@ -19,10 +19,10 @@ typedef void (*OneWireSlaveResultCallback)(void* context);
|
||||
|
||||
/**
|
||||
* Allocate onewire slave
|
||||
* @param pin
|
||||
* @param gpio_pin
|
||||
* @return OneWireSlave*
|
||||
*/
|
||||
OneWireSlave* onewire_slave_alloc();
|
||||
OneWireSlave* onewire_slave_alloc(const GpioPin* gpio_pin);
|
||||
|
||||
/**
|
||||
* Free onewire slave
|
||||
|
Reference in New Issue
Block a user