Repository navigation
Expand file tree
/
Copy pathdrivers.py
More file actions
240 lines (212 loc) · 6.32 KB
/
Copy pathdrivers.py
File metadata and controls
240 lines (212 loc) · 6.32 KB
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
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
from dataclasses import dataclass
from typing import Callable
@dataclass
class PlatformIO:
# PlatformIO board identifier
board: str
# Linker script used for the board
ldscript: str
# PlatformIO framework identifier
framework: str
# PlatformIO platform identifier
platform: str
@dataclass
class TinyUSB:
# TinyUSB MCU identifier
mcu: str
@dataclass
class Bootloader:
# Flash address of the bootloader
address: int
# Magic value to enter bootloader on firmware startup
magic: int
@dataclass
class NonUniformSectors:
# Size of each flash sector
sizes: list[int]
@dataclass
class UniformSectors:
# Common sector size
size: int
# Number of flash sectors
num_sectors: int
@dataclass
class Flash:
sector_sizes: NonUniformSectors | UniformSectors
# Value of an empty word in the flash
empty_value: int
# Get the number of flash sectors
def get_num_sectors(self):
match self.sector_sizes:
case NonUniformSectors(sizes):
return len(sizes)
case UniformSectors(_, num_sectors):
return num_sectors
# Get the total size of the flash
def get_flash_size(self):
match self.sector_sizes:
case NonUniformSectors(sizes):
return sum(sizes)
case UniformSectors(size, num_sectors):
return size * num_sectors
# Get the size of a flash sector
def get_sector_size(self, sector: int):
match self.sector_sizes:
case NonUniformSectors(sizes):
assert sector < len(sizes)
return sizes[sector]
case UniformSectors(size, num_sectors):
assert sector < num_sectors
return size
# Round up the required size to the minimum number of flash sectors (from the end)
def round_up_to_flash_sectors(self, required_size: int):
assert required_size <= self.get_flash_size()
total = 0
for i in range(self.get_num_sectors() - 1, -1, -1):
total += self.get_sector_size(i)
if total >= required_size:
return total
return total
@dataclass
class ADC:
# Maxmimum ADC resolution supported by the MCU
max_resolution: int
# ADC input pin names, in the same order as the ADC channels
input_pins: list[str]
# Convert a list of ADC input pin names to a tuple of (GPIO port names, GPIO pin numbers)
to_gpio_array: Callable[[list[str]], tuple[list[str], list[str]]]
# Convert a list of ADC input channels or GPIO pin names to a list of ADC input channels
def to_adc_inputs(self, channels_or_pins: list[str | int]):
channels: list[int] = []
for x in channels_or_pins:
match x:
case str():
channels.append(self.input_pins.index(x))
case int():
assert x < len(self.input_pins)
channels.append(x)
return channels
@dataclass
class Metadata:
bootloader: Bootloader
flash: Flash
adc: ADC
@dataclass
class Driver:
platformio: PlatformIO
tinyusb: TinyUSB
metadata: Metadata
STM32F446XX = Driver(
platformio=PlatformIO(
board="genericSTM32F446RE",
ldscript="stm32f446retx.ld",
framework="stm32cube",
platform="ststm32",
),
tinyusb=TinyUSB(mcu="stm32f4"),
metadata=Metadata(
bootloader=Bootloader(
address=0x1FFF0000,
magic=0xDEADBEEF,
),
flash=Flash(
sector_sizes=NonUniformSectors(
sizes=[
16 * 1024,
16 * 1024,
16 * 1024,
16 * 1024,
64 * 1024,
128 * 1024,
128 * 1024,
128 * 1024,
]
),
empty_value=0xFFFFFFFF,
),
adc=ADC(
max_resolution=12,
input_pins=[
"A0",
"A1",
"A2",
"A3",
"A4",
"A5",
"A6",
"A7",
"B0",
"B1",
"C0",
"C1",
"C2",
"C3",
"C4",
"C5",
],
to_gpio_array=lambda pins: (
[f"GPIO{pin[0]}" for pin in pins],
[f"GPIO_PIN_{pin[1:]}" for pin in pins],
),
),
),
)
AT32F405XX = Driver(
platformio=PlatformIO(
board="genericAT32F405RCT7",
ldscript="at32f405xc.ld",
framework="at32firmlib",
platform="https://github.com/ArteryTek/platform-arterytekat32.git",
),
tinyusb=TinyUSB(mcu="at32f402_405"),
metadata=Metadata(
bootloader=Bootloader(
address=0x1FFFA400,
magic=0xDEADBEEF,
),
flash=Flash(
sector_sizes=UniformSectors(
size=2048,
num_sectors=128,
),
empty_value=0xFFFFFFFF,
),
adc=ADC(
max_resolution=12,
input_pins=[
"A0",
"A1",
"A2",
"A3",
"A4",
"A5",
"A6",
"A7",
"B0",
"B1",
"C0",
"C1",
"C2",
"C3",
"C4",
"C5",
],
to_gpio_array=lambda pins: (
[f"GPIO{pin[0]}" for pin in pins],
[f"GPIO_PINS_{pin[1:]}" for pin in pins],
),
),
),
)