Skip to content

Commit 160fafc

Browse files
authored
Provide common API for triggering a synchronous or asynchronous reset on various Cyphal enabled platforms. (#6)
1 parent 058b42a commit 160fafc

File tree

7 files changed

+246
-0
lines changed

7 files changed

+246
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ if (auto const opt_err = cyphal::support::save(kv_storage, *node_registry); opt_
4343
Serial.println(static_cast<int>(opt_err.value()));
4444
}
4545
```
46+
* API for performing **synchronous and asynchronous resets**.
47+
```C++
48+
/* Synchronous reset: */
49+
cyphal::support::platform::reset_sync(std::chrono::milliseconds(5000));
50+
/* Asynchronous reset: */
51+
cyphal::support::platform::reset_async(std::chrono::milliseconds(5000));
52+
```

examples/AsyncReset/AsyncReset.ino

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 <107-Arduino-Cyphal-Support.h>
13+
14+
/**************************************************************************************
15+
* GLOBAL VARIABLES
16+
**************************************************************************************/
17+
18+
static unsigned long start = 0;
19+
20+
/**************************************************************************************
21+
* SETUP/LOOP
22+
**************************************************************************************/
23+
24+
void setup()
25+
{
26+
Serial.begin(115200);
27+
while (!Serial) { }
28+
29+
Serial.println("Trigger async reset in 5 seconds ...");
30+
cyphal::support::platform::reset_async(std::chrono::milliseconds(5000));
31+
start = millis();
32+
}
33+
34+
void loop()
35+
{
36+
char msg[32] = {0};
37+
snprintf(msg, sizeof(msg), "[ %ld ]", (millis() - start));
38+
Serial.println(msg);
39+
40+
delay(100);
41+
}

examples/SyncReset/SyncReset.ino

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 <107-Arduino-Cyphal-Support.h>
13+
14+
/**************************************************************************************
15+
* SETUP/LOOP
16+
**************************************************************************************/
17+
18+
void setup()
19+
{
20+
Serial.begin(115200);
21+
while (!Serial) { }
22+
23+
Serial.println("Trigger sync reset in 5 seconds ...");
24+
cyphal::support::platform::reset_sync(std::chrono::milliseconds(5000));
25+
}
26+
27+
void loop()
28+
{
29+
30+
}

keywords.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,18 @@ KeyValueStorage_littlefs KEYWORD1
1919

2020
instance KEYWORD2
2121
value KEYWORD2
22+
2223
get KEYWORD2
2324
put KEYWORD2
2425
drop KEYWORD2
2526

27+
reset KEYWORD2
28+
reset_sync KEYWORD2
29+
reset_async KEYWORD2
30+
is_async_reset_pending KEYWORD2
31+
2632
#######################################
2733
# Constants (LITERAL1)
2834
#######################################
35+
36+
InvalidParam LITERAL1

src/107-Arduino-Cyphal-Support.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
* INCLUDE
1212
**************************************************************************************/
1313

14+
#include "reset/reset.h"
1415
#include "storage/storage.h"
1516
#include "uniqueid/uniqueid.h"

src/reset/reset-rp2040.cpp

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

src/reset/reset.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
#pragma once
9+
10+
/**************************************************************************************
11+
* INCLUDE
12+
**************************************************************************************/
13+
14+
#include <chrono>
15+
#include <optional>
16+
17+
/**************************************************************************************
18+
* NAMESPACE
19+
**************************************************************************************/
20+
21+
namespace cyphal::support::platform
22+
{
23+
24+
/**************************************************************************************
25+
* TYPEDEF
26+
**************************************************************************************/
27+
28+
enum class Error
29+
{
30+
InvalidParam,
31+
};
32+
33+
/**************************************************************************************
34+
* FUNCTION DECLARATION
35+
**************************************************************************************/
36+
37+
/* Immediately after calling this method a reset is performed. */
38+
std::optional<Error> reset()
39+
#if !defined(ARDUINO_ARCH_RP2040)
40+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
41+
#endif
42+
;
43+
44+
/* This method performs a reset in 'ms' milliseconds after its invocation,
45+
* blocking while waiting for the time to expire.
46+
*/
47+
std::optional<Error> reset_sync(std::chrono::milliseconds const ms)
48+
#if !defined(ARDUINO_ARCH_RP2040)
49+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
50+
#endif
51+
;
52+
53+
/* This method performs a reset in 'ms' milliseconds after its invocation,
54+
* but returns immediately after its invocation.
55+
*/
56+
std::optional<Error> reset_async(std::chrono::milliseconds const ms)
57+
#if !defined(ARDUINO_ARCH_RP2040)
58+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
59+
#endif
60+
;
61+
62+
/* Returns true if currently an async reset is pending. Since this
63+
* functionality is implemented via watchdog it is necessary to stop
64+
* regular watchdog feeding i.e. in the main loop.
65+
*/
66+
bool is_async_reset_pending()
67+
#if !defined(ARDUINO_ARCH_RP2040)
68+
__attribute__ ((error("Currently the reset API only supports ARDUINO_ARCH_RP2040.")))
69+
#endif
70+
;
71+
72+
/**************************************************************************************
73+
* NAMESPACE
74+
**************************************************************************************/
75+
76+
} /* cyphal::support::platform */

0 commit comments

Comments
 (0)