|
|
View previous topic :: View next topic |
Author |
Message |
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
4550 USB woes |
Posted: Tue Oct 25, 2011 7:08 pm |
|
|
Ok, struggling again with USB..
While I've got the ex_usb_serial.c example to work, as well as several variations, I've run into a 'snag' with my own program (a simple data logger).
In a nutshell, I read some data and send it to the PC via USB. Works fine,every second,correct data appears onto the PC screen.
However, I'd like some 'mechanism' to stop sending data if the PC monitoring program is shutdown.
I thought this ...
Code: |
if (usb_enumerated()) {
send_data_to_usbPC_1(); // send all data to PC via USB port
}
|
would control the flow of data,but the program halts.This code came from example #2. When the PC program is restarted, the PIC program merrily sends data to the PC.
send_data_to usbPC_1() is just a fprintf(....) function and performs as expected.
Any ideas how to fix this quirk? I know it's this one line of 8253 lines of code as commenting it out, cures the hangup....
Thanks
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Oct 26, 2011 2:12 am |
|
|
A number of separate things:
First, have you got the connection sense wired up?.
Second, is the PC shutting down completely, or 'suspending'?.
Then, what port are you using on the PC?.
The first major problem is that a PC and USB, is not a simple 'on/off' setup. On the PC, some USB ports (normally the two closest to the old keyboard/mouse connectors if present), are designed for devices like mice, that will support 'wake up' from a suspend state. Now, when a PC goes into a suspended state, the USB, is still running on these ports.
So, the first things to do, is to make sure you are not using such a port. Easiest way to test, is to plug a mouse onto each port in turn, and see on which ones the LED goes 'off', when the machine suspends.
Then the second problem depends on how your USB code is handled. The status reflecting 'enumerated', only gets updated in two situations. First a packet from the PC, or second when the setup is 'polled'. So you need to call 'usb_task' at regular intervals round the loop, to get this to update. The simplest basic setup, is:
Code: |
//First, in main, to initialise the USB, use:
usb_init_cs();
//rather than the usb_init
//Then in your main code 'loop' have:
usb_task();
//provided you have the connection sense pin wired, this will then cause
//a 'detach' operation to be called if the port goes off.
//It _should_ update, without the sense pin, but this depends on the
//PC driver sending a disconnect when the PC stops. No guarantee on this...
|
So, if you are shutting down, rather than suspending, the PC driver is not sending a disconnect, and you need to add the connection sense.
If you are suspending, try to find the ports that do go off.
Finally make sure you call usb_task before your test.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Oct 26, 2011 8:52 am |
|
|
answers...
1) yes, connection sense is wired up,using Port RE3 ( pin 1) using (2) 100K as shown in usb example. Power is from USB port of PC,so this pin will always be high.
2)PC is not shutdown,or suspended, etc. I just open Realterm( terminal program) to see data from PIC.X to close(exit) the program.
3)The USB port is shown as COM4. Verified several times, and it works fine using the ex_usb_serial.c program.
4) usb_task(); is at the first line in my MAIN.
Will 'cut and hack' program to smallest size that fails and post later.
Jay |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Oct 26, 2011 9:26 am |
|
|
Ok, here's the code.
Code: |
// 4550_usb_connect_test_1
// runs at 48MHz, USB compatible
// randonmly hangs if PC program NOT running
#include <18f4550.h> //PIC header
#include "4550_USB_fuses.c" //config file
#include "4550_pins.c" //I/O pin definitons
#use delay(clock=48000000) //one fast PIC !
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define USB_CON_SENSE_PIN PIN_E3 //sense pin
#include <usb_cdc.h> //usb header...
#include "jay_lcd.c" //my modified LCD driver
#include "jay_ds1307.c" //my modified RTC driver
//variables
int8 hrs=0; //RTC hours
int8 min=0; //RTC minutes
int8 sec=0; //RTC seconds
int8 sec_flag; //RTC interrupt flag(1 Hz rate)
//local user functions...
void send_data_to_USBPC_1(){
printf(usb_cdc_putc,"%02u:%02u:%02u\n\r",hrs,min,sec);
}
void get_rtc_time(){
ds1307_get_time(hrs,min,sec);
}
void set_rtc_time(){
ds1307_set_time(hrs,min,sec);
}
//ISR for external interrupt from RTC on PortB.0
//Used as hardware RTC interrupt
#int_ext
void rtc_isr(){
sec_flag=1; //set 1 second flag
}
//*************************************
void main(void)
{
lcd_init();
ds1307_init();
set_rtc_time();
enable_interrupts(int_ext); //RTC
enable_interrupts(global);
usb_cdc_init();
usb_init_cs();
//-------------------------------------
//program loop....
while(1){
usb_task();
if(sec_flag!=0){ //check for 1 second up ?
sec_flag=0; //reset second flag for next time
get_rtc_time(); //get RTC time info
lcd_gotoxy(9,1); //dsp RTC time info
printf(lcd_putc,"%02u:%02u:%02u",hrs,min,sec);
if (usb_enumerated()) {
send_data_to_usbPC_1(); // send all data to PC via USB port
}
} // end of 'once per second' code
} //end of forever while
} //end of main
|
Without Realterm open (just desktop), it ran fine for 22 minutes, then halted (was actually reading post here). Clicking on realterm (to open and run)' magically' allows the program to 'continue', and is running as I type.
The Realterm window does show the correct time information from the PIC.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Oct 26, 2011 9:46 am |
|
|
Usb_task, _does not want to be the first line in main_. It needs to be in the main 'code loop'. It _must_ be called at regular intervals to handle changes.
Second thing is, you are using the wrong tool for what you want. You don't want to use usb_enumerated for this. The USB is enumerated as soon as the driver is loaded (nothing to do with a program attaching to the device). It sounds as though you are simply opening/closing the com port. To do this you should test DTR. Terminal programs will set this when they open the comm port, and clear this when they close the port. Look at usb_cdc_carrier.
So:
Code: |
if (usb_cdc_carrier.dte_present)
send_data_to_usbPC_1(); // send all data to PC via USB port
|
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Oct 26, 2011 10:44 am |
|
|
Update. The program as shown 'freezes' at exactly 20 minutes, 36 seconds with PC just sitting.Open/run realterm and PIC carrys on fine....
To my eyes...
usb_task() is the first code inside the 'forever' while(1) loop..
I'll change the test as you've offered.
will report back as to progress....
Jay |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Oct 26, 2011 11:30 am |
|
|
Well 'T', seems your if statement did the trick ! So far 3 passes, all longer than the magical 20:36, so we'll declare a success and try it in the real program
thanks
J |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19505
|
|
Posted: Wed Oct 26, 2011 3:04 pm |
|
|
temtronic wrote: | Update. The program as shown 'freezes' at exactly 20 minutes, 36 seconds with PC just sitting.Open/run realterm and PIC carrys on fine....
To my eyes...
usb_task() is the first code inside the 'forever' while(1) loop..
I'll change the test as you've offered.
will report back as to progress....
Jay |
Yes, my reply was to your earlier post without the code. The forum seems to be having a few oddities today (one of my posts didn't appear, and at one point you couldn't log in at all), so your code hadn't shown when I posted, hence the comments about usb_task, which you said was at the start of main.....
On my own applications, which have to handle the PC possibly suspending as well, I use:
Code: |
if (usb_attached()) {
if (usb_cdc_carrier.dte_present) {
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Wed Oct 26, 2011 6:01 pm |
|
|
Update. Revised program has been running 3 1/2 hours without failure !!
My chunk of code is 60 lines, USB 'support' is over 8200 lines !!
Many thanks "T"....
Now onto the next 'oddity'... |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|