From b0f45923229239bed77c2a7ea1c3c22bb941e122 Mon Sep 17 00:00:00 2001 From: liangkangnan Date: Sat, 15 May 2021 14:52:30 +0800 Subject: [PATCH] temp commit Signed-off-by: liangkangnan --- sdk/bsp/bsp.mk | 1 + sdk/bsp/include/gpio.h | 13 ++++++++ sdk/bsp/lib/gpio.c | 37 ++++++++++++++++++++++ sdk/examples/gpio/main.c | 40 ++++++++++++------------ sdk/examples/machine_timer/main.c | 7 ++--- sdk/examples/timer_int/.gitignore | 8 ----- sdk/examples/timer_int/Makefile | 20 ------------ sdk/examples/timer_int/README.md | 1 - sdk/examples/timer_int/main.c | 51 ------------------------------- 9 files changed, 74 insertions(+), 104 deletions(-) create mode 100644 sdk/bsp/lib/gpio.c delete mode 100644 sdk/examples/timer_int/.gitignore delete mode 100644 sdk/examples/timer_int/Makefile delete mode 100644 sdk/examples/timer_int/README.md delete mode 100644 sdk/examples/timer_int/main.c diff --git a/sdk/bsp/bsp.mk b/sdk/bsp/bsp.mk index 03884ac..dbed548 100644 --- a/sdk/bsp/bsp.mk +++ b/sdk/bsp/bsp.mk @@ -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 diff --git a/sdk/bsp/include/gpio.h b/sdk/bsp/include/gpio.h index 40533d4..c244548 100644 --- a/sdk/bsp/include/gpio.h +++ b/sdk/bsp/include/gpio.h @@ -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 diff --git a/sdk/bsp/lib/gpio.c b/sdk/bsp/lib/gpio.c new file mode 100644 index 0000000..3f271e0 --- /dev/null +++ b/sdk/bsp/lib/gpio.c @@ -0,0 +1,37 @@ +#include + +#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; +} diff --git a/sdk/examples/gpio/main.c b/sdk/examples/gpio/main.c index ed61e2d..ae22c90 100644 --- a/sdk/examples/gpio/main.c +++ b/sdk/examples/gpio/main.c @@ -1,20 +1,20 @@ -#include - -#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 + +#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输出低 + } +} diff --git a/sdk/examples/machine_timer/main.c b/sdk/examples/machine_timer/main.c index 08f1e91..e562f60 100644 --- a/sdk/examples/machine_timer/main.c +++ b/sdk/examples/machine_timer/main.c @@ -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++; diff --git a/sdk/examples/timer_int/.gitignore b/sdk/examples/timer_int/.gitignore deleted file mode 100644 index f8096ab..0000000 --- a/sdk/examples/timer_int/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Object files -*.o -*.ko -*.obj -*.bin -*.dump -*.mem -timer_int diff --git a/sdk/examples/timer_int/Makefile b/sdk/examples/timer_int/Makefile deleted file mode 100644 index d398e00..0000000 --- a/sdk/examples/timer_int/Makefile +++ /dev/null @@ -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 diff --git a/sdk/examples/timer_int/README.md b/sdk/examples/timer_int/README.md deleted file mode 100644 index aa8c119..0000000 --- a/sdk/examples/timer_int/README.md +++ /dev/null @@ -1 +0,0 @@ -定时器中断例程。 \ No newline at end of file diff --git a/sdk/examples/timer_int/main.c b/sdk/examples/timer_int/main.c deleted file mode 100644 index ada0ea5..0000000 --- a/sdk/examples/timer_int/main.c +++ /dev/null @@ -1,51 +0,0 @@ -#include - -#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++; -}