MyFPGA Forum

 找回密码
 注册
搜索
查看: 4047|回复: 1

RISC-V on T-core demo

[复制链接]
发表于 2021-5-18 01:29:57 | 显示全部楼层 |阅读模式
实验手册测试demo及其魔改系列
 楼主| 发表于 2021-5-18 01:41:09 | 显示全部楼层
demo_gpio 主要实现led流水灯效果
在后面的实验中,涉及到我的一些自己需要对其进行一些相关的GPIO操作,但是不知道怎么去针对某一个GPIO进行操作,以及对TERASIC_LED_MASK,掩码的一些理解问题。因为在E203中涉及到32I/O的操作,在TERASIC_LED_MASK中,定义了0x0000000F,即0x0000_000F其中的一位‘0’代表了的16进制的0000,即在对TERASIC_LED_MASK中的一位‘0’进行设置值,就是在对其中的二进制中的“0000”进行操作。而在TERASIC_LED_MASK中定义了0x0000000F,即对应与二进制中的1111,由原理图可知,LED[3:0]对应的是GPIO[3:0]。在设置TERASIC_LED_MASK中,即对GPIO[3:0]进行操作。因而可以对其进行魔改,实现三个、两个LED的流水灯。
#include <stdint.h>
#include <stdbool.h>
#include <stdatomic.h>
#include "encoding.h"
#include <platform.h>


// LED0-3: bit 0-3
// SW0-3:  bit 4-7
// KEY0-1: bit 8-9
// ...
#define TERASIC_LED_MASK  0x0000000F
#define TERASIC_SW_MASK   0x000000F0
#define TERASIC_KEY_MASK  0x00000300


uint32_t gpio_value = 0;

void set_LED(uint8_t value){
    gpio_value &= ~TERASIC_LED_MASK;
    gpio_value |= (uint32_t)(value << 0) & TERASIC_LED_MASK;
}

void update_GPIO_Output(void){
    GPIO_REG(GPIO_OUTPUT_VAL) = (GPIO_REG(GPIO_OUTPUT_VAL)&(~TERASIC_LED_MASK))|gpio_value;
}



int main (void){
    // Wait a bit because we were changing the GPIOs
    volatile int i=0;
    while(i < 10000){i++;}

    // Set LED0-3 output
    GPIO_REG(GPIO_OUTPUT_EN) |= TERASIC_LED_MASK;

    uint8_t led_value=0x01;

    while(1){
        volatile uint64_t *  now = (volatile uint64_t*)(CLINT_CTRL_ADDR + CLINT_MTIME);
        volatile uint64_t then = *now + 4000;

        while (*now < then) { }

        if(led_value >= 0x08)
            led_value = 0x01;
        else
            led_value = led_value << 1;

        set_LED(led_value);
        update_GPIO_Output();
    }    //end of while


}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|MyFPGA

GMT+8, 2024-3-29 16:37 , Processed in 0.040213 second(s), 15 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表