微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI蓝牙设计交流 > 定时器不能启动。。。?

定时器不能启动。。。?

时间:10-02 整理:3721RD 点击:

static Clock_Struct periodicClock;
static Clock_Struct ZZHClock;
// Queue object used for app messages
static Queue_Struct appMsg;
static Queue_Handle appMsgQueue;

// Task configuration
Task_Struct sbpTask;
Char sbpTaskStack[SBP_TASK_STACK_SIZE];

// Scan response data (max size = 31 bytes)
static uint8_t scanRspData[] =
{
// complete name
0x14, // length of this data
GAP_ADTYPE_LOCAL_NAME_COMPLETE,
'S',
'i',
'm',
'p',
'l',
'e',
'B',
'L',
'E',
'P',
'e',
'r',
'i',
'p',
'h',
'e',
'r',
'a',
'l',

// connection interval range
0x05, // length of this data
GAP_ADTYPE_SLAVE_CONN_INTERVAL_RANGE,
LO_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL), // 100ms
HI_UINT16(DEFAULT_DESIRED_MIN_CONN_INTERVAL),
LO_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL), // 1s
HI_UINT16(DEFAULT_DESIRED_MAX_CONN_INTERVAL),

// Tx power level
0x02, // length of this data
GAP_ADTYPE_POWER_LEVEL,
0 // 0dBm
};

// Advertisement data (max size = 31 bytes, though this is
// best kept short to conserve power while advertising)
static uint8_t advertData[] =
{
// Flags: this field sets the device to use general discoverable
// mode (advertises indefinitely) instead of general
// discoverable mode (advertise for 30 seconds at a time)
0x02, // length of this data
GAP_ADTYPE_FLAGS,
DEFAULT_DISCOVERABLE_MODE | GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED,

// service UUID, to notify central devices what services are included
// in this peripheral
0x03, // length of this data
GAP_ADTYPE_16BIT_MORE, // some of the UUID's, but not all
LO_UINT16(SIMPLEPROFILE_SERV_UUID),
HI_UINT16(SIMPLEPROFILE_SERV_UUID)
};


// GAP GATT Attributes
static uint8_t attDeviceName[GAP_DEVICE_NAME_LEN] = "Simple Peripheral";

// Globals used for ATT Response retransmission
static gattMsgEvent_t *pAttRsp = NULL;
static uint8_t rspTxRetry = 0;

/*********************************************************************
* LOCAL FUNCTIONS
*/

static void SimpleBLEPeripheral_init( void );
static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1);
static bool write_command(uint8_t* command, size_t size);
//static bool write_command(uint8_t* command, uint8_t *p);
static int mag3110_read_command();
static int mag3110_config();
static void mag3110_init();
static int read_x ();
static int read_y ();
static int read_z ();
static int _read_xyz();
//static void _read_xyzTask(void);
static uint8_t SimpleBLEPeripheral_processStackMsg(ICall_Hdr *pMsg);
static uint8_t SimpleBLEPeripheral_processGATTMsg(gattMsgEvent_t *pMsg);
static void SimpleBLEPeripheral_processAppMsg(sbpEvt_t *pMsg);
static void SimpleBLEPeripheral_processStateChangeEvt(gaprole_States_t newState);
static void SimpleBLEPeripheral_processCharValueChangeEvt(uint8_t paramID);
static void SimpleBLEPeripheral_performPeriodicTask(void);
static void SimpleBLEPeripheral_clockHandler(UArg arg);

static void SimpleBLEPeripheral_sendAttRsp(void);
static void SimpleBLEPeripheral_freeAttRsp(uint8_t status);

static void SimpleBLEPeripheral_passcodeCB(uint8_t *deviceAddr, uint16_t connHandle,
uint8_t uiInputs, uint8_t uiOutputs);
static void SimpleBLEPeripheral_pairStateCB(uint16_t connHandle, uint8_t state,
uint8_t status);
static void SimpleBLEPeripheral_processPairState(uint8_t state, uint8_t status);
static void SimpleBLEPeripheral_processPasscode(uint8_t uiOutputs);

