2

First my Assignment:

I'm a beginner with BSD and low level peripherals like GPIO etc. and I need to toggle Raspberry GPIO Pins from a C programm on BSD.

I am Using this BSD Image

i took from here: FreeBSD - easiest way to install on my RPi?

and try toggling the GPIO Pins, respectively trying to set a Pin to high by calling MMAP to access the Registers directly form C code.

I took the addressesfrom the BCM2835 Datasheet, Cahpter 1.2.3, page 6 "physical Adresses".

Is my assumption right that it does not matter if I use Raspbian or any other OS - these physical addresses will stay the same, as they are given by the Chip?

I tested my Code as root with Raspbian and it worked fine, i also compiled the Code with gcc in BSD, no errors. The Programm is running as root, the System does not crash or freeze.

As getting a place where i can actually test my programm is hard where i study, i just wanted some feedback please. Are my assuptions correct or did i go horribly wrong somewhere? Eg. Does BSD somehow remap physical memory addresses?(is that possible?) If yes, is there a way to find out how this mapping is done?

Or "should" this work alright in theory?

I really appreciate any help, the assignment is vital to my grade and i am affraid i might have overesteemated myself by choosing this assignment^^.

Many thanks in advance for help!

billdoor
  • 121
  • 3

2 Answers2

2

I'm not a BSD user, but it seems that BSD also implements /dev/mem -- and mmap, of course.

There's a good explanation of how this works in another Q&A.

Does BSD somehow remap physical memory addresses?(is that possible?)

All modern OS's use virtual addressing, meaning, yes, physical addresses are remapped in an unpredictable way; a process's address space does not at all correspond literally to hardware addresses. Further, trying to use direct addressing will generally get you a segmentation fault, unless those addresses happen to be based on space you've actually allocated.

That's why /dev/mem is necessary, and as per the man page, this is exactly what it is for:

The special file /dev/mem is an interface to the physical memory of the computer. Byte offsets in this file are interpreted as physical memory addresses. Reading and writing this file is equivalent to reading and writing memory itself. Only offsets within the bounds of /dev/mem are allowed.

So your method is sound, in so far as the GPIO pins are concerned.

goldilocks
  • 56,430
  • 17
  • 109
  • 217
  • ok sound good. i have now tested with a borrowed oscilloscope. i access /dev/mem in raspian returns not an error code everything is ok, pin toggles. i do the same under bsd, nothing happens. mmap also returns no errorcode. if dev/mem is a file to access physical memory and the chip manuall mentions physical addresses, what could be wrong? – billdoor Jan 23 '14 at 18:43
  • 1
    Since I'm not actually a BSD user, I can't say what the difference is here. You might want to [ask about this on U&L](http://unix.stackexchange.com/), which has a much larger user base and includes (Free)BSD under its umbrella. Explain you are using *memory mapped I/O* (via `/dev/mem` and `mmap()`) to access hardware registers on the raspberry pi, and your code works under linux but fails without error on FreeBSD. Include a cut n' paste of some relevant parts of that code. – goldilocks Jan 23 '14 at 19:00
0

You may want to look at freebsd-gpio (in Perl, Python, and Ruby) or fbsd_gpio_py (Python only, but it looks more active) on GitHub – judging from the Raspberry Pi entry in the FreeBSD Wiki this is the "official" way to do it using ioctl() calls on /dev/gpio0.

dlu
  • 439
  • 3
  • 19