V4.6.1 UART End to End

MediumImplement25 minap_soc v1.5 · firmware · serverReview the topic

Send bytes out of the UART pad for the bench to check. Take its answers back on the receive pad and check them in C. Two judges, one test: the bench judges what left the chip, your firmware judges what came in.

Hardware context: UART0 of ap_soc v1.5 end to end: clock gate, pin mux, pads IO0 and IO1, the bench's UART model. A UART test with two judges.

Write the first UART test of the chip, end to end. TB_ARG holds k (bits 15 to 8) and s (bits 7 to 0). Send the k bytes s, s + 1, ..., s + k - 1, each modulo 256, out of the UART transmit pad IO0. Use 8 clk_sys cycles a bit.

Before the first byte, write 0xE2E to TB_EVENT. The bench then checks each byte on the pad as it arrives. It sends each one back, inverted, on the receive pad IO1. Check every byte that comes back: it must be the inverse of the byte you sent. If all k are right, print uart ok and k as eight hex digits, and return 0.

If a byte comes back wrong, end with fail code 1. If a byte does not come back within 4,000 polls of STATUS, end with fail code 2.

Full specification
RegisterAddressUse
PINMUX0x1000_4010Two bits a pad; value 2 gives IO0 UART TX and IO1 UART RX
UART0 TXDATA0x1000_0000The next byte to send
UART0 STATUS0x1000_0004Bit 0 ROOM, bit 1 RX_READY
UART0 RXDATA0x1000_000CThe oldest received byte
UART0 DIV0x1000_0010clk_sys cycles a bit; resets to 16

The bench's UART runs at 8 cycles a bit. A frame it cannot read, or a byte that never reaches the pad, is a bench error. The run then fails with the bench's own line.

Given Files

Read-only files supplied by the bench.

drivers/uart.h
#ifndef AP_UART_H
#define AP_UART_H
#include "hal.h"

static inline void uart_flush(void)
{
    while (!FIELD_GET(UART0_STATUS_ROOM, reg_read32(UART0_STATUS))) { }
}

static inline void uart_putc(unsigned char byte)
{
    uart_flush();
    reg_write32(UART0_TXDATA, byte);
}

static inline unsigned char uart_getc(void)
{
    while (!FIELD_GET(UART0_STATUS_RX_READY, reg_read32(UART0_STATUS))) { }
    return reg_read32(UART0_RXDATA);
}

static inline void uart_enable_rx_irq(void)
{
    reg_write32(UART0_IRQ_ENABLE, UART0_IRQ_ENABLE_RX_READY_MASK);
}
#endif

tb.h
#ifndef AP_TB_H
#define AP_TB_H
_Noreturn void tb_pass(void);
_Noreturn void tb_fail(unsigned code);
void tb_putc(char byte);
void tb_puts(const char *text);
void tb_put_hex(unsigned value);
void tb_event(unsigned id);
/* Slot zero is the single bench-to-firmware argument word. */
unsigned tb_arg(unsigned n);
void tb_wait_event(unsigned id);
#endif

hal.h
#ifndef AP_HAL_H
#define AP_HAL_H
#include "ap_soc_regs.h"

/* V13.4 supplies these two functions through DPI in a host-native build. */
#ifdef AP_HOST_DPI
unsigned reg_read32(unsigned address);
void reg_write32(unsigned address, unsigned value);
#else
static inline unsigned reg_read32(unsigned address)
{
    return *(volatile unsigned *)address;
}
static inline void reg_write32(unsigned address, unsigned value)
{
    *(volatile unsigned *)address = value;
}
#endif

#define FIELD_GET(field, word) (((word) & field##_MASK) >> field##_SHIFT)
#define FIELD_PREP(field, value) (((unsigned)(value) << field##_SHIFT) & field##_MASK)
#endif

