Byte 오프셋이기 때문에 BASE_ADDR + (OFFSET/4)를 해야합니다. 가 무슨 뜻인가요...?
현재 비글보드를 이용하고 mmap을 이용해서 GPIO를 direct로 제어하려고 코드를 짜고 있습니다.
QNA를 보던 도중에 레지스터에 특정 값을 써서 GPIO 뱅크를 Enable 해야하는데 그 코드 중에 Byte 오프셋이기 때문에 오프셋을 4로 나눈 값을 더한다라고 하는데...
왜 4로 나누는지 이해가 잘 안가네요...
AVR 공부할 땐 그냥 OFFSET 더해주고 바로 값을 썼는데... ARM은 다른가요..?
uint32_t *clock_gpio2;
clock_gpio2 = (uint32_t *) mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0x44E00000);
// 0xb0 is CM_PER_GPIO2_CLKCTRL as given in the TRM, use 0xb4 for GPIO_3 (see the TRM)
int offsetInMemory = 0xb0;
// get the value, we divide by 4 because it is a byte offset
int memValue = clock_gpio2[(offsetInMemory/4)];
// print it – it will probably be 0x030000 if the clock has never been enabled
printf("Value = %04x\n", memValue);
// now set it, this enables the memory
clock_gpio2[(offsetInMemory/4)] = 0x02;
clock_gpio2 가 uint32_t의
clock_gpio2 가 uint32_t의 포인터이므로 인덱스값이 1씩 증가할때 실제 (바이트단위의) 메모리주소값은 4 byte씩 증가합니다. 따라서 바이트 오프셋값인 offsetInMemory의 값이 가리키는 메모리주소와
clock_gpio2의 인덱스가 가리키는 메모리주소를 일치시킬려면 offsetInMemory에 4를 나눈값을 인덱스로 써야 하는 것입니다
아..! 정말 고맙습니다. mmap의 반환형을
아..! 정말 고맙습니다.
mmap의 반환형을 char *로 해주니 4로 나누지 않아도 되네요.. ㅎ
댓글 달기