📄 usb_dongle.c
字号:
/* Copyright (c) 2007 Nordic Semiconductor. All Rights Reserved.
*
* The information contained herein is confidential property of
* Nordic Semiconductor. The use, copying, transfer or disclosure
* of such information is prohibited except by express written
* agreement with Nordic Semiconductor.
*/
/** @file
* Implementation of the USB interface functions.
*
* @author Runar Kjellhaug
*
* @addtogroup usb_code
* @{
*/
#include "usb_dongle.h"
#include "usb_regs.h"
#include "f3xx_usb0_interruptserviceroutine.h"
#include "f3xx_usb0_reporthandler.h"
#include "wdp_common.h"
#include "fap.h"
extern xdata BufferStructure in_buffer, out_buffer;
extern bool ce_state;
//-----------------------------------------------------------------------------
// Global Variable Declarations
//-----------------------------------------------------------------------------
extern uint8_t EP_STATUS[];
void usb_init(void)
{
POLL_WRITE_BYTE (POWER, 0x08); // Force Asynchronous USB Reset
POLL_WRITE_BYTE (IN1IE, 0x0f); // Enable Endpoint 0-3 in interrupts
POLL_WRITE_BYTE (OUT1IE,0x0f); // Enable Endpoint 0-3 out interrupts
POLL_WRITE_BYTE (CMIE, 0x07); // Enable Reset, Resume, and Suspend
// interrupts
USB0XCN = 0xE0; // Enable transceiver; select full speed
POLL_WRITE_BYTE (CLKREC,0x80); // Enable clock recovery, single-step
// mode disabled
EIP1 |= 0x02; // Give the USB interrupt a higher priority
EIE1 |= 0x02; // Enable USB0 Interrupts
// Enable USB0 by clearing the USB
POLL_WRITE_BYTE (POWER, 0x01); // Inhibit Bit and enable suspend detection
}
//-----------------------------------------------------------------------------
// send_usb_packet
//-----------------------------------------------------------------------------
//
// Return Value - None
// Parameters - Report ID that's used to call the appropriate IN handler
//
// This function can be called by other routines to force an IN packet
// transmit. It takes as an input the Report ID of the packet to be
// transmitted.
//-----------------------------------------------------------------------------
void send_usb_packet(uint8_t report_id)
{
bool ea_state;
uint8_t ControlReg;
uint8_t ep_num;
uint8_t ep_addr;
ea_state = EA;
EA = 0;
if (report_id == 0) // Mouse
{
ep_num = 1;
ep_addr = FIFO_EP1;
}
else if (report_id == 1) // Keyboard
{
ep_num = 2;
ep_addr = FIFO_EP2;
}
else if (report_id == 2) // Remote
{
ep_num = 3;
ep_addr = FIFO_EP3;
}
else
{
EA = ea_state;
return;
}
POLL_WRITE_BYTE(INDEX, ep_num); // Set index to the right EP register
POLL_READ_BYTE (EINCSR1, ControlReg); // Read control register for the EP
if (EP_STATUS[ep_num] == EP_HALT) // If endpoint is currently halted, send a stall
{
POLL_WRITE_BYTE(EINCSR1, rbInSDSTL);
}
else if(EP_STATUS[ep_num] == EP_IDLE)
{
EP_STATUS[ep_num] = EP_TX; // the state will be updated inside the ISR handler
if (ControlReg & rbInSTSTL) // Clear sent stall if last packet returned a stall
{
POLL_WRITE_BYTE(EINCSR1, rbInCLRDT);
}
if (ControlReg & rbInUNDRUN) // Clear underrun bit if it was set
{
POLL_WRITE_BYTE(EINCSR1, 0x00);
}
ReportHandler_IN_ISR(report_id);
// Put new data on Fifo
Fifo_Write_InterruptServiceRoutine(ep_addr, in_buffer.Length, (uint8_t*)in_buffer.Ptr);
POLL_WRITE_BYTE (EINCSR1, rbInINPRDY); // Set In Packet ready bit, indicating fresh data on FIFO 1
}
EA = ea_state;
}
/** @} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -