[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:
126
lib/one_wire/ibutton/encoder/encoder_cyfral.c
Normal file
126
lib/one_wire/ibutton/encoder/encoder_cyfral.c
Normal 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;
|
||||
}
|
||||
}
|
54
lib/one_wire/ibutton/encoder/encoder_cyfral.h
Normal file
54
lib/one_wire/ibutton/encoder/encoder_cyfral.h
Normal 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
|
93
lib/one_wire/ibutton/encoder/encoder_metakom.c
Normal file
93
lib/one_wire/ibutton/encoder/encoder_metakom.c
Normal 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;
|
||||
}
|
||||
}
|
54
lib/one_wire/ibutton/encoder/encoder_metakom.h
Normal file
54
lib/one_wire/ibutton/encoder/encoder_metakom.h
Normal 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
|
Reference in New Issue
Block a user