diff options
| author | Uri Shaked | 2021-02-19 23:43:34 +0200 |
|---|---|---|
| committer | Uri Shaked | 2021-02-19 23:43:34 +0200 |
| commit | cf13e06ace0a132cc370e80a7dc87ea3f311bfb2 (patch) | |
| tree | 60f6460b04bee54b5c08bcf18b88e2acf4a24fa4 /src/peripherals/usart.spec.ts | |
| parent | feat(demo): web worker support (diff) | |
| download | avr8js-cf13e06ace0a132cc370e80a7dc87ea3f311bfb2.tar.gz avr8js-cf13e06ace0a132cc370e80a7dc87ea3f311bfb2.tar.bz2 avr8js-cf13e06ace0a132cc370e80a7dc87ea3f311bfb2.zip | |
feat(usart): implement RX #11
close #11
Diffstat (limited to '')
| -rw-r--r-- | src/peripherals/usart.spec.ts | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/src/peripherals/usart.spec.ts b/src/peripherals/usart.spec.ts index fb56967..f92ea00 100644 --- a/src/peripherals/usart.spec.ts +++ b/src/peripherals/usart.spec.ts @@ -18,8 +18,10 @@ const UDR0 = 0xc6; // Register bit names const U2X0 = 2; const TXEN = 8; +const RXEN = 16; const UDRIE = 0x20; const TXCIE = 0x40; +const RXC = 0x80; const TXC = 0x40; const UDRE = 0x20; const USBS = 0x08; @@ -235,7 +237,20 @@ describe('USART', () => { }); }); - describe('integration', () => { + describe('writeByte', () => { + it('should return false if called when RX is busy', () => { + const cpu = new CPU(new Uint16Array(1024)); + const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ); + cpu.writeData(UCSR0B, RXEN); + cpu.writeData(UBRR0L, 103); // baud: 9600 + expect(usart.writeByte(10)).toEqual(true); + expect(usart.writeByte(10)).toEqual(false); + cpu.tick(); + expect(usart.writeByte(10)).toEqual(false); + }); + }); + + describe('Integration tests', () => { it('should set the TXC bit after ~1.04mS when baud rate set to 9600', () => { const cpu = new CPU(new Uint16Array(1024)); new AVRUSART(cpu, usart0Config, FREQ_16MHZ); @@ -249,5 +264,27 @@ describe('USART', () => { cpu.tick(); expect(cpu.data[UCSR0A] & TXC).toEqual(TXC); }); + + it('should be ready to recieve the next byte after ~1.04ms when baudrate set to 9600', () => { + const cpu = new CPU(new Uint16Array(1024)); + const usart = new AVRUSART(cpu, usart0Config, FREQ_16MHZ); + const rxCompleteCallback = jest.fn(); + usart.onRxComplete = rxCompleteCallback; + cpu.writeData(UCSR0B, RXEN); + cpu.writeData(UBRR0L, 103); // baud: 9600 + expect(usart.writeByte(0x42)).toBe(true); + cpu.cycles += 16000; // 1ms + cpu.tick(); + expect(cpu.data[UCSR0A] & RXC).toEqual(0); // byte not received yet + expect(usart.rxBusy).toBe(true); + expect(rxCompleteCallback).not.toHaveBeenCalled(); + cpu.cycles += 800; // 0.05ms + cpu.tick(); + expect(cpu.data[UCSR0A] & RXC).toEqual(RXC); + expect(usart.rxBusy).toBe(false); + expect(rxCompleteCallback).toHaveBeenCalled(); + expect(cpu.readData(UDR0)).toEqual(0x42); + expect(cpu.readData(UDR0)).toEqual(0); + }); }); }); |
