BLF/APP/main.c

209 lines
6.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file main.c
* @brief Пример использования библиотеки blf для создания BLF-файла.
* @details Демонстрирует создание контейнера с CAN, LIN, LIN Error и ENV_DATA объектами.
* Показывает использование как старого формата CAN, так и расширенного VBLCANMessage2.
* Контекст создаётся на стеке (без динамической памяти).
*
* Компиляция для обычного ПК:
* gcc -o create_blf main.c blf.c -std=c99 -D_GNU_SOURCE
* Компиляция для встраиваемой системы с FatFS:
* arm-none-eabi-gcc -DUSE_FATFS -Ipath/to/fatfs -o create_blf main.c blf.c
*
* @author (Ваше имя)
* @date 2026-03-20
*/
#include "blf.h"
/* Вспомогательная структура для хранения семплов АЦП (используется только в примере) */
typedef struct {
uint16_t channel1;
uint16_t channel2;
uint16_t channel3;
uint16_t channel4;
uint16_t channel5;
} AdcSample;
int main() {
BLFContext ctx;
int ret;
SYSTEMTIME startTime = {2025, 1, 3, 1, 0, 0, 0, 0};
// Открываем файл со старым форматом CAN (VBLCANMessage)
ret = blf_open_ex(&ctx, "log.blf", &startTime, 1);
if (ret != 0) {
BLF_ERROR_PRINTF("Failed to open file, exiting.\n");
return 1;
}
// Начинаем контейнер
if (blf_start_container(&ctx, 0) != 0) {
BLF_ERROR_PRINTF("Failed to start container\n");
blf_close(&ctx);
return 1;
}
// Добавляем CAN сообщения в старом формате
CanMessageStruct canMsg = {
.channel = 2,
.id = 0x3F1,
.flags = CAN_MSG_FLAGS(CAN_DIR_RX, 0),
.dlc = 8,
.data = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
.timestamp = 0
// frameLength и bitCount не используются в старом формате
};
int j = 0;
int k = 0;
for (uint32_t i = 0; i < 10000; ++i) {
// Заполняем данные
canMsg.data[0] = j;
canMsg.data[1] = j / 2;
canMsg.data[2] = j / 3;
canMsg.data[3] = j / 4;
canMsg.data[4] = j / 5;
canMsg.data[5] = j / 6;
canMsg.data[6] = j / 7;
canMsg.data[7] = j / 8;
canMsg.timestamp += 100;
if (blf_add_can_message_struct(&ctx, &canMsg) != 0) {
return 1;
}
++k;
if (k > 100) {
k = 0;
++j;
if (j >= 100) {
j = 0;
}
}
}
// Завершаем контейнер
if (blf_end_container(&ctx) != 0) {
BLF_ERROR_PRINTF("Failed to end container\n");
blf_close(&ctx);
return 1;
}
// Закрываем файл
ret = blf_close(&ctx);
if (ret != 0) {
BLF_ERROR_PRINTF("Failed to close file\n");
return 1;
}
return 0;
}
/*
int main() {
BLFContext ctx;
int ret;
// Время начала измерения (1 января 2025 года, 00:00:00.000)
SYSTEMTIME startTime = {
.year = 2025,
.month = 1,
.dayOfWeek = 3,
.day = 1,
.hour = 0,
.minute = 0,
.second = 0,
.milliseconds = 0
};
// Открываем файл с поддержкой расширенного формата CAN (VBLCANMessage2)
ret = blf_open_ex(&ctx, "log.blf", &startTime, 1);
if (ret != 0) {
BLF_ERROR_PRINTF("Failed to open file, exiting.\n");
return 1;
}
BLF_ERROR_PRINTF("Using extended CAN format (VBLCANMessage2)\n");
// Начинаем контейнер
if (blf_start_container(&ctx, 0) != 0) {
BLF_ERROR_PRINTF("Failed to start container\n");
blf_close(&ctx);
return 1;
}
// Добавляем CAN-сообщения с дополнительной информацией о длительности кадра
CanMessageStruct canMsg = {
.channel = 1,
.id = 0x3F1,
.flags = CAN_MSG_FLAGS(CAN_DIR_RX, 0),
.dlc = 8,
.data = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
.timestamp = 0,
.frameLength = 6200, // 6.2 мкс длительность сообщения
.bitCount = 108 // 108 бит общее количество
};
uint8_t j = 0;
for (uint32_t i = 0; i < 10000; ++i) {
// Заполняем данные
canMsg.data[0] = j;
canMsg.data[1] = j / 2;
canMsg.data[2] = j / 3;
canMsg.data[3] = j / 4;
canMsg.data[4] = j / 5;
canMsg.data[5] = j / 6;
canMsg.data[6] = j / 7;
canMsg.data[7] = j / 8;
canMsg.timestamp += 100; // каждые 100 мс
// Используем расширенную функцию добавления CAN-сообщения
if (blf_add_can_message2_struct(&ctx, &canMsg) != 0) {
BLF_ERROR_PRINTF("Failed to add CAN message2\n");
blf_close(&ctx);
return 1;
}
++j;
if (j >= 100) {
j = 0;
}
// Периодически выводим прогресс
if (i % 10000 == 0) {
BLF_ERROR_PRINTF("Added %u messages (frameLength=%u ns, bitCount=%u)\n",
i, canMsg.frameLength, canMsg.bitCount);
}
}
// Завершаем контейнер
if (blf_end_container(&ctx) != 0) {
BLF_ERROR_PRINTF("Failed to end container\n");
blf_close(&ctx);
return 1;
}
// Закрываем файл
ret = blf_close(&ctx);
if (ret != 0) {
BLF_ERROR_PRINTF("Failed to close file\n");
return 1;
}
BLF_ERROR_PRINTF("File log_extended.blf created successfully!\n");
BLF_ERROR_PRINTF("Top-level objects: %d\n", ctx.objectCount);
BLF_ERROR_PRINTF("Used extended CAN format (ObjectType=%d)\n", BL_OBJ_TYPE_CAN_MESSAGE2);
return 0;
}
*/