ether2ser 0.1.0
Ethernet <-> synchronous V.24 bridge firmware for RP2040 + W5500
Loading...
Searching...
No Matches
common.h
Go to the documentation of this file.
1
2/*
3 * ether2ser - Ethernet <-> synchronous V.24 (RS-232/V.28) bridge
4 *
5 * File: src/system/common.h
6 * Purpose: Shared helpers and logging macros.
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Copyright (c) 2026 Florian <f.leuze@outlook.de>
11 */
12
13#ifndef COMMON_H
14#define COMMON_H
15
16// Related headers
17
18// Standard library headers
19#include <inttypes.h>
20#include <limits.h>
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdint.h>
24
25// Project Headers
26
27// Generated headers
28
32#define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
33
37#define RX_SHIFT_TO_LSB (3U * CHAR_BIT)
38
39// Important: Keep this up with reality. It has to match the actual amount
40// of cycles used in the pio program, otherwise the clock will be off
44// #define TX_PIO_CPB_NUM 17.0
45// #define TX_PIO_CPB_DEN 4.0
46// #define TX_PIO_CYCLES_PER_BIT (TX_PIO_CPB_NUM / TX_PIO_CPB_DEN)
47#define TX_PIO_CYCLES_PER_BIT 8U
48
52#define US_PER_SECOND 1000000U
53
57#define FLUSH_LOG_BEFORE_REBOOT_MS 200U
58
59/*
60 * Some arm-none-eabi/newlib combinations may miss PRI* macros unless the
61 * headers line up exactly. Provide conservative fallbacks when absent.
62 */
63#ifndef PRIu32
64#define PRIu32 "u"
65#endif
66
67#ifndef PRIX32
68#define PRIX32 "X"
69#endif
70
71#ifndef PRIu64
72#if UINT64_MAX == ULONG_MAX
73#define PRIu64 "lu"
74#elif UINT64_MAX == ULLONG_MAX
75#define PRIu64 "llu"
76#else
77#error "Unsupported uint64_t format"
78#endif
79#endif
80
81#define UINT32_ALL_ONES UINT32_MAX
82
95
100void set_loglevel(log_level_t level);
101
107
112bool log_take_emitted_flag(void);
113
114void log_core1_drain(void);
115uint32_t log_take_dropped_count(void);
116uint32_t log_get_high_water_mark(void);
117
118// extern log_level_t current_log_level;
119
125static inline const char* log_level_tag(log_level_t level)
126{
127 switch (level)
128 {
129 case LOG_LEVEL_ERROR:
130 return "ERROR";
131 case LOG_LEVEL_INFO:
132 return "INFO";
133 case LOG_LEVEL_DEBUG:
134 return "DEBUG";
135 case LOG_LEVEL_TRACE:
136 return "TRACE";
137 case LOG_LEVEL_PLAIN:
138 return "";
139 default:
140 return "LOG";
141 }
142}
143
150void log_write(log_level_t level, const char* fmt, ...);
151#define LOG(level, fmt, ...) \
152 do \
153 { \
154 /* Evaluate level once to avoid double-evaluation side effects. */ \
155 const log_level_t _log_level = (level); \
156 if (get_loglevel() >= _log_level) \
157 { \
158 log_write(_log_level, (fmt), ##__VA_ARGS__); \
159 } \
160 } while (0)
161
162#define LOG_PLAIN(...) LOG(LOG_LEVEL_PLAIN, __VA_ARGS__)
163#define LOG_ERROR(...) LOG(LOG_LEVEL_ERROR, __VA_ARGS__)
164#define LOG_INFO(...) LOG(LOG_LEVEL_INFO, __VA_ARGS__)
165#define LOG_DEBUG(...) LOG(LOG_LEVEL_DEBUG, __VA_ARGS__)
166#define LOG_TRACE(...) LOG(LOG_LEVEL_TRACE, __VA_ARGS__)
167
168#define PRINT_FRAME_HEX(label, payload_ptr, length) \
169 do \
170 { \
171 LOG_DEBUG("%s", (label)); \
172 for (size_t _i = 0; _i < (length); _i++) \
173 { \
174 LOG_DEBUG("%02X ", (unsigned)(payload_ptr)[_i]); \
175 if (_i % 16 == 15) \
176 { \
177 LOG_DEBUG("\r\n"); \
178 } \
179 } \
180 LOG_DEBUG("\r\n"); \
181 } while (0)
182
183#endif /* COMMON_H */
void log_write(log_level_t level, const char *fmt,...)
Emit one formatted log message if level is enabled.
Definition log.c:71
log_level_t get_loglevel(void)
Get current global log level.
Definition log.c:61
static const char * log_level_tag(log_level_t level)
Get printable tag for a log level.
Definition common.h:125
void log_core1_drain(void)
Definition log.c:115
bool log_take_emitted_flag(void)
Atomically read and clear "log emitted" flag.
Definition log.c:144
uint32_t log_take_dropped_count(void)
Definition log.c:132
void set_loglevel(log_level_t level)
Set current global log level.
Definition log.c:66
log_level_t
Log verbosity levels.
Definition common.h:87
@ LOG_LEVEL_PLAIN
Definition common.h:88
@ LOG_LEVEL_DEBUG
Definition common.h:91
@ LOG_LEVEL_ERROR
Definition common.h:89
@ LOG_LEVEL_TRACE
Definition common.h:92
@ LOG_LEVEL_INFO
Definition common.h:90
uint32_t log_get_high_water_mark(void)
Definition log.c:139