当前位置: 首页 > news >正文

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;
}

相关文章:

day3 驱动开发 c语言编程

通过ioctl&#xff08;内核应用层&#xff09; 控制led灯三盏&#xff0c;风扇&#xff0c;蜂鸣器&#xff0c;小马达 头文件head.h #ifndef __LED_H__ #define __LED_H__typedef struct {volatile unsigned int TZCR; // 0x000volatile unsigned int res1[2]; // 0x…...

【字节跳动青训营】后端笔记整理-3 | Go语言工程实践之测试

**本文由博主本人整理自第六届字节跳动青训营&#xff08;后端组&#xff09;&#xff0c;首发于稀土掘金&#xff1a;&#x1f517;Go语言工程实践之测试 | 青训营 目录 一、概述 1、回归测试 2、集成测试 3、单元测试 二、单元测试 1、流程 2、规则 3、单元测试的例…...

【Android】Recyclerview的缓存复用

介绍 RecyclerView是Android开发中常用的一个高度可定制的列表视图组件。它是在ListView和GridView的基础上进行了改进和增强&#xff0c;旨在提供更好的性能和更灵活的布局管理。 RecyclerView的主要特点如下&#xff1a; 灵活的布局管理器&#xff08;LayoutManager&#…...

机器学习:混合高斯聚类GMM(求聚类标签)+PCA降维(3维降2维)习题

使用混合高斯模型 GMM&#xff0c;计算如下数据点的聚类过程&#xff1a; Datanp.array([1,2,6,7]) 均值初值为: μ1,μ21,5 权重初值为: w1,w20.5,0.5 方差: std1,std21,1 K2 10 次迭代后数据的聚类标签是多少&#xff1f; 采用python代码实现&#xff1a; from scipy import…...

libuv库学习笔记-processes

Processes libuv提供了相当多的子进程管理函数&#xff0c;并且是跨平台的&#xff0c;还允许使用stream&#xff0c;或者说pipe完成进程间通信。 在UNIX中有一个共识&#xff0c;就是进程只做一件事&#xff0c;并把它做好。因此&#xff0c;进程通常通过创建子进程来完成不…...

c++ 给无名形参提供默认值

如上图&#xff0c;若函数的形参不在函数体里使用&#xff0c;可以不提供形参名&#xff0c;而且可以给此形参提供默认值。也能编译通过。 在看vs2019上的源码时&#xff0c;也出现了这种写法。应用SFINAE&#xff08;substitute false is not an error&#xff09;原则&#x…...

NO1.使用命令行创建Maven工程

①在工作空间目录下打开命令窗口 ②使用命令行生成Maven工程 mvn archetype:generate 运行 MVN 原型&#xff1a;生成命令,下面根据提示操作 选择一个数字或应用过滤器&#xff08;格式&#xff1a;[groupId&#xff1a;]artifactId&#xff0c;区分大小写包含&#xff09;&a…...

深度学习入门(一):神经网络基础

一、深度学习概念 1、定义 通过训练多层网络结构对位置数据进行分类或回归&#xff0c;深度学习解决特征工程问题。 2、深度学习应用 图像处理语言识别自然语言处理 在移动端不太好&#xff0c;计算量太大了&#xff0c;速度可能会慢 eg.医学应用、自动上色 3、例子 使用…...

网络知识整理

网络知识整理 网络拓扑网关默认网关 数据传输拓扑结构层面协议层面 网络拓扑 网关 连接两个不同的网络的设备都可以叫网关设备&#xff0c;网关的作用就是实现两个网络之间进行通讯与控制。 网关设备可以是交换机(三层及以上才能跨网络) 、路由器、启用了路由协议的服务器、代…...

如何有效地使用ChatGPT写小说讲故事?

​构思故事情节&#xff0c;虽有趣但耗时&#xff0c;容易陷入写作瓶颈。ChatGPT可提供灵感&#xff0c;帮你解决写作难题。要写出引人入胜的故事&#xff0c;关键在于抓住八个要素——主题、人物、视角、背景、情节、语气、冲突和解决办法。 直接给出故事模板&#xff0c;你可…...

原生求生记:揭秘UniApp的原生能力限制

文章目录 1. 样式适配问题2. 性能问题3. 原生能力限制4. 插件兼容性问题5. 第三方组件库兼容性问题6. 全局变量污染7. 调试和定位问题8. 版本兼容性问题9. 前端生态限制10. 文档和支持附录&#xff1a;「简历必备」前后端实战项目&#xff08;推荐&#xff1a;⭐️⭐️⭐️⭐️…...

网络编程 IO多路复用 [epoll版] (TCP网络聊天室)

//head.h 头文件 //TcpGrpSer.c 服务器端 //TcpGrpUsr.c 客户端 通过IO多路复用实现服务器在单进程单线程下可以与多个客户端交互 API epoll函数 #include<sys/epoll.h> int epoll_create(int size); 功能&#xff1a;创建一个epoll句柄//创建红黑树根…...

