[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:
66
lib/one_wire/pulse_protocols/pulse_decoder.c
Normal file
66
lib/one_wire/pulse_protocols/pulse_decoder.c
Normal 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);
|
||||
}
|
70
lib/one_wire/pulse_protocols/pulse_decoder.h
Normal file
70
lib/one_wire/pulse_protocols/pulse_decoder.h
Normal 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
|
55
lib/one_wire/pulse_protocols/pulse_glue.c
Normal file
55
lib/one_wire/pulse_protocols/pulse_glue.c
Normal 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;
|
||||
}
|
26
lib/one_wire/pulse_protocols/pulse_glue.h
Normal file
26
lib/one_wire/pulse_protocols/pulse_glue.h
Normal 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
|
67
lib/one_wire/pulse_protocols/pulse_protocol.c
Normal file
67
lib/one_wire/pulse_protocols/pulse_protocol.c
Normal 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);
|
||||
}
|
||||
}
|
122
lib/one_wire/pulse_protocols/pulse_protocol.h
Normal file
122
lib/one_wire/pulse_protocols/pulse_protocol.h
Normal 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
|
Reference in New Issue
Block a user