void main()
{

	TD_Init();


	while (1)  // Begin infinite loop
	{
 		const long int DELAY = 10000; 
		OUTA = 0x00;
		//OUTB = 0x00;
		//OUTC = 0x00;
		delay(DELAY);
		OUTA = 0xff; // Set all portA to on
		//OUTB = 0xff;
		//OUTC = 0xff;
		delay(DELAY);
		
		SBUF0 = 'a';
	}// END 
}
void main()
{

	TD_Init();

	while (1)
	{


 		const long int DELAY = 10000; 
		OUTA = 0x00;
		//OUTB = 0x00;
		//OUTC = 0x00;
		delay(DELAY);
		OUTA = 0xff;
		//OUTB = 0xff;
		//OUTC = 0xff;
		delay(DELAY);

	}
}
Beispiel #3
0
// Task dispatcher
void main(void)
{
   // Initialize Global States
//   Sleep = FALSE;               // Disable sleep mode
   Rwuen = FALSE;               // Disable remote wakeup
   Selfpwr = FALSE;            // Disable self powered
   GotSUD = FALSE;               // Clear "Got setup data" flag

   // Initialize user device
   TD_Init();
	EA=0;

   pDeviceDscr = (WORD)&DeviceDscr;
   pDeviceQualDscr = (WORD)&DeviceQualDscr;
   pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr;
   pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr;
   pStringDscr = (WORD)&StringDscr;

   EZUSB_IRQ_ENABLE();            // Enable USB interrupt (INT2)
  // EZUSB_ENABLE_RSMIRQ();            // Wake-up interrupt

   INTSETUP |= (bmAV2EN | bmAV4EN);     // Enable INT 2 & 4 autovectoring

   //USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT;   // Enable selected interrupts
	USBIE |= bmSUDAV | bmSUTOK | bmURES | bmHSGRANT;   // Enable selected interrupts
   EA = 1;                  // Enable 8051 interrupts


#ifndef NO_RENUM
   // Renumerate if necessary.  Do this by checking the renum bit.  If it
   // is already set, there is no need to renumerate.  The renum bit will
   // already be set if this firmware was loaded from an eeprom.
   if(!(USBCS & bmRENUM))
   {
       EZUSB_Discon(TRUE);   // renumerate
   }
#endif

   // unconditionally re-connect.  If we loaded from eeprom we are
   // disconnected and need to connect.  If we just renumerated this
   // is not necessary but doesn't hurt anything
   USBCS &=~bmDISCON;

   CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch

   // clear the Sleep flag.
 //  Sleep = FALSE;

	//download the serial number from the EEPROM
	//EZUSB_InitI2C(); // already done in TD_init which was called above
	downloadSerialNumberFromEEPROM();

   // Task Dispatcher
   while(TRUE)               // Main Loop
   {
      // Poll User Device
      TD_Poll();

      // Check for pending SETUP
      if(GotSUD)
      {
         SetupCommand();          // Implement setup command
         GotSUD = FALSE;          // Clear SETUP flag
      }

      // check for and handle suspend.
      // NOTE: Idle mode stops the processor clock.  There are only two
      // ways out of idle mode, the WAKEUP pin, and detection of the USB
      // resume state on the USB bus.  The timers will stop and the
      // processor will not wake up on any other interrupts.
      if (Sleep) // the device doesn't start if this is checked
      {
         if(TD_Suspend())
         { 
            Sleep = FALSE;     // Clear the "go to sleep" flag.  Do it here to prevent any race condition between wakeup and the next sleep.
            do
            {
               EZUSB_Susp();         // Place processor in idle mode.
            }
            while(!Rwuen && EZUSB_EXTWAKEUP());
            // above.  Must continue to go back into suspend if the host has disabled remote wakeup
            // *and* the wakeup was caused by the external wakeup pin.

            // 8051 activity will resume here due to USB bus or Wakeup# pin activity.
            EZUSB_Resume();   // If source is the Wakeup# pin, signal the host to Resume.      
            TD_Resume();
         }   
      }
   }
}
Beispiel #4
0
// Task dispatcher
void main(void)
{
   DWORD   i;
   WORD   offset;
   DWORD   DevDescrLen;
   DWORD   j=0;
   WORD   IntDescrAddr;
   WORD   ExtDescrAddr;
   DWORD   tCount=0;
   // Initialize Global States
   Sleep = FALSE;               // Disable sleep mode
   Rwuen = FALSE;               // Disable remote wakeup
   Selfpwr = FALSE;            // Disable self powered
   GotSUD = FALSE;               // Clear "Got setup data" flag


	
   // Initialize user device
    TD_Init();
   //JTAG Enable and SYNC signals for ZTEX Spartan 6 module 1.1 (FGPA+FX2LP setup)
	OEA|=0x02; //Declare PA.1 as output
	SYNCDELAY;
	IOA|=0x02; //output 1 on PA.1
	SYNCDELAY;

	OEC|=0x01; //PC.0 as output (SYNC signal)
	SYNCDELAY;
	IOC|=0x00; //output 0 on PC.0...SYNC signal is LOW 
	SYNCDELAY;

	OEC&=0xFD; //PC.1 as input (Clock changing signal)
	SYNCDELAY;
	
	
	

   // The following section of code is used to relocate the descriptor table. 
   // Since the SUDPTRH and SUDPTRL are assigned the address of the descriptor 
   // table, the descriptor table must be located in on-part memory.
   // The 4K demo tools locate all code sections in external memory.
   // The descriptor table is relocated by the frameworks ONLY if it is found 
   // to be located in external memory.
   pDeviceDscr = (WORD)&DeviceDscr;
   pDeviceQualDscr = (WORD)&DeviceQualDscr;
   pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr;
   pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr;
   pStringDscr = (WORD)&StringDscr;

   if (EZUSB_HIGHSPEED())
   {
      pConfigDscr = pHighSpeedConfigDscr;
      pOtherConfigDscr = pFullSpeedConfigDscr;
   }
   else
   {
      pConfigDscr = pFullSpeedConfigDscr;
      pOtherConfigDscr = pHighSpeedConfigDscr;
   }

   if ((WORD)&DeviceDscr & 0xe000)
   {
      IntDescrAddr = INTERNAL_DSCR_ADDR;
      ExtDescrAddr = (WORD)&DeviceDscr;
      DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2;
      for (i = 0; i < DevDescrLen; i++)
         *((BYTE xdata *)IntDescrAddr+i) = 0xCD;
      for (i = 0; i < DevDescrLen; i++)
         *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i);
      pDeviceDscr = IntDescrAddr;
      offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR;
      pDeviceQualDscr -= offset;
      pConfigDscr -= offset;
      pOtherConfigDscr -= offset;
      pHighSpeedConfigDscr -= offset;
      pFullSpeedConfigDscr -= offset;
      pStringDscr -= offset;
   }

   EZUSB_IRQ_ENABLE();            // Enable USB interrupt (INT2)
   EZUSB_ENABLE_RSMIRQ();            // Wake-up interrupt

   INTSETUP |= (bmAV2EN | bmAV4EN);     // Enable INT 2 & 4 autovectoring

   USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT;   // Enable selected interrupts
   EA = 1;                  // Enable 8051 interrupts