static void SimpleBLEPeripheral_stateChangeCB(gaprole_States_t newState);
static void SimpleBLEPeripheral_charValueChangeCB(uint8_t paramID);
static uint8_t SimpleBLEPeripheral_enqueueMsg(uint8_t event, uint8_t state,
uint8_t *pData);
static void _delay(unsigned num_loops)
{
unsigned i;
for (i=0; i<num_loops; i++)
{
asm ("NOP");
}
}
/*********************************************************************
* EXTERN FUNCTIONS
*/
extern void AssertHandler(uint8 assertCause, uint8 assertSubcause);

/*********************************************************************
* PROFILE CALLBACKS
*/

// Peripheral GAPRole Callbacks
static gapRolesCBs_t SimpleBLEPeripheral_gapRoleCBs =
{
SimpleBLEPeripheral_stateChangeCB // GAPRole State Change Callbacks
};

// GAP Bond Manager Callbacks
// These are set to NULL since they are not needed. The application
// is set up to only perform justworks pairing.
static gapBondCBs_t simpleBLEPeripheral_BondMgrCBs =
{
(pfnPasscodeCB_t) SimpleBLEPeripheral_passcodeCB, // Passcode callback
SimpleBLEPeripheral_pairStateCB // Pairing / Bonding state Callback
};

// Simple GATT Profile Callbacks
static simpleProfileCBs_t SimpleBLEPeripheral_simpleProfileCBs =
{
SimpleBLEPeripheral_charValueChangeCB // Simple GATT Characteristic value change callback
};

/*********************************************************************
* PUBLIC FUNCTIONS
*/

/*********************************************************************
* @fn SimpleBLEPeripheral_createTask
*
* @brief Task creation function for the Simple Peripheral.
*
* @param None.
*
* @return None.
*/
void SimpleBLEPeripheral_createTask(void)
{
Task_Params taskParams;

// Configure task
Task_Params_init(&taskParams);
taskParams.stack = sbpTaskStack;
taskParams.stackSize = SBP_TASK_STACK_SIZE;
taskParams.priority = SBP_TASK_PRIORITY;
Task_construct(&sbpTask, SimpleBLEPeripheral_taskFxn, &taskParams, NULL);
}

/*********************************************************************
* @fn Mag3110
*
* @brief Mag3110
*
* @param None.
*
* @return None.
*/


//static bool write_byte(uint8_t data)
//{
// uint8_t d;
// d = data;
// return write_command(&d, 1);
//}
//static bool write_command(uint8_t* command, uint8_t *p)
static bool write_command(uint8_t* command, size_t size)
{

if(NULL == i2c_handle)
return false;

uint8_t *p;
I2C_Transaction i2cTrans;
uint8_t sendBuf[32] = { 0 };
p = sendBuf;
memcpy(sendBuf, command, size);
*p = *command;
p++;
*p = 0x80;
i2cTrans.readBuf = NULL;
i2cTrans.readCount = 0;
i2cTrans.writeCount = p - sendBuf + 1;
i2cTrans.writeBuf = sendBuf;
i2cTrans.slaveAddress = 0x0E;
bool bret = I2C_transfer(i2c_handle, &i2cTrans);
//if(bret)
// Util_startClock(&ZZHClock);
// LOGGER("I2C transfer success\n");
// else
// LOGGER("I2C transfer fail\n");
}
static int mag3110_read_command()
{
if(NULL == i2c_handle)
return -1;
I2C_Transaction i2cTrans;
uint8_t readBuf[1] = { 0 };

i2cTrans.readBuf = readBuf;
i2cTrans.readCount = 1;
i2cTrans.writeCount = 0;
i2cTrans.writeBuf = NULL;
i2cTrans.slaveAddress = 0x0E;
I2C_transfer(i2c_handle, &i2cTrans);
return readBuf[0];
}

