📄 main.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
*
* The USB dongle application.
*
* @author Lasse Olsen
*
*/
#include <c8051f320.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "wdp_host.h"
#include "f3xx_usb0_reporthandler.h"
#include "f3xx_usb0_interruptserviceroutine.h"
#include "target_includes.h"
static void prepare_mouse_usb_packet(uint8_t *src);
static void prepare_keyb_usb_packet(uint8_t *src);
static void prepare_rc_usb_packet(uint8_t *src);
static void clear_mouse_usb_packet();
static void clear_keyb_usb_packet();
static void clear_rc_usb_packet();
// Definitions internal states
typedef enum
{
APP_PAIRING,
APP_NORMAL
} app_states_t;
extern volatile uint16_t timer_cnt;
app_states_t app_state = APP_PAIRING;
void main(void)
{
uint8_t radio_data[WDP_MAX_PL_LENGTH]; // General data buffer
uint8_t dev_type, length; // Variuable for storing in
system_init(); // MCU initialization
wdp_host_init(); // Protocol initialization
usb_init(); // USB initialization
GLOBAL_INT_ENABLE();
LED1_OFF();
LED2_OFF();
LED3_OFF();
LED4_OFF();
while(USB0_STATE!=DEV_CONFIGURED){} //Wait for the USB interface to be configured
clear_mouse_usb_packet(); // Clear USB mouse data buffer
clear_keyb_usb_packet(); // Clear USB keyboard data buffer
clear_rc_usb_packet(); // Clear USB remote data buffer
LED4_ON(); // LED 4 ON indicates pairing ENABLED
wdp_host_enable_random_adr_gen(); // Start generation of random WDP master address
wdp_host_rx_setup(WDP_RX_PAIRING); // Setup RX to listen for pairing requests
timer_cnt = 0; // Reset counter variable used for pairing timeout
while(true)
{ // If APP_PAIRING state
if(app_state == APP_PAIRING)
{
wdp_host_process_events(); // Process radio events
// If pairing request received or 5 second pairing timeout reached
if(wdp_host_get_clear_pairing_result() || timer_cnt == APP_PAIRING_TIMEOUT) // Disable pairing
{
timer_cnt = 0xffff; // Will stop increment of pairing timeout variable
LED4_OFF(); // LED 4 OFF indicates pairing DISABLED
wdp_host_rx_setup(WDP_RX_NORMAL); // Setup RX to lisetn for normal user data (pairing DISABLED)
app_state = APP_NORMAL; // Goto APP_NORMAL state
}
}
else
if(app_state == APP_NORMAL) // If APP_NORMAL state
{
if(!SW1) // Pushing SW1 will re-enter 5 second pairing mode.
{
LED4_ON(); // LED 4 ON indicates pairing ENABLED
timer_cnt = 0; // Reset counter variable used for pairing timeout
wdp_host_rx_setup(WDP_RX_PAIRING); // Setup RX to listen for pairing requests
app_state = APP_PAIRING; // Goto APP_PAIRING state
}
wdp_host_process_events(); // Process radio events and poll for ectivity
if(wdp_host_get_rx_data(radio_data, &length, &dev_type)) // If any data received from a device
{
if(radio_data[APP_CMD] == APP_USER_INPUT)
{
switch(dev_type)
{
case WDP_MOUSE: // If mouse application data
if(length == APP_MOUSE_PL_LENGTH)
{
LED1_TOGGLE();
prepare_mouse_usb_packet(&radio_data[APP_DATA]); // Transfer received data to USB buffer
while(!ep1_sent); // Wait for USB to aquire the previous mouse package
ep1_sent=false;
send_usb_packet(0);
}
break;
case WDP_KEYBOARD: // If keyboard application data
if(length == APP_KEYBOARD_PL_LENGTH)
{
LED2_TOGGLE();
prepare_keyb_usb_packet(&radio_data[APP_DATA]); // Transfer received data to USB buffer
while(!ep2_sent); // Wait for USB to aquire the previous mouse package
ep2_sent=false;
send_usb_packet(1);
}
break;
case WDP_REMOTE: // If remote application data
if(length == APP_REMOTE_PL_LENGTH)
{
LED3_TOGGLE();
prepare_rc_usb_packet(&radio_data[APP_DATA]); // Transfer received data to USB buffer
ep3_sent=false;
send_usb_packet(2);
}
break;
}
}
else
if(radio_data[APP_CMD] == APP_GET_REQUEST) // ACK payload example, doesn't implement any functionality here
{
wdp_host_write_downlink_data(dev_type, radio_data, WDP_MAX_DL_PL_LENGTH); // Preload uplink data to device (no
}
}
//Flush USB buffers if connection to a device lost
if(!wdp_host_get_connection_status(WDP_MOUSE))
{
clear_mouse_usb_packet();
send_usb_packet(0);
}
if(!wdp_host_get_connection_status(WDP_KEYBOARD))
{
clear_keyb_usb_packet();
send_usb_packet(1);
}
if(!wdp_host_get_connection_status(WDP_REMOTE))
{
clear_mouse_usb_packet();
send_usb_packet(2);
}
}
}
} // End main
//-----------------------------------------------------------------------------
// Application functions
//-----------------------------------------------------------------------------
void clear_mouse_usb_packet()
{
usb_in_packet_mouse.button = 0;
usb_in_packet_mouse.disp_x = 0;
usb_in_packet_mouse.disp_y = 0;
usb_in_packet_mouse.disp_z = 0;
}
void clear_keyb_usb_packet()
{
uint8_t index;
usb_in_packet_keyb.mod = 0;
for(index=0; index<6; index++)
{
usb_in_packet_keyb.key[index]=0;
}
}
void clear_rc_usb_packet()
{
uint8_t i;
for (i=0; i<sizeof(usb_in_packet_rc.rc_data); i++)
{
usb_in_packet_rc.rc_data[i] = 0;
}
}
void prepare_mouse_usb_packet(uint8_t *src)
{
memcpy((uint8_t*)&usb_in_packet_mouse, src, sizeof(usb_in_packet_mouse));
}
void prepare_keyb_usb_packet(uint8_t *src)
{
memcpy((uint8_t*)&usb_in_packet_keyb, src, sizeof(usb_in_packet_keyb));
}
void prepare_rc_usb_packet(uint8_t *src)
{
memcpy((uint8_t*)&usb_in_packet_rc, src, sizeof(usb_in_packet_rc));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -