USB SerialEvent or ISR on Teensy 3.1?

Status
Not open for further replies.

greglink

Member
I'm trying to receive a data stream from a PC, and I'm used to doing it with a nice interrupt-based parser (originally written for MBed in C++). In a perfect world, I can simply port it over using SerialEvent or some other USB byte-received interrupt, however, forum searches show that as of late last year, it wasn't implemented. Has there been any progress on this end? I'm very comfortable with thread safety, critical sections, and such, and so don't mind the icky details.

Alternately, does anyone have a link to an easy-to-grab FreeRTOS-on-Teensy setup that I can start from? I could also just make a thread that polls once every few MS, and use that as a faux interrupt, but I hate to spend the hours setting up FreeRTOS myself if someone's got a nice setup example I can start from.
 
I've used the FreeRTOS that's uploaded on this forum - contributed. It works. But it won't solve your problem for event-driven serial reception because the Teensy 3 driver doesn't provide serial events. Teensy 2 has it.

You can easily make or use a cooperative scheduler (just a few lines of code) that will poll for received data is available.
 
Sadly, unless Paul has implemented SerialEvent in the past few weeks/months, it does seem as if I'll have to use a secondary process to periodically poll the USB input, and generate callbacks/messages from that periodic polling to simulate a proper interrupt. I'll be fine with the tiny bit of latency introduced from such a solution, it just seems very heavyweight (needing a full RTOS) to do something that might-should be simple with the system.
 
Note: unless I am missing something, SerialEvent is nothing special. That is looking at the implementation for Arduino 1.0.5 I see main has been changed to have:
Code:
int main(void)
{
	init();

#if defined(USBCON)
	USBDevice.attach();
#endif
	
	setup();
    
	for (;;) {
		loop();
		if (serialEventRun) serialEventRun();
	}
        
	return 0;
}
So serialEventRun is called each time in the main loop. And it just does:
Code:
void serialEventRun(void)
{
#ifdef serialEvent_implemented
  if (Serial.available()) serialEvent();
#endif
#ifdef serialEvent1_implemented
  if (Serial1.available()) serialEvent1();
#endif
#ifdef serialEvent2_implemented
  if (Serial2.available()) serialEvent2();
#endif
#ifdef serialEvent3_implemented
  if (Serial3.available()) serialEvent3();
#endif
}
it simply checks to see if a queue has something and if so calls the SerialEvent function. So you can just as easily update your function loop: to do
Code:
void loop() {
  if Serial.available()
    SerialEvent();
... rest of your code

If you really want the Serial data processed independently of the main loop, I would personally do what Steve mentioned and simply setup a timed task, that checks for input and processes it.

Kurt
 
Status
Not open for further replies.
Back
Top