shouchengcheng
just do it

驱动中的内存分配

#include <linux/slab.h>

void *kmalloc(size_t size, int flags);

这个函数是内核中使用的malloc

特点就是

1)速度快

2)不清零,分配的区仍然持有它原来的内容

3)物理内存中连续

4)用于分配的小内存,不能用于分配大内存

5)使用kfree释放分配的内存

 

#include <linux/vmalloc.h>

void *vmalloc(unsigned long size);

void vfree(void * addr);

在虚拟内存空间分配一块连续的内存区,尽管在物理内存中不连续

 

与硬件进行通信的原理

在linux中可以通过IO端口和硬件进行通信,也可以通过IO内存和硬件进行通信。

ARM体系结构下只有IO内存,没有IO端口,所以对外设的访问均使用IO内存,而不是IO端口。(估计是因为有硬件MMU的关系吧,个人瞎猜)

而在x86体系结构下存在IO端口,所以在x86下可以访问IO端口,但是由于IO端口并不是与内存进行统一编址的(它是独立编址的),所以x86下会提供一套有别于访问IO内存的指令。

 

IO内存操作函数

unsigned int ioread8(void * addr);
unsigned int ioread16(void * addr);
unsigned int ioread32(void * addr);

void iowrite8(u8 value, void * addr);
void iowrite16(u16 value, void * addr);
void iowrite32(u32 value, void * addr);

void ioread8_rep(void * addr, void * buf, unsigned long count);
void ioread16_rep(void * addr, void * buf, unsigned long count);
void ioread32_rep(void * addr, void * buf, unsigned long count);

void iowrite8_rep(void * addr,const void * buf, unsigned long count);
void iowrite16_rep(void * addr,const  void * buf, unsigned long count);
void iowrite32_rep(void * addr,const  void * buf, unsigned long count);

这是新的API,下面的是老的API

 

unsigned readb(address);
unsigned readw(address);
unsigned readl(address);

void writeb(unsigned value,address)
void writew(unsigned value,address)
void writel(unsigned value,address)

void __raw_writel(unsigned value,address)

 

这一节就到这里,下一节就开始动手写驱动了。先写一个LED的驱动吧,我今天回去先把我那板子的环境给搭起来。

posted on 2014-04-02 18:06  shouchengcheng  阅读(279)  评论(0编辑  收藏  举报