| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 | /****************************************************************************** * 消息邮箱 * Copyright 2014, 飞联. * * File Name  : Mbox.c * Description: 消息邮箱,也可称作交换消息,实则为一固定单元(4 bytes)的FIFO队列 * * modification history * -------------------- * V1.0, 27-jun-2014, Simon written * -------------------- ******************************************************************************/#include "mbox.h"#include <stdio.h>#include <stdlib.h>/****************************************************************************** * Mbox_Create - Create a mailbox from dynamic memory pool. * * Input: size, the capacity size of mailbox, with 4 bytes of a mail for a unit. * Return: return the created mailbox, NULL on error. * modification history * -------------------- * 27-jun-2014, Simon written * -------------------- ******************************************************************************/Mbox_t Mbox_Create(const uint32_t size){	Mbox_t mb;	mb = (Mbox_t)malloc(sizeof(struct MailBox));	if(mb == NULL)		return NULL;	mb->max_size = size;	mb->msg_pool = (uint32_t *)malloc(mb->max_size * sizeof(uint32_t));	if(mb->msg_pool == NULL)	{		free(mb);		return NULL;	}	mb->size = 0;	mb->read_index = 0;	mb->save_index = 0;	return mb;}/****************************************************************************** * Mbox_Del - Delete a mailbox and release the memory. * * Input: mb, a pointer to the mailbox. * Return: return the error code. * modification history * -------------------- * 27-jun-2014, Simon written * -------------------- ******************************************************************************/Mbox_Err_t Mbox_Del(Mbox_t mb){	if(mb != NULL)	{		if(mb->msg_pool != NULL)		{			free(mb->msg_pool);		}		mb->msg_pool = NULL;		free(mb);	}	return MBOX_OK;}/****************************************************************************** * Mbox_Post - Send a mail to the mailbox. * * Input: *	mb, a pointer to the mailbox; *	msg, the mail. * Return: return the error code. * modification history * -------------------- * 27-jun-2014, Simon written * -------------------- ******************************************************************************/Mbox_Err_t Mbox_Post(Mbox_t mb, const uint32_t msg){	if(mb == NULL)		return MBOX_ERR;	if(mb->size == mb->max_size)		return MBOX_ERR;	Mbox_IrqDisable();	mb->msg_pool[mb->save_index] = msg;	++mb->save_index;	if(mb->save_index >= mb->max_size)		mb->save_index = 0;	mb->size++;	Mbox_IrqEnable();	return MBOX_OK;}/****************************************************************************** * Mbox_Pend - Receive a mail from mailbox. * * Input: *	mb, a pointer to the mailbox; * Output: *	msg, the received mail will be saved in. * Return: return the error code. * modification history * -------------------- * 27-jun-2014, Simon written * -------------------- ******************************************************************************/Mbox_Err_t Mbox_Pend(Mbox_t mb, uint32_t *msg){	if(mb == NULL)		return MBOX_ERR;	if(mb->size == 0)		return MBOX_ERR;	Mbox_IrqDisable();	*msg = mb->msg_pool[mb->read_index];	++mb->read_index;	if(mb->read_index >= mb->max_size)		mb->read_index = 0;	mb->size--;	Mbox_IrqEnable();	return MBOX_OK;}/****************************************************************************** * Mbox_Pend - This function checks the mailbox to see if a message is available.  Unlike Mbox_Pend(), *              MboxAccept() does not clear the message is not available. * Input: *	mb, a pointer to the mailbox; * Output: *	msg, the received mail will be saved in. * Return: return the error code. * modification history * -------------------- * 10-jul-2016, Simon written * -------------------- ******************************************************************************/Mbox_Err_t Mbox_Accept(Mbox_t mb, uint32_t *msg){	if(mb == NULL)		return MBOX_ERR;	if(mb->size == 0)		return MBOX_ERR;	*msg = mb->msg_pool[mb->read_index];	return MBOX_OK;}
 |