uclinux stm32移植enc28j60笔记

移植enc28j60笔记


enc28j60移植第一步初始化接口
1,spi接口
2,外部中断接口


平台设备注册


arch/arm/mach-stm32/spi.c
注册平台设备
#if defined(CONFIG_STM32_SPI4)
platform_set_drvdata(&spi_stm32_dev4, 
(void *) stm32_clock_get(CLOCK_PCLK2));//设置时钟
platform_device_register(&spi_stm32_dev4);
#endif


io初始化


arch/arm/mach-stm32/iomux.c


struct stm32f2_gpio_dsc gpio_dsc;//gpio指向结构体


struct stm32f2_gpio_dsc {
u32 port; /* GPIO port ap接口
     */
u32 pin; /* GPIO pin  ap接口下的pin      */
};
case STM32F2_GPIO_ROLE_SPI4
otype  = STM32F2_GPIO_OTYPE_PP;//输出
ospeed = STM32F2_GPIO_SPEED_50M;//io频率
pupd   = STM32F2_GPIO_PUPD_UP;//上下拉
break;




#if defined(CONFIG_STM32_SPI4)
gpio_dsc.port = 4; /* CLCK */
gpio_dsc.pin  = 2;




stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_SPI4);//初始化完成


gpio_dsc.port = 4; /* DI */
gpio_dsc.pin  = 5;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_SPI4);


gpio_dsc.port = 4; /* DO */
gpio_dsc.pin  = 6;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_SPI4);


gpio_dsc.port = 4; /* CS */
gpio_dsc.pin  = 4;
stm32f2_gpio_config(&gpio_dsc, STM32F2_GPIO_ROLE_OUT);
#endif




gpio操作函数 


kernel config 设置CONFIG_GPIOLIB =y
1获取gpio地址
#define STM32_GPIO_PORTPIN2NUM(port, pin) \
(STM32_GPIO_OFF + ((port) * STM32_GPIO_PORT_PINS + (pin)))


2设值函数
#define gpio_get_value __gpio_get_value//获取设置值
#define gpio_set_value __gpio_set_value//设置值
#define gpio_to_irq __gpio_to_irq//获取gpio中断
#define gpio_cansleep __gpio_cansleep//使能休眠
linux中断申请函数
int request_irq(unsigned int irq, irq_handler_t handler,
                         unsigned long irqflags, const char *devname, void *dev_id)
irq是要申请的硬件中断号。
handler是向系统注册的中断处理函数,是一个回调函数,中断发生时,系统调用这个函数,dev_id参数将被传递给它。
irqflags是中断处理的属性,若设置了IRQF_DISABLED (老版本中的SA_INTERRUPT,本版zhon已经不支持了),则表示中断处理程序是快速处理程序,快速处理程序被调用时屏蔽所有中断,慢速处理程序不屏蔽;若设置了IRQF_SHARED (老版本中的SA_SHIRQ),则表示多个设备共享中断,若设置了IRQF_SAMPLE_RANDOM(老版本中的SA_SAMPLE_RANDOM),表示对系统熵有贡献,对系统获取随机数有好处。(这几个flag是可以通过或的方式同时使用的)
devname设置中断名称,通常是设备驱动程序的名称  在cat /proc/interrupts中可以看到此名称。
dev_id在中断共享时会用到,一般设置为这个设备的设备结构体或者NULL。
request_irq()返回0表示成功,返回-INVAL表示中断号无效或处理函数指针为NULL,返回-EBUSY表示中断已经被占用且不能共享。


模块中断函数


static irqreturn_t enc28j60_irq(int irq, void *dev_id)
{
struct enc28j60_net *priv = dev_id;


/*
* Can't do anything in interrupt context because we need to
* block (spi_sync() is blocking) so fire of the interrupt
* handling workqueue.
* Remember that we access enc28j60 registers through SPI bus
* via spi_sync() call.
*/
*
在中断的情况下,不能做任何事情,因为block (spi_sync() is blocking)被阻塞。
*进入中断服务函数


*ENC28J60通过SPI总线开始数据通讯.

*通过spi_sync()调用。
*
schedule_work(&priv->irq_work);


stm32irq_clear();//释放中断
gpio_set_value(gpio_to_irq(STM32_GPIO_PORTPIN2NUM(4, 1)),0);//清理寄存器


return IRQ_HANDLED;
}
注册外部中断函数
static stm32irq_reg(void){
gpio_request(gpio_to_irq(STM32_GPIO_PORTPIN2NUM(4, 1)),ethirq);
}
释放中断函数
static stm32irq_clear(void){
free_irq(gpio_to_irq(STM32_GPIO_PORTPIN2NUM(4, 1)),ethirq);
}
获取pe1的gpio值
STM32_GPIO_PORTPIN2NUM(4, 1)
取gpio的中断号
gpio_to_irq(STM32_GPIO_PORTPIN2NUM(4, 1))
用这个替换probe里的中断服务函数
ret = request_irq(gpio_to_irq(STM32_GPIO_PORTPIN2NUM(4, 1)), enc28j60_irq, 0, ethirq, priv);


enc28j60中断函数,我们这里要修改为外部中断
ret = request_irq(spi->irq, enc28j60_irq, 0, DRV_NAME, priv);
修改
dev->irq = spi->irq;
为如下
dev->irq = gpio_to_irq(STM32_GPIO_PORTPIN2NUM(4, 1));




kernel编译设置
device drivers--->
    [*] spi_support --->


          [*] stm32 spi controller


device drivers--->


     [*]network device support --->


          [*]ethernet (10 or 100mbit) --->


                 [*] enc28j60 support


[*]networking support --->


      netowrking options---->


           [*] packet socket


           [*] unix domain sockets


           [*]tcp/ip networking

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值