Skip to content

Commit e32c846

Browse files
authored
Adding UART wakeup (#183)
1 parent a074d2b commit e32c846

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,27 @@ S2 can only be woke up with 1 touch pad. It is recommended to do tests to find t
106106
Sleep.EnableWakeupByTouchPad(PadForSleep, thresholdCoefficient: 90);
107107
```
108108

109+
## UART wake up from light sleep mode
110+
111+
It is possible to use light sleep in any supported nanoFramework ESP32 with UART wake up. There are few elements to keep in mind:
112+
113+
* You **must** properly setup your COM port, in the following for COM2:
114+
115+
```csharp
116+
nanoFramework.Hardware.Esp32.Configuration.SetPinFunction(19, nanoFramework.Hardware.Esp32.DeviceFunction.COM2_TX);
117+
nanoFramework.Hardware.Esp32.Configuration.SetPinFunction(21, nanoFramework.Hardware.Esp32.DeviceFunction.COM2_RX);
118+
```
119+
120+
* Only COM1 and COM2 are supported. Note that by default COM1 is used for debug. Except if you've build your own version, you may not necessarily use it are a normal UART. But you can definitely use it to wake up your board. In that case, the pins are the default ones and the previous step is not necessary.
121+
* In order to wake up the board, you need to set a threshold on how many changes in the RX pin (the reception pin of the UART) is necessary. According to [the documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/sleep_modes.html#uart-wakeup-light-sleep-only), you will lose few characters. So the sender should take this into consideration in the protocol.
122+
123+
Usage is straight forward:
124+
125+
```csharp
126+
EnableWakeupByUart(WakeUpPort.COM2, 5);
127+
StartLightSleep();
128+
```
129+
109130
## Pulse Counter
110131

111132
Pulse counter allows to count pulses without having to setup a GPIO controller and events. It's a fast way to get count during a specific amount of time. This pulse counter allows as well to use 2 different pins and get a pulse count depending on their respective polarities.

nanoFramework.Hardware.Esp32/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
////////////////////////////////////////////////////////////////
1414
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.0.9.0")]
15+
[assembly: AssemblyNativeVersion("100.0.10.0")]
1616
////////////////////////////////////////////////////////////////
1717

1818
// Setting ComVisible to false makes the types in this assembly not visible

nanoFramework.Hardware.Esp32/Sleep.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,23 @@ public enum WakeupGpioPin : UInt64
175175
/// Gpio Pin 39 used for wakeup.
176176
/// </summary>
177177
Pin39 = (UInt64)1 << 39
178-
}
178+
}
179+
180+
/// <summary>
181+
/// UART port used for wakeup.
182+
/// </summary>
183+
public enum WakeUpPort
184+
{
185+
/// <summary>
186+
/// COM1 port.
187+
/// </summary>
188+
COM1 = 0,
189+
190+
/// <summary>
191+
/// COM2 port.
192+
/// </summary>
193+
COM2 = 1,
194+
}
179195

180196
/// <summary>
181197
/// Enable Wakeup by Timer.
@@ -228,6 +244,17 @@ public static EspNativeError EnableWakeupByTouchPad(int padNumber1, int padNumbe
228244
return NativeEnableWakeupByTouchPad(padNumber1, padNumber2, thresholdCoefficient);
229245
}
230246

247+
/// <summary>
248+
/// Enable wakeup by UART.
249+
/// </summary>
250+
/// <param name="portNumber">The port to use, only COM1 and COM2 are supported. Pins MUST be properly configured for COM2 for this to work.</param>
251+
/// <param name="edgeCount">The number of changes in the RX pin to wakeup the ESP.</param>
252+
/// <returns>Returns ESP32 native error enumeration.</returns>
253+
public static EspNativeError EnableWakeupByUart(WakeUpPort portNumber, int edgeCount)
254+
{
255+
return NativeEnableWakeupByUart(portNumber, edgeCount);
256+
}
257+
231258
/// <summary>
232259
/// Enter light sleep with the configured wakeup options.
233260
/// </summary>
@@ -299,6 +326,9 @@ public static int GetWakeupTouchpad()
299326

300327
[MethodImpl(MethodImplOptions.InternalCall)]
301328
private static extern EspNativeError NativeEnableWakeupByTouchPad(int padNumber1, int padNumber2, byte thresholdCoefficient);
329+
330+
[MethodImpl(MethodImplOptions.InternalCall)]
331+
private static extern EspNativeError NativeEnableWakeupByUart(WakeUpPort padNumber1, int edgeCount);
302332

303333
[MethodImpl(MethodImplOptions.InternalCall)]
304334
private static extern EspNativeError NativeStartLightSleep();

0 commit comments

Comments
 (0)