Example #1
0
void check_usb_connection()
{
	int vbus = AT91C_BASE_PIOA->PIO_PDSR & VBUS;
	if (vbus && usb_state == USB_Disconnected)
	{
		// Initialize the USB controller.
		// This will disconnect from the host (important if we have just reset after reprogramming).
		// The host will notice disconnection while we check the FPGA below.
		CDCDSerialDriver_Initialize();
		USBD_Connect();
		usb_state = USB_Connected;
	}
	if (!vbus)
	{
		usb_state = USB_Disconnected;
	}
	
	if (usb_state == USB_Connected && USBD_GetState() >= USBD_STATE_CONFIGURED)
	{
		console_init();
		usb_state = USB_Working;
	}
	
	if (usb_state == USB_Working)
	{
		if (!console_run())
		{
			controller = 0;
		}
	}
}
Example #2
0
void usb_init() {
  // If they are present, configure Vbus & Wake-up pins
  unsigned int priority = 0;
  #ifdef AT91C_ID_PIOD_E
    AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOD_E;
    AT91C_BASE_PIOD->PIO_ISR;
    AT91C_BASE_PIOD->PIO_IDR = 0xFFFFFFFF;
    AT91C_BASE_PIOE->PIO_ISR;
    AT91C_BASE_PIOE->PIO_IDR = 0xFFFFFFFF;
    IRQ_ConfigureIT(AT91C_ID_PIOD_E, priority, PIO_IT_InterruptHandler);
    IRQ_EnableIT(AT91C_ID_PIOD_E);
  #endif

  // If there is on board power, switch it off
  #ifdef PIN_USB_POWER_ENB
  { const Pin pinUsbPwr = PIN_USB_POWER_ENB;
    PIO_Configure(&pinUsbPwr, 1);
  }
  #endif
  
  // BOT driver initialization
  CDCDSerialDriver_Initialize();

  // connect if needed
  VBUS_CONFIGURE();

  // Connect pull-up, wait for configuration
  USBD_Connect();
}
Example #3
0
//------------------------------------------------------------------------------
/// Initializes drivers and start the USB <-> Serial bridge.
//------------------------------------------------------------------------------
void samserial_init()
{
    //TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
//    printf("-- USB Device CDC Serial Project %s --\n\r", SOFTPACK_VERSION);
//    printf("-- %s\n\r", BOARD_NAME);
//    printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);
    // If they are present, configure Vbus & Wake-up pins
    PIO_InitializeInterrupts(0);


    CDCDSerialDriver_Initialize();

    // connect if needed
    VBUS_CONFIGURE();
    // Connect pull-up, wait for configuration
    USBD_Connect();
    
    // Driver loop
        while (USBD_GetState() < USBD_STATE_CONFIGURED) {
            if (isSerialConnected)
                isSerialConnected = 0;
        }
        isSerialConnected = 1;
        // Start receiving data on the USB
        CDCDSerialDriver_Read(usbBuffer,DATABUFFERSIZE,(TransferCallback) UsbDataReceived,0);
       
}
Example #4
0
UsbSerial::UsbSerial( )
{
  CDCDSerialDriver_Initialize();
  USBD_Connect();
  readSemaphore.take( );
  justGot = 0;
  rxBufCount = 0;
}
Example #5
0
void UsbSerial_begin( void )
{
  USBD_Disconnect(); // tell the host we're not attached
  CDCDSerialDriver_Initialize();
  USBD_Connect();
  usbSerial.readSemaphore = SemaphoreCreate();
  SemaphoreTake( usbSerial.readSemaphore, -1 );
  usbSerial.writeSemaphore = SemaphoreCreate();
  SemaphoreTake( usbSerial.writeSemaphore, -1 );
}
Example #6
0
static void prvCDCInit( void )
{
extern WEAK const USBDDriverDescriptors cdcdSerialDriverDescriptors;

	/* If they are present, configure Vbus & Wake-up pins */
	PIO_InitializeInterrupts( 0 );

	/* CDC serial driver initialization */
	CDCDSerialDriver_Initialize( &cdcdSerialDriverDescriptors );

	/* Configure VBus pins and interrupts, and check for connection. */
	prvConfigureVBus();
}
Example #7
0
/**
 * @brief Initializes USB 0
 *
 * This function initializes the USB0 Channel and the ring buffer parameters.
 *
 */
