Отладка модуля FIFO

This commit is contained in:
ru.sadekov 2025-10-24 15:33:11 +03:00
parent bfe0df9256
commit b32be8eaa4
14 changed files with 202 additions and 27 deletions

18
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,18 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
// "defines": ["DO_UNIT_TESTS=1"],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "linux-clang-x64",
"configurationProvider": "ms-vscode.cmake-tools",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"
}
],
"version": 4
}

View File

@ -13,6 +13,11 @@
"fifo_type.h": "c", "fifo_type.h": "c",
"chrono": "c", "chrono": "c",
"span": "c", "span": "c",
"worker.h": "c" "worker.h": "c",
"fifo.h": "c",
"string.h": "c",
"*.ipp": "c",
"cstring": "c",
"stddef.h": "c"
} }
} }

View File

@ -7,6 +7,7 @@ project(${PROJECT_NAME} LANGUAGES C)
set(CMAKE_C_STANDARD 11) set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF) set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_executable(${PROJECT_NAME} ${SOURCE_DIR}/main.c) add_executable(${PROJECT_NAME} ${SOURCE_DIR}/main.c)
@ -29,8 +30,7 @@ set(SOURCES
) )
target_include_directories(${PROJECT_NAME} PRIVATE ${HEADERS}) target_include_directories(${PROJECT_NAME} PRIVATE ${HEADERS})
# target_compile_definitions(${PROJECT_NAME} PRIVATE __USE_POSIX199309 __USE_ISOC11) # target_compile_definitions(${PROJECT_NAME} PUBLIC )
target_compile_definitions(${PROJECT_NAME} PRIVATE _POSIX_C_SOURCE=199309L _ISOC11_SOURCE)
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

View File

@ -68,6 +68,19 @@ FIFO пуст или FIFO полон. Вся логика работы прог
## Функции и их параметры ## Функции и их параметры
### worker
- *int WORK_execute(int (*callback)(workerHost_s*), workerHost_s *worker);*
- int WORK_preparePackage(workerHost_s *worker);
- int WORK_sendPackage(workerHost_s *worker);
- int WORK_waitPackage(workerHost_s *worker);
- int WORK_receivePackage(workerHost_s *worker);
- int WORK_checkPackage(workerHost_s *worker);
- int WORK_parsePackage(workerHost_s *worker);
- int WORK_fillBuffer8b(uint8_t *buffer, size_t size);
// tools
int WORK_printBlocks(workerHost_s *worker, uint8_t mode);
int WORK_testFill(uint8_t* buffer, size_t bufferSize);
### tools ### tools
- TOOL_workerPrintBlocks - TOOL_workerPrintBlocks
- hdlcHost_s *worker - указатель на машину - hdlcHost_s *worker - указатель на машину

View File

@ -1,2 +1,90 @@
#include "fifo.h" #include "fifo.h"
#include "stdio.h"
// if (FIFO_get(fifo, byte) == 0) {
// OK
// } else {
// FIFO ERROR
// }
int FIFO_isUsable(fifo_s* fifo);
// Инициализация fifo
int FIFO_init(fifo_s* fifo) {
if (fifo != NULL) {
for (size_t i = 0; i < FIFO_DATA_SIZE; ++i) {
fifo->data[i] = 0;
}
fifo->head = 0;
fifo->state = FIFOS_empty;
fifo->isInitialized = 1;
if (FIFO_isUsable(fifo) == 0) {
return 0;
}
}
return -1;
}
// Заполнение fifo
int FIFO_put(fifo_s* fifo, uint8_t data) {
if (FIFO_isUsable(fifo) == 0) { // Не заполнено
if ((fifo->state == FIFOS_empty) || (fifo->state == FIFOS_ready)) {
fifo->data[fifo->head] = data;
if (fifo->head < FIFO_DATA_SIZE-1) {
fifo->head++;
fifo->state = FIFOS_ready;
} else {
fifo->head = FIFO_DATA_SIZE-1;
fifo->state = FIFOS_full;
}
return 0;
}
}
return -1;
}
// Получение из fifo
int FIFO_get(fifo_s* fifo, uint8_t *data) {
if (FIFO_isUsable(fifo) == 0) {
if ((fifo->state == FIFOS_full) || (fifo->state == FIFOS_ready)) {
*data = fifo->data[fifo->head-1];
fifo->data[fifo->head-1] = 0;
if (fifo->head > 1) {
fifo->head--;
fifo->state = FIFOS_ready;
} else {
fifo->head = 0;
fifo->state = FIFOS_empty;
}
return 0;
}
}
return -1;
}
int FIFO_print(fifo_s* fifo) {
if (FIFO_isUsable(fifo) == 0) {
printf("FIFO: ");
for (size_t i = 0; i < FIFO_DATA_SIZE; i++) {
printf("%d", fifo->data[i]);
}
printf("\n");
return 0;
}
return -1;
}
// Проверка доступности fifo
int FIFO_isUsable(fifo_s* fifo) {
if (fifo != NULL) {
if (fifo->isInitialized == 1) {
return 0;
}
}
return -1;
}

