[FL-2393][FL-2381] iButton, OneWire: move to plain C (#1068)

* iButton: getting started on the worker concept
* Hal delay: added global instructions_per_us variable
* iButton: one wire slave
* iButton: ibutton key setter
* iButton: one wire host, use ibutton_hal
* iButton\RFID: common pulse decoder concept
* iButton: cyfral decoder
* iButton: worker thread concept
* iButton: metakom decoder
* iButton: write key through worker
* iButton: worker mode holder
* iButton: worker improvements
* iButton: Cyfral encoder
* iButton: Metakom encoder
* lib: pulse protocol helpers
* iButton: Metakom decoder
* iButton: Cyfral decoder
* iButton worker: separate modes
* iButton: libs documentation
* HAL: iButton gpio modes
* iButton worker: rename modes file
* iButton worker, hal: move to LL
* iButton CLI: worker for reading and emulation commands
* iButton HAL: correct init and emulation sequence
* iButton cli: moved to plain C
* iButton: move to worker, small step to plain C
* Libs, one wire: move to plain C
* Libs: added forgotten files to compilation
* iButton writer: get rid of manual disable/enable irq
This commit is contained in:
SG
2022-03-29 23:01:56 +10:00
committed by GitHub
parent d15a9500c6
commit bdba15b366
112 changed files with 4444 additions and 3231 deletions

View File

@@ -65,16 +65,6 @@ CFLAGS += -I$(LIB_DIR)/app_scene_template
CFLAGS += -I$(LIB_DIR)/fnv1a-hash
C_SOURCES += $(LIB_DIR)/fnv1a-hash/fnv1a-hash.c
# onewire library
ONEWIRE_DIR = $(LIB_DIR)/onewire
CFLAGS += -I$(ONEWIRE_DIR)
CPP_SOURCES += $(wildcard $(ONEWIRE_DIR)/*.cpp)
# cyfral library
CYFRAL_DIR = $(LIB_DIR)/cyfral
CFLAGS += -I$(CYFRAL_DIR)
CPP_SOURCES += $(wildcard $(CYFRAL_DIR)/*.cpp)
# common apps api
CFLAGS += -I$(LIB_DIR)/common-api
@@ -128,3 +118,8 @@ C_SOURCES += $(wildcard $(LIB_DIR)/flipper_format/*.c)
# Micro-ECC
CFLAGS += -I$(LIB_DIR)/micro-ecc
C_SOURCES += $(wildcard $(LIB_DIR)/micro-ecc/*.c)
# iButton and OneWire
C_SOURCES += $(wildcard $(LIB_DIR)/one_wire/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/one_wire/*/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/one_wire/*/*/*.c)

View File

@@ -0,0 +1,126 @@
#include "encoder_cyfral.h"
#include <furi_hal.h>
#define CYFRAL_DATA_SIZE sizeof(uint16_t)
#define CYFRAL_PERIOD (125 * instructions_per_us)
#define CYFRAL_0_LOW (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_0_HI (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_LOW (CYFRAL_PERIOD * 0.33f)
#define CYFRAL_1_HI (CYFRAL_PERIOD * 0.66f)
#define CYFRAL_SET_DATA(level, len) \
*polarity = level; \
*length = len;
struct EncoderCyfral {
uint32_t data;
uint32_t index;
};
EncoderCyfral* encoder_cyfral_alloc() {
EncoderCyfral* cyfral = malloc(sizeof(EncoderCyfral));
encoder_cyfral_reset(cyfral);
return cyfral;
}
void encoder_cyfral_free(EncoderCyfral* cyfral) {
free(cyfral);
}
void encoder_cyfral_reset(EncoderCyfral* cyfral) {
cyfral->data = 0;
cyfral->index = 0;
}
uint32_t cyfral_encoder_encode(const uint16_t data) {
uint32_t value = 0;
for(int8_t i = 0; i <= 7; i++) {
switch((data >> (i * 2)) & 0b00000011) {
case 0b11:
value = value << 4;
value += 0b00000111;
break;
case 0b10:
value = value << 4;
value += 0b00001011;
break;
case 0b01:
value = value << 4;
value += 0b00001101;
break;
case 0b00:
value = value << 4;
value += 0b00001110;
break;
default:
break;
}
}
return value;
}
void encoder_cyfral_set_data(EncoderCyfral* cyfral, const uint8_t* data, size_t data_size) {
furi_assert(cyfral);
furi_check(data_size >= CYFRAL_DATA_SIZE);
uint16_t intermediate;
memcpy(&intermediate, data, CYFRAL_DATA_SIZE);
cyfral->data = cyfral_encoder_encode(intermediate);
}
void encoder_cyfral_get_pulse(EncoderCyfral* cyfral, bool* polarity, uint32_t* length) {
if(cyfral->index < 8) {
// start word (0b0001)
switch(cyfral->index) {
case 0:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 1:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 2:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 3:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 4:
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
break;
case 5:
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
break;
case 6:
CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
break;
case 7:
CYFRAL_SET_DATA(true, CYFRAL_1_HI);
break;
}
} else {
// data
uint8_t data_start_index = cyfral->index - 8;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = ((cyfral->data >> bit_index) & 1);
if(!clock_polarity) {
if(bit_value) {
CYFRAL_SET_DATA(false, CYFRAL_1_LOW);
} else {
CYFRAL_SET_DATA(false, CYFRAL_0_LOW);
}
} else {
if(bit_value) {
CYFRAL_SET_DATA(true, CYFRAL_1_HI);
} else {
CYFRAL_SET_DATA(true, CYFRAL_0_HI);
}
}
}
cyfral->index++;
if(cyfral->index >= (9 * 4 * 2)) {
cyfral->index = 0;
}
}

View File

@@ -0,0 +1,54 @@
/**
* @file encoder_cyfral.h
*
* Cyfral pulse format encoder
*/
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct EncoderCyfral EncoderCyfral;
/**
* Allocate Cyfral encoder
* @return EncoderCyfral*
*/
EncoderCyfral* encoder_cyfral_alloc();
/**
* Deallocate Cyfral encoder
* @param cyfral
*/
void encoder_cyfral_free(EncoderCyfral* cyfral);
/**
* Reset Cyfral encoder
* @param cyfral
*/
void encoder_cyfral_reset(EncoderCyfral* cyfral);
/**
* Set data to be encoded to Cyfral pulse format, 2 bytes
* @param cyfral
* @param data
* @param data_size
*/
void encoder_cyfral_set_data(EncoderCyfral* cyfral, const uint8_t* data, size_t data_size);
/**
* Pop pulse from Cyfral encoder
* @param cyfral
* @param polarity
* @param length
*/
void encoder_cyfral_get_pulse(EncoderCyfral* cyfral, bool* polarity, uint32_t* length);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,93 @@
#include "encoder_metakom.h"
#include <furi_hal.h>
#define METAKOM_DATA_SIZE sizeof(uint32_t)
#define METAKOM_PERIOD (125 * instructions_per_us)
#define METAKOM_0_LOW (METAKOM_PERIOD * 0.33f)
#define METAKOM_0_HI (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_LOW (METAKOM_PERIOD * 0.66f)
#define METAKOM_1_HI (METAKOM_PERIOD * 0.33f)
#define METAKOM_SET_DATA(level, len) \
*polarity = !level; \
*length = len;
struct EncoderMetakom {
uint32_t data;
uint32_t index;
};
EncoderMetakom* encoder_metakom_alloc() {
EncoderMetakom* metakom = malloc(sizeof(EncoderMetakom));
encoder_metakom_reset(metakom);
return metakom;
}
void encoder_metakom_free(EncoderMetakom* metakom) {
free(metakom);
}
void encoder_metakom_reset(EncoderMetakom* metakom) {
metakom->data = 0;
metakom->index = 0;
}
void encoder_metakom_set_data(EncoderMetakom* metakom, const uint8_t* data, size_t data_size) {
furi_assert(metakom);
furi_check(data_size >= METAKOM_DATA_SIZE);
memcpy(&metakom->data, data, METAKOM_DATA_SIZE);
}
void encoder_metakom_get_pulse(EncoderMetakom* metakom, bool* polarity, uint32_t* length) {
if(metakom->index == 0) {
// sync bit
METAKOM_SET_DATA(true, METAKOM_PERIOD);
} else if(metakom->index >= 1 && metakom->index <= 6) {
// start word (0b010)
switch(metakom->index) {
case 1:
METAKOM_SET_DATA(false, METAKOM_0_LOW);
break;
case 2:
METAKOM_SET_DATA(true, METAKOM_0_HI);
break;
case 3:
METAKOM_SET_DATA(false, METAKOM_1_LOW);
break;
case 4:
METAKOM_SET_DATA(true, METAKOM_1_HI);
break;
case 5:
METAKOM_SET_DATA(false, METAKOM_0_LOW);
break;
case 6:
METAKOM_SET_DATA(true, METAKOM_0_HI);
break;
}
} else {
// data
uint8_t data_start_index = metakom->index - 7;
bool clock_polarity = (data_start_index) % 2;
uint8_t bit_index = (data_start_index) / 2;
bool bit_value = (metakom->data >> (32 - 1 - bit_index)) & 1;
if(!clock_polarity) {
if(bit_value) {
METAKOM_SET_DATA(false, METAKOM_1_LOW);
} else {
METAKOM_SET_DATA(false, METAKOM_0_LOW);
}
} else {
if(bit_value) {
METAKOM_SET_DATA(true, METAKOM_1_HI);
} else {
METAKOM_SET_DATA(true, METAKOM_0_HI);
}
}
}
metakom->index++;
if(metakom->index >= (1 + 3 * 2 + 32 * 2)) {
metakom->index = 0;
}
}

View File

@@ -0,0 +1,54 @@
/**
* @file encoder_metakom.h
*
* Metakom pulse format encoder
*/
#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct EncoderMetakom EncoderMetakom;
/**
* Allocate Metakom encoder
* @return EncoderMetakom*
*/
EncoderMetakom* encoder_metakom_alloc();
/**
* Deallocate Metakom encoder
* @param metakom
*/
void encoder_metakom_free(EncoderMetakom* metakom);
/**
* Reset Metakom encoder
* @param metakom
*/
void encoder_metakom_reset(EncoderMetakom* metakom);
/**
* Set data to be encoded to Metakom pulse format, 4 bytes
* @param metakom
* @param data
* @param data_size
*/
void encoder_metakom_set_data(EncoderMetakom* metakom, const uint8_t* data, size_t data_size);
/**
* Pop pulse from Metakom encoder
* @param cyfral
* @param polarity
* @param length
*/
void encoder_metakom_get_pulse(EncoderMetakom* metakom, bool* polarity, uint32_t* length);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,121 @@
#include <furi.h>
#include <one_wire/maxim_crc.h>
#include "ibutton_key.h"
struct iButtonKey {
uint8_t data[IBUTTON_KEY_DATA_SIZE];
char name[IBUTTON_KEY_NAME_SIZE];
iButtonKeyType type;
};
iButtonKey* ibutton_key_alloc() {
iButtonKey* key = malloc(sizeof(iButtonKey));
memset(key, 0, sizeof(iButtonKey));
return key;
}
void ibutton_key_free(iButtonKey* key) {
free(key);
}
void ibutton_key_set(iButtonKey* to, const iButtonKey* from) {
memcpy(to, from, sizeof(iButtonKey));
}
void ibutton_key_set_data(iButtonKey* key, uint8_t* data, uint8_t data_count) {
furi_check(data_count > 0);
furi_check(data_count <= IBUTTON_KEY_DATA_SIZE);
memset(key->data, 0, IBUTTON_KEY_DATA_SIZE);
memcpy(key->data, data, data_count);
}
void ibutton_key_clear_data(iButtonKey* key) {
memset(key->data, 0, IBUTTON_KEY_DATA_SIZE);
}
const uint8_t* ibutton_key_get_data_p(iButtonKey* key) {
return key->data;
}
uint8_t ibutton_key_get_data_size(iButtonKey* key) {
return ibutton_key_get_size_by_type(key->type);
}
void ibutton_key_set_name(iButtonKey* key, const char* name) {
strlcpy(key->name, name, IBUTTON_KEY_NAME_SIZE);
}
const char* ibutton_key_get_name_p(iButtonKey* key) {
return key->name;
}
void ibutton_key_set_type(iButtonKey* key, iButtonKeyType key_type) {
key->type = key_type;
}
iButtonKeyType ibutton_key_get_type(iButtonKey* key) {
return key->type;
}
const char* ibutton_key_get_string_by_type(iButtonKeyType key_type) {
switch(key_type) {
case iButtonKeyCyfral:
return "Cyfral";
break;
case iButtonKeyMetakom:
return "Metakom";
break;
case iButtonKeyDS1990:
return "Dallas";
break;
default:
furi_crash("Invalid iButton type");
return "";
break;
}
}
bool ibutton_key_get_type_by_string(const char* type_string, iButtonKeyType* key_type) {
if(strcmp(type_string, ibutton_key_get_string_by_type(iButtonKeyCyfral)) == 0) {
*key_type = iButtonKeyCyfral;
} else if(strcmp(type_string, ibutton_key_get_string_by_type(iButtonKeyMetakom)) == 0) {
*key_type = iButtonKeyMetakom;
} else if(strcmp(type_string, ibutton_key_get_string_by_type(iButtonKeyDS1990)) == 0) {
*key_type = iButtonKeyDS1990;
} else {
return false;
}
return true;
}
uint8_t ibutton_key_get_size_by_type(iButtonKeyType key_type) {
uint8_t size = 0;
switch(key_type) {
case iButtonKeyCyfral:
size = 2;
break;
case iButtonKeyMetakom:
size = 4;
break;
case iButtonKeyDS1990:
size = 8;
break;
}
return size;
}
uint8_t ibutton_key_get_max_size() {
return IBUTTON_KEY_DATA_SIZE;
}
bool ibutton_key_dallas_crc_is_valid(iButtonKey* key) {
return (maxim_crc8(key->data, 8, MAXIM_CRC8_INIT) == 0);
}
bool ibutton_key_dallas_is_1990_key(iButtonKey* key) {
return (key->data[0] == 0x01);
}

View File

@@ -0,0 +1,145 @@
/**
* @file ibutton_key.h
*
* iButton key data holder
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#define IBUTTON_KEY_DATA_SIZE 8
#define IBUTTON_KEY_NAME_SIZE 22
typedef enum {
iButtonKeyDS1990,
iButtonKeyCyfral,
iButtonKeyMetakom,
} iButtonKeyType;
typedef struct iButtonKey iButtonKey;
/**
* Allocate key
* @return iButtonKey*
*/
iButtonKey* ibutton_key_alloc();
/**
* Free key
* @param key
*/
void ibutton_key_free(iButtonKey* key);
/**
* Copy key
* @param to
* @param from
*/
void ibutton_key_set(iButtonKey* to, const iButtonKey* from);
/**
* Set key data
* @param key
* @param data
* @param data_count
*/
void ibutton_key_set_data(iButtonKey* key, uint8_t* data, uint8_t data_count);
/**
* Clear key data
* @param key
*/
void ibutton_key_clear_data(iButtonKey* key);
/**
* Get pointer to key data
* @param key
* @return const uint8_t*
*/
const uint8_t* ibutton_key_get_data_p(iButtonKey* key);
/**
* Get key data size
* @param key
* @return uint8_t
*/
uint8_t ibutton_key_get_data_size(iButtonKey* key);
/**
* Set key name
* @param key
* @param name
*/
void ibutton_key_set_name(iButtonKey* key, const char* name);
/**
* Get pointer to key name
* @param key
* @return const char*
*/
const char* ibutton_key_get_name_p(iButtonKey* key);
/**
* Set key type
* @param key
* @param key_type
*/
void ibutton_key_set_type(iButtonKey* key, iButtonKeyType key_type);
/**
* Get key type
* @param key
* @return iButtonKeyType
*/
iButtonKeyType ibutton_key_get_type(iButtonKey* key);
/**
* Get type string from key type
* @param key_type
* @return const char*
*/
const char* ibutton_key_get_string_by_type(iButtonKeyType key_type);
/**
* Get key type from string
* @param type_string
* @param key_type
* @return bool
*/
bool ibutton_key_get_type_by_string(const char* type_string, iButtonKeyType* key_type);
/**
* Get key data size from type
* @param key_type
* @return uint8_t
*/
uint8_t ibutton_key_get_size_by_type(iButtonKeyType key_type);
/**
* Get max key size
* @return uint8_t
*/
uint8_t ibutton_key_get_max_size();
/**
* Check if CRC for onewire key is valid
* @param key
* @return true
* @return false
*/
bool ibutton_key_dallas_crc_is_valid(iButtonKey* key);
/**
* Check if onewire key is a DS1990 key
* @param key
* @return true
* @return false
*/
bool ibutton_key_dallas_is_1990_key(iButtonKey* key);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,28 @@
/**
* @file ibutton_key_command.h
*
* List of misc commands for Dallas and blanks
*/
#pragma once
#define RW1990_1_CMD_WRITE_RECORD_FLAG 0xD1
#define RW1990_1_CMD_READ_RECORD_FLAG 0xB5
#define RW1990_1_CMD_WRITE_ROM 0xD5
#define RW1990_2_CMD_WRITE_RECORD_FLAG 0x1D
#define RW1990_2_CMD_READ_RECORD_FLAG 0x1E
#define RW1990_2_CMD_WRITE_ROM 0xD5
#define TM2004_CMD_READ_STATUS 0xAA
#define TM2004_CMD_READ_MEMORY 0xF0
#define TM2004_CMD_WRITE_ROM 0x3C
#define TM2004_CMD_FINALIZATION 0x35
#define TM2004_ANSWER_READ_MEMORY 0xF5
#define TM01_CMD_WRITE_RECORD_FLAG 0xC1
#define TM01_CMD_WRITE_ROM 0xC5
#define TM01_CMD_SWITCH_TO_CYFRAL 0xCA
#define TM01_CMD_SWITCH_TO_METAKOM 0xCB
#define DS1990_CMD_READ_ROM 0x33

View File

@@ -0,0 +1,197 @@
#include <furi.h>
#include <furi_hal.h>
#include <atomic.h>
#include "ibutton_worker_i.h"
typedef enum {
iButtonMessageEnd,
iButtonMessageStop,
iButtonMessageRead,
iButtonMessageWrite,
iButtonMessageEmulate,
} iButtonMessageType;
typedef struct {
iButtonMessageType type;
union {
iButtonKey* key;
} data;
} iButtonMessage;
static int32_t ibutton_worker_thread(void* thread_context);
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->writer = ibutton_writer_alloc(worker->host);
worker->device = onewire_device_alloc(0, 0, 0, 0, 0, 0, 0, 0);
worker->pulse_decoder = pulse_decoder_alloc();
worker->protocol_cyfral = protocol_cyfral_alloc();
worker->protocol_metakom = protocol_metakom_alloc();
worker->messages = osMessageQueueNew(1, sizeof(iButtonMessage), NULL);
worker->mode_index = iButtonWorkerIdle;
worker->last_dwt_value = 0;
worker->read_cb = NULL;
worker->write_cb = NULL;
worker->emulate_cb = NULL;
worker->cb_ctx = NULL;
worker->encoder_cyfral = encoder_cyfral_alloc();
worker->encoder_metakom = encoder_metakom_alloc();
worker->thread = furi_thread_alloc();
furi_thread_set_name(worker->thread, "ibutton_worker");
furi_thread_set_callback(worker->thread, ibutton_worker_thread);
furi_thread_set_context(worker->thread, worker);
furi_thread_set_stack_size(worker->thread, 2048);
pulse_decoder_add_protocol(
worker->pulse_decoder,
protocol_cyfral_get_protocol(worker->protocol_cyfral),
PulseProtocolCyfral);
pulse_decoder_add_protocol(
worker->pulse_decoder,
protocol_metakom_get_protocol(worker->protocol_metakom),
PulseProtocolMetakom);
return worker;
}
void ibutton_worker_read_set_callback(
iButtonWorker* worker,
iButtonWorkerReadCallback callback,
void* context) {
furi_check(worker->mode_index == iButtonWorkerIdle);
worker->read_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_write_set_callback(
iButtonWorker* worker,
iButtonWorkerWriteCallback callback,
void* context) {
furi_check(worker->mode_index == iButtonWorkerIdle);
worker->write_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_emulate_set_callback(
iButtonWorker* worker,
iButtonWorkerEmulateCallback callback,
void* context) {
furi_check(worker->mode_index == iButtonWorkerIdle);
worker->emulate_cb = callback;
worker->cb_ctx = context;
}
void ibutton_worker_read_start(iButtonWorker* worker, iButtonKey* key) {
iButtonMessage message = {.type = iButtonMessageRead, .data.key = key};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_write_start(iButtonWorker* worker, iButtonKey* key) {
iButtonMessage message = {.type = iButtonMessageWrite, .data.key = key};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_emulate_start(iButtonWorker* worker, iButtonKey* key) {
iButtonMessage message = {.type = iButtonMessageEmulate, .data.key = key};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_stop(iButtonWorker* worker) {
iButtonMessage message = {.type = iButtonMessageStop};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
}
void ibutton_worker_free(iButtonWorker* worker) {
pulse_decoder_free(worker->pulse_decoder);
protocol_metakom_free(worker->protocol_metakom);
protocol_cyfral_free(worker->protocol_cyfral);
ibutton_writer_free(worker->writer);
onewire_slave_free(worker->slave);
onewire_host_free(worker->host);
onewire_device_free(worker->device);
encoder_cyfral_free(worker->encoder_cyfral);
encoder_metakom_free(worker->encoder_metakom);
osMessageQueueDelete(worker->messages);
furi_thread_free(worker->thread);
free(worker->key_data);
free(worker);
}
void ibutton_worker_start_thread(iButtonWorker* worker) {
furi_thread_start(worker->thread);
}
void ibutton_worker_stop_thread(iButtonWorker* worker) {
iButtonMessage message = {.type = iButtonMessageEnd};
furi_check(osMessageQueuePut(worker->messages, &message, 0, osWaitForever) == osOK);
furi_thread_join(worker->thread);
}
void ibutton_worker_switch_mode(iButtonWorker* worker, iButtonWorkerMode mode) {
ibutton_worker_modes[worker->mode_index].stop(worker);
worker->mode_index = mode;
ibutton_worker_modes[worker->mode_index].start(worker);
}
void ibutton_worker_set_key_p(iButtonWorker* worker, iButtonKey* key) {
worker->key_p = key;
}
static int32_t ibutton_worker_thread(void* thread_context) {
iButtonWorker* worker = thread_context;
bool running = true;
iButtonMessage message;
osStatus_t status;
ibutton_worker_modes[worker->mode_index].start(worker);
while(running) {
status = osMessageQueueGet(
worker->messages, &message, NULL, ibutton_worker_modes[worker->mode_index].quant);
if(status == osOK) {
switch(message.type) {
case iButtonMessageEnd:
ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
ibutton_worker_set_key_p(worker, NULL);
running = false;
break;
case iButtonMessageStop:
ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
ibutton_worker_set_key_p(worker, NULL);
break;
case iButtonMessageRead:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerRead);
break;
case iButtonMessageWrite:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerWrite);
break;
case iButtonMessageEmulate:
ibutton_worker_set_key_p(worker, message.data.key);
ibutton_worker_switch_mode(worker, iButtonWorkerEmulate);
break;
}
} else if(status == osErrorTimeout) {
ibutton_worker_modes[worker->mode_index].tick(worker);
} else {
furi_crash("iButton worker error");
}
}
ibutton_worker_modes[worker->mode_index].stop(worker);
return 0;
}

View File

@@ -0,0 +1,113 @@
/**
* @file ibutton_worker.h
*
* iButton worker
*/
#pragma once
#include "ibutton_key.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
iButtonWorkerWriteOK,
iButtonWorkerWriteSameKey,
iButtonWorkerWriteNoDetect,
iButtonWorkerWriteCannotWrite,
} iButtonWorkerWriteResult;
typedef void (*iButtonWorkerReadCallback)(void* context);
typedef void (*iButtonWorkerWriteCallback)(void* context, iButtonWorkerWriteResult result);
typedef void (*iButtonWorkerEmulateCallback)(void* context, bool emulated);
typedef struct iButtonWorker iButtonWorker;
/**
* Allocate ibutton worker
* @return iButtonWorker*
*/
iButtonWorker* ibutton_worker_alloc();
/**
* Free ibutton worker
* @param worker
*/
void ibutton_worker_free(iButtonWorker* worker);
/**
* Start ibutton worker thread
* @param worker
*/
void ibutton_worker_start_thread(iButtonWorker* worker);
/**
* Stop ibutton worker thread
* @param worker
*/
void ibutton_worker_stop_thread(iButtonWorker* worker);
/**
* Set "read success" callback
* @param worker
* @param callback
* @param context
*/
void ibutton_worker_read_set_callback(
iButtonWorker* worker,
iButtonWorkerReadCallback callback,
void* context);
/**
* Start read mode
* @param worker
* @param key
*/
void ibutton_worker_read_start(iButtonWorker* worker, iButtonKey* key);
/**
* Set "write event" callback
* @param worker
* @param callback
* @param context
*/
void ibutton_worker_write_set_callback(
iButtonWorker* worker,
iButtonWorkerWriteCallback callback,
void* context);
/**
* Start write mode
* @param worker
* @param key
*/
void ibutton_worker_write_start(iButtonWorker* worker, iButtonKey* key);
/**
* Set "emulate success" callback
* @param worker
* @param callback
* @param context
*/
void ibutton_worker_emulate_set_callback(
iButtonWorker* worker,
iButtonWorkerEmulateCallback callback,
void* context);
/**
* Start emulate mode
* @param worker
* @param key
*/
void ibutton_worker_emulate_start(iButtonWorker* worker, iButtonKey* key);
/**
* Stop all modes
* @param worker
*/
void ibutton_worker_stop(iButtonWorker* worker);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,79 @@
/**
* @file ibutton_worker_i.h
*
* iButton worker, internal definitions
*/
#pragma once
#include "ibutton_worker.h"
#include "ibutton_writer.h"
#include "../one_wire_host.h"
#include "../one_wire_slave.h"
#include "../one_wire_device.h"
#include "../pulse_protocols/pulse_decoder.h"
#include "pulse_protocols/protocol_cyfral.h"
#include "pulse_protocols/protocol_metakom.h"
#include "encoder/encoder_cyfral.h"
#include "encoder/encoder_metakom.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
PulseProtocolCyfral,
PulseProtocolMetakom,
} PulseProtocols;
typedef struct {
const uint32_t quant;
const void (*start)(iButtonWorker* worker);
const void (*tick)(iButtonWorker* worker);
const void (*stop)(iButtonWorker* worker);
} iButtonWorkerModeType;
typedef enum {
iButtonWorkerIdle = 0,
iButtonWorkerRead = 1,
iButtonWorkerWrite = 2,
iButtonWorkerEmulate = 3,
} iButtonWorkerMode;
typedef enum {
iButtonEmulateModeCyfral,
iButtonEmulateModeMetakom,
} iButtonEmulateMode;
struct iButtonWorker {
iButtonKey* key_p;
uint8_t* key_data;
OneWireHost* host;
OneWireSlave* slave;
OneWireDevice* device;
iButtonWriter* writer;
iButtonWorkerMode mode_index;
osMessageQueueId_t messages;
FuriThread* thread;
PulseDecoder* pulse_decoder;
ProtocolCyfral* protocol_cyfral;
ProtocolMetakom* protocol_metakom;
uint32_t last_dwt_value;
iButtonWorkerReadCallback read_cb;
iButtonWorkerWriteCallback write_cb;
iButtonWorkerEmulateCallback emulate_cb;
void* cb_ctx;
EncoderCyfral* encoder_cyfral;
EncoderMetakom* encoder_metakom;
iButtonEmulateMode emulate_mode;
};
extern const iButtonWorkerModeType ibutton_worker_modes[];
void ibutton_worker_switch_mode(iButtonWorker* worker, iButtonWorkerMode mode);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,330 @@
#include <furi.h>
#include <furi_hal.h>
#include "ibutton_worker_i.h"
#include "ibutton_key_command.h"
void ibutton_worker_mode_idle_start(iButtonWorker* worker);
void ibutton_worker_mode_idle_tick(iButtonWorker* worker);
void ibutton_worker_mode_idle_stop(iButtonWorker* worker);
void ibutton_worker_mode_emulate_start(iButtonWorker* worker);
void ibutton_worker_mode_emulate_tick(iButtonWorker* worker);
void ibutton_worker_mode_emulate_stop(iButtonWorker* worker);
void ibutton_worker_mode_read_start(iButtonWorker* worker);
void ibutton_worker_mode_read_tick(iButtonWorker* worker);
void ibutton_worker_mode_read_stop(iButtonWorker* worker);
void ibutton_worker_mode_write_start(iButtonWorker* worker);
void ibutton_worker_mode_write_tick(iButtonWorker* worker);
void ibutton_worker_mode_write_stop(iButtonWorker* worker);
const iButtonWorkerModeType ibutton_worker_modes[] = {
{
.quant = osWaitForever,
.start = ibutton_worker_mode_idle_start,
.tick = ibutton_worker_mode_idle_tick,
.stop = ibutton_worker_mode_idle_stop,
},
{
.quant = 100,
.start = ibutton_worker_mode_read_start,
.tick = ibutton_worker_mode_read_tick,
.stop = ibutton_worker_mode_read_stop,
},
{
.quant = 1000,
.start = ibutton_worker_mode_write_start,
.tick = ibutton_worker_mode_write_tick,
.stop = ibutton_worker_mode_write_stop,
},
{
.quant = 1000,
.start = ibutton_worker_mode_emulate_start,
.tick = ibutton_worker_mode_emulate_tick,
.stop = ibutton_worker_mode_emulate_stop,
},
};
/*********************** IDLE ***********************/
void ibutton_worker_mode_idle_start(iButtonWorker* worker) {
}
void ibutton_worker_mode_idle_tick(iButtonWorker* worker) {
}
void ibutton_worker_mode_idle_stop(iButtonWorker* worker) {
}
/*********************** READ ***********************/
void ibutton_worker_comparator_callback(bool level, void* context) {
iButtonWorker* worker = context;
uint32_t current_dwt_value = DWT->CYCCNT;
pulse_decoder_process_pulse(
worker->pulse_decoder, level, current_dwt_value - worker->last_dwt_value);
worker->last_dwt_value = current_dwt_value;
}
bool ibutton_worker_read_comparator(iButtonWorker* worker) {
bool result = false;
pulse_decoder_reset(worker->pulse_decoder);
furi_hal_rfid_pins_reset();
// pulldown pull pin, we sense the signal through the analog part of the RFID schematic
furi_hal_rfid_pin_pull_pulldown();
furi_hal_rfid_comp_set_callback(ibutton_worker_comparator_callback, worker);
worker->last_dwt_value = DWT->CYCCNT;
furi_hal_rfid_comp_start();
// TODO: rework with thread events, "pulse_decoder_get_decoded_index_with_timeout"
delay(100);
int32_t decoded_index = pulse_decoder_get_decoded_index(worker->pulse_decoder);
if(decoded_index >= 0) {
pulse_decoder_get_data(
worker->pulse_decoder, decoded_index, worker->key_data, ibutton_key_get_max_size());
}
switch(decoded_index) {
case PulseProtocolCyfral:
furi_check(worker->key_p != NULL);
ibutton_key_set_type(worker->key_p, iButtonKeyCyfral);
ibutton_key_set_data(worker->key_p, worker->key_data, ibutton_key_get_max_size());
result = true;
break;
case PulseProtocolMetakom:
furi_check(worker->key_p != NULL);
ibutton_key_set_type(worker->key_p, iButtonKeyMetakom);
ibutton_key_set_data(worker->key_p, worker->key_data, ibutton_key_get_max_size());
result = true;
break;
break;
default:
break;
}
furi_hal_rfid_comp_stop();
furi_hal_rfid_comp_set_callback(NULL, NULL);
furi_hal_rfid_pins_reset();
return result;
}
bool ibutton_worker_read_dallas(iButtonWorker* worker) {
bool result = false;
onewire_host_start(worker->host);
delay(100);
FURI_CRITICAL_ENTER();
if(onewire_host_search(worker->host, worker->key_data, NORMAL_SEARCH)) {
onewire_host_reset_search(worker->host);
// key found, verify
if(onewire_host_reset(worker->host)) {
onewire_host_write(worker->host, DS1990_CMD_READ_ROM);
bool key_valid = true;
for(uint8_t i = 0; i < ibutton_key_get_max_size(); i++) {
if(onewire_host_read(worker->host) != worker->key_data[i]) {
key_valid = false;
break;
}
}
if(key_valid) {
result = true;
furi_check(worker->key_p != NULL);
ibutton_key_set_type(worker->key_p, iButtonKeyDS1990);
ibutton_key_set_data(worker->key_p, worker->key_data, ibutton_key_get_max_size());
}
}
} else {
onewire_host_reset_search(worker->host);
}
onewire_host_stop(worker->host);
FURI_CRITICAL_EXIT();
return result;
}
void ibutton_worker_mode_read_start(iButtonWorker* worker) {
furi_hal_power_enable_otg();
}
void ibutton_worker_mode_read_tick(iButtonWorker* worker) {
bool valid = false;
if(ibutton_worker_read_dallas(worker)) {
valid = true;
} else if(ibutton_worker_read_comparator(worker)) {
valid = true;
}
if(valid) {
if(worker->read_cb != NULL) {
worker->read_cb(worker->cb_ctx);
}
ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
}
}
void ibutton_worker_mode_read_stop(iButtonWorker* worker) {
furi_hal_power_disable_otg();
}
/*********************** EMULATE ***********************/
static void onewire_slave_callback(void* context) {
furi_assert(context);
iButtonWorker* worker = context;
if(worker->emulate_cb != NULL) {
worker->emulate_cb(worker->cb_ctx, true);
}
}
void ibutton_worker_emulate_dallas_start(iButtonWorker* worker) {
uint8_t* device_id = onewire_device_get_id_p(worker->device);
const uint8_t* key_id = ibutton_key_get_data_p(worker->key_p);
const uint8_t key_size = ibutton_key_get_max_size();
memcpy(device_id, key_id, key_size);
onewire_slave_attach(worker->slave, worker->device);
onewire_slave_start(worker->slave);
onewire_slave_set_result_callback(worker->slave, onewire_slave_callback, worker);
}
void ibutton_worker_emulate_dallas_stop(iButtonWorker* worker) {
onewire_slave_stop(worker->slave);
onewire_slave_detach(worker->slave);
}
void ibutton_worker_emulate_timer_cb(void* context) {
furi_assert(context);
iButtonWorker* worker = context;
bool polarity;
uint32_t length;
switch(worker->emulate_mode) {
case iButtonEmulateModeCyfral:
encoder_cyfral_get_pulse(worker->encoder_cyfral, &polarity, &length);
break;
case iButtonEmulateModeMetakom:
encoder_metakom_get_pulse(worker->encoder_metakom, &polarity, &length);
break;
}
furi_hal_ibutton_emulate_set_next(length);
if(polarity) {
furi_hal_ibutton_pin_high();
} else {
furi_hal_ibutton_pin_low();
}
}
void ibutton_worker_emulate_timer_start(iButtonWorker* worker) {
furi_assert(worker->key_p);
const uint8_t* key_id = ibutton_key_get_data_p(worker->key_p);
const uint8_t key_size = ibutton_key_get_max_size();
switch(ibutton_key_get_type(worker->key_p)) {
case iButtonKeyDS1990:
return;
break;
case iButtonKeyCyfral:
worker->emulate_mode = iButtonEmulateModeCyfral;
encoder_cyfral_reset(worker->encoder_cyfral);
encoder_cyfral_set_data(worker->encoder_cyfral, key_id, key_size);
break;
case iButtonKeyMetakom:
worker->emulate_mode = iButtonEmulateModeMetakom;
encoder_metakom_reset(worker->encoder_metakom);
encoder_metakom_set_data(worker->encoder_metakom, key_id, key_size);
break;
}
furi_hal_ibutton_start_drive();
furi_hal_ibutton_emulate_start(0, ibutton_worker_emulate_timer_cb, worker);
}
void ibutton_worker_emulate_timer_stop(iButtonWorker* worker) {
furi_hal_ibutton_emulate_stop();
}
void ibutton_worker_mode_emulate_start(iButtonWorker* worker) {
furi_assert(worker->key_p);
furi_hal_rfid_pins_reset();
furi_hal_rfid_pin_pull_pulldown();
switch(ibutton_key_get_type(worker->key_p)) {
case iButtonKeyDS1990:
ibutton_worker_emulate_dallas_start(worker);
break;
case iButtonKeyCyfral:
case iButtonKeyMetakom:
ibutton_worker_emulate_timer_start(worker);
break;
}
}
void ibutton_worker_mode_emulate_tick(iButtonWorker* worker) {
}
void ibutton_worker_mode_emulate_stop(iButtonWorker* worker) {
furi_assert(worker->key_p);
furi_hal_rfid_pins_reset();
switch(ibutton_key_get_type(worker->key_p)) {
case iButtonKeyDS1990:
ibutton_worker_emulate_dallas_stop(worker);
break;
case iButtonKeyCyfral:
case iButtonKeyMetakom:
ibutton_worker_emulate_timer_stop(worker);
break;
}
}
/*********************** WRITE ***********************/
void ibutton_worker_mode_write_start(iButtonWorker* worker) {
furi_hal_power_enable_otg();
onewire_host_start(worker->host);
}
void ibutton_worker_mode_write_tick(iButtonWorker* worker) {
furi_check(worker->key_p != NULL);
iButtonWriterResult writer_result = ibutton_writer_write(worker->writer, worker->key_p);
iButtonWorkerWriteResult result;
switch(writer_result) {
case iButtonWriterOK:
result = iButtonWorkerWriteOK;
break;
case iButtonWriterSameKey:
result = iButtonWorkerWriteSameKey;
break;
case iButtonWriterNoDetect:
result = iButtonWorkerWriteNoDetect;
break;
case iButtonWriterCannotWrite:
result = iButtonWorkerWriteCannotWrite;
break;
default:
result = iButtonWorkerWriteNoDetect;
break;
}
if(worker->write_cb != NULL) {
worker->write_cb(worker->cb_ctx, result);
}
}
void ibutton_worker_mode_write_stop(iButtonWorker* worker) {
furi_hal_power_disable_otg();
onewire_host_stop(worker->host);
}

View File

@@ -0,0 +1,298 @@
#include <furi.h>
#include <furi_hal.h>
#include "ibutton_writer.h"
#include "ibutton_key_command.h"
/*********************** PRIVATE ***********************/
struct iButtonWriter {
OneWireHost* host;
};
static void writer_write_one_bit(iButtonWriter* writer, bool value, uint32_t delay) {
onewire_host_write_bit(writer->host, value);
delay_us(delay);
}
static void writer_write_byte_ds1990(iButtonWriter* writer, uint8_t data) {
for(uint8_t n_bit = 0; n_bit < 8; n_bit++) {
onewire_host_write_bit(writer->host, data & 1);
delay_us(5000);
data = data >> 1;
}
}
static bool writer_compare_key_ds1990(iButtonWriter* writer, iButtonKey* key) {
bool result = false;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
bool presence = onewire_host_reset(writer->host);
if(presence) {
onewire_host_write(writer->host, DS1990_CMD_READ_ROM);
result = true;
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
if(ibutton_key_get_data_p(key)[i] != onewire_host_read(writer->host)) {
result = false;
break;
}
}
}
FURI_CRITICAL_EXIT();
}
return result;
}
static bool writer_write_TM2004(iButtonWriter* writer, iButtonKey* key) {
uint8_t answer;
bool result = true;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
// write rom, addr is 0x0000
onewire_host_reset(writer->host);
onewire_host_write(writer->host, TM2004_CMD_WRITE_ROM);
onewire_host_write(writer->host, 0x00);
onewire_host_write(writer->host, 0x00);
// write key
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
// write key byte
onewire_host_write(writer->host, ibutton_key_get_data_p(key)[i]);
answer = onewire_host_read(writer->host);
// TODO: check answer CRC
// pulse indicating that data is correct
delay_us(600);
writer_write_one_bit(writer, 1, 50000);
// read writed key byte
answer = onewire_host_read(writer->host);
// check that writed and readed are same
if(ibutton_key_get_data_p(key)[i] != answer) {
result = false;
break;
}
}
if(!writer_compare_key_ds1990(writer, key)) {
result = false;
}
onewire_host_reset(writer->host);
FURI_CRITICAL_EXIT();
} else {
result = false;
}
return result;
}
static bool writer_write_1990_1(iButtonWriter* writer, iButtonKey* key) {
bool result = false;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
// unlock
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_1_CMD_WRITE_RECORD_FLAG);
delay_us(10);
writer_write_one_bit(writer, 0, 5000);
// write key
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_1_CMD_WRITE_ROM);
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
// inverted key for RW1990.1
writer_write_byte_ds1990(writer, ~ibutton_key_get_data_p(key)[i]);
delay_us(30000);
}
// lock
onewire_host_write(writer->host, RW1990_1_CMD_WRITE_RECORD_FLAG);
writer_write_one_bit(writer, 1, 10000);
FURI_CRITICAL_EXIT();
if(writer_compare_key_ds1990(writer, key)) {
result = true;
}
}
return result;
}
static bool writer_write_1990_2(iButtonWriter* writer, iButtonKey* key) {
bool result = false;
if(ibutton_key_get_type(key) == iButtonKeyDS1990) {
FURI_CRITICAL_ENTER();
// unlock
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_2_CMD_WRITE_RECORD_FLAG);
delay_us(10);
writer_write_one_bit(writer, 1, 5000);
// write key
onewire_host_reset(writer->host);
onewire_host_write(writer->host, RW1990_2_CMD_WRITE_ROM);
for(uint8_t i = 0; i < ibutton_key_get_data_size(key); i++) {
writer_write_byte_ds1990(writer, ibutton_key_get_data_p(key)[i]);
delay_us(30000);
}
// lock
onewire_host_write(writer->host, RW1990_2_CMD_WRITE_RECORD_FLAG);
writer_write_one_bit(writer, 0, 10000);
FURI_CRITICAL_EXIT();
if(writer_compare_key_ds1990(writer, key)) {
result = true;
}
}
return result;
}
/*
// TODO: adapt and test
static bool writer_write_TM01(
iButtonWriter* writer,
iButtonKey type,
const uint8_t* key,
uint8_t key_length) {
bool result = true;
{
// TODO test and encoding
FURI_CRITICAL_ENTER();
// unlock
onewire_host_reset(writer->host);
onewire_host_write(writer->host, TM01::CMD_WRITE_RECORD_FLAG);
onewire_write_one_bit(1, 10000);
// write key
onewire_host_reset(writer->host);
onewire_host_write(writer->host, TM01::CMD_WRITE_ROM);
// TODO: key types
//if(type == KEY_METAKOM || type == KEY_CYFRAL) {
//} else {
for(uint8_t i = 0; i < key->get_type_data_size(); i++) {
write_byte_ds1990(key->get_data()[i]);
delay_us(10000);
}
//}
// lock
onewire_host_write(writer->host, TM01::CMD_WRITE_RECORD_FLAG);
onewire_write_one_bit(0, 10000);
FURI_CRITICAL_EXIT();
}
if(!compare_key_ds1990(key)) {
result = false;
}
{
FURI_CRITICAL_ENTER();
if(key->get_key_type() == iButtonKeyType::KeyMetakom ||
key->get_key_type() == iButtonKeyType::KeyCyfral) {
onewire_host_reset(writer->host);
if(key->get_key_type() == iButtonKeyType::KeyCyfral)
onewire_host_write(writer->host, TM01::CMD_SWITCH_TO_CYFRAL);
else
onewire_host_write(writer->host, TM01::CMD_SWITCH_TO_METAKOM);
onewire_write_one_bit(1);
}
FURI_CRITICAL_EXIT();
}
return result;
}
*/
static iButtonWriterResult writer_write_DS1990(iButtonWriter* writer, iButtonKey* key) {
iButtonWriterResult result = iButtonWriterNoDetect;
bool same_key = writer_compare_key_ds1990(writer, key);
if(!same_key) {
// currently we can write:
// RW1990_1, TM08v2, TM08vi-2 by write_1990_1()
// RW1990_2 by write_1990_2()
// RW2004, RW2004, TM2004 with EEPROM by write_TM2004();
bool write_result = true;
do {
if(writer_write_1990_1(writer, key)) break;
if(writer_write_1990_2(writer, key)) break;
if(writer_write_TM2004(writer, key)) break;
write_result = false;
} while(false);
if(write_result) {
result = iButtonWriterOK;
} else {
result = iButtonWriterCannotWrite;
}
} else {
result = iButtonWriterSameKey;
}
return result;
}
/*********************** PUBLIC ***********************/
iButtonWriter* ibutton_writer_alloc(OneWireHost* host) {
iButtonWriter* writer = malloc(sizeof(iButtonWriter));
writer->host = host;
return writer;
}
void ibutton_writer_free(iButtonWriter* writer) {
free(writer);
}
iButtonWriterResult ibutton_writer_write(iButtonWriter* writer, iButtonKey* key) {
iButtonWriterResult result = iButtonWriterNoDetect;
osKernelLock();
bool blank_present = onewire_host_reset(writer->host);
osKernelUnlock();
if(blank_present) {
switch(ibutton_key_get_type(key)) {
case iButtonKeyDS1990:
result = writer_write_DS1990(writer, key);
default:
break;
}
}
return result;
}
void ibutton_writer_start(iButtonWriter* writer) {
furi_hal_power_enable_otg();
onewire_host_start(writer->host);
}
void ibutton_writer_stop(iButtonWriter* writer) {
onewire_host_stop(writer->host);
furi_hal_power_disable_otg();
}

