Dr.Gusman
Posts: 13
Joined: Tue Nov 25, 2014 6:19 pm

BCM2835 port to C#

Wed Jun 29, 2016 3:12 am

Hi!

I have ported the BCM2835 library to full C# code.

The repository with the code is here: https://github.com/gusmanb/BCM2835Managed

It can be used as a DLL or just add the code file to your project and a reference to Mono.Posix and all is set-up, you have all the functions on the original BCM2835 plus some pin edge detection goodies.

It uses direct memory access to the devices except for the detection routine which uses a mix of files/threads/poll so it's as fast as it can be in c#.

I have tested the peripheral section and GPIO section, and as I have time I will test all the other functionalities.

To work you must enable Device tree and execute the app as root.

I only have for now a Rpi2, so if anyone tests this in another version I would be very thankful if you post the results, it must work on any device with the Device tree enabled.

If you have any question or request don't esitate to post it.

Cheers!

Alex111
Posts: 32
Joined: Sun Oct 07, 2012 6:56 am

Re: BCM2835 port to C#

Sat Jul 02, 2016 2:03 pm

Thanks for sharing your code.

Couple of questions:

Is edge detection as fast as the original c++ code? If not, is there a way to achieve similar performance with C# only?

Any advantages to this project : https://github.com/raspberry-sharp/raspberry-sharp-io ???

Dr.Gusman
Posts: 13
Joined: Tue Nov 25, 2014 6:19 pm

Re: BCM2835 port to C#

Sat Jul 02, 2016 3:26 pm

Hi Alex.

Yes, there are some advantages using this lib:

-Uses direct memory, instead of writing to the filesystem through /sys/class/gpio it maps to it's own memory memory the peripheral addresses and the access to these are done through raw pointers, so it should be much faster.

-Light, it only does the hardware manipulation, no extra checks, no validations, you must know what you do, but the final result should be more efficient.

-It uses Mono.Posix.Unix instead of declaring manually imports to the OS functions (what means if there is any change to these functions and you update Mono they will be handled).

-It implements the full PWM modules.

-It can work with or without root privileges, if you run it without root it can only interact with the GPIO.

-You can use RaspiSharp with it, check it at https://github.com/gusmanb/RaspiSharp

About the edge detector, you have two options, you can use the original functions on the bcm library (poll through bcm2835_gpio_eds) or the event detector inside the GPIO extras, the second one is the preferred method as it will not consume resources (no manual polling, uses system poll, which doesn't consume resources).

About speed, the bcm2835_gpio_eds should be as fast as the original C code except for the C# overhead, at the end it only reads a pointer, so it must be as fast as C# can be, the second method... well, not sure, haven't tested it, but it should be really fast as the library gets notified by the system when any IO is performed on the edge detection file.

Cheers.

McNerdius
Posts: 1
Joined: Wed Jul 20, 2016 4:29 am

Re: BCM2835 port to C#

Wed Jul 20, 2016 5:26 am

Awesome. The Pi Zero recently sucked me into the raspberry pi universe, and given my background in (hobby) microcontroller programming i jumped right into studying Broadcom's PDF and Mike McCauley's C library. I can't remember what i was googling around for but i stumbled across this - great timing.

This is a great demonstration debunking all-too-common C# myths like "it doesn't have pointers" and "it can't be used for low level code."

I really hope this doesn't get overlooked by the RPi+C# community.

Dr.Gusman
Posts: 13
Joined: Tue Nov 25, 2014 6:19 pm

Re: BCM2835 port to C#

Wed Jul 20, 2016 10:43 am

Yep, you're right, if you understand how and when to use pointers C# is equally powerful as C or C++, but because Microsoft discourages the "unsafe" methods it's considered by the community as a bad practice, but heck, they're there and work like a charm :)

The only caveat I had to supply was to have volatile pointers inside a function, I can't understand why the hell Microsoft decided they were unnecessary.

OTC
Posts: 6
Joined: Sun Jul 31, 2016 3:50 pm

Re: BCM2835 port to C#

Sun Jul 31, 2016 7:58 pm

Is this something that would allow me to capture images off the Camera Module v2? Or is it just for GPIO interactions? I'm very new to RPi and am having a hard time figuring out what API to use to interact with the Camera Module from C#. (Everything online points me to the Python picamera.) If your API is suitable, I have an RPi3 and would be happy to be your tester.

Dr.Gusman
Posts: 13
Joined: Tue Nov 25, 2014 6:19 pm

Re: BCM2835 port to C#

Sun Jul 31, 2016 9:46 pm

Nope, sorry, the library is for gpio, pwm, i2c and spi.

Chek this, its exactly what you seek (another time try ro google what you need, this is the first result searching "raspberry C# camera")

http://www.raspberry-sharp.org/romain-f ... spberrycam
https://bitbucket.org/rflechner/raspberrycam/wiki/Home

OTC
Posts: 6
Joined: Sun Jul 31, 2016 3:50 pm

Re: BCM2835 port to C#

Mon Aug 01, 2016 4:40 pm

Thank you. I did see that link (I've done quite a bit of Googling in this process, trust me) but dismissed it because it appears to expect the camera is connected via USB, whereas I'm looking for the Camera Module manufactured by Raspberry Pi themselves. I cross-posted to another forum and seem to have gotten an answer though. Thanks again.

michasanyi
Posts: 4
Joined: Fri Jul 29, 2016 3:16 pm

Re: BCM2835 port to C#

Sun Oct 02, 2016 8:11 am

Dear Dr.Gusman,

I'm a newbie in Linux and Raspberry world, I'm coming from Windows programming mainly in C#. That's why I appreciate your effort making BCM2835 ports available from C#.
I begun to use it, it is really awesome.

However I'm afraid of I've observed an issue in the code: In GPIOExtras.unexport_pin method the pin number is written into the same file ("/sys/class/gpio/export") as was in export method. In documentation (Git\linux-rpi-4.4.y\Documentation\gpio\sysfs.txt) it writte that unexport method should write pin number into another file: "/sys/class/gpio/unexport". The difference might not cause big errors - until different programs try to use the same GPIO pin. But when one process reserves a given pin with export_pin() for its own purpose (export), uses it and later unreserves it with unexport_pin(), then the pin will not be freed. And the next process will not be able to use that given pin. Changing "/sys/class/gpio/export" to "/sys/class/gpio/unexport" in unexport_pin() method solves the problem.

With best regards,
michasanyi

Dr.Gusman
Posts: 13
Joined: Tue Nov 25, 2014 6:19 pm

Re: BCM2835 port to C#

Sun Oct 02, 2016 9:42 am

Hi michasanyi.

Thanks for the info, you're right, that's a typo, it should be unexport instead of export.

I have corrected it and pushed to github.

Cheers.

Return to “Other programming languages”