mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2024-12-19 03:11:52 +03:00
806428efeb
* 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 * Update CODEOWNERS * Add furi_hal_gpio_get_ext_pin_number * Create README.md * Temporary get Metakom and Cyfral keys out of the way * Better enum name * Syncing work, does not compile * Syncing work, now compiles * Working read impl for DS1990 and DS1992 * Add the ability to display extended key data * Get rid of DialogEx * Add save and load API * Better iButtonKey encapsulation * Fix crash * Load key code boilerplate * More load key code boilerplate * Minor code cleanup * Implement loading and saving DS1990 keys * Implement the Info scene * Implement loading & saving for DS1992 * Implement read error scene stub * Implement delete confirmation screen * Better error messages (protocol-dependent) * Minor old code cleanup * Remove iButtonDevice, add command callback to iButtonSlave * Implement draft emulation for DS1990 * Better emulation for DS1990 * Initial emulation implementation for DS1992 * Better common command definitions * Use common submenu callback, add protocol list * Improve ViewData screen * Improve scene_add_type * Add stubs for write functionality * Improve naming consistency * Implement writing a DS1992 onto another one * Improve DS1992 write code * Improve DS1992 write code once more * Prepare write_blank for DS1990, delete ibutton_writer * Implement writing DS1990 onto blanks * Fix reading DS1990 * Partially implement writing DS1992 onto blanks * Implement GUI for writing keys * Implement GUI for emulating keys * Reduce memory usage for pretty_format * Automatically truncate data more than 256 bytes * Initial implementation of DS1996 (not tested) * Fix crash due to missing virtual function * Improve emulation code * Improve DS1992 emulation code * Correct return value for onewire_slave_send * Correct return value for onewire_slave_receive * Implement emulation for DS1992 & DS1996 * Better constant names * Simplify & optimise the emulation code * Remove duplicate code * Add skip rom command emulation * Show loading animation for large keys * Implement manual adding & editing of keys * Use buffered file streams to speed up saving & loading * Reset key name before adding a new one * Sync a buffered file stream before saving * Use the DSGeneric protocol as a fallback option * Implement emulation via RPC * Refactor iButton code in preparation for comparator keys * Refactor iButton code in preparation for comparator keys once more * Make some functions static * Make protocols not rely on one_wire classes * Improve ProtocolDict usage * Improve ProtocolDict usage more * Implement reading Metakom & Cyfral keys * Rename some files * Better file structure * Implement a unified interface for misc protocols * Implement a unified interface for dallas protocols * Concrete types for Dallas protocols * Implement a unified interface for all key types * Improved type naming * Improved private types * Proper types in protocol definitions * Implement emulation for Cyfral & Metakom keys * Implement save&load for Metakom & Cyfral keys * Better type names * Rename files, better names * Allocate iButtonProtocols like a normal class * Reset the key each time the start scene is selected * Improve comments and constants * Add ibutton_protocols to SDK headers * Add ibutton_key to SDK headers * Add ibutton_key to SDK headers * Implement reading via cli * Implement emulation via cli * Implement writing Dallas blanks via cli * Correctly revert the editing if cancelled by the user * Correct committing mishap * Elide the long text on the info screen * Change key name for data in Misc keys * Update iButtonFileFormat.md * Remember the key's folder * Save menu position in ReadKeyMenu and SavedKeyMenu * Correct use of preselected path in file browser Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
271 lines
8.0 KiB
C
271 lines
8.0 KiB
C
#include <furi.h>
|
|
|
|
#include "one_wire_host.h"
|
|
#include "one_wire_host_timing.h"
|
|
|
|
struct OneWireHost {
|
|
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(const GpioPin* gpio_pin) {
|
|
OneWireHost* host = malloc(sizeof(OneWireHost));
|
|
host->gpio_pin = gpio_pin;
|
|
onewire_host_reset_search(host);
|
|
return host;
|
|
}
|
|
|
|
void onewire_host_free(OneWireHost* host) {
|
|
onewire_host_stop(host);
|
|
free(host);
|
|
}
|
|
|
|
bool onewire_host_reset(OneWireHost* host) {
|
|
uint8_t r;
|
|
uint8_t retries = 125;
|
|
|
|
// wait until the gpio is high
|
|
furi_hal_gpio_write(host->gpio_pin, true);
|
|
do {
|
|
if(--retries == 0) return 0;
|
|
furi_delay_us(2);
|
|
} while(!furi_hal_gpio_read(host->gpio_pin));
|
|
|
|
// pre delay
|
|
furi_delay_us(OWH_RESET_DELAY_PRE);
|
|
|
|
// drive low
|
|
furi_hal_gpio_write(host->gpio_pin, false);
|
|
furi_delay_us(OWH_RESET_DRIVE);
|
|
|
|
// release
|
|
furi_hal_gpio_write(host->gpio_pin, true);
|
|
furi_delay_us(OWH_RESET_RELEASE);
|
|
|
|
// read and post delay
|
|
r = !furi_hal_gpio_read(host->gpio_pin);
|
|
furi_delay_us(OWH_RESET_DELAY_POST);
|
|
|
|
return r;
|
|
}
|
|
|
|
bool onewire_host_read_bit(OneWireHost* host) {
|
|
bool result;
|
|
|
|
// drive low
|
|
furi_hal_gpio_write(host->gpio_pin, false);
|
|
furi_delay_us(OWH_READ_DRIVE);
|
|
|
|
// release
|
|
furi_hal_gpio_write(host->gpio_pin, true);
|
|
furi_delay_us(OWH_READ_RELEASE);
|
|
|
|
// read and post delay
|
|
result = furi_hal_gpio_read(host->gpio_pin);
|
|
furi_delay_us(OWH_READ_DELAY_POST);
|
|
|
|
return result;
|
|
}
|
|
|
|
uint8_t onewire_host_read(OneWireHost* host) {
|
|
uint8_t result = 0;
|
|
|
|
for(uint8_t bitMask = 0x01; bitMask; bitMask <<= 1) {
|
|
if(onewire_host_read_bit(host)) {
|
|
result |= bitMask;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void onewire_host_read_bytes(OneWireHost* host, uint8_t* buffer, uint16_t count) {
|
|
for(uint16_t i = 0; i < count; i++) {
|
|
buffer[i] = onewire_host_read(host);
|
|
}
|
|
}
|
|
|
|
void onewire_host_write_bit(OneWireHost* host, bool value) {
|
|
if(value) {
|
|
// drive low
|
|
furi_hal_gpio_write(host->gpio_pin, false);
|
|
furi_delay_us(OWH_WRITE_1_DRIVE);
|
|
|
|
// release
|
|
furi_hal_gpio_write(host->gpio_pin, true);
|
|
furi_delay_us(OWH_WRITE_1_RELEASE);
|
|
} else {
|
|
// drive low
|
|
furi_hal_gpio_write(host->gpio_pin, false);
|
|
furi_delay_us(OWH_WRITE_0_DRIVE);
|
|
|
|
// release
|
|
furi_hal_gpio_write(host->gpio_pin, true);
|
|
furi_delay_us(OWH_WRITE_0_RELEASE);
|
|
}
|
|
}
|
|
|
|
void onewire_host_write(OneWireHost* host, uint8_t value) {
|
|
uint8_t bitMask;
|
|
|
|
for(bitMask = 0x01; bitMask; bitMask <<= 1) {
|
|
onewire_host_write_bit(host, (bitMask & value) ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
void onewire_host_write_bytes(OneWireHost* host, const uint8_t* buffer, uint16_t count) {
|
|
for(uint16_t i = 0; i < count; ++i) {
|
|
onewire_host_write(host, buffer[i]);
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
void onewire_host_stop(OneWireHost* host) {
|
|
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) {
|
|
host->last_discrepancy = 0;
|
|
host->last_device_flag = false;
|
|
host->last_family_discrepancy = 0;
|
|
for(int i = 7;; i--) {
|
|
host->saved_rom[i] = 0;
|
|
if(i == 0) break;
|
|
}
|
|
}
|
|
|
|
void onewire_host_target_search(OneWireHost* host, uint8_t family_code) {
|
|
host->saved_rom[0] = family_code;
|
|
for(uint8_t i = 1; i < 8; i++) host->saved_rom[i] = 0;
|
|
host->last_discrepancy = 64;
|
|
host->last_family_discrepancy = 0;
|
|
host->last_device_flag = false;
|
|
}
|
|
|
|
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;
|
|
|
|
unsigned char rom_byte_mask, search_direction;
|
|
|
|
// initialize for search
|
|
id_bit_number = 1;
|
|
last_zero = 0;
|
|
rom_byte_number = 0;
|
|
rom_byte_mask = 1;
|
|
search_result = 0;
|
|
|
|
// if the last call was not the last one
|
|
if(!host->last_device_flag) {
|
|
// 1-Wire reset
|
|
if(!onewire_host_reset(host)) {
|
|
// reset the search
|
|
host->last_discrepancy = 0;
|
|
host->last_device_flag = false;
|
|
host->last_family_discrepancy = 0;
|
|
return false;
|
|
}
|
|
|
|
// issue the search command
|
|
switch(mode) {
|
|
case OneWireHostSearchModeConditional:
|
|
onewire_host_write(host, 0xEC);
|
|
break;
|
|
case OneWireHostSearchModeNormal:
|
|
onewire_host_write(host, 0xF0);
|
|
break;
|
|
}
|
|
|
|
// loop to do the search
|
|
do {
|
|
// read a bit and its complement
|
|
id_bit = onewire_host_read_bit(host);
|
|
cmp_id_bit = onewire_host_read_bit(host);
|
|
|
|
// check for no devices on 1-wire
|
|
if((id_bit == 1) && (cmp_id_bit == 1))
|
|
break;
|
|
else {
|
|
// all devices coupled have 0 or 1
|
|
if(id_bit != cmp_id_bit)
|
|
search_direction = id_bit; // bit write value for search
|
|
else {
|
|
// if this discrepancy if before the Last Discrepancy
|
|
// on a previous next then pick the same as last time
|
|
if(id_bit_number < host->last_discrepancy)
|
|
search_direction =
|
|
((host->saved_rom[rom_byte_number] & rom_byte_mask) > 0);
|
|
else
|
|
// if equal to last pick 1, if not then pick 0
|
|
search_direction = (id_bit_number == host->last_discrepancy);
|
|
|
|
// if 0 was picked then record its position in LastZero
|
|
if(search_direction == 0) {
|
|
last_zero = id_bit_number;
|
|
|
|
// check for Last discrepancy in family
|
|
if(last_zero < 9) host->last_family_discrepancy = last_zero;
|
|
}
|
|
}
|
|
|
|
// set or clear the bit in the ROM byte rom_byte_number
|
|
// with mask rom_byte_mask
|
|
if(search_direction == 1)
|
|
host->saved_rom[rom_byte_number] |= rom_byte_mask;
|
|
else
|
|
host->saved_rom[rom_byte_number] &= ~rom_byte_mask;
|
|
|
|
// serial number search direction write bit
|
|
onewire_host_write_bit(host, search_direction);
|
|
|
|
// increment the byte counter id_bit_number
|
|
// and shift the mask rom_byte_mask
|
|
id_bit_number++;
|
|
rom_byte_mask <<= 1;
|
|
|
|
// if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
|
|
if(rom_byte_mask == 0) {
|
|
rom_byte_number++;
|
|
rom_byte_mask = 1;
|
|
}
|
|
}
|
|
} while(rom_byte_number < 8); // loop until through all ROM bytes 0-7
|
|
|
|
// if the search was successful then
|
|
if(!(id_bit_number < 65)) {
|
|
// search successful so set last_Discrepancy, last_device_flag, search_result
|
|
host->last_discrepancy = last_zero;
|
|
|
|
// check for last device
|
|
if(host->last_discrepancy == 0) host->last_device_flag = true;
|
|
|
|
search_result = true;
|
|
}
|
|
}
|
|
|
|
// if no device found then reset counters so next 'search' will be like a first
|
|
if(!search_result || !host->saved_rom[0]) {
|
|
host->last_discrepancy = 0;
|
|
host->last_device_flag = false;
|
|
host->last_family_discrepancy = 0;
|
|
search_result = false;
|
|
} else {
|
|
for(int i = 0; i < 8; i++) new_addr[i] = host->saved_rom[i];
|
|
}
|
|
|
|
return search_result;
|
|
}
|