View File

@@ -0,0 +1,60 @@
/**
* @file ibutton_writer.h
*
* iButton blanks writer
*/
#pragma once
#include <furi_hal_gpio.h>
#include "ibutton_key.h"
#include "../one_wire_host.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
iButtonWriterOK,
iButtonWriterSameKey,
iButtonWriterNoDetect,
iButtonWriterCannotWrite,
} iButtonWriterResult;
typedef struct iButtonWriter iButtonWriter;
/**
* Allocate writer
* @param host
* @return iButtonWriter*
*/
iButtonWriter* ibutton_writer_alloc(OneWireHost* host);
/**
* Deallocate writer
* @param writer
*/
void ibutton_writer_free(iButtonWriter* writer);
/**
* Write key to blank
* @param writer
* @param key
* @return iButtonWriterResult
*/
iButtonWriterResult ibutton_writer_write(iButtonWriter* writer, iButtonKey* key);
/**
* Start writing. Must be called before write attempt
* @param writer
*/
void ibutton_writer_start(iButtonWriter* writer);
/**
* Stop writing
* @param writer
*/
void ibutton_writer_stop(iButtonWriter* writer);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,256 @@
#include "protocol_cyfral.h"
#include <stdlib.h>
#include <string.h>
#include <furi/check.h>
#include <furi_hal_delay.h>
#define CYFRAL_DATA_SIZE 2
#define CYFRAL_MAX_PERIOD_US 230
typedef enum {
CYFRAL_BIT_WAIT_FRONT_HIGH,
CYFRAL_BIT_WAIT_FRONT_LOW,
} CyfralBitState;
typedef enum {
CYFRAL_WAIT_START_NIBBLE,
CYFRAL_READ_NIBBLE,
CYFRAL_READ_STOP_NIBBLE,
} CyfralState;
struct ProtocolCyfral {
PulseProtocol* protocol;
CyfralState state;
CyfralBitState bit_state;
// ready flag, key is read and valid
// TODO: atomic access
bool ready;
// key data storage
uint16_t key_data;
// high + low period time
uint32_t period_time;
// temporary nibble storage
uint8_t nibble;
// data valid flag
// MUST be checked only in READ_STOP_NIBBLE state
bool data_valid;
// nibble index, we expect 8 nibbles
uint8_t index;
// bit index in nibble, 4 bit per nibble
uint8_t bit_index;
// max period, 230us x clock per us
uint32_t max_period;
};
static void cyfral_pulse(void* context, bool polarity, uint32_t length);
static void cyfral_reset(void* context);
static void cyfral_get_data(void* context, uint8_t* data, size_t length);
static bool cyfral_decoded(void* context);
ProtocolCyfral* protocol_cyfral_alloc() {
ProtocolCyfral* cyfral = malloc(sizeof(ProtocolCyfral));
cyfral_reset(cyfral);
cyfral->protocol = pulse_protocol_alloc();
pulse_protocol_set_context(cyfral->protocol, cyfral);
pulse_protocol_set_pulse_cb(cyfral->protocol, cyfral_pulse);
pulse_protocol_set_reset_cb(cyfral->protocol, cyfral_reset);
pulse_protocol_set_get_data_cb(cyfral->protocol, cyfral_get_data);
pulse_protocol_set_decoded_cb(cyfral->protocol, cyfral_decoded);
return cyfral;
}
void protocol_cyfral_free(ProtocolCyfral* cyfral) {
furi_assert(cyfral);
pulse_protocol_free(cyfral->protocol);
free(cyfral);
}
PulseProtocol* protocol_cyfral_get_protocol(ProtocolCyfral* cyfral) {
furi_assert(cyfral);
return cyfral->protocol;
}
static void cyfral_get_data(void* context, uint8_t* data, size_t length) {
furi_assert(context);
furi_check(length >= CYFRAL_DATA_SIZE);
ProtocolCyfral* cyfral = context;
memcpy(data, &cyfral->key_data, CYFRAL_DATA_SIZE);
}
static bool cyfral_decoded(void* context) {
furi_assert(context);
ProtocolCyfral* cyfral = context;
bool decoded = cyfral->ready;
return decoded;
}
static void cyfral_reset(void* context) {
furi_assert(context);
ProtocolCyfral* cyfral = context;
cyfral->state = CYFRAL_WAIT_START_NIBBLE;
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_LOW;
cyfral->period_time = 0;
cyfral->bit_index = 0;
cyfral->ready = false;
cyfral->index = 0;
cyfral->key_data = 0;
cyfral->nibble = 0;
cyfral->data_valid = true;
cyfral->max_period = CYFRAL_MAX_PERIOD_US * instructions_per_us;
}
static bool cyfral_process_bit(
ProtocolCyfral* cyfral,
bool polarity,
uint32_t length,
bool* bit_ready,
bool* bit_value) {
bool result = true;
*bit_ready = false;
// bit start from low
switch(cyfral->bit_state) {
case CYFRAL_BIT_WAIT_FRONT_LOW:
if(polarity == true) {
cyfral->period_time += length;
*bit_ready = true;
if(cyfral->period_time <= cyfral->max_period) {
if((cyfral->period_time / 2) > length) {
*bit_value = false;
} else {
*bit_value = true;
}
} else {
result = false;
}
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_HIGH;
} else {
result = false;
}
break;
case CYFRAL_BIT_WAIT_FRONT_HIGH:
if(polarity == false) {
cyfral->period_time = length;
cyfral->bit_state = CYFRAL_BIT_WAIT_FRONT_LOW;
} else {
result = false;
}
break;
}
return result;
}
static void cyfral_pulse(void* context, bool polarity, uint32_t length) {
furi_assert(context);
ProtocolCyfral* cyfral = context;
bool bit_ready;
bool bit_value;
if(cyfral->ready) return;
switch(cyfral->state) {
case CYFRAL_WAIT_START_NIBBLE:
// wait for start word
if(cyfral_process_bit(cyfral, polarity, length, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = ((cyfral->nibble << 1) | bit_value) & 0x0F;
if(cyfral->nibble == 0b0001) {
cyfral->nibble = 0;
cyfral->state = CYFRAL_READ_NIBBLE;
}
}
} else {
cyfral_reset(cyfral);
}
break;
case CYFRAL_READ_NIBBLE:
// read nibbles
if(cyfral_process_bit(cyfral, polarity, length, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = (cyfral->nibble << 1) | bit_value;
cyfral->bit_index++;
//convert every nibble to 2-bit index
if(cyfral->bit_index == 4) {
switch(cyfral->nibble) {
case 0b1110:
cyfral->key_data = (cyfral->key_data << 2) | 0b11;
break;
case 0b1101:
cyfral->key_data = (cyfral->key_data << 2) | 0b10;
break;
case 0b1011:
cyfral->key_data = (cyfral->key_data << 2) | 0b01;
break;
case 0b0111:
cyfral->key_data = (cyfral->key_data << 2) | 0b00;
break;
default:
cyfral->data_valid = false;
break;
}
cyfral->nibble = 0;
cyfral->bit_index = 0;
cyfral->index++;
}
// succefully read 8 nibbles
if(cyfral->index == 8) {
cyfral->state = CYFRAL_READ_STOP_NIBBLE;
}
}
} else {
cyfral_reset(cyfral);
}
break;
case CYFRAL_READ_STOP_NIBBLE:
// read stop nibble
if(cyfral_process_bit(cyfral, polarity, length, &bit_ready, &bit_value)) {
if(bit_ready) {
cyfral->nibble = ((cyfral->nibble << 1) | bit_value) & 0x0F;
cyfral->bit_index++;
switch(cyfral->bit_index) {
case 0:
case 1:
case 2:
case 3:
break;
case 4:
if(cyfral->nibble == 0b0001) {
// validate data
if(cyfral->data_valid) {
cyfral->ready = true;
} else {
cyfral_reset(cyfral);
}
} else {
cyfral_reset(cyfral);
}
break;
default:
cyfral_reset(cyfral);
break;
}
}
} else {
cyfral_reset(cyfral);
}
break;
}
}

