|
| 1 | +/** |
| 2 | + * This software is distributed under the terms of the MIT License. |
| 3 | + * Copyright (c) 2023 LXRobotics. |
| 4 | + * Author: Alexander Entinger <[email protected]> |
| 5 | + * Contributors: https://github.com/107-systems/107-Arduino-Cyphal-Support/graphs/contributors. |
| 6 | + */ |
| 7 | + |
| 8 | +/************************************************************************************** |
| 9 | + * INCLUDE |
| 10 | + **************************************************************************************/ |
| 11 | + |
| 12 | +#include "reset.h" |
| 13 | + |
| 14 | +#if defined(ARDUINO_ARCH_RENESAS) |
| 15 | + |
| 16 | +#include <WDT.h> |
| 17 | + |
| 18 | +/* Provide prototype for Arduino's delay function. */ |
| 19 | +extern "C" void delay(unsigned long); |
| 20 | + |
| 21 | +/************************************************************************************** |
| 22 | + * NAMESPACE |
| 23 | + **************************************************************************************/ |
| 24 | + |
| 25 | +namespace cyphal::support::platform |
| 26 | +{ |
| 27 | + |
| 28 | +/************************************************************************************** |
| 29 | + * GLOBAL CONSTANTS |
| 30 | + **************************************************************************************/ |
| 31 | + |
| 32 | +#if defined(ARDUINO_MINIMA) || defined(ARDUINO_UNOWIFIR4) |
| 33 | +static uint32_t const WATCHDOG_MAX_DELAY_ms = 5592; |
| 34 | +#elif defined(ARDUINO_PORTENTA_C33) |
| 35 | +static uint32_t const WATCHDOG_MAX_DELAY_ms = 2684; |
| 36 | +#else |
| 37 | +# error "Your selected Renesas MCU is not supported" |
| 38 | +#endif |
| 39 | + |
| 40 | +/************************************************************************************** |
| 41 | + * GLOBAL VARIABLES |
| 42 | + **************************************************************************************/ |
| 43 | + |
| 44 | +static bool is_async_reset_pending_flag = false; |
| 45 | + |
| 46 | +/************************************************************************************** |
| 47 | + * FUNCTION DEFINITION |
| 48 | + **************************************************************************************/ |
| 49 | + |
| 50 | +std::optional<Error> reset() |
| 51 | +{ |
| 52 | + WDT.begin(1); /* Minimum delay is 1 ms. */ |
| 53 | + return std::nullopt; |
| 54 | +} |
| 55 | + |
| 56 | +std::optional<Error> reset_sync(std::chrono::milliseconds const ms) |
| 57 | +{ |
| 58 | + if (ms.count() > WATCHDOG_MAX_DELAY_ms) |
| 59 | + return Error::InvalidParam; |
| 60 | + |
| 61 | + WDT.begin(ms.count()); |
| 62 | + for(;;) { delay(100); } /* Wait for the watchdog to bite. */ |
| 63 | + return std::nullopt; |
| 64 | +} |
| 65 | + |
| 66 | +std::optional<Error> reset_async(std::chrono::milliseconds const ms) |
| 67 | +{ |
| 68 | + if (ms.count() > WATCHDOG_MAX_DELAY_ms) |
| 69 | + return Error::InvalidParam; |
| 70 | + |
| 71 | + WDT.begin(ms.count()); |
| 72 | + |
| 73 | + is_async_reset_pending_flag = true; |
| 74 | + |
| 75 | + return std::nullopt; |
| 76 | +} |
| 77 | + |
| 78 | +bool is_async_reset_pending() |
| 79 | +{ |
| 80 | + return is_async_reset_pending_flag; |
| 81 | +} |
| 82 | + |
| 83 | +/************************************************************************************** |
| 84 | + * NAMESPACE |
| 85 | + **************************************************************************************/ |
| 86 | + |
| 87 | +} /* cyphal::support::platform */ |
| 88 | + |
| 89 | +#endif /* defined(ARDUINO_ARCH_RENESAS) */ |
0 commit comments