⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 reference about wireless design which is helpful to everyone
💻 C
字号:
#include "cc2511_app_ex_lib_headers.h"
#include "cc2511_usb_library_headers.h"
#include "usb_hid_reports.h"
#include "usb_class_requests.h"


#define KEYBORD_DATA    0
#define MICE_DATA       1
#define DUMMY_PKT       2

KEYBOARD_REPORT __data keyboard;
MOUSE_REPORT __data mouse;

BYTE __xdata rfReceiveBuffer[18];

//For simplicity we make a RF_PACKET pointer to the RX buffer
RF_PACKET __xdata * pRfRxBuffer = (RF_PACKET __xdata *) &rfReceiveBuffer[0];

BOOL packetReceived; //is set to TRUE by rfudHookRfEvent() when a packet is received over RF.

void main(void) {
   BYTE i;

   SET_MAIN_CLOCK_SOURCE(CRYSTAL);
   usbfwInit();//Initialize the USB framework
   usbirqInit(0xFFFF);// Initialize the USB interrupt handler with bit mask containing all processed USBIRQ events

   rfudInit(18);//Initialize the RF framework, buffer size 18. That is 9 byte payload data + 9 byte header and CRC required by the RF framework.
   rfudSetMyAddress(0x12, 0x13, 0x14, 0x01);//set Rf network and device address on dongle.

   INT_GLOBAL_ENABLE(INT_ON);// Enable interrupts


   while (TRUE) {
      //handle USB reset.
      if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_RESET) {
         USBIRQ_CLEAR_EVENTS(0xFFFF);
         usbfwResetHandler();
         USBPOW = USBPOW_SUSPEND_EN;
      }

      // Handle USB packets on EP0
      if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_SETUP) {
         USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_SETUP);
         usbfwSetupHandler();
      }

      // Handle USB suspend
      if (USBIRQ_GET_EVENT_MASK() & USBIRQ_EVENT_SUSPEND) {
         USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_SUSPEND);// Clear USB suspend interrupt
         rfudStopRadio(); // turn off the radio
         usbsuspEnter();// calling this function will take the chip into PM1 until a USB resume is deteceted.
         USBIRQ_CLEAR_EVENTS(USBIRQ_EVENT_RESUME); // Clear USB resume interrupt.
      }

      // Make sure the rf module have a rx buffer, send received packets to USB.
      if(rfudRxBufferNeeded()) {
         if(packetReceived)
         { //if we have received a packet and it is long enough to contain valid data, handle it.
            packetReceived = FALSE;
            if(pRfRxBuffer->data[0] == KEYBORD_DATA)
            {//if packet contain keybord data, send it to USB.
               keyboard.modifiers = pRfRxBuffer->data[1]; //copy data from rx buffer.
               for(i = 0; i < pRfRxBuffer->length - 8; i++) { keyboard.pKeyCodes[i] = pRfRxBuffer->data[i + 2]; }
               hidUpdateKeyboardReport(&keyboard); while (!hidSendKeyboardReport());  //send hid keyboard report
               keyboard.modifiers = 0;//clear hid keyboard report
               for(i = 0; i < 8; i++) { keyboard.pKeyCodes[i] = 0; }
               hidUpdateKeyboardReport(&keyboard); while (!hidSendKeyboardReport());  //send hid keyboard report
            }
            else if(pRfRxBuffer->data[0] == MICE_DATA)
            {//if packet contain mice data, send it to USB.
               mouse.buttons = pRfRxBuffer->data[1];//copy data from rx buffer.
               mouse.dX = pRfRxBuffer->data[2];
               mouse.dY = pRfRxBuffer->data[3];
               mouse.dZ = pRfRxBuffer->data[4];
               hidUpdateMouseReport(&mouse); //send hid mice report
               while (!hidSendMouseReport());
            }
         }
         rfudStartReceive(pRfRxBuffer);//give rf module a rx buffer
      }
   }
}



// ****************************************************************************************
// All Hooks and functions required by the RF library.
// ****************************************************************************************
BYTE rfudHookBindRequest(BYTE preferredAddress) {
   //We accept all bind requests and assign address 0x42
   return 0x42;
}

BOOL rfudHookAckPkt(BYTE sourceAddress, BYTE __xdata * ackPktflag){
   //if packet is from address 0x42 we send ack
   if(sourceAddress == 0x42) { return TRUE; }
   else { return FALSE; }
}