void sio_usb_0_init(void)
{
    usb_0_buffer.tx_buf_head = 0;
    usb_0_buffer.tx_buf_tail = 0;
    usb_0_buffer.tx_count = 0;

    usb_0_buffer.rx_buf_head = 0;
    usb_0_buffer.rx_buf_tail = 0;
    usb_0_buffer.rx_count = 0;

    usb_0_rx_disable_flag = 1; //disable rx for startup

    /* CDC serial driver initialization */
    CDCDSerialDriver_Initialize();

    /* connect if needed */
    VBUS_CONFIGURE();

    while (USBD_GetState() < USBD_STATE_CONFIGURED)
        ;

    CDCDSerialDriver_Read(usb_0_rx_temp_buf, sizeof(usb_0_rx_temp_buf),
            (TransferCallback) usb0_rx_complete_handler, 0);
}
Example #8
0
//------------------------------------------------------------------------------
/// Initializes drivers and start the USB <-> Serial bridge.
//------------------------------------------------------------------------------
int main()
{
    TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
    TRACE_INFO("-- USB Device CDC Serial Project %s --\n\r", SOFTPACK_VERSION);
    TRACE_INFO("-- %s\n\r", BOARD_NAME);
    TRACE_INFO("-- Compiled: %s %s --\n\r", __DATE__, __TIME__);

    // If they are present, configure Vbus & Wake-up pins
    PIO_InitializeInterrupts(0);

    // Configure USART
    PIO_Configure(pins, PIO_LISTSIZE(pins));
    AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_US0;
    AT91C_BASE_US0->US_IDR = 0xFFFFFFFF;
    USART_Configure(AT91C_BASE_US0,
                    USART_MODE_ASYNCHRONOUS,
                    115200,
                    BOARD_MCK);
    USART_SetTransmitterEnabled(AT91C_BASE_US0, 1);
    USART_SetReceiverEnabled(AT91C_BASE_US0, 1);
    AIC_ConfigureIT(AT91C_ID_US0, 0, ISR_Usart0);
    AIC_EnableIT(AT91C_ID_US0);

    // Configure timer 0
    AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC0);
    AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
    AT91C_BASE_TC0->TC_IDR = 0xFFFFFFFF;
    AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV5_CLOCK
                             | AT91C_TC_CPCSTOP
                             | AT91C_TC_CPCDIS
                             | AT91C_TC_WAVESEL_UP_AUTO
                             | AT91C_TC_WAVE;
    AT91C_BASE_TC0->TC_RC = 0x00FF;
    AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS;
    AIC_ConfigureIT(AT91C_ID_TC0, 0, ISR_Timer0);
    AIC_EnableIT(AT91C_ID_TC0);

    // BOT driver initialization
    CDCDSerialDriver_Initialize();

    // connect if needed
    VBus_Configure();

    // Driver loop
    while (1) {

        // Device is not configured
        if (USBD_GetState() < USBD_STATE_CONFIGURED) {

            // Connect pull-up, wait for configuration
            USBD_Connect();
            while (USBD_GetState() < USBD_STATE_CONFIGURED);

            // Start receiving data on the USART
            usartCurrentBuffer = 0;
            USART_ReadBuffer(AT91C_BASE_US0, usartBuffers[0], DATABUFFERSIZE);
            USART_ReadBuffer(AT91C_BASE_US0, usartBuffers[1], DATABUFFERSIZE);
            AT91C_BASE_US0->US_IER = AT91C_US_ENDRX
                                     | AT91C_US_FRAME
                                     | AT91C_US_OVER;
            AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;

            // Start receiving data on the USB
            CDCDSerialDriver_Read(usbBuffer,
                                  DATABUFFERSIZE,
                                  (TransferCallback) UsbDataReceived,
                                  0);
        }
        if( USBState == STATE_SUSPEND ) {
            TRACE_DEBUG("suspend  !\n\r");
            LowPowerMode();
            USBState = STATE_IDLE;
        }
        if( USBState == STATE_RESUME ) {
            // Return in normal MODE
            TRACE_DEBUG("resume !\n\r");
            NormalPowerMode();
            USBState = STATE_IDLE;
        }
    }
}
Example #9
0
//------------------------------------------------------------------------------
/// Application entry point. Configures the DBGU, PIT, TC0, LEDs and buttons
/// and makes LED\#1 blink in its infinite loop, using the Wait function.
/// \return Unused (ANSI-C compatibility).
//------------------------------------------------------------------------------
int main(void)
{


  // DBGU configuration
  TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK);
  TRACE_INFO_WP("\n\r");
  TRACE_INFO("Getting new Started Project --\n\r");
  TRACE_INFO("%s\n\r", BOARD_NAME);
  TRACE_INFO("Compiled: %s %s --\n\r", __DATE__, __TIME__);

  //Configure Reset Controller
  AT91C_BASE_RSTC->RSTC_RMR= 0xa5<<24;

  // Configure EMAC PINS
  PIO_Configure(emacRstPins, PIO_LISTSIZE(emacRstPins));

  // Execute reset
  RSTC_SetExtResetLength(0xd);
  RSTC_ExtReset();

  // Wait for end hardware reset
  while (!RSTC_GetNrstLevel());

  TRACE_INFO("init Flash\n\r");
  flash_init();

  TRACE_INFO("init Timer\n\r");
  // Configure timer 0
  ticks=0;
  extern void ISR_Timer0();
  AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC0);
  AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS;
  AT91C_BASE_TC0->TC_IDR = 0xFFFFFFFF;
  AT91C_BASE_TC0->TC_SR;
  AT91C_BASE_TC0->TC_CMR = AT91C_TC_CLKS_TIMER_DIV5_CLOCK | AT91C_TC_CPCTRG;
  AT91C_BASE_TC0->TC_RC = 375;
  AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS;
  AIC_ConfigureIT(AT91C_ID_TC0, AT91C_AIC_PRIOR_LOWEST, ISR_Timer0);
  AIC_EnableIT(AT91C_ID_TC0);
  AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;

  // Configure timer 1
  extern void ISR_Timer1();
  AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_TC1);
  AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS;	//Stop clock
  AT91C_BASE_TC1->TC_IDR = 0xFFFFFFFF;		//Disable Interrupts
  AT91C_BASE_TC1->TC_SR;						//Read Status register
  AT91C_BASE_TC1->TC_CMR = AT91C_TC_CLKS_TIMER_DIV4_CLOCK | AT91C_TC_CPCTRG;  // Timer1: 2,666us = 48MHz/128
  AT91C_BASE_TC1->TC_RC = 0xffff;
  AT91C_BASE_TC1->TC_IER = AT91C_TC_CPCS;
  AIC_ConfigureIT(AT91C_ID_TC1, 1, ISR_Timer1);
  AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG;

  led_init();

  TRACE_INFO("init EEprom\n\r");
  eeprom_init();

  rb_reset(&TTY_Rx_Buffer);
  rb_reset(&TTY_Tx_Buffer);

  input_handle_func = analyze_ttydata;

  LED_OFF();
  LED2_OFF();
  LED3_OFF();

  spi_init();
  fht_init();
  tx_init();

  #ifdef HAS_ETHERNET

  ethernet_init();

  #endif

  TRACE_INFO("init USB\n\r");
  CDCDSerialDriver_Initialize();
  USBD_Connect();

  wdt_enable(WDTO_2S);

  fastrf_on=0;

  display_channel = DISPLAY_USB;

  TRACE_INFO("init Complete\n\r");

  checkFrequency();

  // Main loop
  while (1) {

    CDC_Task();
    Minute_Task();
    RfAnalyze_Task();

    #ifdef HAS_FASTRF
      FastRF_Task();
    #endif
    #ifdef HAS_RF_ROUTER
      rf_router_task();
    #endif
    #ifdef HAS_ASKSIN
      rf_asksin_task();
    #endif
    #ifdef HAS_MORITZ
      rf_moritz_task();
    #endif
    #ifdef HAS_RWE
      rf_rwe_task();
    #endif
    #ifdef HAS_MBUS
      rf_mbus_task();
    #endif
    #ifdef HAS_MAICO
      rf_maico_task();
    #endif

    #ifdef HAS_ETHERNET
      Ethernet_Task();
    #endif

#ifdef DBGU_UNIT_IN
    if(DBGU_IsRxReady()){
      unsigned char volatile * const ram = (unsigned char *) AT91C_ISRAM;
      unsigned char x;

      x=DBGU_GetChar();
      switch(x) {

      case 'd':
        puts("USB disconnect\n\r");
        USBD_Disconnect();
        break;
      case 'c':
        USBD_Connect();
        puts("USB Connect\n\r");
        break;
      case 'r':
        //Configure Reset Controller
        AT91C_BASE_RSTC->RSTC_RMR=AT91C_RSTC_URSTEN | 0xa5<<24;
        break;
      case 'S':
        USBD_Disconnect();

        my_delay_ms(250);
        my_delay_ms(250);

        //Reset
        *ram = 0xaa;
        AT91C_BASE_RSTC->RSTC_RCR = AT91C_RSTC_PROCRST | AT91C_RSTC_PERRST | AT91C_RSTC_EXTRST   | 0xA5<<24;
        while (1);
        break;
      default:
        rb_put(&TTY_Tx_Buffer, x);
      }
    }
#endif

    if (USBD_GetState() == USBD_STATE_CONFIGURED) {
      if( USBState == STATE_IDLE ) {
        CDCDSerialDriver_Read(usbBuffer,
                              DATABUFFERSIZE,
                              (TransferCallback) UsbDataReceived,
                              0);
        LED3_ON();
        USBState=STATE_RX;
      }
    }
    if( USBState == STATE_SUSPEND ) {
      TRACE_INFO("suspend  !\n\r");
      USBState = STATE_IDLE;
    }
    if( USBState == STATE_RESUME ) {
      TRACE_INFO("resume !\n\r");
      USBState = STATE_IDLE;
    }

  }
}
Example #10
0
File: main.c Project: gstroe/Arm
/*! \brief Main function. Execution starts here.
 */