static int mag3110_config()
{
uint8_t cntrl = 0x11;
write_command(&cntrl, 1);
uint8_t auto_reset = 0x80;
write_command(&auto_reset, 1);
//Task_sleep(1000);
_delay(1);
uint8_t cntrl1 = 0x10;
write_command(&cntrl1, 1);
uint8_t active_mode = 0x01;
write_command(&active_mode, 1);
}


static void mag3110_init()
{
I2C_init();
I2C_Params params;
I2C_Params_init(&params);
i2c_handle = I2C_open(Board_I2C0, &params);
if(NULL == i2c_handle)
return;

mag3110_config();
uint8_t zzh7=CTRL_REG1;
uint8_t zzh8=CTRL_REG2;
write_command(&zzh7, 1);
write_command(&zzh8, 1);

// uint8_t auto_reset = 0x80;
// uint8_t active_mode = 0x01;
// write_command(&zzh7, &auto_reset);
// write_command(&zzh8, &active_mode);
//buf[0] = (BIT0);
// I2C_ByteWrite(SLV_ADDR,CTRL_REG1,buf,1); //閰嶇疆瀵勫瓨鍣–TRL_REG1
//buf[0] = (BIT7|BIT4);
// I2C_ByteWrite(SLV_ADDR,CTRL_REG2,buf,1); //閰嶇疆瀵勫瓨鍣–TRL_REG2
}

//static int read_1_byte ()
//{
// I2C_Transaction i2cTrans;
// uint8_t readBuf[1] = { 0 };
//
// i2cTrans.readBuf = readBuf;
// i2cTrans.readCount = 1;
// i2cTrans.writeCount = 0;
// i2cTrans.writeBuf = NULL;
// i2cTrans.slaveAddress = SLV_ADDR;
// I2C_transfer(i2c_handle, &i2cTrans);
// return readBuf[0];
//}
//static int read_2_byte()
//{
// I2C_Transaction i2cTrans;
// uint8_t readBuf[2] = { 0 };
//
// i2cTrans.readBuf = readBuf;
// i2cTrans.readCount = 2;
// i2cTrans.writeCount = 0;
// i2cTrans.writeBuf = NULL;
// i2cTrans.slaveAddress = SLV_ADDR;
// I2C_transfer(i2c_handle, &i2cTrans);
// return readBuf;
//}
//static int read_1_byte (uint8_t address)
//{
// uint8_t buf[1];
// I2C_BufferRead(SLV_ADDR,address,buf,1);
// return buf[0];
//}
//static int read_2_byte (uint8_t address)
//{
// uint8_t buf[2];
// I2C_BufferRead(SLV_ADDR,address,buf,2);
// return (buf[0]<<8)+buf[1];
//}
//static int read_x (void)
//{
// while((read_1_byte(DR_STATUS)&DR_STATUS_XDR) != DR_STATUS_XDR)//绛夊緟鏁版嵁鍑嗗瀹屾瘯
// {
// Task_sleep(100);
// }
// return read_2_byte(OUT_X_MSB);
//}
//static int read_y (void)
//{
// while((read_1_byte(DR_STATUS)&DR_STATUS_YDR) != DR_STATUS_YDR)//绛夊緟鏁版嵁鍑嗗瀹屾瘯
// {
// Task_sleep(100);
// }
// return read_2_byte(OUT_Y_MSB);
//}
//static int read_z (void)
//{
// while((read_1_byte(DR_STATUS)&DR_STATUS_ZDR) != DR_STATUS_ZDR)//绛夊緟鏁版嵁鍑嗗瀹屾瘯
// {
// Task_sleep(100);
// }
// return read_2_byte(OUT_Z_MSB);
//}

