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