diff --git a/myapps/practice/00-uart-tx/Makefile b/myapps/practice/00-uart-tx/Makefile new file mode 100644 index 0000000..f99b21f --- /dev/null +++ b/myapps/practice/00-uart-tx/Makefile @@ -0,0 +1,166 @@ +###################################### +# target +###################################### +TARGET = ch592_blinky +# ../../ + + +###################################### +# building variables +###################################### +# debug build? +DEBUG = 1 +# optimization for size +OPT = -Os + + +####################################### +# paths +####################################### +# Build path +BUILD_DIR = build + +###################################### +# source +###################################### +# C sources +C_SOURCES = \ +src/main.c \ +../../../vendor/StdPeriphDriver/CH59x_clk.c \ +../../../vendor/StdPeriphDriver/CH59x_gpio.c \ +../../../vendor/StdPeriphDriver/CH59x_pwr.c \ +../../../vendor/StdPeriphDriver/CH59x_sys.c \ +../../../vendor/StdPeriphDriver/CH59x_uart3.c \ +../../../vendor/RVMSIS/core_riscv.c \ + + +# ASM sources +ASM_SOURCES = \ +../../../vendor/Startup/startup_CH592.S + +####################################### +# binaries +####################################### +#PREFIX = riscv-none-elf- +#PREFIX = riscv64-elf- +#debian +#PREFIX = riscv64-unknown-elf- +PREFIX = riscv-wch-elf- + +CC = $(PREFIX)gcc +AS = $(PREFIX)gcc -x assembler-with-cpp +CP = $(PREFIX)objcopy +SZ = $(PREFIX)size + +HEX = $(CP) -O ihex +BIN = $(CP) -O binary -S + +####################################### +# CFLAGS +####################################### +# cpu +CPU = -march=rv32imac_zicsr -mabi=ilp32 -msmall-data-limit=8 + +# For gcc version less than v12 +# CPU = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8 + +# fpu +FPU = + +# float-abi +FLOAT-ABI = + +# mcu +MCU = $(CPU) $(FPU) $(FLOAT-ABI) + +# AS includes +AS_INCLUDES = + +# C includes +C_INCLUDES = \ +-I../../../vendor/StdPeriphDriver/inc \ +-I../../../vendor/RVMSIS \ +-I../../../vendor/Core \ +-Isrc/include + +# compile gcc flags +ASFLAGS = $(MCU) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +CFLAGS = $(MCU) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +ifeq ($(DEBUG), 1) +CFLAGS += -g -gdwarf-2 +endif + + +# Generate dependency information +CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" + + +####################################### +# LDFLAGS +####################################### +# link script +LDSCRIPT = ../../../vendor/Ld/Link.ld + +# libraries +LIBS = -lc -lm -lnosys ../../../vendor/StdPeriphDriver/libISP592.a +LIBDIR = +LDFLAGS = $(MCU) -mno-save-restore -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wunused -Wuninitialized -T $(LDSCRIPT) -nostartfiles -Xlinker --gc-sections -Wl,-Map=$(BUILD_DIR)/$(TARGET).map --specs=nano.specs $(LIBS) + +# default action: build all +all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin + + +####################################### +# build the application +####################################### +# list of objects +OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) +vpath %.c $(sort $(dir $(C_SOURCES))) + +# list of ASM program objects +OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.S=.o))) +vpath %.S $(sort $(dir $(ASM_SOURCES))) + +$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) + $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ + +$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR) + $(AS) -c $(CFLAGS) $< -o $@ +#$(LUAOBJECTS) $(OBJECTS) +$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile + $(CC) $(OBJECTS) $(LDFLAGS) -o $@ + $(SZ) $@ + +$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(HEX) $< $@ + +$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(BIN) $< $@ + +$(BUILD_DIR): + mkdir $@ + +####################################### +# Program +####################################### + +isp: $(BUILD_DIR)/$(TARGET).bin + wchisp flash $(BUILD_DIR)/$(TARGET).bin + +flash: $(BUILD_DIR)/$(TARGET).bin + wlink flash $(BUILD_DIR)/$(TARGET).bin + +####################################### +# clean up +####################################### +clean: + -rm -fR $(BUILD_DIR) + +####################################### +# dependencies +####################################### +-include $(wildcard $(BUILD_DIR)/*.d) + +# *** EOF *** diff --git a/myapps/practice/00-uart-tx/src/main.c b/myapps/practice/00-uart-tx/src/main.c new file mode 100644 index 0000000..1488237 --- /dev/null +++ b/myapps/practice/00-uart-tx/src/main.c @@ -0,0 +1,21 @@ +// RXD3/TXD3: PA4/PA5 + + +#include "CH59x_common.h" +#include + + +int main() +{ + char *s = "Hello world\r\n"; + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeOut_PP_5mA); + UART3_DefInit(); + + while (1) { + UART3_SendString((uint8_t *)s, strlen(s)); + mDelaymS(1000); + } + +} diff --git a/myapps/practice/00a-uart-rx/Makefile b/myapps/practice/00a-uart-rx/Makefile new file mode 100644 index 0000000..f99b21f --- /dev/null +++ b/myapps/practice/00a-uart-rx/Makefile @@ -0,0 +1,166 @@ +###################################### +# target +###################################### +TARGET = ch592_blinky +# ../../ + + +###################################### +# building variables +###################################### +# debug build? +DEBUG = 1 +# optimization for size +OPT = -Os + + +####################################### +# paths +####################################### +# Build path +BUILD_DIR = build + +###################################### +# source +###################################### +# C sources +C_SOURCES = \ +src/main.c \ +../../../vendor/StdPeriphDriver/CH59x_clk.c \ +../../../vendor/StdPeriphDriver/CH59x_gpio.c \ +../../../vendor/StdPeriphDriver/CH59x_pwr.c \ +../../../vendor/StdPeriphDriver/CH59x_sys.c \ +../../../vendor/StdPeriphDriver/CH59x_uart3.c \ +../../../vendor/RVMSIS/core_riscv.c \ + + +# ASM sources +ASM_SOURCES = \ +../../../vendor/Startup/startup_CH592.S + +####################################### +# binaries +####################################### +#PREFIX = riscv-none-elf- +#PREFIX = riscv64-elf- +#debian +#PREFIX = riscv64-unknown-elf- +PREFIX = riscv-wch-elf- + +CC = $(PREFIX)gcc +AS = $(PREFIX)gcc -x assembler-with-cpp +CP = $(PREFIX)objcopy +SZ = $(PREFIX)size + +HEX = $(CP) -O ihex +BIN = $(CP) -O binary -S + +####################################### +# CFLAGS +####################################### +# cpu +CPU = -march=rv32imac_zicsr -mabi=ilp32 -msmall-data-limit=8 + +# For gcc version less than v12 +# CPU = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8 + +# fpu +FPU = + +# float-abi +FLOAT-ABI = + +# mcu +MCU = $(CPU) $(FPU) $(FLOAT-ABI) + +# AS includes +AS_INCLUDES = + +# C includes +C_INCLUDES = \ +-I../../../vendor/StdPeriphDriver/inc \ +-I../../../vendor/RVMSIS \ +-I../../../vendor/Core \ +-Isrc/include + +# compile gcc flags +ASFLAGS = $(MCU) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +CFLAGS = $(MCU) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections + +ifeq ($(DEBUG), 1) +CFLAGS += -g -gdwarf-2 +endif + + +# Generate dependency information +CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)" + + +####################################### +# LDFLAGS +####################################### +# link script +LDSCRIPT = ../../../vendor/Ld/Link.ld + +# libraries +LIBS = -lc -lm -lnosys ../../../vendor/StdPeriphDriver/libISP592.a +LIBDIR = +LDFLAGS = $(MCU) -mno-save-restore -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wunused -Wuninitialized -T $(LDSCRIPT) -nostartfiles -Xlinker --gc-sections -Wl,-Map=$(BUILD_DIR)/$(TARGET).map --specs=nano.specs $(LIBS) + +# default action: build all +all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin + + +####################################### +# build the application +####################################### +# list of objects +OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o))) +vpath %.c $(sort $(dir $(C_SOURCES))) + +# list of ASM program objects +OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.S=.o))) +vpath %.S $(sort $(dir $(ASM_SOURCES))) + +$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) + $(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@ + +$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR) + $(AS) -c $(CFLAGS) $< -o $@ +#$(LUAOBJECTS) $(OBJECTS) +$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile + $(CC) $(OBJECTS) $(LDFLAGS) -o $@ + $(SZ) $@ + +$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(HEX) $< $@ + +$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR) + $(BIN) $< $@ + +$(BUILD_DIR): + mkdir $@ + +####################################### +# Program +####################################### + +isp: $(BUILD_DIR)/$(TARGET).bin + wchisp flash $(BUILD_DIR)/$(TARGET).bin + +flash: $(BUILD_DIR)/$(TARGET).bin + wlink flash $(BUILD_DIR)/$(TARGET).bin + +####################################### +# clean up +####################################### +clean: + -rm -fR $(BUILD_DIR) + +####################################### +# dependencies +####################################### +-include $(wildcard $(BUILD_DIR)/*.d) + +# *** EOF *** diff --git a/myapps/practice/00a-uart-rx/src/main.c b/myapps/practice/00a-uart-rx/src/main.c new file mode 100644 index 0000000..ef69f87 --- /dev/null +++ b/myapps/practice/00a-uart-rx/src/main.c @@ -0,0 +1,45 @@ +// RXD3/TXD3: PA4/PA5 + + +#include "CH59x_common.h" +#include +#include + +char b[25]; +bool isrx = 0; + +__INTERRUPT +__HIGH_CODE +void UART3_IRQHandler() +{ + GPIOA_InverseBits(GPIO_Pin_8); + UART3_RecvString((uint8_t *)b); + //UART3_SendString((uint8_t *)b, strlen(b)); + isrx = 1; +} + +int main() +{ + //char *s = "Hello world\r\n"; + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_SetBits(GPIO_Pin_8); // LED + GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeOut_PP_5mA); + + GPIOA_ModeCfg(GPIO_Pin_4, GPIO_ModeIN_PU); // RXD3 + GPIOA_ModeCfg(GPIO_Pin_5, GPIO_ModeOut_PP_5mA); // TXD3 + + UART3_DefInit(); + UART3_INTCfg(ENABLE, RB_IER_RECV_RDY); + PFIC_EnableIRQ(UART3_IRQn); + + while (1) { + + if (isrx == 1) { + UART3_SendString((uint8_t *)b, strlen(b)); + isrx = 0; + } + mDelaymS(100); + } + +}