当前位置: 首页 > 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;要使用等于符号…...

Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误

HTTP 状态码 406 (Not Acceptable) 和 500 (Internal Server Error) 是两类完全不同的错误&#xff0c;它们的含义、原因和解决方法都有显著区别。以下是详细对比&#xff1a; 1. HTTP 406 (Not Acceptable) 含义&#xff1a; 客户端请求的内容类型与服务器支持的内容类型不匹…...

大数据零基础学习day1之环境准备和大数据初步理解

学习大数据会使用到多台Linux服务器。 一、环境准备 1、VMware 基于VMware构建Linux虚拟机 是大数据从业者或者IT从业者的必备技能之一也是成本低廉的方案 所以VMware虚拟机方案是必须要学习的。 &#xff08;1&#xff09;设置网关 打开VMware虚拟机&#xff0c;点击编辑…...

多种风格导航菜单 HTML 实现(附源码)

下面我将为您展示 6 种不同风格的导航菜单实现&#xff0c;每种都包含完整 HTML、CSS 和 JavaScript 代码。 1. 简约水平导航栏 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport&qu…...

高防服务器能够抵御哪些网络攻击呢?

高防服务器作为一种有着高度防御能力的服务器&#xff0c;可以帮助网站应对分布式拒绝服务攻击&#xff0c;有效识别和清理一些恶意的网络流量&#xff0c;为用户提供安全且稳定的网络环境&#xff0c;那么&#xff0c;高防服务器一般都可以抵御哪些网络攻击呢&#xff1f;下面…...

#Uniapp篇:chrome调试unapp适配

chrome调试设备----使用Android模拟机开发调试移动端页面 Chrome://inspect/#devices MuMu模拟器Edge浏览器&#xff1a;Android原生APP嵌入的H5页面元素定位 chrome://inspect/#devices uniapp单位适配 根路径下 postcss.config.js 需要装这些插件 “postcss”: “^8.5.…...

Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问&#xff08;基础概念问题&#xff09; 1. 请解释Spring框架的核心容器是什么&#xff1f;它在Spring中起到什么作用&#xff1f; Spring框架的核心容器是IoC容器&#…...

TSN交换机正在重构工业网络,PROFINET和EtherCAT会被取代吗?

在工业自动化持续演进的今天&#xff0c;通信网络的角色正变得愈发关键。 2025年6月6日&#xff0c;为期三天的华南国际工业博览会在深圳国际会展中心&#xff08;宝安&#xff09;圆满落幕。作为国内工业通信领域的技术型企业&#xff0c;光路科技&#xff08;Fiberroad&…...

Xela矩阵三轴触觉传感器的工作原理解析与应用场景

Xela矩阵三轴触觉传感器通过先进技术模拟人类触觉感知&#xff0c;帮助设备实现精确的力测量与位移监测。其核心功能基于磁性三维力测量与空间位移测量&#xff0c;能够捕捉多维触觉信息。该传感器的设计不仅提升了触觉感知的精度&#xff0c;还为机器人、医疗设备和制造业的智…...

LLaMA-Factory 微调 Qwen2-VL 进行人脸情感识别(二)

在上一篇文章中,我们详细介绍了如何使用LLaMA-Factory框架对Qwen2-VL大模型进行微调,以实现人脸情感识别的功能。本篇文章将聚焦于微调完成后,如何调用这个模型进行人脸情感识别的具体代码实现,包括详细的步骤和注释。 模型调用步骤 环境准备:确保安装了必要的Python库。…...

VisualXML全新升级 | 新增数据库编辑功能

VisualXML是一个功能强大的网络总线设计工具&#xff0c;专注于简化汽车电子系统中复杂的网络数据设计操作。它支持多种主流总线网络格式的数据编辑&#xff08;如DBC、LDF、ARXML、HEX等&#xff09;&#xff0c;并能够基于Excel表格的方式生成和转换多种数据库文件。由此&…...