Seregon/rtnet-stack

Real-Time Embedded Network Stack

C/66 B/No license
platforms/rtnet_platform_freertos.c
rtnet-stack / platforms / rtnet_platform_freertos.c
1/**
2 * @file rtnet_platform_freertos.c
3 * @brief FreeRTOS-based platform hooks (critical sections, timing, TX)
4 * @note Requires FreeRTOS headers and a board-specific Ethernet transmit hook.
5 * @link https://github.com/seregonwar/rtnet-stack/blob/main/platforms/rtnet_platform_freertos.c
6 * @version 1.0.0
7 * @date 2026-01-07
8 * @author Seregon
9 *
10MIT License
11 
12Copyright (c) 2026 Seregon
13 
14Permission is hereby granted, free of charge, to any person obtaining a copy
15of this software and associated documentation files (the "Software"), to deal
16in the Software without restriction, including without limitation the rights
17to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18copies of the Software, and to permit persons to whom the Software is
19furnished to do so, subject to the following conditions:
20 
21The above copyright notice and this permission notice shall be included in all
22copies or substantial portions of the Software.
23 
24THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30SOFTWARE.
31 */
32 
33#include "rtnet_stack.h"
34#include "FreeRTOS.h"
35#include "task.h"
36 
37#if defined(_MSC_VER)
38 #define RTNET_WEAK __declspec(selectany)
39#else
40 #define RTNET_WEAK __attribute__((weak))
41#endif
42 
43/* Optional software loopback (for bring-up without NIC) */
44static bool g_loopback_enabled = false;
45 
46void RTNET_Platform_EnableLoopback(bool enable)
47{
48 g_loopback_enabled = enable;
49}
50 
51/* Board-specific transmit hook; override to use real NIC.
52 * Default behavior: optional software loopback for testing. */
53RTNET_WEAK void RTNET_Platform_EthTransmit(const uint8_t* data, uint16_t length)
54{
55 if (g_loopback_enabled && data != NULL && length > 0U) {
56 /* Feed back into RX path to emulate a receive (safe in host/bring-up) */
57 (void)RTNET_ProcessRxPacket(data, length);
58 }
59 /* Otherwise: drop silently; override this function in BSP for real TX */
60}
61 
62void RTNET_CriticalSectionEnter(void)
63{
64 taskENTER_CRITICAL();
65}
66 
67void RTNET_CriticalSectionExit(void)
68{
69 taskEXIT_CRITICAL();
70}
71 
72uint32_t RTNET_GetTimeMs(void)
73{
74 return (uint32_t)(xTaskGetTickCount() * (TickType_t)portTICK_PERIOD_MS);
75}
76 
77void RTNET_HardwareTransmit(const uint8_t* data, uint16_t length)
78{
79 RTNET_Platform_EthTransmit(data, length);
80}
81