【go-zero】浅析 01

“github.com/google/uuid” uuid.New().String() go-zero 文档 https://www.w3cschool.cn/gozero/ go-zero 官网 https://go-zero.dev/ 快速开始&#xff1a; $ mkdir go-zero-demo $ cd go-zero-demo $ go mod init go-zero-demo $ goctl api new greet $ go mod tidy Done…...

音视频——视频流H264编码格式

1 H264介绍 我们了解了什么是宏快&#xff0c;宏快作为压缩视频的最小的一部分&#xff0c;需要被组织&#xff0c;然后在网络之间做相互传输。 H264更深层次 —》宏块 太浅了 ​ 如果单纯的用宏快来发送数据是杂乱无章的&#xff0c;就好像在没有集装箱 出现之前&#xff0c;…...

【使用深度学习的城市声音分类】使用从提取音频特征(频谱图)中提取的深度学习进行声音分类研究(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

机器学习完整路径

一个机器学习项目从开始到结束大致分为 5 步&#xff0c;分别是定义问题、收集数据和预处理、选择算法和确定模型、训练拟合模型、评估并优化模型性能。是一个循环迭代的过程&#xff0c;优秀的模型都是一次次迭代的产物。 定义问题 要剖析业务场景&#xff0c;设定清晰的目标…...

CK-00靶机详解

CK-00靶机详解 靶场下载地址&#xff1a;https://download.vulnhub.com/ck/CK-00.zip 这个靶场扫描到ip打开后发现主页面css是有问题的&#xff0c;一般这种情况就是没有配置域名解析。 我们网站主页右击查看源代码&#xff0c;发现一个域名。 把域名添加到我们hosts文件中。…...

17-C++ 数据结构 - 栈

&#x1f4d6; 1.1 什么是栈 栈是一种线性数据结构&#xff0c;具有后进先出&#xff08;Last-In-First-Out&#xff0c;LIFO&#xff09;的特点。可以类比为装满盘子的餐桌&#xff0c;每次放盘子都放在最上面&#xff0c;取盘子时也从最上面取&#xff0c;因此最后放进去的盘…...

Redis如何实现排行榜?

今天给大家简单聊聊 Redis Sorted Set 数据类型底层的实现原理和游戏排行榜实战。特别简单&#xff0c;一点也不深入&#xff0c;也就 7 张图&#xff0c;粉丝可放心食用&#xff0c;哈哈哈哈哈~~~~。 1. 是什么 Sorted Sets 与 Sets 类似&#xff0c;是一种集合类型&#xff…...

Pycharm debug程序,跳转至指定循环条件/循环次数

在断点出右键&#xff0c;然后设置条件 示例 for i in range(1,100):a i 1b i 2print(a, b, i) 注意&#xff1a; 1、你应该debug断点在循环后的位置而不是循环上的位置&#xff0c;然后你就可以设置你的条件进入到指定的循环上了 2、设置条件&#xff0c;要使用等于符号…...

react实现markdown

参考&#xff1a;https://blog.csdn.net/Jack_lzx/article/details/118495763 参考&#xff1a;https://blog.csdn.net/m0_48474585/article/details/119742984 0. 示例 用react实现markdown编辑器 1.基本布局及样式 <><div classNametf_editor_header>头部&…...

HTTP请求走私漏洞简单分析

文章目录 HTTP请求走私漏洞的产生HTTP请求走私漏洞的分类HTTP请求走私攻击的危害确认HTTP请求走私漏洞通过时间延迟技术确认CL漏洞通过时间延迟技术寻找TE.CL漏洞 使用差异响应内容确认漏洞通过差异响应确认CL.TE漏洞通过差异响应确认TE.CL漏洞 请求走私漏洞的利用通过请求漏洞…...

BI-SQL丨两表差异比较

BOSS&#xff1a;哎&#xff0c;白茶&#xff0c;我们最近新上了一个系统&#xff0c;后续有一些数据要进行源切换&#xff0c;这个能整么&#xff1f; 白茶&#xff1a;没问题&#xff0c;可以整&#xff01; BOSS&#xff1a;哦&#xff0c;对了&#xff0c;差点忘记告诉你了…...

ZooKeeper 选举的过半机制防止脑裂

结论&#xff1a; Zookeeper采用过半选举机制&#xff0c;防止了脑裂。 原因&#xff1a; 如果有5台节点&#xff0c;leader联系不上了&#xff0c;其他4个节点由于超过半数&#xff0c;所以又选出了一个leader&#xff0c;当失联的leader恢复网络时&#xff0c;发现集群中已…...

【图论】树上差分(边差分)

一.简介 其实点差分和边差分区别不大。 点差分中&#xff0c;d数组存储的是树上的节点 边差分中&#xff0c;d数组存储的是当前节点到父节点的那条边的差分值。 指定注意的是&#xff1a;边差分中因为根连的父节点是虚点&#xff0c;所以遍历结果时应当忽略&#xff01; 二…...

RT1052的定时器

文章目录 1 通用定时器1.1 定时器框图1.2 实现周期性中断 2 相关寄存器3 定时器配置3.1 时钟使能3.2 初始化GPT1定时器3.2.1 base3.2.2 initConfig3.2.2.1 clockSorce3.2.2.2 divider3.2.2.3 enablexxxxx 3.3 设置 GPT1 比较值3.3.1 base3.3.2 channel3.3.3 value 3.4 设置 GPT…...

opencv python 训练自己的分类器

源码下载 一、分类器制作 1.样本准备 收集好你所需的正样本&#xff0c;和负样本&#xff0c;分别保存在不同文件夹 在pycharm新建项目&#xff0c;项目结构如下&#xff1a;has_mask文件夹放置正样本&#xff0c;no_mask文件夹放置负样本 安装opencv&#xff0c;把opencv包…...

详解Mybatis之分页插件【PageHelper】

编译软件&#xff1a;IntelliJ IDEA 2019.2.4 x64 操作系统&#xff1a;win10 x64 位 家庭版 Maven版本&#xff1a;apache-maven-3.6.3 Mybatis版本&#xff1a;3.5.6 文章目录 一. 什么是分页&#xff1f;二. 为什么使用分页&#xff1f;三. 如何设计一个Page类&#xff08;分…...

【基于矢量射线的衍射积分 (VRBDI)】基于矢量射线的衍射积分 (VRBDI) 和仿真工具(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…...

基于jackson对bean的序列号和反序列化

通过观察控制台输出的SQL发现页面传递过来的员工id的值和数据库中的id值不一致&#xff0c;这是怎么回事呢? 分页查询时服务端响应给页面的数据中id的值为19位数字&#xff0c;类型为long 页面中js处理long型数字只能精确到前16位&#xff0c;所以最终通过ajax请求提交给服务…...

非自己的网站如何做二次跳转/哪里有免费的网站推广服务

首先&#xff0c;要辨析进程与线程的概念&#xff1a; 进程是程序执行的过程&#xff0c;它持有资源和线程&#xff0c;相对于程序本身而言具有动态性。 线程是系统中最小的执行单元&#xff0c;同一个进程中可能有多个线程&#xff0c;它们共享该进程持有的资源。线程的通信…...

沈阳做网站的/西安网站关键词推广

HTTP/2概述 HTTP/2意在减轻为维护HTTP/1.1复杂的底层架构而带来的痛苦&#xff0c;以提高HTTP/1.1的性能。尽管HTTP/2仍然对HTTP/1.1向下兼容&#xff0c;但它已不再是一个基于文本的协议。当客户端通过HTTP/1.1请求建立一个连接时&#xff0c;所有请求将会被升级。从这一点上…...

上海高端网站制作公司/网站制作费用

在vmware中克隆Centos6.0以上版本都会出现网卡和设备文件不匹配的问题。编辑/etc/udev/rules.d/70-persistent-net.rules把name修改为对应的配置文件名称&#xff0c;在/etc/sysconfig/network-script/下编辑对应的配置文件把mac地址修改为address地址即可&#xff0c;重启后生…...

企业外贸营销型网站/营销页面

文章目录2、 输入控件&#xff08;一&#xff09;2.1 纯键盘2.1.1 QLineEdit2.1.1.1 描述2.1.1.2 控件创建2.1.1.3 输出模式2.1.1.4 提示字符串2.1.1.5 清空按钮2.1.1.6 添加操作行为2.1.1.7 自动补全2.1.1.8 输入限制2.1.1.8.1 语法2.1.1.8.2 掩码验证2.1.1.9 案例&#xff1a…...

怎么可以做网站/如何给公司做网络推广

最近两天在学习sqlite数据库&#xff0c;sqlite数据库是轻量级的&#xff1b;但是自我认为有一个不好的地方就是不能写存储过程&#xff1b;连接.net的字符串可以直接写路径就可以&#xff08;SQLiteConnection conn new SQLiteConnection("Data SourceC:\Users\jxx\Desk…...

网站公司简介模板/河南百度推广公司

一、了解机器连接数情况问题&#xff1a;1.2.3.4的sshd的监听端口是22&#xff0c;如何统计1.2.3.4的sshd服务各种连接状态(TIME_WAIT/ CLOSE_WAIT/ ESTABLISHED)的连接数。常见方法&#xff1a;netstat -n | grep 1.2.3.4:22 | awk /^tcp/ {S[$NF]} END {for(a in S) print a,…...