View File

@@ -0,0 +1,38 @@
/**
* @file protocol_cyfral.h
*
* Cyfral pulse format decoder
*/
#pragma once
#include <stdint.h>
#include "../../pulse_protocols/pulse_protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ProtocolCyfral ProtocolCyfral;
/**
* Allocate decoder
* @return ProtocolCyfral*
*/
ProtocolCyfral* protocol_cyfral_alloc();
/**
* Deallocate decoder
* @param cyfral
*/
void protocol_cyfral_free(ProtocolCyfral* cyfral);
/**
* Get protocol interface
* @param cyfral
* @return PulseProtocol*
*/
PulseProtocol* protocol_cyfral_get_protocol(ProtocolCyfral* cyfral);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,262 @@
#include "protocol_metakom.h"
#include <stdlib.h>
#include <string.h>
#include <furi/check.h>
#include <furi_hal_delay.h>
#define METAKOM_DATA_SIZE 4
#define METAKOM_PERIOD_SAMPLE_COUNT 10
typedef enum {
METAKOM_WAIT_PERIOD_SYNC,
METAKOM_WAIT_START_BIT,
METAKOM_WAIT_START_WORD,
METAKOM_READ_WORD,
METAKOM_READ_STOP_WORD,
} MetakomState;
typedef enum {
METAKOM_BIT_WAIT_FRONT_HIGH,
METAKOM_BIT_WAIT_FRONT_LOW,
} MetakomBitState;
struct ProtocolMetakom {
PulseProtocol* protocol;
// high + low period time
uint32_t period_time;
uint32_t low_time_storage;
uint8_t period_sample_index;
uint32_t period_sample_data[METAKOM_PERIOD_SAMPLE_COUNT];
// ready flag
// TODO: atomic access
bool ready;
uint8_t tmp_data;
uint8_t tmp_counter;
uint32_t key_data;
uint8_t key_data_index;
MetakomBitState bit_state;
MetakomState state;
};
static void metakom_pulse(void* context, bool polarity, uint32_t length);
static void metakom_reset(void* context);
static void metakom_get_data(void* context, uint8_t* data, size_t length);
static bool metakom_decoded(void* context);
ProtocolMetakom* protocol_metakom_alloc() {
ProtocolMetakom* metakom = malloc(sizeof(ProtocolMetakom));
metakom_reset(metakom);
metakom->protocol = pulse_protocol_alloc();
pulse_protocol_set_context(metakom->protocol, metakom);
pulse_protocol_set_pulse_cb(metakom->protocol, metakom_pulse);
pulse_protocol_set_reset_cb(metakom->protocol, metakom_reset);
pulse_protocol_set_get_data_cb(metakom->protocol, metakom_get_data);
pulse_protocol_set_decoded_cb(metakom->protocol, metakom_decoded);
return metakom;
}
void protocol_metakom_free(ProtocolMetakom* metakom) {
furi_assert(metakom);
pulse_protocol_free(metakom->protocol);
free(metakom);
}
PulseProtocol* protocol_metakom_get_protocol(ProtocolMetakom* metakom) {
furi_assert(metakom);
return metakom->protocol;
}
static void metakom_get_data(void* context, uint8_t* data, size_t length) {
furi_assert(context);
furi_check(length >= METAKOM_DATA_SIZE);
ProtocolMetakom* metakom = context;
memcpy(data, &metakom->key_data, METAKOM_DATA_SIZE);
}
static bool metakom_decoded(void* context) {
furi_assert(context);
ProtocolMetakom* metakom = context;
bool decoded = metakom->ready;
return decoded;
}
static void metakom_reset(void* context) {
furi_assert(context);
ProtocolMetakom* metakom = context;
metakom->ready = false;
metakom->period_sample_index = 0;
metakom->period_time = 0;
metakom->tmp_counter = 0;
metakom->tmp_data = 0;
for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
metakom->period_sample_data[i] = 0;
};
metakom->state = METAKOM_WAIT_PERIOD_SYNC;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
metakom->key_data = 0;
metakom->key_data_index = 0;
metakom->low_time_storage = 0;
}
static bool metakom_parity_check(uint8_t data) {
uint8_t ones_count = 0;
bool result;
for(uint8_t i = 0; i < 8; i++) {
if((data >> i) & 0b00000001) {
ones_count++;
}
}
result = (ones_count % 2 == 0);
return result;
}
static bool metakom_process_bit(
ProtocolMetakom* metakom,
bool polarity,
uint32_t time,
uint32_t* high_time,
uint32_t* low_time) {
bool result = false;
switch(metakom->bit_state) {
case METAKOM_BIT_WAIT_FRONT_LOW:
if(polarity == false) {
*low_time = metakom->low_time_storage;
*high_time = time;
result = true;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_HIGH;
}
break;
case METAKOM_BIT_WAIT_FRONT_HIGH:
if(polarity == true) {
metakom->low_time_storage = time;
metakom->bit_state = METAKOM_BIT_WAIT_FRONT_LOW;
}
break;
}
return result;
}
static void metakom_pulse(void* context, bool polarity, uint32_t time) {
furi_assert(context);
ProtocolMetakom* metakom = context;
if(metakom->ready) return;
uint32_t high_time = 0;
uint32_t low_time = 0;
switch(metakom->state) {
case METAKOM_WAIT_PERIOD_SYNC:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
metakom->period_sample_data[metakom->period_sample_index] = high_time + low_time;
metakom->period_sample_index++;
if(metakom->period_sample_index == METAKOM_PERIOD_SAMPLE_COUNT) {
for(uint8_t i = 0; i < METAKOM_PERIOD_SAMPLE_COUNT; i++) {
metakom->period_time += metakom->period_sample_data[i];
};
metakom->period_time /= METAKOM_PERIOD_SAMPLE_COUNT;
metakom->state = METAKOM_WAIT_START_BIT;
}
}
break;
case METAKOM_WAIT_START_BIT:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
metakom->tmp_counter++;
if(high_time > metakom->period_time) {
metakom->tmp_counter = 0;
metakom->state = METAKOM_WAIT_START_WORD;
}
if(metakom->tmp_counter > 40) {
metakom_reset(metakom);
}
}
break;
case METAKOM_WAIT_START_WORD:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 3) {
if(metakom->tmp_data == 0b010) {
metakom->tmp_counter = 0;
metakom->tmp_data = 0;
metakom->state = METAKOM_READ_WORD;
} else {
metakom_reset(metakom);
}
}
}
break;
case METAKOM_READ_WORD:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 8) {
if(metakom_parity_check(metakom->tmp_data)) {
metakom->key_data = (metakom->key_data << 8) | metakom->tmp_data;
metakom->key_data_index++;
metakom->tmp_data = 0;
metakom->tmp_counter = 0;
if(metakom->key_data_index == 4) {
// check for stop bit
if(high_time > metakom->period_time) {
metakom->state = METAKOM_READ_STOP_WORD;
} else {
metakom_reset(metakom);
}
}
} else {
metakom_reset(metakom);
}
}
}
break;
case METAKOM_READ_STOP_WORD:
if(metakom_process_bit(metakom, polarity, time, &high_time, &low_time)) {
if(low_time < (metakom->period_time / 2)) {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b0;
} else {
metakom->tmp_data = (metakom->tmp_data << 1) | 0b1;
}
metakom->tmp_counter++;
if(metakom->tmp_counter == 3) {
if(metakom->tmp_data == 0b010) {
metakom->ready = true;
} else {
metakom_reset(metakom);
}
}
}
break;
}
}

View File

@@ -0,0 +1,38 @@
/**
* @file protocol_metakom.h
*
* Metakom pulse format decoder
*/
#pragma once
#include <stdint.h>
#include "../../pulse_protocols/pulse_protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ProtocolMetakom ProtocolMetakom;
/**
* Allocate decoder
* @return ProtocolMetakom*
*/
ProtocolMetakom* protocol_metakom_alloc();
/**
* Free decoder
* @param metakom
*/
void protocol_metakom_free(ProtocolMetakom* metakom);
/**
* Get protocol interface
* @param metakom
* @return PulseProtocol*
*/
PulseProtocol* protocol_metakom_get_protocol(ProtocolMetakom* metakom);
#ifdef __cplusplus
}
#endif

16
lib/one_wire/maxim_crc.c Normal file
View File

@@ -0,0 +1,16 @@
#include "maxim_crc.h"
uint8_t maxim_crc8(const uint8_t* data, const uint8_t data_size, const uint8_t crc_init) {
uint8_t crc = crc_init;
for(uint8_t index = 0; index < data_size; ++index) {
uint8_t input_byte = data[index];
for(uint8_t bit_position = 0; bit_position < 8; ++bit_position) {
const uint8_t mix = (crc ^ input_byte) & (uint8_t)(0x01);
crc >>= 1;
if(mix != 0) crc ^= 0x8C;
input_byte >>= 1;
}
}
return crc;
}

