[FL-1472, FL-1596, FL-1673] IRDA: stability improvements (#655)

- Restrict with 31 bytes length for remote and signal name
- Don't stuck for 0 PWM cycle timings
- Support timings > 65535 PWM cycles
- Fix remote file open error
- Add IRDA TX debug redirect
- Add remote parse error print, improve parsing, support tabs
- Fix stucks with uncorrect RAW signal values, long strings in remote file, etc
- Fix HAL signals capturing (save previous read value)
- Fix leak in case of failed parsing
This commit is contained in:
Albert Kharisov 2021-08-19 03:18:42 +03:00 committed by GitHub
parent 9d38f28de7
commit 5f6aff2255
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 347 additions and 177 deletions

View file

@ -74,6 +74,12 @@ static void irda_cli_print_usage(void) {
printf(" %s", irda_get_protocol_name((IrdaProtocol)i));
}
printf("\r\n");
printf("\tRaw format:\r\n");
printf("\tir_tx RAW F:<frequency> DC:<duty_cycle> <sample0> <sample1>...\r\n");
printf(
"\tFrequency (%d - %d), Duty cycle (0 - 100), max 512 samples\r\n",
IRDA_MIN_FREQUENCY,
IRDA_MAX_FREQUENCY);
}
static bool parse_message(const char* str, IrdaMessage* message) {
@ -115,6 +121,14 @@ static bool parse_signal_raw(
*duty_cycle = (float)atoi(duty_cycle_str) / 100;
str += strlen(frequency_str) + strlen(duty_cycle_str) + 10;
if((*frequency > IRDA_MAX_FREQUENCY) || (*frequency < IRDA_MIN_FREQUENCY)) {
return false;
}
if((*duty_cycle <= 0) || (*duty_cycle > 1)) {
return false;
}
uint32_t timings_cnt_max = *timings_cnt;
*timings_cnt = 0;
@ -131,13 +145,15 @@ static bool parse_signal_raw(
++*timings_cnt;
}
if(*timings_cnt > 0) {
printf("\r\nTransmit:");
for(size_t i = 0; i < *timings_cnt; ++i) {
printf(" %ld", timings[i]);
}
printf("\r\n");
}
return true;
return (parsed == 2) && (*timings_cnt > 0);
}
static void irda_cli_start_ir_tx(Cli* cli, string_t args, void* context) {
@ -150,8 +166,8 @@ static void irda_cli_start_ir_tx(Cli* cli, string_t args, void* context) {
const char* str = string_get_cstr(args);
uint32_t frequency;
float duty_cycle;
uint32_t* timings = (uint32_t*)furi_alloc(sizeof(uint32_t) * 1000);
uint32_t timings_cnt = 1000;
uint32_t* timings = (uint32_t*)furi_alloc(sizeof(uint32_t) * 512);
uint32_t timings_cnt = 512;
if(parse_message(str, &message)) {
irda_send(&message, 1);

View file

@ -1,8 +1,10 @@
#include "irda-app-brute-force.h"
#include "irda/irda-app-file-parser.h"
#include "m-string.h"
#include <file-worker-cpp.h>
#include <memory>
#include <m-string.h>
#include <furi.h>
#include <file-worker-cpp.h>
void IrdaAppBruteForce::add_record(int index, const char* name) {
records[name].index = index;
@ -16,7 +18,7 @@ bool IrdaAppBruteForce::calculate_messages() {
file_parser = std::make_unique<IrdaAppFileParser>();
fs_res = file_parser->open_irda_file_read(universal_db_filename);
if(!fs_res) {
file_parser.reset(nullptr);
file_parser.reset();
return false;
}
@ -31,7 +33,7 @@ bool IrdaAppBruteForce::calculate_messages() {
}
file_parser->close();
file_parser.reset(nullptr);
file_parser.reset();
return true;
}
@ -43,7 +45,7 @@ void IrdaAppBruteForce::stop_bruteforce() {
furi_assert(file_parser);
current_record.clear();
file_parser->close();
file_parser.reset(nullptr);
file_parser.reset();
}
}
@ -81,7 +83,7 @@ bool IrdaAppBruteForce::start_bruteforce(int index, int& record_amount) {
file_parser = std::make_unique<IrdaAppFileParser>();
result = file_parser->open_irda_file_read(universal_db_filename);
if(!result) {
(void)file_parser.reset(nullptr);
file_parser.reset();
}
}

View file

@ -1,7 +1,8 @@
#pragma once
#include "furi/check.h"
#include <unordered_map>
#include "irda-app-file-parser.h"
#include <unordered_map>
#include <memory>
class IrdaAppBruteForce {

View file

@ -1,22 +1,16 @@
#include "irda-app-file-parser.h"
#include "furi/check.h"
#include "irda-app-remote-manager.h"
#include "irda-app-signal.h"
#include "m-string.h"
#include <m-string.h>
#include <cstdio>
#include <text-store.h>
#include <irda.h>
#include <cstdio>
#include <stdint.h>
#include <string>
#include <string_view>
#include <furi.h>
#include <furi-hal-irda.h>
#include <file-worker-cpp.h>
uint32_t const IrdaAppFileParser::max_line_length = ((9 + 1) * 512 + 100);
const char* IrdaAppFileParser::irda_directory = "/any/irda";
const char* IrdaAppFileParser::irda_extension = ".ir";
uint32_t const IrdaAppFileParser::max_raw_timings_in_signal = 512;
bool IrdaAppFileParser::open_irda_file_read(const char* name) {
std::string full_filename;
if(name[0] != '/')
@ -158,18 +152,41 @@ std::unique_ptr<IrdaAppFileParser::IrdaFileSignal>
IrdaProtocol protocol = irda_get_protocol_by_name(protocol_name);
if(!irda_is_protocol_valid((IrdaProtocol)protocol)) {
size_t end_of_str = MIN(str.find_last_not_of(" \t\r\n") + 1, (size_t)30);
FURI_LOG_E(
"IrdaFileParser",
"Unknown protocol(\'%.*s...\'): \'%s\'",
end_of_str,
str.c_str(),
protocol_name);
return nullptr;
}
int address_length = irda_get_protocol_address_length(protocol);
uint32_t address_mask = (1LU << (4 * address_length)) - 1;
if(address != (address & address_mask)) {
size_t end_of_str = MIN(str.find_last_not_of(" \t\r\n") + 1, (size_t)30);
FURI_LOG_E(
"IrdaFileParser",
"Signal(\'%.*s...\'): address is too long (mask for this protocol is 0x%08X): 0x%X",
end_of_str,
str.c_str(),
address_mask,
address);
return nullptr;
}
int command_length = irda_get_protocol_command_length(protocol);
uint32_t command_mask = (1LU << (4 * command_length)) - 1;
if(command != (command & command_mask)) {
size_t end_of_str = MIN(str.find_last_not_of(" \t\r\n") + 1, (size_t)30);
FURI_LOG_E(
"IrdaFileParser",
"Signal(\'%.*s...\'): command is too long (mask for this protocol is 0x%08X): 0x%X",
end_of_str,
str.c_str(),
command_mask,
command);
return nullptr;
}
@ -195,34 +212,81 @@ const char* find_first_not_of(const char* str, char symbol) {
return str;
}
static int remove_args32(std::string_view& str, size_t num) {
int removed_length = 0;
while(num--) {
char buf[32];
size_t index = str.find_first_not_of(" \t");
if(index == std::string_view::npos) break;
removed_length += index;
str.remove_prefix(index);
if(str.empty()) break;
int parsed = std::sscanf(str.data(), "%31s", buf);
if(!parsed) break;
size_t len = strlen(buf);
if(!len) break;
removed_length += len;
str.remove_prefix(len);
if(str.empty()) break;
}
return removed_length;
}
std::unique_ptr<IrdaAppFileParser::IrdaFileSignal>
IrdaAppFileParser::parse_signal_raw(const std::string& string) const {
uint32_t frequency;
uint32_t duty_cycle;
int str_len = string.size();
std::string_view str(string.c_str());
auto irda_file_signal = std::make_unique<IrdaFileSignal>();
int parsed = std::sscanf(
str.data(), "%31s RAW F:%ld DC:%ld", irda_file_signal->name, &frequency, &duty_cycle);
if((parsed != 3) || (frequency > 42000) || (frequency < 32000) || (duty_cycle == 0) ||
(duty_cycle >= 100)) {
if(parsed != 3) {
return nullptr;
}
char dummy[100] = {0};
int header_len = 0;
header_len = sniprintf(
dummy,
sizeof(dummy),
"%.31s RAW F:%ld DC:%ld",
irda_file_signal->name,
frequency,
duty_cycle);
if((frequency < IRDA_MIN_FREQUENCY) || (frequency > IRDA_MAX_FREQUENCY)) {
size_t end_of_str = MIN(string.find_last_not_of(" \t\r\n") + 1, (size_t)30);
FURI_LOG_E(
"IrdaFileParser",
"RAW signal(\'%.*s...\'): frequency is out of bounds (%ld-%ld): %ld",
end_of_str,
string.c_str(),
IRDA_MIN_FREQUENCY,
IRDA_MAX_FREQUENCY,
frequency);
return nullptr;
}
furi_assert(header_len < str_len);
str.remove_prefix(header_len);
if((duty_cycle == 0) || (duty_cycle > 100)) {
size_t end_of_str = MIN(string.find_last_not_of(" \t\r\n") + 1, (size_t)30);
FURI_LOG_E(
"IrdaFileParser",
"RAW signal(\'%.*s...\'): duty cycle is out of bounds (0-100): %ld",
end_of_str,
string.c_str(),
duty_cycle);
return nullptr;
}
int header_len = remove_args32(str, 4);
size_t last_valid_ch = str.find_last_not_of(" \t\r\n");
if(last_valid_ch != std::string_view::npos) {
str.remove_suffix(str.size() - last_valid_ch - 1);
} else {
FURI_LOG_E(
"IrdaFileParser", "RAW signal(\'%.*s\'): no timings", header_len, string.c_str());
return nullptr;
}
/* move allocated timings into raw signal object */
IrdaAppSignal::RawSignal raw_signal = {
@ -231,40 +295,59 @@ std::unique_ptr<IrdaAppFileParser::IrdaFileSignal>
while(!str.empty()) {
char buf[10];
size_t index = str.find_first_not_of(' ', 1);
size_t index = str.find_first_not_of(" \t", 1);
if(index == std::string_view::npos) {
break;
}
str.remove_prefix(index);
parsed = std::sscanf(str.data(), "%9s", buf);
if(parsed != 1) {
FURI_LOG_E(
"IrdaFileParser",
"RAW signal(\'%.*s...\'): failed on timing[%ld] \'%*s\'",
header_len,
string.c_str(),
raw_signal.timings_cnt,
str.size(),
str.data());
result = false;
furi_assert(0);
break;
}
str.remove_prefix(strlen(buf));
int value = atoi(buf);
if(value <= 0) {
FURI_LOG_E(
"IrdaFileParser",
"RAW signal(\'%.*s...\'): failed on timing[%ld] \'%s\'",
header_len,
string.c_str(),
raw_signal.timings_cnt,
buf);
result = false;
break;
}
if(raw_signal.timings_cnt >= max_raw_timings_in_signal) {
FURI_LOG_E(
"IrdaFileParser",
"RAW signal(\'%.*s...\'): too much timings (max %ld)",
header_len,
string.c_str(),
max_raw_timings_in_signal);
result = false;
furi_assert(0);
break;
}
raw_signal.timings[raw_signal.timings_cnt] = value;
++raw_signal.timings_cnt;
result = true;
if(raw_signal.timings_cnt >= max_raw_timings_in_signal) {
result = false;
furi_assert(0);
break;
}
}
if(result) {
/* copy timings instead of moving them to occupy less than max_raw_timings_in_signal */
irda_file_signal->signal.copy_raw_signal(raw_signal.timings, raw_signal.timings_cnt);
} else {
(void)irda_file_signal.release();
irda_file_signal.reset();
}
delete[] raw_signal.timings;
return irda_file_signal;
@ -306,13 +389,11 @@ bool IrdaAppFileParser::check_errors() {
}
std::string IrdaAppFileParser::file_select(const char* selected) {
TextStore* filename_ts = new TextStore(128);
auto filename_ts = std::make_unique<TextStore>(IrdaAppRemoteManager::max_remote_name_length);
bool result;
result = file_worker.file_select(
irda_directory, irda_extension, filename_ts->text, filename_ts->text_size, selected);
delete filename_ts;
return result ? std::string(filename_ts->text) : std::string();
}

View file

@ -1,8 +1,12 @@
#pragma once
#include "irda-app-signal.h"
#include <irda.h>
#include <file-worker-cpp.h>
#include "irda-app-signal.h"
#include <memory>
#include <string>
#include <cstdint>
class IrdaAppFileParser {
public:
@ -40,10 +44,11 @@ private:
std::unique_ptr<IrdaFileSignal> parse_signal_raw(const std::string& str) const;
std::string make_full_name(const std::string& name) const;
static const char* irda_directory;
static const char* irda_extension;
static const uint32_t max_line_length;
static uint32_t const max_raw_timings_in_signal;
static inline const char* const irda_directory = "/any/irda";
static inline const char* const irda_extension = ".ir";
static inline const uint32_t max_raw_timings_in_signal = 512;
static inline const uint32_t max_line_length =
(9 + 1) * IrdaAppFileParser::max_raw_timings_in_signal + 100;
FileWorkerCpp file_worker;
char file_buf[128];

View file

@ -1,15 +1,14 @@
#include "irda-app-remote-manager.h"
#include <storage/storage.h>
#include "furi.h"
#include "furi/check.h"
#include "gui/modules/button_menu.h"
#include "irda.h"
#include <cstdio>
#include <stdint.h>
#include <string>
#include <utility>
#include "irda-app-file-parser.h"
#include <utility>
#include <irda.h>
#include <cstdio>
#include <furi.h>
#include <gui/modules/button_menu.h>
#include <storage/storage.h>
static const std::string default_remote_name = "remote";
std::string IrdaAppRemoteManager::find_vacant_remote_name(const std::string& name) {

View file

@ -1,13 +1,15 @@
#pragma once
#include <irda_worker.h>
#include <stdint.h>
#include <string>
#include <vector>
#include <memory>
#include <irda.h>
#include <storage/storage.h>
#include "irda-app-signal.h"
#include <irda_worker.h>
#include <irda.h>
#include <cstdint>
#include <string>
#include <memory>
#include <vector>
class IrdaAppRemoteButton {
friend class IrdaAppRemoteManager;
std::string name;
@ -40,13 +42,13 @@ public:
};
class IrdaAppRemoteManager {
static const char* irda_directory;
static const char* irda_extension;
std::unique_ptr<IrdaAppRemote> remote;
std::string make_full_name(const std::string& remote_name) const;
std::string make_remote_name(const std::string& full_name) const;
public:
static inline const uint32_t max_button_name_length = 31;
static inline const uint32_t max_remote_name_length = 31;
bool add_remote_with_button(const char* button_name, const IrdaAppSignal& signal);
bool add_button(const char* button_name, const IrdaAppSignal& signal);

View file

@ -5,9 +5,9 @@ void IrdaAppSignal::copy_timings(const uint32_t* timings, size_t size) {
furi_assert(size);
furi_assert(timings);
payload.raw.timings_cnt = size;
if(size) {
payload.raw.timings = new uint32_t[size];
payload.raw.timings_cnt = size;
memcpy(payload.raw.timings, timings, size * sizeof(uint32_t));
}
}

View file

@ -48,6 +48,18 @@ int32_t IrdaApp::run(void* args) {
return 0;
};
IrdaApp::IrdaApp() {
furi_check(IrdaAppRemoteManager::max_button_name_length < get_text_store_size());
notification = static_cast<NotificationApp*>(furi_record_open("notification"));
irda_worker = irda_worker_alloc();
}
IrdaApp::~IrdaApp() {
irda_worker_free(irda_worker);
furi_record_close("notification");
for(auto& [key, scene] : scenes) delete scene;
}
IrdaAppViewManager* IrdaApp::get_view_manager() {
return &view_manager;
}

View file

@ -88,19 +88,12 @@ public:
static void text_input_callback(void* context);
static void popup_callback(void* context);
IrdaApp() {
notification = static_cast<NotificationApp*>(furi_record_open("notification"));
irda_worker = irda_worker_alloc();
}
~IrdaApp() {
irda_worker_free(irda_worker);
furi_record_close("notification");
for(auto& it : scenes) delete it.second;
}
IrdaApp();
~IrdaApp();
private:
static const uint8_t text_store_size = 128;
static const uint8_t text_store_max = 2;
static inline const uint8_t text_store_size = 128;
static inline const uint8_t text_store_max = 2;
char text_store[text_store_max][text_store_size + 1];
bool learn_new_remote;
EditElement element;

View file

@ -3,24 +3,31 @@
void IrdaAppSceneEditRename::on_enter(IrdaApp* app) {
IrdaAppViewManager* view_manager = app->get_view_manager();
TextInput* text_input = view_manager->get_text_input();
size_t enter_name_length = 0;
auto remote_manager = app->get_remote_manager();
if(app->get_edit_element() == IrdaApp::EditElement::Button) {
furi_assert(app->get_current_button() != IrdaApp::ButtonNA);
auto button_name = remote_manager->get_button_name(app->get_current_button());
strncpy(app->get_text_store(0), button_name.c_str(), app->get_text_store_size());
char* buffer_str = app->get_text_store(0);
size_t max_len = IrdaAppRemoteManager::max_button_name_length;
strncpy(buffer_str, button_name.c_str(), max_len);
buffer_str[max_len + 1] = 0;
enter_name_length = max_len;
text_input_set_header_text(text_input, "Name the key");
} else {
auto remote_name = remote_manager->get_remote_name();
strncpy(app->get_text_store(0), remote_name.c_str(), app->get_text_store_size());
enter_name_length = IrdaAppRemoteManager::max_remote_name_length;
text_input_set_header_text(text_input, "Name the remote");
}
text_input_set_header_text(text_input, "Name the key");
text_input_set_result_callback(
text_input,
IrdaApp::text_input_callback,
app,
app->get_text_store(0),
app->get_text_store_size(),
enter_name_length,
false);
view_manager->switch_to(IrdaAppViewManager::ViewType::TextInput);

View file

@ -26,7 +26,7 @@ void IrdaAppSceneLearnEnterName::on_enter(IrdaApp* app) {
IrdaApp::text_input_callback,
app,
app->get_text_store(0),
app->get_text_store_size(),
IrdaAppRemoteManager::max_button_name_length,
false);
view_manager->switch_to(IrdaAppViewManager::ViewType::TextInput);

View file

@ -17,6 +17,13 @@
#include <main.h>
#include <furi-hal-pwm.h>
#define IRDA_TX_DEBUG 0
#if IRDA_TX_DEBUG == 1
#define gpio_irda_tx gpio_irda_tx_debug
const GpioPin gpio_irda_tx_debug = {.port = GPIOA, .pin = GPIO_PIN_7};
#endif
#define IRDA_TIM_TX_DMA_BUFFER_SIZE 200
#define IRDA_POLARITY_SHIFT 1
@ -46,6 +53,9 @@ typedef struct {
void* signal_sent_context;
IrdaTxBuf buffer[2];
osSemaphoreId_t stop_semaphore;
uint32_t tx_timing_rest_duration; /** if timing is too long (> 0xFFFF), send it in few iterations */
bool tx_timing_rest_level;
FuriHalIrdaTxGetDataState tx_timing_rest_status;
} IrdaTimTx;
typedef enum {
@ -62,7 +72,7 @@ static volatile IrdaState furi_hal_irda_state = IrdaStateIdle;
static IrdaTimTx irda_tim_tx;
static IrdaTimRx irda_tim_rx;
static bool furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift);
static void furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift);
static void furi_hal_irda_async_tx_free_resources(void);
static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift);
static void furi_hal_irda_tx_dma_set_buffer(uint8_t buf_num);
@ -72,6 +82,7 @@ static void furi_hal_irda_tx_dma_polarity_isr();
static void furi_hal_irda_tx_dma_isr();
static void furi_hal_irda_tim_rx_isr() {
static uint32_t previous_captured_ch2 = 0;
/* Timeout */
if(LL_TIM_IsActiveFlag_CC3(TIM2)) {
@ -97,7 +108,7 @@ static void furi_hal_irda_tim_rx_isr() {
if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC1S)) {
/* Low pin level is a Mark state of IRDA signal. Invert level for further processing. */
uint32_t duration = LL_TIM_IC_GetCaptureCH1(TIM2) - LL_TIM_IC_GetCaptureCH2(TIM2);
uint32_t duration = LL_TIM_IC_GetCaptureCH1(TIM2) - previous_captured_ch2;
if (irda_tim_rx.capture_callback)
irda_tim_rx.capture_callback(irda_tim_rx.capture_context, 1, duration);
} else {
@ -113,6 +124,7 @@ static void furi_hal_irda_tim_rx_isr() {
if(READ_BIT(TIM2->CCMR1, TIM_CCMR1_CC2S)) {
/* High pin level is a Space state of IRDA signal. Invert level for further processing. */
uint32_t duration = LL_TIM_IC_GetCaptureCH2(TIM2);
previous_captured_ch2 = duration;
if (irda_tim_rx.capture_callback)
irda_tim_rx.capture_callback(irda_tim_rx.capture_context, 0, duration);
} else {
@ -258,14 +270,10 @@ static void furi_hal_irda_tx_dma_isr() {
if (irda_tim_tx.buffer[buf_num].last_packet_end) {
LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
} else if (!irda_tim_tx.buffer[buf_num].packet_end || (furi_hal_irda_state == IrdaStateAsyncTx)) {
bool result = furi_hal_irda_tx_fill_buffer(next_buf_num, 0);
furi_hal_irda_tx_fill_buffer(next_buf_num, 0);
if (irda_tim_tx.buffer[next_buf_num].last_packet_end) {
LL_DMA_DisableIT_HT(DMA1, LL_DMA_CHANNEL_2);
}
if (!result) {
furi_assert(0);
furi_hal_irda_state = IrdaStateAsyncTxStopReq;
}
} else if (furi_hal_irda_state == IrdaStateAsyncTxStopReq) {
/* fallthrough */
} else {
@ -291,7 +299,7 @@ static void furi_hal_irda_tx_dma_isr() {
/* if it's not end of the packet - continue receiving */
furi_hal_irda_tx_dma_set_buffer(next_buf_num);
}
if (irda_tim_tx.signal_sent_callback) {
if (irda_tim_tx.signal_sent_callback && irda_tim_tx.buffer[buf_num].packet_end && (furi_hal_irda_state != IrdaStateAsyncTxStopped)) {
irda_tim_tx.signal_sent_callback(irda_tim_tx.signal_sent_context);
}
}
@ -309,6 +317,16 @@ static void furi_hal_irda_configure_tim_pwm_tx(uint32_t freq, float duty_cycle)
LL_TIM_SetCounterMode(TIM1, LL_TIM_COUNTERMODE_UP);
LL_TIM_EnableARRPreload(TIM1);
LL_TIM_SetAutoReload(TIM1, __LL_TIM_CALC_ARR(SystemCoreClock, LL_TIM_GetPrescaler(TIM1), freq));
#if IRDA_TX_DEBUG == 1
LL_TIM_OC_SetCompareCH1(TIM1, ( (LL_TIM_GetAutoReload(TIM1) + 1 ) * (1 - duty_cycle)));
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
/* LL_TIM_OCMODE_PWM2 set by DMA */
LL_TIM_OC_SetMode(TIM1, LL_TIM_CHANNEL_CH1, LL_TIM_OCMODE_FORCED_INACTIVE);
LL_TIM_OC_SetPolarity(TIM1, LL_TIM_CHANNEL_CH1N, LL_TIM_OCPOLARITY_HIGH);
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH1N);
LL_TIM_DisableIT_CC1(TIM1);
#else
LL_TIM_OC_SetCompareCH3(TIM1, ( (LL_TIM_GetAutoReload(TIM1) + 1 ) * (1 - duty_cycle)));
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH3);
/* LL_TIM_OCMODE_PWM2 set by DMA */
@ -317,6 +335,7 @@ static void furi_hal_irda_configure_tim_pwm_tx(uint32_t freq, float duty_cycle)
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH3);
LL_TIM_CC_EnableChannel(TIM1, LL_TIM_CHANNEL_CH3N);
LL_TIM_DisableIT_CC3(TIM1);
#endif
LL_TIM_DisableMasterSlaveMode(TIM1);
LL_TIM_EnableAllOutputs(TIM1);
LL_TIM_DisableIT_UPDATE(TIM1);
@ -330,7 +349,11 @@ static void furi_hal_irda_configure_tim_cmgr2_dma_tx(void) {
LL_C2_AHB1_GRP1_EnableClock(LL_C2_AHB1_GRP1_PERIPH_DMA1);
LL_DMA_InitTypeDef dma_config = {0};
#if IRDA_TX_DEBUG == 1
dma_config.PeriphOrM2MSrcAddress = (uint32_t)&(TIM1->CCMR1);
#else
dma_config.PeriphOrM2MSrcAddress = (uint32_t)&(TIM1->CCMR2);
#endif
dma_config.MemoryOrM2MDstAddress = (uint32_t) NULL;
dma_config.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH;
dma_config.Mode = LL_DMA_MODE_NORMAL;
@ -399,7 +422,7 @@ static void furi_hal_irda_tx_fill_buffer_last(uint8_t buf_num) {
irda_tim_tx.buffer[buf_num].packet_end = true;
}
static bool furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
static void furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift) {
furi_assert(buf_num < 2);
furi_assert(furi_hal_irda_state != IrdaStateAsyncRx);
furi_assert(furi_hal_irda_state < IrdaStateMAX);
@ -418,28 +441,53 @@ static bool furi_hal_irda_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift
}
for (*size = 0; (*size < IRDA_TIM_TX_DMA_BUFFER_SIZE) && (status == FuriHalIrdaTxGetDataStateOk); ++(*size), ++polarity_counter) {
status = irda_tim_tx.data_callback(irda_tim_tx.data_context, &duration, &level);
if (status == FuriHalIrdaTxGetDataStateError) {
furi_assert(0);
break;
if (irda_tim_tx.tx_timing_rest_duration > 0) {
if (irda_tim_tx.tx_timing_rest_duration > 0xFFFF) {
buffer->data[*size] = 0xFFFF;
status = FuriHalIrdaTxGetDataStateOk;
} else {
buffer->data[*size] = irda_tim_tx.tx_timing_rest_duration;
status = irda_tim_tx.tx_timing_rest_status;
}
irda_tim_tx.tx_timing_rest_duration -= buffer->data[*size];
buffer->polarity[polarity_counter] = irda_tim_tx.tx_timing_rest_level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
continue;
}
status = irda_tim_tx.data_callback(irda_tim_tx.data_context, &duration, &level);
uint32_t num_of_impulses = roundf(duration / irda_tim_tx.cycle_duration);
if ((buffer->data[*size] + num_of_impulses - 1) > 0xFFFF) {
furi_assert(0);
status = FuriHalIrdaTxGetDataStateError;
break;
if (num_of_impulses == 0) {
if ((*size == 0) && (status == FuriHalIrdaTxGetDataStateDone)) {
/* if this is one sample in current buffer, but we
* have more to send - continue
*/
status = FuriHalIrdaTxGetDataStateOk;
}
--(*size);
--polarity_counter;
} else if ((num_of_impulses - 1) > 0xFFFF) {
irda_tim_tx.tx_timing_rest_duration = num_of_impulses - 1;
irda_tim_tx.tx_timing_rest_status = status;
irda_tim_tx.tx_timing_rest_level = level;
buffer->polarity[polarity_counter] = level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
buffer->data[*size] = 0xFFFF;
status = FuriHalIrdaTxGetDataStateOk;
} else {
buffer->polarity[polarity_counter] = level ? IRDA_TX_CCMR_HIGH : IRDA_TX_CCMR_LOW;
buffer->data[*size] = num_of_impulses - 1;
}
}
buffer->last_packet_end = (status == FuriHalIrdaTxGetDataStateLastDone);
buffer->packet_end = buffer->last_packet_end || (status == FuriHalIrdaTxGetDataStateDone);
return status != FuriHalIrdaTxGetDataStateError;
if (*size == 0) {
buffer->data[0] = 0; // 1 pulse
buffer->polarity[0] = IRDA_TX_CCMR_LOW;
buffer->size = 1;
}
}
static void furi_hal_irda_tx_dma_set_polarity(uint8_t buf_num, uint8_t polarity_shift) {
@ -505,10 +553,9 @@ static void furi_hal_irda_async_tx_free_resources(void) {
irda_tim_tx.buffer[1].polarity = NULL;
}
bool furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
if ((duty_cycle > 1) || (duty_cycle < 0) || (freq > 40000) || (freq < 10000) || (irda_tim_tx.data_callback == NULL)) {
furi_assert(0);
return false;
void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
if ((duty_cycle > 1) || (duty_cycle <= 0) || (freq > IRDA_MAX_FREQUENCY) || (freq < IRDA_MIN_FREQUENCY) || (irda_tim_tx.data_callback == NULL)) {
furi_check(0);
}
furi_assert(furi_hal_irda_state == IrdaStateIdle);
@ -527,10 +574,10 @@ bool furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
irda_tim_tx.stop_semaphore = osSemaphoreNew(1, 0, NULL);
irda_tim_tx.cycle_duration = 1000000.0 / freq;
irda_tim_tx.tx_timing_rest_duration = 0;
bool result = furi_hal_irda_tx_fill_buffer(0, IRDA_POLARITY_SHIFT);
furi_hal_irda_tx_fill_buffer(0, IRDA_POLARITY_SHIFT);
if (result) {
furi_hal_irda_configure_tim_pwm_tx(freq, duty_cycle);
furi_hal_irda_configure_tim_cmgr2_dma_tx();
furi_hal_irda_configure_tim_rcr_dma_tx();
@ -552,12 +599,6 @@ bool furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle) {
LL_TIM_GenerateEvent_UPDATE(TIM1); /* TIMx_RCR -> Repetition counter */
LL_TIM_EnableCounter(TIM1);
__enable_irq();
} else {
furi_hal_irda_async_tx_free_resources();
}
return result;
}
void furi_hal_irda_async_tx_wait_termination(void) {

View file

@ -7,8 +7,10 @@
extern "C" {
#endif
#define IRDA_MAX_FREQUENCY 56000
#define IRDA_MIN_FREQUENCY 10000
typedef enum {
FuriHalIrdaTxGetDataStateError, /* An error occured during transmission */
FuriHalIrdaTxGetDataStateOk, /* New data obtained */
FuriHalIrdaTxGetDataStateDone, /* New data obtained, and this is end of package */
FuriHalIrdaTxGetDataStateLastDone, /* New data obtained, and this is end of package and no more data available */
@ -103,10 +105,8 @@ void furi_hal_irda_async_tx_set_data_isr_callback(FuriHalIrdaTxGetDataISRCallbac
*
* @param[in] freq - frequency for PWM
* @param[in] duty_cycle - duty cycle for PWM
* @return true if transmission successfully started, false otherwise.
* If start failed no need to free resources.
*/
bool furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle);
void furi_hal_irda_async_tx_start(uint32_t freq, float duty_cycle);
/**
* Stop IR asynchronous transmission and free resources.

View file

@ -1,5 +1,5 @@
#include "file-worker.h"
#include "m-string.h"
#include <m-string.h>
#include <lib/toolbox/hex.h>
#include <dialogs/dialogs.h>
#include <furi.h>
@ -350,7 +350,7 @@ bool file_worker_read_until_buffered(
}
}
if(max_length_exceeded) string_clear(str_result);
if(max_length_exceeded) string_clean(str_result);
return string_size(str_result) || *file_buf_cnt;
}

View file

@ -38,7 +38,6 @@ FuriHalIrdaTxGetDataState irda_get_raw_data_callback (void* context, uint32_t* d
void irda_send_raw_ext(const uint32_t timings[], uint32_t timings_cnt, bool start_from_mark, uint32_t frequency, float duty_cycle) {
furi_assert(timings);
furi_assert(timings_cnt > 1);
irda_tx_raw_start_from_mark = start_from_mark;
irda_tx_raw_timings_index = 0;
@ -56,7 +55,7 @@ void irda_send_raw(const uint32_t timings[], uint32_t timings_cnt, bool start_fr
}
FuriHalIrdaTxGetDataState irda_get_data_callback (void* context, uint32_t* duration, bool* level) {
FuriHalIrdaTxGetDataState state = FuriHalIrdaTxGetDataStateError;
FuriHalIrdaTxGetDataState state = FuriHalIrdaTxGetDataStateLastDone;
IrdaEncoderHandler* handler = context;
IrdaStatus status = IrdaStatusError;
@ -65,7 +64,9 @@ FuriHalIrdaTxGetDataState irda_get_data_callback (void* context, uint32_t* durat
}
if (status == IrdaStatusError) {
state = FuriHalIrdaTxGetDataStateError;
state = FuriHalIrdaTxGetDataStateLastDone;
*duration = 0;
*level = 0;
} else if (status == IrdaStatusOk) {
state = FuriHalIrdaTxGetDataStateOk;
} else if (status == IrdaStatusDone) {
@ -74,8 +75,7 @@ FuriHalIrdaTxGetDataState irda_get_data_callback (void* context, uint32_t* durat
state = FuriHalIrdaTxGetDataStateLastDone;
}
} else {
furi_assert(0);
state = FuriHalIrdaTxGetDataStateError;
furi_check(0);
}
return state;
@ -90,8 +90,11 @@ void irda_send(const IrdaMessage* message, int times) {
irda_reset_encoder(handler, message);
irda_tx_number_of_transmissions = times;
uint32_t frequency = irda_get_protocol_frequency(message->protocol);
float duty_cycle = irda_get_protocol_duty_cycle(message->protocol);
furi_hal_irda_async_tx_set_data_isr_callback(irda_get_data_callback, handler);
furi_hal_irda_async_tx_start(IRDA_COMMON_CARRIER_FREQUENCY, IRDA_COMMON_DUTY_CYCLE);
furi_hal_irda_async_tx_start(frequency, duty_cycle);
furi_hal_irda_async_tx_wait_termination();
irda_free_encoder(handler);

View file

@ -44,8 +44,9 @@ struct IrdaWorkerSignal{
size_t timings_cnt;
union {
IrdaMessage message;
uint32_t timings[MAX_TIMINGS_AMOUNT];
} data;
/* +1 is for pause we add at the beginning */
uint32_t timings[MAX_TIMINGS_AMOUNT + 1];
};
};
struct IrdaWorker {
@ -125,7 +126,7 @@ static void irda_worker_process_timeout(IrdaWorker* instance) {
static void irda_worker_process_timings(IrdaWorker* instance, uint32_t duration, bool level) {
const IrdaMessage* message_decoded = irda_decode(instance->irda_decoder, level, duration);
if (message_decoded) {
instance->signal.data.message = *message_decoded;
instance->signal.message = *message_decoded;
instance->signal.timings_cnt = 0;
instance->signal.decoded = true;
if (instance->rx.received_signal_callback)
@ -137,7 +138,7 @@ static void irda_worker_process_timings(IrdaWorker* instance, uint32_t duration,
}
if (instance->signal.timings_cnt < MAX_TIMINGS_AMOUNT) {
instance->signal.data.timings[instance->signal.timings_cnt] = duration;
instance->signal.timings[instance->signal.timings_cnt] = duration;
++instance->signal.timings_cnt;
} else {
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_OVERRUN);
@ -211,7 +212,7 @@ IrdaWorker* irda_worker_alloc() {
furi_thread_set_stack_size(instance->thread, 2048);
furi_thread_set_context(instance->thread, instance);
size_t buffer_size = MAX(sizeof(IrdaWorkerTiming) * MAX_TIMINGS_AMOUNT, sizeof(LevelDuration) * MAX_TIMINGS_AMOUNT);
size_t buffer_size = MAX(sizeof(IrdaWorkerTiming) * (MAX_TIMINGS_AMOUNT + 1), sizeof(LevelDuration) * MAX_TIMINGS_AMOUNT);
instance->stream = xStreamBufferCreate(buffer_size, sizeof(IrdaWorkerTiming));
instance->irda_decoder = irda_alloc_decoder();
instance->irda_encoder = irda_alloc_encoder();
@ -283,13 +284,13 @@ void irda_worker_get_raw_signal(const IrdaWorkerSignal* signal, const uint32_t**
furi_assert(timings);
furi_assert(timings_cnt);
*timings = signal->data.timings;
*timings = signal->timings;
*timings_cnt = signal->timings_cnt;
}
const IrdaMessage* irda_worker_get_decoded_signal(const IrdaWorkerSignal* signal) {
furi_assert(signal);
return &signal->data.message;
return &signal->message;
}
void irda_worker_rx_enable_blink_on_receiving(IrdaWorker* instance, bool enable) {
@ -328,21 +329,24 @@ static FuriHalIrdaTxGetDataState irda_worker_furi_hal_data_isr_callback(void* co
furi_assert(level);
IrdaWorker* instance = context;
IrdaWorkerTiming timing = {.state = FuriHalIrdaTxGetDataStateError} ;
IrdaWorkerTiming timing;
FuriHalIrdaTxGetDataState state;
if (sizeof(IrdaWorkerTiming) == xStreamBufferReceiveFromISR(instance->stream, &timing, sizeof(IrdaWorkerTiming), 0)) {
*level = timing.level;
*duration = timing.duration;
furi_assert(timing.state != FuriHalIrdaTxGetDataStateError);
state = timing.state;
} else {
furi_assert(0);
timing.state = FuriHalIrdaTxGetDataStateError;
*level = 0;
*duration = 100;
state = FuriHalIrdaTxGetDataStateDone;
}
uint32_t flags_set = osEventFlagsSet(instance->events, IRDA_WORKER_TX_FILL_BUFFER);
furi_check(flags_set & IRDA_WORKER_TX_FILL_BUFFER);
return timing.state;
return state;
}
static bool irda_get_new_signal(IrdaWorker* instance) {
@ -353,8 +357,8 @@ static bool irda_get_new_signal(IrdaWorker* instance) {
uint32_t new_tx_frequency = 0;
float new_tx_duty_cycle = 0;
if (instance->signal.decoded) {
new_tx_frequency = irda_get_protocol_frequency(instance->signal.data.message.protocol);
new_tx_duty_cycle = irda_get_protocol_duty_cycle(instance->signal.data.message.protocol);
new_tx_frequency = irda_get_protocol_frequency(instance->signal.message.protocol);
new_tx_duty_cycle = irda_get_protocol_duty_cycle(instance->signal.message.protocol);
} else {
furi_assert(instance->signal.timings_cnt > 1);
new_tx_frequency = IRDA_COMMON_CARRIER_FREQUENCY;
@ -366,7 +370,7 @@ static bool irda_get_new_signal(IrdaWorker* instance) {
instance->tx.frequency = new_tx_frequency;
instance->tx.duty_cycle = new_tx_duty_cycle;
if (instance->signal.decoded) {
irda_reset_encoder(instance->irda_encoder, &instance->signal.data.message);
irda_reset_encoder(instance->irda_encoder, &instance->signal.message);
}
new_signal_obtained = true;
} else if (response == IrdaWorkerGetSignalResponseSame) {
@ -390,8 +394,8 @@ static bool irda_worker_tx_fill_buffer(IrdaWorker* instance) {
if (instance->signal.decoded) {
status = irda_encode(instance->irda_encoder, &timing.duration, &timing.level);
} else {
timing.duration = instance->signal.data.timings[instance->tx.tx_raw_cnt];
/* raw always starts from Mark, but we fulfill it with space delay at start */
timing.duration = instance->signal.timings[instance->tx.tx_raw_cnt];
/* raw always starts from Mark, but we fill it with space delay at start */
timing.level = (instance->tx.tx_raw_cnt % 2);
++instance->tx.tx_raw_cnt;
if (instance->tx.tx_raw_cnt >= instance->signal.timings_cnt) {
@ -532,16 +536,18 @@ void irda_worker_set_decoded_signal(IrdaWorker* instance, const IrdaMessage* mes
furi_assert(message);
instance->signal.decoded = true;
instance->signal.data.message = *message;
instance->signal.message = *message;
}
void irda_worker_set_raw_signal(IrdaWorker* instance, const uint32_t* timings, size_t timings_cnt) {
furi_assert(instance);
furi_assert(timings);
furi_assert(timings_cnt > 2);
furi_assert(timings_cnt > 0);
size_t max_copy_num = COUNT_OF(instance->signal.timings) - 1;
furi_check(timings_cnt <= max_copy_num);
instance->signal.data.timings[0] = IRDA_RAW_TX_TIMING_DELAY_US;
memcpy(&instance->signal.data.timings[1], timings, timings_cnt * sizeof(uint32_t));
instance->signal.timings[0] = IRDA_RAW_TX_TIMING_DELAY_US;
memcpy(&instance->signal.timings[1], timings, timings_cnt * sizeof(uint32_t));
instance->signal.decoded = false;
instance->signal.timings_cnt = timings_cnt + 1;
}

View file

@ -1,7 +1,8 @@
#pragma once
#include "m-string.h"
#include "stdint.h"
#include "stdbool.h"
#include <stdint.h>
#include <stdbool.h>
#include <m-string.h>
#ifdef __cplusplus
extern "C" {

View file

@ -1,5 +1,6 @@
#pragma once
#include "m-string.h"
#include <m-string.h>
#ifdef __cplusplus
extern "C" {