Skip to content

Commit

Permalink
feat(uart): baud rate calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
urish committed Oct 2, 2023
1 parent 4edfa0c commit 1408874
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/peripherals/uart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { describe, expect, it } from 'vitest';
import { RP2040 } from '../rp2040.js';
import { RPUART } from './uart.js';

const UARTIBRD = 0x24;
const UARTFBRD = 0x28;
const OFFSET_UARTLCR_H = 0x2c;

describe('UART', () => {
Expand All @@ -11,4 +13,12 @@ describe('UART', () => {
uart.writeUint32(OFFSET_UARTLCR_H, 0x70);
expect(uart.wordLength).toEqual(8);
});

it('should correctly calculate the baud rate based on UARTIBRD, UARTFBRD values', () => {
const rp2040 = new RP2040();
const uart = new RPUART(rp2040, 'UART', 0, { rx: 0, tx: 0 });
uart.writeUint32(UARTIBRD, 67); // Values taken from example in section 4.2.7.1. of the datasheet
uart.writeUint32(UARTFBRD, 52);
expect(uart.baudRate).toEqual(115207);
});
});
27 changes: 27 additions & 0 deletions src/peripherals/uart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { BasePeripheral, Peripheral } from './peripheral.js';

const UARTDR = 0x0;
const UARTFR = 0x18;
const UARTIBRD = 0x24;
const UARTFBRD = 0x28;
const UARTLCR_H = 0x2c;
const UARTCR = 0x30;
const UARTIMSC = 0x38;
Expand Down Expand Up @@ -39,8 +41,11 @@ export class RPUART extends BasePeripheral implements Peripheral {
private rxFIFO = new FIFO(32);
private interruptMask = 0;
private interruptStatus = 0;
private intDivisor = 0;
private fracDivisor = 0;

public onByte?: (value: number) => void;
public onBaudRateChange?: (baudRate: number) => void;

constructor(
rp2040: RP2040,
Expand Down Expand Up @@ -83,6 +88,14 @@ export class RPUART extends BasePeripheral implements Peripheral {
}
}

get baudDivider() {
return this.intDivisor + this.fracDivisor / 64;
}

get baudRate() {
return Math.round(this.rp2040.clkPeri / (this.baudDivider * 16));
}

get flags() {
return (this.rxFIFO.full ? RXFF : 0) | (this.rxFIFO.empty ? RXFE : 0) | TXFE;
}
Expand Down Expand Up @@ -110,6 +123,10 @@ export class RPUART extends BasePeripheral implements Peripheral {
}
case UARTFR:
return this.flags;
case UARTIBRD:
return this.intDivisor;
case UARTFBRD:
return this.fracDivisor;
case UARTLCR_H:
return this.lineCtrlRegister;
case UARTCR:
Expand All @@ -130,6 +147,16 @@ export class RPUART extends BasePeripheral implements Peripheral {
this.onByte?.(value & 0xff);
break;

case UARTIBRD:
this.intDivisor = value & 0xffff;
this.onBaudRateChange?.(this.baudRate);
break;

case UARTFBRD:
this.fracDivisor = value & 0x3f;
this.onBaudRateChange?.(this.baudRate);
break;

case UARTLCR_H:
this.lineCtrlRegister = value;
break;
Expand Down

0 comments on commit 1408874

Please sign in to comment.