#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 ;
}

最新文章

  1. webService学习之路(二):springMVC集成CXF快速发布webService
  2. Tomcat源码导入eclipse的步骤
  3. 分享一个绿色版本 sql server 查询器,
  4. hdu 5106 组合数学+找规律
  5. java设计模式之抽象工厂模式
  6. django的前后的结合,search搜索功能案例
  7. ( 转)基于.NET平台常用的框架整理
  8. IOS枚举使用
  9. openwrt的交叉编译
  10. PHP笔试题汇总
  11. Ext.MessageBox的用法
  12. leetcode面试准备:Summary Ranges
  13. PHP获取中英文混合字符串长度及截取
  14. List 随机排序
  15. Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php
  16. 03.javabean
  17. Git是什么、Git的功能、为什么versioncontrol用Git、Git的常用命令、Git的优缺点
  18. Vue + Element UI 实现权限管理系统 前端篇(九):接口格式定义
  19. 学习笔记之100 TOP Ikm C++ Online Test Questions
  20. 使用Python编写简单网络爬虫抓取视频下载资源

热门文章

  1. hdu 1565(状态压缩基础题)
  2. C#隐式运行CMD命令(隐藏命令窗口)
  3. Files
  4. Nginx中的upstream轮询机制介绍
  5. 【转】CentOS 6.3 X64自动安装OpenERP 7.0脚本
  6. sqlserver数据可空插入报错
  7. 可进行JavaScript代码测试与调试的12个网站
  8. 小数点输出精度控制问题 .xml
  9. 网站压力测试工具-Webbench源码笔记
  10. hive 面试题 转载