Skip to content

Instantly share code, notes, and snippets.

@dgrubb
Created October 17, 2017 15:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgrubb/44635b9d8ec235bd9de00813b9801f63 to your computer and use it in GitHub Desktop.
Save dgrubb/44635b9d8ec235bd9de00813b9801f63 to your computer and use it in GitHub Desktop.
HiFive1 Timer interrupt sample
/* Standard includes */
#include <stdio.h>
/* HiFive1/FE310 includes */
#include "sifive/devices/spi.h"
#include "platform.h"
#include "encoding.h"
#include "plic/plic_driver.h"
volatile int invert_LEDs = 0;
void handle_m_time_interrupt()
{
/* Disable the machine and timer interrupts until setup is completed */
clear_csr(mie, MIP_MTIP);
/* Set the machine timer to go off in X seconds */
volatile uint64_t * mtime = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIME);
volatile uint64_t * mtimecmp = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
uint64_t now = *mtime;
/* Send data every second */
uint64_t then = now + 100;
*mtimecmp = then;
/* Notify the main loop that it can now invert the LED output */
invert_LEDs = 1;
/* Re-enable timer */
set_csr(mie, MIP_MTIP);
}
void handle_m_ext_interrupt()
{
}
void init_GPIO()
{
GPIO_REG(GPIO_OUTPUT_EN) |= (1 << GREEN_LED_OFFSET);
GPIO_REG(GPIO_OUTPUT_VAL) |= (1 << GREEN_LED_OFFSET);
}
void init_timer()
{
clear_csr(mie, MIP_MTIP);
volatile uint64_t * mtime = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIME);
volatile uint64_t * mtimecmp = (uint64_t*) (CLINT_CTRL_ADDR + CLINT_MTIMECMP);
uint64_t now = *mtime;
uint64_t then = now + 100;
*mtimecmp = then;
/* Enable timer and all interrupts */
set_csr(mie, MIP_MTIP);
set_csr(mstatus, MSTATUS_MIE);
}
int main()
{
puts("Timer setup complete, periodically inverting the green LED ...");
init_GPIO();
init_timer();
while (1) {
if (invert_LEDs) {
GPIO_REG(GPIO_OUTPUT_VAL) ^= (0x1 << GREEN_LED_OFFSET);
invert_LEDs = 0;
}
};
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment