Skip to content

Commit 3e4a8df

Browse files
committed
Provide WDT backed reset for ArduinoCore-renesas.
1 parent b9827fa commit 3e4a8df

File tree

4 files changed

+108
-10
lines changed

4 files changed

+108
-10
lines changed

examples/AsyncReset/AsyncReset.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ void setup()
2727
while (!Serial) { }
2828

2929
Serial.println("Trigger async reset in 5 seconds ...");
30-
cyphal::support::platform::reset_async(std::chrono::milliseconds(5000));
30+
auto const rc = cyphal::support::platform::reset_async(std::chrono::milliseconds(5000));
31+
if (rc.has_value()) {
32+
Serial.print("reset_async failed with error code ");
33+
Serial.println(static_cast<int>(rc.value()));
34+
}
35+
3136
start = millis();
3237
}
3338

examples/SyncReset/SyncReset.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ void setup()
2121
while (!Serial) { }
2222

2323
Serial.println("Trigger sync reset in 5 seconds ...");
24-
cyphal::support::platform::reset_sync(std::chrono::milliseconds(5000));
24+
auto const rc = cyphal::support::platform::reset_sync(std::chrono::milliseconds(5000));
25+
if (rc.has_value()) {
26+
Serial.print("reset_sync failed with error code ");
27+
Serial.println(static_cast<int>(rc.value()));
28+
}
2529
}
2630

2731
void loop()

src/reset/reset-renesas.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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); /* Minium 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) */

src/reset/reset.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,26 @@ enum class Error
3939

4040
/* Immediately after calling this method a reset is performed. */
4141
std::optional<Error> reset()
42-
#if !defined(ARDUINO_ARCH_RP2040)
43-
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
42+
#if !defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_RENESAS)
43+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040 and ARDUINO_ARCH_RENESAS.")))
4444
#endif
4545
;
4646

4747
/* This method performs a reset in 'ms' milliseconds after its invocation,
4848
* blocking while waiting for the time to expire.
4949
*/
5050
std::optional<Error> reset_sync(std::chrono::milliseconds const ms)
51-
#if !defined(ARDUINO_ARCH_RP2040)
52-
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
51+
#if !defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_RENESAS)
52+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040 and ARDUINO_ARCH_RENESAS.")))
5353
#endif
5454
;
5555

5656
/* This method performs a reset in 'ms' milliseconds after its invocation,
5757
* but returns immediately after its invocation.
5858
*/
5959
std::optional<Error> reset_async(std::chrono::milliseconds const ms)
60-
#if !defined(ARDUINO_ARCH_RP2040)
61-
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
60+
#if !defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_RENESAS)
61+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040 and ARDUINO_ARCH_RENESAS.")))
6262
#endif
6363
;
6464

@@ -67,8 +67,8 @@ __attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040
6767
* regular watchdog feeding i.e. in the main loop.
6868
*/
6969
bool is_async_reset_pending()
70-
#if !defined(ARDUINO_ARCH_RP2040)
71-
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
70+
#if !defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_RENESAS)
71+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040 and ARDUINO_ARCH_RENESAS.")))
7272
#endif
7373
;
7474

0 commit comments

Comments
 (0)