Skip to content

Commit ab20049

Browse files
author
Eric Bézine
committed
Removed dependency on libbcm2835.so
1 parent 320a08c commit ab20049

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

Raspberry.System.nuspec

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
Raspberry.System is a Mono/.NET assembly providing access to Raspberry Pi system features.
1515

1616
It is an initiative of the Raspberry# Community, aimed at providing tools and information concerning use of Raspberry Pi boards with Mono/.NET framework.
17-
Parts of this work are based on [BCM2835 C library](http://www.open.com.au/mikem/bcm2835/).
18-
1917
Visit project [Github site](https://github.com/raspberry-sharp/raspberry-sharp-system/) for documentation and samples.
2018
</description>
2119
<summary>Raspberry.System is a Mono/.NET assembly providing access to Raspberry Pi system features.</summary>
@@ -30,7 +28,6 @@
3028
<file src="Raspberry.System\bin\release\Raspberry.System.dll" target="lib\net40" />
3129
<file src="Raspberry.System\bin\release\Raspberry.System.pdb" target="lib\net40" />
3230
<file src="Raspberry.System\bin\release\Raspberry.System.xml" target="lib\net40" />
33-
<file src="Raspberry.System\bin\release\libbcm2835.so" target="lib\net40" />
3431
<file src="Raspberry.System\**\*.cs" target="src" />
3532
</files>
3633
</package>

Raspberry.System/Raspberry.System.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@
4545
<Compile Include="Timers\StandardTimer.cs" />
4646
<Compile Include="Timers\Timer.cs" />
4747
</ItemGroup>
48-
<ItemGroup>
49-
<None Include="libbcm2835.so">
50-
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
51-
</None>
52-
</ItemGroup>
5348
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5449
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5550
Other similar extension points exist, see Microsoft.Common.targets.

Raspberry.System/Timers/HighResolutionTimer.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#region References
22

33
using System;
4+
using System.Linq;
45
using System.Threading;
56

67
#endregion
@@ -20,10 +21,15 @@ public class HighResolutionTimer : ITimer
2021

2122
private Thread thread;
2223

24+
private static readonly int nanoSleepOffset = Calibrate();
25+
2326
#endregion
2427

2528
#region Instance Management
2629

30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="HighResolutionTimer"/> class.
32+
/// </summary>
2733
public HighResolutionTimer()
2834
{
2935
if (!Board.Current.IsRaspberryPi)
@@ -80,12 +86,39 @@ public Action Action
8086
/// <param name="delay">The delay.</param>
8187
public static void Sleep(decimal delay)
8288
{
89+
// Based on [BCM2835 C library](http://www.open.com.au/mikem/bcm2835/)
90+
91+
// Calling nanosleep() takes at least 100-200 us, so use it for
92+
// long waits and use a busy wait on the hires timer for the rest.
93+
var start = DateTime.Now.Ticks;
94+
8395
if (delay >= 100)
84-
Thread.Sleep((int)delay);
96+
{
97+
// Do not use high resolution timer for long interval (>= 100ms)
98+
Thread.Sleep((int) delay);
99+
}
100+
else if (delay > 0.450m)
101+
{
102+
var t1 = new Interop.timespec();
103+
var t2 = new Interop.timespec();
104+
105+
// Use nanosleep if interval is higher than 450µs
106+
t1.tv_sec = (IntPtr)0;
107+
t1.tv_nsec = (IntPtr)((long) (delay * 1000000) - nanoSleepOffset);
108+
109+
Interop.nanosleep(ref t1, ref t2);
110+
}
85111
else
86-
Interop.bcm2835_delayMicroseconds((uint)(delay * 1000));
112+
{
113+
while (true)
114+
{
115+
if ((DateTime.Now.Ticks - start) * 0.0001m >= delay)
116+
break;
117+
}
118+
}
87119
}
88120

121+
89122
/// <summary>
90123
/// Starts this instance.
91124
/// </summary>
@@ -126,6 +159,28 @@ public void Stop()
126159

127160
#region Private Helpers
128161

162+
private static int Calibrate()
163+
{
164+
const int referenceCount = 1000;
165+
return Enumerable.Range(0, referenceCount)
166+
.Aggregate(
167+
(long) 0,
168+
(a, i) =>
169+
{
170+
var t1 = new Interop.timespec();
171+
var t2 = new Interop.timespec();
172+
173+
t1.tv_sec = (IntPtr) 0;
174+
t1.tv_nsec = (IntPtr) 1000000;
175+
176+
var start = DateTime.Now.Ticks;
177+
Interop.nanosleep(ref t1, ref t2);
178+
179+
return a + ((DateTime.Now.Ticks - start) * 100 - 1000000);
180+
},
181+
a => (int)(a / referenceCount));
182+
}
183+
129184
private void ThreadProcess()
130185
{
131186
var thisThread = thread;

Raspberry.System/Timers/Interop.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
1+
#region References
2+
3+
using System;
14
using System.Runtime.InteropServices;
25

6+
#endregion
7+
38
namespace Raspberry.Timers
49
{
510
internal static class Interop
611
{
12+
#region Constants
13+
14+
public static int CLOCK_MONOTONIC_RAW = 4;
15+
16+
#endregion
17+
18+
#region Classes
19+
20+
public struct timespec
21+
{
22+
public IntPtr tv_sec; /* seconds */
23+
public IntPtr tv_nsec; /* nanoseconds */
24+
}
25+
26+
#endregion
27+
728
#region Methods
829

9-
[DllImport("libbcm2835.so", EntryPoint = "bcm2835_delayMicroseconds")]
10-
public static extern void bcm2835_delayMicroseconds(uint microseconds);
30+
[DllImport("libc.so.6")]
31+
public static extern int nanosleep(ref timespec req, ref timespec rem);
1132

1233
#endregion
1334
}

Raspberry.System/libbcm2835.so

-29.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)