}
}
+proc power_restore {} {
+ puts "Sensed power restore."
+ reset init
+}
+
+add_help_text power_restore "Overridable procedure run when power restore is detected. Runs 'reset init' by default."
+
+proc power_dropout {} {
+ puts "Sensed power dropout."
+}
+
+proc srst_deasserted {} {
+ puts "Sensed nSRST deasserted."
+ reset init
+}
+add_help_text srst_deasserted "Overridable procedure run when srst deassert is detected. Runs 'reset init' by default."
+
+proc srst_asserted {} {
+ puts "Sensed nSRST asserted."
+}
static int powerDropout;
static int srstAsserted;
+static int runPowerRestore;
+static int runPowerDropout;
+static int runSrstAsserted;
+static int runSrstDeasserted;
+
static int sense_handler()
{
static int prevSrstAsserted = 0;
powerRestored = prevPowerdropout && !powerDropout;
if (powerRestored)
{
- LOG_USER("Sensed power restore.");
+ runPowerRestore = 1;
}
long long current = timeval_ms();
int waitMore = lastPower + 2000 > current;
if (powerDropout && !waitMore)
{
- LOG_USER("Sensed power dropout.");
+ runPowerDropout = 1;
lastPower = current;
}
waitMore = lastSrst + 2000 > current;
if (srstDeasserted && !waitMore)
{
- LOG_USER("Sensed nSRST deasserted");
+ runSrstDeasserted = 1;
lastSrst = current;
}
if (!prevSrstAsserted && srstAsserted)
{
- LOG_USER("Sensed nSRST asserted");
+ runSrstAsserted = 1;
}
prevSrstAsserted = srstAsserted;
int handle_target(void *priv)
{
int retval = ERROR_OK;
+
+ /* we do not want to recurse here... */
+ static int recursive = 0;
+ if (! recursive)
+ {
+ recursive = 1;
+ sense_handler();
+ /* danger! running these procedures can trigger srst assertions and power dropouts.
+ * We need to avoid an infinite loop/recursion here and we do that by
+ * clearing the flags after running these events.
+ */
+ int did_something = 0;
+ if (runSrstAsserted)
+ {
+ Jim_Eval( interp, "srst_asserted");
+ did_something = 1;
+ }
+ if (runSrstDeasserted)
+ {
+ Jim_Eval( interp, "srst_deasserted");
+ did_something = 1;
+ }
+ if (runPowerDropout)
+ {
+ Jim_Eval( interp, "power_dropout");
+ did_something = 1;
+ }
+ if (runPowerRestore)
+ {
+ Jim_Eval( interp, "power_restore");
+ did_something = 1;
+ }
+
+ if (did_something)
+ {
+ /* clear detect flags */
+ sense_handler();
+ }
+
+ /* clear action flags */
+
+ runSrstAsserted=0;
+ runSrstDeasserted=0;
+ runPowerRestore=0;
+ runPowerDropout=0;
+
+ recursive = 0;
+ }
+
target_t *target = all_targets;
while (target)
{
- sense_handler();
/* only poll target if we've got power and srst isn't asserted */
if (target_continous_poll&&!powerDropout&&!srstAsserted)
target = target->next;
}
+
return retval;
}