USB Serial Initialize

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 5.
To post in this forum you must have a registered copy of Flowcode 5 or higher.

Moderator: Benj

Post Reply
Rich
Posts: 4
Joined: Tue Jun 12, 2012 10:27 pm

USB Serial Initialize

Post by Rich »

I am attempting to create a program that will auto execute if the USB is not connected. The program use USB_Serial_Initialize as the first step. This function does not seem however to have a timeout. The program locks until the USB port is attached. I tried

FCV_COMM_ON = 255 ;
try
{FCV_COMM_ON = FCD_USBSerial0_Initialise_Serial();
}
but the compiler does not like that I changed it to
FCV_COMM_ON = 255 ;
try:{FCV_COMM_ON = FCD_USBSerial0_Initialise_Serial();}
But the effect is the same.

If the serial port is attached everything works fine, if not the firmware will not run. Is there anyway to apply a timeout or an error trap that will allow the firmware to run when the USB is not attached.

Thanks for your help.
Rich

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: USB Serial Initialize

Post by Spanish_dude »

You will need to customize the initialize function.
Anyways, what version of flowcode are you using ?

Nicolas

Rich
Posts: 4
Joined: Tue Jun 12, 2012 10:27 pm

Re: USB Serial Initialize

Post by Rich »

Sorry, Guess that would be helpful. I am using Flowcode 5.2 Professional.

Rich

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: USB Serial Initialize

Post by Spanish_dude »

If you have Flowcode v5 I'd suggest you to register to the Flowcode v5 forum.
http://www.matrixmultimedia.com/mmforum ... =46&t=9958

I could take a look at how the initialize function works, maybe I can do something (but I doubt it)

Cheers,

Nicolas

EDIT:
Benj, feel free to correct me :mrgreen: .
So it seems like there's a loop in this function that waits for a variable to be set.

Code: Select all

	#else
	  #ifdef FC_CAL_PIC														//8-bit PIC specific
		while(CDC_USB_status == 0);
	  #else																	//16-bit PIC specific
	  	while(USBDeviceState != CONFIGURED_STATE);
	  #endif
What you could try to do is add something like this:

Code: Select all

unsigned int _modcounter = 0; // 'mod' so you know it's a modified version of the usb initialise function
	#else
	  #ifdef FC_CAL_PIC														//8-bit PIC specific
		while(CDC_USB_status == 0 && _modcounter++ < 1000)
			delay_ms(1);
	  #else																	//16-bit PIC specific
	  	while(USBDeviceState != CONFIGURED_STATE  && _modcounter++ < 1000)
			delay_ms(1);
	  #endif
If nothing else blocks the program or if there's no interrupt being used for the USB then this is ok to do. I set it to 1 sec timeout.
If there is an interrupt being used for setting the CDC_USB_status or USBDeviceState, and I suppose there is, then it would be best to disable it first.
I don't know which one it is but I think there's an (hardware) USB interrupt, not sure.

Btw, to see the C code of your component click on it in the 'Panel' and then on 'Custom Code ...' to the right side of the screen, select the function and click on 'Edit Code'.
A window will open, stretch it out a bit so it's easier to see and there you have it, the C code of the selected function.

Cheers

Rich
Posts: 4
Joined: Tue Jun 12, 2012 10:27 pm

Re: USB Serial Initialize

Post by Rich »

Thanks, I tried that but it did not work.

The firmware enters the initialize function (I have it turn on an LED entering the call, then turn it off when it exits) , It never exits the call. If I plug in the USB port it, enter then exits the call in a matter of a second or two.

Any other thoughts?

Rich

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: USB Serial Initialize

Post by Spanish_dude »

Well, if that didn't do it I don't really know what would work.
There are a couple of functions at the start of the usb initialize function, but I don't think I can access these.
It's probably the USBDeviceAttach function, I think that's the one who waits for a device to get connected.

Probably a bad idea, but you could enable an interrupt and force the exit of the usb initialize function after a while.
I won't recommend it though because if the stack is not managed correctly you can easily have a stack overflow, plus you'll get some complicated stuff to code.

Here's what you'd need to do in order to force the exit of a function.
  • enable timer interrupt
  • make a copy of every register (WREG, STATUS and stuff like that), the stack and the stack pointer(very important)
  • call usb initialize (just like you're doing now in your program)
  • when the timer interrupt is called, let's say after 1 sec after entering the usb initialize function :
    • write all the registers back with the saved values from before and fill the stack back in with the saved stack, don't forget the stack pointer
    • add the address right after the usb initialize function to the stack
    • disable all interrupts
    • exit interrupt, it should jump right after the usb initialize function
(Now you know why I don't recommend it :P )

Cheers,

Nicolas

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: USB Serial Initialize

Post by medelec35 »

I have moved this topic to the v5 section for you. Please ensure you sign up for this v5 topic and post your v5 related questions
Martin

Post Reply