Trying to access the sensor's registers using memory-mapped I/O in a flat memory model

Hi, I'm working on a small embedded system project using x86-64 assembly on an Intel Atom E3845 microprocessor. The program aims to read temperature data from an Adafruit BMP280 sensor connected with I2C
Solution
No more error message, it worked:
global _start

section .text
_start:
    ; I2C communication registers set up...

    ; 
    mov $0, %eax  ; opening system call
    mov $0xFD, %ebx ; open file descriptor for /dev/mem
    mov $0, %ecx  ; 
    mov $_dev_mem, %edx  ; 
    int $0x80     ; system call

    ; checking for successful open
    cmp $0, %eax
    jl .open_failed  ; if fails

    ; Mapping my memory region 
    mov $9, %eax   ; mmap system call
    mov %eax, %ebx  ; file descriptor from open
    mov $0x1000, %ecx ; 
    mov $0x1000, %edx ; 
    mov $0x1, %esi  ; prot - PROT_READ
    mov $0x0, %edi  ; flags
    mov $0, %ebp   ; offset 
    int $0x80     ; system call

    ; if mapping is successful
    cmp $0, %eax
    jl .mmap_failed  ; if mapping fails 

    ; accessing registers using the mapped address
    movb (%rax), %bl  ; reading byte from temp register

    ; processing my temp data...

    ; Unmapping memory region before exiting 
    mov $17, %eax  ; munmap system call
    mov %eax, %ebx  ; mapped address
    mov $0x1000, %ecx ; 
    int $0x80     ; system call

    mov $1, %eax
    int $0x80         ; exit system call

.open_failed:
    ; Handle error message 
    ...

.mmap_failed:
    ; Handle mapping error 
    ...

section .data
    _dev_mem: db "/dev/mem", 0x0  ; 
    message db "Temp data read", 0x0

Had to dig deep with some resources using mmap
Was this page helpful?