|
本帖最后由 @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 。- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
- #include <signal.h>
- #include <fcntl.h>
- #include <ctype.h>
- #include <termios.h>
- #include <sys/types.h>
- #include <sys/mman.h>
- #define MAP_SIZE 4096UL
- #define MAP_MASK (MAP_SIZE - 1)
- int verbose = 0;
- int main(int argc, char **argv)
- {
- int fd;
- void *map_base, *virt_addr;
- unsigned long readval, writeval;
- off_t porth;
- if ( argc == 2 && ( argv[1][1] == 'h' || argv[1][1] == '-' ) )
- {
- printf("Usage: cbusb [+|-][1|2]...\n");
- printf("Sets the Cubieboard (A10 and A20) USB power directly for port 1 and 2.\n");
- printf("USB1 is the upper, USB2 is the lower - closer to the PCB - port.\n\n");
- printf("Be warned: it directly writes the data register of Port H, bypassing the kernel.\n\n");
- printf("Without parameters just prints the port power status.\n");
- printf("+ turns the power on, - turns it off for the specified port.\n");
- printf("Port H only gets written if the settings differ from the current ones.\n");
- printf("Note, changing power for one port might momentarily disrupt the other too.\n\n");
- printf("Examples:\n cbusb +1\n cbusb +2 -1\n\n");
- fflush(stdout);
- }
- setuid(0); // try
- if ( (fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1 )
- {
- printf("ERR: Can't open /dev/mem\n");
- exit(1);
- }
- if ( verbose )
- {
- printf("/dev/mem opened.\n");
- fflush(stdout);
- }
- porth = 0x1c2090c; // Port H data reg.
- map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, porth & ~MAP_MASK);
- if ( map_base == (void *) -1 )
- {
- printf("ERR: Can't map the requested memory page\n");
- exit(2);
- }
- if ( verbose )
- {
- printf("Memory mapped at address %p.\n", map_base);
- fflush(stdout);
- }
- virt_addr = map_base + (porth & MAP_MASK);
- readval = *((unsigned long *) virt_addr);
- printf("USB1: %i\n", (readval & 8) >> 3);
- printf("USB2: %i\n", (readval & 64) >> 6);
- if ( verbose ) printf("OLD: 0x%08x\n", readval);
- fflush(stdout);
- writeval = readval;
- int i;
- for (i=1; i<=2; ++i)
- {
- if ( argc > i )
- {
- unsigned long usbmask = 0;
- if ( argv[i][1]=='1' ) usbmask = 8; else if ( argv[i][1]=='2' ) usbmask = 64;
- if ( argv[i][0]=='+' ) writeval |= usbmask; else writeval &= ~usbmask;
- }
- }
- if ( verbose ) { printf("NEW: 0x%08x\n", writeval); fflush(stdout); }
- if ( writeval != readval )
- {
- *((unsigned long *) virt_addr) = writeval;
- printf("NEWUSB1: %i\n", (writeval & 8) >> 3);
- printf("NEWUSB2: %i\n", (writeval & 64) >> 6);
- fflush(stdout);
- }
- if ( munmap(map_base, MAP_SIZE) == -1 )
- {
- printf("ERR: munmap failed\n");
- exit(3);
- }
- close(fd);
- return 0;
- }
复制代码 保存为cbusb.c
arm-linux-gnueabihf-gcc-o cbusb cbusb.c
mv cbusb /usr/local/sbin/
查看帮助
cbusb -h
查看状态
cbusb
关闭USB1
cbusb -1
使能USB1
cbusb +1
|
|