Skip to content

Commit 25a89a4

Browse files
committed
fix reset pin for portenta machine control
1 parent 1f5ca5a commit 25a89a4

File tree

9 files changed

+364
-185
lines changed

9 files changed

+364
-185
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ The procedure:
3535
* `Arduino GIGA R1 WiFi`: short the pin 7 to GND until the led turns off
3636
* `Arduino Nano RP2040 Connect`: short the pin 2 to 3.3V until the led turns off
3737
* `Arduino Portenta H7`: short the pin 0 to GND until the led turns off
38+
* `Portenta Machine Control`: plug the device to a 24V power source, short the pin Digital Inputs 0 to 24VOUT until the led (`LED_BUILTIN`) turns off
3839
* Other boards: short the pin 2 to GND until the led turns off
39-
* `Portenta Machine Control`: currently the reset procedure is not available
4040

4141
### More on the reconfiguration pin
4242
Internally, the pin indicated in the procedure is set as `INPUT_PULLUP` (except for `Arduino Opta` ) and it's attached to an ISR fired on every change of the pin's status.
@@ -47,6 +47,10 @@ In order to be notified when the ISR is fired, it's possible to register a callb
4747
In order to change the default pin for resetting the board, it's possible to use the function `NetworkConfigurator.setReconfigurePin(your_pin)` specifying the new pin.
4848
The pin must be in the list of digital pins usable for interrupts. Please refer to the Arduino documentation for more details: https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/
4949

50+
N.B.: For `Portenta Machine Control`, the pin must be one of the Digital Inputs 0-7.
51+
The parameter must be one of the range DIN_READ_CH_PIN_00-DIN_READ_CH_PIN_07.
52+
The defines are available in the Portenta Machine Control library and you must include Arduino_PortentaMachineControl.h for having access to them.
53+
5054
### Disable the reconfiguration feature
5155
In order to disable the reconfiguration procedure, use this function in the sketch `NetworkConfigurator.setReconfigurePin(DISABLE_PIN)`
5256