int main(void)
{
	uint8_t isUsbConnected = 0;
	/* Disable watchdog */
	WDT_Disable(WDT);


	SCB_EnableICache();
	SCB_EnableDCache();

	/* Output example information */
	printf("-- USB Device CDC Serial Project %s --\n\r", SOFTPACK_VERSION);
	printf("-- %s\n\r", BOARD_NAME);
	printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ ,
			COMPILER_NAME);

	/* Initialize PIO interrupts */
	PIO_InitializeInterrupts(0);

	/* Interrupt priority */
	NVIC_SetPriority(USBHS_IRQn, 2);

	/* Configure DMA driver */
	_ConfigureDma();

	/* Configure USART */
	_ConfigureUsart();
	_UsartDmaRxSetup();

	/* Initialize OTG clocks */
	_ConfigureUotghs();

	/* CDC serial driver initialization */
	CDCDSerialDriver_Initialize(&cdcdSerialDriverDescriptors);

	/* Help information */
	_DebugHelp();

	// Start USB stack to authorize VBus monitoring
	USBD_Connect();

	/* Driver loop */
	while (1) {
		/* Device is not configured */
		if (USBD_GetState() < USBD_STATE_CONFIGURED) {
			if (isUsbConnected) {
				isUsbConnected = 0;
				isCdcSerialON  = 0;
			}
		} else if (isUsbConnected == 0)
			isUsbConnected = 1;

		/* Serial port ON/OFF */
		if (CDCDSerialDriver_GetControlLineState() & CDCControlLineState_DTR) {
			if (!isCdcSerialON) {
				isCdcSerialON = 1;

				/* Start receiving data on the USART */
				_UsartDmaRx();
				USART_EnableIt(BASE_USART, US_CSR_FRAME | US_CSR_OVRE | US_IER_TIMEOUT);
				USART_EnableRecvTimeOut(BASE_USART, USART_TIMEOUT);
				/* Start receiving data on the USB */
				CDCDSerialDriver_Read(usbBuffer,
									  DATAPACKETSIZE,
									  (TransferCallback) _UsbDataReceived,
									  0);
			}
		} else if (isCdcSerialON)
			isCdcSerialON = 0;

		if (DBG_IsRxReady()) {
			uint8_t key = DBG_GetChar();

			/* ESC: CDC Echo ON/OFF */
			if (key == 27) {
				printf("** CDC Echo %s\n\r",
					   isCdcEchoON ? "OFF" : "ON");
				isCdcEchoON = !isCdcEchoON;
			}
			/* 't': Test CDC writing  */
			else if (key == 't')
				_SendText();
			else {
				printf("Alive\n\r");

				while (CDCDSerialDriver_Write((char *)"Alive\n\r", 8, 0, 0)
					   != USBD_STATUS_SUCCESS);

				_DebugHelp();
			}
		}
	}
}