Code runs only when on debugger

What exactly does this patch do?
My code wont run unless a debugger is attached, ive erased the firmware and reuploaded still the same.

https://github.com/ghi-electronics/TinyCLR-Ports/issues/584

static void Patch()
        {
            // G30
            const int MODE_ADDRESS = 0x20001a68;
            const int EVENT_ADDRESS = 0x20002348;
            const int OFFSET = 0;

            var isUsbDebugMode = Marshal.ReadInt32(IntPtr.Add(new IntPtr(OFFSET), MODE_ADDRESS)) != 0 ? true : false;
            var value = Marshal.ReadInt32(IntPtr.Add(new IntPtr(OFFSET), (EVENT_ADDRESS))) | (isUsbDebugMode ? 512 : 256);
            Marshal.WriteInt32(IntPtr.Add(new IntPtr(OFFSET), EVENT_ADDRESS), value);
        }

you have to call that code once, right after MAIN in your application… and it writes a different value to a register that influences behaviour when not debugging

I am, it changed something on my G30, now my code does not run when not on debugger. Is there a way to reverse it?

sorry, just to be crystal clear…

Is it just “your app” or is it any app? IE you should deploy a blinky app and see if that then works as expected, first on debugger and then off.

Just tried it, its the same. Works on debugger, not when detached

what does “not working” actually mean? Does the LED blinky actually flash at all, once, or anything? Does it go reeeeeaal slow - like left it 10 mins and nothing flashed? How do you deduce it’s not working…

(btw I have no real world experience of using this patch, and no rig to go reproducing or troubleshooting - GHI will be along in the morning their time, which may not be long, knowing Gus)

Ive tried a blinky example with a button as well, the LED flashes and when I press the button another LED turns on then back off. This is all working when on debugger.

When I remove USB cord, unplug the G30 from power. Plug G30 again to power the code does not run, when I press button nothing happens. It might as well be 1 day on the power its not working.

I then plug the USB in, start TinyCLR config, connect device, press Reboot in the config tool, the G30 restarts and everything works fine again.

Is it now clear?

The program was prob throwing an exception when the debugger was not attached. wrap the code in a try block without re throwing the exception to see if that’s the culprit.

1 Like

I removed the patch code, still the same.

The G30 worked fine before you applied the patch? What version of the firmware was on the board when you ran it? Have you tried to erase the board and reflash the firmware from the GHI bootloader?

Finally found the problem, I have an open drain (active low) line connected to LDR1. (there were no more free pins)

What does this mean?

LDR1 is reserved for future use.

what happens when LDR1 is low on startup? can I fix this without disconnecting the line?
when the line is connected the code does not run unless on debugger

it being reserved for future use, and you using it, seems to be a big deal :slight_smile:

If you have no other pins, you’re stuffed. You really shouldn’t have used that pin… time to find another pin, re-route the connection, multiplex something somehow, or use an IO-expander…

If its reserved for future use, that means its not used yet. I thought id use it then. LOL
(should’ve been just reserved)

I still have the MODE pin free :joy:

But this one is pretty clear

Additionally, the communications interface between the host PC and the G30 is selected on startup through the MODE pin, which is pulled high on startup. The USB interface is selected when MODE is high and COM1 is selected when MODE is low.

Its on a connector to an outside device so I can disconnect it easily, ill figure something alternative then.

Asserting LDR1 will block the application from running on TinyCLR, useful if a bad application was loaded.

This is really good to know. Struggled with this myself recently. Thought “Reserved for Future Use” meant okay to use as GPIO (just like the other special pins) and will not affect startup.

I could be wrong, but IIRC you can release the line at any moment to have the app startup.