examples/NetworkConfiguratorDemo/NetworkConfiguratorDemo.ino

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
* - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off
2323
* - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
2424
* - Portenta H7: short the pin 0 to GND until the led turns off
25-
* - Portenta Machine Control: the reset is not available
25+
* - Portenta Machine Control: plug the device to a 24V power source, short the pin Digital Inputs 0
26+
* to 24VOUT until the led (`LED_BUILTIN`) turns off
2627
* - Other boards: short the pin 2 to GND until the led turns off
2728
*
2829
* In this sketch the BLE and Serial interfaces are always enabled and ready for accepting
@@ -78,6 +79,10 @@ void setup() {
7879
* The pin must be in the list of digital pins usable for interrupts.
7980
* Please refer to the Arduino documentation for more details:
8081
* https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/
82+
* N.B.: For Portenta Machine Control, the pin must be one of the Digital Inputs 0-7.
83+
* The parameter must be one of the range DIN_READ_CH_PIN_00-DIN_READ_CH_PIN_07.
84+
* The defines are available in the Portenta Machine Control library and you must include
85+
* Arduino_PortentaMachineControl.h for having access to them.
8186
*/
8287
//NetworkConfigurator.setReconfigurePin(your_pin);
8388

src/Arduino_NetworkConfigurator.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ NetworkConfiguratorClass::NetworkConfiguratorClass(ConnectionHandler &connection
4141
_receivedEvent = NetworkConfiguratorEvents::NONE;
4242
_optionUpdateTimer.begin(NC_UPDATE_NETWORK_OPTIONS_TIMER_ms); //initialize the timer before calling begin
4343
_agentsManager = &AgentsManagerClass::getInstance();
44-
_resetInput = &ResetInput::getInstance();
44+
_resetInput = &ResetInputBase::getInstance();
4545
_ledFeedback = &LEDFeedbackClass::getInstance();
4646
}
4747

@@ -123,7 +123,8 @@ NetworkConfiguratorStates NetworkConfiguratorClass::update() {
123123
* - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off
124124
* - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
125125
* - Portenta H7: short the pin 0 to GND until the led turns off
126-
* - Portenta Machine Control: the reset is not available
126+
* - Portenta Machine Control: plug the device to a 24V power source, short the pin Digital Inputs 0
127+
* to 24VOUT until the led (`LED_BUILTIN`) turns off
127128
* - Other boards: short the pin 2 to GND until the led turns off
128129
*/
129130

@@ -210,7 +211,7 @@ void NetworkConfiguratorClass::setReconfigurePin(int pin) {
210211
_resetInput->setPin(pin);
211212
}
212213

213-
void NetworkConfiguratorClass::addReconfigurePinCallback(ResetInput::ResetInputCallback callback) {
214+
void NetworkConfiguratorClass::addReconfigurePinCallback(ResetInputBase::ResetInputCallback callback) {
214215
_resetInput->setPinChangedCallback(callback);
215216
}
216217

src/Arduino_NetworkConfigurator.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <connectionHandlerModels/settings.h>
1717
#include <Arduino_TimedAttempt.h>
1818
#include <Arduino_KVStore.h>
19-
#include "utility/ResetInput.h"
19+
#include "utility/ResetInputBase.h"
2020
#include "utility/LEDFeedback.h"
2121

2222
/**
@@ -59,7 +59,8 @@ enum class NetworkConfiguratorStates { ZERO_TOUCH_CONFIG,
5959
* - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off
6060
* - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off
6161
* - Portenta H7: short the pin 0 to GND until the led turns off
62-
* - Portenta Machine Control: the reset is not available
62+
* - Portenta Machine Control: plug the device to a 24V power source, short the pin Digital Inputs 0
63+
* to 24VOUT until the led (`LED_BUILTIN`) turns off
6364
* - Other boards: short the pin 2 to GND until the led turns off
6465
*
6566
*/
@@ -121,6 +122,10 @@ class NetworkConfiguratorClass {
121122
* The pin must be in the list of digital pins usable for interrupts.
122123
* Please refer to the Arduino documentation for more details:
123124
* https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/
125+
* N.B.: For Portenta Machine Control, the pin must be one of the Digital Inputs 0-7.
126+
* The parameter must be one of the range DIN_READ_CH_PIN_00-DIN_READ_CH_PIN_07.
127+
* The defines are available in the Portenta Machine Control library and you must include
128+
* Arduino_PortentaMachineControl.h for having access to them.
124129
*/
125130
void setReconfigurePin(int pin);
126131

@@ -129,7 +134,7 @@ class NetworkConfiguratorClass {
129134
* interrupt on the reconfiguration pin is fired.
130135
* @param callback Pointer to the callback function.
131136
*/
132-
void addReconfigurePinCallback(ResetInput::ResetInputCallback callback);
137+
void addReconfigurePinCallback(ResetInputBase::ResetInputCallback callback);
133138

134139
/**
135140
* @brief Checks if a specific configuration agent is enabled.
@@ -165,7 +170,7 @@ class NetworkConfiguratorClass {
165170
static inline models::NetworkSetting _networkSetting;
166171
bool _connectionHandlerIstantiated;
167172
bool _configInProgress;
168-
ResetInput *_resetInput;
173+
ResetInputBase *_resetInput;
169174
LEDFeedbackClass *_ledFeedback;
170175
/* Timeout instances */
171176
// Timeout for connection attempt

src/utility/ResetInput.cpp

Lines changed: 0 additions & 121 deletions
This file was deleted.

src/utility/ResetInput.h

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,82 @@
88

99
#pragma once
1010

11-
#include "Arduino.h"
12-
#include <functional>
11+
#include "ANetworkConfigurator_Config.h"
12+
#include "ResetInputBase.h"
13+
#include "LEDFeedback.h"
1314

14-
#define DISABLE_PIN -1
15-
16-
/**
17-
* @class ResetInput
18-
* @brief A singleton class to handle input of the reset functionality with interrupt-based monitoring.
19-
*
20-
* This class provides methods to configure and monitor a reset input pin. It allows
21-
* setting up a custom callback function to be executed when the pin status changes.
22-
*/
23-
class ResetInput{
15+
class ResetInput : public ResetInputBase {
2416
public:
25-
/**
26-
* @typedef ResetInput::ResetInputCallback
27-
* @brief A type definition for the callback function to be executed on pin status change.
28-
*/
29-
typedef std::function<void()> ResetInputCallback;
30-
/**
31-
* @brief Get the singleton instance of the ResetInput class.
32-
* @return A reference to the ResetInput instance.
33-
*/
34-
static ResetInput& getInstance();
35-
/**
36-
* @brief Initialize the reset input by setting up the interrupt pin.
37-
*/
38-
void begin();
39-
/**
40-
* @brief Check if the reset event has been fired.
41-
* @return True if the event is fired, otherwise false.
42-
*/
43-
bool isEventFired();
44-
/**
45-
* @brief Set a custom callback function to be called when the pin status changes.
46-
* This function must be called before invoking the `begin` method.
47-
* @param callback The custom callback function to be executed.
48-
*/
49-
void setPinChangedCallback(ResetInputCallback callback);
50-
/**
51-
* @brief Set the pin to be monitored for reset events.
52-
* By default, the pin is set as INPUT_PULLUP.
53-
* Use the value DISABLE_PIN to disable the pin and the reset procedure.
54-
* This function must be called before invoking the `begin` method.
55-
* @param pin The pin number to be monitored. The pin must
56-
* be in the list of digital pins usable for interrupts.
57-
* Please refer to the Arduino documentation for more details:
58-
* https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/
59-
*/
60-
void setPin(int pin);
17+
static ResetInput &getInstance() {
18+
static ResetInput instance;
19+
return instance;
20+
}
21+
22+
void begin() override {
23+
if(_pin == DISABLE_PIN){
24+
return;
25+
}
26+
#ifdef ARDUINO_OPTA
27+
pinMode(_pin, INPUT);
28+
#else
29+
pinMode(_pin, INPUT_PULLUP);
30+
#endif
31+
pinMode(LED_RECONFIGURE, OUTPUT);
32+
digitalWrite(LED_RECONFIGURE, LED_OFF);
33+
attachInterrupt(digitalPinToInterrupt(_pin),_pressedCallback, CHANGE);
34+
}
35+
36+
bool isEventFired() override {
37+
if(_startPressed != 0){
38+
#if defined(ARDUINO_NANO_RP2040_CONNECT) || defined(ARDUINO_SAMD_MKRWIFI1010)
39+
LEDFeedbackClass::getInstance().stop();
40+
#endif
41+
if(micros() - _startPressed > RESET_HOLD_TIME){
42+
digitalWrite(LED_RECONFIGURE, LED_OFF);
43+
_expired = true;
44+
}
45+
}
46+
47+
return _fireEvent;
48+
}
49+
6150
private:
62-
/**
63-
* @brief Private constructor to enforce the singleton pattern.
64-
*/
65-
ResetInput();
66-
static inline ResetInputCallback _pressedCustomCallback;
67-
static inline int _pin;
51+
ResetInput() {
52+
_pin = PIN_RECONFIGURE;
53+
_expired = false;
54+
_startPressed = 0;
55+
_fireEvent = false;
56+
}
6857
static inline volatile bool _expired;
6958
static inline volatile bool _fireEvent;
7059
static inline volatile uint32_t _startPressed;
7160
/**
7261
* @brief Internal callback function to handle pin press events.
7362
*/
74-
static void _pressedCallback();
63+
static void _pressedCallback() {
64+
#if defined(ARDUINO_NANO_RP2040_CONNECT)
65+
if(digitalRead(_pin) == HIGH){
66+
#else
67+
if(digitalRead(_pin) == LOW){
68+
#endif
69+
#if !defined(ARDUINO_NANO_RP2040_CONNECT) && !defined(ARDUINO_SAMD_MKRWIFI1010)
70+
LEDFeedbackClass::getInstance().stop();
71+
#endif
72+
_startPressed = micros();
73+
digitalWrite(LED_RECONFIGURE, LED_ON);
74+
} else {
75+
digitalWrite(LED_RECONFIGURE, LED_OFF);
76+
if(_startPressed != 0 && _expired){
77+
_fireEvent = true;
78+
}else{
79+
LEDFeedbackClass::getInstance().restart();
80+
}
81+
_startPressed = 0;
82+
}
83+
84+
if (_pressedCustomCallback) {
85+
_pressedCustomCallback();
86+
}
87+
}
7588
};
89+

0 commit comments

Comments
 (0)