Mailbox and Mail
#ifndef __MAILBOX_H__
#define __MAILBOX_H__ #include <stdint.h>
#include <stdlib.h>
#include <string.h> typedef struct
{
// uint32_t Capacity;
uint8_t * Memory;
uint32_t MailSize;
uint32_t MailCount;
uint32_t ReadIndex;
uint32_t ReadCount;
} MAILBOX; // Creates a new mailbox and Init it.
MAILBOX * MBX_Create( uint32_t MailSize, uint32_t MailCount ); // Deletes a specified mailbox.
void MBX_Delete( MAILBOX * Mailbox ); // Clears all messages in a specified mailbox.
void MBX_Clear( MAILBOX * Mailbox ); // Init a new mailbox.
void MBX_Init( MAILBOX * Mailbox, uint32_t MailSize, uint32_t MailCount,
void * Memory ); // Stores a new message of a predefined size in a mailbox.
uint32_t MBX_Write( MAILBOX * Mailbox, void * Mail ); /*
* Stores a new message of a predefined size into a mailbox in front of all
* other messages, if the mailbox is able to accept one more message.
* The new message will be retrieved first.
*/
uint32_t MBX_WriteFront( MAILBOX * Mailbox, void * Mail ); /* Retrieves a new message of a predefined size from a mailbox.
* Mail : Pointer to the memory area that the message should be stored at.
* Make sure that it points to a valid memory area and that there is sufficient
* space for an entiremessage. The message size (in bytes) was defined
* when the mailbox was created.
*/
uint32_t MBX_Read( MAILBOX * Mailbox, void * Mail ); // Returns the number of mail currently available in a specified mailbox.
uint32_t MBX_GetCount( MAILBOX * Mailbox ); #endif /* __MAILBOX_H__ */
#include "mailbox.h"
#include "macro_misc.h"
#include "cmsis_os.h" // Creates a new mailbox and Init it.
MAILBOX * MBX_Create( uint32_t MailSize, uint32_t MailCount )
{
uint32_t Size = ALIGN_UP( sizeof(MAILBOX), ) + MailSize * MailCount;
MAILBOX * Mailbox = (MAILBOX *) osMalloc( Size, osWaitForever );
if ( Mailbox == )
return Mailbox; uint8_t * Memory = //
(uint8_t *) ( ( (uint32_t) ( Mailbox ) ) + ALIGN_UP( sizeof(MAILBOX), ) ); MBX_Init( Mailbox, MailSize, MailCount, Memory ); return Mailbox;
} // Deletes a specified mailbox.
void MBX_Delete( MAILBOX * Mailbox )
{
osFree( Mailbox );
} // Clears all messages in a specified mailbox.
void MBX_Clear( MAILBOX * Mailbox )
{
Mailbox->ReadCount = ;
} // Returns the number of mail currently available in a specified mailbox.
uint32_t MBX_GetCount( MAILBOX * Mailbox )
{
return Mailbox->ReadCount;
} // Init a new mailbox.
void MBX_Init( MAILBOX * Mailbox, uint32_t MailSize, uint32_t MailCount,
void * Memory )
{
// Mailbox->Capacity = MailCount * MailSize;
Mailbox->MailSize = MailSize;
Mailbox->MailCount = MailCount;
Mailbox->Memory = Memory;
Mailbox->ReadIndex = ;
Mailbox->ReadCount = ;
} /* Stores a new message of a predefined size in a mailbox.
*
* 0: Message could not be stored (mailbox is full).
* 1: Success; message stored.
*/
uint32_t MBX_Write( MAILBOX * Mailbox, void * Mail )
{
if ( Mailbox->ReadCount == Mailbox->MailCount )
return ; uint32_t Value = osDisableInterrupt( ); uint32_t IndexToWrite = ( Mailbox->ReadIndex + Mailbox->ReadCount );
if ( IndexToWrite == Mailbox->MailCount )
IndexToWrite = ; uint32_t MemoryIndexToWrite = IndexToWrite * Mailbox->MailSize;
memcpy( &Mailbox->Memory[ MemoryIndexToWrite ], Mail, Mailbox->MailSize );
Mailbox->ReadCount++; osRestoreInterrupt( Value ); return ;
} /*
* Stores a new message of a predefined size into a mailbox in front of all
* other messages, if the mailbox is able to accept one more message.
* The new message will be retrieved first.
*
* 0: Message could not be stored (mailbox is full).
* 1: Success; message stored.
*/
uint32_t MBX_WriteFront( MAILBOX * Mailbox, void * Mail )
{
if ( Mailbox->ReadCount == Mailbox->MailCount )
return ; if ( Mailbox->ReadCount == )
return MBX_Write( Mailbox, Mail ); uint32_t Value = osDisableInterrupt( ); uint32_t IndexToWrite; if ( Mailbox->ReadIndex )
IndexToWrite = Mailbox->ReadIndex - ;
else
IndexToWrite = Mailbox->MailCount - ; uint32_t MemoryIndexToWrite = IndexToWrite * Mailbox->MailSize;
memcpy( &Mailbox->Memory[ MemoryIndexToWrite ], Mail, Mailbox->MailSize ); Mailbox->ReadIndex = IndexToWrite;
Mailbox->ReadCount++; osRestoreInterrupt( Value ); return ;
} /* Retrieves a new message of a predefined size from a mailbox.
* Mail : Pointer to the memory area that the message should be stored at.
* Make sure that it points to a valid memory area and that there is sufficient
* space for an entiremessage. The message size (in bytes) was defined
* when the mailbox was created.
*
* 0: Message could not be retrieved (mailbox is empty)
* 1: Success; message retrieved.
*/
uint32_t MBX_Read( MAILBOX * Mailbox, void * Mail )
{
if ( Mailbox->ReadCount == )
return ; uint32_t Value = osDisableInterrupt( ); uint32_t MemoryIndexToRead = Mailbox->ReadIndex * Mailbox->MailSize;
memcpy( Mail, &Mailbox->Memory[ MemoryIndexToRead ], Mailbox->MailSize ); Mailbox->ReadIndex++;
if ( Mailbox->ReadIndex == Mailbox->MailCount )
Mailbox->ReadIndex = ;
Mailbox->ReadCount--; osRestoreInterrupt( Value ); return ;
}
Mailbox and Mail的更多相关文章
- DNS报文格式(RFC1035)
一.域名和资源记录的定义 1.Name space definitions 2.资源记录定义(RR definitions) 2.1 格式 后面分析报文的时候详细解释. ...
- DNS 中的协议字段详细定义
DNS中的协议字段定义 Table of Contents 1 概述 2 DNS Classes 3 DNS OpCodes 4 DNS RCODEs 5 DNS Label Types 6 DNS资 ...
- TCP/UDP端口列表
http://zh.wikipedia.org/wiki/TCP/UDP%E7%AB%AF%E5%8F%A3%E5%88%97%E8%A1%A8 TCP/UDP端口列表 本条目可通过翻译外语维 ...
- 【转载】Powershell获取世纪互联Office365所有用户最后一次登录时间
#$Mails=get-mailbox -ResultSize 10 $Mails=get-mailbox -ResultSize Unlimited $Mails | Measure-Object ...
- conky 配置变量表
转自conky 配置变量表 项目主页:http://conky.sourceforge.net/ 文档说明:http://conky.sourceforge.net/docs.html Variabl ...
- 《Linux命令行与shell脚本编程大全》 第十五章 学习笔记
第十五章:控制脚本 处理信号 重温Linux信号 信号 名称 描述 1 HUP 挂起 2 INT 中断 3 QUIT 结束运行 9 KILL 无条件终止 11 SEGV 段错误 15 TERM 尽可能 ...
- 计算机网络——DNS协议的学习与实现
1. 主要内容 不说废话,直接进入正题.先说说本文本文的主要内容,好让你决定是否看下去: 介绍DNS是干什么的: 介绍DNS是如何工作的: 介绍DNS请求与响应的消息格式: 编程实现一个简单的DNS服 ...
- 邮件服务器 postfix
背景介绍 邮件服务器普遍需要一个主机名来使得mail from 以"账号@主机名"方式显示.由于外网上垃圾邮件太多,现在已不使用ip发邮件,很多网络供应商都会对来源不明的邮件进行限 ...
- TCP/UDP 常用端口列表
计算机之间依照互联网传输层TCP/IP协议不同的协议通信,都有不同的对应端口.所以,利用短信(datagram)的UDP,所采用的端口号码不一定和采用TCP的端口号码一样.以下为两种通信协议的端口列表 ...
随机推荐
- C#格式化小数位的方法
C#格式化字符串还是很方便的,之前一直没用过,也就没有缘分了解 Double dValue = 95.12345; int iValue = 10000; string strValue = &quo ...
- DB2因表空间不够产生load表失败
今天下午恢复表的时候发现出现错误: SQL3520W Load Consistency Point was successful. SQL3110N The utility has complet ...
- C# 中对WinForm窗体中的控件快速设置TableIndex次序
点击“视图”--“Tab键顺序”,然后便可设置.
- Android 动画深入解析
http://blog.csdn.net/rain_butterfly/article/details/39642613
- 怎样为EXCEL2010添加下拉列表
注意,下面是Excel2010的步骤和截图,其他版本的Excel类似. 首先用鼠标左键点击你要添加下拉列表的单元格. 如果你只想部分区域有下拉列表,也可以选择部分区域. 下面图片是选择的整个列都是 ...
- INDEX_JOIN
这里就以INDEX_JOIN为例,简单描述一下如何影响Oracle的执行计划的产生. 控制执行计划最简单的方法莫过于使用HINT,这篇文章要介绍的是,在不使用HINT的情况下,让Oracle产生IND ...
- webstorm启动bug
场景描述: win10系统下,webstorm(32位)经常遇到无法启动的情况. 解决方案: 重启电脑. 1.win10系统需要更新时,webstorm无法启动,此为win10 bug,重启时,系统自 ...
- 阿里巴巴2013年实习生笔试题B
阿里巴巴集团2013实习生招聘技术类笔试题(B) 一.单向选择题 1.在常用的网络协议中,___B__是面向连接的.有重传功能的协议. A. IP B. TCP C. UDP D. DXP 2.500 ...
- Scrum角色
产品负责人(Product Owner)的职责如下: 确定产品的功能. 决定发布的日期和发布内容. 为产品的profitability of the product (ROI)负责. 根据 ...
- 原生JS取代一些JQuery方法
1.选取元素 // jQuery var els = $('.el'); // Native var els = document.querySelectorAll('.el'); // Shorth ...