-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cs
330 lines (294 loc) · 13.5 KB
/
test.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//
// Copyright (c) 2010-2023 Antmicro
//
// This file is licensed under the MIT License.
// Full license text is available in 'licenses/MIT.txt'.
//
using System;
using System.Collections.Generic;
using Antmicro.Renode.Core;
using Antmicro.Renode.Logging;
using Antmicro.Renode.Utilities;
using Antmicro.Renode.Exceptions;
using Antmicro.Renode.Peripherals.Bus;
using Antmicro.Renode.Core.Structure.Registers;
namespace Antmicro.Renode.Peripherals.GPIOPort
{
[AllowedTranslations(AllowedTranslation.WordToDoubleWord)]
public class STM32_GPIOPort : BaseGPIOPort, IDoubleWordPeripheral, ILocalGPIOReceiver
{
public STM32_GPIOPort(IMachine machine, uint modeResetValue = 0, uint outputSpeedResetValue = 0, uint pullUpPullDownResetValue = 0,
uint numberOfAFs = 16) : base(machine, NumberOfPins)
{
if(numberOfAFs < 1 || numberOfAFs > 16)
{
throw new ConstructionException("Number of alternate functions can't be lower than 1 or higher than 16");
}
mode = new Mode[NumberOfPins];
outputSpeed = new OutputSpeed[NumberOfPins];
pullUpPullDown = new PullUpPullDown[NumberOfPins];
this.modeResetValue = modeResetValue;
this.outputSpeedResetValue = outputSpeedResetValue;
this.pullUpPullDownResetValue = pullUpPullDownResetValue;
this.numberOfAFs = numberOfAFs;
alternateFunctionOutputs = new GPIOAlternateFunction[NumberOfPins];
for(var i = 0; i < NumberOfPins; i++)
{
alternateFunctionOutputs[i] = new GPIOAlternateFunction(this, i);
}
registers = CreateRegisters();
Reset();
}
public override void Reset()
{
base.Reset();
registers.Reset();
for(var i = 0; i < NumberOfPins; i++)
{
// Reset AF outputs before resetting pin modes as changing pin mode can affect whether AF is connected or not.
alternateFunctionOutputs[i].Reset();
ChangeMode(i, (Mode)BitHelper.GetValue(modeResetValue, 2 * i, 2));
outputSpeed[i] = (OutputSpeed)BitHelper.GetValue(outputSpeedResetValue, 2 * i, 2);
pullUpPullDown[i] = (PullUpPullDown)BitHelper.GetValue(pullUpPullDownResetValue, 2 * i, 2);
}
}
public uint ReadDoubleWord(long offset)
{
return registers.Read(offset);
}
public void WriteDoubleWord(long offset, uint value)
{
registers.Write(offset, value);
}
public override void OnGPIO(int number, bool value)
{
base.OnGPIO(number, value);
Connections[number].Set(value);
}
public IGPIOReceiver GetLocalReceiver(int pin)
{
if(pin < 0 || pin >= NumberOfPins)
{
throw new RecoverableException($"This peripheral supports GPIO inputs from 0 to {NumberOfPins - 1}, but {pin} was called.");
}
return alternateFunctionOutputs[pin];
}
private void WritePin(int number, bool value)
{
State[number] = value;
Connections[number].Set(value);
}
private void WriteState(ushort value)
{
for(var i = 0; i < NumberOfPins; i++)
{
var state = ((value & 1u) == 1);
WritePin(i, state);
value >>= 1;
}
}
private void ChangeMode(int number, Mode newMode)
{
mode[number] = newMode;
alternateFunctionOutputs[number].IsConnected = newMode == Mode.AlternateFunction;
}
private DoubleWordRegisterCollection CreateRegisters()
{
var registersMap = new Dictionary<long, DoubleWordRegister>
{
{(long)Registers.Mode, new DoubleWordRegister(this)
.WithEnumFields<DoubleWordRegister, Mode>(0, 2, NumberOfPins, name: "MODER",
valueProviderCallback: (idx, _) => mode[idx],
writeCallback: (idx, _, val) => ChangeMode(idx, val))
},
{(long)Registers.OutputType, new DoubleWordRegister(this)
.WithTaggedFlag("OT0", 0)
.WithTaggedFlag("OT1", 1)
.WithTaggedFlag("OT2", 2)
.WithTaggedFlag("OT3", 3)
.WithTaggedFlag("OT4", 4)
.WithTaggedFlag("OT5", 5)
.WithTaggedFlag("OT6", 6)
.WithTaggedFlag("OT7", 7)
.WithTaggedFlag("OT8", 8)
.WithTaggedFlag("OT9", 9)
.WithTaggedFlag("OT10", 10)
.WithTaggedFlag("OT11", 11)
.WithTaggedFlag("OT12", 12)
.WithTaggedFlag("OT13", 13)
.WithTaggedFlag("OT14", 14)
.WithTaggedFlag("OT15", 15)
.WithReservedBits(16, 16)
},
{(long)Registers.OutputSpeed, new DoubleWordRegister(this)
.WithEnumFields<DoubleWordRegister, OutputSpeed>(0, 2, NumberOfPins, name: "OSPEEDR",
valueProviderCallback: (idx, _) => outputSpeed[idx],
writeCallback: (idx, _, val) => { outputSpeed[idx] = val; })
},
{(long)Registers.PullUpPullDown, new DoubleWordRegister(this)
.WithEnumFields<DoubleWordRegister, PullUpPullDown>(0, 2, NumberOfPins, name: "PUPDR0",
valueProviderCallback: (idx, _) => pullUpPullDown[idx],
writeCallback: (idx, _, val) => { pullUpPullDown[idx] = val; })
},
{(long)Registers.InputData, new DoubleWordRegister(this)
.WithValueField(0, 16, FieldMode.Read, valueProviderCallback: _ => BitHelper.GetValueFromBitsArray(State), name: "IDR")
.WithReservedBits(16, 16)
},
{(long)Registers.OutputData, new DoubleWordRegister(this)
.WithValueField(0, 16, writeCallback: (_, val) => WriteState((ushort)val), valueProviderCallback: _ => BitHelper.GetValueFromBitsArray(State), name: "ODR")
.WithReservedBits(16, 16)
},
{(long)Registers.BitSet, new DoubleWordRegister(this)
.WithValueField(0, 16, FieldMode.Write,
writeCallback: (_, val) => { if(val != 0) WriteState((ushort)(BitHelper.GetValueFromBitsArray(State) | val)); },
name: "GPIOx_BS")
.WithValueField(16, 16, FieldMode.Write,
writeCallback: (_, val) => { if(val != 0) WriteState((ushort)(BitHelper.GetValueFromBitsArray(State) & ~val)); },
name: "GPIOx_BR")
},
{ (long)Registers.ConfigurationLock, new DoubleWordRegister(this)
.WithTaggedFlag("LCK0", 0)
.WithTaggedFlag("LCK1", 1)
.WithTaggedFlag("LCK2", 2)
.WithTaggedFlag("LCK3", 3)
.WithTaggedFlag("LCK4", 4)
.WithTaggedFlag("LCK5", 5)
.WithTaggedFlag("LCK6", 6)
.WithTaggedFlag("LCK7", 7)
.WithTaggedFlag("LCK8", 8)
.WithTaggedFlag("LCK9", 9)
.WithTaggedFlag("LCK10", 10)
.WithTaggedFlag("LCK11", 11)
.WithTaggedFlag("LCK12", 12)
.WithTaggedFlag("LCK13", 13)
.WithTaggedFlag("LCK14", 14)
.WithTaggedFlag("LCK15", 15)
.WithTaggedFlag("LCKK", 16)
.WithReservedBits(17, 15)
},
{(long)Registers.AlternateFunctionLow, new DoubleWordRegister(this)
.WithValueFields(0, 4, 8, name: "AFSEL_LO",
writeCallback: (i, _, val) => alternateFunctionOutputs[i].ActiveFunction = val,
valueProviderCallback: (i, _) => alternateFunctionOutputs[i].ActiveFunction
)
},
{(long)Registers.AlternateFunctionHigh, new DoubleWordRegister(this)
.WithValueFields(0, 4, 8, name: "AFSEL_HI",
writeCallback: (i, _, val) => alternateFunctionOutputs[i + 8].ActiveFunction = val,
valueProviderCallback: (i, _) => alternateFunctionOutputs[i + 8].ActiveFunction
)
},
{(long)Registers.BitReset, new DoubleWordRegister(this)
.WithValueField(0, 16, FieldMode.Write,
writeCallback: (_, val) => { if(val != 0) WriteState((ushort)(BitHelper.GetValueFromBitsArray(State) & ~val)); },
name: "GPIOx_BRR")
.WithReservedBits(16, 16)
},
};
return new DoubleWordRegisterCollection(this, registersMap);
}
private readonly Mode[] mode;
private readonly OutputSpeed[] outputSpeed;
private readonly PullUpPullDown[] pullUpPullDown;
private readonly uint modeResetValue;
private readonly uint outputSpeedResetValue;
private readonly uint pullUpPullDownResetValue;
private readonly DoubleWordRegisterCollection registers;
private readonly uint numberOfAFs;
// NOTE: This array holds connections from AFs to specific GPIO pins.
// From the PoV of this peripheral they're inputs,
// however they represent the output pins of this peripheral hence the name.
private readonly GPIOAlternateFunction[] alternateFunctionOutputs;
// TODO: AF inputs
private const int NumberOfPins = 16;
private class GPIOAlternateFunction : IGPIOReceiver
{
public GPIOAlternateFunction(STM32_GPIOPort port, int pin)
{
this.port = port;
this.pin = pin;
Reset();
}
public void Reset()
{
IsConnected = false;
ActiveFunction = 0;
}
public void OnGPIO(int number, bool value)
{
if(!CheckAFNumber(number) || !IsConnected || number != activeFunction)
{
// Don't emit any log as it is valid to receive signals from AFs when they are not active.
// All alternate function sources are always connected and always sending signals.
// The GPIO configuration then decides whether those are connected
// to GPIO input/output or are simply ingored.
return;
}
port.WritePin(pin, value);
}
public bool IsConnected { get; set; }
public ulong ActiveFunction
{
get => (ulong)activeFunction;
set
{
var val = (int)value;
if(CheckAFNumber(val))
{
activeFunction = val;
}
}
}
private bool CheckAFNumber(int number)
{
if(number < 0 || number >= port.numberOfAFs)
{
this.Log(LogLevel.Error, "Alternate function number must be between 0 and {0}, but {1} was given instead.", port.numberOfAFs - 1, number);
return false;
}
return true;
}
private readonly STM32_GPIOPort port;
private readonly int pin;
private int activeFunction;
}
private enum Mode
{
Input = 0x0,
Output = 0x1,
AlternateFunction = 0x2,
AnalogMode = 0x3,
}
private enum OutputSpeed
{
// The reference manual defines the 'Low' setting with 2 possible values
Low1 = 0b00,
Low2 = 0b10,
Medium = 0b01,
High = 0b11,
}
private enum PullUpPullDown
{
No = 0b00,
Up = 0b01,
Down = 0b10,
Reserved = 0b11,
}
// Source: Chapter 7.4 in RM0090 Cortex M4 Reference Manual (Doc ID 018909 Rev 4)
// for STM32F40xxx, STM32F41xxx, STM32F42xxx, STM32F43xxx advanced ARM-based 32-bit MCUs
private enum Registers
{
Mode = 0x00, //GPIOx_MODE Mode register
OutputType = 0x04, //GPIOx_OTYPER Output type register
OutputSpeed = 0x08, //GPIOx_OSPEEDR Output speed register
PullUpPullDown = 0x0C, //GPIOx_PUPDR Pull-up/pull-down register
InputData = 0x10, //GPIOx_IDR Input data register
OutputData = 0x14, //GPIOx_ODR Output data register
BitSet = 0x18, //GPIOx_BSRR Bit set/reset register
ConfigurationLock = 0x1C, //GPIOx_LCKR Configuration lock register
AlternateFunctionLow = 0x20, //GPIOx_AFRL Alternate function low register
AlternateFunctionHigh = 0x24, //GPIOx_AFRH Alternate function high register
BitReset = 0x28, //GPIOx_BRR Bit reset register
}
}
}