UART0 · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000TXDATA7:0datawonot storedThe low byte is the next byte to send. Byte to send.
0x004STATUS0roomroblockTransmit room and receive queue state. The transmitter can take a byte.
0x004STATUS1rx_readyroblockAt least one received byte waits.
0x004STATUS2rx_fullroblockThe receive queue is full.
0x008IRQ_ENABLE0roomrw0x0regsWhich status bits raise interrupt source 1. ROOM raises the request.
0x008IRQ_ENABLE1rx_readyrw0x0regsRX_READY raises the request (new in v1.5).
0x00CRXDATA7:0dataroblockThe oldest received byte. The read removes it from the queue. Received byte.
0x010DIV15:0cyclesrw0x0010regsBit time in clk_sys cycles (new in v1.5). clk_sys cycles a bit.
ap_sysctl · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000CHIP_ID31:0valuero0x2A5310C5constThe chip's identity: the IDCODE of the TAP. Fixed number.
0x004RESET_CAUSE0power_onroblockSticky record of reset sources since power-on. Power-on reset.
0x004RESET_CAUSE1pinroblockReset pin.
0x004RESET_CAUSE2watchdogroblockWatchdog.
0x004RESET_CAUSE3softwareroblockSoftware request.
0x004RESET_CAUSE4debugroblockDebug request.
0x008SOFT_RESET0timerwonot storedReset requests from software. 1 arms one restart of the timer.
0x008SOFT_RESET1chipwonot stored1 requests the software reset (new in v1.5).
0x00CSTRAPS0bootroblockStraps as sampled at power-up (new in v1.5). 0: start in the boot ROM. 1: start in SRAM.
0x010PINMUX31:0selectrw0x00000000regsTwo bits for each of sixteen pads: 0 released, 1 GPIO, 2 serial, 3 auxiliary. Pad n uses bits 2n+1 and 2n.
0x014CLK_GATE0uart0rw0x1regs1 lets that block's clock run (new in v1.5). UART0.
0x014CLK_GATE1gpiorw0x1regsGPIO.
0x014CLK_GATE2timerrw0x1regsTimer and PWM.
0x014CLK_GATE3spirw0x1regsSPI.
0x014CLK_GATE4dmarw0x1regsDMA.
0x018LOCK0pinmuxw1s0x0regsWrite-once locks. Only a reset clears one (new in v1.5). 1: PINMUX ignores writes.
0x018LOCK1clk_gatew1s0x0regs1: CLK_GATE ignores writes.
0x018LOCK2debugw1s0x0regs1: the debug lock is closed.
0x020TB_STATUS31:0valuerw0x00000000regs (power-on reset only)Scratch word 0. The firmware kit reports progress, pass and fail here. Plain storage.
0x024TB_PUTC31:0valuerw0x00000000regs (power-on reset only)Scratch word 1. The firmware kit writes one character here. Plain storage.
0x028TB_EVENT31:0valuerw0x00000000regs (power-on reset only)Scratch word 2. The firmware kit writes an event number here. Plain storage.
0x02CTB_ARG31:0valuerw0x00000000regs (power-on reset only)Scratch word 3. A bench leaves a word for the firmware here. Plain storage.

Examples

Example 1

Four Bytes
Input
4 bytes from 0x41
Output
pad: 41 42 43 44; console: "uart ok 00000004"

The bench sends back BE BD BC BB, the inverses.

Example 2

One Zero Byte
Input
1 byte, 0x00
Output
pad: 00; console: "uart ok 00000001"

The inverse of 0x00 is 0xFF.

Example 3

Sixteen Bytes
Input
16 bytes from 0xF8, wrapping at 0xFF
Output
pad: F8 ... FF 00 ... 07

The bytes wrap at 0xFF: (s + i) modulo 256.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • k is from 1 to 16.
  • The bench compares the bytes on the pad, the status word and, on a pass, the console.

Hints

Hint 1

Which two pads must the pin mux give to the UART?

Hint 2

What does uart_getc do when the bench sends one byte too few?

Hint 3

Inverting a byte: ~b, kept to 8 bits.

What the grader checks: Each case runs one exchange. The bench checks every transmitted byte on the pad, sends chosen answers, and compares the status word and the console. It cannot see the bytes your firmware received, only what it reported.

8 checks on ap_soc v1.5 · firmware · server. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain your text only; the chip, the bench and the hidden checks stay on the server. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 problems on this path

Report a problem with this page

Your code CSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Four Bytes

Input
4 bytes from 0x41
Expected output
pad: 41 42 43 44; console: "uart ok 00000004"

The bench sends back BE BD BC BB, the inverses.

Run samples executes all 3 public checks. Check all tests runs 8 checks on the server.

ap_soc v1.5 · firmware · server