Hi all.
Today I'm think and find any solution how to check if is USB connected or not.
Some user use USB only short time , other time use native function and with interrupt too.
If use interrupt (timer, UART ...) than can't use USB . There is big time critical function for emulate USB
on AVR MCU.O want use USB for upgrade MCU firmware or change any setting in internal EEprom.
But it's only if need other use normal program with enabled other interrupt peripherals. For this I don't want
use other mcu like FTDI or other. With this I must check if is USB or not. If is plugged into USB , small function check it
and disable other interrupt and some special function, switch internal program into USB mode. Make all what I need and if unplug
then my small function check so USB is lost and can enable all needed interrupt and user function. My program work normal mode.
If plug it in to USB again then check it and disable other interrupt and re-enumerate USB and PC detect USB device normal (not tested now).
Check:
if use MCU with pin change interrupt then we can enable any D+ or D- used pin for USB and check interrupt flag.
If USB communication change any state on this pin then set flag on special function register for pin change interrupt.
But If use ATmega8 then we can use this because ATmega8 don't have this.
other solution is use any free bit in special function register and append or change any ASM function on usbdrvasm.inc file.
I use this solution.
In my application never want use Analog comparator and use ACIS0 bit in ACSR special function register.
On (I use 12MHz ) usbdrvasm12.inc find this instruction nop2. This instruction is defined in usbportability.h
#define nop2 rjmp .... and definitionof jump. This define is used for 2 cycle MCU and one instruction (is possible use 2x nop but it's use 2 instruction)
I define new
#define nopU sbi ACSR,ACIS0
and all nop2 instruction change with this nopU
if External Interrupt 0 trigger then set this bit and SFR and on my program can check it and clear .
in my program check if is this bit set , if is then clear it and wait 100mS. after this time I check again if is then I know so is plugged in to USB .
if not can I run my normal function.
king regards.
PS: I try only detect, with re-enumerate not tested (later)