static int read_x ()
{
int xl, xh; //define the MSB and LSB
uint8_t zzh1 = 0x01;
uint8_t zzh2 = 0x02;
write_command(&zzh1, 1);
_delay(1);
//Task_sleep(1000); //needs at least 1.3us free time between start and stop
xh = mag3110_read_command();
_delay(1);
//Task_sleep(1000); //needs at least 1.3us free time between start and stop
write_command(&zzh2, 1);
_delay(1);
// Task_sleep(1000); //needs at least 1.3us free time between start and stop
xl = mag3110_read_command();
int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
//LOGGER("x:%d \r\n", xout );
return xout;
}
static int read_y ()
{
int yl, yh; //define the MSB and LSB
uint8_t zzh3 = 0x03;
uint8_t zzh4 = 0x04;
write_command(&zzh3, 1);
_delay(1);
//Task_sleep(1000); //needs at least 1.3us free time between start and stop
yh = mag3110_read_command();
_delay(1);
// Task_sleep(1000); //needs at least 1.3us free time between start and stop
write_command(&zzh4, 1);
_delay(1);
//Task_sleep(1000); //needs at least 1.3us free time between start and stop
yl = mag3110_read_command();
int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
//LOGGER("y:%d \r\n", yout );
return yout;
}
static int read_z ()
{
int zl, zh; //define the MSB and LSB
uint8_t zzh5 = 0x05;
uint8_t zzh6 = 0x06;
write_command(&zzh5, 1);
_delay(1);
//Task_sleep(1000); //needs at least 1.3us free time between start and stop
zh = mag3110_read_command();
_delay(1);
//Task_sleep(1000); //needs at least 1.3us free time between start and stop
write_command(&zzh6, 1);
// Task_sleep(1000); //needs at least 1.3us free time between start and stop
_delay(1);
zl = mag3110_read_command();
int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
//LOGGER("z:%d \r\n", zout );
return zout;
}