14
lib/one_wire/maxim_crc.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
#define MAXIM_CRC8_INIT 0
uint8_t maxim_crc8(const uint8_t* data, const uint8_t data_size, const uint8_t crc_init);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,59 @@
#include <stdlib.h>
#include "maxim_crc.h"
#include "one_wire_device.h"
#include "one_wire_slave.h"
#include "one_wire_slave_i.h"
struct OneWireDevice {
uint8_t id_storage[8];
OneWireSlave* bus;
};
OneWireDevice* onewire_device_alloc(
uint8_t id_1,
uint8_t id_2,
uint8_t id_3,
uint8_t id_4,
uint8_t id_5,
uint8_t id_6,
uint8_t id_7,
uint8_t id_8) {
OneWireDevice* device = malloc(sizeof(OneWireDevice));
device->id_storage[0] = id_1;
device->id_storage[1] = id_2;
device->id_storage[2] = id_3;
device->id_storage[3] = id_4;
device->id_storage[4] = id_5;
device->id_storage[5] = id_6;
device->id_storage[6] = id_7;
device->id_storage[7] = id_8;
device->bus = NULL;
return device;
}
void onewire_device_free(OneWireDevice* device) {
if(device->bus != NULL) {
onewire_slave_detach(device->bus);
}
free(device);
}
void onewire_device_send_id(OneWireDevice* device) {
if(device->bus != NULL) {
onewire_slave_send(device->bus, device->id_storage, 8);
}
}
void onewire_device_attach(OneWireDevice* device, OneWireSlave* bus) {
device->bus = bus;
}
void onewire_device_detach(OneWireDevice* device) {
device->bus = NULL;
}
uint8_t* onewire_device_get_id_p(OneWireDevice* device) {
return device->id_storage;
}

View File

@@ -0,0 +1,74 @@
/**
* @file one_wire_device.h
*
* 1-Wire slave library, device interface. Currently it can only emulate ID.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct OneWireSlave OneWireSlave;
typedef struct OneWireDevice OneWireDevice;
/**
* Allocate onewire device with ID
* @param id_1
* @param id_2
* @param id_3
* @param id_4
* @param id_5
* @param id_6
* @param id_7
* @param id_8
* @return OneWireDevice*
*/
OneWireDevice* onewire_device_alloc(
uint8_t id_1,
uint8_t id_2,
uint8_t id_3,
uint8_t id_4,
uint8_t id_5,
uint8_t id_6,
uint8_t id_7,
uint8_t id_8);
/**
* Deallocate onewire device
* @param device
*/
void onewire_device_free(OneWireDevice* device);
/**
* Send ID report, called from onewire slave
* @param device
*/
void onewire_device_send_id(OneWireDevice* device);
/**
* Attach device to onewire slave bus
* @param device
* @param bus
*/
void onewire_device_attach(OneWireDevice* device, OneWireSlave* bus);
/**
* Attach device from onewire slave bus
* @param device
*/
void onewire_device_detach(OneWireDevice* device);
/**
* Get pointer to device id array
* @param device
* @return uint8_t*
*/
uint8_t* onewire_device_get_id_p(OneWireDevice* device);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,261 @@
#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];
uint8_t last_discrepancy;
uint8_t last_family_discrepancy;
bool last_device_flag;
};
OneWireHost* onewire_host_alloc() {
OneWireHost* host = malloc(sizeof(OneWireHost));
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_ibutton_pin_high();
do {
if(--retries == 0) return 0;
delay_us(2);
} while(!furi_hal_ibutton_pin_get_level());
// pre delay
delay_us(OWH_RESET_DELAY_PRE);
// drive low
furi_hal_ibutton_pin_low();
delay_us(OWH_RESET_DRIVE);
// release
furi_hal_ibutton_pin_high();
delay_us(OWH_RESET_RELEASE);
// read and post delay
r = !furi_hal_ibutton_pin_get_level();
delay_us(OWH_RESET_DELAY_POST);
return r;
}
bool onewire_host_read_bit(OneWireHost* host) {
bool result;
// drive low
furi_hal_ibutton_pin_low();
delay_us(OWH_READ_DRIVE);
// release
furi_hal_ibutton_pin_high();
delay_us(OWH_READ_RELEASE);
// read and post delay
result = furi_hal_ibutton_pin_get_level();
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_ibutton_pin_low();
delay_us(OWH_WRITE_1_DRIVE);
// release
furi_hal_ibutton_pin_high();
delay_us(OWH_WRITE_1_RELEASE);
} else {
// drive low
furi_hal_ibutton_pin_low();
delay_us(OWH_WRITE_0_DRIVE);
// release
furi_hal_ibutton_pin_high();
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_skip(OneWireHost* host) {
onewire_host_write(host, 0xCC);
}
void onewire_host_start(OneWireHost* host) {
furi_hal_ibutton_start_drive();
}
void onewire_host_stop(OneWireHost* host) {
furi_hal_ibutton_stop();
}
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* newAddr, 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 CONDITIONAL_SEARCH:
onewire_host_write(host, 0xEC);
break;
case NORMAL_SEARCH:
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++) newAddr[i] = host->saved_rom[i];
}
return search_result;
}

View File

@@ -0,0 +1,121 @@
/**
* @file one_wire_host.h
*
* 1-Wire host (master) library
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <furi_hal_gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
CONDITIONAL_SEARCH = 0, /**< Search for alarmed device */
NORMAL_SEARCH = 1, /**< Search all devices */
} OneWireHostSearchMode;
typedef struct OneWireHost OneWireHost;
/**
* Allocate onewire host bus
* @param gpio
* @return OneWireHost*
*/
OneWireHost* onewire_host_alloc();
/**
* Deallocate onewire host bus
* @param host
*/
void onewire_host_free(OneWireHost* host);
/**
* Reset bus
* @param host
* @return bool
*/
bool onewire_host_reset(OneWireHost* host);
/**
* Read one bit
* @param host
* @return bool
*/
bool onewire_host_read_bit(OneWireHost* host);
/**
* Read one byte
* @param host
* @return uint8_t
*/
uint8_t onewire_host_read(OneWireHost* host);
/**
* Read many bytes
* @param host
* @param buffer
* @param count
*/
void onewire_host_read_bytes(OneWireHost* host, uint8_t* buffer, uint16_t count);
/**
* Write one bit
* @param host
* @param value
*/
void onewire_host_write_bit(OneWireHost* host, bool value);
/**
* Write one byte
* @param host
* @param value
*/
void onewire_host_write(OneWireHost* host, uint8_t value);
/**
* Skip ROM command
* @param host
*/
void onewire_host_skip(OneWireHost* host);
/**
* Start working with the bus
* @param host
*/
void onewire_host_start(OneWireHost* host);
/**
* Stop working with the bus
* @param host
*/
void onewire_host_stop(OneWireHost* host);
/**
*
* @param host
*/
void onewire_host_reset_search(OneWireHost* host);
/**
*
* @param host
* @param family_code
*/
void onewire_host_target_search(OneWireHost* host, uint8_t family_code);
/**
*
* @param host
* @param newAddr
* @param mode
* @return uint8_t
*/
uint8_t onewire_host_search(OneWireHost* host, uint8_t* newAddr, OneWireHostSearchMode mode);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,30 @@
/**
* @file one_wire_host_timing.h
*
* 1-Wire library, timing list
*/
#pragma once
#define OWH_TIMING_A 9
#define OWH_TIMING_B 64
#define OWH_TIMING_C 64
#define OWH_TIMING_D 14
#define OWH_TIMING_E 9
#define OWH_TIMING_F 55
#define OWH_TIMING_G 0
#define OWH_TIMING_H 480
#define OWH_TIMING_I 70
#define OWH_TIMING_J 410
#define OWH_WRITE_1_DRIVE OWH_TIMING_A
#define OWH_WRITE_1_RELEASE OWH_TIMING_B
#define OWH_WRITE_0_DRIVE OWH_TIMING_C
#define OWH_WRITE_0_RELEASE OWH_TIMING_D
#define OWH_READ_DRIVE 3
#define OWH_READ_RELEASE OWH_TIMING_E
#define OWH_READ_DELAY_POST OWH_TIMING_F
#define OWH_RESET_DELAY_PRE OWH_TIMING_G
#define OWH_RESET_DRIVE OWH_TIMING_H
#define OWH_RESET_RELEASE OWH_TIMING_I
#define OWH_RESET_DELAY_POST OWH_TIMING_J

View File

