Linux內核版本號:linux 2.6.39
交叉編譯工具:arm-linux-gcc 4.5.1
Linux內核下載:www.kernel.org
開發板:友善之臂Tiny6410
LCD:友善之臂S70
一、移植LED驅動
打開arch/arm/mach-s3c64xx/mach-mini6410.c添加下列代碼:
1 static struct gpio_led tiny6410_gpio_led[] = {
2 [0] = {
3 .name = 'led1', //設備名
4 .gpio = S3C64XX_GPK(4), //GPK4
5 .active_low = 1, //低電平點亮
6 .default_state = LEDS_GPIO_DEFSTATE_ON, //系統啟動后默認為打開
7 },
8 [1] = {
9 .name = 'led2',
10 .gpio = S3C64XX_GPK(5),
11 .active_low = 1,
12 .default_state = LEDS_GPIO_DEFSTATE_OFF, //系統啟動后默認關閉
13 },
14 [2] = {
15 .name = 'led3',
16 .gpio = S3C64XX_GPK(6),
17 .active_low = 1,
18 .default_state = LEDS_GPIO_DEFSTATE_ON,
19 },
20 [3] = {
21 .name = 'led4',
22 .gpio = S3C64XX_GPK(7),
23 .active_low = 1,
24 .default_state = LEDS_GPIO_DEFSTATE_OFF,
25 },
26 };
27
28 static struct gpio_led_platform_data tiny6410_leds_data = {
29 .num_leds = ARRAY_SIZE(tiny6410_gpio_led),
30 .leds = &tiny6410_gpio_led,
31 };
32
33 static struct platform_device tiny6410_device_leds = {
34 .name = 'leds-gpio',
35 .id = -1,
36 .dev = {
37 .platform_data = &tiny6410_leds_data,
38 },
39 };
在mini6410_devices中添加tiny6410_device_leds,系統啟動時將自動注冊LED平臺設備:
1 static struct platform_device *mini6410_devices[] __initdata = {
2 ...
3 &tiny6410_device_leds,
4 };
執行make menuconfig修改內核配置,添加對LED設備的支持:
Device Drivers --->
│ │ [*] LED Support --->
│ │ [*] LED Class Support
│ │ *** LED drivers ***
│ │ <*> LED Support for GPIO connected LEDs
│ │ [*] Platform device bindings for GPIO LEDs
編譯并燒寫內核,啟動開發板可以看到第一、第三個LED被點亮。
編寫應用程序控制LED:
系統LED設備名為每個LED設備創建了一個節點文件夾,位于/sys/devices/platform/leds-gpio/leds/目錄下,對設備文件夾里面的brightness 文件寫0或寫非0即可對LED進行操作。
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 8 9 int main(int argc,char** argv) 10 { 11 int fd = 0; 12 char path[64] = '/sys/devices/platform/leds-gpio/leds/'; 13 14 if(argc != 3) 15 { 16 printf('format error!n'); 17 return -1; 18 } 19 20 strcat(path,argv[1]); 21 strcat(path,'/brightness'); 22 23 printf('%sn',path); 24 fd = open(path,O_RDWR); 25 if(fd == -1) 26 { 27 printf('open file failure!n'); 28 return -1; 29 } 30 if(atoi(argv[2])) 31 write(fd,'1',1); 32 else 33 write(fd,'0',1); 34 35 close(fd); 36 return 0; 37 } 二、按鍵驅動移植 在arch/arm/mach-s3c64xx/mach-mini6410.c添加下列代碼: 1 static struct gpio_keys_button tiny6410_gpio_keys[] = { 2 [0] = { 3 .code = KEY_F1, //鍵值 4 .type = EV_KEY, //按鍵輸入類型 5 .gpio = S3C64XX_GPN(0), 6 .active_low = 1, //低電平表示按下 7 .wakeup = 0, 8 .debounce_interval = 5, /* ms */ //延時消抖 9 .desc = 'Button 1', 10 }, 11 [1] = { 12 .code = KEY_F2, 13 .type = EV_KEY, 14 .gpio = S3C64XX_GPN(1), 15 .active_low = 1, 16 .wakeup = 0, 17 .debounce_interval = 5, /* ms */ 18 .desc = 'Button 2', 19 }, 20 [2] = { 21 .code = KEY_F3, 22 .type = EV_KEY, 23 .gpio = S3C64XX_GPN(2), 24 .active_low = 1, 25 .wakeup = 0, 26 .debounce_interval = 5, /* ms */ 27 .desc = 'Button 3', 28 }, 29 [3] = { 30 .code = KEY_F4, 31 .type = EV_KEY, 32 .gpio = S3C64XX_GPN(3), 33 .active_low = 1, 34 .wakeup = 0, 35 .debounce_interval = 5, /* ms */ 36 .desc = 'Button 4', 37 }, 38 39 }; 40 41 static struct gpio_keys_platform_data tiny6410_key_data = { 42 .buttons = &tiny6410_gpio_keys, 43 .nbuttons = ARRAY_SIZE(tiny6410_gpio_keys), 44 }; 45 46 static struct platform_device tiny6410_device_keys = { 47 .name = 'gpio-keys', 48 .id = -1, 49 .dev = { 50 .platform_data = &tiny6410_key_data, 51 }, 52 }; 在mini6410_devices中添加tiny6410_device_keys: 1 static struct platform_device *mini6410_devices[] __initdata = { 2 .... 3 &tiny6410_device_leds, 4 &tiny6410_device_keys, 5 }; 執行make menuconfig修改內核配置,添加對LED設備的支持: Device Drivers ---> │ │ Input device support ---> │ │ [*] Keyboards ---> │ │ <*> GPIO Buttons 同時在Input device support里面添加event interface的支持,在/dev/下面就能生成一個event設備文件: Device Drivers ---> │ │ Input device support ---> │ │ <*> Event interface 編譯并燒寫內核,啟動開發板可以在/dev/目錄下生成了event0設備文件,對按鍵驅動進行簡單的測試: 執行hexdump /dev/event0 每次按下按鍵可以看到如下所示按鍵信息,表明按鍵是工作正常的。 1 /dev # hexdump event0 2 0000000 034d 0000 0e3b 000c 0001 003b 0001 0000 3 0000010 034d 0000 0e4c 000c 0000 0000 0000 0000 4 0000020 034d 0000 cd5f 000e 0001 003b 0000 0000 5 0000030 034d 0000 cd6b 000e 0000 0000 0000 0000 編寫應用程序測試按鍵驅動: 按鍵驅動為輸入子系統,應用程序中需要對event進行循環檢測看系統有沒有上報輸入事件,按鍵的輸入事件類型為EV_KEY,鍵值分別問KEY_F1、KEY_F2、KEY_F3、KEY_F4,數值為1表示按鍵按下為0表示按鍵釋放。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 8 int main(void) 9 { 10 int fd = 0; 11 struct input_event event_key; 12 int count = 0; 13 14 fd = open('/dev/event0',O_RDONLY); 15 if(fd == -1) 16 { 17 printf('open file failedn'); 18 return -1; 19 } 20 21 while(1) 22 { 23 count = read(fd,&event_key,sizeof(struct input_event)); 24 if(count < 0) 25 { 26 printf('read failedn'); 27 break; 28 } 29 if(event_key.type == EV_KEY)
上一篇:應用程序調用tslib出現segmentation fault
下一篇:Linux-2.6.39在Tiny6410上的移植
推薦閱讀最新更新時間:2025-06-07 23:44


設計資源 培訓 開發板 精華推薦
- 芯原超低能耗NPU可為移動端大語言模型推理提供超40 TOPS算力
- 芯原AI-ISP芯片定制方案助力客戶智能手機量產出貨
- AI加持,安森美一站式服務推動助聽器市場持續進步
- 意法半導體推出用于匹配遠距離無線微控制器STM32WL33的集成的匹配濾波芯片
- 如何為您的應用選擇光傳感器
- Samtec新型農業漫談系列二 | 垂直農業案列分享
- Samtec應用科普 | C-V2X技術在汽車領域的應用
- 尼得科運動&能源事業本部在印度卡納塔克邦胡布利舉行新工廠竣工儀式
- 貿澤開售Qorvo適用于5G和mMIMO應用的新型QPA9822線性5G高增益/高驅動放大器
- 邊緣計算網關工業物聯網應用:空壓機遠程運維監控管理