diff --git a/myapps/practice/01-button/Makefile b/myapps/practice/01-button/Makefile new file mode 100644 index 0000000..3560750 --- /dev/null +++ b/myapps/practice/01-button/Makefile @@ -0,0 +1,165 @@ +###################################### +# 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/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/01-button/Makefile-fullsdk.bkp b/myapps/practice/01-button/Makefile-fullsdk.bkp new file mode 100644 index 0000000..1bffc48 --- /dev/null +++ b/myapps/practice/01-button/Makefile-fullsdk.bkp @@ -0,0 +1,185 @@ +###################################### +# 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_timer1.c \ +../../../vendor/StdPeriphDriver/CH59x_adc.c \ +../../../vendor/StdPeriphDriver/CH59x_flash.c \ +../../../vendor/StdPeriphDriver/CH59x_usb2hostBase.c \ +../../../vendor/StdPeriphDriver/CH59x_uart2.c \ +../../../vendor/StdPeriphDriver/CH59x_spi1.c \ +../../../vendor/StdPeriphDriver/CH59x_usbhostBase.c \ +../../../vendor/StdPeriphDriver/CH59x_timer3.c \ +../../../vendor/StdPeriphDriver/CH59x_usbhostClass.c \ +../../../vendor/StdPeriphDriver/CH59x_timer2.c \ +../../../vendor/StdPeriphDriver/CH59x_uart1.c \ +../../../vendor/StdPeriphDriver/CH59x_clk.c \ +../../../vendor/StdPeriphDriver/CH59x_gpio.c \ +../../../vendor/StdPeriphDriver/CH59x_usb2hostClass.c \ +../../../vendor/StdPeriphDriver/CH59x_pwr.c \ +../../../vendor/StdPeriphDriver/CH59x_timer0.c \ +../../../vendor/StdPeriphDriver/CH59x_pwm.c \ +../../../vendor/StdPeriphDriver/CH59x_uart0.c \ +../../../vendor/StdPeriphDriver/CH59x_spi0.c \ +../../../vendor/StdPeriphDriver/CH59x_i2c.c \ +../../../vendor/StdPeriphDriver/CH59x_usbdev.c \ +../../../vendor/StdPeriphDriver/CH59x_usb2dev.c \ +../../../vendor/StdPeriphDriver/CH59x_uart3.c \ +../../../vendor/StdPeriphDriver/CH59x_sys.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/01-button/src/main.c b/myapps/practice/01-button/src/main.c new file mode 100644 index 0000000..90e3acf --- /dev/null +++ b/myapps/practice/01-button/src/main.c @@ -0,0 +1,24 @@ + +// LED: PA8 (active low) +// BUT: PB22 (active low, no pullup) + +#include "CH59x_common.h" + +int main() +{ + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_SetBits(GPIO_Pin_8); // LED + GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeOut_PP_5mA); + + GPIOB_ModeCfg(GPIO_Pin_22, GPIO_ModeIN_PU); // Button + + while(1) { + if (GPIOB_ReadPortPin(GPIO_Pin_22)) + GPIOA_SetBits(GPIO_Pin_8); + else + GPIOA_ResetBits(GPIO_Pin_8); + } + +} + diff --git a/myapps/practice/02-button-int/Makefile b/myapps/practice/02-button-int/Makefile new file mode 100644 index 0000000..3560750 --- /dev/null +++ b/myapps/practice/02-button-int/Makefile @@ -0,0 +1,165 @@ +###################################### +# 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/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/02-button-int/src/main.c b/myapps/practice/02-button-int/src/main.c new file mode 100644 index 0000000..35dba0c --- /dev/null +++ b/myapps/practice/02-button-int/src/main.c @@ -0,0 +1,33 @@ +// TODO fix line 27 +// LED: PA8 (active low) +// BUT: PB22 (active low, no pullup) + +#include "CH59x_common.h" + +__INTERRUPT +__HIGH_CODE +void GPIOB_IRQHandler() +{ + if (GPIOB_ReadITFlagBit(GPIO_Pin_8)) { + GPIOA_InverseBits(GPIO_Pin_8); + GPIOB_ClearITFlagBit(GPIO_Pin_8); + } +} + +int main() +{ + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_SetBits(GPIO_Pin_8); // LED + GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeOut_PP_5mA); + + GPIOB_ModeCfg(GPIO_Pin_22, GPIO_ModeIN_PU); // button + GPIOB_ITModeCfg(GPIO_Pin_8, GPIO_ITMode_FallEdge); // PB8 mux'd as PB22 see below + GPIOB_ClearITFlagBit(GPIO_Pin_8); + GPIOPinRemap(ENABLE, RB_PIN_INTX); // <================== workaround, as R16_PB_INT reg is 16b wide + PFIC_EnableIRQ(GPIO_B_IRQn); + + while (1); + +} + diff --git a/myapps/practice/02-button-int/src/main.c.bkp1 b/myapps/practice/02-button-int/src/main.c.bkp1 new file mode 100644 index 0000000..de2e6cc --- /dev/null +++ b/myapps/practice/02-button-int/src/main.c.bkp1 @@ -0,0 +1,41 @@ + +// LED: PA8 (active low) +// BUT: PB22 (active low, no pullup) + +#include "CH59x_common.h" +#include + +volatile bool buttonPressed = false; + +__INTERRUPT +__HIGH_CODE +void GPIOB_IRQHandler() +{ + if (GPIOB_ReadITFlagBit(GPIO_Pin_8)) { + buttonPressed = true; + GPIOB_ClearITFlagBit(GPIO_Pin_8); + } +} + +int main() +{ + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_SetBits(GPIO_Pin_8); // LED + GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeOut_PP_5mA); + + GPIOB_ModeCfg(GPIO_Pin_22, GPIO_ModeIN_PU); // button + GPIOB_ITModeCfg(GPIO_Pin_8, GPIO_ITMode_FallEdge); + GPIOB_ClearITFlagBit(GPIO_Pin_8); + GPIOPinRemap(ENABLE, RB_PIN_INTX); // <================== + PFIC_EnableIRQ(GPIO_B_IRQn); + + while (1) { + if (buttonPressed) { + buttonPressed = false; + GPIOA_InverseBits(GPIO_Pin_8); + } + } + +} + diff --git a/myapps/practice/03-tmr-int/Makefile b/myapps/practice/03-tmr-int/Makefile new file mode 100644 index 0000000..43923fb --- /dev/null +++ b/myapps/practice/03-tmr-int/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_timer1.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/03-tmr-int/src/main.c b/myapps/practice/03-tmr-int/src/main.c new file mode 100644 index 0000000..d2f2ff2 --- /dev/null +++ b/myapps/practice/03-tmr-int/src/main.c @@ -0,0 +1,32 @@ + +// LED: PA8 (active low) + +#include "CH59x_common.h" +#include + +__INTERRUPT +__HIGH_CODE +void TMR1_IRQHandler() +{ + // + if (TMR1_GetITFlag(TMR0_3_IT_CYC_END)) { + GPIOA_InverseBits(GPIO_Pin_8); + TMR1_ClearITFlag(TMR0_3_IT_CYC_END); + } +} + +int main() +{ + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_SetBits(GPIO_Pin_8); // LED + GPIOA_ModeCfg(GPIO_Pin_8, GPIO_ModeOut_PP_5mA); + + TMR1_TimerInit(30000000); // for 500ms + TMR1_ClearITFlag(TMR0_3_IT_CYC_END); + TMR1_ITCfg(true, TMR0_3_IT_CYC_END); //RB_TMR_IE_CYC_END, first bit + PFIC_EnableIRQ(TMR1_IRQn); + TMR1_Enable(); + + while (1); +} diff --git a/myapps/practice/04-pwm/Makefile b/myapps/practice/04-pwm/Makefile new file mode 100644 index 0000000..43923fb --- /dev/null +++ b/myapps/practice/04-pwm/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_timer1.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/04-pwm/src/main.c b/myapps/practice/04-pwm/src/main.c new file mode 100644 index 0000000..96f8770 --- /dev/null +++ b/myapps/practice/04-pwm/src/main.c @@ -0,0 +1,61 @@ + +// TMR1/PWM1/CAP1: PA10 (active low) + +#include "CH59x_common.h" +#include + +#define PWM_FREQ 1000 +#define PWM_PRD FREQ_SYS/PWM_FREQ +// 0 -> 60,000 +#define PWM_STEP 600 +// 600: 100 steps +#define DELAY_STEP 50 +// 5 second fade in + +int main() +{ + int32_t d = 0; + bool rising = true; + + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_SetBits(GPIO_Pin_10); // PWM OUT + GPIOA_ModeCfg(GPIO_Pin_10, GPIO_ModeOut_PP_5mA); + + TMR1_PWMInit(Low_Level, PWM_Times_1); // Note: PWMX_PolarTypeDef defined in CH58x_pwm.h + // Need to init before setting PWMCycleCfg & PWMActDataWidth + TMR1_PWMCycleCfg(PWM_PRD); + //TMR1_PWMActDataWidth(PWM_PRD*PWM_DUTY); // MAX value: TMR1_PWMCycleCfg above (for 100%) + TMR1_PWMActDataWidth(d); // MAX value: TMR1_PWMCycleCfg above (for 100%) + TMR1_PWMEnable(); // enables output pin, configured above + TMR1_Enable(); + + while (1) { + + if (rising == true) { + + DelayMs(DELAY_STEP); + d += PWM_STEP; + + if (d == PWM_PRD) + rising = false; + + TMR1_Disable(); + TMR1_PWMActDataWidth(d); + TMR1_Enable(); + + } else { + + DelayMs(DELAY_STEP); + d -= PWM_STEP; + + if (d == 0) + rising = true; + + TMR1_Disable(); + TMR1_PWMActDataWidth(d); + TMR1_Enable(); + } + } + +} diff --git a/myapps/practice/04-pwm/src/main.c.fixedpulse b/myapps/practice/04-pwm/src/main.c.fixedpulse new file mode 100644 index 0000000..c31ce71 --- /dev/null +++ b/myapps/practice/04-pwm/src/main.c.fixedpulse @@ -0,0 +1,26 @@ + +// TMR1/PWM1/CAP1: PA10 (active low) + +#include "CH59x_common.h" + +#define PWM_FREQ 1000 +#define PWM_PRD FREQ_SYS/PWM_FREQ +#define PWM_DUTY 0.20 + +int main() +{ + SetSysClock(CLK_SOURCE_PLL_60MHz); + + GPIOA_SetBits(GPIO_Pin_10); // PWM OUT + GPIOA_ModeCfg(GPIO_Pin_10, GPIO_ModeOut_PP_5mA); + + TMR1_PWMInit(Low_Level, PWM_Times_1); // Note: PWMX_PolarTypeDef defined in CH58x_pwm.h + // Need to init before setting PWMCycleCfg & PWMActDataWidth + TMR1_PWMCycleCfg(PWM_PRD); + TMR1_PWMActDataWidth(PWM_PRD*PWM_DUTY); // MAX value: TMR1_PWMCycleCfg above (for 100%) + TMR1_PWMEnable(); // enables output pin, configured above + TMR1_Enable(); + + while (1); + +} diff --git a/myapps/practice/05-pwm-dma/Makefile b/myapps/practice/05-pwm-dma/Makefile new file mode 100644 index 0000000..43923fb --- /dev/null +++ b/myapps/practice/05-pwm-dma/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_timer1.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/05-pwm-dma/src/main.c b/myapps/practice/05-pwm-dma/src/main.c new file mode 100644 index 0000000..6992113 --- /dev/null +++ b/myapps/practice/05-pwm-dma/src/main.c @@ -0,0 +1,49 @@ + +// TMR1/PWM1/CAP1: PA10 (active low) + +#include "CH59x_common.h" +#include + +#define PWM_FREQ 1000 +#define PWM_PRD FREQ_SYS/PWM_FREQ +// 0 -> 60,000 +#define PWM_STEP 240 +// 125 steps +#define TABLE_SIZE 500 + +uint32_t step_table[TABLE_SIZE]; // need to use 32bit register to match 'FIFO' reg + +void load_dma() +{ + uint16_t i = 0; + for (uint32_t n = 0; n < PWM_PRD; n += PWM_STEP) { + step_table[i] = n; + i++; + } + for (uint32_t n = PWM_PRD; n > 0; n -= PWM_STEP) { + step_table[i] = n; + i++; + } +} + +int main() +{ + + SetSysClock(CLK_SOURCE_PLL_60MHz); + + load_dma(); + + GPIOA_SetBits(GPIO_Pin_10); // PWM OUT + GPIOA_ModeCfg(GPIO_Pin_10, GPIO_ModeOut_PP_5mA); + + TMR1_PWMInit(Low_Level, PWM_Times_8); // Note: PWMX_PolarTypeDef defined in CH58x_pwm.h + // Need to init before setting PWMCycleCfg & PWMActDataWidth + TMR1_DMACfg(ENABLE, (uint16_t)(uint32_t)step_table, (uint16_t)(uint32_t)&step_table[TABLE_SIZE], true); + TMR1_PWMCycleCfg(PWM_PRD); + TMR1_PWMActDataWidth(0); // MAX value: TMR1_PWMCycleCfg above (for 100%) + TMR1_PWMEnable(); // enables output pin, configured above + TMR1_Enable(); + + while (1); + +}