RF_PACKET __xdata * rfudHookDataPktRequest(BYTE sourceAddress) {
   //we have no data to send to remote unit so we reject all data requests
   return NULL;
}

void rfudHookRfEvent(BYTE rfEvent, BYTE eventData){
   switch (rfEvent)
   {
   case RFUD_EVENT_DATA_PACKET_RECEIVED :
      if((eventData == 0x42) && (pRfRxBuffer->length > 7))
      {//if packet is from address 0x42, and long enough to be valid
         packetReceived = TRUE; //we set packetReceived to TRUE to notify the main loop.
      }
      break;
   case RFUD_EVENT_ACK_RECEIVED ://dont care
      break;
   case RFUD_EVENT_NACK_RECEIVED ://dont care
      break;
   case RFUD_EVENT_ACK_TIMEOUT ://dont care
      break;
   }
}

// ****************************************************************************************
// All Hooks and functions required by the USB library.
// ****************************************************************************************

// **************** Process USB class requests with OUT data phase ************************
void usbcrHookProcessOut(void) {
   // Process USB class requests with OUT data phase, or stall endpoint 0 when unsupported
   //usbfwData.ep0Status = EP_STALL;
    switch (usbSetupHeader.request) {
    case SET_REPORT:    usbcrSetReport(); break;
    case SET_PROTOCOL:  usbcrSetProtocol(); break;
    case SET_IDLE:      usbcrSetIdle(); break;
    default:            usbfwData.ep0Status = EP_STALL; break;
    }
}

// **************** Process USB class requests with IN data phase *************************
void usbcrHookProcessIn(void) {
   // Process USB class requests with IN data phase, or stall endpoint 0 when unsupported
   switch (usbSetupHeader.request) {
    case GET_REPORT:    usbcrGetReport(); break;
    case GET_PROTOCOL:  usbcrGetProtocol(); break;
    case GET_IDLE:      usbcrGetIdle(); break;
    default:            usbfwData.ep0Status = EP_STALL; break;
    }
}

// ********************************  Unsupported USB hooks ********************************
void usbvrHookProcessOut(void) {usbfwData.ep0Status = EP_STALL; }
void usbvrHookProcessIn(void) {usbfwData.ep0Status = EP_STALL; }

// ************************  unsupported/unhandled standard requests **********************
void usbsrHookSetDescriptor(void) { usbfwData.ep0Status = EP_STALL; }
void usbsrHookSynchFrame(void) { usbfwData.ep0Status = EP_STALL; }
void usbsrHookClearFeature(void) { usbfwData.ep0Status = EP_STALL; }
void usbsrHookSetFeature(void) { usbfwData.ep0Status = EP_STALL; }
void usbsrHookModifyGetStatus(BYTE recipient, BYTE index, WORD __xdata *pStatus) { }

// ************************ USB standard request event processing *************************
void usbsrHookProcessEvent(BYTE event, UINT8 index) {
   // Process relevant events, one at a time.
   switch (event) {
   case USBSR_EVENT_CONFIGURATION_CHANGING : //(the device configuration is about to change)
      break;
   case USBSR_EVENT_CONFIGURATION_CHANGED :// (the device configuration has changed)
      break;
   case USBSR_EVENT_INTERFACE_CHANGING ://(the alternate setting of the given interface is about to change)
      break;
   case USBSR_EVENT_INTERFACE_CHANGED : //(the alternate setting of the given interface has changed)
      break;
   case USBSR_EVENT_REMOTE_WAKEUP_ENABLED ://(remote wakeup has been enabled by the host)
      break;
   case USBSR_EVENT_REMOTE_WAKEUP_DISABLED ://(remote wakeup has been disabled by the host)
      break;
   case USBSR_EVENT_EPIN_STALL_CLEARED ://(the given IN endpoint's stall condition has been cleared the host)
      break;
   case USBSR_EVENT_EPIN_STALL_SET ://(the given IN endpoint has been stalled by the host)
      break;
   case USBSR_EVENT_EPOUT_STALL_CLEARED ://(the given OUT endpoint's stall condition has been cleared the host)
      break;
   case USBSR_EVENT_EPOUT_STALL_SET ://(the given OUT endpoint has been stalled by the PC)
      break;
   }
}

// ************************ USB interrupt event processing ********************************
void usbirqHookProcessEvents(void) {
    // Handle events that require immediate processing here
}







⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -