Skip to content

Commit

Permalink
make console override code optional + enable for BANGLEJS
Browse files Browse the repository at this point in the history
fix typos/comments
  • Loading branch information
fanoush committed Aug 9, 2024
1 parent 205a5df commit c60f49c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
27 changes: 18 additions & 9 deletions libs/swdcon/jswrap_swdcon.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
Expand Down Expand Up @@ -59,7 +61,7 @@ static uint8_t srvMode = MODE_OFF; ///< current mode
"ifdef" : "USE_SWDCON"
}
In memory serial I/O device accessible via SWD debugger.
uses SEGGER RTT so it can be used with openocd and other SEGGER compatible tools.
Uses SEGGER RTT so it can be used with openocd and other SEGGER compatible tools.
*/


Expand Down Expand Up @@ -102,35 +104,39 @@ bool jswrap_swdcon_idle(void) {
bool active = false;
active |= swdconRecv();
// prevent sleep if SWD console is active, we probably have power attached anyway
active |= (jsiGetConsoleDevice() == EV_SWDCON);
// sleeping would work if SWD debugger would trigger interrupt after sending data (it does not)
// however it is possible to trigger interrupt in our own SWD RTT host in future
// e.g. triggering TIMER1 interrupt vie writing STIR register wakes nrf52 espruino device
active |= (jsiGetConsoleDevice() == EV_SWDCON);
return active;
}

//===== Internal functions
bool wasForced=false;
IOEventFlags oldConsole = EV_NONE;
#ifdef BANGLEJS
// on Bangle 2 looks like console is always forced to something (bluetooth or null)
// setting to non-programmable forces console to null in .boot0
// allow to override this case
#define shouldOverride (console == EV_NONE)
#define OVERRIDE_CONDITION (console == EV_NONE)
bool wasForced=false;
IOEventFlags oldConsole = EV_NONE;
#endif

bool swdconActivate() {
// if the console is not already us, then change it
// if current console is not already us, then change it
IOEventFlags console = jsiGetConsoleDevice();
if (console != EV_SWDCON) {
if (!jsiIsConsoleDeviceForced()) {
jsiSetConsoleDevice(EV_SWDCON, false);
//jshHadEvent();
#ifdef OVERRIDE_CONDITION
wasForced=false;
} else {
if (shouldOverride){
if (OVERRIDE_CONDITION){
wasForced=true;
oldConsole=console;
jsiSetConsoleDevice(EV_SWDCON, true);
}
#endif
}
}
return true;
Expand All @@ -141,10 +147,13 @@ void swdconRelease() {
IOEventFlags console = jsiGetConsoleDevice();
// only switch away if the current console is us
if (console == EV_SWDCON){
#ifdef OVERRIDE_CONDITION
if (wasForced){
jsiSetConsoleDevice(oldConsole, true); // restore previos forced one back
jsiSetConsoleDevice(oldConsole, true); // restore previous forced one back
wasForced=false;
} else {
} else
#endif
{
jsiSetConsoleDevice(jsiGetPreferredConsoleDevice(), false);
}
}
Expand Down
9 changes: 2 additions & 7 deletions libs/swdcon/jswrap_swdcon.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Thorsten von Eicken
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Contains JavaScript HTTP Functions
* Contains JavaScript SWD console
* ----------------------------------------------------------------------------
*/
#ifndef LIBS_SWDCON_H_
#define LIBS_SWDCON_H_

#include "jsvar.h"

void jswrap_swdcon_setOptions(JsVar *options);

void jswrap_swdcon_init(void);
void jswrap_swdcon_kill(void);

// Listen, accept, send, and recv on telnet console connections. Returns true if something
// was done.
bool jswrap_swdcon_idle(void);

#endif

0 comments on commit c60f49c

Please sign in to comment.