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的端口号码一样.以下为两种通信协议的端口列表 ...
随机推荐
- ORACLE常用脚本示例
create table DBO.INDEX_POLICY_TBL( ID NUMBER(10) NOT NULL PRIMARY KEY, POLICY_ID NUMBER(10,0) defaul ...
- C# 类的多态、结构、接口、抽象、虚函数总结
多态: 类的多态是通过在子类(派生类)中重载基类的虚方法或成员函数来实现的. 可见,重载和虚函数的重写,并在调用时用父类装箱子类对象,是实现多态的一种重要的编程方式. 接口: 接口是一种用来定义程序的 ...
- spring mvc 异常统一处理方式
springMVC提供的异常处理主要有两种方式: 一种是直接实现自己的HandlerExceptionResolver: 另一种是使用注解的方式实现一个专门用于处理异常的Controller——Exc ...
- Yii连接多个数据库的方法
一.配置多数据库 大多数情况下,我们都会采用同一类型的数据库,只是为了缓解压力分成主从或分布式形式而已.声明你可以在 主配置文件 ( main.php ) 中里声明其它的数据库连接: <?p ...
- selenium + python 自动化测试环境搭建
selenium + python 自动化测试 —— 环境搭建 关于 selenium Selenium 是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操 ...
- Topogun教学视频
http://www.iqiyi.com/w_19rrfss6dd.html http://www.iqiyi.com/w_19rrfsvo3h.html http://www.iqiyi.com/w ...
- linux 如何让程序在开机时启动,关机前关闭
可以将自己所写的script的文件名写入/etc/rc.d/rc.local(用户自定义开机启动程序) 中,在/etc/rc.d/init.d 中貌似也可以
- Java多线程的五种状态
新建状态:new Thread(参数)之后,建立了一个线程对象; 就绪状态:线程对象建立之后,调用start()方法,进入就绪状态,此时并不会直接调用run()方法,线程进入运行状态还需要抢占CPU资 ...
- 使用ncc分析代码
1 ncc是一个编译器, 用于输出程序的一些调用信息等, 可以查看函数调用关系, 支持函数指针, 查看数据结构和代码. 可以用来分析和理解代码. “" ... with ncc, in le ...
- web.xml 配置介绍
这个不是原创,有点早了,具体从哪里来的已经记不得了.但是东西是实实在在的. 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<c ...