#ifndef NO_RENUM
   // Renumerate if necessary.  Do this by checking the renum bit.  If it
   // is already set, there is no need to renumerate.  The renum bit will
   // already be set if this firmware was loaded from an eeprom.
   if(!(USBCS & bmRENUM))
   {
       EZUSB_Discon(TRUE);   // renumerate
   }
#endif

   // unconditionally re-connect.  If we loaded from eeprom we are
   // disconnected and need to connect.  If we just renumerated this
   // is not necessary but doesn't hurt anything
   USBCS &=~bmDISCON;

   CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch to 0 (after renumeration)

   // clear the Sleep flag.
   Sleep = FALSE;



   // Task Dispatcher
   while(TRUE)               // Main Loop
   {
      if(GotSUD)            // Wait for SUDAV
      {
         SetupCommand();          // Implement setup command
           GotSUD = FALSE;            // Clear SUDAV flag
      }

      // Poll User Device
      // NOTE: Idle mode stops the processor clock.  There are only two
      // ways out of idle mode, the WAKEUP pin, and detection of the USB
      // resume state on the USB bus.  The timers will stop and the
      // processor will not wake up on any other interrupts.
      if (Sleep)
          {
          if(TD_Suspend())
              { 
              Sleep = FALSE;            // Clear the "go to sleep" flag.  Do it here to prevent any race condition between wakeup and the next sleep.
              do
                  {
                    EZUSB_Susp();         // Place processor in idle mode.
                  }
                while(!Rwuen && EZUSB_EXTWAKEUP());
                // Must continue to go back into suspend if the host has disabled remote wakeup
                // *and* the wakeup was caused by the external wakeup pin.
                
             // 8051 activity will resume here due to USB bus or Wakeup# pin activity.
             EZUSB_Resume();   // If source is the Wakeup# pin, signal the host to Resume.      
             TD_Resume();
              }   
          }

	
	
    	TD_Poll();

   }
}
Beispiel #5
0
// Task dispatcher
void main(void)
{
   DWORD   i;
   WORD   offset;
   DWORD   DevDescrLen;
   DWORD   j=0;
   WORD   IntDescrAddr;
   WORD   ExtDescrAddr;

   // Initialize Global States
   Sleep = FALSE;               // Disable sleep mode
   Rwuen = FALSE;               // Disable remote wakeup
   Selfpwr = FALSE;            // Disable self powered
   GotSUD = FALSE;               // Clear "Got setup data" flag

   // Initialize user device
   TD_Init();

   // The following section of code is used to relocate the descriptor table. 
   // The frameworks uses SUDPTRH and SUDPTRL to automate the SETUP requests
   // for descriptors.  These registers only work with memory locations
   // in the EZ-USB internal RAM.  Therefore, if the descriptors are located
   // in external RAM, they must be copied to in internal RAM.  
   // The descriptor table is relocated by the frameworks ONLY if it is found 
   // to be located in external memory.
   pDeviceDscr = (WORD)&DeviceDscr;
   pDeviceQualDscr = (WORD)&DeviceQualDscr;
   pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr;
   pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr;
   pStringDscr = (WORD)&StringDscr;

   // Is the descriptor table in external RAM (> 16Kbytes)?  If yes,
   // then relocate.
   // Note that this code only checks if the descriptors START in 
   // external RAM.  It will not work if the descriptor table spans
   // internal and external RAM.
   if ((WORD)&DeviceDscr & 0xC000)
   {
      // first, relocate the descriptors
      IntDescrAddr = INTERNAL_DSCR_ADDR;
      ExtDescrAddr = (WORD)&DeviceDscr;
      DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2;
      for (i = 0; i < DevDescrLen; i++)
         *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i);

      // update all of the descriptor pointers
      pDeviceDscr = IntDescrAddr;
      offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR;
      pDeviceQualDscr -= offset;
      pConfigDscr -= offset;
      pOtherConfigDscr -= offset;
      pHighSpeedConfigDscr -= offset;
      pFullSpeedConfigDscr -= offset;
      pStringDscr -= offset;
   }

   EZUSB_IRQ_ENABLE();            // Enable USB interrupt (INT2)
   EZUSB_ENABLE_RSMIRQ();            // Wake-up interrupt

   INTSETUP |= (bmAV2EN | bmAV4EN);     // Enable INT 2 & 4 autovectoring

   USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT;   // Enable selected interrupts
   EA = 1;                  // Enable 8051 interrupts

