Various improvements (#461)
* **New Features** * Time and delay utilities added (ticks, ms, µs); SD card now uses an expansion-header CS pin; HTTP downloads warn when run on the GUI task and yield to avoid blocking. * **Bug Fixes / Reliability** * Many hard-crash paths converted to guarded checks to reduce abrupt termination and improve stability. * **Tests** * Unit tests added to validate time and delay accuracy. * **Chores** * License header and build/macro updates.
This commit is contained in:
committed by
GitHub
parent
619b5aa53b
commit
e6abd496f9
@@ -6,7 +6,7 @@ if (DEFINED ENV{ESP_IDF_VERSION})
|
||||
idf_component_register(
|
||||
SRCS ${SOURCE_FILES}
|
||||
INCLUDE_DIRS "Include/"
|
||||
REQUIRES TactilityFreeRtos mbedtls nvs_flash esp_rom
|
||||
REQUIRES TactilityFreeRtos TactilityKernel mbedtls nvs_flash esp_rom
|
||||
)
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
|
||||
@@ -26,8 +26,9 @@ else()
|
||||
add_definitions(-D_Nullable=)
|
||||
add_definitions(-D_Nonnull=)
|
||||
|
||||
target_link_libraries(TactilityCore
|
||||
PUBLIC TactilityFreeRtos
|
||||
PUBLIC mbedtls
|
||||
target_link_libraries(TactilityCore PUBLIC
|
||||
TactilityFreeRtos
|
||||
TactilityKernel
|
||||
mbedtls
|
||||
)
|
||||
endif()
|
||||
@@ -1,69 +0,0 @@
|
||||
/**
|
||||
* @file check.h
|
||||
*
|
||||
* Tactility crash and check functions.
|
||||
*
|
||||
* The main problem with crashing is that you can't do anything without disturbing registers,
|
||||
* and if you disturb registers, you won't be able to see the correct register values in the debugger.
|
||||
*
|
||||
* Current solution works around it by passing the message through r12 and doing some magic with registers in crash function.
|
||||
* r0-r10 are stored in the ram2 on crash routine start and restored at the end.
|
||||
* The only register that is going to be lost is r11.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "CoreDefines.h"
|
||||
#include "Logger.h"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#define TT_NORETURN [[noreturn]]
|
||||
|
||||
/** Crash system */
|
||||
namespace tt {
|
||||
/**
|
||||
* Don't call this directly. Use tt_crash() as it will trace info.
|
||||
*/
|
||||
TT_NORETURN void _crash();
|
||||
}
|
||||
|
||||
// TODO: Move crash logic to kernel namespace and consider refactoring to C++
|
||||
/** Crash system with message. */
|
||||
#define tt_crash(...) TT_ARG_CAT(_tt_crash,TT_ARGCOUNT(__VA_ARGS__))(__VA_ARGS__)
|
||||
|
||||
#define _tt_crash0() do { \
|
||||
tt::Logger("Kernel").error("Crash at {}:{}", __FILE__, __LINE__); \
|
||||
tt::_crash(); \
|
||||
} while (0)
|
||||
|
||||
#define _tt_crash1(message) do { \
|
||||
tt::Logger("Kernel").error("Crash: {}\n\tat {}:{}", message, __FILE__, __LINE__); \
|
||||
tt::_crash(); \
|
||||
} while (0)
|
||||
|
||||
/** Halt the system
|
||||
* @param[in] optional message (const char*)
|
||||
*/
|
||||
#define tt_halt(...) M_APPLY(__tt_halt, M_IF_EMPTY(__VA_ARGS__)((NULL), (__VA_ARGS__)))
|
||||
|
||||
/** Check condition and crash if check failed */
|
||||
#define tt_check_internal(__e, __m) \
|
||||
do { \
|
||||
if (!(__e)) { \
|
||||
tt::Logger("Kernel").error("Check failed: {}", #__e); \
|
||||
if (__m) { \
|
||||
tt_crash(__m); \
|
||||
} else { \
|
||||
tt_crash(""); \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/** Check condition and crash if failed
|
||||
*
|
||||
* @param[in] condition to check
|
||||
* @param[in] optional message (const char*)
|
||||
*/
|
||||
|
||||
#define tt_check(x, ...) if (!(x)) { tt::Logger("Kernel").error("Check failed: {}", #x); tt::_crash(); }
|
||||
@@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#define TT_UNUSED __attribute__((unused))
|
||||
|
||||
#define TT_STRINGIFY(x) #x
|
||||
|
||||
// region Variable arguments support
|
||||
|
||||
@@ -2,6 +2,5 @@
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include "Check.h"
|
||||
#include "CoreDefines.h"
|
||||
#include "Logger.h"
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
#include <Tactility/Check.h>
|
||||
|
||||
#include <Tactility/Logger.h>
|
||||
#include <Tactility/freertoscompat/Task.h>
|
||||
|
||||
static const auto LOGGER = tt::Logger("kernel");
|
||||
|
||||
static void logMemoryInfo() {
|
||||
#ifdef ESP_PLATFORM
|
||||
LOGGER.error("default caps:");
|
||||
LOGGER.error(" total: {}", heap_caps_get_total_size(MALLOC_CAP_DEFAULT));
|
||||
LOGGER.error(" free: {}", heap_caps_get_free_size(MALLOC_CAP_DEFAULT));
|
||||
LOGGER.error(" min free: {}", heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
|
||||
LOGGER.error("internal caps:");
|
||||
LOGGER.error(" total: {}", heap_caps_get_total_size(MALLOC_CAP_INTERNAL));
|
||||
LOGGER.error(" free: {}", heap_caps_get_free_size(MALLOC_CAP_INTERNAL));
|
||||
LOGGER.error(" min free: {}", heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL));
|
||||
#endif
|
||||
}
|
||||
|
||||
static void logTaskInfo() {
|
||||
const char* name = pcTaskGetName(nullptr);
|
||||
const char* safe_name = name ? name : "main";
|
||||
LOGGER.error("Task: {}", safe_name);
|
||||
LOGGER.error("Stack watermark: {}", uxTaskGetStackHighWaterMark(NULL) * 4);
|
||||
}
|
||||
|
||||
namespace tt {
|
||||
|
||||
TT_NORETURN void _crash() {
|
||||
logTaskInfo();
|
||||
logMemoryInfo();
|
||||
// TODO: Add breakpoint when debugger is attached.
|
||||
#ifdef ESP_PLATFORM
|
||||
esp_system_abort("System halted. Connect debugger for more info.");
|
||||
#endif
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,7 +29,7 @@ static void get_hardware_key(uint8_t key[32]) {
|
||||
// MAC can be 6 or 8 bytes
|
||||
size_t mac_length = esp_mac_addr_len_get(ESP_MAC_EFUSE_FACTORY);
|
||||
LOGGER.info("Using MAC with length {}", mac_length);
|
||||
tt_check(mac_length <= 8);
|
||||
check(mac_length <= 8);
|
||||
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_EFUSE_FACTORY));
|
||||
|
||||
// Fill buffer with repeating MAC
|
||||
@@ -68,13 +68,13 @@ static void get_nvs_key(uint8_t key[32]) {
|
||||
|
||||
if (result != ESP_OK) {
|
||||
LOGGER.error("Failed to get key from NVS ({})", esp_err_to_name(result));
|
||||
tt_crash("NVS error");
|
||||
check(false, "NVS error");
|
||||
}
|
||||
|
||||
size_t length = 32;
|
||||
if (nvs_get_blob(handle, "key", key, &length) == ESP_OK) {
|
||||
LOGGER.info("Fetched key from NVS ({} bytes)", length);
|
||||
tt_check(length == 32);
|
||||
check(length == 32);
|
||||
} else {
|
||||
// TODO: Improved randomness
|
||||
esp_cpu_cycle_count_t cycle_count = esp_cpu_get_cycle_count();
|
||||
@@ -144,7 +144,7 @@ static int aes256CryptCbc(
|
||||
const unsigned char* input,
|
||||
unsigned char* output
|
||||
) {
|
||||
tt_check(key && iv && input && output);
|
||||
check(key && iv && input && output);
|
||||
|
||||
if ((length % 16) || (length == 0)) {
|
||||
return -1; // TODO: Proper error code from mbed lib?
|
||||
@@ -163,7 +163,7 @@ static int aes256CryptCbc(
|
||||
}
|
||||
|
||||
int encrypt(const uint8_t iv[16], const uint8_t* inData, uint8_t* outData, size_t dataLength) {
|
||||
tt_check(dataLength % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256");
|
||||
check(dataLength % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256)");
|
||||
uint8_t key[32];
|
||||
getKey(key);
|
||||
|
||||
@@ -175,7 +175,7 @@ int encrypt(const uint8_t iv[16], const uint8_t* inData, uint8_t* outData, size_
|
||||
}
|
||||
|
||||
int decrypt(const uint8_t iv[16], const uint8_t* inData, uint8_t* outData, size_t dataLength) {
|
||||
tt_check(dataLength % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256");
|
||||
check(dataLength % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256)");
|
||||
uint8_t key[32];
|
||||
getKey(key);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user