Help with USB Slave usage

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
hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Help with USB Slave usage

Post by hyperion007 »

So the object of this project is to send a text as String from the PC (VB application) to the PIC18F4550 and then return one byte

The VB application sends to the PIC every 1/100th second

The return byte value of 123 is sent every time from the PIC to the VB app, but I can't get the text String to print on the LCD.


I have attached the VB files and the FC file.


If anyone would have a look that would be great. Maybe you can't send text as String using the get string component macro?


Thanks in advance.

VB files: http://www.level6.se/SpotifyEF-Project_007-7.rar (Too large to attach)
Attachments
USB Slave Demo 3-Byte.fcf
(11.5 KiB) Downloaded 248 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

At the very top of your service_macro delete the icon that calls the get_string function.

The macro is already called with the string variable populated with valid data so your current get_string macro call will be overwriting the valid data.

Let me know how you get on.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Thanks Ben,

I did as you said.

The results are that the first two characters on the display changes when I change the track in spotify but the characters are symbols etc.
Also the LCD clear macro doesn't do anything so it will always say **B STARTED

I changed the string size to 20 but that didn't do any difference.
Attachments
USB Slave Demo 3-Byte.fcf
(11.5 KiB) Downloaded 246 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

Maybe try the LCD on another port then PortA as there is sometimes issues with PortA on the specific device your using.

PORTB or PORTD should work better.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Ok I will try that when I get home on Saturday.

If you don't mind, I would like to hear about the issues with PIC18F4550 regarding PortA. I had plans to use PortA for the display since I am using the other ports for other things (not in the program attached here)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

I think there may be a bug with the analogue channels and comparator functionality on start up with means that one or more of the PORTA pins do not behave correctly. I will see if I can dig out the information on the workaround to allow this to work correctly.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

I now have the LCD connected on port D instead but it made no difference.

Still only the first two characters change on the display.

Since I didn't do the VB programming and don't know much about VB it might be completely wrong.
I think the VB program just keeps sending the string value from spotify.nowplaying() over and over again every 1/100th second and at the same time gets the send byte value from the PIC.

The send byte works. I get "123" every 1/100th second into the VB program.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

Looking at the VB I think the ECIO_Transmit function may need a byte array rather then a string as I don't think the VB system is flexible with these two data types like Flowcode is.

Can you convert the string variable to a byte array variable before calling the transmit function.

This page may help to show you how.
http://msdn.microsoft.com/en-us/library/ms172828.aspx

Let me know how your getting on.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Thanks Ben!

I'll ask the guy who is helping me with the VB stuff :)

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Can someone have a look at this latest revision?

I just can't seem to iron out why it isn't working.

I have attached both the fcf file and the Visual Studio Express 2012 files

When running this test application, with spotify running, the second line of the display reads first two random characters and then "Started" like in "USB Started"

The first line says USB Starting and then USB Started and stays like that, but the second line blinks "XX Started" everytime the VB application sends a byte to the PIC.
Attachments
USB Slave Demo 3-Byte.fcf
(11.5 KiB) Downloaded 221 times
Excercise6.zip
(37.75 KiB) Downloaded 248 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

It looks like your still passing a string as the argument for the ECIO function. This needs to be a pointer to the first location of a byte array, a string variable type in Windows is a structure of data plus extra's that will not be processed correctly. Here is a known working VB program which passes byte arrays.
Attachments
USB Scope.zip
(280.92 KiB) Downloaded 235 times

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Ok we got the string issue sorted.

Now I have some more questions :)

Since the string length will be variable (artist - title) is there a way to set the variable USB_IN[80] for example to USB_IN[.length] if I have length=length$(USB_IN) ?

I want to scroll the string on the display but I don't want to scroll [80] positions if the (artist - title) is only say 20 characters long


Also, I don't really know what the best practice is ragarding the USB Slave component. What I mean is, as it is now, we send the string(artist - title) every 1/100th second and get a reply back with a byte value of 0 if no button is pressed on the remote and other values depending on what button is pressed.

The problem is that since we are sending stuff to the PIC all the time, it doesn't seem to ever really get out of the USB interrupt.

Best case would be if we only needed to send to the pic when the NowPlaying string changes, and that the PIC could initialize a send when a button is pressed on the remote. It seems unnecessary to have to send the same string over and over just to poll if any button has been pressed on the remote.

How would you suggest I go about this? Maybe I should have used USB HID instead?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

There is a string manipulation "length" function which will give you the size of the data in the string as it is received by the slave service macro. You can then use this size to control how the text is displayed.

Could you reduce the external switch polling e.g. down to maybe 10/20/30 times a second and see if this gives you any more overhead to do things.

USB HID is based on a constant tick so you would again have to be constantly sending data. The USB serial may be a better option as you can simply send and receive data as it happens as if it were a standard RS232 COM port.

As you already have the slave working for now why not transmit an empty string if the artist has remained the same and this way based on the string length you should be able to tell if the data is a switch poll or an artist / song update.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Thanks, that might just work. I don't want to go down the USB serial route as I have had some issues with that and it actually caused my first ever blue screens on my win7 machine.

