|
| 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_RP2040) && !defined(ARDUINO_ARCH_MBED) |
| 15 | + |
| 16 | +#include "hardware/watchdog.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 | +static uint32_t const RP2040_MAX_DELAY_ms = 0x7FFFFF; |
| 33 | + |
| 34 | +/************************************************************************************** |
| 35 | + * GLOBAL VARIABLES |
| 36 | + **************************************************************************************/ |
| 37 | + |
| 38 | +static bool is_async_reset_pending_flag = false; |
| 39 | + |
| 40 | +/************************************************************************************** |
| 41 | + * FUNCTION DEFINITION |
| 42 | + **************************************************************************************/ |
| 43 | + |
| 44 | +std::optional<Error> reset() |
| 45 | +{ |
| 46 | + watchdog_enable(/* delay_ms */ 0, /* pause_on_debug */ true); |
| 47 | + return std::nullopt; |
| 48 | +} |
| 49 | + |
| 50 | +std::optional<Error> reset_sync(std::chrono::milliseconds const ms) |
| 51 | +{ |
| 52 | + if (ms.count() > RP2040_MAX_DELAY_ms) |
| 53 | + return Error::InvalidParam; |
| 54 | + |
| 55 | + watchdog_enable(/* delay_ms */ ms.count(), /* pause_on_debug */ true); |
| 56 | + for(;;) { delay(100); } /* Wait for the watchdog to bite. */ |
| 57 | + return std::nullopt; |
| 58 | +} |
| 59 | + |
| 60 | +std::optional<Error> reset_async(std::chrono::milliseconds const ms) |
| 61 | +{ |
| 62 | + if (ms.count() > RP2040_MAX_DELAY_ms) |
| 63 | + return Error::InvalidParam; |
| 64 | + |
| 65 | + watchdog_enable(/* delay_ms */ ms.count(), /* pause_on_debug */ true); |
| 66 | + |
| 67 | + is_async_reset_pending_flag = true; |
| 68 | + |
| 69 | + return std::nullopt; |
| 70 | +} |
| 71 | + |
| 72 | +bool is_async_reset_pending() |
| 73 | +{ |
| 74 | + return is_async_reset_pending_flag; |
| 75 | +} |
| 76 | + |
| 77 | +/************************************************************************************** |
| 78 | + * NAMESPACE |
| 79 | + **************************************************************************************/ |
| 80 | + |
| 81 | +} /* cyphal::support::platform */ |
| 82 | + |
| 83 | +#endif /* defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED) */ |
0 commit comments