-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathrp2040.ts
374 lines (338 loc) · 12.8 KB
/
rp2040.ts
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { IClock } from './clock/clock.js';
import { SimulationClock } from './clock/simulation-clock.js';
import { CortexM0Core } from './cortex-m0-core.js';
import { GPIOPin } from './gpio-pin.js';
import { IRQ } from './irq.js';
import { RPADC } from './peripherals/adc.js';
import { RPBUSCTRL } from './peripherals/busctrl.js';
import { RPClocks } from './peripherals/clocks.js';
import { DREQChannel, RPDMA } from './peripherals/dma.js';
import { RPI2C } from './peripherals/i2c.js';
import { RPIO } from './peripherals/io.js';
import { RPPADS } from './peripherals/pads.js';
import { Peripheral, UnimplementedPeripheral } from './peripherals/peripheral.js';
import { RPPIO } from './peripherals/pio.js';
import { RPPPB } from './peripherals/ppb.js';
import { RPPWM } from './peripherals/pwm.js';
import { RPReset } from './peripherals/reset.js';
import { RP2040RTC } from './peripherals/rtc.js';
import { RPSPI } from './peripherals/spi.js';
import { RPSSI } from './peripherals/ssi.js';
import { RP2040SysCfg } from './peripherals/syscfg.js';
import { RP2040SysInfo } from './peripherals/sysinfo.js';
import { RPTBMAN } from './peripherals/tbman.js';
import { RPTimer } from './peripherals/timer.js';
import { RPUART } from './peripherals/uart.js';
import { RPUSBController } from './peripherals/usb.js';
import { RPWatchdog } from './peripherals/watchdog.js';
import { RPSIO } from './sio.js';
import { ConsoleLogger, LogLevel, Logger } from './utils/logging.js';
export const FLASH_START_ADDRESS = 0x10000000;
export const FLASH_END_ADDRESS = 0x14000000;
export const RAM_START_ADDRESS = 0x20000000;
export const APB_START_ADDRESS = 0x40000000;
export const DPRAM_START_ADDRESS = 0x50100000;
export const SIO_START_ADDRESS = 0xd0000000;
const LOG_NAME = 'RP2040';
const KB = 1024;
const MB = 1024 * KB;
const MHz = 1_000_000;
export class RP2040 {
readonly bootrom = new Uint32Array(4 * KB);
readonly sram = new Uint8Array(264 * KB);
readonly sramView = new DataView(this.sram.buffer);
readonly flash = new Uint8Array(16 * MB);
readonly flash16 = new Uint16Array(this.flash.buffer);
readonly flashView = new DataView(this.flash.buffer);
readonly usbDPRAM = new Uint8Array(4 * KB);
readonly usbDPRAMView = new DataView(this.usbDPRAM.buffer);
readonly core = new CortexM0Core(this);
/* Clocks */
clkSys = 125 * MHz;
clkPeri = 125 * MHz;
readonly ppb = new RPPPB(this, 'PPB');
readonly sio = new RPSIO(this);
readonly uart = [
new RPUART(this, 'UART0', IRQ.UART0, {
rx: DREQChannel.DREQ_UART0_RX,
tx: DREQChannel.DREQ_UART0_TX,
}),
new RPUART(this, 'UART1', IRQ.UART1, {
rx: DREQChannel.DREQ_UART1_RX,
tx: DREQChannel.DREQ_UART1_TX,
}),
];
readonly i2c = [new RPI2C(this, 'I2C0', IRQ.I2C0), new RPI2C(this, 'I2C1', IRQ.I2C1)];
readonly pwm = new RPPWM(this, 'PWM_BASE');
readonly adc = new RPADC(this, 'ADC');
readonly gpio = [
new GPIOPin(this, 0),
new GPIOPin(this, 1),
new GPIOPin(this, 2),
new GPIOPin(this, 3),
new GPIOPin(this, 4),
new GPIOPin(this, 5),
new GPIOPin(this, 6),
new GPIOPin(this, 7),
new GPIOPin(this, 8),
new GPIOPin(this, 9),
new GPIOPin(this, 10),
new GPIOPin(this, 11),
new GPIOPin(this, 12),
new GPIOPin(this, 13),
new GPIOPin(this, 14),
new GPIOPin(this, 15),
new GPIOPin(this, 16),
new GPIOPin(this, 17),
new GPIOPin(this, 18),
new GPIOPin(this, 19),
new GPIOPin(this, 20),
new GPIOPin(this, 21),
new GPIOPin(this, 22),
new GPIOPin(this, 23),
new GPIOPin(this, 24),
new GPIOPin(this, 25),
new GPIOPin(this, 26),
new GPIOPin(this, 27),
new GPIOPin(this, 28),
new GPIOPin(this, 29),
];
readonly qspi = [
new GPIOPin(this, 0, 'SCLK'),
new GPIOPin(this, 1, 'SS'),
new GPIOPin(this, 2, 'SD0'),
new GPIOPin(this, 3, 'SD1'),
new GPIOPin(this, 4, 'SD2'),
new GPIOPin(this, 5, 'SD3'),
];
readonly dma = new RPDMA(this, 'DMA');
readonly pio = [
new RPPIO(this, 'PIO0', IRQ.PIO0_IRQ0, 0),
new RPPIO(this, 'PIO1', IRQ.PIO1_IRQ0, 1),
];
readonly usbCtrl = new RPUSBController(this, 'USB');
readonly spi = [
new RPSPI(this, 'SPI0', IRQ.SPI0, {
rx: DREQChannel.DREQ_SPI0_RX,
tx: DREQChannel.DREQ_SPI0_TX,
}),
new RPSPI(this, 'SPI1', IRQ.SPI1, {
rx: DREQChannel.DREQ_SPI1_RX,
tx: DREQChannel.DREQ_SPI1_TX,
}),
];
public logger: Logger = new ConsoleLogger(LogLevel.Debug, true);
readonly peripherals: { [index: number]: Peripheral } = {
0x18000: new RPSSI(this, 'SSI'),
0x40000: new RP2040SysInfo(this, 'SYSINFO_BASE'),
0x40004: new RP2040SysCfg(this, 'SYSCFG'),
0x40008: new RPClocks(this, 'CLOCKS_BASE'),
0x4000c: new RPReset(this, 'RESETS_BASE'),
0x40010: new UnimplementedPeripheral(this, 'PSM_BASE'),
0x40014: new RPIO(this, 'IO_BANK0_BASE'),
0x40018: new UnimplementedPeripheral(this, 'IO_QSPI_BASE'),
0x4001c: new RPPADS(this, 'PADS_BANK0_BASE', 'bank0'),
0x40020: new RPPADS(this, 'PADS_QSPI_BASE', 'qspi'),
0x40024: new UnimplementedPeripheral(this, 'XOSC_BASE'),
0x40028: new UnimplementedPeripheral(this, 'PLL_SYS_BASE'),
0x4002c: new UnimplementedPeripheral(this, 'PLL_USB_BASE'),
0x40030: new RPBUSCTRL(this, 'BUSCTRL_BASE'),
0x40034: this.uart[0],
0x40038: this.uart[1],
0x4003c: this.spi[0],
0x40040: this.spi[1],
0x40044: this.i2c[0],
0x40048: this.i2c[1],
0x4004c: this.adc,
0x40050: this.pwm,
0x40054: new RPTimer(this, 'TIMER_BASE'),
0x40058: new RPWatchdog(this, 'WATCHDOG_BASE'),
0x4005c: new RP2040RTC(this, 'RTC_BASE'),
0x40060: new UnimplementedPeripheral(this, 'ROSC_BASE'),
0x40064: new UnimplementedPeripheral(this, 'VREG_AND_CHIP_RESET_BASE'),
0x4006c: new RPTBMAN(this, 'TBMAN_BASE'),
0x50000: this.dma,
0x50110: this.usbCtrl,
0x50200: this.pio[0],
0x50300: this.pio[1],
};
// Debugging
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public onBreak = (code: number) => {
// TODO: raise HardFault exception
// console.error('Breakpoint!', code);
};
constructor(readonly clock: IClock = new SimulationClock()) {
this.reset();
}
loadBootrom(bootromData: Uint32Array) {
this.bootrom.set(bootromData);
this.reset();
}
reset() {
this.core.reset();
this.pwm.reset();
this.flash.fill(0xff);
}
readUint32(address: number) {
address = address >>> 0; // round to 32-bits, unsigned
if (address & 0x3) {
this.logger.error(
LOG_NAME,
`read from address ${address.toString(16)}, which is not 32 bit aligned`,
);
}
const { bootrom } = this;
if (address < bootrom.length * 4) {
return bootrom[address / 4];
} else if (address >= FLASH_START_ADDRESS && address < FLASH_END_ADDRESS) {
// Flash is mirrored four times:
// - 0x10000000 XIP
// - 0x11000000 XIP_NOALLOC
// - 0x12000000 XIP_NOCACHE
// - 0x13000000 XIP_NOCACHE_NOALLOC
const offset = address & 0x00ff_ffff;
return this.flashView.getUint32(offset, true);
} else if (address >= RAM_START_ADDRESS && address < RAM_START_ADDRESS + this.sram.length) {
return this.sramView.getUint32(address - RAM_START_ADDRESS, true);
} else if (
address >= DPRAM_START_ADDRESS &&
address < DPRAM_START_ADDRESS + this.usbDPRAM.length
) {
return this.usbDPRAMView.getUint32(address - DPRAM_START_ADDRESS, true);
} else if (address >>> 12 === 0xe000e) {
return this.ppb.readUint32(address & 0xfff);
} else if (address >= SIO_START_ADDRESS && address < SIO_START_ADDRESS + 0x10000000) {
return this.sio.readUint32(address - SIO_START_ADDRESS);
}
const peripheral = this.findPeripheral(address);
if (peripheral) {
return peripheral.readUint32(address & 0x3fff);
}
this.logger.warn(LOG_NAME, `Read from invalid memory address: ${address.toString(16)}`);
return 0xffffffff;
}
findPeripheral(address: number) {
return this.peripherals[(address >>> 14) << 2];
}
/** We assume the address is 16-bit aligned */
readUint16(address: number) {
if (address >= FLASH_START_ADDRESS && address < FLASH_START_ADDRESS + this.flash.length) {
return this.flashView.getUint16(address - FLASH_START_ADDRESS, true);
} else if (address >= RAM_START_ADDRESS && address < RAM_START_ADDRESS + this.sram.length) {
return this.sramView.getUint16(address - RAM_START_ADDRESS, true);
}
const value = this.readUint32(address & 0xfffffffc);
return address & 0x2 ? (value & 0xffff0000) >>> 16 : value & 0xffff;
}
readUint8(address: number) {
if (address >= FLASH_START_ADDRESS && address < FLASH_START_ADDRESS + this.flash.length) {
return this.flash[address - FLASH_START_ADDRESS];
} else if (address >= RAM_START_ADDRESS && address < RAM_START_ADDRESS + this.sram.length) {
return this.sram[address - RAM_START_ADDRESS];
}
const value = this.readUint16(address & 0xfffffffe);
return (address & 0x1 ? (value & 0xff00) >>> 8 : value & 0xff) >>> 0;
}
writeUint32(address: number, value: number) {
address = address >>> 0;
const { bootrom } = this;
const peripheral = this.findPeripheral(address);
if (peripheral) {
const atomicType = (address & 0x3000) >> 12;
const offset = address & 0xfff;
peripheral.writeUint32Atomic(offset, value, atomicType);
} else if (address < bootrom.length * 4) {
bootrom[address / 4] = value;
} else if (
address >= FLASH_START_ADDRESS &&
address < FLASH_START_ADDRESS + this.flash.length
) {
this.flashView.setUint32(address - FLASH_START_ADDRESS, value, true);
} else if (address >= RAM_START_ADDRESS && address < RAM_START_ADDRESS + this.sram.length) {
this.sramView.setUint32(address - RAM_START_ADDRESS, value, true);
} else if (
address >= DPRAM_START_ADDRESS &&
address < DPRAM_START_ADDRESS + this.usbDPRAM.length
) {
const offset = address - DPRAM_START_ADDRESS;
this.usbDPRAMView.setUint32(offset, value, true);
this.usbCtrl.DPRAMUpdated(offset, value);
} else if (address >= SIO_START_ADDRESS && address < SIO_START_ADDRESS + 0x10000000) {
this.sio.writeUint32(address - SIO_START_ADDRESS, value);
} else if (address >>> 12 === 0xe000e) {
this.ppb.writeUint32(address & 0xfff, value);
} else {
this.logger.warn(LOG_NAME, `Write to undefined address: ${address.toString(16)}`);
}
}
writeUint8(address: number, value: number) {
if (address >= RAM_START_ADDRESS && address < RAM_START_ADDRESS + this.sram.length) {
this.sram[address - RAM_START_ADDRESS] = value;
return;
}
const alignedAddress = (address & 0xfffffffc) >>> 0;
const offset = address & 0x3;
const peripheral = this.findPeripheral(address);
if (peripheral) {
const atomicType = (alignedAddress & 0x3000) >> 12;
const offset = alignedAddress & 0xfff;
peripheral.writeUint32Atomic(
offset,
(value & 0xff) | ((value & 0xff) << 8) | ((value & 0xff) << 16) | ((value & 0xff) << 24),
atomicType,
);
return;
}
const originalValue = this.readUint32(alignedAddress);
const newValue = new Uint32Array([originalValue]);
new DataView(newValue.buffer).setUint8(offset, value);
this.writeUint32(alignedAddress, newValue[0]);
}
writeUint16(address: number, value: number) {
// we assume that addess is 16-bit aligned.
// Ideally we should generate a fault if not!
if (address >= RAM_START_ADDRESS && address < RAM_START_ADDRESS + this.sram.length) {
this.sramView.setUint16(address - RAM_START_ADDRESS, value, true);
return;
}
const alignedAddress = (address & 0xfffffffc) >>> 0;
const offset = address & 0x3;
const peripheral = this.findPeripheral(address);
if (peripheral) {
const atomicType = (alignedAddress & 0x3000) >> 12;
const offset = alignedAddress & 0xfff;
peripheral.writeUint32Atomic(offset, (value & 0xffff) | ((value & 0xffff) << 16), atomicType);
return;
}
const originalValue = this.readUint32(alignedAddress);
const newValue = new Uint32Array([originalValue]);
new DataView(newValue.buffer).setUint16(offset, value, true);
this.writeUint32(alignedAddress, newValue[0]);
}
get gpioValues() {
const { gpio } = this;
let result = 0;
for (let gpioIndex = 0; gpioIndex < gpio.length; gpioIndex++) {
if (gpio[gpioIndex].inputValue) {
result |= 1 << gpioIndex;
}
}
return result;
}
setInterrupt(irq: number, value: boolean) {
this.core.setInterrupt(irq, value);
}
updateIOInterrupt() {
let interruptValue = false;
for (const pin of this.gpio) {
if (pin.irqValue) {
interruptValue = true;
}
}
this.setInterrupt(IRQ.IO_BANK0, interruptValue);
}
step() {
this.core.executeInstruction();
}
}