CubieBoard博客-HomeCubieTech公司网站

cubie.cc CubieBoard中文论坛

 找回密码
 立即注册
搜索
热搜: unable
查看: 4202|回复: 3

[嵌入式学习] USB设备上电断电应用程序

[复制链接]

74

主题

5

好友

2万

积分

超级版主

Rank: 8Rank: 8

贡献
3722
金钱
8557
威望
3730
买家信用
卖家信用
积分
20422
发表于 2015-6-9 11:25:19 |显示全部楼层
本帖最后由 @allen 于 2015-6-9 11:26 编辑


转载自  http://www.cubieforums.com/index.php/topic,3753.0.html



有时需要在不拨出设备的情况下,让USB设备不工作或是工作,可以通过映射寄存器后,写高或写低VBUS GPIO,即 the data register of Port H 。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <signal.h>
  7. #include <fcntl.h>
  8. #include <ctype.h>
  9. #include <termios.h>
  10. #include <sys/types.h>
  11. #include <sys/mman.h>

  12. #define MAP_SIZE 4096UL
  13. #define MAP_MASK (MAP_SIZE - 1)

  14. int verbose = 0;
  15. int main(int argc, char **argv)
  16. {
  17.   int fd;
  18.   void *map_base, *virt_addr;
  19.   unsigned long readval, writeval;
  20.   off_t porth;

  21.   if ( argc == 2 && ( argv[1][1] == 'h' || argv[1][1] == '-' ) )
  22.   {
  23.     printf("Usage: cbusb [+|-][1|2]...\n");
  24.     printf("Sets the Cubieboard (A10 and A20) USB power directly for port 1 and 2.\n");
  25.     printf("USB1 is the upper, USB2 is the lower - closer to the PCB - port.\n\n");
  26.     printf("Be warned: it directly writes the data register of Port H, bypassing the kernel.\n\n");
  27.     printf("Without parameters just prints the port power status.\n");
  28.     printf("+ turns the power on, - turns it off for the specified port.\n");
  29.     printf("Port H only gets written if the settings differ from the current ones.\n");
  30.     printf("Note, changing power for one port might momentarily disrupt the other too.\n\n");
  31.     printf("Examples:\n        cbusb +1\n        cbusb +2 -1\n\n");
  32.     fflush(stdout);
  33.   }

  34.   setuid(0); // try

  35.   if ( (fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1 )
  36.   {
  37.     printf("ERR: Can't open /dev/mem\n");
  38.     exit(1);
  39.   }
  40.   if ( verbose )
  41.   {
  42.     printf("/dev/mem opened.\n");
  43.     fflush(stdout);
  44.   }

  45.   porth = 0x1c2090c; // Port H data reg.

  46.   map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, porth & ~MAP_MASK);
  47.   if ( map_base == (void *) -1 )
  48.   {
  49.     printf("ERR: Can't map the requested memory page\n");
  50.     exit(2);
  51.   }
  52.   if ( verbose )
  53.   {
  54.     printf("Memory mapped at address %p.\n", map_base);
  55.     fflush(stdout);
  56.   }

  57.   virt_addr = map_base + (porth & MAP_MASK);
  58.   readval = *((unsigned long *) virt_addr);
  59.   printf("USB1: %i\n", (readval & 8) >> 3);
  60.   printf("USB2: %i\n", (readval & 64) >> 6);
  61.   if ( verbose ) printf("OLD: 0x%08x\n", readval);
  62.   fflush(stdout);

  63.   writeval = readval;
  64.   int i;
  65.   for (i=1; i<=2; ++i)
  66.   {
  67.     if ( argc > i )
  68.     {
  69.       unsigned long usbmask = 0;
  70.       if ( argv[i][1]=='1' ) usbmask = 8; else if ( argv[i][1]=='2' ) usbmask = 64;
  71.       if ( argv[i][0]=='+' ) writeval |= usbmask; else writeval &= ~usbmask;
  72.     }
  73.   }
  74.   if ( verbose ) { printf("NEW: 0x%08x\n", writeval); fflush(stdout); }

  75.   if ( writeval != readval )
  76.   {
  77.     *((unsigned long *) virt_addr) = writeval;
  78.     printf("NEWUSB1: %i\n", (writeval & 8) >> 3);
  79.     printf("NEWUSB2: %i\n", (writeval & 64) >> 6);
  80.     fflush(stdout);
  81.   }

  82.   if ( munmap(map_base, MAP_SIZE) == -1 )
  83.   {
  84.     printf("ERR: munmap failed\n");
  85.     exit(3);
  86.   }

  87.   close(fd);

  88.   return 0;
  89. }
复制代码
保存为cbusb.c
arm-linux-gnueabihf-gcc-o cbusb cbusb.c
mv cbusb /usr/local/sbin/

查看帮助
cbusb -h

查看状态
cbusb

关闭USB1  
cbusb -1

使能USB1  
cbusb +1






回复

使用道具 举报

20

主题

0

好友

4118

积分

论坛元老

Rank: 8Rank: 8

贡献
754
金钱
1775
威望
754
买家信用
卖家信用
积分
4118
发表于 2015-6-10 13:38:36 |显示全部楼层
很好,十分感谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|邮件群发|cubie.cc---深刻的嵌入式技术和应用讨论中文社区 ( 粤ICP备13051116号-1  

GMT+8, 2020-1-19 02:56 , Processed in 0.017165 second(s), 5 queries , Apc On.

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc. | Style by Coxxs

回顶部