Dr.Gusman
Posts: 15
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: 15
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: 15
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: 15
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: 15
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.

ldog3211
Posts: 2
Joined: Thu Jan 16, 2020 5:22 am

Re: BCM2835 port to C#

Thu Jan 16, 2020 5:30 am

I know this project is quite old now, but I'm new to programming and I was wondering if it would be possible to somehow get this library to work on a Raspberry Pi with Windows IoT as the operating system instead of Raspian? We're reading in input from an ADC via SPI, and I've successfully translated our input/output code from C with the C library to C#. When I run, however, it says that the libc.dll dependency cannot be found, and after some research I found out that it's built into the Unix kernel. Is there any way to modify the code in the library to get around this and run on a Windows based operating system? If not, what process do you recommend for creating C# solutions containing multiple projects and running them on a Raspberry Pi without Visual Studio? The first option is preferable as Windows IoT is much more seamless when it comes to C# development on a Rasp Pi, but we're also familiar with development on Raspian, though we've only ever had experience with C and C++ and don't know where to start with C# development on the Pi. Any help would be immensely appreciated as we're stuck in a rut right now, haha, so thank you in advance for your time!

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

Re: BCM2835 port to C#

Thu Jan 16, 2020 10:42 am

It's not possible to translate this code to Windows as it uses features of Linux totally incompatible with windows. Also, it does not make sense at all as Windows IoT already exposes all necessary API's to handle the hardware side.

About developing, why you don't want to use Visual Studio? Install the community edition and that's all, you will have the best IDE for developing, there's no better tool for programming than VS.

To be honest, what I see is a lot of bad decissions (try to port code from Linux, not to use the correct tools, etc) based on lack of knowledge of the platform. Don't try to reinvent the wheel and use VS and the IoT API, read an ADC using it is a matter of three lines. Here you have a link to a tutorial that makes exactly what you're asking for: https://www.hackster.io/HiAmadeus/analo ... adc-493ab9

Cheers.

ldog3211
Posts: 2
Joined: Thu Jan 16, 2020 5:22 am

Re: BCM2835 port to C#

Sat Jan 18, 2020 3:38 pm

Yeahhh after looking into it a bit more while I was waiting for my reply to post on the forum we discovered it was actually much easier than we were making it out to be to accomplish the same task of reading in those digital signals with IoT. I actually ran across that same tutorial too haha. Now the only issue is that Windows IoT doesn't officially support any Rasp Pi board past the Pi 3 Model B (B+ is a non-fully-functional technical preview), so would there be any downside to working with the Model B other than slower performance based on your experience? Thank you for your help by the way, I really appreciate it!

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

Re: BCM2835 port to C#

Sat Jan 18, 2020 7:27 pm

It should be no problem at all, the RB3B is more than enought for any IoT project, the RB4 will make no difference.

Cheers.

Return to “Other programming languages”