day3 驱动开发 c语言编程

通过ioctl(内核+应用层)

控制led灯三盏,风扇,蜂鸣器,小马达

头文件head.h

#ifndef __LED_H__
#define __LED_H__typedef struct {volatile unsigned int TZCR;     	// 0x000volatile unsigned int res1[2]; 		// 0x004-0x008volatile unsigned int OCENSETR;     // 0x00Cvolatile unsigned int OCENCLRR;  	// 0x010volatile unsigned int res2[1]; 		// 0x014volatile unsigned int HSICFGR; 		// 0x018volatile unsigned int CSICFGR; 		// 0x01Cvolatile unsigned int MPCKSELR; 	// 0x020volatile unsigned int ASSCKSELR; 	// 0x024volatile unsigned int PCK12SELR; 	// 0x028volatile unsigned int MPCKDIVR; 	// 0x02Cvolatile unsigned int AXIDIVR; 		// 0x030volatile unsigned int res3[2];      volatile unsigned int APB4DIVR; 	// 0x03Cvolatile unsigned int APB5DIVR; 	// 0x040volatile unsigned int RTCDIVR; 		// 0x044volatile unsigned int MSSCKSELR;    // 0x048volatile unsigned int res4[13];volatile unsigned int PLL1CR; 		// 0x080volatile unsigned int PLL1CFGR1; 	// 0x084volatile unsigned int PLL1CFGR2; 	// 0x088volatile unsigned int PLL1FRACR; 	// 0x08Cvolatile unsigned int PLL1CSGR;     // 0x090volatile unsigned int PLL2CR; 		// 0x094volatile unsigned int PLL2CFGR1; 	// 0x098volatile unsigned int PLL2CFGR2; 	// 0x09Cvolatile unsigned int PLL2FRACR;    // 0x0A0volatile unsigned int PLL2CSGR;     // 0x0A4volatile unsigned int res5[6];volatile unsigned int I2C46CKSELR;  // 0x0C0volatile unsigned int SPI6CKSELR;   // 0x0C4volatile unsigned int UART1CKSELR;  // 0x0C8volatile unsigned int RNG1CKSELR;   // 0x0CCvolatile unsigned int CPERCKSELR;   // 0x0D0volatile unsigned int STGENCKSELR;  // 0x0D4volatile unsigned int DDRITFCR; 	// 0x0D8volatile unsigned int res6[9];volatile unsigned int MP_BOOTCR;  	// 0x100volatile unsigned int MP_SREQSETR;  // 0x104volatile unsigned int MP_SREQCLRR;  // 0x108volatile unsigned int MP_GCR;  		// 0x10Cvolatile unsigned int MP_APRSTCR; 	// 0x110 volatile unsigned int MP_APRSTSR;   // 0x114volatile unsigned int res7[10];volatile unsigned int BDCR; 		// 0x140volatile unsigned int RDLSICR;  	// 0x144volatile unsigned int res8[14];volatile unsigned int APB4RSTSETR; 	// 0x180volatile unsigned int APB4RSTCLRR; 	// 0x184volatile unsigned int APB5RSTSETR;  // 0x188volatile unsigned int APB5RSTCLRR;  // 0x18Cvolatile unsigned int AHB5RSTSETR;  // 0x190volatile unsigned int AHB5RSTCLRR;  // 0x194volatile unsigned int AHB6RSTSETR;  // 0x198volatile unsigned int AHB6RSTCLRR;  // 0x19Cvolatile unsigned int TZAHB6RSTSELR;// 0x1A0volatile unsigned int TZAHB6RSTCLRR;// 0x1A4volatile unsigned int res9[22];volatile unsigned int MP_APB4ENSETR;// 0x200volatile unsigned int MP_APB4ENCLRR;// 0x204volatile unsigned int MP_APB5ENSETR;// 0x208volatile unsigned int MP_APB5ENCLRR;// 0x20Cvolatile unsigned int MP_AHB5ENSETR;// 0x210volatile unsigned int MP_AHB5ENCLRR;// 0x214volatile unsigned int MP_AHB6ENSETR;// 0x218volatile unsigned int MP_AHB6ENCLRR;// 0x21Cvolatile unsigned int MP_TZAHB6ENSELR;// 0x220volatile unsigned int MP_TZAHB6ENCLRR;// 0x224volatile unsigned int res10[22];volatile unsigned int MC_APB4ENSETR; // 0x280volatile unsigned int MC_APB4ENCLRR; // 0x284volatile unsigned int MC_APB5ENSETR; // 0x288volatile unsigned int MC_APB5ENCLRR; // 0x28Cvolatile unsigned int MC_AHB5ENSETR; // 0x290volatile unsigned int MC_AHB5ENCLRR; // 0x294volatile unsigned int MC_AHB6ENSETR; // 0x298volatile unsigned int MC_AHB6ENCLRR; // 0x29Cvolatile unsigned int res11[24];volatile unsigned int MP_APB4LPENSETR; // 0x300volatile unsigned int MP_APB4LPENCLRR; // 0x304volatile unsigned int MP_APB5LPENSETR; // 0x308volatile unsigned int MP_APB5LPENCLRR; // 0x30Cvolatile unsigned int MP_AHB5LPENSETR; // 0x310volatile unsigned int MP_AHB5LPENCLRR; // 0x314volatile unsigned int MP_AHB6LPENSETR; // 0x318volatile unsigned int MP_AHB6LPENCLRR; // 0x31Cvolatile unsigned int MP_TZAHB6LPENSETR; // 0x320volatile unsigned int MP_TZAHB6LPENCLRR; // 0x324volatile unsigned int res12[22];volatile unsigned int MC_APB4LPENSETR; // 0x380volatile unsigned int MC_APB4LPENCLRR; // 0x384volatile unsigned int MC_APB5LPENSETR; // 0x388volatile unsigned int MC_APB5LPENCLRR; // 0x38Cvolatile unsigned int MC_AHB5LPENSETR; // 0x390volatile unsigned int MC_AHB5LPENCLRR; // 0x394volatile unsigned int MC_AHB6LPENSETR; // 0x398volatile unsigned int MC_AHB6LPENCLRR; // 0x39Cvolatile unsigned int res13[24];volatile unsigned int BR_RSTSCLRR; 		// 0x400volatile unsigned int MP_GRSTCSETR; 	// 0x404volatile unsigned int MP_RSTSR; 		// 0x408 volatile unsigned int MP_IWDGFZSETR; 	// 0x40Cvolatile unsigned int MP_IWDGFZCLRR;  	// 0x410volatile unsigned int MP_CIER; 			// 0x414volatile unsigned int MP_CIFR; 			// 0x418volatile unsigned int PWRLPDLYCR; 		// 0x41Cvolatile unsigned int MP_RSTSS; 		// 0x420volatile unsigned int res14[247];volatile unsigned int MCO1CFGR; 		// 0x800volatile unsigned int MCO2CFGR; 		// 0x804 volatile unsigned int OCRDYR; 			// 0x808volatile unsigned int DBGCFGR; 			// 0x80Cvolatile unsigned int res15[4];volatile unsigned int RCK3SELR; 		// 0x820volatile unsigned int RCK4SELR; 		// 0x824volatile unsigned int TIMG1PRER;  		// 0x828volatile unsigned int TIMG2PRER; 		// 0x82Cvolatile unsigned int MCUDIVR; 			// 0x830volatile unsigned int APB1DIVR; 		// 0x834volatile unsigned int APB2DIVR; 		// 0x838volatile unsigned int APB3DIVR; 		// 0x83Cvolatile unsigned int res16[16];volatile unsigned int PLL3CR;   		// 0x880volatile unsigned int PLL3CFGR1; 		// 0x884volatile unsigned int PLL3CFGR2; 		// 0x888volatile unsigned int PLL3FRACR; 		// 0x88Cvolatile unsigned int PLL3CSGR; 		// 0x890volatile unsigned int PLL4CR; 			// 0x894volatile unsigned int PLL4CFGR1; 		// 0x898volatile unsigned int PLL4CFGR2; 		// 0x89Cvolatile unsigned int PLL4FRACR; 		// 0x8A0volatile unsigned int PLL4CSGR; 		// 0x8A4volatile unsigned int res17[6];volatile unsigned int I2C12CKSELR; 		// 0x8C0volatile unsigned int I2C35CKSELR;  	// 0x8C4volatile unsigned int SAI1CKSELR; 		// 0x8C8volatile unsigned int SAI2CKSELR; 		// 0x8CCvolatile unsigned int SAI3CKSELR; 		// 0x8D0volatile unsigned int SAI4CKSELR; 		// 0x8D4volatile unsigned int SPI2S1CKSELR; 	// 0x8D8volatile unsigned int SPI2S23CKSELR; 	// 0x8DCvolatile unsigned int SPI45CKSELR; 		// 0x8E0volatile unsigned int UART6CKSELR; 		// 0x8E4volatile unsigned int UART24CKSELR; 	// 0x8E8volatile unsigned int UART35CKSELR; 	// 0x8ECvolatile unsigned int UART78CKSELR; 	// 0x8F0volatile unsigned int SDMMC12CKSELR; 	// 0x8F4volatile unsigned int SDMMC3CKSELR; 	// 0x8F8volatile unsigned int ETHCKSELR; 		// 0x8FCvolatile unsigned int QSPICKSELR; 		// 0x900volatile unsigned int FMCCKSELR; 		// 0x904volatile unsigned int res18[1];volatile unsigned int FDCANCKSELR; 		// 0x90Cvolatile unsigned int res19[1];volatile unsigned int SPDIFCKSELR; 		// 0x914volatile unsigned int CECCKSELR; 		// 0x918volatile unsigned int USBCKSELR; 		// 0x91Cvolatile unsigned int RNG2CKSELR;  		// 0x920volatile unsigned int DSICKSELR; 		// 0x924volatile unsigned int ADCCKSELR; 		// 0x928volatile unsigned int LPTIM45CKSELR; 	// 0x92Cvolatile unsigned int LPTIM23CKSELR;    // 0x930volatile unsigned int LPTIM1CKSELR; 	// 0x934volatile unsigned int res20[18];volatile unsigned int APB1RSTSETR; 		// 0x980volatile unsigned int APB1RSTCLRR; 		// 0x984volatile unsigned int APB2RSTSETR; 		// 0x988volatile unsigned int APB2RSTCLRR; 		// 0x98Cvolatile unsigned int APB3RSTSETR; 		// 0x990volatile unsigned int APB3RSTCLRR; 		// 0x994volatile unsigned int AHB2RSTSETR; 		// 0x998volatile unsigned int AHB2RSTCLRR;  	// 0x99Cvolatile unsigned int AHB3RSTSETR; 		// 0x9A0volatile unsigned int AHB3RSTCLRR; 		// 0x9A4volatile unsigned int AHB4RSTSETR; 		// 0x9A8volatile unsigned int AHB4RSTCLRR; 		// 0x9ACvolatile unsigned int res21[20];volatile unsigned int MP_APB1ENSETR; 	// 0xA00volatile unsigned int MP_APB1ENCLRR; 	// 0xA04volatile unsigned int MP_APB2ENSETR; 	// 0xA08volatile unsigned int MP_APB2ENCLRR;  	// 0xA0Cvolatile unsigned int MP_APB3ENSETR; 	// 0xA10volatile unsigned int MP_APB3ENCLRR; 	// 0xA14volatile unsigned int MP_AHB2ENSETR; 	// 0xA18volatile unsigned int MP_AHB2ENCLRR; 	// 0xA1Cvolatile unsigned int MP_AHB3ENSETR; 	// 0xA20volatile unsigned int MP_AHB3ENCLRR; 	// 0xA24volatile unsigned int MP_AHB4ENSETR; 	// 0xA28volatile unsigned int MP_AHB4ENCLRR; 	// 0xA2Cvolatile unsigned int res22[2];volatile unsigned int MP_MLAHBENSETR; 	// 0xA38volatile unsigned int MP_MLAHBENCLRR; 	// 0xA3Cvolatile unsigned int res23[16];volatile unsigned int MC_APB1ENSETR; 	// 0xA80volatile unsigned int MC_APB1ENCLRR; 	// 0xA84volatile unsigned int MC_APB2ENSETR; 	// 0xA88volatile unsigned int MC_APB2ENCLRR; 	// 0xA8Cvolatile unsigned int MC_APB3ENSETR; 	// 0xA90volatile unsigned int MC_APB3ENCLRR; 	// 0xA94volatile unsigned int MC_AHB2ENSETR; 	// 0xA98volatile unsigned int MC_AHB2ENCLRR; 	// 0xA9Cvolatile unsigned int MC_AHB3ENSETR; 	// 0xAA0volatile unsigned int MC_AHB3ENCLRR; 	// 0xAA4volatile unsigned int MC_AHB4ENSETR; 	// 0xAA8volatile unsigned int MC_AHB4ENCLRR; 	// 0xAACvolatile unsigned int MC_AXIMENSETR; 	// 0xAB0volatile unsigned int MC_AXIMENCLRR; 	// 0xAB4volatile unsigned int MC_MLAHBENSETR; 	// 0xAB8volatile unsigned int MC_MLAHBENCLRR; 	// 0xABCvolatile unsigned int res24[16];volatile unsigned int MP_APB1LPENSETR; 	// 0xB00volatile unsigned int MP_APB1LPENCLRR; 	// 0xB04volatile unsigned int MP_APB2LPENSETR;  // 0xB08volatile unsigned int MP_APB2LPENCLRR; 	// 0xB0Cvolatile unsigned int MP_APB3LPENSETR; 	// 0xB10volatile unsigned int MP_APB3LPENCLRR;  // 0xB14volatile unsigned int MP_AHB2LPENSETR;  // 0xB18volatile unsigned int MP_AHB2LPENCLRR;  // 0xB1Cvolatile unsigned int MP_AHB3LPENSETR;  // 0xB20volatile unsigned int MP_AHB3LPENCLRR;  // 0xB24volatile unsigned int MP_AHB4LPENSETR;  // 0xB28volatile unsigned int MP_AHB4LPENCLRR;  // 0xB2Cvolatile unsigned int MP_AXIMLPENSETR;  // 0xB30volatile unsigned int MP_AXIMLPENCLRR;  // 0xB34volatile unsigned int MP_MLAHBLPENSETR; // 0xB38volatile unsigned int MP_MLAHBLPENCLRR; // 0xB3Cvolatile unsigned int res25[16];volatile unsigned int MC_APB1LPENSETR;  // 0xB80volatile unsigned int MC_APB1LPENCLRR; 	// 0xB84volatile unsigned int MC_APB2LPENSETR;  // 0xB88volatile unsigned int MC_APB2LPENCLRR;  // 0xB8Cvolatile unsigned int MC_APB3LPENSETR;  // 0xB90 volatile unsigned int MC_APB3LPENCLRR;  // 0xB94volatile unsigned int MC_AHB2LPENSETR;  // 0xB98volatile unsigned int MC_AHB2LPENCLRR;  // 0xB9Cvolatile unsigned int MC_AHB3LPENSETR;  // 0xBA0 volatile unsigned int MC_AHB3LPENCLRR;  // 0xBA4volatile unsigned int MC_AHB4LPENSETR;  // 0xBA8volatile unsigned int MC_AHB4LPENCLRR;  // 0xBACvolatile unsigned int MC_AXIMLPENSETR;  // 0xBB0volatile unsigned int MC_AXIMLPENCLRR;  // 0xBB4volatile unsigned int MC_MLAHBLPENSETR; // 0xBB8volatile unsigned int MC_MLAHBLPENCLRR; // 0xBBCvolatile unsigned int res26[16];volatile unsigned int MC_RSTSCLRR;  	// 0xC00volatile unsigned int res27[4];volatile unsigned int MC_CIER;  		// 0xC14volatile unsigned int MC_CIFR; 			// 0xC18volatile unsigned int res28[246];volatile unsigned int VERR; 			// 0xFF4volatile unsigned int IDR; 				// 0xFF8volatile unsigned int SIDR; 			// 0xFFC
}rcc_t;#define RCC   ((rcc_t *)0x50000000)typedef struct {volatile unsigned int MODER;   // 0x00volatile unsigned int OTYPER;  // 0x04volatile unsigned int OSPEEDR; // 0x08volatile unsigned int PUPDR;   // 0x0Cvolatile unsigned int IDR;     // 0x10volatile unsigned int ODR;     // 0x14volatile unsigned int BSRR;    // 0x18volatile unsigned int LCKR;    // 0x1C volatile unsigned int AFRL;    // 0x20 volatile unsigned int AFRH;    // 0x24volatile unsigned int BRR;     // 0x28volatile unsigned int res;volatile unsigned int SECCFGR; // 0x30}gpio_t;#define  GPIOA   ((gpio_t *)0x50002000)
#define  GPIOB   ((gpio_t *)0x50003000)
#define  GPIOC   ((gpio_t *)0x50004000)
#define  GPIOD   ((gpio_t *)0x50005000)
#define  GPIOE   ((gpio_t *)0x50006000)
#define  GPIOF   ((gpio_t *)0x50007000)
#define  GPIOG   ((gpio_t *)0x50008000)
#define  GPIOH   ((gpio_t *)0x50009000)
#define  GPIOI   ((gpio_t *)0x5000A000)
#define  GPIOJ   ((gpio_t *)0x5000B000)
#define  GPIOK   ((gpio_t *)0x5000C000)
#define  GPIOZ   ((gpio_t *)0x54004000)#define LED1_ON _IOW('l',11,int)
#define LED1_OFF _IOW('l',10,int)
#define LED2_ON _IOW('l',21,int)
#define LED2_OFF _IOW('l',20,int)
#define LED3_ON _IOW('l',31,int)
#define LED3_OFF _IOW('l',30,int)
#define FAN_ON _IOW('f',1,int)
#define FAN_OFF _IOW('f',0,int)
#define BUZ_ON _IOW('b',1,int)
#define BUZ_OFF _IOW('b',0,int)
#define MOT_ON _IOW('m',1,int)
#define MOT_OFF _IOW('m',0,int)
#endif

