Real-Time Embedded Network Stack
| 1 | /** |
| 2 | * @file rtnet_platform_baremetal.c |
| 3 | * @brief Bare-metal platform hooks using IRQ disable/enable and a weak TX hook. |
| 4 | * @link https://github.com/seregonwar/rtnet-stack/blob/main/platforms/rtnet_platform_baremetal.c |
| 5 | * @version 1.0.0 |
| 6 | * @date 2026-01-07 |
| 7 | * @author Seregon |
| 8 | * |
| 9 | MIT License |
| 10 | |
| 11 | Copyright (c) 2026 Seregon |
| 12 | |
| 13 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | of this software and associated documentation files (the "Software"), to deal |
| 15 | in the Software without restriction, including without limitation the rights |
| 16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | copies of the Software, and to permit persons to whom the Software is |
| 18 | furnished to do so, subject to the following conditions: |
| 19 | |
| 20 | The above copyright notice and this permission notice shall be included in all |
| 21 | copies or substantial portions of the Software. |
| 22 | |
| 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | SOFTWARE. |
| 30 | */ |
| 31 | |
| 32 | #include "rtnet_stack.h" |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | #if defined(_MSC_VER) |
| 36 | #define RTNET_WEAK __declspec(selectany) |
| 37 | #else |
| 38 | #define RTNET_WEAK __attribute__((weak)) |
| 39 | #endif |
| 40 | |
| 41 | /* Board-specific transmit hook to be provided by the BSP */ |
| 42 | RTNET_WEAK void RTNET_Platform_EthTransmit(const uint8_t* data, uint16_t length) |
| 43 | { |
| 44 | (void)data; |
| 45 | (void)length; |
| 46 | /* Implement MAC driver TX here */ |
| 47 | } |
| 48 | |
| 49 | /* Weak hooks for IRQ control; override with MCU-specific intrinsics */ |
| 50 | RTNET_WEAK void RTNET_Platform_DisableIRQ(void) |
| 51 | { |
| 52 | /* __disable_irq(); */ |
| 53 | } |
| 54 | |
| 55 | RTNET_WEAK void RTNET_Platform_EnableIRQ(void) |
| 56 | { |
| 57 | /* __enable_irq(); */ |
| 58 | } |
| 59 | |
| 60 | static volatile uint32_t g_time_ms = 0U; |
| 61 | |
| 62 | void RTNET_CriticalSectionEnter(void) |
| 63 | { |
| 64 | RTNET_Platform_DisableIRQ(); |
| 65 | } |
| 66 | |
| 67 | void RTNET_CriticalSectionExit(void) |
| 68 | { |
| 69 | RTNET_Platform_EnableIRQ(); |
| 70 | } |
| 71 | |
| 72 | uint32_t RTNET_GetTimeMs(void) |
| 73 | { |
| 74 | /* In bare-metal mode, ensure a 1ms tick updates g_time_ms (e.g., SysTick) */ |
| 75 | return g_time_ms; |
| 76 | } |
| 77 | |
| 78 | void RTNET_HardwareTransmit(const uint8_t* data, uint16_t length) |
| 79 | { |
| 80 | RTNET_Platform_EthTransmit(data, length); |
| 81 | } |
| 82 | |
| 83 | /* Call this from a 1ms ISR (e.g., SysTick) to advance time */ |
| 84 | void RTNET_Platform_Tick1ms(void) |
| 85 | { |
| 86 | g_time_ms++; |
| 87 | } |
| 88 |