Real-Time Embedded Network Stack
| 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 | * |
| 10 | MIT License |
| 11 | |
| 12 | Copyright (c) 2026 Seregon |
| 13 | |
| 14 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | of this software and associated documentation files (the "Software"), to deal |
| 16 | in the Software without restriction, including without limitation the rights |
| 17 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 18 | copies of the Software, and to permit persons to whom the Software is |
| 19 | furnished to do so, subject to the following conditions: |
| 20 | |
| 21 | The above copyright notice and this permission notice shall be included in all |
| 22 | copies or substantial portions of the Software. |
| 23 | |
| 24 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 27 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 29 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | SOFTWARE. |
| 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) */ |
| 44 | static bool g_loopback_enabled = false; |
| 45 | |
| 46 | void 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. */ |
| 53 | RTNET_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 | |
| 62 | void RTNET_CriticalSectionEnter(void) |
| 63 | { |
| 64 | taskENTER_CRITICAL(); |
| 65 | } |
| 66 | |
| 67 | void RTNET_CriticalSectionExit(void) |
| 68 | { |
| 69 | taskEXIT_CRITICAL(); |
| 70 | } |
| 71 | |
| 72 | uint32_t RTNET_GetTimeMs(void) |
| 73 | { |
| 74 | return (uint32_t)(xTaskGetTickCount() * (TickType_t)portTICK_PERIOD_MS); |
| 75 | } |
| 76 | |
| 77 | void RTNET_HardwareTransmit(const uint8_t* data, uint16_t length) |
| 78 | { |
| 79 | RTNET_Platform_EthTransmit(data, length); |
| 80 | } |
| 81 |