Hi,I'm using z-stack mesh and I want to know where is the right place to implementing Uart read codes in Generecapp.c?
I want to read uart any time the data comes.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
I don't want to use MT and I want to use HalUARTRead() function, but I don't know where is the right place to implement this function in GenericApp.C
I wasn't succeed this way,,,, would you please modify my code to do this?
/*********************************************************************
* INCLUDES
*/
#include "OSAL.h"
#include "stdio.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"
#include "stdio.h"
#include "GenericApp.h"
#include "DebugTrace.h"
#include "mac_low_level.h"
#include "string.h"
#if !defined( WIN32 ) || defined( ZBIT )
#include "OnBoard.h"
#endif
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include "hal_adc.h"
/* RTOS */
#if defined( IAR_ARMCM3_LM )
#include "RTOS_App.h"
#endif
// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
GENERICAPP_CLUSTERID
};
const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
GENERICAPP_ENDPOINT, // int Endpoint;
GENERICAPP_PROFID, // uint16 AppProfId[2];
GENERICAPP_DEVICEID, // uint16 AppDeviceId[2];
GENERICAPP_DEVICE_VERSION, // int AppDevVer:4;
GENERICAPP_FLAGS, // int AppFlags:4;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList, // byte *pAppInClusterList;
GENERICAPP_MAX_CLUSTERS, // byte AppNumInClusters;
(cId_t *)GenericApp_ClusterList // byte *pAppInClusterList;
};
// This is the Endpoint/Interface description. It is defined here, but
// filled-in in GenericApp_Init(). Another way to go would be to fill
// in the structure here and make it a "const" (in code space). The
// way it's defined in this sample app it is define in RAM.
endPointDesc_t GenericApp_epDesc;
//configuration of UART
halUARTCfg_t uartConfig;
/*********************************************************************
* LOCAL VARIABLES
*/
byte GenericApp_TaskID; // Task ID for internal task/event processing
// This variable will be received when
// GenericApp_Init() is called.
devStates_t GenericApp_NwkState;
byte GenericApp_TransID; // This is the unique message ID (counter)
afAddrType_t GenericApp_DstAddr;
afAddrType_t Tag_DstAddr;
// Number of recieved messages
static uint16 rxMsgCount;
char theMessageData[40];
// Time interval between sending messages
static uint32 txMsgDelay = GENERICAPP_SEND_MSG_TIMEOUT;
/*********************************************************************
* LOCAL FUNCTIONS
*/
static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg );
static void GenericApp_HandleKeys( byte shift, byte keys );
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
static void GenericApp_SendTheMessage( void );
//static void GenericApp_ReadADC ( void );
/*********************************************************************
* NETWORK LAYER CALLBACKS
*/
/*********************************************************************
* PUBLIC FUNCTIONS
*/
/*********************************************************************
* @fn GenericApp_Init
*
* @brief Initialization function for the Generic App Task.
* This is called during initialization and should contain
* any application specific initialization (ie. hardware
* initialization/setup, table initialization, power up
* notificaiton ... ).
*
* @param task_id - the ID assigned by OSAL. This ID should be
* used to send messages and set timers.
*
* @return none
*/
void GenericApp_Init( uint8 task_id )
{
GenericApp_TaskID = task_id;
GenericApp_NwkState = DEV_INIT;
GenericApp_TransID = 0;
macRadioSetTxPower(10);
/* UART Configuration */
uartConfig.configured = TRUE;
uartConfig.baudRate = HAL_UART_BR_115200;
uartConfig.flowControl = FALSE;
uartConfig.flowControlThreshold = 64;
uartConfig.rx.maxBufSize = 128;
uartConfig.tx.maxBufSize = 128;
uartConfig.idleTimeout = 6;
uartConfig.intEnable = TRUE;
uartConfig.callBackFunc = NULL;
HalUARTOpen (HAL_UART_PORT_0, &uartConfig);
//HalUARTWrite(HAL_UART_PORT_0, "Hello World!", 12);
//GenericApp_ReadADC();
//register for ZDO init
// ZDO_RegisterForZDOMsg(GenericApp_TaskID, Device_annce);
// Device hardware initialization can be added here or in main() (Zmain.c).
// If the hardware is application specific - add it here.
// If the hardware is other parts of the device add it in main().
// GenericApp_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
// GenericApp_DstAddr.endPoint = 0;
// GenericApp_DstAddr.addr.shortAddr = 0;
GenericApp_DstAddr.addrMode = afAddr16Bit;
GenericApp_DstAddr.addr.shortAddr = 0x0000; //Coordinator
GenericApp_DstAddr.endPoint = GENERICAPP_ENDPOINT;
// Fill out the endpoint description.
GenericApp_epDesc.endPoint = GENERICAPP_ENDPOINT;
GenericApp_epDesc.task_id = &GenericApp_TaskID;
GenericApp_epDesc.simpleDesc
= (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
GenericApp_epDesc.latencyReq = noLatencyReqs;
// Register the endpoint description with the AF
afRegister( &GenericApp_epDesc );
// Register for all key events - This app will handle all key events
RegisterForKeys( GenericApp_TaskID );
ZDO_RegisterForZDOMsg( GenericApp_TaskID, End_Device_Bind_rsp );
ZDO_RegisterForZDOMsg( GenericApp_TaskID, Match_Desc_rsp );
}
/*********************************************************************
* @fn GenericApp_ProcessEvent
*
* @brief Generic Application Task event processor. This function
* is called to process all events for the task. Events
* include timers, messages and any other user defined events.
*
* @param task_id - The OSAL assigned task ID.
* @param events - events to process. This is a bit map and can
* contain more than one event.
*
* @return none
*/
uint16 GenericApp_ProcessEvent( uint8 task_id, uint16 events )
{
afIncomingMSGPacket_t *MSGpkt;
afDataConfirm_t *afDataConfirm;
// Data Confirmation message fields
byte sentEP;
ZStatus_t sentStatus;
byte sentTransID; // This should match the value sent
(void)task_id; // Intentionally unreferenced parameter
if ( events & SYS_EVENT_MSG )
{
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
case ZDO_CB_MSG:
GenericApp_ProcessZDOMsgs( (zdoIncomingMsg_t *)MSGpkt );
break;
case KEY_CHANGE:
GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
break;
case AF_DATA_CONFIRM_CMD:
// This message is received as a confirmation of a data packet sent.
// The status is of ZStatus_t type [defined in ZComDef.h]
// The message fields are defined in AF.h
afDataConfirm = (afDataConfirm_t *)MSGpkt;
sentEP = afDataConfirm->endpoint;
(void)sentEP; // This info not used now
sentTransID = afDataConfirm->transID;
(void)sentTransID; // This info not used now
sentStatus = afDataConfirm->hdr.status;
// Action taken when confirmation is received.
if ( sentStatus != ZSuccess )
{
// The data wasn't delivered -- Do something
}
break;
case AF_INCOMING_MSG_CMD:
GenericApp_MessageMSGCB( MSGpkt );
break;
case ZDO_STATE_CHANGE:
GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
if ( (GenericApp_NwkState == DEV_ZB_COORD) ||
(GenericApp_NwkState == DEV_ROUTER) ||
(GenericApp_NwkState == DEV_END_DEVICE) )
{
// Start sending "the" message in a regular interval.
osal_start_timerEx( GenericApp_TaskID,
GENERICAPP_SEND_MSG_EVT,
txMsgDelay );
}
break;
default:
break;
}
// Release the memory
osal_msg_deallocate( (uint8 *)MSGpkt );
// Next
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
// Send a message out - This event is generated by a timer
// (setup in GenericApp_Init()).
if ( events & GENERICAPP_SEND_MSG_EVT )
{
// Send "the" message
//GenericApp_SendTheMessage();
//GenericApp_ReadADC();
// Setup to send message again
osal_start_timerEx( GenericApp_TaskID,
GENERICAPP_SEND_MSG_EVT,
txMsgDelay );
// return unprocessed events
return (events ^ GENERICAPP_SEND_MSG_EVT);
}
#if defined( IAR_ARMCM3_LM )
// Receive a message from the RTOS queue
if ( events & GENERICAPP_RTOS_MSG_EVT )
{
// Process message from RTOS queue
GenericApp_ProcessRtosMessage();
// return unprocessed events
return (events ^ GENERICAPP_RTOS_MSG_EVT);
}
#endif
// Discard unknown events
return 0;
}
/*********************************************************************
* Event Generation Functions
*/
/*********************************************************************
* @fn GenericApp_ProcessZDOMsgs()
*
* @brief Process response messages
*
* @param none
*
* @return none
*/
static void GenericApp_ProcessZDOMsgs( zdoIncomingMsg_t *inMsg )
{
switch ( inMsg->clusterID )
{
case End_Device_Bind_rsp:
if ( ZDO_ParseBindRsp( inMsg ) == ZSuccess )
{
// Light LED
// HalLedSet( HAL_LED_3, HAL_LED_MODE_ON );
}
break;
}
}
/*********************************************************************
* @fn GenericApp_HandleKeys
*
* @brief Handles all key events for this device.
*
* @param shift - true if in shift/alt.
* @param keys - bit field for key events.
*
* @return none
*/
static void GenericApp_HandleKeys( uint8 shift, uint8 keys )
{
zAddrType_t dstAddr;
if ( keys & HAL_KEY_SW_6 )
{
// HalLedSet ( HAL_LED_1, HAL_LED_MODE_ON );
// Initiate an End Device Bind Request for the mandatory endpoint
dstAddr.addrMode = Addr16Bit;
dstAddr.addr.shortAddr = 0x0000; // Coordinator
ZDP_EndDeviceBindReq( &dstAddr, NLME_GetShortAddr(),
GenericApp_epDesc.endPoint,
GENERICAPP_PROFID,
GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
GENERICAPP_MAX_CLUSTERS, (cId_t *)GenericApp_ClusterList,
FALSE );
}
if ( keys & HAL_KEY_SW_7 )
{
//GenericApp_SendTheMessage();
// HalLedSet ( HAL_LED_1, HAL_LED_MODE_FLASH );
//NLME_GetShortAddr();
}
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
/*********************************************************************
* @fn GenericApp_MessageMSGCB
*
* @brief Data message processor callback. This function processes
* any incoming data - probably from other devices. So, based
* on cluster ID, perform the intended action.
*
* @param none
*
* @return none
*/
char *a,b[20];
int c;
uint8 lq;
uint16 e;
//char y;
int i=0;
char buf[10];
char buf2[10];
char buf3[10];
char buf4[10];
char main_buff[40];
char main_buff_test[40];
//a=&b;
static void GenericApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{
//inja pkt ra be soorate 8 biti ya 1 byti mifrestim
//uint8 *Ptr=(uint8*)&pkt;
switch ( pkt->clusterId )
{
case GENERICAPP_CLUSTERID:
rxMsgCount += 1; // Count this message
// Blink an LEDS
//in ghesmat rooye UART pkt ra mifrestim
//HalUARTWrite( HAL_UART_PORT_0, Ptr ,sizeof(afIncomingMSGPacket_t) );
}
a=(char*)pkt->cmd.Data;
//b=*a;
c=pkt->rssi;
lq=pkt->LinkQuality;
e=pkt->srcAddr.addr.shortAddr;
//y=e;
if(strcmp(a,"PING")==0){
//strcpy(b,"This is ok.");
HalLedSet ( HAL_LED_3, HAL_LED_MODE_BLINK );
//HalUARTWrite( HAL_UART_PORT_0, a ,4 );
//HalUARTWrite( HAL_UART_PORT_0, Template ,10 );
sprintf(buf,"LQI=%d",lq);
sprintf(buf2,"rssi=%d",c);
sprintf(buf3,"Sho=%d",e);
sprintf(buf4,"data=%d",a);
for(i=0;i<40;i++)
{
if(i<10)
main_buff[i]=buf[i];
else if(i>=10&&i<20)
main_buff[i]=buf2[i-10];
else if(i>=20&&i<30)
main_buff[i]=buf3[i-20];
else if(i>=30&&i<40)
main_buff[i]=buf4[i-30];
}
// sprintf(theMessageData,"%d",main_buff);
// HalUARTWrite( HAL_UART_PORT_0, buf ,16 );
// HalUARTWrite( HAL_UART_PORT_0, buf2 ,20 );
// HalUARTWrite( HAL_UART_PORT_0, buf3 ,30 );
// HalUARTWrite( HAL_UART_PORT_0, buf4 ,15 );
// HalUARTWrite( HAL_UART_PORT_0, main_buff ,40 );
// strcpy(main_buff_test,main_buff);
//strcpy(theMessageData,main_buff);
for(i=0;i<40;i++)
{
theMessageData[i]=main_buff[i];
}
GenericApp_SendTheMessage();
// struct Alireza_Pack1
// {
// int C;
// //uint8 Lq;
// // uint16 E;
// };
//struct Alireza_Pack1 Obj_Pack1;
//Obj_Pack1.C=c;
//Obj_Pack.Lq=lq;
//Obj_Pack.E=e;
//int8 *Ptr2 =(int8*)&Obj_Pack1;
// HalUARTWrite( HAL_UART_PORT_0, Ptr2 ,1 );
// GenericApp_SendTheMessage();
}
else{;}
}
/*********************************************************************
* @fn GenericApp_SendTheMessage
*
* @brief Send "the" message.
*
*/
static void GenericApp_SendTheMessage( void )
{
//printf(theMessageData,"ADC=%d",abbss);
if ( AF_DataRequest( &GenericApp_DstAddr, &GenericApp_epDesc,
GENERICAPP_CLUSTERID,
41,
(byte *)&theMessageData,
&GenericApp_TransID,
AF_DISCV_ROUTE,
AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )
{
// Successfully requested to be sent.
//HalLedSet ( HAL_LED_1, HAL_LED_MODE_BLINK );
//GenericApp_ReadADC();
}
else
{
// Error occurred in request to send.
// HalLedSet ( HAL_LED_1, HAL_LED_MODE_FLASH );
}
}
//uint16 adc_ain4=0;
//uint16 abbss;
//uint16 level;
//uint16 last_level=0;
//static void GenericApp_ReadADC( void )
//{
// HalAdcSetReference(HAL_ADC_REF_125V);
// adc_ain4=HalAdcRead(HAL_ADC_CHN_AIN4,HAL_ADC_RESOLUTION_10);
// abbss=adc_ain4;
// if( abbss<300 ){
// HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
// level=1;
// }
// else if( abbss>300 && abbss<400 )
// {
// HalLedSet ( HAL_LED_2, HAL_LED_MODE_ON );
// level=2;
// }
// else
// {
// HalLedSet ( HAL_LED_2, HAL_LED_MODE_OFF );
// level=3;
// }
// if( level!=last_level)
// {
// strcpy(theMessageData,"Alirezaa");
// GenericApp_SendTheMessage();
// }
// last_level=level;
//}