Files
CelloEffects/Appli/Core/Src/st67_glue.c
T
2026-10-24 00:14:53 +02:00

99 lines
2.5 KiB
C

#include "st67_glue.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "logging.h"
#include "spi_iface.h"
char st67_log_ring[ST67_LOG_RING_SIZE];
volatile uint32_t st67_log_head;
/* Niveau le plus verbeux conserve dans le tampon (LOG_ERROR < LOG_WARN < LOG_INFO < LOG_DEBUG). */
#define ST67_LOG_MAX_LEVEL LOG_INFO
static const char *level_tag(uint32_t level)
{
switch (level)
{
case LOG_ERROR: return "E ";
case LOG_WARN: return "W ";
case LOG_INFO: return "I ";
default: return "D ";
}
}
static void ring_write(const char *text, uint32_t len)
{
uint32_t primask = __get_PRIMASK();
__disable_irq();
uint32_t head = st67_log_head;
for (uint32_t i = 0; i < len; i++)
{
st67_log_ring[head] = text[i];
head = (head + 1U) % ST67_LOG_RING_SIZE;
}
st67_log_head = head;
__set_PRIMASK(primask);
}
/* Implementation attendue par les macros LogInfo/LogError/... du driver (composant "Logging" non
* retenu, et pas de UART : les traces vont dans le tampon circulaire st67_log_ring). */
void vLoggingPrintf(uint32_t log_level,
const uint8_t metadata_print,
const uint32_t line_number,
const char *const p_file_name,
const char *const p_format,
...)
{
char line[128];
int n = 0;
if (log_level > ST67_LOG_MAX_LEVEL)
{
return; /* les traces DEBUG du driver saturent le tampon circulaire */
}
n += snprintf(line + n, sizeof(line) - (size_t)n, "%s", level_tag(log_level));
if (metadata_print != 0U && p_file_name != NULL)
{
n += snprintf(line + n, sizeof(line) - (size_t)n, "%s:%lu ", p_file_name, (unsigned long)line_number);
}
va_list args;
va_start(args, p_format);
int m = vsnprintf(line + n, sizeof(line) - (size_t)n, p_format, args);
va_end(args);
if (m < 0)
{
return;
}
n += m;
if ((size_t)n >= sizeof(line))
{
n = (int)sizeof(line) - 1; /* ligne tronquee */
}
ring_write(line, (uint32_t)n);
}
void ST67_ExtiCallback(uint16_t gpio_pin)
{
if (gpio_pin != SPI_RDY_Pin)
{
return;
}
/* front montant : le module a des donnees a transmettre ; front descendant : il a acquitte l'en-tete */
if (HAL_GPIO_ReadPin(SPI_RDY_GPIO_Port, SPI_RDY_Pin) == GPIO_PIN_SET)
{
(void)spi_on_txn_data_ready();
}
else
{
(void)spi_on_header_ack();
}
}