고급 개발자로 가는 길

C

[C언어] sram open 및 설정

다크엔지니어 2021. 3. 29. 21:18
반응형

임베디드 시스템에서의 C 언어를 통해 sram 메모리를 읽어와 설정 하는 부분을 진행 해 볼것이다.

우선 OS 는 Custom OS 이다. 완전한 리눅스랑은 상이하다.

 

/dev/mem 에 메모리 장치 파일이 존재한다.

즉, 메모리 장치 파일에 접근하여 파일의 메모리 공간을 가져와야 한다.

메모리 공간을 가져오는 방법으로는 lseek or mmap 등이 존재한다.

이 중 mmap 방법을 활용해 볼 것 이다.

 

void *pMem_sram;
unsigned short		usi;
unsigned short		*usp;
int					fd_sram;

fd_sram = open("/dev/mem", O_RDWR);
    
if( fd_sram < 0 )
{
    printf("ERROR: Failed to open SRAM\n");
    exit(-1);
}

pMem_sram = mmap(0, 0x7FFFF, PROT_READ | PROT_WRITE, MAP_SHARED, fd_sram, 0x1000000);

if( pMem_sram == MAP_FAILED )
{
    printf("ERROR: Failed to mmap SRAM\n");
    exit(-1);
} 
반응형