[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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user