PC操作系統(tǒng):ubuntu 11.10
使用的開(kāi)發(fā)板:am335x_evm
開(kāi)發(fā)板使用的操作系統(tǒng):linux 3.2
用途
/usr/ccs/bin/nm 包含 nm 命令。
示例:
程序源碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | #include <linux init.h=""> #include <linux module.h=""> #include <linux leds.h=""> #include <linux io.h=""> #include <linux semaphore.h=""> #include <linux kernel.h=""> #include <linux cdev.h=""> #include <linux types.h=""> #include <linux fs.h=""> //register_chrdev_region #include <mach gpio.h=""> #include <plat mux.h=""> #include <linux gpio.h=""> MODULE_LICENSE( "Dual BSD/GPL" ); MODULE_AUTHOR( "SM" ); MODULE_VERSION( "0.0.1" ); //MODULE_DEVICE("global memory"); MODULE_DESCRIPTION( "led ..." ); MODULE_ALIAS( "LED" ); /*******************************************/ #define NAME "led" #define GPIO_TO_PIN(bank, gpio) (32 * (bank) + (gpio)) #define LED_GPIO_1 1 #define LED_PIN_1 24 static int major =283; //定義主設(shè)備號(hào) static struct class *led_class; /*******************************************/ void led_on( void ) { gpio_set_value(GPIO_TO_PIN(LED_GPIO_1,LED_PIN_1), 1); } EXPORT_SYMBOL(led_on); void led_off( void ) { gpio_set_value(GPIO_TO_PIN(LED_GPIO_1,LED_PIN_1), 0); } EXPORT_SYMBOL(led_off); void led_init( void ) { int result; /* Allocating GPIOs and setting direction */ gpio_free(GPIO_TO_PIN(LED_GPIO_1,LED_PIN_1)); result = gpio_request(GPIO_TO_PIN(LED_GPIO_1,LED_PIN_1), "Leds" ); //usr1 if (result != 0) printk( "gpio_request(%d_%d) failed!\n" ,LED_GPIO_1,LED_PIN_1); result = gpio_direction_output(GPIO_TO_PIN(LED_GPIO_1,LED_PIN_1), 1); if (result != 0) printk( "gpio_direction(%d_%d) failed!\n" ,LED_GPIO_1,LED_PIN_1); } struct light_dev { struct cdev cdev; unsigned char value; }; struct light_dev *light_devp; // 打開(kāi)和關(guān)閉函數(shù) int light_open( struct inode *inode, struct file *filp) { struct light_dev *dev; // 獲得設(shè)備結(jié)構(gòu)體指針 dev = container_of(inode->i_cdev, struct light_dev,cdev); // 讓設(shè)備結(jié)構(gòu)體作為設(shè)備的私有信息 filp->private_data = dev; return 0; } int light_release( struct inode *inode, struct file *filp) { return 0; } // ioctl long light_ioctl( struct file *filp,unsigned int cmd, unsigned long arg) { struct light_dev *dev = filp->private_data; switch (cmd) { case 0: dev->value = 0; led_off(); break ; case 1: dev->value = 1; led_on(); break ; default : return -ENOTTY; // break; } return 0; } //set up the cdev structure for a device static void led_setup_cdev( struct cdev* dev, int minor, struct file_operations *fops) { int err, devno = MKDEV(major,minor); cdev_init(dev,fops); dev->owner = THIS_MODULE; dev->ops = fops; err = cdev_add(dev,devno,1); if (err) printk(KERN_NOTICE "Error %d adding led%d" ,err,minor); } struct file_operations light_fops = { .owner = THIS_MODULE, .unlocked_ioctl = light_ioctl, .open = light_open, .release = light_release, }; static struct cdev cdev_led; // 模塊加載函數(shù) int light_init( void ) { int ret; dev_t dev = MKDEV(major,0); led_init(); printk(KERN_ALERT "led modules is install\n" ); // ret=register_chrdev(major,NAME,&light_fops); if (major) ret = register_chrdev_region(dev,1,NAME); else { ret = alloc_chrdev_region(&dev,0,1,NAME); major = MAJOR(dev); } if (ret<0) { printk( "unable to register myled driver!\n" ); return ret; } printk(KERN_DEBUG "led device number : %x\n" ,dev); led_setup_cdev(&cdev_led,0,&light_fops); led_class = class_create(THIS_MODULE, "led_class" ); if (IS_ERR(led_class)) { printk(KERN_INFO "create led class error\n" ); return -1; } device_create(led_class,NULL,dev,NULL, "led" "%d" ,MINOR(dev)); return 0; } // 模塊卸載函數(shù) void light_cleanup( void ) { // unregister_chrdev(major,NAME); device_destroy(led_class,MKDEV(major,0)); class_destroy(led_class); cdev_del(&cdev_led); unregister_chrdev_region(MKDEV(major,0),1); printk( "Goodbye,cruel world!\n" ); } module_init(light_init); module_exit(light_cleanup); </linux></plat></mach></linux></linux></linux></linux></linux></linux></linux></linux></linux> |
結(jié)果顯示:
聯(lián)系客服