@@ -0,0 +1,317 @@
#include "one_wire_slave.h"
#include "one_wire_slave_i.h"
#include "one_wire_device.h"
#include <furi.h>
#include <furi_hal_delay.h>
#include <furi_hal_ibutton.h>
#define OWS_RESET_MIN 270
#define OWS_RESET_MAX 960
#define OWS_PRESENCE_TIMEOUT 20
#define OWS_PRESENCE_MIN 100
#define OWS_PRESENCE_MAX 480
#define OWS_MSG_HIGH_TIMEOUT 15000
#define OWS_SLOT_MAX 135
#define OWS_READ_MIN 20
#define OWS_READ_MAX 60
#define OWS_WRITE_ZERO 30
typedef enum {
NO_ERROR = 0,
VERY_LONG_RESET,
VERY_SHORT_RESET,
PRESENCE_LOW_ON_LINE,
AWAIT_TIMESLOT_TIMEOUT_HIGH,
INCORRECT_ONEWIRE_CMD,
FIRST_BIT_OF_BYTE_TIMEOUT,
RESET_IN_PROGRESS
} OneWireSlaveError;
struct OneWireSlave {
OneWireSlaveError error;
OneWireDevice* device;
OneWireSlaveResultCallback result_cb;
void* result_cb_ctx;
};
/*********************** PRIVATE ***********************/
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 * instructions_per_us;
uint32_t time_captured;
do {
time_captured = DWT->CYCCNT;
if(furi_hal_ibutton_pin_get_level() != pin_value) {
uint32_t remaining_time = time_ticks - (time_captured - start);
remaining_time /= instructions_per_us;
return remaining_time;
}
} while((time_captured - start) < time_ticks);
return 0;
}
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();
delay_us(OWS_PRESENCE_MIN);
furi_hal_ibutton_pin_high();
// somebody also can show presence
const uint32_t wait_low_time = OWS_PRESENCE_MAX - OWS_PRESENCE_MIN;
// so we will wait
if(onewire_slave_wait_while_gpio_is(bus, wait_low_time, false) == 0) {
bus->error = PRESENCE_LOW_ON_LINE;
return false;
}
return true;
}
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);
if(time == 0) {
bus->error = RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWS_MSG_HIGH_TIMEOUT;
time = onewire_slave_wait_while_gpio_is(bus, time, true);
if(time == 0) {
bus->error = AWAIT_TIMESLOT_TIMEOUT_HIGH;
return false;
}
// wait a time of zero
time = OWS_READ_MIN;
time = onewire_slave_wait_while_gpio_is(bus, time, false);
return (time > 0);
}
bool onewire_slave_send_bit(OneWireSlave* bus, bool value) {
const bool write_zero = !value;
// wait while bus is low
uint32_t time = OWS_SLOT_MAX;
time = onewire_slave_wait_while_gpio_is(bus, time, false);
if(time == 0) {
bus->error = RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWS_MSG_HIGH_TIMEOUT;
time = onewire_slave_wait_while_gpio_is(bus, time, true);
if(time == 0) {
bus->error = AWAIT_TIMESLOT_TIMEOUT_HIGH;
return false;
}
// choose write time
if(write_zero) {
furi_hal_ibutton_pin_low();
time = OWS_WRITE_ZERO;
} else {
time = OWS_READ_MAX;
}
// hold line for ZERO or ONE time
delay_us(time);
furi_hal_ibutton_pin_high();
return true;
}
void onewire_slave_cmd_search_rom(OneWireSlave* bus) {
const uint8_t key_bytes = 8;
uint8_t* key = onewire_device_get_id_p(bus->device);
for(uint8_t i = 0; i < key_bytes; i++) {
uint8_t key_byte = key[i];
for(uint8_t j = 0; j < 8; j++) {
bool bit = (key_byte >> j) & 0x01;
if(!onewire_slave_send_bit(bus, bit)) return;
if(!onewire_slave_send_bit(bus, !bit)) return;
onewire_slave_receive_bit(bus);
if(bus->error != NO_ERROR) return;
}
}
}
bool onewire_slave_receive_and_process_cmd(OneWireSlave* bus) {
uint8_t cmd;
onewire_slave_receive(bus, &cmd, 1);
if(bus->error == RESET_IN_PROGRESS) return true;
if(bus->error != NO_ERROR) return false;
switch(cmd) {
case 0xF0:
// SEARCH ROM
onewire_slave_cmd_search_rom(bus);
return true;
case 0x0F:
case 0x33:
// READ ROM
onewire_device_send_id(bus->device);
return true;
default: // Unknown command
bus->error = INCORRECT_ONEWIRE_CMD;
}
if(bus->error == RESET_IN_PROGRESS) return true;
return (bus->error == NO_ERROR);
}
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();
bus->error = NO_ERROR;
if(onewire_slave_show_presence(bus)) {
// TODO think about multiple command cycles
onewire_slave_receive_and_process_cmd(bus);
result = (bus->error == NO_ERROR || bus->error == INCORRECT_ONEWIRE_CMD);
} else {
result = false;
}
furi_hal_ibutton_start_interrupt_in_isr();
FURI_CRITICAL_EXIT();
}
return result;
}
static void exti_cb(void* context) {
OneWireSlave* bus = context;
volatile bool input_state = furi_hal_ibutton_pin_get_level();
static uint32_t pulse_start = 0;
if(input_state) {
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / instructions_per_us;
if(pulse_length >= OWS_RESET_MIN) {
if(pulse_length <= OWS_RESET_MAX) {
// reset cycle ok
bool result = onewire_slave_bus_start(bus);
if(result && bus->result_cb != NULL) {
bus->result_cb(bus->result_cb_ctx);
}
} else {
bus->error = VERY_LONG_RESET;
}
} else {
bus->error = VERY_SHORT_RESET;
}
} else {
//FALL event
pulse_start = DWT->CYCCNT;
}
};
/*********************** PUBLIC ***********************/
OneWireSlave* onewire_slave_alloc() {
OneWireSlave* bus = malloc(sizeof(OneWireSlave));
bus->error = NO_ERROR;
bus->device = NULL;
bus->result_cb = NULL;
bus->result_cb_ctx = NULL;
return bus;
}
void onewire_slave_free(OneWireSlave* bus) {
onewire_slave_stop(bus);
free(bus);
}
void onewire_slave_start(OneWireSlave* bus) {
furi_hal_ibutton_add_interrupt(exti_cb, bus);
furi_hal_ibutton_start_interrupt();
}
void onewire_slave_stop(OneWireSlave* bus) {
furi_hal_ibutton_stop();
furi_hal_ibutton_remove_interrupt();
}
void onewire_slave_attach(OneWireSlave* bus, OneWireDevice* device) {
bus->device = device;
onewire_device_attach(device, bus);
}
void onewire_slave_detach(OneWireSlave* bus) {
if(bus->device != NULL) {
onewire_device_detach(bus->device);
}
bus->device = NULL;
}
void onewire_slave_set_result_callback(
OneWireSlave* bus,
OneWireSlaveResultCallback result_cb,
void* context) {
bus->result_cb = result_cb;
bus->result_cb_ctx = context;
}
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();
// bytes loop
for(; bytes_sent < data_length; ++bytes_sent) {
const uint8_t data_byte = address[bytes_sent];
// bit loop
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(!onewire_slave_send_bit(bus, bit_mask & data_byte)) {
// if we cannot send first bit
if((bit_mask == 0x01) && (bus->error == AWAIT_TIMESLOT_TIMEOUT_HIGH))
bus->error = FIRST_BIT_OF_BYTE_TIMEOUT;
return false;
}
}
}
return true;
}
bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, const uint8_t data_length) {
uint8_t bytes_received = 0;
furi_hal_ibutton_pin_high();
for(; bytes_received < data_length; ++bytes_received) {
uint8_t value = 0;
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(onewire_slave_receive_bit(bus)) value |= bit_mask;
}
data[bytes_received] = value;
}
return (bytes_received != data_length);
}

View File

@@ -0,0 +1,71 @@
/**
* @file one_wire_slave.h
*
* 1-Wire slave library. Currently it can only emulate ID.
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <furi_hal_gpio.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct OneWireDevice OneWireDevice;
typedef struct OneWireSlave OneWireSlave;
typedef void (*OneWireSlaveResultCallback)(void* context);
/**
* Allocate onewire slave
* @param pin
* @return OneWireSlave*
*/
OneWireSlave* onewire_slave_alloc();
/**
* Free onewire slave
* @param bus
*/
void onewire_slave_free(OneWireSlave* bus);
/**
* Start working with the bus
* @param bus
*/
void onewire_slave_start(OneWireSlave* bus);
/**
* Stop working with the bus
* @param bus
*/
void onewire_slave_stop(OneWireSlave* bus);
/**
* Attach device for emulation
* @param bus
* @param device
*/
void onewire_slave_attach(OneWireSlave* bus, OneWireDevice* device);
/**
* Detach device from bus
* @param bus
*/
void onewire_slave_detach(OneWireSlave* bus);
/**
* Set a callback to report emulation success
* @param bus
* @param result_cb
* @param context
*/
void onewire_slave_set_result_callback(
OneWireSlave* bus,
OneWireSlaveResultCallback result_cb,
void* context);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,38 @@
/**
* @file one_wire_slave_i.h
*
* 1-Wire slave library, internal functions
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct OneWireDevice OneWireDevice;
typedef struct OneWireSlave OneWireSlave;
/**
* Send data, called from emulated device
* @param bus
* @param address
* @param data_length
* @return bool
*/
bool onewire_slave_send(OneWireSlave* bus, const uint8_t* address, const uint8_t data_length);
/**
* Receive data, called from emulated device
* @param bus
* @param data
* @param data_length
* @return bool
*/
bool onewire_slave_receive(OneWireSlave* bus, uint8_t* data, const uint8_t data_length);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,66 @@
#include <stdlib.h>
#include "pulse_decoder.h"
#include <string.h>
#include <furi/check.h>
#define MAX_PROTOCOL 5
struct PulseDecoder {
PulseProtocol* protocols[MAX_PROTOCOL];
};
PulseDecoder* pulse_decoder_alloc() {
PulseDecoder* decoder = malloc(sizeof(PulseDecoder));
memset(decoder, 0, sizeof(PulseDecoder));
return decoder;
}
void pulse_decoder_free(PulseDecoder* reader) {
furi_assert(reader);
free(reader);
}
void pulse_decoder_add_protocol(PulseDecoder* reader, PulseProtocol* protocol, int32_t index) {
furi_check(index < MAX_PROTOCOL);
furi_check(reader->protocols[index] == NULL);
reader->protocols[index] = protocol;
}
void pulse_decoder_process_pulse(PulseDecoder* reader, bool polarity, uint32_t length) {
furi_assert(reader);
for(size_t index = 0; index < MAX_PROTOCOL; index++) {
if(reader->protocols[index] != NULL) {
pulse_protocol_process_pulse(reader->protocols[index], polarity, length);
}
}
}
int32_t pulse_decoder_get_decoded_index(PulseDecoder* reader) {
furi_assert(reader);
int32_t decoded = -1;
for(size_t index = 0; index < MAX_PROTOCOL; index++) {
if(reader->protocols[index] != NULL) {
if(pulse_protocol_decoded(reader->protocols[index])) {
decoded = index;
break;
}
}
}
return decoded;
}
void pulse_decoder_reset(PulseDecoder* reader) {
furi_assert(reader);
for(size_t index = 0; index < MAX_PROTOCOL; index++) {
if(reader->protocols[index] != NULL) {
pulse_protocol_reset(reader->protocols[index]);
}
}
}
void pulse_decoder_get_data(PulseDecoder* reader, int32_t index, uint8_t* data, size_t length) {
furi_assert(reader);
furi_check(reader->protocols[index] != NULL);
pulse_protocol_get_data(reader->protocols[index], data, length);
}

View File

@@ -0,0 +1,70 @@
/**
* @file pulse_decoder.h
*
* Generic pulse protocol decoder library
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "pulse_protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct PulseDecoder PulseDecoder;
/**
* Allocate decoder
* @return PulseDecoder*
*/
PulseDecoder* pulse_decoder_alloc();
/**
* Deallocate decoder
* @param decoder
*/
void pulse_decoder_free(PulseDecoder* decoder);
/**
* Add protocol to decoder
* @param decoder
* @param protocol protocol implementation
* @param index protocol index, should not be repeated
*/
void pulse_decoder_add_protocol(PulseDecoder* decoder, PulseProtocol* protocol, int32_t index);
/**
* Push and process pulse with decoder
* @param decoder
* @param polarity
* @param length
*/
void pulse_decoder_process_pulse(PulseDecoder* decoder, bool polarity, uint32_t length);
/**
* Get indec of decoded protocol
* @param decoder
* @return int32_t, -1 if nothing decoded, or index of decoded protocol
*/
int32_t pulse_decoder_get_decoded_index(PulseDecoder* decoder);
/**
* Reset all protocols in decoder
* @param decoder
*/
void pulse_decoder_reset(PulseDecoder* decoder);
/**
* Get decoded data from protocol
* @param decoder
* @param index
* @param data
* @param length
*/
void pulse_decoder_get_data(PulseDecoder* decoder, int32_t index, uint8_t* data, size_t length);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,55 @@
#include "pulse_glue.h"
struct PulseGlue {
int32_t hi_period;
int32_t low_period;
int32_t next_hi_period;
};
PulseGlue* pulse_glue_alloc() {
PulseGlue* pulse_glue = malloc(sizeof(PulseGlue));
pulse_glue_reset(pulse_glue);
return pulse_glue;
}
void pulse_glue_free(PulseGlue* pulse_glue) {
free(pulse_glue);
}
void pulse_glue_reset(PulseGlue* pulse_glue) {
pulse_glue->hi_period = 0;
pulse_glue->low_period = 0;
pulse_glue->next_hi_period = 0;
}
bool pulse_glue_push(PulseGlue* pulse_glue, bool polarity, uint32_t length) {
bool pop_ready = false;
if(polarity) {
if(pulse_glue->low_period == 0) {
// stage 1, accumulate hi period
pulse_glue->hi_period += length;
} else {
// stage 3, accumulate next hi period and be ready for pulse_glue_pop
pulse_glue->next_hi_period = length;
// data is ready
pop_ready = true;
}
} else {
if(pulse_glue->hi_period != 0) {
// stage 2, accumulate low period
pulse_glue->low_period += length;
}
}
return pop_ready;
}
void pulse_glue_pop(PulseGlue* pulse_glue, uint32_t* length, uint32_t* period) {
*length = pulse_glue->hi_period + pulse_glue->low_period;
*period = pulse_glue->hi_period;
pulse_glue->hi_period = pulse_glue->next_hi_period;
pulse_glue->low_period = 0;
pulse_glue->next_hi_period = 0;
}

View File

