|
Hello everybody,
in the last days I learned to understand the usb-device-cdc-serial project from Atmel. Most of the parts I have modified for my own requirements, but not all. Actually, my program is checking in the main-loop on the usb-device for new data. My requirement is not a program which checks in every loop for the data, in fact, I will use an interrupt for this. Thus, if data arrives on the usb-device from an external uart-device, an interrupt should be triggered. I don’t understand this completely, apparently. How can I trip the interrupt or how can I configure this interrupt? I also use some other interrupts, but for usb I don’t find an interrupt which can do this – or have I looked wrong? Can anybody help me?
Here is an extract from my code:
//myusb.h #ifndef MYUSB_H_ #define MYUSB_H_
#include <usb/device/cdc-serial/CDCDSerialDriver.h> #include <usb/device/cdc-serial/CDCDSerialDriverDescriptors.h> #include <pio/pio.h> #include <pio/pio_it.h>
//------------------------------------------------------------------------------ // Definitions //------------------------------------------------------------------------------ /// Size in bytes of the buffer used for reading data from the USB & USART #define DATABUFFERSIZE \ (CHIP_USB_ENDPOINTS_MAXPACKETSIZE(CDCDSerialDriverDescriptors_DATAIN)+2)
/// Use for power management #define STATE_IDLE 0 /// The USB device is in suspend state #define STATE_SUSPEND 4 /// The USB device is in resume state #define STATE_RESUME 5
//------------------------------------------------------------------------------ // Internal variables //------------------------------------------------------------------------------
/// State of USB, for suspend and resume extern unsigned char USBState;
/// Buffer for storing incoming USB data. extern unsigned char usbBuffer[DATABUFFERSIZE];
/// CDC Echo back ON/OFF extern unsigned char isCdcEchoON;
/// DBG Port Activity ON/OFF extern unsigned char isDbgStrON;
extern unsigned char count;
// Die Funktionen
#if defined(PIN_USB_VBUS) #define VBUS_CONFIGURE() VBus_Configure() extern const Pin pinVbus; extern void ISR_Vbus(const Pin *pPin); extern void VBus_Configure( void ); #else #define VBUS_CONFIGURE() USBD_Connect() #endif //#if defined(PIN_USB_VBUS)
extern void UsbDataReceived(unsigned int unused, unsigned char status, unsigned int received, unsigned int remaining); extern void USBDCallbacks_Resumed(void); extern void USBDCallbacks_Suspended(void);
#endif /* MYUSB_H_ */
//myusb.c #include "myusb.h" #include "../myprintf/myprintf.h" #include <stdio.h> #include <utility/led.h> #include <utility/trace.h>
unsigned char USBState = STATE_IDLE; unsigned char usbBuffer[DATABUFFERSIZE]; unsigned char isCdcEchoON = 0; unsigned char isDbgStrON = 0; unsigned char count = 0;
//------------------------------------------------------------------------------ // VBus monitoring (optional) //------------------------------------------------------------------------------ #if defined(PIN_USB_VBUS)
#define VBUS_CONFIGURE() VBus_Configure()
/// VBus pin instance. const Pin pinVbus = PIN_USB_VBUS;
//------------------------------------------------------------------------------ /// Handles interrupts coming from PIO controllers. //------------------------------------------------------------------------------ void ISR_Vbus(const Pin *pPin) { // Check current level on VBus if (PIO_Get(&pinVbus)) {
TRACE_INFO("VBUS conn\n\r"); USBD_Connect(); } else {
TRACE_INFO("VBUS discon\n\r"); USBD_Disconnect(); } }
//------------------------------------------------------------------------------ /// Configures the VBus pin to trigger an interrupt when the level on that pin /// changes. //------------------------------------------------------------------------------ void VBus_Configure( void ) { TRACE_INFO("VBus configuration\n\r");
// Configure PIO PIO_Configure(&pinVbus, 1); PIO_ConfigureIt(&pinVbus, ISR_Vbus); PIO_EnableIt(&pinVbus);
// Check current level on VBus if (PIO_Get(&pinVbus)) {
// if VBUS present, force the connect TRACE_INFO("VBUS conn\n\r"); USBD_Connect(); } else { USBD_Disconnect(); } }
#else #define VBUS_CONFIGURE() USBD_Connect() #endif //#if defined(PIN_USB_VBUS)
//------------------------------------------------------------------------------ /// Callback invoked when data has been received on the USB. //------------------------------------------------------------------------------ void UsbDataReceived(unsigned int unused, unsigned char status, unsigned int received, unsigned int remaining) {
// Check that data has been received successfully if (status == USBD_STATUS_SUCCESS) { // Send back CDC data if (isCdcEchoON) { CDCDSerialDriver_Write(usbBuffer, received, 0, 0); } // Debug log: USB->US // DebugLog("\n\r<", usbBuffer, received);
// Send data through USART // while (!USART_WriteBuffer(BASE_USART, usbBuffer, received)); // BASE_USART->US_IER = AT91C_US_TXBUFE; myprintf("%d Empfangen : %s\n\r", count++, usbBuffer);
// Check if bytes have been discarded if ((received == DATABUFFERSIZE) && (remaining > 0)) {
TRACE_WARNING( "UsbDataReceived: %u bytes discarded\n\r", remaining); } } else {
TRACE_WARNING( "UsbDataReceived: Transfer error\n\r"); } }
//------------------------------------------------------------------------------ // Callbacks re-implementation //------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Invoked when the USB device leaves the Suspended state. void USBDCallbacks_Resumed(void) { USBState = STATE_RESUME;
// über USB soll jetzt gequasselt werden. myprintf_mode |= 0x02; TRACE_INFO("USB dran: %d", myprintf_mode); }
// Invoked when the USB device gets suspended. void USBDCallbacks_Suspended(void) {
if (USBD_GetState() >= USBD_STATE_CONFIGURED) USBState = STATE_SUSPEND;
// über USB darf jetzt nicht mehr gesprochen werden myprintf_mode &= 0xFD; TRACE_INFO("USB ab: %d", myprintf_mode); }
Here's the important part from my main-function:
int main(void) {
CDCDSerialDriver_Initialize(); // BOT driver initialization VBUS_CONFIGURE(); // connect if needed isCdcEchoON = 0; // ob ein Echosignal zurück gegeben werden soll
PIOconfigure(); // PIOs konfigurieren
// If they are present, configure Vbus & Wake-up pins PIO_InitializeInterrupts(5);
//.... //here are a lot of other functions which are not important for my problem //....
while(1) {
// Device is not configured if (USBD_GetState() < USBD_STATE_CONFIGURED) { } else { // if (isSerialConnected == 0) {
// Start receiving data on the USB CDCDSerialDriver_Read(usbBuffer, DATABUFFERSIZE, (TransferCallback) UsbDataReceived, 0); }
if( USBState == STATE_SUSPEND ) { USBState = STATE_IDLE; TRACE_DEBUG("suspend !\n\r"); } if( USBState == STATE_RESUME ) { USBState = STATE_IDLE; TRACE_DEBUG("resume !\n\r"); } }
Many thanks in advance, Sebastian
|