资源简介
海思平台代码读取某个GPIO口的值,方便用户在官方SDK去掉API读值的情况下使用
代码片段和文件信息
#include
#include
#include
#include
#include
#include
typedef struct tag_MMAP_Node
{
unsigned int Start_P;
unsigned int Start_V;
unsigned int length;
unsigned int refcount;
struct tag_MMAP_Node * next;
}TMMAP_Node_t;
TMMAP_Node_t * pTMMAPNode = NULL;
#define PAGE_SIZE 0x1000
#define PAGE_SIZE_MASK 0xfffff000
static int fd = -1;
static const char dev[]=“/dev/mem“;
#define ASC2NUM(ch) (ch - ‘0‘)
#define HEXASC2NUM(ch) (ch - ‘A‘ + 10)
#define DEFAULT_MD_LEN 128
void * memmap(unsigned int phy_addr unsigned int size)
{
unsigned int phy_addr_in_page;
unsigned int page_diff;
unsigned int size_in_page;
TMMAP_Node_t * pTmp;
TMMAP_Node_t * pNew;
void *addr=NULL;
if(size == 0)
{
printf(“memmap():size can‘t be zero!\n“);
return NULL;
}
/* check if the physical memory space have been mmaped */
pTmp = pTMMAPNode;
while(pTmp != NULL)
{
if( (phy_addr >= pTmp->Start_P) &&
( (phy_addr + size) <= (pTmp->Start_P + pTmp->length) ) )
{
pTmp->refcount++; /* referrence count increase by 1 */
return (void *)(pTmp->Start_V + phy_addr - pTmp->Start_P);
}
pTmp = pTmp->next;
}
/* not mmaped yet */
if(fd < 0)
{
/* dev not opened yet so open it */
fd = open (dev O_RDWR | O_SYNC);
if (fd < 0)
{
printf(“memmap():open %s error!\n“ dev);
return NULL;
}
}
/* addr align in page_size(4K) */
phy_addr_in_page = phy_addr & PAGE_SIZE_MASK;
page_diff = phy_addr - phy_addr_in_page;
/* size in page_size */
size_in_page =((size + page_diff - 1) & PAGE_SIZE_MASK) + PAGE_SIZE;
addr = mmap ((void *)0 size_in_page PROT_READ|PROT_WRITE MAP_SHARED fd phy_addr_in_page);
if (addr == MAP_FAILED)
{
printf(“memmap():mmap @ 0x%x error!\n“ phy_addr_in_page);
return NULL;
}
/* add this mmap to MMAP Node */
pNew = (TMMAP_Node_t *)malloc(sizeof(TMMAP_Node_t));
if(NULL == pNew)
{
printf(“memmap():malloc new node failed!\n“);
return NULL;
}
pNew->Start_P = phy_addr_in_page;
pNew->Start_V = (unsigned int)addr;
pNew->length = size_in_page;
pNew->refcount = 1;
pNew->next = NULL;
if(pTMMAPNode == NULL)
{
pTMMAPNode = pNew;
}
else
{
pTmp = pTMMAPNode;
while(pTmp
相关资源
- 编译的中间代码四元式生成
- 数据采集卡ISA驱动程序源代码
- 原创透明加解密-AES等长加密算法含
- 基于STM32的SPI驱动HMC5983的代码
- 三轴陀螺仪MPU6050基于STM32F103的IIC驱动
- VHDL产生方波的源代码
- Leaflet动态风场图代码
- cmpp2开发代码包
- 打地鼠游戏flash实现全过程
- Swift-UI控件的全部使用代码
- 无卡cdma校时模块参考代码
- Ov9650-Coms摄像头在Linux环境下的代码开
- 简单的二维装箱代码
- jpeg-ls无损压缩C代码
- 网络聊天室程序源代码
- 触摸板的库源代码
- Ns网络模拟和协议仿真
- 最全的机场三字、四字代码表
- PCI运动控制卡VC代码
- 人工智能 8数码问题 3个实验
- s3c2440下LED灯驱动的源代码
- DSP芯片的uPP接口与FPGA通信代码
- 超声波程序代码
- msp430代码模拟spi
- QT和linux简易远程聊天源代码
- 《学习opecnv》第十二章鸟瞰图变换可
- K-近邻算法识别手写数字代码以及原始
- Qt服务器1源代码
- 国家行政区及代码
- 内存乒乓缓存机制和消息分发机制的
评论
共有 条评论