@@ -0,0 +1,26 @@
/**
* @file pulse_glue.h
*
* Simple tool to glue separated pulses to corret
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct PulseGlue PulseGlue;
PulseGlue* pulse_glue_alloc();
void pulse_glue_free(PulseGlue* pulse_glue);
void pulse_glue_reset(PulseGlue* pulse_glue);
bool pulse_glue_push(PulseGlue* pulse_glue, bool polarity, uint32_t length);
void pulse_glue_pop(PulseGlue* pulse_glue, uint32_t* length, uint32_t* period);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,67 @@
#include "pulse_protocol.h"
#include <stdlib.h>
#include <string.h>
struct PulseProtocol {
void* context;
PulseProtocolPulseCallback pulse_cb;
PulseProtocolResetCallback reset_cb;
PulseProtocolGetDataCallback get_data_cb;
PulseProtocolDecodedCallback decoded_cb;
};
PulseProtocol* pulse_protocol_alloc() {
PulseProtocol* protocol = malloc(sizeof(PulseProtocol));
memset(protocol, 0, sizeof(PulseProtocol));
return protocol;
}
void pulse_protocol_set_context(PulseProtocol* protocol, void* context) {
protocol->context = context;
}
void pulse_protocol_set_pulse_cb(PulseProtocol* protocol, PulseProtocolPulseCallback callback) {
protocol->pulse_cb = callback;
}
void pulse_protocol_set_reset_cb(PulseProtocol* protocol, PulseProtocolResetCallback callback) {
protocol->reset_cb = callback;
}
void pulse_protocol_set_get_data_cb(PulseProtocol* protocol, PulseProtocolGetDataCallback callback) {
protocol->get_data_cb = callback;
}
void pulse_protocol_set_decoded_cb(PulseProtocol* protocol, PulseProtocolDecodedCallback callback) {
protocol->decoded_cb = callback;
}
void pulse_protocol_free(PulseProtocol* protocol) {
free(protocol);
}
void pulse_protocol_process_pulse(PulseProtocol* protocol, bool polarity, uint32_t length) {
if(protocol->pulse_cb != NULL) {
protocol->pulse_cb(protocol->context, polarity, length);
}
}
void pulse_protocol_reset(PulseProtocol* protocol) {
if(protocol->reset_cb != NULL) {
protocol->reset_cb(protocol->context);
}
}
bool pulse_protocol_decoded(PulseProtocol* protocol) {
bool result = false;
if(protocol->decoded_cb != NULL) {
result = protocol->decoded_cb(protocol->context);
}
return result;
}
void pulse_protocol_get_data(PulseProtocol* protocol, uint8_t* data, size_t length) {
if(protocol->get_data_cb != NULL) {
protocol->get_data_cb(protocol->context, data, length);
}
}

View File

@@ -0,0 +1,122 @@
/**
* @file pulse_protocol.h
*
* Generic pulse protocol decoder library, protocol interface
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Anonymous PulseProtocol struct
*/
typedef struct PulseProtocol PulseProtocol;
/**
* Process pulse callback
*/
typedef void (*PulseProtocolPulseCallback)(void* context, bool polarity, uint32_t length);
/**
* Reset protocol callback
*/
typedef void (*PulseProtocolResetCallback)(void* context);
/**
* Get decoded data callback
*/
typedef void (*PulseProtocolGetDataCallback)(void* context, uint8_t* data, size_t length);
/**
* Is protocol decoded callback
*/
typedef bool (*PulseProtocolDecodedCallback)(void* context);
/**
* Allocate protocol
* @return PulseProtocol*
*/
PulseProtocol* pulse_protocol_alloc();
/**
* Deallocate protocol
* @param protocol
*/
void pulse_protocol_free(PulseProtocol* protocol);
/**
* Set context for callbacks
* @param protocol
* @param context
*/
void pulse_protocol_set_context(PulseProtocol* protocol, void* context);
/**
* Set "Process pulse" callback. Called from the decoder when a new pulse is received.
* @param protocol
* @param callback
*/
void pulse_protocol_set_pulse_cb(PulseProtocol* protocol, PulseProtocolPulseCallback callback);
/**
* Set "Reset protocol" callback. Called from the decoder when the decoder is reset.
* @param protocol
* @param callback
*/
void pulse_protocol_set_reset_cb(PulseProtocol* protocol, PulseProtocolResetCallback callback);
/**
* Set "Get decoded data" callback. Called from the decoder when the decoder wants to get decoded data.
* @param protocol
* @param callback
*/
void pulse_protocol_set_get_data_cb(PulseProtocol* protocol, PulseProtocolGetDataCallback callback);
/**
* Set "Is protocol decoded" callback. Called from the decoder when the decoder wants to know if a protocol has been decoded.
* @param protocol
* @param callback
*/
void pulse_protocol_set_decoded_cb(PulseProtocol* protocol, PulseProtocolDecodedCallback callback);
/**
* Part of decoder interface.
* @param protocol
* @param polarity
* @param length
*/
void pulse_protocol_process_pulse(PulseProtocol* protocol, bool polarity, uint32_t length);
/**
* Part of decoder interface.
* @param protocol
* @return true
* @return false
*/
bool pulse_protocol_decoded(PulseProtocol* protocol);
/**
* Part of decoder interface.
* @param protocol
* @return true
* @return false
*/
void pulse_protocol_get_data(PulseProtocol* protocol, uint8_t* data, size_t length);
/**
* Part of decoder interface.
* @param protocol
* @return true
* @return false
*/
void pulse_protocol_reset(PulseProtocol* protocol);
#ifdef __cplusplus
}
#endif

View File

@@ -1,48 +0,0 @@
#include "maxim_crc.h"
uint8_t maxim_crc8(const uint8_t* data, const uint8_t data_size, const uint8_t crc_init) {
uint8_t crc = crc_init;
for(uint8_t index = 0; index < data_size; ++index) {
uint8_t input_byte = data[index];
for(uint8_t bit_position = 0; bit_position < 8; ++bit_position) {
const uint8_t mix = (crc ^ input_byte) & static_cast<uint8_t>(0x01);
crc >>= 1;
if(mix != 0) crc ^= 0x8C;
input_byte >>= 1;
}
}
return crc;
}
uint16_t maxim_crc16(const uint8_t* address, const uint8_t length, const uint16_t init) {
uint16_t crc = init;
static const uint8_t odd_parity[16] = {0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0};
for(uint8_t i = 0; i < length; ++i) {
uint16_t cdata = address[i];
cdata = (cdata ^ crc) & static_cast<uint16_t>(0xff);
crc >>= 8;
if((odd_parity[cdata & 0x0F] ^ odd_parity[cdata >> 4]) != 0) crc ^= 0xC001;
cdata <<= 6;
crc ^= cdata;
cdata <<= 1;
crc ^= cdata;
}
return crc;
}
uint16_t maxim_crc16(uint8_t value, uint16_t crc) {
static const uint8_t odd_parity[16] = {0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0};
value = (value ^ static_cast<uint8_t>(crc));
crc >>= 8;
if((odd_parity[value & 0x0F] ^ odd_parity[value >> 4]) != 0) crc ^= 0xC001;
uint16_t cdata = (static_cast<uint16_t>(value) << 6);
crc ^= cdata;
crc ^= (static_cast<uint16_t>(cdata) << 1);
return crc;
}

View File

@@ -1,6 +0,0 @@
#pragma once
#include <stdint.h>
uint8_t maxim_crc8(const uint8_t* data, const uint8_t data_size, const uint8_t crc_init = 0);
uint16_t maxim_crc16(const uint8_t* address, const uint8_t length, const uint16_t init = 0);
uint16_t maxim_crc16(uint8_t value, uint16_t crc);

View File

@@ -1,39 +0,0 @@
#include "one_wire_device.h"
OneWireDevice::OneWireDevice(
uint8_t id_1,
uint8_t id_2,
uint8_t id_3,
uint8_t id_4,
uint8_t id_5,
uint8_t id_6,
uint8_t id_7) {
id_storage[0] = id_1;
id_storage[1] = id_2;
id_storage[2] = id_3;
id_storage[3] = id_4;
id_storage[4] = id_5;
id_storage[5] = id_6;
id_storage[6] = id_7;
id_storage[7] = maxim_crc8(id_storage, 7);
}
OneWireDevice::~OneWireDevice() {
if(bus != nullptr) {
bus->deattach();
}
}
void OneWireDevice::send_id() const {
if(bus != nullptr) {
bus->send(id_storage, 8);
}
}
void OneWireDevice::attach(OneWireSlave* _bus) {
bus = _bus;
}
void OneWireDevice::deattach(void) {
bus = nullptr;
}

View File

@@ -1,26 +0,0 @@
#pragma once
#include <stdint.h>
#include "maxim_crc.h"
#include "one_wire_slave.h"
class OneWireDevice {
public:
OneWireDevice(
uint8_t id_1,
uint8_t id_2,
uint8_t id_3,
uint8_t id_4,
uint8_t id_5,
uint8_t id_6,
uint8_t id_7);
~OneWireDevice();
uint8_t id_storage[8];
void send_id() const;
OneWireSlave* bus = nullptr;
void attach(OneWireSlave* _bus);
void deattach(void);
};

View File

@@ -1,12 +0,0 @@
#include "one_wire_device_ds_1990.h"
DS1990::DS1990(
uint8_t ID1,
uint8_t ID2,
uint8_t ID3,
uint8_t ID4,
uint8_t ID5,
uint8_t ID6,
uint8_t ID7)
: OneWireDevice(ID1, ID2, ID3, ID4, ID5, ID6, ID7) {
}

View File

@@ -1,16 +0,0 @@
#pragma once
#include "one_wire_device.h"
class DS1990 : public OneWireDevice {
public:
static constexpr uint8_t family_code{0x01};
DS1990(
uint8_t ID1,
uint8_t ID2,
uint8_t ID3,
uint8_t ID4,
uint8_t ID5,
uint8_t ID6,
uint8_t ID7);
};

View File

@@ -1,247 +0,0 @@
#include "one_wire_master.h"
#include "one_wire_timings.h"
OneWireMaster::OneWireMaster(const GpioPin* one_wire_gpio) {
gpio = one_wire_gpio;
reset_search();
}
OneWireMaster::~OneWireMaster() {
stop();
}
void OneWireMaster::start(void) {
hal_gpio_init(gpio, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
}
void OneWireMaster::stop(void) {
hal_gpio_init(gpio, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}
void OneWireMaster::reset_search() {
// reset the search state
last_discrepancy = 0;
last_device_flag = false;
last_family_discrepancy = 0;
for(int i = 7;; i--) {
saved_rom[i] = 0;
if(i == 0) break;
}
}
void OneWireMaster::target_search(uint8_t family_code) {
// set the search state to find SearchFamily type devices
saved_rom[0] = family_code;
for(uint8_t i = 1; i < 8; i++) saved_rom[i] = 0;
last_discrepancy = 64;
last_family_discrepancy = 0;
last_device_flag = false;
}
uint8_t OneWireMaster::search(uint8_t* newAddr, bool search_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(!last_device_flag) {
// 1-Wire reset
if(!reset()) {
// reset the search
last_discrepancy = 0;
last_device_flag = false;
last_family_discrepancy = 0;
return false;
}
// issue the search command
if(search_mode == true) {
write(0xF0); // NORMAL SEARCH
} else {
write(0xEC); // CONDITIONAL SEARCH
}
// loop to do the search
do {
// read a bit and its complement
id_bit = read_bit();
cmp_id_bit = read_bit();
// 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 < last_discrepancy)
search_direction = ((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 == 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) 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)
saved_rom[rom_byte_number] |= rom_byte_mask;
else
saved_rom[rom_byte_number] &= ~rom_byte_mask;
// serial number search direction write bit
write_bit(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
last_discrepancy = last_zero;
// check for last device
if(last_discrepancy == 0) 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 || !saved_rom[0]) {
last_discrepancy = 0;
last_device_flag = false;
last_family_discrepancy = 0;
search_result = false;
} else {
for(int i = 0; i < 8; i++) newAddr[i] = saved_rom[i];
}
return search_result;
}
bool OneWireMaster::reset(void) {
uint8_t r;
uint8_t retries = 125;
// wait until the gpio is high
hal_gpio_write(gpio, true);
do {
if(--retries == 0) return 0;
delay_us(2);
} while(!hal_gpio_read(gpio));
// pre delay
delay_us(OneWireTiming::RESET_DELAY_PRE);
// drive low
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::RESET_DRIVE);
// release
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::RESET_RELEASE);
// read and post delay
r = !hal_gpio_read(gpio);
delay_us(OneWireTiming::RESET_DELAY_POST);
return r;
}
bool OneWireMaster::read_bit(void) {
bool result;
// drive low
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::READ_DRIVE);
// release
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::READ_RELEASE);
// read and post delay
result = hal_gpio_read(gpio);
delay_us(OneWireTiming::READ_DELAY_POST);
return result;
}
void OneWireMaster::write_bit(bool value) {
if(value) {
// drive low
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_1_DRIVE);
// release
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_1_RELEASE);
} else {
// drive low
hal_gpio_write(gpio, false);
delay_us(OneWireTiming::WRITE_0_DRIVE);
// release
hal_gpio_write(gpio, true);
delay_us(OneWireTiming::WRITE_0_RELEASE);
}
}
uint8_t OneWireMaster::read(void) {
uint8_t result = 0;
for(uint8_t bitMask = 0x01; bitMask; bitMask <<= 1) {
if(read_bit()) {
result |= bitMask;
}
}
return result;
}
void OneWireMaster::read_bytes(uint8_t* buffer, uint16_t count) {
for(uint16_t i = 0; i < count; i++) {
buffer[i] = read();
}
}
void OneWireMaster::write(uint8_t value) {
uint8_t bitMask;
for(bitMask = 0x01; bitMask; bitMask <<= 1) {
write_bit((bitMask & value) ? 1 : 0);
}
}
void OneWireMaster::skip(void) {
write(0xCC);
}

View File

@@ -1,32 +0,0 @@
#pragma once
#include <furi.h>
#include <furi_hal.h>
#include "one_wire_timings.h"
class OneWireMaster {
private:
const GpioPin* gpio;
// global search state
unsigned char saved_rom[8];
uint8_t last_discrepancy;
uint8_t last_family_discrepancy;
bool last_device_flag;
public:
OneWireMaster(const GpioPin* one_wire_gpio);
~OneWireMaster();
bool reset(void);
bool read_bit(void);
uint8_t read(void);
void read_bytes(uint8_t* buf, uint16_t count);
void write_bit(bool value);
void write(uint8_t value);
void skip(void);
void start(void);
void stop(void);
void reset_search();
void target_search(uint8_t family_code);
uint8_t search(uint8_t* newAddr, bool search_mode = true);
};

