void _reentrant USBInsertionMonitorTask(void)
{
    USHORT usStatus;
    BOOL bInserted = FALSE;
    WORD i,j;
	WORD iWaitCount;

    // Check for USB disconnect
    while(1)
    {
        usb_device_get_status(USB_STATUS_CONNECTION,&usStatus);

        switch(usStatus)
        {
        case USB_CONNECTED:
            bInserted=TRUE;
            break;
        default:
            if(bInserted)//if we've been inserted, and now we're not, lets shut down
            {
//              #ifdef DEVICE_3410         // 3410 still has power when removed from usb.
              #ifdef CHKDSK
                  UsbMscCheckDiskAll();
              #endif
//              #endif
              
              // TODO - put this back in later if needed
              // Clear the display
//              ClearDisplay();
        // We need to immediately pull Write Protect low to protect the NANDs and
        // to reset the Renesas part (same line).
        HW_GPFLASH_CSR1R.B.WP0 = 0;

			  // We need to clean up any media transactions that may be hanging
              // As best we can :)
			  // stmp4770 and stmp4795, Remove drive flush on USB disconnect for NAND devices.
			  // Drive flush still necessary for MMC media, to properly close multi-writes
			  // that may be pending between SCSI commands.
              for(i=0;i<g_wUsbMscNumDevices;i++)
              {                  
                for(j=0;j<UsbMscDevice[i].wNumLunsSupported;j++)
                {
                  if(UsbMscDevice[i].Lun[j].bMediaIsRemovable == TRUE)
                  {
                    DriveFlush(UsbMscDevice[i].Lun[j].wFileSystemDriveNumber);
                  } 
                }               
              }
              
              //Do not leave.  Wait for 5V to be removed.
	          //Monitor D+/- and reset if it returns.
    	      HW_USBCSR.B.PLUGGEDIN_EN = 1;

			  iWaitCount = 0;
			  //ensure 5V Disconnect IRQ is enabled
			  SysSetIrqLevel(HW_SR_IM_L2_SETMASK);

			  while(HW_USBCSR.B.VBUSSENSE)
			  {
                  //if USB is plugged-in.  Break loop and reset to return to USBMSC.
                  if(HW_USBCSR.B.PLUGGEDIN)
                  {
                     break;
                  }


			      //wait a bit
			      SysWait(200);

				  if(iWaitCount > 10)
				  {
					 break;		// if we waited a couple secs, time to break
				  }
				  else
				  {
				  	iWaitCount++;	//increment wait count
				  }

			   }                     
			  SysWait(200);

              // Shut down
              SystemShutdown();

              // Reset the device
              SystemReset();
            }
        }
        SysWait(USB_WATCH_CALL_BACK_DELAY);
    }    
}
    ;  they are included in dcc.xref
#pragma endasm

