/
Program.cs
282 lines (203 loc) · 8.09 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using GHIElectronics.Endpoint.Core;
using GHIElectronics.Endpoint.Native;
using Steadsoft.Spitfire.STM32MP153;
using System.Collections.Specialized;
using System.Device.Gpio;
using System.Device.Gpio.Drivers;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.InteropServices;
using Steadsoft.Spitfire.STM32MP153.Peripherals;
using static Steadsoft.Spitfire.STM32MP153.PeripheralAddress;
using static Steadsoft.Spitfire.STM32MP153.PeripheralInstance;
using static Steadsoft.Spitfire.STM32MP153.Peripherals.Gpio;
using static Steadsoft.Spitfire.STM32MP153.Peripherals.TimerGeneral;
namespace PeripheralAddresses
{
internal class Program
{
// STM32MP153 - TFBGA361 package
// REFMAN https://www.st.com/resource/en/reference_manual/rm0442-stm32mp153-advanced-armbased-32bit-mpus-stmicroelectronics.pdf
// DATASHEET https://www.st.com/resource/en/datasheet/stm32mp153a.pdf
// ALSO: https://community.st.com/t5/stm32-mpus-products/using-mmap-in-c-code-from-linux-application-to-access-gpios-i/td-p/239506
// Board pinouts
// https://www.ghielectronics.com/endpoint/
private static readonly Stopwatch clock = new();
private static readonly int iterations = 37000; // 32616 or greater causes out of memory exception.
public static void Main(string[] args)
{
Console.WriteLine($"Board Image Version: {DeviceInformation.Version}.");
Console.WriteLine("Pulsing via DAC");
PulseViaDac();
Console.WriteLine("Pulsing via Timer GPIO");
PulseViaTimerGpio();
Console.WriteLine("Pulsing via Timer");
PulseViaTimer();
Console.WriteLine("Pulsing via Register");
PulseViaRegister();
//Console.WriteLine("Pulsing via Library");
//PulseViaLibrary();
//Console.WriteLine($"Elapsed seconds: {clock.Elapsed.TotalSeconds}");
Console.WriteLine("Pulsing via Direct");
PulseViaDirect();
Console.WriteLine($"Elapsed seconds: {clock.Elapsed.TotalSeconds}");
Console.ReadLine();
}
public static void PulseViaDac()
{
double[] sine = new double[4096];
uint[] intsine = new uint[4096];
double full = Math.PI * 2;
double incr = full / 2048;
for (int i = 0; i < 4096; i++)
{
sine[i] = Math.Sin(i * incr);
intsine[i] = Convert.ToUInt32((sine[i] + 1) * 2048);
}
using var clocks = Peripheral.OpenRcc();
using var gpioa = Peripheral.OpenGpio(GpioPort.A);
using var dac1 = Peripheral.OpenDac();
clocks.MP_APB1ENSETR.DAC12EN = true;
clocks.MP_AHB4ENSETR[GpioPort.A] = true;
gpioa.MODER[4] = Mode.Analog;
dac1.CR.EN1 = true;
uint data = 0;
while (true)
{
for (int i = 0; i < 4096; i++)
{
dac1.DHR12R1.DACC1DHR = intsine[i];
}
}
}
public static void PulseViaTimerGpio()
{
using var clock = Peripheral.OpenRcc();
using var timer = Peripheral.OpenTimer(GeneralTimer.TIM2);
using var gpioa = Peripheral.OpenGpio(GpioPort.A);
// Enable clocks for GPIOA and TIM2
clock.MP_APB1ENSETR.TIM2EN = true;
clock.MP_AHB4ENSETR[GpioPort.A] = true;
// Setup the GPIO pin 5 here
gpioa.MODER[5] = Mode.Alternate;
gpioa.OSPEEDR[5] = Speed.VeryHigh;
gpioa.PUPDR[5] = Pull.None;
gpioa.AFRL[5] = AlternateFunction.AF1;
// Setup the timer registers
timer.PSC = 9;
timer.ARR = 4;
timer.CCMR1.OC1M = OCMode.ToggleOnMatch;
timer.CCR1.AllBits = 0;
timer.CCER.CC1E = true;
timer.CNT_NRE = 0;
timer.CR1.CEN = true;
Thread.Sleep(-1); // do nothing
}
/// <summary>
/// Polls a timer and toggles LED on expiration
/// </summary>
public static void PulseViaTimer()
{
using var clock = Peripheral.OpenRcc();
using var timer = Peripheral.OpenTimer(GeneralTimer.TIM2);
using var gpioa = Peripheral.OpenGpio(GpioPort.A);
// Enable clocks for GPIOA and TIM2
clock.MP_APB1ENSETR.TIM2EN = true;
clock.MP_AHB4ENSETR[GpioPort.A] = true;
// Setup the GPIO pin 4 here
gpioa.MODER[4] = Mode.Output;
gpioa.OSPEEDR[4] = Speed.VeryHigh;
gpioa.PUPDR[4] = Pull.Down;
// Setup the timer registers
timer.PSC = 500;
timer.ARR = 20;
timer.CNT_NRE = 0;
// Enable counting
timer.CR1.CEN = true;
// Crudely poll and toggle PA4 on each timer overflow
while(true)
{
while(timer.SR.UIF == false)
{
}
gpioa.BSRR = 0x_00000010U;
gpioa.BSRR = 0x_00100000U;
// Reset the flag
timer.SR.UIF = false;
}
}
public static void PulseViaDirect()
{
using var rcc = Peripheral.OpenRcc();
using var gpioa = Peripheral.OpenGpio(GpioPort.A);
rcc.MP_AHB4ENSETR[GpioPort.A] = true;
//gpioa.OSPEEDR.AllBits = 0;
gpioa.EditRegister(ref gpioa.OSPEEDR, (ref Gpio.Registers.OSPEEDR reg) =>
{
reg[2] = Gpio.Speed.Medium;
reg[3] = Gpio.Speed.VeryHigh;
}
);
var mem = GC.GetGCMemoryInfo();
// This serves no real purpose here but does prevent
// any collecttions taking place within the designated region.
Platform.WithoutGC(4096, () =>
{
gpioa.MODER[4] = Gpio.Mode.Output;
gpioa.OSPEEDR[4] = Gpio.Speed.VeryHigh;
gpioa.PUPDR[4] = Gpio.Pull.Down;
});
while (true)
{
gpioa.BSRR.AllBits = 0x_00000010;
gpioa.BSRR.AllBits = 0x_00100000;
}
}
public static void PulseViaLibrary()
{
var port = EPM815.Gpio.Pin.PA4 / 16;
var pin = EPM815.Gpio.Pin.PA4 % 16;
var gpioDriver = new LibGpiodDriver((int)port);
var gpioController = new GpioController(PinNumberingScheme.Logical, gpioDriver);
gpioController.OpenPin(pin);
gpioController.SetPinMode(pin, PinMode.Output);
clock.Start();
for (int I = 0; I < iterations; I++)
{
gpioController.Write(pin, PinValue.High);
gpioController.Write(pin, PinValue.Low);
}
clock.Stop();
}
public static void PulseViaRegister()
{
var id = EPM815.Gpio.Pin.PA0;
var port = id / 16;
var pin = id % 16;
var GPIO_PORT = 0x50002000 + port * 0x1000;
var GPIO_PORT_MODER = GPIO_PORT + 0; var GPIO_PORT_OTYPER = GPIO_PORT + 0x4;
var GPIO_PORT_BSRR = GPIO_PORT + 0x18;
Register.Write(0x50000A28, (uint)(1 << (int)port));
var value = Register.Read((uint)GPIO_PORT_MODER);
var clear = 3 << (pin * 2);
value &= (uint)~clear;
var set = (uint)1 << (pin * 2);
value |= set;
Register.Write((uint)GPIO_PORT_MODER, value);
clock.Start();
for (int I = 0; I < iterations; I++)
{
try
{
Register.Write((uint)GPIO_PORT_BSRR, 1 << 0);
Register.Write((uint)GPIO_PORT_BSRR, 1 << 16);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
clock.Stop();
}
}
}