//定义三个灯指针指向映射后的虚拟内存
unsigned int *vir_led1_moder;
unsigned int *vir_led1_odr;
unsigned int *vir_led2_moder;
unsigned int *vir_led2_odr;
unsigned int *vir_led3_moder;
unsigned int *vir_led3_odr;
unsigned int *vir_rcc;
ssize_t mycdev_write(struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
int ret;
ret = copy_from_user(kbuf, ubuf, size);
if(ret)
{
printk("copy_from_user filed\n");
return -EIO;
}
if (kbuf[0] == '0') //关灯
{
//关灯逻辑
(*vir_led1_odr) &= (~(0x1 << 10)); //默认关灯
(*vir_led2_odr) &= (~(0x1 << 10));
(*vir_led3_odr) &= (~(0x1 << 8));
}
else if (kbuf[0] == '1')
{
//开灯逻辑
(*vir_led1_odr) |= (0x1 << 10);
(*vir_led2_odr) |= (0x1 << 10);
(*vir_led3_odr) |= (0x1 << 8);
}
printk("%s:%s:%d\n",__FILE__, __func__, __LINE__);
return 0;
}
//进行寄存器的地址映射
vir_led1_moder = ioremap(PHY_LED1_MODER, 4);
if (vir_led1_moder == NULL)
{
printk("物理内存地址映射失败 %d\n", __LINE__);
return -EFAULT;
}
vir_led1_odr = ioremap(PHY_LED1_ODR, 4);
if (vir_led1_odr == NULL)
{
printk("物理内存地址映射失败 %d\n", __LINE__);
return -EFAULT;
}
vir_led2_moder = ioremap(PHY_LED2_MODER, 4);
if (vir_led2_moder == NULL)
{
printk("物理内存地址映射失败 %d\n", __LINE__);
return -EFAULT;
}
vir_led2_odr = ioremap(PHY_LED2_ODR, 4);
if (vir_led2_odr == NULL)
{
printk("物理内存地址映射失败 %d\n", __LINE__);
return -EFAULT;
}
vir_led3_moder = ioremap(PHY_LED3_MODER, 4);
if (vir_led3_moder == NULL)
{
printk("物理内存地址映射失败 %d\n", __LINE__);
return -EFAULT;
}
vir_led3_odr = ioremap(PHY_LED3_ODR, 4);
if (vir_led3_odr == NULL)
{
printk("物理内存地址映射失败 %d\n", __LINE__);
return -EFAULT;
}
vir_rcc = ioremap(PHY_RCC, 4);
if (vir_rcc == NULL)
{
printk("物理内存地址映射失败 %d\n", __LINE__);
return -EFAULT;
}
printk("寄存器内存映射成功\n");
(*vir_rcc) |= (0x3 << 4); //GPIOE、F控制器时钟使能
//LED1寄存器初始化
(*vir_led1_moder) &= (~(0x3 << 20)); //MODER[21:20] -> 20
(*vir_led1_moder) |= (0x1 << 20); //MODER[21:20] -> 01
(*vir_led1_odr) &= (~(0x1 << 10)); //默认关灯
//LED2寄存器初始化
(*vir_led2_moder) &= (~(0x3 << 20)); //MODER[21:20] -> 20
(*vir_led2_moder) |= (0x1 << 20); //MODER[21:20] -> 01
(*vir_led2_odr) &= (~(0x1 << 10)); //默认关灯
//LED3寄存器初始化
(*vir_led3_moder) &= (~(0x3 << 16)); //MODER[21:20] -> 20
(*vir_led3_moder) |= (0x1 << 16); //MODER[21:20] -> 01
(*vir_led3_odr) &= (~(0x1 << 8)); //默认关灯
//取消内存映射
iounmap(vir_led1_moder);
iounmap(vir_led1_odr);
iounmap(vir_led2_moder);
iounmap(vir_led2_odr);
iounmap(vir_led3_moder);
iounmap(vir_led3_odr);
iounmap(vir_rcc);
驱动 day2
最新推荐文章于 2024-08-22 17:10:39 发布