# /dev/gpiomem error for BaremetalPi (Mmap.mmap regression)

**URL:** https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611
**Category:** General Usage
**Tags:** question, package
**Created:** [April 6, 2024, 4:33pm UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611 "2024-04-06T16:33:30Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [April 6, 2024, 4:33pm UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/1 "2024-04-06T16:33:30Z")

</div>

On a Raspberry PI 4 with 8 gig memory, I have upgraded the OS to Bookworm. Now when I execute the function `init_gpio()`  
it gives me the error `Could not map /dev/gpiomem . Make sure you have the required permissions.`

When I look at the permissions of /dev/gpiomem, it is rw-rw----. I changed this to rw-rw-rw- to no avail. When I reboot the PI it goes back to the original permissions of rw-rw----.

This function was working on the previous OS, so that may be an option. Anything that I am overlooking? Perhaps @Ronis_BR can help.

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [April 6, 2024, 7:26pm UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/2 "2024-04-06T19:26:24Z")

</div>

Some further research shows that /dev/gpiomem is part of the gpio group. The user name also has gpio as one of the groups it belongs to.

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [April 6, 2024, 7:39pm UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/3 "2024-04-06T19:39:17Z")

</div>

Hi! I have no idea! I always tested everything in the RPI using `root`.

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [April 9, 2024, 2:48pm UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/4 "2024-04-09T14:48:39Z")

</div>

Initially I thought the error may have something to do with the upgrade in OS from Bullseye to Bookworm. This turns out not to be the case.

Some internet searching indicates that gpio should be a member of the group logged in (use the groups command to find out). This seems to be the case by default. Also I did all testing as a normal user, not as root.

In using Bullseye, I went back to Julia 1.10.0, and the error is not present. Then I tried Julia 1.10.2 and the error is present. The error occurs in the line with the function call Mmap.mmap in the function init\_gpio(), shown below, in BaremetalPi file gpio.jl, so it seems that the Mmap function call has changed.

Is this something that an issue should be raised for?

```julia
"""
    init_gpio()

Initialize the GPIO.
"""
function init_gpio()
    # Open and map /dev/gpiomem.
    try
        gpiomem_io = open("/dev/gpiomem", "w+")
        gpiomem_map = Mmap.mmap(
            gpiomem_io,
            Vector{UInt32},
            (GPIOMEM_SIZE,),
            0,
            grow = false
        )

        objects.gpiomem_io = gpiomem_io
        objects.gpiomem_map = gpiomem_map
        objects.gpio_init = true
    catch
        error("Could not map `/dev/gpiomem`. Make sure you have the required permissions.")
    end

    return nothing
end

```

edit: I added to the title to more accurately reflect the problem

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [April 13, 2024, 3:31am UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/5 "2024-04-13T03:31:55Z")

</div>

To see what the actual error is, copy paste first 2 function calls to julia repl. Otherwise, you see the error thrown in the catch block and it suppresses the one in try block.

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [April 22, 2024, 9:32pm UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/6 "2024-04-22T21:32:35Z")

</div>

Using a Raspberry PI 4, the output from Julia 1.10.0 is

```julia
julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (aarch64-linux-gnu)
  CPU: 4 Ã Cortex-A72
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, cortex-a72)
  Threads: 1 on 4 virtual cores

julia> using Mmap

julia> io = open("/dev/gpiomem", "w+")
IOStream(<file /dev/gpiomem>)

julia> gpiomap = Mmap.mmap(io, Vector{UInt32}, (1024,), 0, grow=false)
1024-element Vector{UInt32}:
 0x21200900
 0x00040264
 0x12040000
 0x3fffffff
 0x00000064
 0x00000000
 0x00000000
 0x6770696f
 0x6770696f
 0x6770696f
          â®
 0x6770696f
 0x6770696f
 0x6770696f
 0x6770696f
 0x6770696f
 0x6770696f
 0x6770696f
 0x6770696f
 0x6770696f

julia> 

```

and from Julia 1.10.1

```julia
julia> versioninfo
versioninfo (generic function with 2 methods)

julia> versioninfo()
Julia Version 1.10.1
Commit 7790d6f0641 (2024-02-13 20:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (aarch64-linux-gnu)
  CPU: 4 Ã Cortex-A72
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, cortex-a72)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

julia> using Mmap

julia> io = open("/dev/gpiomem", "w+")
IOStream(<file /dev/gpiomem>)

julia> gpiomap = Mmap.mmap(io, Vector{UInt32}, (1024,), 0, grow=false)
ERROR: IOError: filesize: Illegal seek for <file /dev/gpiomem>
Stacktrace:
 [1] filesize(s::IOStream)
   @ Base ./iostream.jl:226
 [2] mmap(io::IOStream, ::Type{Vector{UInt32}}, dims::Tuple{Int64}, offset::Int64; grow::Bool, shared::Bool)
   @ Mmap ~/.julia/juliaup/julia-1.10.1+0.aarch64.linux.gnu/share/julia/stdlib/v1.10/Mmap/src/Mmap.jl:215
 [3] top-level scope
   @ REPL[4]:1

julia> 

```

edit: I filed a bug [report](https://github.com/JuliaLang/julia/issues/54203) for Mmap, hopefully I did it correctly. Letting @Ronis_BR know, as it affects BaremetalPI.

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [April 22, 2024, 10:19pm UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/7 "2024-04-22T22:19:10Z")

</div>

I’m pretty sure the regression is due to [🐛 fix bus error on smaller readonly file in unix by xgdgsc · Pull Request #44354 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/44354) as I noted on the issue. The call to `filesize` introduced there is not appropriate in all cases.

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [April 23, 2024, 3:55am UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/8 "2024-04-23T03:55:15Z")

</div>

OK. How many bytes can you actually read on the `/dev/gpiomem` and how many bytes `filesize` reports? If the `filesize` result matches correct size then 1024 is very long to request. ~~Small internet search showed that people use [0xB4 for `mmap` block size](https://raspberrypi.stackexchange.com/questions/99785/what-is-the-correct-way-to-use-dev-gpiomem-with-mmap-to-get-access-to-raspberry).~~ Nevermind, I found some uses 4096 so it might be different thing then I thought. Maybe, `GPIOMEM_SIZE` is not set to the correct value.

---

<div class="post-metadata">

### Author: ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)
#### Post date: [April 23, 2024, 10:55am UTC](https://discourse.julialang.org/t/dev-gpiomem-error-for-baremetalpi-mmap-mmap-regression/112611/9 "2024-04-23T10:55:58Z")

</div>

Thanks @Jake !

I will only have access to a Raspeberry Pi this weekend to test. However, I have no ideia if there is any workaround. Let’s see what the devs say in that issue.