#ifndef NO_RENUM
   // Renumerate if necessary.  Do this by checking the renum bit.  If it
   // is already set, there is no need to renumerate.  The renum bit will
   // already be set if this firmware was loaded from an eeprom.
   if(!(USBCS & bmRENUM))
   {
       EZUSB_Discon(TRUE);   // renumerate
   }
#endif

   // unconditionally re-connect.  If we loaded from eeprom we are
   // disconnected and need to connect.  If we just renumerated this
   // is not necessary but doesn't hurt anything
   USBCS &=~bmDISCON;

   CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch

   // clear the Sleep flag.
   Sleep = FALSE;

   // Task Dispatcher
   while(TRUE)               // Main Loop
   {
      // Poll User Device
      TD_Poll();

      // Check for pending SETUP
      if(GotSUD)
      {
         SetupCommand();          // Implement setup command
         GotSUD = FALSE;          // Clear SETUP flag
      }

      // check for and handle suspend.
      // NOTE: Idle mode stops the processor clock.  There are only two
      // ways out of idle mode, the WAKEUP pin, and detection of the USB
      // resume state on the USB bus.  The timers will stop and the
      // processor will not wake up on any other interrupts.
      if (Sleep)
      {
         if(TD_Suspend())
         { 
            Sleep = FALSE;     // Clear the "go to sleep" flag.  Do it here to prevent any race condition between wakeup and the next sleep.
            do
            {
               EZUSB_Susp();         // Place processor in idle mode.
            }
            while(!Rwuen && EZUSB_EXTWAKEUP());
            // above.  Must continue to go back into suspend if the host has disabled remote wakeup
            // *and* the wakeup was caused by the external wakeup pin.

            // 8051 activity will resume here due to USB bus or Wakeup# pin activity.
            EZUSB_Resume();   // If source is the Wakeup# pin, signal the host to Resume.      
            TD_Resume();
         }   
      }

   }
}
Beispiel #6
0
// Task dispatcher
void main(void)
{
   DWORD   i;
   WORD   offset;
   DWORD   DevDescrLen;
   DWORD   j=0;
   WORD   IntDescrAddr;
   WORD   ExtDescrAddr;

   // Initialize Global States
   Sleep = FALSE;               // Disable sleep mode
   Rwuen = FALSE;               // Disable remote wakeup
   Selfpwr = FALSE;            // Disable self powered
   GotSUD = FALSE;               // Clear "Got setup data" flag



   // The following section of code is used to relocate the descriptor table. 
   // Since the SUDPTRH and SUDPTRL are assigned the address of the descriptor 
   // table, the descriptor table must be located in on-part memory.
   // The 4K demo tools locate all code sections in external memory.
   // The descriptor table is relocated by the frameworks ONLY if it is found 
   // to be located in external memory.
   pDeviceDscr = (WORD)&DeviceDscr;
   pDeviceQualDscr = (WORD)&DeviceQualDscr;
   pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr;
   pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr;
   pStringDscr = (WORD)&StringDscr;

   if (EZUSB_HIGHSPEED())
   {
      pConfigDscr = pHighSpeedConfigDscr;
      pOtherConfigDscr = pFullSpeedConfigDscr;
   }
   else
   {
      pConfigDscr = pFullSpeedConfigDscr;
      pOtherConfigDscr = pHighSpeedConfigDscr;
   }

   if ((WORD)&DeviceDscr & 0xe000)
   {
      IntDescrAddr = INTERNAL_DSCR_ADDR;
      ExtDescrAddr = (WORD)&DeviceDscr;
      DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2;
      for (i = 0; i < DevDescrLen; i++)
         *((BYTE xdata *)IntDescrAddr+i) = 0xCD;
      for (i = 0; i < DevDescrLen; i++)
         *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i);
      pDeviceDscr = IntDescrAddr;
      offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR;
      pDeviceQualDscr -= offset;
      pConfigDscr -= offset;
      pOtherConfigDscr -= offset;
      pHighSpeedConfigDscr -= offset;
      pFullSpeedConfigDscr -= offset;
      pStringDscr -= offset;
   }

   EZUSB_IRQ_ENABLE();            // Enable USB interrupt (INT2)
   EZUSB_ENABLE_RSMIRQ();            // Wake-up interrupt

   INTSETUP |= (bmAV2EN | bmAV4EN);     // Enable INT 2 & 4 autovectoring

   USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT;   // Enable selected interrupts
   EA = 1;                  // Enable 8051 interrupts

   #ifndef NO_RENUM
   // Note: at full speed, high speed hosts may take 5 sec to detect device
   EZUSB_Discon(TRUE); // Renumerate
   #endif

   CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch to 0 (after renumeration)

   // clear the Sleep flag.
   Sleep = FALSE;

   // Initialize user device
   TD_Init();
   
   // Task Dispatcher
   while(TRUE)               // Main Loop
   {
      if(GotSUD)            // Wait for SUDAV
      {
         SetupCommand();          // Implement setup command
           GotSUD = FALSE;            // Clear SUDAV flag
      }
	  else
	  {
		TD_Poll();	  
	  }

      // Poll User Device
      // NOTE: Idle mode stops the processor clock.  There are only two
      // ways out of idle mode, the WAKEUP pin, and detection of the USB
      // resume state on the USB bus.  The timers will stop and the
      // processor will not wake up on any other interrupts.
      if (Sleep)
          {
          if(TD_Suspend())
              { 
              Sleep = FALSE;            // Clear the "go to sleep" flag.  Do it here to prevent any race condition between wakeup and the next sleep.
              do
                  {
                    EZUSB_Susp();         // Place processor in idle mode.
                  }
                while(!Rwuen && EZUSB_EXTWAKEUP());
                // Must continue to go back into suspend if the host has disabled remote wakeup
                // *and* the wakeup was caused by the external wakeup pin.
                
             // 8051 activity will resume here due to USB bus or Wakeup# pin activity.
             EZUSB_Resume();   // If source is the Wakeup# pin, signal the host to Resume.      
             TD_Resume();
              }   
          }
      
   }
}
Beispiel #7
0
/* ******************************************************************** */
void Check_Bootloader(void)
{
		// reinit all chip
		TD_Init();
		// just to allow send/receive from midi and usb inputs
		z8_board_fully_updated_from_ez0_bit = 1;
		// Init i2c bus for eeprom ez upgrade
		//EZUSB_InitI2C();
		// update bootloading flags and state
		bootloader_state = SEND_IDENTITY_SYSEX; 		

		// First send the Bootloader identity sysex acknoledge to midi & usb out
		SendIdentityBootLoader();

		must_program_z8 = FALSE;

		// The data firmware upgrade starts here
		bootloader_state = WAIT_EZ_FW_DATA;				
		// Now Wait for the ez firmware data sysexs
		WaitNextBootLoaderState(bootloader_state);

		if(must_program_z8)
		{
			// Release Z8 board reset line
			Z8_RESET_PORT_OUT |=  Z8_RESET_PIN;
			// wait por
			EZUSB_Delay(200);

			// disable rx1
			SCON1 = 0x40;		/* SCON1: mode 1, 8-bit UART, disable rcvr  */

			// tx1 is now dedicated to z8 dbg pin
			OCD_EN_PORT_OUT |= OCD_EN_PIN;

			// set z8 dbg pin low
			TX1_TO_Z8_DBG_PORT_OUT &= ~TX1_TO_Z8_DBG_PIN;

			// wait 2sec
			EZUSB_Delay(2000);

			// set z8 dbg pin high
			TX1_TO_Z8_DBG_PORT_OUT |= TX1_TO_Z8_DBG_PIN;
			
			// turn on tx1 and rx1 (serial to/from z8)
			PORTBCFG |= 0x0C;

			// send ocd first byte so the z8 autobaudrate the serial line
			Send_To_Serial_Z8(OCD_FIRST_BYTE);
			EZUSB_Delay1ms();

			// place the z8 chip in ocd mode
			Send_To_Serial_Z8(OCD_WRITE_CTR_REG_CMD);
			Send_To_Serial_Z8(OCD_CTR_REG_DBGMODE);
			EZUSB_Delay(100);

			InitFlashFrqAndUnlock();

			// selet flash page 0
			WriteSingleRegOCD(OCD_FLASH_SELECT_PAGE, 0x00);

			// launch a mass erase
			WriteSingleRegOCD(OCD_FLASH_CTR_REG_ADD, OCD_FLASH_MASS_ERASE);
			
			EZUSB_Delay(Z8_ERASING_TIME);

			InitFlashFrqAndUnlock();
		
			// Now Wait for the z8 firmware data sysexs
			WaitNextBootLoaderState(bootloader_state);

			EZUSB_Delay(50);

			// relock the flash controller
			WriteSingleRegOCD(OCD_FLASH_CTR_REG_ADD, OCD_FLASH_CTR_REG_RESET);
			
			/*
			///////////////////// Z8 CRC CHECKS /////////////////////////////////
			// V1.4 it is now time to verify Z8 programmed flash 
			bootloader_state = VERIFY_Z8_CRC;	
			// V1.4 Read the Z8 memory CRC and compare to calculated CRC
			Send_To_Serial_Z8(OCD_READ_FLASH_CRC);
			// V1.4 required time is 65536 * 1/(20MHz)
			EZUSB_Delay(50);
			// V1.4 the serial port should now have received the z8 internal CRC in buffer 0 to 3
			// Send this CRC to host
			Serial_In_From_Z8.buffer[7]   = Serial_In_From_Z8.buffer[0];
			Serial_In_From_Z8.buffer[8]   = Serial_In_From_Z8.buffer[1];  			
			Serial_In_From_Z8.end_index   = WID_SYX_HEADER_SIZE + 3 + 1;
			AssignWidSysexHeader();
			// send ack firmware 
  			Serial_In_From_Z8.buffer[6]   = FRAME_IS_FIRMWARE_OR_PURE_MIDI | sysex_direction_sysxid0 | prog_upload_sysxid0;
  			// send eox
 			Serial_In_From_Z8.buffer[9]  = MIDI_EOX;
			SendDispatchFwSysex();
			///////////////////// END Z8 CRC CHECKS /////////////////////////////////
			*/
						
			// exit z8 ocd mode
			OCD_EN_PORT_OUT   &=  ~OCD_EN_PIN;

			// exit z8 ocd mode
			Z8_RESET_PORT_OUT &=  ~Z8_RESET_PIN;
		}

	// Now finish with a soft reset
	// SoftReset();
	while(1)
	{ 
		Check_Usb();
		Ep2out_Function();
	}
}
// Called once at startup
//
void mainInit(void) {

	__xdata uint8 thisByte = 0xFF;
	__xdata uint16 blockSize;

	

	// This is only necessary for cases where you want to load firmware into the RAM of an FX2 that
	// has already loaded firmware from an EEPROM. It should definitely be removed for firmwares
	// which are themselves to be loaded from EEPROM.
#ifndef EEPROM
	RENUMERATE_UNCOND();
#endif

// Needs to be matched to stuff in HDMI2USB/cypress/hdmi2usb.c TD_Init
// void TD_Init(void)             // Called once at startup

	
	// Clear wakeup (see AN15813: http://www.cypress.com/?docID=4633)
	WAKEUPCS = bmWU | bmDPEN | bmWUEN;
	WAKEUPCS = bmWU | bmDPEN | bmWUEN;

	// Disable alternate functions for PORTA 0,1,3 & 7.
	PORTACFG = 0x00;

	/*
	// Return FIFO settings back to default just in case previous firmware messed with them.
	SYNCDELAY; PINFLAGSAB = 0x00;
	SYNCDELAY; PINFLAGSCD = 0x00;
	SYNCDELAY; FIFOPINPOLAR = 0x00;

	// Global settings
	SYNCDELAY; REVCTL = (bmDYN_OUT | bmENH_PKT); // 0x03
	SYNCDELAY; CPUCS = bmCLKSPD1;  // 48MHz 0x10

	// Drive IFCLK at 48MHz, enable slave FIFOs
	//SYNCDELAY; IFCONFIG = (bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | bmFIFOS);
	SYNCDELAY; IFCONFIG = (bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | bmPORTS); // 0xe0

	// EP1OUT & EP1IN
	SYNCDELAY; EP1OUTCFG = (bmVALID | bmBULK); // 0xa0
	SYNCDELAY; EP1INCFG = (bmVALID | bmBULK); // 0xa0

	// EP2OUT & EP6IN are quad-buffered bulk endpoints
	SYNCDELAY; EP2CFG = (bmVALID | bmBULK); // 0xa0
	SYNCDELAY; EP4CFG = 0x00;
	SYNCDELAY; EP6CFG = (bmVALID | bmBULK | bmDIR); // 0xe0
	SYNCDELAY; EP8CFG = 0x00;

	// EP2OUT & EP6IN automatically commit packets
	SYNCDELAY; EP2FIFOCFG = bmAUTOOUT; // 0x10
	SYNCDELAY; EP4FIFOCFG = 0x00;
	SYNCDELAY; EP6FIFOCFG = bmAUTOIN; // 0x08
	SYNCDELAY; EP8FIFOCFG = 0x00;

	// Reset FIFOs for EP2OUT & EP6IN
	SYNCDELAY; FIFORESET = bmNAKALL; // 0x80
	SYNCDELAY; FIFORESET = 2;  // reset EP2OUT
	SYNCDELAY; FIFORESET = 6;  // reset EP6IN
	SYNCDELAY; FIFORESET = 0x00;

	// Arm EP1OUT
	EP1OUTBC = 0x00;

	// Arm the EP2OUT buffers. Done four times because it's quad-buffered
	SYNCDELAY; OUTPKTEND = bmSKIP | 2;  // EP2OUT 0x82
	SYNCDELAY; OUTPKTEND = bmSKIP | 2;
	SYNCDELAY; OUTPKTEND = bmSKIP | 2;
	SYNCDELAY; OUTPKTEND = bmSKIP | 2;


	// Auto-commit 512-byte packets from EP6IN (master may commit early by asserting PKTEND)
	SYNCDELAY; EP6AUTOINLENH = 0x02;
	SYNCDELAY; EP6AUTOINLENL = 0x00;
	
	// Turbo I2C
	I2CTL |= bm400KHZ;

	// Auto-pointers
	AUTOPTRSETUP = bmAPTREN | bmAPTR1INC | bmAPTR2INC; // 0x07

	// Port lines all inputs...
	IOA = 0xFF;
	OEA = 0x00;
	IOB = 0xFF;
	OEB = 0x00;
	IOC = 0xFF;
	OEC = 0x00;
	IOD = 0xFF;
	OED = 0x00;
	IOE = 0xFF;
	OEE = 0x00;

#ifdef EEPROM
	#ifdef BSP
		#include STR(boards/BSP.c)
	#endif
#endif
	*/
	I2CTL |= bm400KHZ;
	TD_Init();
	
#ifdef DEBUG
	usartInit();
	{
		const uint8 *s = dev_strings;
		uint8 len;
		s = s + *s;
		len = (*s)/2 - 1;
		s += 2;
		while ( len ) {
			usartSendByte(*s);
			s += 2;
			len--;
		}
		usartSendByte(' ');
		len = (*s)/2 - 1;
		s += 2;
		while ( len ) {
			usartSendByte(*s);
			s += 2;
			len--;
		}
		usartSendByte('\r');
	}
#endif
}
Beispiel #9
0
kal_bool BT_PcmLoopbackTest(void)
{
   kal_int16 i, count = 0;
   kal_uint16 *buf;
   kal_uint32 len = 0, acmLen = 0;
   kal_int32 magDB = 0;
   TD_Handle hdl;
   kal_uint16 *rb_base;
   
   AM_BluetoothOn(2);//2:open earphone mode, 1: open cordless mode
   AM_FlushQFunction();
   
   kal_prompt_trace(MOD_L1SP, "after set,DP_VOL_OUT_PCM= %d",*DP_VOL_OUT_PCM);
   
   /*initialize*/
   rb_base = (kal_uint16 *)get_ctrl_buffer(BTLB_BUF_LEN << 1);
   Media_SetBuffer( rb_base, BTLB_BUF_LEN );
   TD_Init( &hdl, TARGET_TONE, BLOCK_SIZE);

#ifdef BTLB_DEBUG
   pcmDebugCnt = 0;
   pcmDebugPtr = btlbPcmDebugBuf;
#endif
   /*start tone_play and record*/
   
   KT_Play(TARGET_TONE, 0, 0); /*play single tone , continuous play*/
   Media_Record(MEDIA_FORMAT_PCM_8K, BTLB_Handler, (void *)0);

   for (i = 0 ; i < 100 ; i++)/*if delay is large, we should enlarge this waiting*/
   {
      Media_GetReadBuffer(&buf ,&len);
      if ( len==0 )
         kal_sleep_task(1);
      else{
         if ( (len + acmLen) >= BLOCK_SIZE ){/*acumulated length >= BLOCK_SIZE*/
            len = BLOCK_SIZE - acmLen;/*recount samples to fit block*/
         }
         TD_ProcessBlock(&hdl, (kal_int16 *)buf, len);
#ifdef BTLB_DEBUG
{
         int j;
         for(j = 0 ; j < len ; j++ )
            if( pcmDebugCnt < 80000 )
            {
               *pcmDebugPtr++ = buf[j];
               pcmDebugCnt ++;
            }
}
#endif
         Media_ReadDataDone( len );
         acmLen += len;
         if ( acmLen >= BLOCK_SIZE){
            magDB = TD_GetMagnitudeDB( &hdl );
            kal_prompt_trace( MOD_L1SP, "magDB = %d", magDB);
            if( magDB > BTLB_TONE_MAG_DB_THRESHOLD )
              count ++;
            TD_Reset( &hdl );
            acmLen=0;//reset
         
            if ( count > 2)
               break;
         }
      }
   }

#ifdef BTLB_DEBUG
{
      int file_handle;
      kal_prompt_trace(MOD_L1SP, "prepare to dump pcm file");
      kal_wsprintf(debugFile, "d:\\BTLBdebug.pcm");
      file_handle = FS_Open(debugFile, FS_CREATE);
      if(file_handle > 0) 
      { 
         FS_Write( file_handle, (void *)btlbPcmDebugBuf, pcmDebugCnt << 1, NULL );
         FS_Close(file_handle);
         kal_prompt_trace(MOD_L1SP, "finish dump pcm file BTLBdebug.pcm");
      }
      else{
      	kal_prompt_trace(MOD_L1SP, "unable to dump file");
      }
      
}
#endif
   KT_StopAndWait();
   Media_Stop();
   AM_BluetoothOff();//close earphone mode
   /*close loopback path, should be removed in the future*/
   //*AFE_VAC_CON1 &= ~0x02;
   
   free_ctrl_buffer( rb_base );
   
   if ( count > 2){
      return true;
   }else
      return false;
}