static int _read_xyz()
{
int _x = read_x();
int _y = read_y();
int _z = read_z();
Util_startClock(&ZZHClock);
LOGGER("x:%d, y:%d, z:%d \r\n", _x, _y, _z );
LOGGER("\r\n");
}
/*********************************************************************
* @fn SimpleBLEPeripheral_init

*
* @brief Called during initialization and contains application
* specific initialization (ie. hardware initialization/setup,
* table initialization, power up notification, etc), and
* profile initialization/setup.
*
* @param None.
*
* @return None.
*/
static void SimpleBLEPeripheral_init(void)
{
// ******************************************************************
// N0 STACK API CALLS CAN OCCUR BEFORE THIS CALL TO ICall_registerApp\

// ******************************************************************
// Register the current thread as an ICall dispatcher application
// so that the application can send and receive messages.
ICall_registerApp(&selfEntity, &syncEvent);
#ifdef USE_RCOSC
RCOSC_enableCalibration();
#endif // USE_RCOSC

#if defined( USE_FPGA )
// configure RF Core SMI Data Link
IOCPortConfigureSet(IOID_12, IOC_PORT_RFC_GPO0, IOC_STD_OUTPUT);
IOCPortConfigureSet(IOID_11, IOC_PORT_RFC_GPI0, IOC_STD_INPUT);

// configure RF Core SMI Command Link
IOCPortConfigureSet(IOID_10, IOC_IOCFG0_PORT_ID_RFC_SMI_CL_OUT, IOC_STD_OUTPUT);
IOCPortConfigureSet(IOID_9, IOC_IOCFG0_PORT_ID_RFC_SMI_CL_IN, IOC_STD_INPUT);

// configure RF Core tracer IO
IOCPortConfigureSet(IOID_8, IOC_PORT_RFC_TRC, IOC_STD_OUTPUT);
#else // !USE_FPGA
#if defined( DEBUG_SW_TRACE )
// configure RF Core tracer IO
IOCPortConfigureSet(IOID_8, IOC_PORT_RFC_TRC, IOC_STD_OUTPUT | IOC_CURRENT_4MA | IOC_SLEW_ENABLE);
#endif // DEBUG_SW_TRACE
#endif // USE_FPGA

// Create an RTOS queue for message from profile to be sent to app.
appMsgQueue = Util_constructQueue(&appMsg);

// Create one-shot clocks for internal periodic events.
Util_constructClock(&periodicClock, SimpleBLEPeripheral_clockHandler,
SBP_PERIODIC_EVT_PERIOD, 0, false, SBP_PERIODIC_EVT);
Util_constructClock(&ZZHClock, SimpleBLEPeripheral_clockHandler,
SBP_ZZH_EVT_PERIOD, 0, false, SBP_READ_EVT);
Util_startClock(&ZZHClock);

dispHandle = Display_open(SBP_DISPLAY_TYPE, NULL);

// Set GAP Parameters: After a connection was established, delay in seconds
// before sending when GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE,...)
// uses GAPROLE_LINK_PARAM_UPDATE_INITIATE_BOTH_PARAMS or
// GAPROLE_LINK_PARAM_UPDATE_INITIATE_APP_PARAMS
// For current defaults, this has no effect.
GAP_SetParamValue(TGAP_CONN_PAUSE_PERIPHERAL, DEFAULT_CONN_PAUSE_PERIPHERAL);

// Setup the Peripheral GAPRole Profile. For more information see the User's
// Guide:
// http://software-dl.ti.com/lprf/sdg-latest/html/
{
// Device starts advertising upon initialization of GAP
uint8_t initialAdvertEnable = TRUE;

// By setting this to zero, the device will go into the waiting state after
// being discoverable for 30.72 second, and will not being advertising again
// until re-enabled by the application
uint16_t advertOffTime = 0;

uint8_t enableUpdateRequest = DEFAULT_ENABLE_UPDATE_REQUEST;
uint16_t desiredMinInterval = DEFAULT_DESIRED_MIN_CONN_INTERVAL;
uint16_t desiredMaxInterval = DEFAULT_DESIRED_MAX_CONN_INTERVAL;
uint16_t desiredSlaveLatency = DEFAULT_DESIRED_SLAVE_LATENCY;
uint16_t desiredConnTimeout = DEFAULT_DESIRED_CONN_TIMEOUT;

// Set the Peripheral GAPRole Parameters
GAPRole_SetParameter(GAPROLE_ADVERT_ENABLED, sizeof(uint8_t),
&initialAdvertEnable);
GAPRole_SetParameter(GAPROLE_ADVERT_OFF_TIME, sizeof(uint16_t),
&advertOffTime);

GAPRole_SetParameter(GAPROLE_SCAN_RSP_DATA, sizeof(scanRspData),
scanRspData);
GAPRole_SetParameter(GAPROLE_ADVERT_DATA, sizeof(advertData), advertData);

GAPRole_SetParameter(GAPROLE_PARAM_UPDATE_ENABLE, sizeof(uint8_t),
&enableUpdateRequest);
GAPRole_SetParameter(GAPROLE_MIN_CONN_INTERVAL, sizeof(uint16_t),
&desiredMinInterval);
GAPRole_SetParameter(GAPROLE_MAX_CONN_INTERVAL, sizeof(uint16_t),
&desiredMaxInterval);
GAPRole_SetParameter(GAPROLE_SLAVE_LATENCY, sizeof(uint16_t),
&desiredSlaveLatency);
GAPRole_SetParameter(GAPROLE_TIMEOUT_MULTIPLIER, sizeof(uint16_t),
&desiredConnTimeout);
}

// Set the Device Name characteristic in the GAP GATT Service
// For more information, see the section in the User's Guide:
// http://software-dl.ti.com/lprf/sdg-latest/html
GGS_SetParameter(GGS_DEVICE_NAME_ATT, GAP_DEVICE_NAME_LEN, attDeviceName);

// Set GAP Parameters to set the advertising interval
// For more information, see the GAP section of the User's Guide:
// http://software-dl.ti.com/lprf/sdg-latest/html
{
// Use the same interval for general and limited advertising.
// Note that only general advertising will occur based on the above configuration
uint16_t advInt = DEFAULT_ADVERTISING_INTERVAL;

GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_LIM_DISC_ADV_INT_MAX, advInt);
GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MIN, advInt);
GAP_SetParamValue(TGAP_GEN_DISC_ADV_INT_MAX, advInt);
}