View File

@@ -1,308 +0,0 @@
#include "one_wire_slave.h"
#include "callback-connector.h"
#include "main.h"
#include "one_wire_device.h"
#define OWET OneWireEmulateTiming
void OneWireSlave::start(void) {
// add exti interrupt
hal_gpio_add_int_callback(one_wire_pin_record, exti_cb, this);
// init gpio
hal_gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
pin_set_float();
// init instructions per us count
__instructions_per_us = (SystemCoreClock / 1000000.0f);
}
void OneWireSlave::stop(void) {
// deinit gpio
hal_gpio_init(one_wire_pin_record, GpioModeInput, GpioPullNo, GpioSpeedLow);
// remove exti interrupt
hal_gpio_remove_int_callback(one_wire_pin_record);
// deattach devices
deattach();
}
OneWireSlave::OneWireSlave(const GpioPin* pin) {
one_wire_pin_record = pin;
exti_cb = cbc::obtain_connector(this, &OneWireSlave::exti_callback);
}
OneWireSlave::~OneWireSlave() {
stop();
}
void OneWireSlave::attach(OneWireDevice* attached_device) {
device = attached_device;
device->attach(this);
}
void OneWireSlave::deattach(void) {
if(device != nullptr) {
device->deattach();
}
device = nullptr;
}
void OneWireSlave::set_result_callback(OneWireSlaveResultCallback result_cb, void* ctx) {
this->result_cb = result_cb;
this->result_cb_ctx = ctx;
}
void OneWireSlave::pin_set_float() {
hal_gpio_write(one_wire_pin_record, true);
}
void OneWireSlave::pin_set_low() {
hal_gpio_write(one_wire_pin_record, false);
}
void OneWireSlave::pin_init_interrupt_in_isr_ctx(void) {
hal_gpio_init(one_wire_pin_record, GpioModeInterruptRiseFall, GpioPullNo, GpioSpeedLow);
__HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
}
void OneWireSlave::pin_init_opendrain_in_isr_ctx(void) {
hal_gpio_init(one_wire_pin_record, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
__HAL_GPIO_EXTI_CLEAR_IT(one_wire_pin_record->pin);
}
OneWiteTimeType OneWireSlave::wait_while_gpio_is(OneWiteTimeType time, const bool pin_value) {
uint32_t start = DWT->CYCCNT;
uint32_t time_ticks = time * __instructions_per_us;
uint32_t time_captured;
do {
time_captured = DWT->CYCCNT;
if(hal_gpio_read(one_wire_pin_record) != pin_value) {
OneWiteTimeType remaining_time = time_ticks - (time_captured - start);
remaining_time /= __instructions_per_us;
return remaining_time;
}
} while((time_captured - start) < time_ticks);
return 0;
}
bool OneWireSlave::show_presence(void) {
// wait while master delay presence check
wait_while_gpio_is(OWET::PRESENCE_TIMEOUT, true);
// show presence
pin_set_low();
delay_us(OWET::PRESENCE_MIN);
pin_set_float();
// somebody also can show presence
const OneWiteTimeType wait_low_time = OWET::PRESENCE_MAX - OWET::PRESENCE_MIN;
// so we will wait
if(wait_while_gpio_is(wait_low_time, false) == 0) {
error = OneWireSlaveError::PRESENCE_LOW_ON_LINE;
return false;
}
return true;
}
bool OneWireSlave::receive_bit(void) {
// wait while bus is low
OneWiteTimeType time = OWET::SLOT_MAX;
time = wait_while_gpio_is(time, false);
if(time == 0) {
error = OneWireSlaveError::RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWET::MSG_HIGH_TIMEOUT;
time = wait_while_gpio_is(time, true);
if(time == 0) {
error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
return false;
}
// wait a time of zero
time = OWET::READ_MIN;
time = wait_while_gpio_is(time, false);
return (time > 0);
}
bool OneWireSlave::send_bit(bool value) {
const bool write_zero = !value;
// wait while bus is low
OneWiteTimeType time = OWET::SLOT_MAX;
time = wait_while_gpio_is(time, false);
if(time == 0) {
error = OneWireSlaveError::RESET_IN_PROGRESS;
return false;
}
// wait while bus is high
time = OWET::MSG_HIGH_TIMEOUT;
time = wait_while_gpio_is(time, true);
if(time == 0) {
error = OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH;
return false;
}
// choose write time
if(write_zero) {
pin_set_low();
time = OWET::WRITE_ZERO;
} else {
time = OWET::READ_MAX;
}
// hold line for ZERO or ONE time
delay_us(time);
pin_set_float();
return true;
}
bool OneWireSlave::send(const uint8_t* address, const uint8_t data_length) {
uint8_t bytes_sent = 0;
pin_set_float();
// bytes loop
for(; bytes_sent < data_length; ++bytes_sent) {
const uint8_t data_byte = address[bytes_sent];
// bit loop
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(!send_bit(static_cast<bool>(bit_mask & data_byte))) {
// if we cannot send first bit
if((bit_mask == 0x01) && (error == OneWireSlaveError::AWAIT_TIMESLOT_TIMEOUT_HIGH))
error = OneWireSlaveError::FIRST_BIT_OF_BYTE_TIMEOUT;
return false;
}
}
}
return true;
}
bool OneWireSlave::receive(uint8_t* data, const uint8_t data_length) {
uint8_t bytes_received = 0;
pin_set_float();
for(; bytes_received < data_length; ++bytes_received) {
uint8_t value = 0;
for(uint8_t bit_mask = 0x01; bit_mask != 0; bit_mask <<= 1) {
if(receive_bit()) value |= bit_mask;
}
data[bytes_received] = value;
}
return (bytes_received != data_length);
}
void OneWireSlave::cmd_search_rom(void) {
const uint8_t key_bytes = 8;
uint8_t* key = device->id_storage;
for(uint8_t i = 0; i < key_bytes; i++) {
uint8_t key_byte = key[i];
for(uint8_t j = 0; j < 8; j++) {
bool bit = (key_byte >> j) & 0x01;
if(!send_bit(bit)) return;
if(!send_bit(!bit)) return;
receive_bit();
if(error != OneWireSlaveError::NO_ERROR) return;
}
}
}
bool OneWireSlave::receive_and_process_cmd(void) {
uint8_t cmd;
receive(&cmd, 1);
if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
if(error != OneWireSlaveError::NO_ERROR) return false;
switch(cmd) {
case 0xF0:
// SEARCH ROM
cmd_search_rom();
return true;
case 0x0F:
case 0x33:
// READ ROM
device->send_id();
return true;
default: // Unknown command
error = OneWireSlaveError::INCORRECT_ONEWIRE_CMD;
}
if(error == OneWireSlaveError::RESET_IN_PROGRESS) return true;
return (error == OneWireSlaveError::NO_ERROR);
}
bool OneWireSlave::bus_start(void) {
bool result = true;
if(device == nullptr) {
result = false;
} else {
FURI_CRITICAL_ENTER();
pin_init_opendrain_in_isr_ctx();
error = OneWireSlaveError::NO_ERROR;
if(show_presence()) {
// TODO think about multiple command cycles
receive_and_process_cmd();
result =
(error == OneWireSlaveError::NO_ERROR ||
error == OneWireSlaveError::INCORRECT_ONEWIRE_CMD);
} else {
result = false;
}
pin_init_interrupt_in_isr_ctx();
FURI_CRITICAL_EXIT();
}
return result;
}
void OneWireSlave::exti_callback(void* _ctx) {
OneWireSlave* _this = static_cast<OneWireSlave*>(_ctx);
volatile bool input_state = hal_gpio_read(_this->one_wire_pin_record);
static uint32_t pulse_start = 0;
if(input_state) {
uint32_t pulse_length = (DWT->CYCCNT - pulse_start) / __instructions_per_us;
if(pulse_length >= OWET::RESET_MIN) {
if(pulse_length <= OWET::RESET_MAX) {
// reset cycle ok
bool result = _this->bus_start();
if(result && _this->result_cb != nullptr) {
_this->result_cb(result, _this->result_cb_ctx);
}
} else {
error = OneWireSlaveError::VERY_LONG_RESET;
}
} else {
error = OneWireSlaveError::VERY_SHORT_RESET;
}
} else {
//FALL event
pulse_start = DWT->CYCCNT;
}
}

View File

@@ -1,75 +0,0 @@
#pragma once
#include <furi.h>
#include <furi_hal.h>
#include "one_wire_timings.h"
class OneWireDevice;
typedef void (*OneWireSlaveResultCallback)(bool success, void* ctx);
class OneWireSlave {
private:
enum class OneWireSlaveError : uint8_t {
NO_ERROR = 0,
READ_TIMESLOT_TIMEOUT,
WRITE_TIMESLOT_TIMEOUT,
WAIT_RESET_TIMEOUT,
VERY_LONG_RESET,
VERY_SHORT_RESET,
PRESENCE_LOW_ON_LINE,
READ_TIMESLOT_TIMEOUT_LOW,
AWAIT_TIMESLOT_TIMEOUT_HIGH,
PRESENCE_HIGH_ON_LINE,
INCORRECT_ONEWIRE_CMD,
INCORRECT_SLAVE_USAGE,
TRIED_INCORRECT_WRITE,
FIRST_TIMESLOT_TIMEOUT,
FIRST_BIT_OF_BYTE_TIMEOUT,
RESET_IN_PROGRESS
};
const GpioPin* one_wire_pin_record;
// exti callback and its pointer
void exti_callback(void* _ctx);
void (*exti_cb)(void* _ctx);
uint32_t __instructions_per_us;
OneWireSlaveError error;
OneWireDevice* device = nullptr;
bool bus_start(void);
void pin_set_float(void);
void pin_set_low(void);
void pin_init_interrupt_in_isr_ctx(void);
void pin_init_opendrain_in_isr_ctx(void);
OneWiteTimeType wait_while_gpio_is(OneWiteTimeType time, const bool pin_value);
bool show_presence(void);
bool receive_and_process_cmd(void);
bool receive_bit(void);
bool send_bit(bool value);
void cmd_search_rom(void);
OneWireSlaveResultCallback result_cb = nullptr;
void* result_cb_ctx = nullptr;
public:
void start(void);
void stop(void);
bool send(const uint8_t* address, const uint8_t data_length);
bool receive(uint8_t* data, const uint8_t data_length = 1);
OneWireSlave(const GpioPin* pin);
~OneWireSlave();
void attach(OneWireDevice* device);
void deattach(void);
void set_result_callback(OneWireSlaveResultCallback result_cb, void* ctx);
};

View File

@@ -1,16 +0,0 @@
#include "one_wire_timings.h"
// fix pre C++17 "undefined reference" errors
constexpr const OneWiteTimeType OneWireEmulateTiming::RESET_MIN;
constexpr const OneWiteTimeType OneWireEmulateTiming::RESET_MAX;
constexpr const OneWiteTimeType OneWireEmulateTiming::PRESENCE_TIMEOUT;
constexpr const OneWiteTimeType OneWireEmulateTiming::PRESENCE_MIN;
constexpr const OneWiteTimeType OneWireEmulateTiming::PRESENCE_MAX;
constexpr const OneWiteTimeType OneWireEmulateTiming::MSG_HIGH_TIMEOUT;
constexpr const OneWiteTimeType OneWireEmulateTiming::SLOT_MAX;
constexpr const OneWiteTimeType OneWireEmulateTiming::READ_MIN;
constexpr const OneWiteTimeType OneWireEmulateTiming::READ_MAX;
constexpr const OneWiteTimeType OneWireEmulateTiming::WRITE_ZERO;

View File

@@ -1,53 +0,0 @@
#pragma once
#include <stdint.h>
class __OneWireTiming {
public:
constexpr static const uint16_t TIMING_A = 9;
constexpr static const uint16_t TIMING_B = 64;
constexpr static const uint16_t TIMING_C = 64;
constexpr static const uint16_t TIMING_D = 14;
constexpr static const uint16_t TIMING_E = 9;
constexpr static const uint16_t TIMING_F = 55;
constexpr static const uint16_t TIMING_G = 0;
constexpr static const uint16_t TIMING_H = 480;
constexpr static const uint16_t TIMING_I = 70;
constexpr static const uint16_t TIMING_J = 410;
};
class OneWireTiming {
public:
constexpr static const uint16_t WRITE_1_DRIVE = __OneWireTiming::TIMING_A;
constexpr static const uint16_t WRITE_1_RELEASE = __OneWireTiming::TIMING_B;
constexpr static const uint16_t WRITE_0_DRIVE = __OneWireTiming::TIMING_C;
constexpr static const uint16_t WRITE_0_RELEASE = __OneWireTiming::TIMING_D;
constexpr static const uint16_t READ_DRIVE = 3;
constexpr static const uint16_t READ_RELEASE = __OneWireTiming::TIMING_E;
constexpr static const uint16_t READ_DELAY_POST = __OneWireTiming::TIMING_F;
constexpr static const uint16_t RESET_DELAY_PRE = __OneWireTiming::TIMING_G;
constexpr static const uint16_t RESET_DRIVE = __OneWireTiming::TIMING_H;
constexpr static const uint16_t RESET_RELEASE = __OneWireTiming::TIMING_I;
constexpr static const uint16_t RESET_DELAY_POST = __OneWireTiming::TIMING_J;
};
typedef uint32_t OneWiteTimeType;
class OneWireEmulateTiming {
public:
constexpr static const OneWiteTimeType RESET_MIN = 270;
constexpr static const OneWiteTimeType RESET_MAX = 960;
constexpr static const OneWiteTimeType PRESENCE_TIMEOUT = 20;
constexpr static const OneWiteTimeType PRESENCE_MIN = 100;
constexpr static const OneWiteTimeType PRESENCE_MAX = 480;
constexpr static const OneWiteTimeType MSG_HIGH_TIMEOUT = 15000;
constexpr static const OneWiteTimeType SLOT_MAX = 135;
constexpr static const OneWiteTimeType READ_MIN = 20;
constexpr static const OneWiteTimeType READ_MAX = 60;
constexpr static const OneWiteTimeType WRITE_ZERO = 30;
};