内核代码

ioctldev.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/device.h>
#include "head.h"//定义四个变量保存主设备号
unsigned int major_led;
unsigned int major_fan;
unsigned int major_buz;
unsigned int major_mot;//定义多个寄存器的虚拟地址
unsigned int *vir_gpiob_moder = NULL;
unsigned int *vir_gpiob_odr = NULL;
unsigned int *vir_gpioe_moder = NULL;
unsigned int *vir_gpioe_odr = NULL;
unsigned int *vir_gpiof_moder = NULL;
unsigned int *vir_gpiof_odr = NULL;
unsigned int *vir_rcc = NULL;
//定义四个向上提交目录的信息struct class
struct class *cls_led;
struct class *cls_fan;
struct class *cls_buz;
struct class *cls_mot;
//定义四个向上提交设备节点信息struct device
struct device *dev_led;
struct device *dev_fan;
struct device *dev_buz;
struct device *dev_mot;
//封装操作方法
int mycdev_open(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}long mycdev_ioctl(struct file *file, unsigned int request, unsigned long args)
{//args == 0 时,表示操作灯if(args == 0){//开led1if(request == LED1_ON){*vir_gpioe_odr |= 0x1 << 10;}//关led1else if(request == LED1_OFF){*vir_gpioe_odr &= ~(0x1 << 10);}//开led2else if(request == LED2_ON){*vir_gpiof_odr |= 0x1 << 10;}//关led2else if(request == LED2_OFF){*vir_gpiof_odr &= ~(0x1 << 10);}//开led3else if(request == LED3_ON){*vir_gpioe_odr |= 0x1 << 8;}//关led3else if(request == LED3_OFF){*vir_gpioe_odr &= ~(0x1 << 8);}}//操作风扇else if(args == 1){//打开风扇if(request == FAN_ON){*vir_gpioe_odr |= 0x1 << 9;}//关闭风扇else if(request == FAN_OFF){*vir_gpioe_odr &= ~(0x1 << 9);}}//操作蜂鸣器else if(args == 2){//打开蜂鸣器if(request == BUZ_ON){*vir_gpiob_odr |= 0x1 << 6;}//关闭蜂鸣器else if(request == BUZ_OFF){*vir_gpiob_odr &= ~(0x1 << 6);}}else if(args == 3){if(request == MOT_ON){*vir_gpiof_odr |= 0x1 << 6;}else if(request == MOT_OFF){*vir_gpiof_odr &= ~(0x1 << 6);}}return 0;
}int mycdev_close(struct inode *inode, struct file *file)
{printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);return 0;
}struct file_operations fops = {.open = mycdev_open,.unlocked_ioctl = mycdev_ioctl,.release = mycdev_close,
};static int __init mycdev_init(void)
{//定义一个long类型(匹配ioremap函数第一个参数),用来保存每个寄存器的地址信息volatile unsigned long tmp = 0;//定义一个led设备数量变量int led_num = 0;//注册设备,获得主设备号major_led = register_chrdev(0, "led", &fops);if (major_led < 0) {printk("注册字符设备驱动失败\n");return major_led;}printk("注册字符设备led驱动成功\n");major_fan = register_chrdev(0, "fan", &fops);if (major_fan < 0) {printk("注册字符设备fan驱动失败\n");return major_fan;}printk("注册字符设备驱动成功\n");major_buz = register_chrdev(0, "buz", &fops);if (major_buz < 0) {printk("注册字符设备驱动失败\n");return major_buz;}printk("注册字符设备buz驱动成功\n");major_mot = register_chrdev(0, "mot", &fops);if (major_mot < 0) {printk("注册字符设备驱动失败\n");return major_mot;}printk("注册字符设备mot驱动成功\n");//初始化几个寄存器,将物理地址映射到虚拟地址,以方便用户空间进行读写操作//将地址信息强转为long类型方便tmp保存tmp = (long)&GPIOB->MODER;//将寄存器的物理地址映射到虚拟地址并保存vir_gpiob_moder = ioremap(tmp, 4);if (vir_gpiob_moder == NULL) {printk("映射物理内存失败\n");return -EFAULT;}//将地址信息强转为long类型方便tmp保存tmp = (long)&GPIOE->MODER;//将寄存器的物理地址映射到虚拟地址并保存vir_gpioe_moder = ioremap(tmp, 4);if (vir_gpioe_moder == NULL) {printk("映射物理内存失败\n");return -EFAULT;}//将地址信息强转为long类型方便tmp保存tmp = (long)&GPIOF->MODER;//将寄存器的物理地址映射到虚拟地址并保存vir_gpiof_moder = ioremap(tmp, 4);if (vir_gpioe_moder == NULL) {printk("映射物理内存失败\n");return -EFAULT;}//将地址信息强转为long类型方便tmp保存tmp = (long)&GPIOB->ODR;//将寄存器的物理地址映射到虚拟地址并保存vir_gpiob_odr = ioremap(tmp, 4);if (vir_gpiob_odr == NULL) {printk("映射物理内存失败\n");return -EFAULT;}//将地址信息强转为long类型方便tmp保存tmp = (long)&GPIOE->ODR;//将寄存器的物理地址映射到虚拟地址并保存vir_gpioe_odr = ioremap(tmp, 4);if (vir_gpioe_odr == NULL) {printk("映射物理内存失败\n");return -EFAULT;}//将地址信息强转为long类型方便tmp保存tmp = (long)&GPIOF->ODR;//将寄存器的物理地址映射到虚拟地址并保存vir_gpiof_odr = ioremap(tmp, 4);if (vir_gpiof_odr == NULL) {printk("映射物理内存失败\n");return -EFAULT;}//将地址信息强转为long类型方便tmp保存tmp = (long)&RCC->MP_AHB4ENSETR;//将寄存器的物理地址映射到虚拟地址并保存vir_rcc = ioremap(tmp, 4);if (vir_rcc == NULL) {printk("映射物理内存失败\n");return -EFAULT;}printk("映射物理内存成功\n");//初始化几个寄存器的值//设置RCC_MP_AHB4ENSETR寄存器第1第4第5两个引脚为1,使能GPIOB,GPIOE,GPIOF*vir_rcc |= (0b11001 << 1);//设置GPIOB_MODER第12-13位为01(TIM4)*vir_gpiob_moder &= ~(0b11 << 12);*vir_gpiob_moder |= (0b01 << 12);//设置GPIOE_MODER第20-21位为01(LED1),第18-19位为01(TIM1),第16-17位为01(LED3输出)*vir_gpioe_moder &= ~(0b111111 << 16);*vir_gpioe_moder |= (0b010101 << 16);//设置GPIOF_MODER第20-21位为01(LED2),第12-13位为01(TIM16)*vir_gpiof_moder &= ~(0b11 << 20);*vir_gpiof_moder |= (0b01 << 20);*vir_gpiof_moder &= ~(0b11 << 12);*vir_gpiof_moder |= (0b01 << 12);//设置GPIOB_ODR第6位为0(TIM4)*vir_gpiob_odr &= ~(0b1 << 6);//设置GPIOE_ODR第10位为0(LED1),第9位为0(TIM1),第8位为0(LED3)*vir_gpioe_odr &= ~(0b111 << 8);//设置GPIOF_ODR第10位为0(LED2),第6位为0(TIM16)*vir_gpiof_odr &= ~(0b1 << 10);*vir_gpiof_odr &= ~(0b1 << 6);printk("寄存器初始化成功\n");//向上提交struct class设备目录信息cls_led = class_create(THIS_MODULE, "my_led");if (IS_ERR_VALUE(cls_led)) {printk("向上提交目录失败\n");return -PTR_ERR(cls_led);}//向上提交struct device设备节点信息for (led_num = 0; led_num < 3; led_num++) {dev_led =device_create(cls_led, NULL, MKDEV(major_led, led_num),NULL, "LED%d", led_num);if (IS_ERR(dev_led)) {printk("向上提交设备节点信息失败\n");return -PTR_ERR(dev_led);}}printk("向上提交设备节点信息成功\n");//向上提交struct class设备目录信息cls_fan = class_create(THIS_MODULE, "my_fan");if (IS_ERR_VALUE(cls_fan)) {printk("向上提交目录失败\n");return -PTR_ERR(cls_fan);}//向上提交struct device设备节点信息dev_fan = device_create(cls_fan, NULL, MKDEV(major_fan, 0), NULL,"FAN");if (IS_ERR(dev_fan)) {printk("向上提交设备节点信息失败\n");return -PTR_ERR(dev_fan);}printk("向上提交设备节点信息成功\n");//向上提交struct class设备目录信息cls_buz = class_create(THIS_MODULE, "my_buz");if (IS_ERR_VALUE(cls_buz)) {printk("向上提交目录失败\n");return -PTR_ERR(cls_buz);}//向上提交struct device设备节点信息dev_buz = device_create(cls_buz, NULL, MKDEV(major_buz, 0), NULL,"BUZ");if (IS_ERR(dev_buz)) {printk("向上提交设备节点信息失败\n");return -PTR_ERR(dev_buz);}//向上提交struct class设备目录信息cls_mot = class_create(THIS_MODULE, "my_mot");if (IS_ERR_VALUE(cls_mot)) {printk("向上提交目录失败\n");return -PTR_ERR(cls_mot);}//向上提交struct device设备节点信息dev_mot = device_create(cls_mot, NULL, MKDEV(major_mot, 0), NULL,"MOT");if (IS_ERR(dev_mot)) {printk("向上提交设备节点信息失败\n");return -PTR_ERR(dev_mot);}return 0;
}static void __exit mycdev_exit(void)
{//定义一个led设备数量变量int led_num = 0;//销毁设备节点信息for (led_num = 0; led_num < 3; led_num++) {device_destroy(cls_led, MKDEV(major_led, led_num));}device_destroy(cls_fan, MKDEV(major_fan, 0));device_destroy(cls_buz, MKDEV(major_buz, 0));device_destroy(cls_mot, MKDEV(major_mot, 0));//销毁设备目录class_destroy(cls_led);class_destroy(cls_fan);class_destroy(cls_buz);class_destroy(cls_mot);//取消物理内存映射iounmap(vir_gpiob_moder);iounmap(vir_gpioe_moder);iounmap(vir_gpiof_moder);iounmap(vir_gpiob_odr);iounmap(vir_gpioe_odr);iounmap(vir_gpiof_odr);iounmap(vir_rcc);printk("设备卸载\n");//取消注册设备信息unregister_chrdev(major_led, "led");unregister_chrdev(major_fan, "fan");unregister_chrdev(major_buz, "buz");unregister_chrdev(major_mot, "mot");
}module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用层代码