// Setup the GAP Bond Manager. For more information see the section in the
// User's Guide:
// http://software-dl.ti.com/lprf/sdg-latest/html/
{
// Don't send a pairing request after connecting; the peer device must
// initiate pairing
uint8_t pairMode = GAPBOND_PAIRING_MODE_WAIT_FOR_REQ;
// Use authenticated pairing: require passcode.
uint8_t mitm = TRUE;
// This device only has display capabilities. Therefore, it will display the
// passcode during pairing. However, since the default passcode is being
// used, there is no need to display anything.
uint8_t ioCap = GAPBOND_IO_CAP_DISPLAY_ONLY;
// Request bonding (storing long-term keys for re-encryption upon subsequent
// connections without repairing)
uint8_t bonding = TRUE;

GAPBondMgr_SetParameter(GAPBOND_PAIRING_MODE, sizeof(uint8_t), &pairMode);
GAPBondMgr_SetParameter(GAPBOND_MITM_PROTECTION, sizeof(uint8_t), &mitm);
GAPBondMgr_SetParameter(GAPBOND_IO_CAPABILITIES, sizeof(uint8_t), &ioCap);
GAPBondMgr_SetParameter(GAPBOND_BONDING_ENABLED, sizeof(uint8_t), &bonding);
}

// Initialize GATT attributes
GGS_AddService(GATT_ALL_SERVICES); // GAP GATT Service
GATTServApp_AddService(GATT_ALL_SERVICES); // GATT Service
DevInfo_AddService(); // Device Information Service
SimpleProfile_AddService(GATT_ALL_SERVICES); // Simple GATT Profile

// Setup the SimpleProfile Characteristic Values
// For more information, see the sections in the User's Guide:
// http://software-dl.ti.com/lprf/sdg-latest/html/
{
uint8_t charValue1 = 1;
uint8_t charValue2 = 2;
uint8_t charValue3 = 3;
uint8_t charValue4 = 4;
uint8_t charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };

SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR1, sizeof(uint8_t),
&charValue1);
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR2, sizeof(uint8_t),
&charValue2);
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR3, sizeof(uint8_t),
&charValue3);
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR4, sizeof(uint8_t),
&charValue4);
SimpleProfile_SetParameter(SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN,
charValue5);
}

// Register callback with SimpleGATTprofile
SimpleProfile_RegisterAppCBs(&SimpleBLEPeripheral_simpleProfileCBs);

// Start the Device
VOID GAPRole_StartDevice(&SimpleBLEPeripheral_gapRoleCBs);

// Start Bond Manager and register callback
VOID GAPBondMgr_Register(&simpleBLEPeripheral_BondMgrCBs);

// Register with GAP for HCI/Host messages. This is needed to receive HCI
// events. For more information, see the section in the User's Guide:
// http://software-dl.ti.com/lprf/sdg-latest/html
GAP_RegisterForMsgs(selfEntity);

// Register for GATT local events and ATT Responses pending for transmission
GATT_RegisterForMsgs(selfEntity);

//Set default values for Data Length Extension
{
//Set initial values to maximum, RX is set to max. by default(251 octets, 2120us)
#define APP_SUGGESTED_PDU_SIZE 251 //default is 27 octets(TX)
#define APP_SUGGESTED_TX_TIME 2120 //default is 328us(TX)

//This API is documented in hci.h
//See the LE Data Length Extension section in the BLE-Stack User's Guide for information on using this command:
//http://software-dl.ti.com/lprf/sdg-latest/html/cc2640/index.html
//HCI_LE_WriteSuggestedDefaultDataLenCmd(APP_SUGGESTED_PDU_SIZE, APP_SUGGESTED_TX_TIME);
}

