void power_off(AsebaVMState *vm) { unsigned int flags; // Protect against two racing poweroff: // One from the softirq (button) // One from the VM RAISE_IPL(flags,1); behavior_stop(B_ALL); play_sound_block(SOUND_POWEROFF); // Shutdown all peripherals ... switch_off(); // Switch off USB // If we are connected to a PC, disconnect. // If we are NOT connected to a PC but 5V is present // ( == charger ) we need to keep the transciever on if(usb_uart_configured()) USBDeviceDetach(); // In any case, disable the usb interrupt. It's safer _USB1IE = 0; CHARGE_ENABLE_DIR = 1; analog_enter_poweroff_mode(); }
void main(void) { InitializeSystem(); while (1) { // Comprueba el terminal que indica la conexión USB al inicio o al reset if (PORTBbits.RB4 == 1) { // Si no se ha activado el USB, lo activa if ((USBGetDeviceState() == DETACHED_STATE)) { USBDeviceAttach(); } else { // Si ya se ha activado, realiza las tareas USB // USB Tasks blinkUSBStatus(); processUSBData(); } } else { // Si no está conectado el terminal USB, entra en modo de bajo consumo USBDeviceDetach(); LATCbits.LATC0 = 0; OSCCONbits.IDLEN = 0; Sleep(); Nop(); } }//end while }//end main
void ConsoleInit(void) { #if defined(ENABLE_CONSOLE) unsigned long i = 0; // Don't attempt anything if not connected if (!USB_BUS_SENSE) return; USBInitializeSystem(); USBDeviceAttach(); // This will only work in an interrupt driven USB system - exits on button press, only exits after enumeration is usb is detected while (USBDeviceState < CONFIGURED_STATE) { #ifndef USB_INTERRUPT USBDeviceTasks(); // Interrupt or polling method. If using polling, must call #endif USBProcessIO(); Delay10us(1); // Timed-out starting connection (perhaps a charger or disconnected?) if (i++ >= 1000000ul) { // The USB connection has failed -- if we're not using the PLL when the radio is on, turn it off now USBDeviceDetach(); return; } } // Gives host time to assign CDC port i = 0; while (USBDeviceState >= CONFIGURED_STATE && i++ < 600000ul) { MRF_LED = 1; #ifndef USB_INTERRUPT USBDeviceTasks(); // Interrupt or polling method. If using polling, must call #endif USBProcessIO(); Delay10us(10); if (usb_haschar()) { break; } } MRF_LED = 0; #endif return; }
void ChipKITUSBDeviceDetach(void) { USBDeviceDetach(); }
// Attached to USB void RunAttached(void) { // Do this first to give the card time to start up SD_ENABLE(); // Turn on SD card // Enable peripherals RtcInterruptOn(0); // Keeps time upto date LED_SET(LED_WHITE); CLOCK_PLL(); // PLL clock // Initialize sensors // Check if we have an accelerometer AccelVerifyDeviceId(); // Check if we have a gyro #ifdef USE_GYRO GyroVerifyDeviceId(); #endif #ifdef HAS_PROX // Check for prox ProxVerifyDeviceId(); ProxStartup(); #endif // Initialize sensors AccelStartup(ACCEL_RANGE_4G|ACCEL_RATE_100); #ifdef USE_GYRO GyroStartup(); #endif AdcInit(); #ifndef NO_DISPLAY DisplayClear(); Display_print_xy(" <= USB =>",0,2,2); #endif status.diskMounted = (status.lockCode == 0x0000) ? 1 : 0; status.stream = 0; // MDD_MediaInitialize(); // KL FIX: The SD card is re-inited in the usb framework which causes a lockup in some cases // Power up module if off PMD4bits.USB1MD = 0; USBInitializeSystem(); // Initializes buffer, USB module SFRs and firmware #ifdef USB_INTERRUPT USBDeviceAttach(); #endif while(USB_BUS_SENSE && restart != 1) { // Check bus status and service USB interrupts. #ifndef USB_INTERRUPT USBDeviceTasks(); // Interrupt or polling method. If using polling, must call #endif USBProcessIO(); if ((USBGetDeviceState() >= CONFIGURED_STATE) && (USBIsDeviceSuspended() == FALSE)) { const char *line = _user_gets(); status.attached = 1; if (line != NULL) { status.stream = 0; // Disable streaming SettingsCommand(line, SETTINGS_USB); } } else { status.attached = -1; } TimedTasks(); // Stream accelerometer data if (status.stream) { #define STREAM_RATE 10 #define STREAM_INTERVAL (0x10000UL / STREAM_RATE) static unsigned long lastSampleTicks = 0; unsigned long now = RtcTicks(); if (lastSampleTicks == 0) { lastSampleTicks = now; } if (now - lastSampleTicks > STREAM_INTERVAL) { accel_t accelSample; #ifdef USE_GYRO gyro_t gyroSample; #endif extern unsigned char scratchBuffer[]; char * ptr = (char *)scratchBuffer; unsigned short len; lastSampleTicks += STREAM_INTERVAL; if (now - lastSampleTicks > 2 * STREAM_INTERVAL) { lastSampleTicks = now; } // not keeping up with sample rate #ifdef HAS_PROX // Sample sensors if(ProxSampleReady()) { ProxReadSample(); ProxStartSample(); } #endif AccelSingleSample(&accelSample); #ifdef USE_GYRO GyroSingleSample(&gyroSample); #endif // Write ascii to scratch buffer ptr = (char *)scratchBuffer; ptr += sprintf(ptr, "\f$ACCEL=%d,%d,%d\r\n", accelSample.x, accelSample.y, accelSample.z); #ifdef USE_GYRO ptr += sprintf(ptr, "$GYRO=%d,%d,%d\r\n", gyroSample.x, gyroSample.y, gyroSample.z); #endif #ifdef HAS_PROX ptr += sprintf(ptr, "$PROX=%d\r\n", prox.proximity); ptr += sprintf(ptr, "$LIGHT=%d\r\n", prox.light); #endif len = (unsigned short)((void*)ptr - (void*)scratchBuffer); // Stream over USB if ((status.stream) && status.attached == 1) { usb_write(scratchBuffer, len); } } } } #if defined(USB_INTERRUPT) USBDeviceDetach(); #endif status.attached = -1; return; }