View File

@ -3,6 +3,25 @@
#define FIFO_H #define FIFO_H
#include "fifo_type.h" #include "fifo_type.h"
#include <stddef.h>
// Размер FIFO
#define FIFO_DATA_SIZE 8
// Структура FIFO
typedef struct {
uint8_t data[FIFO_DATA_SIZE];
uint8_t head;
fifoState_e state;
uint8_t isInitialized;
} fifo_s;
// Функции FIFO
int FIFO_init(fifo_s* fifo);
int FIFO_put(fifo_s* fifo, uint8_t data);
int FIFO_get(fifo_s* fifo, uint8_t *data);
int FIFO_print(fifo_s* fifo);
int FIFO_isUsable(fifo_s* fifo);
#endif // FIFO_H #endif // FIFO_H

View File

@ -10,13 +10,7 @@ typedef enum {
FIFOS_ready, FIFOS_ready,
FIFOS_full, FIFOS_full,
FIFOS_error FIFOS_error
} fifoState; } fifoState_e;
// Структура FIFO
typedef struct {
uint8_t data[8];
uint8_t pointer;
fifoState state;
} fifo_s;
#endif // FIFO_TYPE_H #endif // FIFO_TYPE_H

View File

@ -1,12 +1,5 @@
#include "main.h" #include "main.h"
#if __cplusplus
#pragma message ("plus")
#else
#pragma message ("pure")
#endif
// Перечисление статусов машины состояния // Перечисление статусов машины состояния
enum bigLoopOsState_e { enum bigLoopOsState_e {
OS_first = 0, OS_first = 0,

View File

@ -2,6 +2,8 @@
#ifndef MAIN_H #ifndef MAIN_H
#define MAIN_H #define MAIN_H
#define DO_UNIT_TESTS 1
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>

View File

@ -1,8 +1,18 @@
// Файл с общими функциями и конфигурацией // Файл с общими функциями и конфигурацией
#include "main.h" #define _POSIX_C_SOURCE 199309L
#include <time.h> #include <time.h>
#if __cplusplus
#pragma message ("plus")
#else
#pragma message ("pure")
#endif
#include "main.h"
// Вычисление контрольной суммы CRC16 // Вычисление контрольной суммы CRC16
uint16_t CRC16_compute(uint8_t *data, size_t length) { uint16_t CRC16_compute(uint8_t *data, size_t length) {
@ -53,12 +63,12 @@ uint16_t TOOL_reverseBitsN(uint16_t value, uint8_t n) {
void TOOL_msleep(long milliseconds) { void TOOL_msleep(long milliseconds) {
struct timespec ts; struct timespec time;
ts.tv_sec = milliseconds / 1000; // Seconds part time.tv_sec = milliseconds / 1000;
ts.tv_nsec = (milliseconds % 1000) * 1000000; // Nanoseconds part time.tv_nsec = (milliseconds % 1000) * 1000000;
// nanosleep can be interrupted by signals, so we loop if interrupted // nanosleep can be interrupted by signals, so we loop if interrupted
while (nanosleep(&ts, &ts) == -1) { while (nanosleep(&time, &time) == -1) {
// If interrupted, remaining time is in ts, so we continue sleeping // If interrupted, remaining time is in ts, so we continue sleeping
// unless an actual error occurred (other than EINTR) // unless an actual error occurred (other than EINTR)
// For simplicity, this example assumes EINTR is the only -1 cause. // For simplicity, this example assumes EINTR is the only -1 cause.

View File

@ -29,15 +29,24 @@ int WORK_sendPackage(workerHost_s *worker) {
// Ожидание пакета // Ожидание пакета
int WORK_waitPackage(workerHost_s *worker) { int WORK_waitPackage(workerHost_s *worker) {
printf("WORKX: .. Waiting for a package\n"); printf("WORKX: .. Waiting for a package\n");
return 0; if (FIFO_isUsable(worker->fifo) == 0) {
return 0;
}
return -1;
} }
int WORK_receivePackage(workerHost_s *worker) { int WORK_receivePackage(workerHost_s *worker) {
printf("WORKX: .. Receiving package\n"); printf("WORKX: .. Receiving package\n");
// Receiving in buffer // Receiving in buffer
uint8_t buf;
if (FIFO_get(worker->fifo, &buf) == 0) {
// return 0;
}
WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE); return -1;
if (DO_UNIT_TESTS){WORK_testFill(worker->byteBuffer, WORK_BYTE_BUFFER_SIZE);}
return 0; return 0;
} }

View File

@ -10,7 +10,7 @@
#include "hdlc_type.h" #include "hdlc_type.h"
#include "fifo_type.h" #include "fifo.h"
// Структура устройства // Структура устройства
typedef struct { typedef struct {
@ -33,9 +33,11 @@ int WORK_parsePackage(workerHost_s *worker);
int WORK_fillBuffer8b(uint8_t *buffer, size_t size); int WORK_fillBuffer8b(uint8_t *buffer, size_t size);
// tools // toolsNtests
int WORK_printBlocks(workerHost_s *worker, uint8_t mode); int WORK_printBlocks(workerHost_s *worker, uint8_t mode);
int WORK_testFill(uint8_t* buffer, size_t bufferSize); int WORK_testFill(uint8_t* buffer, size_t bufferSize);
int WORK_testFifo(fifo_s *fifo);
#endif //WORKER_H #endif //WORKER_H

View File

@ -16,6 +16,10 @@ workerHost_s* WORK_masterInit () {
hdlcMaster.fifo = &hdlcMasterFifo; hdlcMaster.fifo = &hdlcMasterFifo;
hdlcMaster.isInitialized = true; hdlcMaster.isInitialized = true;
hdlcMaster.byteBuffer = masterBuffer; hdlcMaster.byteBuffer = masterBuffer;
FIFO_init(&hdlcMasterFifo);
if (DO_UNIT_TESTS){WORK_testFifo(hdlcMaster.fifo);}
printf("WMAST: Master initialization done\n"); printf("WMAST: Master initialization done\n");
WORK_printBlocks(&hdlcMaster, 3); WORK_printBlocks(&hdlcMaster, 3);

View File

@ -32,7 +32,7 @@ int WORK_printBlocks(workerHost_s *worker, uint8_t mode) {
} }
printf("\n"); printf("\n");
printf("TOOL: .. Pointer: %d ", worker->fifo->pointer); printf("TOOL: .. Pointer: %d ", worker->fifo->head);
} }
return 0; return 0;
@ -57,6 +57,24 @@ int WORK_testFill(uint8_t* buffer, size_t bufferSize) {
} }
int WORK_testFifo(fifo_s *fifo) {
printf("TEST: FIFO START:\n");
FIFO_put(fifo, 4);
FIFO_put(fifo, 3);
FIFO_put(fifo, 8);
FIFO_print(fifo);
uint8_t arr[10] = {0};
for (int a = 0; a < 10; a++) {
FIFO_get(fifo, &arr[a]);
printf("%d:", arr[a]);
}
printf("\n");
printf("TEST: FIFO END:\n");
}
// Заполнение массива // Заполнение массива
int WORK_fillBuffer8b(uint8_t *buffer, size_t size) { int WORK_fillBuffer8b(uint8_t *buffer, size_t size) {
memset(buffer, 0, size); memset(buffer, 0, size);