V4.7.1 An Edge, a Level and a Handler That Keeps Up

MediumImplement30 minap_soc v1.5 · firmware · serverReview the topic

Two GPIO pins share one interrupt. One is a pulse line, counted edge by edge; the other is an alarm line that stays high until served. Write the handler that counts both, exactly, without drowning in a level that will not go away.

Hardware context: Interrupts of ap_soc v1.5 from each device through ap_plic and the core's wires to a C handler. An edge and a level on one shared source.

GPIO pin 8 carries pulses: count every rising edge. GPIO pin 9 is an alarm line: the board raises it and holds it high until it decides to lower it. Count each high period once. Both pins ask through the GPIO block, which is ap_plic source 2, so one handler serves both.

Write 0x7A00 to TB_EVENT when you are ready. The board then pulses and raises lines, and finally puts 0x7AFF into TB_ARG. Pulses come at least 400 cycles apart, and the alarm stays high for at least 300 cycles each time. Print edges, the count, alarms , the count, each as eight hex digits, and a new line. In some runs the alarm is already high before your event, and it must be counted too.

Full specification
GPIO registerOffsetUse
IN0x04The synchronised pin levels
IRQ_ENABLE0x18A pin may ask only while its bit is 1
LEVEL_MODE0x1C1: the pin asks while it is active; 0: it asks on an edge
ACTIVE_HIGH0x201: active high, rising edge
STATUS0x24Latched requests; write 1 to clear
MASKED_STATUS0x28STATUS and IRQ_ENABLE: which pins ask now

A level request cannot be cleared while its pin is still active: writing STATUS does not quieten it. Each interrupt the core takes is counted by the bench (ext_gpio).

Given Files

Read-only files supplied by the bench.

drivers/gpio.h
#ifndef AP_GPIO_H
#define AP_GPIO_H
#include "hal.h"

static inline void gpio_drive(unsigned outputs, unsigned value)
{
    reg_write32(GPIO_OUT, value);
    reg_write32(GPIO_DIR, outputs);
}

static inline unsigned gpio_read(void)
{
    return reg_read32(GPIO_IN);
}

/* Select the rising-edge inputs for this test and discard stale edges. */
static inline void gpio_rising_irq(unsigned pins)
{
    reg_write32(GPIO_LEVEL_MODE, 0);
    reg_write32(GPIO_ACTIVE_HIGH, pins);
    reg_write32(GPIO_STATUS, pins);
    reg_write32(GPIO_IRQ_ENABLE, pins);
}

static inline void gpio_ack(unsigned pins)
{
    reg_write32(GPIO_STATUS, pins);
}
#endif

irq.h
#ifndef AP_IRQ_H
#define AP_IRQ_H
typedef void (*irq_handler)(void);
unsigned plic_claim(void);
void plic_complete(unsigned id);
void irq_enable(unsigned id, irq_handler handler);
unsigned long long mtime_read(void);
void mtime_set_compare(unsigned long long deadline);
void msip_enable(irq_handler handler);
void mtime_enable(irq_handler handler);
#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

GPIO · register manual
OffsetRegisterBitsFieldAccessResetHeld byMeaning
0x000DIR15:0valuerw0x0000regs1 turns the output driver of that pin on. One bit for each pin.
0x004IN15:0valueroblockThe pin levels, after two sampling flip-flops. One bit for each pin.
0x008OUT15:0valuerw0x0000regsThe saved output value. One bit for each pin.
0x00CSET15:0valuew1sacts on OUT.valueA 1 sets that bit of OUT. One bit for each pin.
0x010CLR15:0valuew1cacts on OUT.valueA 1 clears that bit of OUT. One bit for each pin.
0x014TGL15:0valuew1tacts on OUT.valueA 1 inverts that bit of OUT. One bit for each pin.
0x018IRQ_ENABLE15:0valuerw0x0000regsA 1 lets that pin's status bit raise interrupt source 2. One bit for each pin.
0x01CLEVEL_MODE15:0valuerw0x0000regs1: the pin is a level cause. 0: an edge cause. One bit for each pin.
0x020ACTIVE_HIGH15:0valuerw0xFFFFregs1: a high level, or a rising edge, is the event. One bit for each pin.
0x024STATUS15:0valuew1cblockRaw status. A written 1 clears the saved edge flag of that pin. One bit for each pin.
0x028MASKED_STATUS15:0valueroblockSTATUS AND IRQ_ENABLE. One bit for each pin.

Examples

Example 1

Three Pulses, One Alarm
Input
three pulses and one alarm
Output
edges 00000003 alarms 00000001

Four requests through source 2; the handler reads MASKED_STATUS to tell them apart.

Example 2

Two Alarms
Input
two alarms, no pulse
Output
edges 00000000 alarms 00000002

Each high period counts once: the handler quietens pin 9 and main re-enables it after the line falls.

Example 3

An Early Alarm
Input
an alarm already high at the start
Output
edges 00000001 alarms 00000001

The alarm was high before the event. In level mode it asks as soon as it is enabled.

Constraints and Contract

  • C firmware with the kit, SRAM layout.
  • Take both pins by interrupt. The bench requires as many GPIO interrupts as edges and alarms, and allows two more at most.
  • Keep the event numbers and the line format.

Hints

Hint 1

Which register tells the handler which of the two pins asked?

Hint 2

What happens if the handler returns while pin 9 is still high and still enabled?

Hint 3

When is it safe to let pin 9 ask again?

What the grader checks: Each case drives the two pins in a chosen pattern. It compares the console line and the number of GPIO interrupts the core took. It cannot see the order in which your handler did its work.

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 3 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

Three Pulses, One Alarm

Input
three pulses and one alarm
Expected output
edges 00000003 alarms 00000001

Four requests through source 2; the handler reads MASKED_STATUS to tell them apart.

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

ap_soc v1.5 · firmware · server