Host Initiated Reset for Teensy2++ (Visual C#/Win7 environment)

Status
Not open for further replies.

dreschel

Member
“In the latest "experimental" 0.9 Teensyduino code, I have switched Windows and Linux support to use 134 baud setting instead of the break signal, and the reset-on-break behavior has been removed.

This should allow Teensy to work with Winbatch, or any other programs that unexpectedly sends the break signal. Just don't set the baud rate to 134. On Teensy, the data transfer is always native 12 Mbit/sec with native flow control, no matter what the baud rate setting in software.” (quote from Paul online...)


Hi All,
I have a large need of being able to do a normal reset (“The normal reset causes your program (at location zero) to run.”) of a Teensy 2.0++ from a C# program running on a Windows 7 PC. With original style Arduino’s I could toggle DTR under PC software control to force a reset and then wait til the bootloader had adequate time to quit and begin executing my Arduino program.

My goal is to generate a software routine on the host PC side (talking to a Teensyduino program on Teensy 2.0++) to force a restart of the Teensyduino program. For instance, if some event locks up the Teensyduino code, I want the host to be able to force it to restart. Or if the host needs to force the Teensy into a known state. (Again, like the DTR toggle trick on a regular Arduino.)

I used the 134 baud setting switch and am able to get the Teensy 2.0++ to switch from being a serial port to a USB HID device (I can see it occur in Device Manager in Windows 7). I am doing this using Visual Studio C#.NET Express. Now the problem comes in when I am trying to turn HalfKay off and reboot my Teensyduino program. (http://www.pjrc.com/teensy/halfkay_protocol.html)

In Windows land I am using LIBUSBDOTNET ( http://sourceforge.net/projects/libusbdotnet/) to try to communicate with the HID and generate a usb_control_msg to reboot from HalfKay, via your pjrc.com documentation. I am able to open the HID but only after creating a fake libusb-win32 filter driver (http://sourceforge.net/apps/phpbb/libusbdotnet/viewtopic.php?f=3&t=46)... I still have not been able to get HalfKay to respond to the control transfer I am trying to send and my guess is that it is due to the fake filter driver...

So, two thoughts:
Could someone create another baud setting switch that allows the teensyduino program to be restarted? (I am not sure how to approach this... any suggestions would be awesome.) So my C# program sets the baud rate to, say, 135, waits a few hundred milliseconds and then can expect (or check) that the Teensyduino program to be restarted... This would be a major help for me and I think other users.

The other thought: Does anyone have a piece of code running under Windows, Visual Studio would be great, that shows how to use the 134 baud rate switch, then do the HalfKay reset to the user program?

This has been a major time sucker for me in the last week with no forward progress. Any help or suggestions would be appreciated.
Bill
 
How the Arduino Micro resets

http://arduino.cc/en/Main/arduinoBoardMicro

The Arduino Micro is a Teensy-like board (unused pins, bah!) but note how it is capable of a software reset to user program using much the same mechanism as Paul did to reset to HalfKay, setting the serial baud rate. Even though it takes 8 seconds to get there...

More experimentation leads me to believe my attempt to write to HalfKay with a C# program to reset it, is being blocked by my selection of Windows INF file: HID_Compliant_Device & USB_Input_Device
I think since Teensy2.0++ is being seen as a USB Input Device by Win7, I am blocked from writing to it.

So, the C# code that turns HalfKay on:

this.serialPort1.PortName = COMport; // Replace with your COM port!
serialPort1.BaudRate = 134;
this.serialPort1.Open();
this.serialPort1.Close();

MessageBox.Show("Wait for HID...");

Within a second, the serial port disappears from Device Manager and the HID device appears.
I can see the HID device using:

// Find and open the usb device.
UsbDeviceFinder MyUsbFinder = new UsbDeviceFinder(0x16C0, 0x0478);
UsbDevice MyUSBDevice;
MyUSBDevice = UsbDevice.OpenUsbDevice(MyUsbFinder);
if (MyUSBDevice == null) MessageBox.Show("Device Not Found.");

Then, according to http://www.pjrc.com/teensy/halfkay_protocol.html
to reset HalfKay to get to the user program, we need to do a control transfer to Teensy.
I can't tell you how may variations of Control Transfers I have tried.
Here is one attempt using LibUsbDotNet to emulate Paul's code on the website above:

int lengthTransferred;
byte[] buf = new byte[256];
buf[0] = 0xFF;
buf[1] = 0xFF;
UsbSetupPacket cmd = new UsbSetupPacket(0x21, 9, 0x200, 0xFF, 130);
bool bSuccess = MyUSBDevice.ControlTransfer(ref cmd, buf, 130, out lengthTransferred);
MessageBox.Show("Wait for Serial Port... " + bSuccess.ToString() + " " + lengthTransferred.ToString());

Whenever I set up the RequestType to 0xA1 (reading from the HID) I get bSuccess = TRUE. Whenever I set it to 0x21, I get bSuccess = FALSE.
The HID device never goes away, the Serial Port never returns in Device Manager.

Any suggestions would be greatly appreciated...
 
Status
Not open for further replies.
Back
Top