////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
//
//>  Name:          Project_Startup          
//
//   Type:          Function
//
//   Description:   A main entry point called by the kernel
//
//   Inputs:        none
//
//   Outputs:       none
//
//   Notes:         none
//<
////////////////////////////////////////////////////////////////////////////////
//int jCurrLimEntry=1;
void Project_Startup(void)
{
    //BOOL bDone;
    WORD i;
    //WORD j;
    //DWORD wTime;
    //WORD wCount = 5000;
    //RETCODE retCode;
    
    USHORT  usStatus;
    BOOL bLowPowerUsbStickyBitReadResult = FALSE;

    //bDone = FALSE;
    
    // Startup
    // Run debug initialization
    (DebugInit()== SUCCESS);

    // Let's start with a clean system
    SystemShutdown();

    #pragma asm

        extern  FTimeInit
        jsr     FTimeInit
    
    #pragma endasm
    
// In order to free up P memory space, we're doing the same trick as above for
// some other initialization functions.
    {
        unsigned int ClearSize = (unsigned int)(&_lc_u_y_MMC_bssclear_end-&_lc_u_y_MMC_bssclear_start);

        unsigned int CopySize = (unsigned int)(&_lc_u_y_OVLY_copy_end-&_lc_u_y_OVLY_copy_start);
        
        CopyCodeIntoBootmanagerArea((_Y WORD *)&_lc_u_y_OVLY_copy_start, 
                                    (_P WORD * _P)&_lc_u_p_OVLY_start, CopySize);
        
        // In either case(MMC or NAND only), clear the bss section.
        ClearMemory((_Y WORD *)&_lc_u_y_MMC_bssclear_start, ClearSize);
    }
Exemplo n.º 3
0
Arquivo: Main.c Projeto: bochaqos/tol
/*------------------------------------------------------------------------------

 FUNCTION NAME: Main_EndApp

 DESCRIPTION: Application end procedure

 PARAMETERS:
   - INPUT:
   - OUTPUT:

 RETURN:

 NOTES:

------------------------------------------------------------------------------*/
void Main_EndApp (t_uchar With_Confirmation, t_uchar Do_Exit)
{
int     LCDContrastOffset;

  /* Pedido de confirmacion para cierre del sistema */
  if (With_Confirmation)
  {
    char Key;
    
    Key = Display_Aviso (S4160, S4161, NULL, NULL, 80, (t_uchar) AVISO_BEEP, TRUE);
    if (Key == KEY_ESC) return;
  }
  
  /* Save global database */
  DB_SaveDBGlobal ();

  /* Ramdisk exit function */
  RamDisk_End ();

  /* Restore LCD contrast */
  DB_GetLCDContrastOffset (&LCDContrastOffset);
  Util_SetLCDContrast (-LCDContrastOffset);

#if !defined (NT_VERSION)
  GrphDisp_End();
#endif
  
/* MAEL Version */
#if defined (FEDORA_VERSION)

  TCPIP_Close();

#else

#if defined(PC_TERMINAL) && !defined(NT_VERSION)

  /* Disable graphics */
  closegraph();
  /* Disable serial port communications */
  Uart_DisChannels ();

  #if defined (DOSX286)
    /* Restore Ctrl-Break handler */
    DosSetRealProtVec (0x1B, OldCtrlBrkProt, OldCtrlBrkReal, NULL, NULL);
    DosSetRealProtVec (0x23, OldCtrlCProt, OldCtrlCReal, NULL, NULL);
  #endif

#endif

#endif /* MAEL Version */

  /* Keyboard exit function */
  Kb_End ();

/* MAEL Version */
#if defined (FEDORA_VERSION)
  Devices_CD_Close ();
  Devices_Scanner_Close ();

#if !defined(DEBUG)
  /* Shutdown */
  if (!DB_AutoRestartAppFlag ())
    SystemShutdown();
#endif
  
#endif

  if (Do_Exit)
    exit(0);

} /* Main_EndApp */
// Function Description: 
// Inputs:  no parameters
// Returns: no register returns
// Notes:   
void _reentrant UserInterfaceTask(void)
{
    USHORT      usStatus;
    //int iTransferRate;
    WORD State=SCSI_IDLE;
    //Message Msg;
    int iDelay = HALF_SEC_UPDATE_RATE;
    BOOL bNeedToReEnable = FALSE;
    int Write_State_Persisten_Time = WRITE_STATE_PERSISTENT_TIME; 
    int i;
    
    for (i=10;i< USER_INTERFACE_STACK_SIZE;i++ )
    {   g_UserInterfaceStack[i]=0xc0ffee;
    }

    Init5VSense();

    USBLCDDisplayInit();

    SysWait(250);

    while(1)
    {
    #if (NUM_REMOVABLE_MEDIA == 1)
        // Wait until we have decided whether device is in MTP vs. MSC mode
        // Media detection uses different code for the two modes
        if(g_MtpArbitrationDone)
        {   if(MTPDetected)
            {   MtpCheckExternalMedia();
            }
            else
            {   ScsiInsertionRemovalCode();                
            }
        }
    #endif

        usb_device_get_status(USB_STATUS, &usStatus);
        if(usStatus == USB_STATE_SUSPENDED)
        {   SysWait(UPDATE_RATE);
        }
        else
        {   SysPostMessage(2,LCD_END_FRAME);
            SysWait(UPDATE_RATE);
            SysPostMessage(2,LCD_BEGIN_FRAME);
            USBLCDCheckForTransfers();
            
            if(g_bServiceDCDC)
            {   ServiceDCDC();  //Service DCDC converter
            }
        #ifdef BATTERY_CHARGE
            // For 3500 Battery Monitoring must be done only during battery charging
            // The rest of the time the system operates from USB +5V
            // and battery monitoring does not make any sense.
            // Also, battery brownout always triggered if sys operates w/ no battery.

            //Only charge if we're in high usb current mode. March 11 2005 addition
            if(usb_get_current_limit() <= 100) //mA
            {    BatteryChargeDisableCharging(TRUE); // until next stmp bootup
            }
            else
            {   if(usStatus !=  USB_STATE_CONFIGURED)
                {
                    if(bNeedToReEnable == FALSE)
                    {   BatteryChargeDisableCharging(FALSE);
                        bNeedToReEnable = TRUE;
                    }
                }
                else 
                {   if(bNeedToReEnable == TRUE)
                    {   BatteryChargeEnableCharging();
                        bNeedToReEnable = FALSE;
                    }                
                    BatteryChargeStateMachine();
                }
            }
        #endif

#if defined(BATTERY_CHARGE)            
            // I only want to update the Battery icon once per 1/2 second
            // but always check and average battery level.
            USBLCDCheckBatteryLevel();
#endif            
            if (iDelay-- <= 0)
            {
#if defined(BATTERY_CHARGE)               
                USBLCDDisplayBatteryLevel();
#endif                                
                USBLCDCheckDBStoreDirty();
                iDelay = HALF_SEC_UPDATE_RATE;
            }


            // g_wActivityState - This global is set by other code to indicate the state of the
            // system.
            // Can be set by SCSI code, or MTP code (depending on the connection)

            // Modification added for MTP/MSC system
            // In MTP/MSC, MSC uses overlays thus we need to be careful yielding to the kernel for display updates.
            // This can not be done in the middle of a multi write sequence.
            // The code was modified and uses the same technique as a monostable device. Each time scsi write is entered
            // the global g_wActivityStateMultiWrite is set to SCSI_WRITING. This will trigger the monostable for  
            // WRITE_STATE_PERSISTENT_TIME miliseconds. If a new scsi write command arrives prior WRITE_STATE_PERSISTENT_TIME
            // expires, the new scsi write command re-starts the monostable for a new full WRITE_STATE_PERSISTENT_TIME ms.
            // Once the monostable time expires without no new SCSI write commands, the state will switch to IDLE and display 
            // will return to the READY state.
            // The display task has a chance to run everytime a new SCSI write command arrives. There is no display refresh
            // during mutltiwrites. 
            if((g_wActivityStateMultiWrite == SCSI_WRITING) || (Write_State_Persisten_Time < WRITE_STATE_PERSISTENT_TIME))
            {
                if(g_wActivityStateMultiWrite == SCSI_WRITING)
                {
                    g_wActivityStateMultiWrite = SCSI_IDLE;
                    Write_State_Persisten_Time = 0;
                    
                    if(State != SCSI_WRITING)
                    {   State = SCSI_WRITING;
                        USBLCDWriting();
                    }
                }
                else
                {   Write_State_Persisten_Time += UPDATE_RATE;
                }
            }   
            else
            {
                if(State!= g_wActivityState )
                {   State = g_wActivityState;
                    switch(State)
                    {
                    case SCSI_IDLE:
                        USBLCDIdle();
                        break;
                    case SCSI_READING:
                        USBLCDReading();
                        break;
                    case SCSI_WRITING:
                        USBLCDWriting();
                        break;
                    default :
                        USBLCDIdle();
                    }
                }
            }
        } // end else !suspended

        {   USHORT usStatus;  // fixes 8777 (0 or 2 usb unplug tries out of 20 would not 
                              // boot player & mtp was still running due to rare irq loss)
            usb_device_get_status(USB_STATUS_CONNECTION,&usStatus); //we could just check the usb 5v bit instead of calling this.
            if( usStatus == USB_DISCONNECTED )
            {   // Restart the MTP device and re-enumerate
                //DebugBuildAssert(0); //During JLN verification, this hit & the reset started player. 
                StorFlush();   // flush the database first
                SystemShutdown();// we could use pragma asm to jump to the missed isr instead which resets also.
                SystemReset();
            }
        }
        
        // Update the count if it is not disabled( negative means disabled).
        if (g_StoreWatchDogCount >= 0)
        {   g_StoreWatchDogCount++;
        }
        
    } // while(1)
} // void _reentrant UserInterfaceTask(void)