ioctl.c

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include "head.h"int main()
{int dev = 0;char buf[128] = {0};fprintf(stdout,"调用open\n");int fd_led = open("/dev/LED0",O_RDWR);if( fd_led < 0){perror("");exit(-1);}int fd_fan = open("/dev/FAN",O_RDWR);if( fd_fan < 0){perror("");exit(-1);}int fd_buz = open("/dev/BUZ",O_RDWR);if( fd_buz < 0){perror("");exit(-1);}int fd_mot = open("/dev/MOT",O_RDWR);if( fd_mot < 0){perror("");exit(-1);}while(1){fprintf(stdout,"请输入开关选项:\n");fprintf(stdout,"先输入0123控制器件,再紧跟输入1开,0关\n");fprintf(stdout,"11开1号灯,10关1号灯以此类推\n");fprintf(stdout,"输入q来结束\n");fgets(buf,sizeof(buf),stdin);buf[strlen(buf) - 1] = '\0';//如果输入的是q或Q,结束循环if(buf[0] == 'q' || buf[0] == 'Q'){break;}else if(buf[0] == '0'){dev = 0;if(buf[1] == '1' && buf[2] == '0'){ioctl(fd_led, LED1_OFF, dev);}else if(buf[1] == '1' && buf[2] == '1'){ioctl(fd_led, LED1_ON, dev);}else if(buf[1] == '2' && buf[2] == '0'){ioctl(fd_led, LED2_OFF, dev);}else if(buf[1] == '2' && buf[2] == '1'){ioctl(fd_led, LED2_ON, dev);}else if(buf[1] == '3' && buf[2] == '0'){ioctl(fd_led, LED3_OFF, dev);}else if(buf[1] == '3' && buf[2] == '1'){ioctl(fd_led, LED3_ON, dev);}}else if(buf[0] == '1'){dev = 1;if(buf[1] == '0'){ioctl(fd_fan, FAN_OFF, dev);}else if(buf[1] == '1'){ioctl(fd_fan, FAN_ON, dev);}}else if(buf[0] == '2'){dev = 2;if(buf[1] == '0'){ioctl(fd_buz, BUZ_OFF, dev);}else if(buf[1] == '1'){ioctl(fd_buz, BUZ_ON, dev);}}else if(buf[0] == '3'){dev = 3;if(buf[1] == '0'){ioctl(fd_mot, MOT_OFF, dev);}else if(buf[1] == '1'){ioctl(fd_mot, MOT_ON, dev);}}}close(fd_led);close(fd_fan);close(fd_buz);close(fd_mot);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/13555.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vscode远程调试配置

配置目标服务器免密登录 1.本地生成一组公私钥 输入ssh-keygen -t rsa&#xff0c;三次回车 2.公钥拷贝到远程机器&#xff08;注意要用git bash执行&#xff09; ssh-copy-id -i ~/.ssh/id_rsa.pub 用户名目标服务器ip 3.重启目标服务器ssh服务 service sshd restart 4.测…

css实现鼠标滑动左下角弹框带动画效果

代码 <div classNamekuang></div> css代码 .kuang {height: 500px;width: 400px;// background-color: #fff;position: absolute;z-index: 10;bottom: 0;transform: translateX(-390px)}.kuang:hover {animation: myanimation 3s linear 1;animation-fill-mode:f…

Flowable-中间事件-空中间抛出事件

定义 空中间抛出事件是一个 Throwing 事件&#xff0c;在 intermediateThrowEvent 元素下不加入任何的事件定 义元素&#xff0c;就构成一个空中间抛出事件。它通常用于表示流程中的某个状态&#xff0c;在实际使用的过程中可 以通过添加执行监听器&#xff0c;来表示流程状态…

算法通关村第二关——两两交换链表中的节点的问题解析

题目类型 链表反转 题目描述 给你一个链表&#xff0c;两两交换其中相邻的节点&#xff0c;并返回交换后链表的头节点&#xff0c;且必须在不修改节点内部的值的情况下完成本题&#xff0c;即&#xff1a;只能进行节点交换 效果图 题目分析 如果原始顺序是 dummy(虚拟头节点) …

Linux学习之系统函数库

cat /etc/redhat-release看到操作系统的版本是CentOS Linux release 7.6.1810 (Core)&#xff0c;uname -r可以看到内核版本是3.10.0-957.21.3.el7.x86_64&#xff0c;bash --version可以bash的版本是4.2.46。 在/etc/init.d/functions有许多系统定义好的函数&#xff0c;比如…

Swift 对象数组去重

使用 reduce 方法去重 使用 reduce 方法结合 contains 方法可以实现去重。reduce 方法用于将数组的元素进行累积计算&#xff0c;而 contains 方法用于检查元素是否已经存在于结果数组中。 struct SearchRecord: Equatable {let id: Intlet name: String }let records [Sear…

基于ChatGPT聊天的零样本信息提取7.25

基于ChatGPT聊天的零样本信息提取 摘要介绍ChatIE用于零样本IE的多轮 QA 实验总结 摘要 零样本信息提取&#xff08;IE&#xff09;旨在从未注释的文本中构建IE系统。由于很少涉及人类干预&#xff0c;因此具有挑战性。 零样本IE减少了数据标记所需的时间和工作量。最近对大型…

DevOps-Jenkins

Jenkins Jenkins是一个可扩展的持续集成引擎&#xff0c;是一个开源软件项目&#xff0c;旨在提供一个开放易用的软件平台&#xff0c;使软件的持续集成变成可能。 官网 应用场景 场景一 研发人员上传开发好的代码到github代码仓库需要将代码下载nginx服务器部署手动下载再…

递归实现 组合问题+排列问题(DFS)

目录 递归实现排列型枚举 递归实现排列类型枚举 II 递归实现组合型枚举 递归实现组合型枚举 II 递归实现指数型枚举 递归实现指数型枚举 II 递归不是循环&#xff0c;递归利用了系统栈&#xff0c;只要是函数都会被系统管理。当执行到函数地址入口时就会为函数在系统栈上分…

mac 删除自带的ABC输入法保留一个搜狗输入法,搜狗配置一下可以减少很多的敲击键盘和鼠标点击次数

0. 背景 对于开发者来说&#xff0c;经常被中英文切换输入法所困扰&#xff0c;我这边有一个方法&#xff0c;删除mac默认的ABC输入法 仅仅保留搜狗一个输入法&#xff0c;配置一下搜狗输入&#xff1a;哪些指定为英文输入&#xff0c;哪些指定为中文输入&#xff08;符号也可…

wps图表怎么改横纵坐标,MLP 多层感知器和CNN卷积神经网络区别

目录 wps表格横纵坐标轴怎么设置&#xff1f; MLP (Multilayer Perceptron) 多层感知器 CNN (Convolutional Neural Network) 卷积神经网络 多层感知器MLP&#xff0c;全连接网络&#xff0c;DNN三者的关系 wps表格横纵坐标轴怎么设置&#xff1f; 1、打开表格点击图的右侧…

arm 函数栈回溯

大概意思就是arm每个函数开始都会将PC、LR、SP以及FP四个寄存器入栈。 下面我们看一下这四个寄存器里面保存的是什么内存 arm-linux-gnueabi-gcc unwind.c -mapcs -w -g -o unwind&#xff08;需要加上-mapcs才会严格按照上面说的入栈&#xff09; #include <stdio.h> …

雪花算法,在分布式环境下实现高效的ID生成

其实雪花算法比较简单&#xff0c;可能称不上什么算法就是一种构造UID的方法。 点1&#xff1a;UID是一个long类型的41位时间戳&#xff0c;10位存储机器码&#xff0c;12位存储序列号。 点2&#xff1a;时间戳的单位是毫秒&#xff0c;可以同时链接1024台机器&#xff0c;每台…

Android 面试题 线程间通信 六

&#x1f525; 主线程向子线程发送消息 Threadhandler&#x1f525; 子线程中定义Handler&#xff0c;Handler定义在哪个线程中&#xff0c;就跟那个线程绑定&#xff0c;在线程中绑定Handler需要调用Looper.prepare(); 方法&#xff0c;主线程中不调用是因为主线程默认帮你调用…

怎么在线修改图片?分享一个图片修改工具

无论是在个人或商业领域&#xff0c;我们都需要使用高质量的图片来传达信息或提高品牌形象。大尺寸的图片也会占据大量的存储空间和带宽&#xff0c;影响网站的加载速度和用户体验。因此&#xff0c;我们需要一种高效的工具来解决这个问题。今天向大家介绍一款非常实用的图片处…

Javascript -- 数组prototype方法探究

一、数组prototype方法探究 1、不改变原数组 1. concat() 这个是数组拼接方法&#xff0c;可以将两个数组或多个数组拼接并返回一个包含两个数组或多个数组内容的新数组&#xff0c;不会改变原数组 方法里面理论上可以写入n个参数&#xff0c; const arr [1,2]; var str …

Abaqus 导出单元刚度矩阵和全局刚度矩阵

Abaqus 导出单元刚度矩阵和全局刚度矩阵 首次创建&#xff1a;2023.7.29 最后更新&#xff1a;2023.7.29 如有什么改进的地方&#xff0c;欢迎大家讨论&#xff01; 详细情况请查阅&#xff1a;Abaqus Analysis User’s Guide 一、Abaqus 导出单元刚度矩阵 1.生成单元刚度矩阵…

Linux CentOS快速安装VNC并开启服务

以下是在 CentOS 上安装并开启 VNC 服务的步骤&#xff1a; 安装 VNC 服务器软件包。运行以下命令&#xff1a; sudo yum install tigervnc-server 输出 $ sudo yum install tigervnc-server Loaded plugins: fastestmirror, langpacks Repository epel is missing name i…

教雅川学缠论04-笔

笔由3部分组成&#xff1a; 顶分型K线底分型&#xff0c;或者 底分型K线顶分型 注意&#xff1a;笔加一起至少7根K线&#xff0c;因为一个底分型至少3根&#xff0c;K先至少1个&#xff0c;顶分型至少3根 下图中红色线段就是一个标准的笔&#xff0c;它始于一个底分型&#xff…

[点微]同城原生微信小程序 小程序原生版 1.0.7(tom_xiaofenlei)

注意!!!这是点微后出的原生版小程序!!! 依赖点微同城分类主插件、点微同城小程序后端插件!!! 【以下为模块路径】 同城首页 pages/index/index 个人中心 pages/index/my 好店首页 pages/module/tcshop 商城首页 pages/module/tcmall 抢购首页 pages/module/tcqianggou…