#if !defined (USE_LL_CONN_PARAM_UPDATE)
// Get the currently set local supported LE features
// The HCI will generate an HCI event that will get received in the main
// loop
HCI_LE_ReadLocalSupportedFeaturesCmd();
#endif // !defined (USE_LL_CONN_PARAM_UPDATE)

Display_print0(dispHandle, 0, 0, "BLE Peripheral");
Util_startClock(&ZZHClock);

}

/*********************************************************************
* @fn SimpleBLEPeripheral_taskFxn
*
* @brief Application task entry point for the Simple Peripheral.
*
* @param a0, a1 - not used.
*
* @return None.
*/
static void SimpleBLEPeripheral_taskFxn(UArg a0, UArg a1)
{
// Initialize application
SimpleBLEPeripheral_init();

mag3110_init();
_read_xyz();
Util_startClock(&ZZHClock);
// Application main loop
for (;;)
{
uint32_t events;

// Waits for an event to be posted associated with the calling thread.
// Note that an event associated with a thread is posted when a
// message is queued to the message receive queue of the thread
events = Event_pend(syncEvent, Event_Id_NONE, SBP_ALL_EVENTS,
ICALL_TIMEOUT_FOREVER);

if (events)
{
ICall_EntityID dest;
ICall_ServiceEnum src;
ICall_HciExtEvt *pMsg = NULL;

// Fetch any available messages that might have been sent from the stack
if (ICall_fetchServiceMsg(&src, &dest,
(void **)&pMsg) == ICALL_ERRNO_SUCCESS)
{
uint8 safeToDealloc = TRUE;

if ((src == ICALL_SERVICE_CLASS_BLE) && (dest == selfEntity))
{
ICall_Stack_Event *pEvt = (ICall_Stack_Event *)pMsg;

// Check for BLE stack events first
if (pEvt->signature == 0xffff)
{
// The GATT server might have returned a blePending as it was trying
// to process an ATT Response. Now that we finished with this
// connection event, let's try sending any remaining ATT Responses
// on the next connection event.
if (pEvt->event_flag & SBP_HCI_CONN_EVT_END_EVT)
{
// Try to retransmit pending ATT Response (if any)
SimpleBLEPeripheral_sendAttRsp();
}
}
else
{
// Process inter-task message
safeToDealloc = SimpleBLEPeripheral_processStackMsg((ICall_Hdr *)pMsg);
}
}

if (pMsg && safeToDealloc)
{
ICall_freeMsg(pMsg);
}
}

// If RTOS queue is not empty, process app message.
if (events & SBP_QUEUE_EVT)
{
while (!Queue_empty(appMsgQueue))
{
sbpEvt_t *pMsg = (sbpEvt_t *)Util_dequeueMsg(appMsgQueue);
if (pMsg)
{
// Process message.
SimpleBLEPeripheral_processAppMsg(pMsg);

// Free the space from the message.
ICall_free(pMsg);
}
}
}

if (events & SBP_PERIODIC_EVT)
{
Util_startClock(&periodicClock);

// Perform periodic application task
SimpleBLEPeripheral_performPeriodicTask();
}

if (events & SBP_READ_EVT)
{
// Perform periodic application task
Util_startClock(&ZZHClock);
_read_xyz();
}
}
}
}

// Create one-shot clocks for internal periodic events. Util_constructClock(&MeasPerClock, MeasTemp_clockHandlerCb, DEFAULT_PERIOD, 0, false,MEASUREMENT_PERIODIC_EVT); . Util_startClock(&MeasPerClock); void MeasTemp_clockHandlerCb(UArg arg) { uint32_t clockTicks; Clock_Handle handle; // Store the event. events |= arg; //Do Measurement and required processing.... // Required only if new timeout value needs to be set. { handle = Clock_handle(&MeasPerClock); // Convert NEW_PERIOD in milliseconds to ticks. clockTicks = NEW_PERIOD* (1000 / Clock_tickPeriod); // Set the timeout Clock_setTimeout(handle, clockTicks); } //Restart Clock Util_startClock(&MeasPerClock); }

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top