⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 msg.c

📁 制作Zigbee模块的详细电路原理图和C代码
💻 C
字号:
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: msg.c

       Version: 0.1.2

       Usage: Message operation function

       Platform: U-NET01 DK with IAR 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124

               UBEC: UZ2400

       Note : 

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/

#include ".\zigbee\unet.h"
#define DisInt	_INT_OFF
#define EnabInt	 _INT_ON
extern  void _INT_ON();
extern  void _INT_OFF();

extern UNET_SYS UnetSys;


/*******************************************************************************

			Main Memoey Function

********************************************************************************/


void *MM_Alloc(UINT8 size){
	void *Addr;
	
	DisInt();

	Addr = malloc(size);

	EnabInt();

	return Addr;

	//return malloc(size);
}


void MM_Free(void *allocaddr){

	DisInt();

	free(allocaddr);

	EnabInt();
}


void MM_Init(MSG_QUEUE *Queue){

	UINT8 i;

	Queue->QueueDataCount = 0;
	Queue->QueueReadPoint = 0;
	Queue->QueueWritePoint = 0;

	for(i=0;i<MSG_MAX_QUEUE_SIZE; i++){
		Queue->Msg[i].DataFlag = false;
		Queue->Msg[i].MsgPtr = NULL;
	}
}

//将收到的帧放到消息队列中,注意,队列中只记录了帧数据的首地址,帧的原始数据是存放在一块申请的内存中,
//没有释放,用完一个消息后要记得释放内存。
INT8 MSG_Add(void *Msg, UINT8 msgType, MSG_QUEUE *QueuePtr, UINT8 Length){
	UINT8 WritePtr;

	if(QueuePtr->QueueDataCount < MSG_MAX_QUEUE_SIZE)
	{ // MSG Queue is not full

		WritePtr = QueuePtr->QueueWritePoint; // Fetch empty entry

		do
		{ // Move to next empty entry
			if(WritePtr == MSG_MAX_QUEUE_SIZE -1)
			{					
				QueuePtr->QueueWritePoint = 0;
			}
			else
			{
				QueuePtr->QueueWritePoint++;
			}
		}
		while(QueuePtr->Msg[WritePtr].DataFlag != false);

		// Fill Data
		QueuePtr->Msg[WritePtr].DataFlag = true;
		QueuePtr->Msg[WritePtr].MsgType = msgType;
		QueuePtr->Msg[WritePtr].MsgPtr = Msg;
		QueuePtr->Msg[WritePtr].Length = Length;

		QueuePtr->QueueDataCount ++; // Increase Queue Data Count

		// fill data successful, return message
		return SUCCESS; // Successful
	}

	return QUEUE_FULL; // MSG Queue is full, return message

}

//检查消息队列中是否有收到的帧
INT8 MSG_Fetch(MSG_QUEUE *QueuePtr)
{
	UINT8 ReadPtr;
	
	//  Have data in queue? or still need handle?
	if(QueuePtr->QueueDataCount >0)
	{
		// Fetch previous read entry
		while(1)
		{
			ReadPtr = QueuePtr->QueueReadPoint;
			
			if(QueuePtr->QueueReadPoint == MSG_MAX_QUEUE_SIZE -1)
			{					
				QueuePtr->QueueReadPoint = 0;
			}
			else
			{
				QueuePtr->QueueReadPoint++;
			}
			
			if(QueuePtr->Msg[ReadPtr].DataFlag == true)
			{				
				return ReadPtr;
			}	
		}
	}
	return QUEUE_EMPTY;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -