Close

Hardware Peripherals for C Programs

A project log for 2019 Hackaday Superconference Badge

FPGA Running RISC-V Core in a Game Boy form factor

lutetiumLutetium 11/04/2019 at 05:500 Comments

The most comprehensive information on hardware peripherals available to C programs is found in the through comments of the mach_defines.h file of soc/ipl/gloss

Buttons

Buttons may be read from a register; the bits in this register go high when the corresponding button is pressed. Button names begin BUTTON_ and end with: UP, DOWN, LEFT, RIGHT, A, B, SELECT, START.

if ((MISC_REG(MISC_BTN_REG) & BUTTON_RIGHT)) {
    //Do something when right button is pressed.
}

Upcounting Timer

A 60 Hz upcounting timer is available. Higher resolution timers are in the works too. Implement this function to easily read this counter:

uint32_t counter60hz(void) {
    return GFX_REG(GFX_VBLCTR_REG);
}

Additional time and delay utilities are available from the badge time module.

#include "badgetime.h"     

Following example set by Arduino, badge time provides a way to retrieve a 32-bit milliseconds count and a way to delay a given number of milliseconds.

uint32_t now = millis(); // Number of milliseconds since badge power-up.

delay(1000); // Wait one second

In addition to delay, badge time also provides convenience functions to perform several other popular types of wait. While waiting, these functions will service system background tasks.

wait_for_button_release(); // Returns once user releases all buttons

wait_for_button_press(BUTTON_RIGHT); // Returns when user presses given button(s)

while (doing something on every frame) {
    uint32_t current_frame = GFX_REG(GFX_VBLCTR_REG);
    // Do what we need to do
    wait_for_next_frame(current_frame); // Service background tasks until next frame needs to be drawn.
}

 Apps with a tight loop without wait would need to periodically call delay with count of zero to ensure the background tasks are not starved.

delay(0); // Service background tasks and return as soon as possible

Random Number Source

Hardware random number generator can be read via this register:

MISC_REG(MISC_RNG_REG)

LEDs

LED control is provides by this register for most of the functionality for most of the LEDs on the badge. Note that some LEDs can't initially be accessed by software.

MISC_REG(MISC_LED_REG)=0xF;

Discussions