temp commit

Signed-off-by: liangkangnan <liangkangnan@163.com>
pull/4/head
liangkangnan 2021-05-15 14:52:30 +08:00
parent 6e466fbbf7
commit b0f4592322
9 changed files with 74 additions and 104 deletions

View File

@ -23,6 +23,7 @@ C_SRCS += $(BSP_DIR)/lib/xprintf.c
C_SRCS += $(BSP_DIR)/lib/uart.c
C_SRCS += $(BSP_DIR)/lib/sim_ctrl.c
C_SRCS += $(BSP_DIR)/lib/machine_timer.c
C_SRCS += $(BSP_DIR)/lib/gpio.c
LINKER_SCRIPT := $(BSP_DIR)/link.lds

View File

@ -7,4 +7,17 @@
#define GPIO_REG(addr) (*((volatile uint32_t *)addr))
#define GPIO0 (0)
#define GPIO1 (1)
#define GPIO_OUTPUT (1)
#define GPIO_INPUT (2)
void gpio_output_enable(uint8_t gpio);
void gpio_input_enable(uint8_t gpio);
uint8_t gpio_get_data(uint8_t gpio);
void gpio_set_data(uint8_t gpio, uint8_t data);
void gpio_data_toggle(uint8_t gpio);
#endif

37
sdk/bsp/lib/gpio.c Normal file
View File

@ -0,0 +1,37 @@
#include <stdint.h>
#include "../../bsp/include/gpio.h"
#include "../../bsp/include/utils.h"
void gpio_output_enable(uint8_t gpio)
{
GPIO_REG(GPIO_CTRL) &= ~(0x3 << (gpio << 1));
GPIO_REG(GPIO_CTRL) |= GPIO_OUTPUT << (gpio << 1);
}
void gpio_input_enable(uint8_t gpio)
{
GPIO_REG(GPIO_CTRL) &= ~(0x3 << (gpio << 1));
GPIO_REG(GPIO_CTRL) |= GPIO_INPUT << (gpio << 1);
}
uint8_t gpio_get_data(uint8_t gpio)
{
if (GPIO_REG(GPIO_DATA) & (1 << gpio))
return 1;
else
return 0;
}
void gpio_set_data(uint8_t gpio, uint8_t data)
{
if (data)
GPIO_REG(GPIO_DATA) |= 1 << gpio;
else
GPIO_REG(GPIO_DATA) &= ~(1 << gpio);
}
void gpio_data_toggle(uint8_t gpio)
{
GPIO_REG(GPIO_DATA) ^= 1 << gpio;
}

View File

@ -1,20 +1,20 @@
#include <stdint.h>
#include "../../bsp/include/gpio.h"
#include "../../bsp/include/utils.h"
int main()
{
GPIO_REG(GPIO_CTRL) |= 0x1; // gpio0输出模式
GPIO_REG(GPIO_CTRL) |= 0x1 << 3; // gpio1输入模式
while (1) {
// 如果GPIO1输入高
if (GPIO_REG(GPIO_DATA) & 0x2)
GPIO_REG(GPIO_DATA) |= 0x1; // GPIO0输出高
// 如果GPIO1输入低
else
GPIO_REG(GPIO_DATA) &= ~0x1; // GPIO0输出低
}
}
#include <stdint.h>
#include "../../bsp/include/gpio.h"
#include "../../bsp/include/utils.h"
int main()
{
gpio_output_enable(GPIO0); // gpio0输出模式
gpio_input_enable(GPIO1); // gpio1输入模式
while (1) {
// 如果GPIO1输入高
if (gpio_get_data(GPIO1))
gpio_set_data(GPIO0, 1); // GPIO0输出高
// 如果GPIO1输入低
else
gpio_set_data(GPIO0, 0); // GPIO0输出低
}
}

View File

@ -34,20 +34,19 @@ int main()
machine_timer_irq_enable(1);
machine_timer_enable(1);
GPIO_REG(GPIO_CTRL) |= 0x1; // set gpio0 output mode
gpio_output_enable(GPIO0);
while (1) {
// 500ms
if (count == 50) {
count = 0;
GPIO_REG(GPIO_DATA) ^= 0x1; // toggle led
gpio_data_toggle(GPIO0); // toggle led
}
}
#endif
}
void timer_irq_handler() __attribute__((interrupt));
void timer_irq_handler()
__attribute__((interrupt)) void timer_irq_handler()
{
count++;

View File

@ -1,8 +0,0 @@
# Object files
*.o
*.ko
*.obj
*.bin
*.dump
*.mem
timer_int

View File

@ -1,20 +0,0 @@
RISCV_ARCH := rv32im
RISCV_ABI := ilp32
RISCV_MCMODEL := medlow
TARGET = timer_int
CFLAGS += -DSIMULATION
#CFLAGS += -Os
#ASM_SRCS +=
#LDFLAGS +=
#INCLUDES += -I.
C_SRCS := \
main.c \
BSP_DIR = ../../bsp
include ../../bsp/bsp.mk

View File

@ -1 +0,0 @@
定时器中断例程。

View File

@ -1,51 +0,0 @@
#include <stdint.h>
#include "../../bsp/include/timer.h"
#include "../../bsp/include/gpio.h"
#include "../../bsp/include/utils.h"
static volatile uint32_t count;
int main()
{
count = 0;
#ifdef SIMULATION
TIMER0_REG(TIMER0_VALUE) = 5000; // 100us period
TIMER0_REG(TIMER0_CTRL) = 0x07; // enable interrupt and start timer
while (1) {
if (count == 2) {
TIMER0_REG(TIMER0_CTRL) = 0x00; // stop timer
count = 0;
// TODO: do something
set_test_pass();
break;
}
}
#else
TIMER0_REG(TIMER0_VALUE) = 500000; // 10ms period
TIMER0_REG(TIMER0_CTRL) = 0x07; // enable interrupt and start timer
GPIO_REG(GPIO_CTRL) |= 0x1; // set gpio0 output mode
while (1) {
// 500ms
if (count == 50) {
count = 0;
GPIO_REG(GPIO_DATA) ^= 0x1; // toggle led
}
}
#endif
return 0;
}
void timer0_irq_handler()
{
TIMER0_REG(TIMER0_CTRL) |= (1 << 2) | (1 << 0); // clear int pending and start timer
count++;
}