[FL-2219, FL-2251] System, FuriCore, FuriHal: various bug fixes and improvements (#986)
* Replace irq shenanigans with critical section * Power: halt system on power off instead of crash. * Gui: properly handle input event on NULL current_view * FuriHal: correct gpio configuration sequence * FuriHal: cleanup uart initialization. Makefile: allow to disable thread support. * Loader: improve locking, fix simultaneous app start crash, full command line args support for gui apps, more consistent insomnia * Loader: correct spelling * FuriHal: increase gpio configuration readability * FuriHal: correct gpio configuration error when mode is GpioModeEventRiseFall Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com>
This commit is contained in:
@@ -22,6 +22,8 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <furi/common_defines.h>
|
||||
|
||||
#include "cmsis_os2.h" // ::CMSIS:RTOS2
|
||||
#include "cmsis_compiler.h" // Compiler agnostic definitions
|
||||
#include "os_tick.h" // OS Tick API
|
||||
@@ -455,11 +457,10 @@ uint32_t osKernelGetTickFreq (void) {
|
||||
Get the RTOS kernel system timer count.
|
||||
*/
|
||||
uint32_t osKernelGetSysTimerCount (void) {
|
||||
uint32_t irqmask = IS_IRQ_MASKED();
|
||||
TickType_t ticks;
|
||||
uint32_t val;
|
||||
|
||||
__disable_irq();
|
||||
FURI_CRITICAL_ENTER();
|
||||
|
||||
ticks = xTaskGetTickCount();
|
||||
val = OS_Tick_GetCount();
|
||||
@@ -471,9 +472,7 @@ uint32_t osKernelGetSysTimerCount (void) {
|
||||
}
|
||||
val += ticks * OS_Tick_GetInterval();
|
||||
|
||||
if (irqmask == 0U) {
|
||||
__enable_irq();
|
||||
}
|
||||
FURI_CRITICAL_EXIT();
|
||||
|
||||
/* Return system timer count */
|
||||
return (val);
|
||||
|
@@ -1,96 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
class CyfralTiming {
|
||||
public:
|
||||
constexpr static const uint8_t ZERO_HIGH = 50;
|
||||
constexpr static const uint8_t ZERO_LOW = 70;
|
||||
constexpr static const uint8_t ONE_HIGH = 100;
|
||||
constexpr static const uint8_t ONE_LOW = 70;
|
||||
};
|
||||
|
||||
class CyfralEmulator {
|
||||
private:
|
||||
void send_nibble(uint8_t nibble);
|
||||
void send_byte(uint8_t data);
|
||||
inline void send_bit(bool bit);
|
||||
const GpioPin* emulate_pin_record;
|
||||
|
||||
public:
|
||||
CyfralEmulator(const GpioPin* emulate_pin);
|
||||
~CyfralEmulator();
|
||||
void send(uint8_t* data, uint8_t count = 1, uint8_t repeat = 1);
|
||||
void start(void);
|
||||
void stop(void);
|
||||
};
|
||||
|
||||
// 7 = 0 1 1 1
|
||||
// B = 1 0 1 1
|
||||
// D = 1 1 0 1
|
||||
// E = 1 1 1 0
|
||||
|
||||
void CyfralEmulator::send_nibble(uint8_t nibble) {
|
||||
for(uint8_t i = 0; i < 4; i++) {
|
||||
bool bit = nibble & (0b1000 >> i);
|
||||
send_bit(bit);
|
||||
}
|
||||
}
|
||||
|
||||
void CyfralEmulator::send_byte(uint8_t data) {
|
||||
for(uint8_t i = 0; i < 8; i++) {
|
||||
bool bit = data & (0b10000000 >> i);
|
||||
send_bit(bit);
|
||||
}
|
||||
}
|
||||
|
||||
void CyfralEmulator::send_bit(bool bit) {
|
||||
if(!bit) {
|
||||
hal_gpio_write(&ibutton_gpio, false);
|
||||
delay_us(CyfralTiming::ZERO_LOW);
|
||||
hal_gpio_write(&ibutton_gpio, true);
|
||||
delay_us(CyfralTiming::ZERO_HIGH);
|
||||
hal_gpio_write(&ibutton_gpio, false);
|
||||
delay_us(CyfralTiming::ZERO_LOW);
|
||||
} else {
|
||||
hal_gpio_write(&ibutton_gpio, true);
|
||||
delay_us(CyfralTiming::ONE_HIGH);
|
||||
hal_gpio_write(&ibutton_gpio, false);
|
||||
delay_us(CyfralTiming::ONE_LOW);
|
||||
}
|
||||
}
|
||||
|
||||
CyfralEmulator::CyfralEmulator(const GpioPin* emulate_pin) {
|
||||
emulate_pin_record = emulate_pin;
|
||||
}
|
||||
|
||||
CyfralEmulator::~CyfralEmulator() {
|
||||
}
|
||||
|
||||
void CyfralEmulator::send(uint8_t* data, uint8_t count, uint8_t repeat) {
|
||||
osKernelLock();
|
||||
__disable_irq();
|
||||
|
||||
for(uint8_t i = 0; i < repeat; i++) {
|
||||
// start sequence
|
||||
send_nibble(0x01);
|
||||
|
||||
// send data
|
||||
for(uint8_t i = 0; i < count; i++) {
|
||||
send_byte(data[i]);
|
||||
}
|
||||
}
|
||||
|
||||
__enable_irq();
|
||||
osKernelUnlock();
|
||||
}
|
||||
|
||||
void CyfralEmulator::start(void) {
|
||||
hal_gpio_init(emulate_pin_record, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
||||
hal_gpio_write(emulate_pin_record, false);
|
||||
}
|
||||
|
||||
void CyfralEmulator::stop(void) {
|
||||
hal_gpio_init(emulate_pin_record, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
|
||||
}
|
@@ -1,272 +0,0 @@
|
||||
#pragma once
|
||||
#include <furi.h>
|
||||
|
||||
enum class CyfralReaderError : uint8_t {
|
||||
NO_ERROR = 0,
|
||||
UNABLE_TO_DETECT = 1,
|
||||
RAW_DATA_SIZE_ERROR = 2,
|
||||
UNKNOWN_NIBBLE_VALUE = 3,
|
||||
NO_START_NIBBLE = 4,
|
||||
};
|
||||
|
||||
class CyfralReader {
|
||||
private:
|
||||
ADC_HandleTypeDef adc_config;
|
||||
ADC_TypeDef* adc_instance;
|
||||
uint32_t adc_channel;
|
||||
|
||||
void get_line_minmax(uint16_t times, uint32_t* min_level, uint32_t* max_level);
|
||||
void capture_data(bool* data, uint16_t capture_size, uint32_t line_min, uint32_t line_max);
|
||||
bool parse_data(bool* raw_data, uint16_t capture_size, uint8_t* data, uint8_t count);
|
||||
uint32_t search_array_in_array(
|
||||
const bool* haystack,
|
||||
const uint32_t haystack_size,
|
||||
const bool* needle,
|
||||
const uint32_t needle_size);
|
||||
|
||||
// key is 9 nibbles
|
||||
static const uint16_t bits_in_nibble = 4;
|
||||
static const uint16_t key_length = 9;
|
||||
static const uint32_t capture_size = key_length * bits_in_nibble * 2;
|
||||
CyfralReaderError error;
|
||||
|
||||
public:
|
||||
CyfralReader(ADC_TypeDef* adc, uint32_t Channel);
|
||||
~CyfralReader();
|
||||
void start(void);
|
||||
void stop(void);
|
||||
bool read(uint8_t* data, uint8_t count);
|
||||
};
|
||||
|
||||
void CyfralReader::get_line_minmax(uint16_t times, uint32_t* min_level, uint32_t* max_level) {
|
||||
uint32_t in = 0;
|
||||
uint32_t min = UINT_MAX;
|
||||
uint32_t max = 0;
|
||||
|
||||
for(uint32_t i = 0; i < 256; i++) {
|
||||
HAL_ADC_Start(&adc_config);
|
||||
HAL_ADC_PollForConversion(&adc_config, 100);
|
||||
in = HAL_ADC_GetValue(&adc_config);
|
||||
if(in < min) min = in;
|
||||
if(in > max) max = in;
|
||||
}
|
||||
|
||||
*min_level = min;
|
||||
*max_level = max;
|
||||
}
|
||||
|
||||
void CyfralReader::capture_data(
|
||||
bool* data,
|
||||
uint16_t capture_size,
|
||||
uint32_t line_min,
|
||||
uint32_t line_max) {
|
||||
uint32_t input_value = 0;
|
||||
bool last_input_value = 0;
|
||||
|
||||
uint32_t diff = line_max - line_min;
|
||||
uint32_t mid = line_min + diff / 2;
|
||||
|
||||
uint32_t low_threshold = mid - (diff / 4);
|
||||
uint32_t high_threshold = mid - (diff / 4);
|
||||
|
||||
uint16_t capture_position = 0;
|
||||
uint32_t instructions_per_us = (SystemCoreClock / 1000000.0f);
|
||||
uint32_t time_threshold = 75 * instructions_per_us;
|
||||
uint32_t capture_max_time = 140 * (capture_size * 2) * instructions_per_us;
|
||||
|
||||
uint32_t start = DWT->CYCCNT;
|
||||
uint32_t end = DWT->CYCCNT;
|
||||
|
||||
memset(data, 0, capture_size);
|
||||
|
||||
osKernelLock();
|
||||
|
||||
uint32_t capture_start = DWT->CYCCNT;
|
||||
while((capture_position < capture_size) &&
|
||||
((DWT->CYCCNT - capture_start) < capture_max_time)) {
|
||||
// read adc
|
||||
HAL_ADC_Start(&adc_config);
|
||||
HAL_ADC_PollForConversion(&adc_config, 100);
|
||||
input_value = HAL_ADC_GetValue(&adc_config);
|
||||
|
||||
// low to high transition
|
||||
if((input_value > high_threshold) && last_input_value == 0) {
|
||||
last_input_value = 1;
|
||||
start = DWT->CYCCNT;
|
||||
}
|
||||
|
||||
// high to low transition
|
||||
if((input_value < low_threshold) && last_input_value == 1) {
|
||||
last_input_value = 0;
|
||||
end = DWT->CYCCNT;
|
||||
|
||||
// check transition time
|
||||
if(end - start < time_threshold) {
|
||||
data[capture_position] = 1;
|
||||
capture_position++;
|
||||
} else {
|
||||
data[capture_position] = 0;
|
||||
capture_position++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
osKernelUnlock();
|
||||
}
|
||||
|
||||
uint32_t CyfralReader::search_array_in_array(
|
||||
const bool* haystack,
|
||||
const uint32_t haystack_size,
|
||||
const bool* needle,
|
||||
const uint32_t needle_size) {
|
||||
uint32_t haystack_index = 0, needle_index = 0;
|
||||
|
||||
while(haystack_index < haystack_size && needle_index < needle_size) {
|
||||
if(haystack[haystack_index] == needle[needle_index]) {
|
||||
haystack_index++;
|
||||
needle_index++;
|
||||
if(needle_index == needle_size) {
|
||||
return (haystack_index - needle_size);
|
||||
};
|
||||
} else {
|
||||
haystack_index = haystack_index - needle_index + 1;
|
||||
needle_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return haystack_index;
|
||||
}
|
||||
|
||||
bool CyfralReader::parse_data(bool* raw_data, uint16_t capture_size, uint8_t* data, uint8_t count) {
|
||||
const bool start_nibble[bits_in_nibble] = {1, 1, 1, 0};
|
||||
uint32_t start_position =
|
||||
search_array_in_array(raw_data, capture_size, start_nibble, bits_in_nibble);
|
||||
uint32_t end_position = 0;
|
||||
|
||||
memset(data, 0, count);
|
||||
|
||||
if(start_position < capture_size) {
|
||||
start_position = start_position + bits_in_nibble;
|
||||
end_position = start_position + count * 2 * bits_in_nibble;
|
||||
|
||||
if(end_position >= capture_size) {
|
||||
error = CyfralReaderError::RAW_DATA_SIZE_ERROR;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool first_nibble = true;
|
||||
uint8_t data_position = 0;
|
||||
uint8_t nibble_value = 0;
|
||||
|
||||
while(data_position < count) {
|
||||
nibble_value = !raw_data[start_position] << 3 | !raw_data[start_position + 1] << 2 |
|
||||
!raw_data[start_position + 2] << 1 | !raw_data[start_position + 3];
|
||||
|
||||
switch(nibble_value) {
|
||||
case(0x7):
|
||||
case(0xB):
|
||||
case(0xD):
|
||||
case(0xE):
|
||||
break;
|
||||
default:
|
||||
error = CyfralReaderError::UNKNOWN_NIBBLE_VALUE;
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(first_nibble) {
|
||||
data[data_position] |= nibble_value << 4;
|
||||
} else {
|
||||
data[data_position] |= nibble_value;
|
||||
}
|
||||
|
||||
first_nibble = !first_nibble;
|
||||
|
||||
if(first_nibble) {
|
||||
data_position++;
|
||||
}
|
||||
|
||||
start_position = start_position + bits_in_nibble;
|
||||
}
|
||||
|
||||
error = CyfralReaderError::NO_ERROR;
|
||||
return true;
|
||||
}
|
||||
|
||||
error = CyfralReaderError::NO_START_NIBBLE;
|
||||
return false;
|
||||
}
|
||||
|
||||
CyfralReader::CyfralReader(ADC_TypeDef* adc, uint32_t channel) {
|
||||
adc_instance = adc;
|
||||
adc_channel = channel;
|
||||
}
|
||||
|
||||
CyfralReader::~CyfralReader() {
|
||||
}
|
||||
|
||||
void CyfralReader::start(void) {
|
||||
ADC_ChannelConfTypeDef sConfig = {0};
|
||||
|
||||
// init ADC
|
||||
adc_config.Instance = adc_instance;
|
||||
adc_config.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
|
||||
adc_config.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
adc_config.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
adc_config.Init.ScanConvMode = ADC_SCAN_DISABLE;
|
||||
adc_config.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
||||
adc_config.Init.LowPowerAutoWait = DISABLE;
|
||||
adc_config.Init.ContinuousConvMode = DISABLE;
|
||||
adc_config.Init.NbrOfConversion = 1;
|
||||
adc_config.Init.DiscontinuousConvMode = DISABLE;
|
||||
adc_config.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
adc_config.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
adc_config.Init.DMAContinuousRequests = DISABLE;
|
||||
adc_config.Init.Overrun = ADC_OVR_DATA_PRESERVED;
|
||||
adc_config.Init.OversamplingMode = DISABLE;
|
||||
if(HAL_ADC_Init(&adc_config) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
|
||||
// init channel
|
||||
sConfig.Channel = adc_channel;
|
||||
sConfig.Rank = ADC_REGULAR_RANK_1;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
|
||||
sConfig.SingleDiff = ADC_SINGLE_ENDED;
|
||||
sConfig.OffsetNumber = ADC_OFFSET_NONE;
|
||||
sConfig.Offset = 0;
|
||||
if(HAL_ADC_ConfigChannel(&adc_config, &sConfig) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void CyfralReader::stop(void) {
|
||||
HAL_ADC_DeInit(&adc_config);
|
||||
}
|
||||
|
||||
bool CyfralReader::read(uint8_t* data, uint8_t count) {
|
||||
uint32_t line_level_min, line_level_max;
|
||||
bool raw_data[capture_size];
|
||||
bool result = false;
|
||||
error = CyfralReaderError::NO_ERROR;
|
||||
|
||||
// calibrate
|
||||
get_line_minmax(256, &line_level_min, &line_level_max);
|
||||
|
||||
// TODO think about other detection method
|
||||
// key not on line
|
||||
if(line_level_max > 2000) {
|
||||
error = CyfralReaderError::UNABLE_TO_DETECT;
|
||||
return false;
|
||||
}
|
||||
|
||||
// capturing raw data consisting of bits
|
||||
capture_data(raw_data, capture_size, line_level_min, line_level_max);
|
||||
|
||||
// parse captured data
|
||||
if(parse_data(raw_data, capture_size, data, count)) {
|
||||
result = true;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
@@ -1,283 +0,0 @@
|
||||
#pragma once
|
||||
#include <furi.h>
|
||||
#include "callback-connector.h"
|
||||
#include <atomic>
|
||||
|
||||
enum class CyfralReaderCompError : uint8_t {
|
||||
NO_ERROR = 0,
|
||||
UNABLE_TO_DETECT = 1,
|
||||
RAW_DATA_SIZE_ERROR = 2,
|
||||
UNKNOWN_NIBBLE_VALUE = 3,
|
||||
NO_START_NIBBLE = 4,
|
||||
NOT_ENOUGH_DATA = 5,
|
||||
};
|
||||
|
||||
extern COMP_HandleTypeDef hcomp1;
|
||||
|
||||
typedef struct {
|
||||
bool value;
|
||||
uint32_t dwt_value;
|
||||
} CompEvent;
|
||||
|
||||
class CyfralReaderComp {
|
||||
private:
|
||||
bool capture_data(bool* data, uint16_t capture_size);
|
||||
bool parse_data(bool* raw_data, uint16_t capture_size, uint8_t* data, uint8_t count);
|
||||
uint32_t search_array_in_array(
|
||||
const bool* haystack,
|
||||
const uint32_t haystack_size,
|
||||
const bool* needle,
|
||||
const uint32_t needle_size);
|
||||
|
||||
// key is 9 nibbles
|
||||
static const uint16_t bits_in_nibble = 4;
|
||||
static const uint16_t key_length = 9;
|
||||
static const uint32_t capture_size = key_length * bits_in_nibble * 2;
|
||||
CyfralReaderCompError error;
|
||||
const GpioPin* pin_record;
|
||||
|
||||
std::atomic<bool> ready_to_process;
|
||||
void comparator_trigger_callback(void* hcomp, void* comp_ctx);
|
||||
osMessageQueueId_t comp_event_queue;
|
||||
|
||||
public:
|
||||
CyfralReaderComp(const GpioPin* emulate_pin);
|
||||
~CyfralReaderComp();
|
||||
void start(void);
|
||||
void stop(void);
|
||||
bool read(uint8_t* data, uint8_t count);
|
||||
};
|
||||
|
||||
bool CyfralReaderComp::capture_data(bool* data, uint16_t capture_size) {
|
||||
uint32_t prev_timing = 0;
|
||||
uint16_t data_index = 0;
|
||||
CompEvent event_0, event_1;
|
||||
osStatus_t status;
|
||||
|
||||
// read first event to get initial timing
|
||||
status = osMessageQueueGet(comp_event_queue, &event_0, NULL, 0);
|
||||
|
||||
if(status != osOK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
prev_timing = event_0.dwt_value;
|
||||
|
||||
// read second event until we get 0
|
||||
while(1) {
|
||||
status = osMessageQueueGet(comp_event_queue, &event_0, NULL, 0);
|
||||
if(status != osOK) {
|
||||
return false;
|
||||
}
|
||||
prev_timing = event_0.dwt_value;
|
||||
if(event_0.value == 0) break;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
// if event "zero" correct
|
||||
if(status == osOK && event_0.value == 0) {
|
||||
// get timing
|
||||
event_0.dwt_value -= prev_timing;
|
||||
prev_timing += event_0.dwt_value;
|
||||
|
||||
// read next event
|
||||
status = osMessageQueueGet(comp_event_queue, &event_1, NULL, 0);
|
||||
|
||||
// if event "one" correct
|
||||
if(status == osOK && event_1.value == 1) {
|
||||
// get timing
|
||||
event_1.dwt_value -= prev_timing;
|
||||
prev_timing += event_1.dwt_value;
|
||||
|
||||
// calculate percentage of event "one" to full timing
|
||||
uint32_t full_timing = event_0.dwt_value + event_1.dwt_value;
|
||||
uint32_t percentage_1 = 1000000 / full_timing * event_1.dwt_value;
|
||||
|
||||
// write captured data
|
||||
data[data_index] = percentage_1 > 500000 ? 0 : 1;
|
||||
data_index++;
|
||||
if(data_index >= capture_size) return true;
|
||||
|
||||
status = osMessageQueueGet(comp_event_queue, &event_0, NULL, 0);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
osMessageQueueReset(comp_event_queue);
|
||||
}
|
||||
|
||||
uint32_t CyfralReaderComp::search_array_in_array(
|
||||
const bool* haystack,
|
||||
const uint32_t haystack_size,
|
||||
const bool* needle,
|
||||
const uint32_t needle_size) {
|
||||
uint32_t haystack_index = 0, needle_index = 0;
|
||||
|
||||
while(haystack_index < haystack_size && needle_index < needle_size) {
|
||||
if(haystack[haystack_index] == needle[needle_index]) {
|
||||
haystack_index++;
|
||||
needle_index++;
|
||||
if(needle_index == needle_size) {
|
||||
return (haystack_index - needle_size);
|
||||
};
|
||||
} else {
|
||||
haystack_index = haystack_index - needle_index + 1;
|
||||
needle_index = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return haystack_index;
|
||||
}
|
||||
|
||||
void CyfralReaderComp::comparator_trigger_callback(void* hcomp, void* comp_ctx) {
|
||||
CyfralReaderComp* _this = static_cast<CyfralReaderComp*>(comp_ctx);
|
||||
COMP_HandleTypeDef* _hcomp = static_cast<COMP_HandleTypeDef*>(hcomp);
|
||||
|
||||
// check that hw is comparator 1
|
||||
if(_hcomp != &hcomp1) return;
|
||||
|
||||
// if queue if not full
|
||||
if(_this->ready_to_process == false) {
|
||||
// send event to queue
|
||||
CompEvent event;
|
||||
// TOOD F4 and F5 differ
|
||||
event.value = (HAL_COMP_GetOutputLevel(_hcomp) == COMP_OUTPUT_LEVEL_LOW);
|
||||
event.dwt_value = DWT->CYCCNT;
|
||||
osStatus_t status = osMessageQueuePut(_this->comp_event_queue, &event, 0, 0);
|
||||
|
||||
// queue is full, so we need to process data
|
||||
if(status != osOK) {
|
||||
_this->ready_to_process = true;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
bool CyfralReaderComp::parse_data(
|
||||
bool* raw_data,
|
||||
uint16_t capture_size,
|
||||
uint8_t* data,
|
||||
uint8_t count) {
|
||||
const bool start_nibble[bits_in_nibble] = {1, 1, 1, 0};
|
||||
uint32_t start_position =
|
||||
search_array_in_array(raw_data, capture_size, start_nibble, bits_in_nibble);
|
||||
uint32_t end_position = 0;
|
||||
|
||||
memset(data, 0, count);
|
||||
|
||||
if(start_position < capture_size) {
|
||||
start_position = start_position + bits_in_nibble;
|
||||
end_position = start_position + count * 2 * bits_in_nibble;
|
||||
|
||||
if(end_position >= capture_size) {
|
||||
error = CyfralReaderCompError::RAW_DATA_SIZE_ERROR;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool first_nibble = true;
|
||||
uint8_t data_position = 0;
|
||||
uint8_t nibble_value = 0;
|
||||
|
||||
while(data_position < count) {
|
||||
nibble_value = !raw_data[start_position] << 3 | !raw_data[start_position + 1] << 2 |
|
||||
!raw_data[start_position + 2] << 1 | !raw_data[start_position + 3];
|
||||
|
||||
switch(nibble_value) {
|
||||
case(0x7):
|
||||
case(0xB):
|
||||
case(0xD):
|
||||
case(0xE):
|
||||
break;
|
||||
default:
|
||||
error = CyfralReaderCompError::UNKNOWN_NIBBLE_VALUE;
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
if(first_nibble) {
|
||||
data[data_position] |= nibble_value << 4;
|
||||
} else {
|
||||
data[data_position] |= nibble_value;
|
||||
}
|
||||
|
||||
first_nibble = !first_nibble;
|
||||
|
||||
if(first_nibble) {
|
||||
data_position++;
|
||||
}
|
||||
|
||||
start_position = start_position + bits_in_nibble;
|
||||
}
|
||||
|
||||
error = CyfralReaderCompError::NO_ERROR;
|
||||
return true;
|
||||
}
|
||||
|
||||
error = CyfralReaderCompError::NO_START_NIBBLE;
|
||||
return false;
|
||||
}
|
||||
|
||||
CyfralReaderComp::CyfralReaderComp(const GpioPin* gpio_pin) {
|
||||
pin_record = gpio_pin;
|
||||
}
|
||||
|
||||
CyfralReaderComp::~CyfralReaderComp() {
|
||||
}
|
||||
|
||||
void CyfralReaderComp::start(void) {
|
||||
// pulldown lf-rfid pins to prevent interference
|
||||
// TODO open record
|
||||
GpioPin rfid_pull_pin = {.port = RFID_PULL_GPIO_Port, .pin = RFID_PULL_Pin};
|
||||
hal_gpio_init((GpioPin*)&rfid_pull_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
||||
hal_gpio_write((GpioPin*)&rfid_pull_pin, false);
|
||||
|
||||
// TODO open record
|
||||
GpioPin rfid_out_pin = {.port = RFID_OUT_GPIO_Port, .pin = RFID_OUT_Pin};
|
||||
hal_gpio_init((GpioPin*)&rfid_out_pin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedLow);
|
||||
hal_gpio_write((GpioPin*)&rfid_out_pin, false);
|
||||
|
||||
// connect comparator callback
|
||||
void* comp_ctx = this;
|
||||
comp_event_queue = osMessageQueueNew(capture_size * 2 + 2, sizeof(CompEvent), NULL);
|
||||
ready_to_process = false;
|
||||
|
||||
auto cmp_cb = cbc::obtain_connector(this, &CyfralReaderComp::comparator_trigger_callback);
|
||||
api_interrupt_add(cmp_cb, InterruptTypeComparatorTrigger, comp_ctx);
|
||||
|
||||
// start comaparator
|
||||
HAL_COMP_Start(&hcomp1);
|
||||
}
|
||||
|
||||
void CyfralReaderComp::stop(void) {
|
||||
// stop comaparator
|
||||
HAL_COMP_Stop(&hcomp1);
|
||||
|
||||
// disconnect comparator callback
|
||||
auto cmp_cb = cbc::obtain_connector(this, &CyfralReaderComp::comparator_trigger_callback);
|
||||
api_interrupt_remove(cmp_cb, InterruptTypeComparatorTrigger);
|
||||
osMessageQueueDelete(comp_event_queue);
|
||||
}
|
||||
|
||||
bool CyfralReaderComp::read(uint8_t* data, uint8_t count) {
|
||||
bool raw_data[capture_size];
|
||||
bool result = false;
|
||||
error = CyfralReaderCompError::NO_ERROR;
|
||||
|
||||
if(ready_to_process == false) {
|
||||
error = CyfralReaderCompError::NOT_ENOUGH_DATA;
|
||||
} else {
|
||||
memset(raw_data, 0, sizeof(bool) * capture_size);
|
||||
if(capture_data(raw_data, capture_size)) {
|
||||
if(parse_data(raw_data, capture_size, data, count)) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
ready_to_process = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
@@ -1,320 +0,0 @@
|
||||
#include "blanks_writer.h"
|
||||
|
||||
class RW1990_1 {
|
||||
public:
|
||||
constexpr static const uint8_t CMD_WRITE_RECORD_FLAG = 0xD1;
|
||||
constexpr static const uint8_t CMD_READ_RECORD_FLAG = 0xB5;
|
||||
constexpr static const uint8_t CMD_WRITE_ROM = 0xD5;
|
||||
};
|
||||
|
||||
class RW1990_2 {
|
||||
public:
|
||||
constexpr static const uint8_t CMD_WRITE_RECORD_FLAG = 0x1D;
|
||||
constexpr static const uint8_t CMD_READ_RECORD_FLAG = 0x1E;
|
||||
constexpr static const uint8_t CMD_WRITE_ROM = 0xD5;
|
||||
};
|
||||
|
||||
class TM2004 {
|
||||
public:
|
||||
constexpr static const uint8_t CMD_READ_STATUS = 0xAA;
|
||||
constexpr static const uint8_t CMD_READ_MEMORY = 0xF0;
|
||||
constexpr static const uint8_t CMD_WRITE_ROM = 0x3C;
|
||||
constexpr static const uint8_t CMD_FINALIZATION = 0x35;
|
||||
|
||||
constexpr static const uint8_t ANSWER_READ_MEMORY = 0xF5;
|
||||
};
|
||||
|
||||
class TM01 {
|
||||
public:
|
||||
constexpr static const uint8_t CMD_WRITE_RECORD_FLAG = 0xC1;
|
||||
constexpr static const uint8_t CMD_WRITE_ROM = 0xC5;
|
||||
constexpr static const uint8_t CMD_SWITCH_TO_CYFRAL = 0xCA;
|
||||
constexpr static const uint8_t CMD_SWITCH_TO_METAKOM = 0xCB;
|
||||
};
|
||||
|
||||
class DS1990 {
|
||||
public:
|
||||
constexpr static const uint8_t CMD_READ_ROM = 0x33;
|
||||
};
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <furi_hal.h>
|
||||
|
||||
void BlanksWriter::onewire_release(void) {
|
||||
hal_gpio_write(gpio, true);
|
||||
}
|
||||
|
||||
void BlanksWriter::onewire_write_one_bit(bool value, uint32_t delay = 10000) {
|
||||
onewire->write_bit(value);
|
||||
delay_us(delay);
|
||||
onewire_release();
|
||||
}
|
||||
|
||||
BlanksWriter::BlanksWriter(const GpioPin* one_wire_gpio) {
|
||||
gpio = one_wire_gpio;
|
||||
onewire = new OneWireMaster(gpio);
|
||||
}
|
||||
|
||||
BlanksWriter::~BlanksWriter() {
|
||||
free(onewire);
|
||||
}
|
||||
|
||||
WriterResult BlanksWriter::write(KeyType type, const uint8_t* key, uint8_t key_length) {
|
||||
uint8_t write_result = -1;
|
||||
WriterResult result = WR_ERROR;
|
||||
|
||||
bool same_key = false;
|
||||
|
||||
osKernelLock();
|
||||
bool presence = onewire->reset();
|
||||
osKernelUnlock();
|
||||
|
||||
if(presence) {
|
||||
switch(type) {
|
||||
case KeyType::KEY_DS1990:
|
||||
same_key = compare_key_ds1990(key, key_length);
|
||||
|
||||
if(!same_key) {
|
||||
// currently we can write:
|
||||
// RW1990, TM08v2, TM08vi-2 by write_1990_1()
|
||||
// RW2004, RW2004 with EEPROM by write_TM2004();
|
||||
|
||||
if(write_result != 1) {
|
||||
write_result = write_1990_1(key, key_length);
|
||||
}
|
||||
if(write_result != 1) {
|
||||
write_result = write_1990_2(key, key_length);
|
||||
}
|
||||
if(write_result != 1) {
|
||||
write_result = write_TM2004(key, key_length);
|
||||
}
|
||||
|
||||
if(write_result == 1) {
|
||||
result = WR_OK;
|
||||
} else if(write_result == 0) {
|
||||
result = WR_ERROR;
|
||||
}
|
||||
} else {
|
||||
write_result = 0;
|
||||
result = WR_SAME_KEY;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool BlanksWriter::write_TM2004(const uint8_t* key, uint8_t key_length) {
|
||||
uint8_t answer;
|
||||
bool result = true;
|
||||
|
||||
osKernelLock();
|
||||
__disable_irq();
|
||||
|
||||
// write rom, addr is 0x0000
|
||||
onewire->reset();
|
||||
onewire->write(TM2004::CMD_WRITE_ROM);
|
||||
onewire->write(0x00);
|
||||
onewire->write(0x00);
|
||||
|
||||
// write key
|
||||
for(uint8_t i = 0; i < key_length; i++) {
|
||||
// write key byte
|
||||
onewire->write(key[i]);
|
||||
answer = onewire->read();
|
||||
// TODO: check answer CRC
|
||||
|
||||
// pulse indicating that data is correct
|
||||
delay_us(600);
|
||||
onewire_write_one_bit(1, 50000);
|
||||
|
||||
// read writed key byte
|
||||
answer = onewire->read();
|
||||
|
||||
// check that writed and readed are same
|
||||
if(key[i] != answer) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
onewire->reset();
|
||||
|
||||
__enable_irq();
|
||||
osKernelUnlock();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool BlanksWriter::write_1990_1(const uint8_t* key, uint8_t key_length) {
|
||||
bool result = true;
|
||||
|
||||
osKernelLock();
|
||||
__disable_irq();
|
||||
|
||||
// unlock
|
||||
onewire->reset();
|
||||
onewire->write(RW1990_1::CMD_WRITE_RECORD_FLAG);
|
||||
delay_us(10);
|
||||
onewire_write_one_bit(0, 5000);
|
||||
|
||||
// write key
|
||||
onewire->reset();
|
||||
onewire->write(RW1990_1::CMD_WRITE_ROM);
|
||||
for(uint8_t i = 0; i < key_length; i++) {
|
||||
// inverted key for RW1990.1
|
||||
write_byte_ds1990(~key[i]);
|
||||
delay_us(30000);
|
||||
}
|
||||
|
||||
// lock
|
||||
onewire->write(RW1990_1::CMD_WRITE_RECORD_FLAG);
|
||||
onewire_write_one_bit(1);
|
||||
|
||||
__enable_irq();
|
||||
osKernelUnlock();
|
||||
|
||||
if(!compare_key_ds1990(key, key_length)) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool BlanksWriter::write_1990_2(const uint8_t* key, uint8_t key_length) {
|
||||
bool result = true;
|
||||
|
||||
osKernelLock();
|
||||
__disable_irq();
|
||||
|
||||
// unlock
|
||||
onewire->reset();
|
||||
onewire->write(RW1990_2::CMD_WRITE_RECORD_FLAG);
|
||||
delay_us(10);
|
||||
onewire_write_one_bit(1, 5000);
|
||||
|
||||
// write key
|
||||
onewire->reset();
|
||||
onewire->write(RW1990_2::CMD_WRITE_ROM);
|
||||
for(uint8_t i = 0; i < key_length; i++) {
|
||||
write_byte_ds1990(key[i]);
|
||||
delay_us(30000);
|
||||
}
|
||||
|
||||
// lock
|
||||
onewire->write(RW1990_2::CMD_WRITE_RECORD_FLAG);
|
||||
onewire_write_one_bit(0);
|
||||
|
||||
__enable_irq();
|
||||
osKernelUnlock();
|
||||
|
||||
if(!compare_key_ds1990(key, key_length)) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// TODO: untested
|
||||
bool BlanksWriter::write_TM01(KeyType type, const uint8_t* key, uint8_t key_length) {
|
||||
bool result = true;
|
||||
|
||||
osKernelLock();
|
||||
__disable_irq();
|
||||
|
||||
// unlock
|
||||
onewire->reset();
|
||||
onewire->write(TM01::CMD_WRITE_RECORD_FLAG);
|
||||
onewire_write_one_bit(1, 10000);
|
||||
|
||||
// write key
|
||||
onewire->reset();
|
||||
onewire->write(TM01::CMD_WRITE_ROM);
|
||||
|
||||
// TODO: key types
|
||||
//if(type == KEY_METAKOM || type == KEY_CYFRAL) {
|
||||
//} else {
|
||||
for(uint8_t i = 0; i < key_length; i++) {
|
||||
write_byte_ds1990(key[i]);
|
||||
delay_us(10000);
|
||||
}
|
||||
//}
|
||||
|
||||
// lock
|
||||
onewire->write(TM01::CMD_WRITE_RECORD_FLAG);
|
||||
onewire_write_one_bit(0, 10000);
|
||||
|
||||
__enable_irq();
|
||||
osKernelUnlock();
|
||||
|
||||
if(!compare_key_ds1990(key, key_length)) {
|
||||
result = false;
|
||||
}
|
||||
|
||||
osKernelLock();
|
||||
__disable_irq();
|
||||
|
||||
if(type == KEY_METAKOM || type == KEY_CYFRAL) {
|
||||
onewire->reset();
|
||||
if(type == KEY_CYFRAL)
|
||||
onewire->write(TM01::CMD_SWITCH_TO_CYFRAL);
|
||||
else
|
||||
onewire->write(TM01::CMD_SWITCH_TO_METAKOM);
|
||||
onewire_write_one_bit(1);
|
||||
}
|
||||
|
||||
__enable_irq();
|
||||
osKernelUnlock();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void BlanksWriter::write_byte_ds1990(uint8_t data) {
|
||||
for(uint8_t n_bit = 0; n_bit < 8; n_bit++) {
|
||||
onewire->write_bit(data & 1);
|
||||
onewire_release();
|
||||
delay_us(5000);
|
||||
data = data >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool BlanksWriter::compare_key_ds1990(const uint8_t* key, uint8_t key_length) {
|
||||
uint8_t buff[key_length];
|
||||
bool result = false;
|
||||
|
||||
osKernelLock();
|
||||
bool presence = onewire->reset();
|
||||
osKernelUnlock();
|
||||
|
||||
if(presence) {
|
||||
osKernelLock();
|
||||
__disable_irq();
|
||||
onewire->write(DS1990::CMD_READ_ROM);
|
||||
onewire->read_bytes(buff, key_length);
|
||||
__enable_irq();
|
||||
osKernelUnlock();
|
||||
|
||||
result = true;
|
||||
for(uint8_t i = 0; i < 8; i++) {
|
||||
if(key[i] != buff[i]) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void BlanksWriter::start() {
|
||||
onewire->start();
|
||||
}
|
||||
|
||||
void BlanksWriter::stop() {
|
||||
onewire->stop();
|
||||
}
|
@@ -1,40 +0,0 @@
|
||||
#pragma once
|
||||
#include "one_wire_master.h"
|
||||
#include "maxim_crc.h"
|
||||
|
||||
typedef enum {
|
||||
KEY_DS1990, /**< DS1990 */
|
||||
KEY_CYFRAL, /**< CYFRAL*/
|
||||
KEY_METAKOM, /**< METAKOM */
|
||||
} KeyType;
|
||||
|
||||
typedef enum {
|
||||
WR_OK,
|
||||
WR_SAME_KEY,
|
||||
WR_ERROR,
|
||||
} WriterResult;
|
||||
|
||||
class BlanksWriter {
|
||||
private:
|
||||
const GpioPin* gpio;
|
||||
OneWireMaster* onewire;
|
||||
|
||||
void onewire_release(void);
|
||||
void onewire_write_one_bit(bool value, uint32_t delay);
|
||||
|
||||
bool write_TM2004(const uint8_t* key, uint8_t key_length);
|
||||
bool write_1990_1(const uint8_t* key, uint8_t key_length);
|
||||
bool write_1990_2(const uint8_t* key, uint8_t key_length);
|
||||
bool write_TM01(KeyType type, const uint8_t* key, uint8_t key_length);
|
||||
|
||||
void write_byte_ds1990(uint8_t data);
|
||||
bool compare_key_ds1990(const uint8_t* key, uint8_t key_length);
|
||||
|
||||
public:
|
||||
BlanksWriter(const GpioPin* one_wire_gpio);
|
||||
~BlanksWriter();
|
||||
|
||||
WriterResult write(KeyType type, const uint8_t* key, uint8_t key_length);
|
||||
void start();
|
||||
void stop();
|
||||
};
|
@@ -258,7 +258,7 @@ bool OneWireSlave::bus_start(void) {
|
||||
if(device == nullptr) {
|
||||
result = false;
|
||||
} else {
|
||||
__disable_irq();
|
||||
FURI_CRITICAL_ENTER();
|
||||
pin_init_opendrain_in_isr_ctx();
|
||||
error = OneWireSlaveError::NO_ERROR;
|
||||
|
||||
@@ -274,7 +274,7 @@ bool OneWireSlave::bus_start(void) {
|
||||
}
|
||||
|
||||
pin_init_interrupt_in_isr_ctx();
|
||||
__enable_irq();
|
||||
FURI_CRITICAL_EXIT();
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -305,4 +305,4 @@ void OneWireSlave::exti_callback(void* _ctx) {
|
||||
//FALL event
|
||||
pulse_start = DWT->CYCCNT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user