📄 ia443x_demo.c
字号:
/*
** ============================================================================
**
** FILE
** IA443x_demo.c
**
** DESCRIPTION
** Contains the RF dependent functions
**
** CREATED
** Silicon Laboratories Hungary Ltd
**
** COPYRIGHT
** Copyright 2008 Silicon Laboratories, Inc.
** http://www.silabs.com
**
** ============================================================================
*/
/*------------------------------------------------------------------------*/
/* INCLUDE */
/*------------------------------------------------------------------------*/
#include "IA443x_demo.h"
#include "global_definitions.h"
#include "IA443x_rf.h"
#include "IA443x_rf_callback.h"
#include "menu.h"
#include "IAI_EBID.h"
/*------------------------------------------------------------------------*/
/* GLOBAL variables */
/*------------------------------------------------------------------------*/
idata DEMO_STATES DemoStates, NewDemoStates;
bit fChangeDemoStates, AutoTx;
idata UU16 DemoTimer;
idata uint8 LongDelay;
xdata PACKET_TYPE fWaitForPacketOrAck,fPacketSent;
xdata uint8 rf_param[10];
xdata uint16 NmbrOfSentPackets, NmbrOfReceivedPackets,NmbrOfMissedPackets,LastPacketId;
xdata ANTENNA_TYPE SelectedAntennaType;
idata PHY_STATUS RfStatus;
xdata uint8 RxTxBuffer[MAX_LENGTH_OF_PAYLOAD];
xdata HEADER HeaderBuffer;
xdata MESSAGE MessageBuffer;
xdata RECEIVE_COMMAND RxCommand;
xdata RECEIVED_DATA ReceivedData;
//EXTERN variables
//global_constans.c
extern code DEMO_FREQ demo_freqs[MAX_FREQ_SETTING];
extern code DEMO_FREQ arib_freqs[MAX_ARIB_FREQ_SETTING];
//IAI_EBID.c
extern xdata EBID_DATA TestcardData;
//menu.c
extern xdata MENU_ITEM_VALUE MenuItems;
extern code uint16 v_max_packets[8];
/*------------------------------------------------------------------------*/
/* LOCAL function prototypes */
/*------------------------------------------------------------------------*/
void DemoAckSent(uint8 success);
void DemoPingReceived(void);
void DemoAckReceived(uint8 success);
/*------------------------------------------------------------------------*/
/* LOCAL definitions */
/*------------------------------------------------------------------------*/
#define PER_RESET_INPUT PB2_PIN
#define SEND_PACKET_INPUT PB1_PIN
#define LED_TX LED1_PIN
#define LED_RX LED2_PIN
#define LED_ANT1 LED3_PIN
#define LED_ANT2 LED4_PIN
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void DemoInit(void)
+
+ DESCRIPTION: initializes the demo
+
+ RETURN: None
+
+ NOTES:
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void DemoInit(void)
{
//initialize the demo variables
DemoStates = sDemoInit;
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+
+ FUNCTION NAME: void DemoStateMachine(void)
+
+ DESCRIPTION: state machine of the demo
+
+ RETURN: None
+
+ NOTES:
+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void DemoStateMachine(void)
{
uint8 temp8,i,string[6];
uint16 temp16;
float temp_float;
switch( DemoStates )
{
case sDemoInit:
//set default variables
DemoStates = sDemoStartScreen;
//find out what kind of testcard is plugged into the PIC SW Dev board
GetEBIDDataFromEEPROM();
if( TestcardData.valid_info == FALSE )
{//there is no Testcard inserted or there is no EEPROM on the testcard
PerformMenu(sMenuTestcardOrEEPROMError);
DemoStates = sDemoDoNothing;
return;
}
//find out whether the chip is supported by the current FW or not
switch( TestcardData.chip_type )
{
case SI4030:
case SI4031:
case SI4032:
temp8 = TRUE;
break;
case SI4330:
temp8 = TRUE;
break;
case SI4430:
case SI4431:
case SI4432:
if( TestcardData.chip_revision[0] == 'V' )
{
temp8 = TRUE;
}
break;
default:
temp8 = FALSE;
break;
}
if( temp8 == TRUE )
{//the chip is supported, check the POR
//initialize the RF chip
temp8 = RfInitHw();
if( temp8 == RF_ERROR_TIMING )
{//POR Timeout error
PerformMenu( sMenuPORTimeout );
DemoStates = sDemoDoNothing;
return;
}
}
else
{//the chip is not supported by the current FW
PerformMenu(sMenuNotSupportedChip);
DemoStates = sDemoDoNothing;
return;
}
//disable hidden menu
MenuItems.name.show_hidden_menu = FALSE;
//set the antenna type
switch( TestcardData.ant_type )
{
case SMA_TRX_SWITCH: SelectedAntennaType = RX_TX_SWITCH; break;
case SEPARATE_SMA: SelectedAntennaType = SEPARATE_RX_TX; break;
case ANT_DIVERSITY: SelectedAntennaType = ANTENNA_DIVERSITY; break;
default: SelectedAntennaType = SEPARATE_RX_TX; break;
}
break;
case sDemoStartScreen:
//draw the startup screen
PerformMenu( sMenuStartScreen );
//start long timeout -> ~3s
LongDelay = START_SCREEN_DELAY_SW_DIV;
DemoTimer.U16 = START_SCREEN_DELAY;
StartTmr3(START_SCREEN_DELAY_DIV, DemoTimer, FALSE);
DemoStates = sDemoDelayAfterScreens;
break;
case sDemoDelayAfterScreens:
//check whether the timer expired or not
if( Tmr3Expired() == TRUE )
{//TMR expired
StopTmr3();
if( --LongDelay == 0 )
{
DemoStates = sDemoInitRf;
return;
}
else
{//restart timer
DemoTimer.U16 = START_SCREEN_DELAY;
StartTmr3(START_SCREEN_DELAY_DIV, DemoTimer, FALSE);
}
}
//enable the hidden menu if needed
if( (PB1_PIN == 0) && (PB2_PIN == 0) )
{ //wait for releasing the buttons
while( (PB1_PIN == 0) && (PB2_PIN == 0) );
//enable the hidden menu
MenuItems.name.show_hidden_menu = TRUE;
DemoStates = sDemoInitRf;
return;
}
//skip the start screen if any of the PB is pressed
if( (PB1_PIN == 0) || (PB2_PIN == 0) || (PB3_PIN == 0) || (PB4_PIN == 0) )
{
while( (PB1_PIN == 0) || (PB2_PIN == 0) || (PB3_PIN == 0) || (PB4_PIN == 0) );
DemoStates = sDemoInitRf;
}
break;
case sDemoInitRf:
if( SpiReadByteFromTestcardEEPROM( EE_ADDRESS_FIRST_PLUG ) == 0xFF )
{//no data is saved previously into the EEPROM
//reset menu variables
for(i=0;i<NMBR_OF_MENU_ITEM;i++) MenuItems.value[i] = 0;
//-> set destination ID equal to the source ID
MenuItems.name.dest_id = (uint8)(TestcardData.serial_nmbr & 0x00FF);
MenuItems.name.self_id = (uint8)(TestcardData.serial_nmbr & 0x00FF);
MenuItems.name.max_packets = 4;
}
else
{//read menu items from the EEPROM
for(i=0;i<NMBR_OF_MENU_ITEM-1;i++)
{
MenuItems.value[i] = SpiReadByteFromTestcardEEPROM(EE_ADDRESS_FIRST_PLUG + i + 1);
}
MenuItems.name.self_id = (uint8)(TestcardData.serial_nmbr & 0x00FF);
}
ChangeSettingsIfNotvalid();
MenuSetCursorPosition(0);
DemoStates = sDemoSettingScreen;
break;
case sDemoSettingScreen:
//draw first setting page
PerformMenu( sMenuDrawSystemFunctionScreen );
DemoStates = sDemoChangeSP1;
break;
case sDemoChangeSP1:
switch( PerformMenu(sMenuChangeSystemFunction) )
{
case UPDATE_PAGE: DemoStates = sDemoSettingScreen; break;
case NEXT_PAGE:
DemoStates = sDemoDrawSP2;
MenuSetCursorPosition(0);
break;
default: break;
}
break;
case sDemoDrawSP2:
PerformMenu( sMenuDrawRfParametersScreen );
DemoStates = sDemoChangeSP2;
break;
case sDemoChangeSP2:
switch( PerformMenu(sMenuChangeRfParameters) )
{
case UPDATE_PAGE: DemoStates = sDemoDrawSP2; break;
case NEXT_PAGE:
DemoStates = sDemoDrawSP3;
MenuSetCursorPosition(1);
break;
default: break;
}
break;
case sDemoDrawSP3:
PerformMenu( sMenuDrawNodeParametersScreen );
DemoStates = sDemoChangeSP3;
break;
case sDemoChangeSP3:
switch( PerformMenu( sMenuChangeNodeParameters ) )
{
case UPDATE_PAGE: DemoStates = sDemoDrawSP3; break;
case NEXT_PAGE:
if(MenuItems.name.show_hidden_menu == TRUE) DemoStates = sDemoDrawSP4;
else DemoStates = sDemoDefineDemoOrTest;
MenuSetCursorPosition(0);
break;
default: break;
}
break;
case sDemoDrawSP4:
PerformMenu( sMenuDrawHiddenScreen );
DemoStates = sDemoChangeSP4;
break;
case sDemoChangeSP4:
switch( PerformMenu( sMenuChangeHiddenScreen ) )
{
case UPDATE_PAGE: DemoStates = sDemoDrawSP4; break;
case NEXT_PAGE: DemoStates = sDemoDefineDemoOrTest; break;
default: break;
}
break;
case sDemoDefineDemoOrTest:
ChangeSettingsIfNotvalid();
//save the settings into the EEPROM
for(i=0;i<NMBR_OF_MENU_ITEM;i++)
{
SpiWriteByteToTestcardEEPROM((EE_ADDRESS_FIRST_PLUG + i + 1), MenuItems.value[i]);
}
SpiWriteByteToTestcardEEPROM(EE_ADDRESS_FIRST_PLUG, 1);
//deefine the next state
if( MenuItems.name.system_function == DEMO_MODE )
{
switch( MenuItems.name.demo_mode )
{
case DEMO_MODE_TRX: DemoStates = sDemoRestartDemo; break;
case DEMO_MODE_TX: DemoStates = sDemoRestartTxDemo; break;
case DEMO_MODE_RX: DemoStates = sDemoRestartRxDemo; break;
default: DemoStates = sDemoRestartDemo; break;
}
}
else
{
switch( MenuItems.name.lab_mode )
{
case LAB_MODE_CW: DemoStates = sDemoStartCW; break;
case LAB_MODE_PN9: DemoStates = sDemoStartPN9; break;
case LAB_MODE_BER: DemoStates = sDemoStartBER; break;
case LAB_MODE_PER: DemoStates = sDemoStartPER; break;
default: DemoStates = sDemoStartCW; break;
}
}
/*************************************/
/* initialize RF stack and variables */
/*************************************/
SetRadio();
//clear flags and variables
NmbrOfSentPackets = 0;
NmbrOfReceivedPackets = 0;
NmbrOfMissedPackets = 0;
LastPacketId = 0;
AutoTx = FALSE;
fWaitForPacketOrAck = PING_PACKET;
fChangeDemoStates = FALSE;
LED_TX = 0;
LED_RX = 0;
BLED_PIN = 0;
break;
/*Extra function for CW-TEST mode*/
case sDemoStartCW:
PerformMenu( sMenuCWStartScreen );
//SET direct modulation mode: CW
rf_param[0] = TX_CLK_ON_GPIO | MOD_TYPE_CW;
RFSetParameter(pRfModulation, rf_param, 1);
//disable packet handler
rf_param[0] = TX_PACKET_HANDLER_DIS | CRC_DIS;
RFSetParameter(pRfPacketHandler,rf_param,1);
//start CW mode
RFDirectTransmit();
//next state
DemoStates = sDemoCheckSettingButton;
break;
case sDemoCheckSettingButton:
if( PB3_PIN == 0 || PB4_PIN == 0 )
{
while( PB3_PIN == 0 || PB4_PIN == 0 );
//goto setting page
MenuSetCursorPosition(0);
DemoStates = sDemoSettingScreen;
return;
}
break;
/*Extra function for PN9-TEST mode*/
case sDemoStartPN9:
PerformMenu( sMenuPN9StartScreen );
//SET direct modulation mode: PN9
temp8 = SpiRfReadRegister( ModulationModeControl2 );
temp8 &= 0x0F;
temp8 += TX_CLK_ON_GPIO | MOD_PN9;
SpiRfWriteAddressData((REG_WRITE | ModulationModeControl2), temp8);
//disable packet handler
rf_param[0] = TX_PACKET_HANDLER_DIS | CRC_DIS;
RFSetParameter(pRfPacketHandler,rf_param,1);
//start CW mode
RFDirectTransmit();
//next state
DemoStates = sDemoCheckSettingButton;
break;
/*Extra function for BER-TEST mode*/
case sDemoStartBER:
PerformMenu( sMenuBERStartScreen );
//GPIO0 - RX_DATA
SpiRfWriteAddressData((REG_WRITE | GPIO0Configuration), GPIO_RX_DATA );
//SET direct modulation mode
temp8 = SpiRfReadRegister( ModulationModeControl2 );
temp8 &= 0x0F;
temp8 += TX_CLK_ON_GPIO | MOD_DIRECT_VIA_GPIO;
SpiRfWriteAddressData((REG_WRITE | ModulationModeControl2), temp8);
//disable packet handler
rf_param[0] = RX_PACKET_HANDLER_DIS | CRC_DIS;
RFSetParameter(pRfPacketHandler, rf_param, 1);
//start receiving
RFDirectReceive();
//next state
DemoStates = sDemoCheckSettingButton;
break;
/*Extra function for PER-TEST mode*/
case sDemoStartPER:
//draw screen
PerformMenu( sMenuPERStartScreen );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -