/** * Initializes the system, connects the USB and waits indefinitely. * * \callgraph */ int main(void) { /* Disable watchdog */ WDT_Disable( WDT ); /* Output example information */ printf( "-- USB Device Core 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) ; /* Enable UPLL for USB */ _ConfigureUsbClock(); /* USB initialization, Disable Pull-up */ TRACE_INFO("USB initialization\n\r"); USBDDriver_Initialize(&usbdDriver, &usbdDriverDescriptors, 0); USBD_Init(); /* Wait about 10ms so that host detach the device to re-enumerate Device connection */ TRACE_INFO("Connecting device\n\r"); /* connect if needed */ VBus_Configure(); while (USBD_GetState() < USBD_STATE_CONFIGURED); // Infinite loop while (1) { } }
void usb_routines() { // Device is not configured if (USBD_GetState() < USBD_STATE_CONFIGURED) { if (isSerialConnected) { isSerialConnected = 0; AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; } } else if (!isSerialConnected) { isSerialConnected = 1; // Start receiving data on the USB CDCDSerialDriver_Read(usbBuffer, DATABUFFERSIZE, (TransferCallback) UsbDataReceived, 0); } else if (isSerialConnected) { CDCDSerialDriver_Read(usbBuffer, DATABUFFERSIZE, (TransferCallback) UsbDataReceived, 0); } if( USBState == STATE_SUSPEND ) { USBState = STATE_IDLE; LowPowerMode(); } if( USBState == STATE_RESUME ) { USBState = STATE_IDLE; // Return in normal MODE NormalPowerMode(); } }
/** Read data from a USB host. This will read up to 64 bytes of data at a time, as this is the maximum USB transfer for the Make Controller internally. If you want to read more than that, keep calling read until you've got what you need. If nothing is ready to be read, this will not return until new data arrives. @param buffer Where to store the incoming data. @param length How many bytes to read. 64 is the max that can be read at one time. @param timeout The number of milliseconds to wait if no data is available. -1 means wait forever. @return The number of bytes successfully read. \b Example \code char mydata[128]; UsbSerial* usb = UsbSerial::get(); // get a reference to the usb system // simplest is reading a short chunk int read = usb->read(mydata, 20); // or, we can wait until we've read more than the maximum of 64 bytes int got_so_far = 0; while(got_so_far < 128) // wait until we've read 128 bytes { int read = usb->read(mydata, (128 - got_so_far)); // read some new data got_so_far += read; // add to how much we've gotten so far } \endcode */ int UsbSerial::read( char *buffer, int length, int timeout ) { if( USBD_GetState() != USBD_STATE_CONFIGURED ) return 0; int length_to_go = length; if( rxBufCount ) // do we already have some lying around? { int copylen = (rxBufCount > length_to_go) ? length_to_go : rxBufCount; memcpy( buffer, rxBuf, copylen ); buffer += copylen; rxBufCount -= copylen; length_to_go -= copylen; } if(length_to_go) // if we still would like to get more { unsigned char result = USBD_Read(CDCDSerialDriverDescriptors_DATAOUT, rxBuf, USB_SER_RX_BUF_LEN, onUsbData, this); if(result == USBD_STATUS_SUCCESS) { if( readSemaphore.take( timeout ) ) { int copylen = (justGot > length_to_go) ? length_to_go : justGot; memcpy( buffer, rxBuf, copylen ); buffer += copylen; rxBufCount -= copylen; length_to_go -= copylen; justGot = 0; } } } return length - length_to_go; }
//------------------------------------------------------------------------------ /// 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); }
/** Read data from a USB host. This will read up to 64 bytes of data at a time, as this is the maximum USB transfer for the Make Controller internally. If you want to read more than that, keep calling read until you've got what you need. If nothing is ready to be read, this will not return until new data arrives. @param buffer Where to store the incoming data. @param length How many bytes to read. 64 is the max that can be read at one time. @param timeout The number of milliseconds to wait if no data is available. -1 means wait forever. @return The number of bytes successfully read. \b Example \code char mydata[128]; // simplest is reading a short chunk int read = UsbSerial_read(mydata, 20); // or, we can wait until we've read more than the maximum of 64 bytes int got_so_far = 0; while(got_so_far < 128) // wait until we've read 128 bytes { int read = UsbSerial_read(mydata, (128 - got_so_far)); // read some new data got_so_far += read; // add to how much we've gotten so far } \endcode */ int UsbSerial_read( char *buffer, int length, int timeout ) { if( USBD_GetState() != USBD_STATE_CONFIGURED ) return 0; int length_to_go = length; if( usbSerial.rxBufCount ) { // do we already have some lying around? int copylen = MIN(usbSerial.rxBufCount, length_to_go); memcpy( buffer, usbSerial.rxBuf, copylen ); buffer += copylen; usbSerial.rxBufCount -= copylen; length_to_go -= copylen; } if(length_to_go) { // if we still would like to get more unsigned char result = USBD_Read(CDCDSerialDriverDescriptors_DATAOUT, usbSerial.rxBuf, USBSER_MAX_READ, UsbSerial_onUsbData, 0); if(result == USBD_STATUS_SUCCESS) { if( SemaphoreTake( usbSerial.readSemaphore, timeout ) ) { int copylen = MIN(usbSerial.justGot, length_to_go); memcpy( buffer, usbSerial.rxBuf, copylen ); buffer += copylen; usbSerial.rxBufCount -= copylen; length_to_go -= copylen; usbSerial.justGot = 0; } } } return length - length_to_go; }
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; } } }
//------------------------------------------------------------------------------ /// Invoked when the USB device gets suspended. By default, turns off all LEDs. //------------------------------------------------------------------------------ void USBDCallbacks_Suspended(void) { // Turn off LEDs LED_Clear(USBD_LEDPOWER); LED_Clear(USBD_LEDUSB); if (USBD_GetState() >= USBD_STATE_CONFIGURED) USBState = STATE_SUSPEND; }
/* user API: requests us to start transmitting data via USB IN EP */ void fastsource_start(void) { if(USBD_GetState() != USBD_STATE_CONFIGURED) return; if (!usb_state.active) refill_dma(); }
/** * \brief usb_massstorage Application entry point. * * Configures UART, * Configures TC0, USB MSD Driver and run it. * * \return Unused (ANSI-C compatibility). */ int main( void ) { sSdCard *pSd = 0; /* Disable watchdog */ WDT_Disable( WDT ) ; SCB_EnableICache(); SCB_EnableDCache(); #if defined LUN_RAMDISK /* Enable SDRAM */ BOARD_ConfigureSdram(); #endif TRACE_INFO("-- USB Device Mass Storage Example %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); /* Initialize all USB power (off) */ _ConfigureUotghs(); /* Initialize PIO pins */ _ConfigurePIOs(); /* Initialize drivers */ _ConfigureDrivers(); _MemoriesInitialize(pSd); /* BOT driver initialization */ MSDDriver_Initialize(&msdDriverDescriptors, luns, MAX_LUNS); /* connect if needed */ USBD_Connect(); while (1) { /* Mass storage state machine */ if (USBD_GetState() < USBD_STATE_CONFIGURED){} else { MSDDriver_StateMachine(); if (msdRefresh) { msdRefresh = 0; if (msdWriteTotal < 50 * 1000) { /* Flush Disk Media */ } msdWriteTotal = 0; } } } }
/** Write data to a USB host. @param buffer The data to send. @param length How many bytes to send. @return The number of bytes successfully written. \b Example \code UsbSerial* usb = UsbSerial::get(); // get a reference to the usb system int written = usb->write( "hi hi", 5 ); \endcode */ int UsbSerial::write( const char *buffer, int length ) { int rv = 0; if( USBD_GetState() == USBD_STATE_CONFIGURED ) { if( USBD_Write(CDCDSerialDriverDescriptors_DATAIN, buffer, length, 0, 0) == USBD_STATUS_SUCCESS ) rv = length; } return rv; }
/** * Sends the current configuration number to the host. * \param pDriver Pointer to a USBDDriver instance. */ static void GetConfiguration(const USBDDriver *pDriver) { unsigned long tmp; // Coud be unsigned char : unsigned long has been chose to avoid any potential alignment issue with DMA if( USBD_GetState() < USBD_STATE_CONFIGURED) tmp = 0; // If device is unconfigured, returned configuration must be 0 else tmp = pDriver->cfgnum; USBD_Write(0, &tmp, 1, 0, 0); }
/** Write data to a USB host. @param buffer The data to send. @param length How many bytes to send. @return The number of bytes successfully written. \b Example \code int written = UsbSerial_write( "hi hi", 5 ); \endcode */ int UsbSerial_write( const char *buffer, int length, int timeout ) { int rv = 0; if( USBD_GetState() == USBD_STATE_CONFIGURED ) { if( USBD_Write(CDCDSerialDriverDescriptors_DATAIN, buffer, length, UsbSerial_onUsbWritten, 0) == USBD_STATUS_SUCCESS ) { if( SemaphoreTake( usbSerial.writeSemaphore, timeout ) ) { rv = usbSerial.justWrote; usbSerial.justWrote = 0; } } } return rv; }
//------------------------------------------------------------------------------ /// Initializes the CCID driver and runs it. /// \return Unused (ANSI-C compatibility) //------------------------------------------------------------------------------ int main( void ) { // Initialize traces TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK); printf("-- USB Device CCID 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); // Configure IT on Smart Card ConfigureCardDetection(); // Configure ISO7816 driver PIO_Configure(pinsISO7816, PIO_LISTSIZE(pinsISO7816)); PIO_Configure(pinsPower, PIO_LISTSIZE(pinsPower)); /* power up the card */ PIO_Set(&pinsPower[0]); ISO7816_Init( pinIso7816RstMC ); // USB audio driver initialization CCIDDriver_Initialize(); // connect if needed VBUS_CONFIGURE(); while (USBD_GetState() < USBD_STATE_CONFIGURED); CCID_Insertion(); // Infinite loop while (1) { 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; } CCID_SmartCardRequest(); } return 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); }
/** * Initializes drivers and start the USB CDCHID device. */ int main(void) { uint8_t usbConnected = 0, serialON = 0; /* Disable watchdog */ WDT_Disable( WDT ); printf("-- USB CDCHID Device 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); /* If there is on board power, switch it off */ /* Enable UPLL for USB */ ConfigureUsbClock(); /* Configure timer 0 */ ConfigureTc0(); /* ----- CDC Function Initialize */ /* Configure USART */ ConfigureUsart(); /* ----- HID Function Initialize */ /* Initialize key statuses and configure push buttons */ PIO_Configure(pinsPushButtons, PIO_LISTSIZE(pinsPushButtons)); memset(keyStatus, 1, NUM_KEYS); /* Configure LEDs */ LED_Configure(LED_NUMLOCK); /* USB CDCHID driver initialization */ CDCHIDDDriver_Initialize(&cdchiddDriverDescriptors); /* connect if needed */ VBus_Configure(); /* Driver loop */ while (1) { /* Device is not configured */ if (USBD_GetState() < USBD_STATE_CONFIGURED) { if (usbConnected) { printf("-I- USB Disconnect/Suspend\n\r"); usbConnected = 0; /* Serial port closed */ isSerialPortON = 0; } } else { if (usbConnected == 0) { printf("-I- USB Connect\n\r"); usbConnected = 1; } if (!serialON && isSerialPortON) { printf("-I- SerialPort ON\n\r"); /* Start receiving data on the USART */ usartCurrentBuffer = 0; USART_ReadBuffer(BASE_USART, usartBuffers[0], DATABUFFERSIZE); USART_ReadBuffer(BASE_USART, usartBuffers[1], DATABUFFERSIZE); BASE_USART->US_IER = US_CSR_ENDRX | US_CSR_FRAME | US_CSR_OVRE; /* Start receiving data on the USB */ CDCDSerial_Read(usbSerialBuffer0, DATABUFFERSIZE, (TransferCallback) UsbDataReceived, 0); serialON = 1; } else if (serialON && !isSerialPortON) { printf("-I- SeriaoPort OFF\n\r"); serialON = 0; } HIDDKeyboardProcessKeys(); } } }
//------------------------------------------------------------------------------ /// 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; } } }
//------------------------------------------------------------------------------ /// Handles interrupts coming from USART #0. //------------------------------------------------------------------------------ static void ISR_Usart0() { unsigned int status = AT91C_BASE_US0->US_CSR; unsigned short serialState; // If USB device is not configured, do nothing if (USBD_GetState() != USBD_STATE_CONFIGURED) { AT91C_BASE_US0->US_IDR = 0xFFFFFFFF; return; } // Buffer has been read successfully if ((status & AT91C_US_ENDRX) != 0) { // Disable timer AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS; // Send buffer through the USB while (CDCDSerialDriver_Write(usartBuffers[usartCurrentBuffer], DATABUFFERSIZE, 0, 0) != USBD_STATUS_SUCCESS); // Restart read on buffer USART_ReadBuffer(AT91C_BASE_US0, usartBuffers[usartCurrentBuffer], DATABUFFERSIZE); usartCurrentBuffer = 1 - usartCurrentBuffer; // Restart timer AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN | AT91C_TC_SWTRG; } // Buffer has been sent if ((status & AT91C_US_TXBUFE) != 0) { // Restart USB read CDCDSerialDriver_Read(usbBuffer, DATABUFFERSIZE, (TransferCallback) UsbDataReceived, 0); AT91C_BASE_US0->US_IDR = AT91C_US_TXBUFE; } // Errors serialState = CDCDSerialDriver_GetSerialState(); // Overrun if ((status & AT91C_US_OVER) != 0) { TRACE_WARNING( "ISR_Usart0: Overrun\n\r"); serialState |= CDCDSerialDriver_STATE_OVERRUN; } // Framing error if ((status & AT91C_US_FRAME) != 0) { TRACE_WARNING( "ISR_Usart0: Framing error\n\r"); serialState |= CDCDSerialDriver_STATE_FRAMING; } CDCDSerialDriver_SetSerialState(serialState); }
/** * \brief usb_iad_hid_aud Application entry point. * * Starts the driver and waits for an audio input stream to forward to the DAC. */ int main(void) { volatile uint8_t usbConn = 0; volatile uint8_t audioOn = 0; int32_t numDiff = 0, prevDiff = 0; int8_t clockAdjust = 0; /* Disable watchdog */ WDT_Disable(WDT); SCB_EnableICache(); SCB_EnableDCache(); printf("-- USB HID + Audio Device Example %s --\n\r", SOFTPACK_VERSION); printf("-- %s\n\r", BOARD_NAME); printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME); TimeTick_Configure(); /* Interrupt priority */ NVIC_SetPriority(USBHS_IRQn, 2); /* If they are present, configure Vbus & Wake-up pins */ PIO_InitializeInterrupts(0); /* Initialize all USB power (off) */ _ConfigureUsbhs(); /* ----- HID Function Initialize */ #ifdef NO_PUSHBUTTON printf("-- : DBG key 1 2 used as buttons\n\r"); printf("-- : 1st press to push, 2nd press to release\n\r"); #else /* Initialize key statuses and configure push buttons */ PIO_Configure(pinsPushButtons, PIO_LISTSIZE(pinsPushButtons)); #endif memset(keyStatus, 1, NUM_KEYS); //LED_Configure(LED_NUMLOCK); /* Audio STREAM LED */ LED_Configure(USBD_LEDOTHER); /* Configure Audio */ _ConfigureAudioPlay(AUDDevice_SAMPLERATE, BOARD_MCK); /* Configure DMA */ _ConfigureDma(); /* USB audio driver initialization */ HIDAUDDDriver_Initialize(&hidauddDriverDescriptors); /* connect if needed */ USBD_Connect(); /* Infinite loop */ while (1) { if (USBD_GetState() < USBD_STATE_CONFIGURED) { usbConn = 0; continue; } if (audioOn) { if (isDacActive == 0) { AudioPlayEnable(0); printf("audE "); isFirstFrame = 1; audioOn = 0; } else { numDiff = numBuffersToSend - DAC_DELAY; if (prevDiff != numDiff) { prevDiff = numDiff; if (numDiff > 1 && clockAdjust != 1) { printf("+"); /* USB too fast or SSC too slow: faster clock */ clockAdjust = 1; _SyncAdjust(1); } if (numDiff < -1 && clockAdjust != -1) { printf("-"); /* USB too slow or SSC too fast: slower clock */ clockAdjust = -1; _SyncAdjust(-1); } if (numDiff == 0 && clockAdjust != 0) { clockAdjust = 0; _SyncAdjust(0); } } } } else if (isDacActive) { printf("audS "); audioOn = 1; } if (usbConn == 0) { usbConn = 1; /* Start Reading the incoming audio stream */ AUDDFunction_Read(buffers[inBufferIndex], AUDDevice_BYTESPERFRAME, (TransferCallback) FrameReceived, 0); // No optional argument } HIDDKeyboardProcessKeys(); } }
//------------------------------------------------------------------------------ /// Invoked when the USB device gets suspended. By default, turns off all LEDs. //------------------------------------------------------------------------------ void USBDCallbacks_Suspended(void) { if (USBD_GetState() >= USBD_STATE_CONFIGURED) USBState = STATE_SUSPEND; }
//----------------------------------------------------------------------------- /// State machine for the MSD driver //----------------------------------------------------------------------------- void MSDDriver_StateMachine(void) { if (USBD_GetState() < USBD_STATE_CONFIGURED){} else MSDD_StateMachine(&msdDriver); }
/*! \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(); } } } }
/** * State machine for the MSD driver */ void MSDFunction_StateMachine(void) { if (USBD_GetState() < USBD_STATE_CONFIGURED){} else MSDD_StateMachine(&msdFunction); }
int main(void) { unsigned char bmButtons = 0; TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK); printf("-- USB Device HID Mouse Project %s --\n\r", SOFTPACK_VERSION); printf("-- %s\n\r", BOARD_NAME); printf("-- Compiled: %s %s --\n\r", __DATE__, __TIME__); // SPP + int i; button_flag = 0; //Enable RESET m_pRSTC->RSTC_RCR = 0xA5000008; m_pRSTC->RSTC_RMR = 0xA5000001; Delay(1000); // Init USB device //AT91F_USB_Open(); // Configure the RTT: *AT91C_RTTC_RTMR = BUTTON_SAMPLING; // Set in PIO mode and Configure in Input //AT91F_PIOA_CfgPMC(); // InitMAM InitMMA(); // InitADC ADCInit(); // UART0 Init InitUSART0(); // Stat led s_pPio->PIO_PER = BIT31; // Configure PA31 as output s_pPio->PIO_OER = BIT31; // Set PA31 to HIGH s_pPio->PIO_SODR = BIT31; // Test flash if(TestFlash()) { for(i=0; i<20; i++) { s_pPio->PIO_CODR = BIT31; Delay(150000); s_pPio->PIO_SODR = BIT31; Delay(150000); } } // Button // Configure P20 as input s_pPio->PIO_ODR = BIT20; // Enable s_pPio->PIO_PER = BIT20; Delay(1000); // CALIBRATE WHEN BUTT IS PRESS // while((s_pPio->PIO_PDSR&BIT20)==BIT20); Delay(1000); // SPP - // If they are present, configure Vbus & Wake-up pins PIO_InitializeInterrupts(0); WAKEUP_CONFIGURE(); // 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 // Initialize key statuses and configure push buttons #if defined(at91cap9dk) const Pin pinRow0 = PIN_KEYBOARD_ROW0; PIO_Configure(&pinRow0, 1); #endif PIO_Configure(pinsJoystick, PIO_LISTSIZE(pinsJoystick)); // HID driver initialization HIDDMouseDriver_Initialize(); // connect if needed VBUS_CONFIGURE(); // Infinite loop while (1) { // SPP + #define TIMEOUT 100 static int Timeout = TIMEOUT; GetCoordinates(); base_y = Coordinates[0]/NSAMPLE; base_x = Coordinates[1]/NSAMPLE; base_z = Coordinates[2]/NSAMPLE; // SPP - if( USBState == STATE_SUSPEND ) { TRACE_DEBUG("suspend !\n\r"); USBState = STATE_IDLE; LowPowerMode(); } if( USBState == STATE_RESUME ) { // Return in normal MODE NormalPowerMode(); USBState = STATE_IDLE; TRACE_DEBUG("resume !\n\r"); } if (USBD_GetState() < USBD_STATE_CONFIGURED) continue; if (!Timeout) { unsigned char status; do { status = HIDDMouseDriver_ChangePoints(bmButtons, base_x, base_y); } while (status != USBD_STATUS_SUCCESS); Timeout = TIMEOUT; } else Timeout--; } }
//------------------------------------------------------------------------------ /// 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; } } }
/** Check if the USB system got set up OK. When things are starting up, if you want to wait until the USB is ready, you can use this to check. @return Whether the UsbSerial system is currently running. \b Example \code while( !UsbSerial_isActive() ) // while usb is not active Sleep(10); // wait around for a little bit // now we're ready to go \endcode */ bool UsbSerial_isActive() { return USBD_GetState() == USBD_STATE_CONFIGURED; }
/** * Initializes drivers and start the USB CDCMSD device. */ int main(void) { uint8_t usbConnected = 0, serialON = 0; /* Disable watchdog */ WDT_Disable(WDT); SCB_EnableICache(); SCB_EnableDCache(); printf("-- USB CDCMSD Device Project %s --\n\r", SOFTPACK_VERSION); printf("-- %s\n\r", BOARD_NAME); printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME); /* If they are present, configure Vbus & Wake-up pins */ PIO_InitializeInterrupts(0); /* If there is on board power, switch it off */ _ConfigureUotghs(); /* ----- MSD Function Initialize */ /* Configure memories */ _MemoriesInitialize(); /* USB CDCMSD driver initialization */ CDCMSDDriver_Initialize(&cdcmsddDriverDescriptors, luns, MAX_LUNS); /* connect if needed */ USBD_Connect(); /* Driver loop */ while (1) { /* Device is not configured */ if (USBD_GetState() < USBD_STATE_CONFIGURED) { if (usbConnected) { printf("-I- USB Disconnect/Suspend\n\r"); usbConnected = 0; /* Serial port closed */ isSerialPortON = 0; } } else { if (usbConnected == 0) { printf("-I- USB Connect\n\r"); usbConnected = 1; } if (!serialON && isSerialPortON) { printf("-I- SerialPort ON\n\r"); /* Start receiving data on the USART */ /* Start receiving data on the USB */ CDCDSerial_Read(usbSerialBuffer0, DATAPACKETSIZE, 0, 0); serialON = 1; } else if (serialON && !isSerialPortON) { printf("-I- SeriaoPort OFF\n\r"); serialON = 0; } MSDFunction_StateMachine(); if (msdRefresh) { msdRefresh = 0; if (msdWriteTotal < 50 * 1000) { /* Flush Disk Media */ } msdWriteTotal = 0; } } } }