Turn off power to a USB port

和自甴很熟 提交于 2019-12-05 13:53:02

Indeed, that other question did have a technique that worked for what I was trying to do. Note this isn't a generic Linux answer, it will only work on BeagleBone Black and similar devices. (I tested on a BeagleBone Green.) Working backwards from the devmem2 example, this block of C++ code turns the USB power off, then back on:

const size_t page_size_in_bytes = getpagesize();
const size_t address_gpio3_13   = 0x47401c60; // see comment below
const size_t address_start      = address_gpio3_13 / page_size_in_bytes * page_size_in_bytes;
const size_t address_offset     = address_gpio3_13 - address_start;

int fd = open("/dev/mem", O_RDWR);
void *addr = mmap( 0, page_size_in_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, address_start );

uint8_t *byte_ptr = reinterpret_cast<uint8_t*>(addr);

byte_ptr[address_offset] = 0x00;    // turn off USB
std::this_thread::sleep_for( std::chrono::milliseconds(500) );
byte_ptr[address_offset] = 0x01;    // turn on USB

munmap( addr, page_size_in_bytes );

close(fd);

(Error handling not included.)

The magic number 0x47401c60 really is a magic number. According to some posts, it looks like a NDA needs to be signed to get access to some of the USB-related documentation. In the ARM335X Technical Reference Manual, the only mention of the 0x47401Cxx address space is the following on page 156:

Block Name    Start Address    End Address
USB1 Core     0x4740_1C00      0x4740_1FFF
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!