From 4d0f63ef8657d46a8bb789e2c7b8e5620aaf3bd0 Mon Sep 17 00:00:00 2001 From: liangkangnan Date: Tue, 7 Sep 2021 09:46:09 +0800 Subject: [PATCH] sdk:examples: add int_preempt Signed-off-by: liangkangnan --- sdk/examples/int_preempt/.gitignore | 8 ++++ sdk/examples/int_preempt/Makefile | 20 +++++++++ sdk/examples/int_preempt/README.md | 1 + sdk/examples/int_preempt/main.c | 66 +++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 sdk/examples/int_preempt/.gitignore create mode 100644 sdk/examples/int_preempt/Makefile create mode 100644 sdk/examples/int_preempt/README.md create mode 100644 sdk/examples/int_preempt/main.c diff --git a/sdk/examples/int_preempt/.gitignore b/sdk/examples/int_preempt/.gitignore new file mode 100644 index 0000000..cb2d871 --- /dev/null +++ b/sdk/examples/int_preempt/.gitignore @@ -0,0 +1,8 @@ +# Object files +*.o +*.ko +*.obj +*.bin +*.dump +*.mem +int_preempt diff --git a/sdk/examples/int_preempt/Makefile b/sdk/examples/int_preempt/Makefile new file mode 100644 index 0000000..b87fede --- /dev/null +++ b/sdk/examples/int_preempt/Makefile @@ -0,0 +1,20 @@ +RISCV_ARCH := rv32im +RISCV_ABI := ilp32 +RISCV_MCMODEL := medlow + + +TARGET = int_preempt + + +#CFLAGS += -DSIMULATION +#CFLAGS += -Os +#ASM_SRCS += +#LDFLAGS += +#INCLUDES += -I. + +C_SRCS := \ + main.c \ + + +BSP_DIR = ../../bsp +include ../../bsp/bsp.mk diff --git a/sdk/examples/int_preempt/README.md b/sdk/examples/int_preempt/README.md new file mode 100644 index 0000000..9cc5dca --- /dev/null +++ b/sdk/examples/int_preempt/README.md @@ -0,0 +1 @@ +中断抢占例程。 \ No newline at end of file diff --git a/sdk/examples/int_preempt/main.c b/sdk/examples/int_preempt/main.c new file mode 100644 index 0000000..c7912cb --- /dev/null +++ b/sdk/examples/int_preempt/main.c @@ -0,0 +1,66 @@ +#include + +#include "../../bsp/include/timer.h" +#include "../../bsp/include/utils.h" +#include "../../bsp/include/rvic.h" +#include "../../bsp/include/gpio.h" +#include "../../bsp/include/uart.h" +#include "../../bsp/include/xprintf.h" + +static volatile uint32_t count; + +int main() +{ + count = 0; + + uart0_init(uart0_putc); + + timer0_set_div(25); + timer0_set_value(10000); // 10ms period + timer0_clear_int_pending(); + timer0_set_int_enable(1); + timer0_set_mode_ontshot(); + // timer0中断优先级为1 + rvic_set_irq_prio_level(0, 1); + global_irq_enable(); + rvic_irq_enable(0); + timer0_start(1); + + // gpio0输出模式 + gpio_set_mode(GPIO0, GPIO_MODE_OUTPUT); + // gpio1输入模式 + gpio_set_mode(GPIO1, GPIO_MODE_INPUT); + // gpio1双沿中断 + gpio_set_interrupt_mode(GPIO1, GPIO_INTR_DOUBLE_EDGE); + rvic_irq_enable(3); + // gpio1中断优先级为2 + rvic_set_irq_prio_level(3, 2); + + while (1) { + if (count == 3) { + gpio_set_output_toggle(GPIO0); // toggle led + busy_wait(500 * 1000); + } + } +} + +void timer0_irq_handler() +{ + timer0_clear_int_pending(); + rvic_clear_irq_pending(0); + + xprintf("timer0 isr\n"); + // GPIO0对应LED为灭 + gpio_set_output_data(GPIO0, 1); + + while (count != 3); +} + +void gpio1_irq_handler() +{ + gpio_clear_intr_pending(GPIO1); + rvic_clear_irq_pending(3); + + xprintf("gpio1 isr\n"); + count++; +}