I'll ask the guy helping me with the VB stuff to implement an empty string when nothing has changed.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

The problem is that I want a really responsive system.

For example if I press a button on the remote, I don't want a noticeable delay between the button being pressed and the track changing on the PC.

But maybe that is too much to ask of the PIC?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

100 transactions a second is not too bad and as you say its much more responsive. Did you try the string length idea?
it actually caused my first ever blue screens on my win7 machine.
Ok I'm not aware of any problems with the USB serial component as I use it quite a lot for my personal projects, can you tell me what PC side software you were using that may have caused the bluescreen?

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

I think it was just the windows 7 machine that didn't like me just unplugging and then plugging in the PIC to the USB port over and over again :)

but do you think the PIC is fast enough to do other things than just in and out of the service macro for USB slave if I keep sending (polling) 100 times a second?

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

I am trying to read a string from USB and display it on the LCD display and have it scroll because the string is to long to display in it's entirety.
The problem I have is that this string is of unknown length.

Is there a way to change the length of the String variable on the fly?

I am reading the length of the string and putting the value in a variable and then scrolling the display that amount of steps.

I want this string to scroll over and over, but I also want the PIC to do other things at the same time? but I have no idea if that is even possible. A multitasking PIC would have been nice to have :)

Another question I have about the USB Slave component: If I send one string, it works. The string is displayed and scrolled across the display, then it goes blank for a very long time and then it displays some text again for a while but then goes blank again. I want it to "start over" as soon as I send a new string.

Is there a way to scroll a string on the display without using a loop and still get control over the speed etc? Or a way of breaking the loop with an interrupt so that if there is a new string coming through the USB it breaks the loop and begins at the top of the USB service macro?
Attachments
USB Slave Demo 3-Byte.fcf
(13.63 KiB) Downloaded 142 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

It sounds like you may be using fixed delays in your program. One way I would tackle it would be to setup a timer interrupt before enabling the slave service. In the timer interrupt you simply display a string and do the nice scrolling you are talking about. Then the slave service can update the contents of the string and maybe reset the animation by clearing the timer's position variable.

I use the USB serial on my CNC so there are all kinds of macros and timer events running in the background and it copes very nicely.
A multitasking PIC would have been nice to have :)
There is a multitasking library available for PIC but multitasking is not as nice as you are imagining. There are all kinds of issues when trying to synchronize the threads and pass data around and essentially it all has to come down and be run by the single core anyway making it effectively pointless. Ladder logic works this way and it can be a nightmare trying to work out exactly what will happen next. Single threaded is much much easier! The propellor range of microcontrollers look interesting as I think they have 7 processor cores all connected together to the same memory busses, maybe one day I will buy one and have a play.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Ok I'll give that a try and see how I fare with it.

Another followup question

The program doesn't seem to return to main at the end of the service macro?

I tested this now by putting a loop in main after the Run_Slave_Service macro, that should print "Hello?" on line 0 of the display but nothing.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

I had overlooked this: Run_Service_Macro Stalls the microcontroller and waits for an incoming message from the USB

Now what? Should I try to use the CheckRx then Get_String instead?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Hello,

This information is taken from the help file.

Run_Slave_Service()
Stalls the microcontroller and waits for an incoming message from the USB, when a message is received the macro defined in the USB Slave properties is run. While the macro is running you can choose to send data back to the PC if you wish.

Stop_Slave_Service()
Allows the microcontroller to leave the stalled slave mode and resume its normal independant functionality.

Basically the run slave service macro creates a loop that will sit and wait for a valid transaction from the PC. When this is received the service macro is called. When the service macro is finished the program returns to the loop to wait for the next valid message. You can run the USB slave without the slave service running but I suggest you try using a timer interrupt first and see how you get on.

In the main Flowcode v5 examples download there is a "USB Slave - no service" demo if you want to see how the program is built up without the slave service running.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Yet another question:

any recommendation on how to send information from PIC to PC over USB without having to constantly poll the PIC from the PC which seems to keep the PIC so busy it can't do other stuff? Is USB Serial the only option then?

I did try to put a stop_slave_service at the end of the slave_service macro and then at the end of the loop in main I put a start_slave_service but since I send on the USB once every 1/100 second it doesn't seem to work like I want it to.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Help with USB Slave usage

Post by Benj »

Offered a solution for your issue here.
http://www.matrixmultimedia.com/mmforum ... =7&t=12236

Let me know how you get on.

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Help with USB Slave usage

Post by hyperion007 »

Ok I got most of it figured out but the USB slave send byte isn't sending anything? I got it working before but now I can't figure out why the VB application isn't seeing the byte I am sending.
I opted to use no service macro.

I put the SendByte macro at the end of the main loop.
Attachments
USB_Slave_Demo_3-Byte_test1.fcf
(45.59 KiB) Downloaded 131 times

Post Reply