CPAC+Øvelse+4

=Øvelse 4=

Lav dit første Kernemodul
code format="scheme" stud@ubuntu:~/cpac/Hello_Module$ make make -C /home/stud/source/linux-2.6.28-omap M=/home/stud/cpac/Hello_Module modules make[1]: Entering directory `/home/stud/source/linux-2.6.28-omap' CC [M] /home/stud/cpac/Hello_Module/hello_world.o  Building modules, stage 2. MODPOST 1 modules CC     /home/stud/cpac/Hello_Module/hello_world.mod.o  LD [M]  /home/stud/cpac/Hello_Module/hello_world.ko make[1]: Leaving directory `/home/stud/source/linux-2.6.28-omap' stud@ubuntu:~/cpac/Hello_Module$ ls hello_world   hello_world.ko     hello_world.mod.o  Makefile       Module.symvers hello_world.c hello_world.mod.c  hello_world.o      modules.order code

code format="scheme" stud@ubuntu:~/cpac/Hello_Module$ cp2tgt hello_world.ko root@10.9.8.2's password: hello_world.ko                                                                                    100%   19KB  19.2KB/s   00:00 stud@ubuntu:~/cpac/Hello_Module$ conn2tgt root@10.9.8.2's password: root@DevKit8000:~# ls FirstPthread   VectorThread    abc             asdf            bac             hello_world.ko  minicom.log root@DevKit8000:~# insmod hello_world.ko root@DevKit8000:~# lsmod |grep hello hello_world 1344 0 - Live 0xbf007000 root@DevKit8000:~# rmmod hello_world.ko root@DevKit8000:~# dmesg |tail VFS: Mounted root (ext3 filesystem). Freeing init memory: 152K udevd version 124 started cpld driver initializing creating a new device CPLD new adapter: -946422728 Registering cpld driver g_ether gadget: high speed config #1: CDC Ethernet (ECM) Hello, world Goodbye, cruel world code

Få Lysdioden til at lyse
code format="c"
 * 1) include 
 * 2) include 
 * 3) include 

MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("Soeren og eskild"); MODULE_DESCRIPTION("");

static int __init cLED_init(void) {	// Tildele addresser til pointere (mappet til en anden end den fysiske) // Pointere til IO6 Registre volatile unsigned long *REG_GPIO6_OE; volatile unsigned long *REG_GPIO6_DIN; volatile unsigned long *REG_GPIO6_DOUT;

REG_GPIO6_OE = ioremap( 0x49058034, 1); REG_GPIO6_DIN = ioremap(0x49058038, 1); REG_GPIO6_DOUT = ioremap(0x4905803c, 1); //unsigned long LED_status; //unsigned long LED_OE;

// Bede kernen om lov til at benytte memory området if (request_mem_region (0x49058034, 12, "hello") == NULL) { printk ("Allocation for I/O memory range is failed\n"); return -1; }	// Læse data out for gpio bank 6 (32-bit) //LED_OE = ioread32(REG_GPIO6_OE); //printk("LED-OE:%08lX\n", LED_OE);

// Skrive 32-bit iowrite32(ioread32(REG_GPIO6_OE) & ~(1UL<<4), REG_GPIO6_OE); //LED_OE = ioread32(REG_GPIO6_OE); //printk("LED-OE2:%08lX\n", LED_OE);

// Læse data out for gpio bank 6 (32-bit) //LED_status = ioread32(REG_GPIO6_DOUT); //printk("LED-status:%lX\n", LED_status); // Skrive 32-bit iowrite32(ioread32(REG_GPIO6_DOUT) & ~(1UL<<4), REG_GPIO6_DOUT);

//LED_status = ioread32(REG_GPIO6_DOUT); //printk("LED-status2:%08lX\n", LED_status); return 0; }

static void __exit cLED_exit(void) {	volatile unsigned long *REG_GPIO6_OE; volatile unsigned long *REG_GPIO6_DIN; volatile unsigned long *REG_GPIO6_DOUT;

REG_GPIO6_OE = ioremap( 0x49058034, 1); REG_GPIO6_DIN = ioremap(0x49058038, 1); REG_GPIO6_DOUT = ioremap(0x4905803c, 1); //unsigned long LED_status; // Læse data out for gpio bank 6 (32-bit) //LED_status = ioread32(REG_GPIO6_DOUT);

// Sluk LED (32-bit) iowrite32(ioread32(REG_GPIO6_DOUT)| (1UL<<4), REG_GPIO6_DOUT);

// Frigiv memory release_mem_region (0x49058034, 12); } module_init(cLED_init); module_exit(cLED_exit);

code

Lav en C-Applikation
code format="c"
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include 

int main(int argc, char *argv[]) { int fd;

if ((fd = open("/sys/class/leds/led3/brightness", O_WRONLY)) == -1) { printf("Cannot open target file\n"); return 1; // exit upon error } if (write(fd, argv[1], 1) == -1) { printf("Cannot write to target file\n"); return 1; // exit upon error } if (close(fd) == -1) { printf("Cannot close target file\n"); return 1; // exit upon error } if (!atoi(argv[1])) printf("LED is now ON!\n"); else printf("LED is now OFF!\n");

return 0; } code