Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report UART status as events; addl UART events #129

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions demo/emulator-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ loadHex(hex, mcu.flash, 0x10000000);
const gdbServer = new GDBTCPServer(mcu, 3333);
console.log(`RP2040 GDB Server ready! Listening on port ${gdbServer.port}`);

mcu.uart[0].onByte = (value) => {
mcu.uart[0].on('byteSent', (value) => {
process.stdout.write(new Uint8Array([value]));
};
});

mcu.core.PC = 0x10000000;
mcu.execute();
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
"**/*.js": [
"prettier --write"
]
},
"dependencies": {
"eventemitter3": "^5.0.1"
}
}
10 changes: 8 additions & 2 deletions src/peripherals/peripheral.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EventEmitter from 'eventemitter3';
import { RP2040 } from '../rp2040';

const ATOMIC_NORMAL = 0;
Expand Down Expand Up @@ -25,10 +26,15 @@ export interface Peripheral {
writeUint32Atomic(offset: number, value: number, atomicType: number): void;
}

export class BasePeripheral implements Peripheral {
// eslint-disable-next-line @typescript-eslint/ban-types -- EventEmitter3 defines the allowed types as `object`, so copy that here
export class BasePeripheral<E extends object = Record<string, unknown>>
extends EventEmitter<E>
implements Peripheral {
protected rawWriteValue = 0;

constructor(protected rp2040: RP2040, readonly name: string) {}
constructor(protected rp2040: RP2040, readonly name: string) {
super();
}

readUint32(offset: number) {
this.warn(`Unimplemented peripheral read from ${offset.toString(16)}`);
Expand Down
28 changes: 25 additions & 3 deletions src/peripherals/uart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,25 @@ const UARTEN = 1 << 0;
// Interrupt bits
const UARTRXINTR = 1 << 4;

export class RPUART extends BasePeripheral implements Peripheral {
type UartEvents = {
/** the MCU has written a byte to the UART */
byteSent: (value: number) => void;
/** readFhe MCU's read FIFO is full, and any further bytes fed to the UART will be dropped */
readFifoFull: () => void;
/** the MCU's read FIFO is empty */
readFifoEmpty: () => void;
/** the MCU has read a byte from the UART */
byteConsumed: () => void;
};

export class RPUART extends BasePeripheral<UartEvents> implements Peripheral {
private ctrlRegister = RXE | TXE;
private lineCtrlRegister = 0;
private rxFIFO = new FIFO(32);
private interruptMask = 0;
private interruptStatus = 0;

/** @deprecated prefer `on("byteSent", callback)` */
public onByte?: (value: number) => void;

constructor(rp2040: RP2040, name: string, readonly irq: number) {
Expand Down Expand Up @@ -95,6 +107,13 @@ export class RPUART extends BasePeripheral implements Peripheral {
this.interruptStatus |= UARTRXINTR;
this.checkInterrupts();
}
if (this.rxFIFO.empty) {
this.emit('readFifoEmpty');
}
if (this.rxFIFO.full) {
this.emit('readFifoFull');
}
this.emit('byteConsumed');
return value;
}
case UARTFR:
Expand All @@ -115,9 +134,12 @@ export class RPUART extends BasePeripheral implements Peripheral {

writeUint32(offset: number, value: number) {
switch (offset) {
case UARTDR:
this.onByte?.(value & 0xff);
case UARTDR: {
const byte = value & 0xff;
this.onByte?.(byte);
this.emit('byteSent', byte);
break;
}

case UARTLCR_H:
this.lineCtrlRegister = value;
Expand Down