📄 accelerometer.c
字号:
/************************************************************************************
* Accelerometer main
*
* Author(s):
*
* (c) Copyright 2008, Freescale, Inc. All rights reserved.
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale.
*
* Last Inspected: 05/16/2008
* Last Tested:
************************************************************************************/
#include "../Configure/app_config.h"
#include "accelerometer.h"
/************************************************************************************
*************************************************************************************
* Private macros
*************************************************************************************
************************************************************************************/
/************************************************************************************
*************************************************************************************
* Private prototypes
*************************************************************************************
************************************************************************************/
/************************************************************************************
*************************************************************************************
* Private type definitions
*************************************************************************************
************************************************************************************/
/************************************************************************************
*************************************************************************************
* Private memory declarations
*************************************************************************************
************************************************************************************/
/************************************************************************************
*************************************************************************************
* Public functions
*************************************************************************************
************************************************************************************/
uint16_t gu16msTimer;
uint16_t gu16timerEvent[2];
uint16_t gu16Events;
int8_t gi8AppStatus;
uint8_t gu8TxAccelState;
uint8_t Buzzer;
uint8_t toggle;
uint8_t dat[7];
/* Global Variables */
uint8_t num_retries;
uint16_t adcConvResult;
uint8_t doAccelMeasurementBuffer[2];
uint8_t u8LastX, u8LastY, u8LastZ;
tAccelCal sCalValues;
crmVRegCntl_t VRegCntl;
nvmType_t NvmType;
TmrConfig_t pConfig;
TmrStatusCtrl_t pStatusCtrl;
const uint8_t CalArray[7] = {0x5D, 0x9D, 0x5D, 0x9D, 0x7D, 0xBE, 0xFF}; //Initial default calibration values
const tAccelCal * sCalDefaultValues = (tAccelCal *)(CalArray);
/*******************************************************************************
* main function
*
* Executes the main function.
*******************************************************************************/
void Main(void)
{
/* Variables declaration */
uint8_t data[TX_SIZE];
uint8_t dataRX[RX_SIZE];
gu16msTimer = 0;
gbDataIndicationFlag = FALSE;
gu8Channel = CHANNEL_NUMBER;
gu8TxAccelState = 'x';
num_retries = 0;
toggle = 0;
Buzzer=FALSE;
/* Variables initialization */
MSG_INIT(TX_msg, &data, NULL);
MSG_INIT(RX_msg, &dataRX, accelerometer_rx_cb);
RX_msg.u8BufSize = 14;
InterruptInit();
IntDisableAll();
IntAssignHandler(gMacaInt_c, MACA_Interrupt);
ITC_SetPriority(gMacaInt_c, gItcFastPriority_c);
ITC_EnableInterrupt(gMacaInt_c);
IntAssignHandler(gCrmInt_c, (IntHandlerFunc_t)CRM_Isr);
ITC_SetPriority(gCrmInt_c, gItcNormalPriority_c);
ITC_EnableInterrupt(gCrmInt_c);
IntAssignHandler(gTmrInt_c, TmrIsr);
ITC_SetPriority(gTmrInt_c, gItcNormalPriority_c);
ITC_EnableInterrupt(gTmrInt_c);
IntAssignHandler(gUart1Int_c, UartIsr1);
ITC_SetPriority(gUart1Int_c, gItcNormalPriority_c);
ITC_EnableInterrupt(gUart1Int_c);
IntDisableAll();
ResetMaca();
MLMERadioInit();
PlatformPortInit();
DelayMs(100);
SetupAccelerometer();
ADC_Setup();
NVMSetup();
Uart_Init();
IntEnableAll();
UartReadData(gUart_PortDefault_d, gu8SCIData, 1, TRUE);
MLMEPAOutputAdjust(gDefaultPowerLevel_c);
MLMESetChannelRequest((channel_num_t)gu8Channel);
RX_msg.u8BufSize = RX_SIZE;
MLMERXEnableRequest(&RX_msg, 0x0000F000);
AppInit();
#if OTAP_ENABLED == TRUE
OTAP_Init(&RX_msg);
gbOtapExecute = OTAP_ENABLED;
#endif
for(;;)
{
(void)process_radio_msg();
data_indication_execute();
#if OTAP_ENABLED == TRUE
if(gbOtapExecute)
{
OTAP_execute();
}
else
#endif
{
if(num_retries>0)
{
if(TRANSMIT_END())
{
_t_dec(num_retries);
MCPSDataRequest(&TX_msg);
}
}
else if(0==num_retries)
{
switch (gi8AppStatus)
{
case PC_RADIO_STATE:
BUZZER_OFF();
PC_Radio_App();
break;
case XYZ_STATE:
pingPacket(650, PING_BATTERYSAVE_BUZZER_OFF);
TxAccel();
break;
case FREEFALL_STATE:
freefall_app();
if (gu16Events & PING_FREEFALL)
{
appEventPacket(PING_FREEFALL, 5);
}
else
{
pingPacket(165, PING_NO_OPTIONS);
}
break;
case SHOCK_STATE:
pingPacket(600, PING_NO_OPTIONS);
shockDetect_app();
break;
case ANTITHEFT_STATE:
antiTheft_app();
pingPacket(670, PING_NO_OPTIONS);
break;
case BATTERYSAVE_STATE:
if ((gu16Events & TIMER_EVENT) != 0)
{
pingPacket(350, PING_BATTERYSAVE_BUZZER_OFF);
}
else
{
pingPacket(350, PING_BATTERYSAVE_BUZZER_ON);
}
batterySave_app();
break;
}
}
if(RECEIVE_END())
{
RX_msg.u8BufSize = RX_SIZE;
MLMERXEnableRequest(&RX_msg, 0x0000F000);
}
#if TARGET_BOARD != MC1322XUSB
if(gu16Events & SW1_EVENT)
{
DelayMs(100);
gu16Events &= ~SW1_EVENT; // Clear SW1 Event
increase_App();
}
if(gu16Events & SW2_EVENT)
{
DelayMs(100);
gu16Events &= ~SW2_EVENT; // Clear SW2 Event
increase_channel();
}
if(gu16Events & SW4_EVENT)
{
DelayMs(100);
gu16Events &= ~SW4_EVENT; // Clear SW4 Event
calibration_app();
}
#endif
}
}
}
/*******************************************************************************
* This function initialize LEDs, SWs, Interrupts and initial application states
*******************************************************************************/
void AppInit(void)
{
#if TARGET_BOARD != MC1322XUSB
NVM_Read(gNvmInternalInterface_c, NvmType, (void *)(&dat[0]), 0x00010000, 7);
if(dat[6]!=0x01)
{
SetCalibrationValues();
}
else
{
sCalValues.NxOff = dat[0];
sCalValues.NxMax = dat[1];
sCalValues.NyOff = dat[2];
sCalValues.NyMax = dat[3];
sCalValues.NzOff = dat[4];
sCalValues.NzMax = dat[5];
}
#endif
LED_Init();
KbGpioInit();
LED_TurnOffAllLeds();
/*********************KBI Interruptions Initialization***********************/
MLMESetWakeupSource(gExtWuKBI4En_c | gExtWuKBI5En_c | gExtWuKBI7En_c, 0x00, 0x0F);
CRM_RegisterISR(gCrmKB4WuEvent_c,increase_AppState_isr);
CRM_RegisterISR(gCrmKB5WuEvent_c,increase_channel_isr);
CRM_RegisterISR(gCrmKB7WuEvent_c,calibration_app_isr);
/*********************Timer Initialization***********************************/
TmrInit();
Timer_Setup(gTmr0_c, Tmr_Isr, 36000);
#if gBuzzerSupported_d
SetupBuzzer();
#endif
/*********************States Initialization**********************************/
TX_msg.u8Status.msg_state = MSG_TX_ACTION_COMPLETE_SUCCESS;
gi8AppStatus = PC_RADIO_STATE;
gi8PCRadioState = RECEIVER_ALWAYS_ON;
}
/*******************************************************************************
* This is the callback function when a valid packet has been received
* by the radio. A flag is set TRUE to store the Radio receive Event.
* It is not recommended to call other radio functions here.
*******************************************************************************/
void accelerometer_rx_cb (void)
{
if(TRUE == gRadioEvntFlags.Bits.data_indication_flag)
{
gRadioEvntFlags.Bits.data_indication_flag = FALSE;
gbDataIndicationFlag = TRUE;
}
else
{
/* Do nothing */
}
}
/*******************************************************************************
* This sends an App Packet to the PC_Radio to tell the
* PC Radio that an event has occured.
*******************************************************************************/
void appEventPacket(uint8_t options, uint8_t retries)
{
BuildEventPacket();
num_retries = retries;
}
/*******************************************************************************
* This sends a ping packet to the PC_Radio
*******************************************************************************/
void pingPacket(uint16_t rate, uint8_t options)
{
static uint16_t gu8pingRate;
gu8pingRate = rate;
/* Every 2 seconds send a packet - keep alive */
if (((gu16msTimer % gu8pingRate) == 0) && TRANSMIT_END())
{
BuildPingPacket();
LED_ToggleLed(LED2);
LED_TurnOffLed(LED1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -