main.c
来自「非常全的nrf2401设计资料」· C语言 代码 · 共 234 行
C
234 行
/* 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
*
* Application for demonstration of WDP using a PS2 Mouse.
*
* @author Lasse Olsen
*
*/
#include "app_ps2.h"
#include "wdp_device.h"
#include "lu1_bfb.h"
#include "timer.h"
#include "circle_test.h"
#include "hal_flash.h"
#include "app_param.h"
#include "nordic_common.h"
#include "string.h"
extern volatile uint16_t timer_cnt; // Global timer variable
typedef enum
{
NO_DATA_RDY,
SYNC_DATA_RDY,
UNSYNC_DATA_RDY
} tx_buf_states_t;
void main(void)
{
uint16_t startupcount = 0xffff; // Wait to ensure proper radio startup
tx_buf_states_t tx_buf_state = NO_DATA_RDY; // TX buffer status variable
uint8_t buttons, tx_pl_length, rx_pl_length;
uint16_t tries=0, packages=0; // For communication monitoring
int8_t tx_buf[WDP_MAX_UL_PL_LENGTH]; // Transmit (uplink) data buffer
int8_t rx_buf[WDP_MAX_DL_PL_LENGTH]; // Receive (downlink) data buffer
uint8_t temp_adr_buf1[FAP_ADDRESS_WIDTH];
uint8_t temp_adr_buf2[FAP_ADDRESS_WIDTH];
bool ps2_device_connected = false;
bool button_pressed = false;
while(startupcount--)
;
CLKCTL = 0;
// Radio + SPI setup
CE_LOW();
RFCTL = 0x10; // RF SPI Enable
RF = 1; // Radio IRQ enable
RFCKEN = 1; // RF clk enable
t2_init(FAP_RX_PERIOD); // Setup WDP/FAP timer
ET2 = 1; // Timer2 IRQ enable
/*
P0.5 = B1 (IN)
P0.4 = B2 (IN)
P0.3 = PS2 CIN (IN)
P0.2 = PS2 COUT (OUT)
P0.1 = PS2 DOUT (OUT)
P0.0 = PS2 DIN (IN)
*/
P0DIR = (BIT_5 | BIT_4 | BIT_3 | BIT_0);
P0ALT = 0; // Set up P0 as GPIO
P0EXP = 0;
if(SW1)
{
COUT_HIGH();
DOUT_HIGH();
if((app_ps2_init() == PS2_MOUSE_STANDARD)) // Verify that PS2 mouse is connected
{
ps2_device_connected = true;
}
else
{
ps2_device_connected = false;
}
}
else
{
ps2_device_connected = false;
}
wdp_device_init(WDP_MOUSE);
wdp_select_radio_idle_mode(WDP_STANDBY_IDLE); // Radio in IDLE between transmissions for minimum latency
hal_flash_bytes_read(PARAM_DEV_ADR0, temp_adr_buf1, FAP_ADDRESS_WIDTH);
wdp_device_set_address(temp_adr_buf1); // Setup address stored in flash
// Clear TX and RX buffer
for(buttons = 0; buttons < WDP_MAX_DL_PL_LENGTH; buttons++)
{
rx_buf[buttons] = tx_buf[buttons] = 0;
}
EA = 1; // Enable global interrupt
tx_buf[APP_ATTRIBUTE] = 0xFF;
//-----------------------------------------------------------------------------
// Main application
//-----------------------------------------------------------------------------
while(1)
{
if (!SW1) // If trying to draw a circle
{
if(timer_cnt >= APP_MOUSE_POLL_PERIOD)
{
timer_cnt = 0;
tx_buf[APP_CMD]=APP_USER_INPUT;
circle_test_get(&tx_buf[APP_MOUSE_X_DISP]);
tx_buf[APP_MOUSE_BUTTONS] = 0x09; // Left mouse button pressed
tx_buf_state = SYNC_DATA_RDY; // Indicates new data, protocol sync OK
wdp_device_ch_sync_enable(); // Synchronize new transmission to receive channel rotation
button_pressed = true;
}
}
else // Not trying to draw a circle
{
if (button_pressed) // If we just finished drawing a circle, clean up all potential mess
{
tx_buf[APP_CMD]=APP_USER_INPUT;
tx_buf[APP_MOUSE_BUTTONS] = 0x80; // Left mouse button released
tx_buf_state = SYNC_DATA_RDY; // Indicates new data, protocol sync OK
wdp_device_ch_sync_enable(); // Synchronize new transmission to receive channel rotation
button_pressed = false;
}
if(ps2_device_connected) // Poll PS2 mouse every 8 ms
{
/*
If SW1 not pressed -> acquire mouse data from PS2 interface.
Note, this causes protocol sync to be lost due to disabling of
WDP interrupts during PS2 communication. Thus, the WDP synchronization
mechanisms cannot be used when transmitting this data.
*/
tx_buf[APP_CMD]=APP_USER_INPUT; // Assemble TX packet
tx_pl_length = APP_MOUSE_PL_LENGTH;
EA = 0; // Global interrupt disable
ps2_write(PS2_READ_DATA_NO_RET); // Send PS2 "read data" command to mouse
buttons = ps2_read();
tx_buf[APP_MOUSE_X_DISP] += ps2_read(); // Assemble TX packet
tx_buf[APP_MOUSE_Y_DISP] += -ps2_read(); // Assemble TX packet
tx_buf[APP_MOUSE_Z_DISP] = 0;
EA = 1; // Global interrupt enable
// If mouse movement or buttons altered
if(tx_buf[APP_MOUSE_X_DISP] || tx_buf[APP_MOUSE_Y_DISP] || (tx_buf[APP_MOUSE_BUTTONS] != buttons))
{
tx_buf[APP_MOUSE_BUTTONS] = buttons;
tx_buf_state = UNSYNC_DATA_RDY; // Indicates new data, protocol out of sync
wdp_device_ch_sync_disable();
}
}
}
//-----------------------------------------------------------------------------
// Transmit mouse data
//-----------------------------------------------------------------------------
/*
For "circle test" data, transmission is started using
synchronization to the previous good channel.
*/
if( (wdp_get_mode() == WDP_IDLE) &&
(((tx_buf_state == SYNC_DATA_RDY) && (wdp_device_get_ch_offset()==0)) || (tx_buf_state == UNSYNC_DATA_RDY)))
{
tries += wdp_device_get_tries(); // Counts the number of sent payloads including retransmits
packages++; // Counts the number sent data packages excluding retransmits
if(!wdp_device_tx_success())
{
if(wdp_device_request_pairing()) // Send pairing request whenever previous transmission failed
{
wdp_device_get_adr(temp_adr_buf1);
// Uses tx_buf for temporary storage
hal_flash_bytes_read(PARAM_DEV_ADR0, temp_adr_buf2, FAP_ADDRESS_WIDTH);
// Store received pairing address if different from previous stored pairing address
if(memcmp(temp_adr_buf1, temp_adr_buf2, FAP_ADDRESS_WIDTH))
{
hal_flash_page_erase(PARAM_PAGE_N0);
hal_flash_bytes_write(PARAM_DEV_ADR0, temp_adr_buf1, FAP_ADDRESS_WIDTH);
}
}
}
tx_buf[APP_ATTRIBUTE]++;
wdp_device_send_data(tx_buf, APP_MOUSE_PL_LENGTH);
tx_buf[APP_MOUSE_X_DISP] = 0;
tx_buf[APP_MOUSE_Y_DISP] = 0;
tx_buf[APP_MOUSE_Z_DISP] = 0;
tx_buf_state = NO_DATA_RDY; // Indicates no unsent data
}
//-----------------------------------------------------------------------------
// Downlink data from host to device (example)
//-----------------------------------------------------------------------------
if(wdp_device_get_downlink_data(rx_buf, &rx_pl_length))
{
// Data received from host to rx_buf
}
else
{
// No data received
}
//-----------------------------------------------------------------------------
} // End while()
} // End main()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?