diff --git a/examples/adc_polled/README.md b/examples/adc_polled/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f39a17af9f26e29ab5c7d558beee47ca3618d33d --- /dev/null +++ b/examples/adc_polled/README.md @@ -0,0 +1,14 @@ +# Polled ADC example +This example code demonstrates the use of the CH32V003 on-chip ADC in polled mode +wherein a single analog input channel is configured on GPIO and then digitized +under software control. When a sample is needed the ADC is started and the CPU +busy-waits until the conversion is complete before reading the result. + +This is just about the simplest and easiest use-case for the ADC and wastes a lot +of CPU cycles but it does show the basics of doing conversions in a situation +where high performance is not needed. + +## Use +Connect an analog voltage source that varies between 0V and 3.3v to GPIO pin PD4 +and a 3.3V logic level serial terminal to pin PD5. The 10-bit result will be +printed periodically on the serial output. diff --git a/examples/i2c_oled/README.md b/examples/i2c_oled/README.md new file mode 100644 index 0000000000000000000000000000000000000000..625a07552f3d2e21353334f551caa36d46d824e9 --- /dev/null +++ b/examples/i2c_oled/README.md @@ -0,0 +1,22 @@ +# I2C OLED demonstration +This example code demonstrates use of the CH32V003 I2C peripheral with an SSD1306 +OLED (128x32 pixel type). It provides a generic I2C port initialization and +transmit (write-only) low level interface and a high-level graphics driver with +pixels, lines, circles, rectangles and 8x8 character font rendering. Out of the +box this demo cycles through a few different graphic screens to test out the +various drawing primitives. + + +https://user-images.githubusercontent.com/1132011/230734071-dee305de-5aad-4ca0-a422-5fb31d2bb0e0.mp4 + + +## Use +Connect an SSD1306-based OLED in I2C interface mode to pins PC1 (SCL) and PC2 (SDA) +of the CH32V003 with proper I2C pullup resistors and observe the various graphic +images that cycle at approximately 2 second intervals. + +Note - I used an Adafruit 0.91" 128x32 OLED breakout (stock #4440) for my testing +and found that the built-in 10k pullup resistors were too weak for reliable I2C +bus transactions. I had to add 2.2k resistors to the SCL and SDA pads to allow +proper operation. + diff --git a/examples/systick_irq/README.md b/examples/systick_irq/README.md new file mode 100644 index 0000000000000000000000000000000000000000..732b29bc679d1a063229888c6a66b198b0a7e58d --- /dev/null +++ b/examples/systick_irq/README.md @@ -0,0 +1,22 @@ +# Systick IRQ demonstration +This example shows how to set up the built-in Systick IRQ to generate periodic +interrupts for timing. Many bare-metal and RTOS based embedded applications will +use the Systick IRQ for timing, periodic housekeeping and task arbitration so +knowing how to set that up is useful. + +Note that this example is *NOT* compatible with the Delay_Ms() function that's +part of the ch32v003fun library - that function uses the Systick counter for +doing busy-wait delays and will interfere with its use in generating predictable +IRQs. Do not use the built-in Delay_Ms() and related functions when using Systick +for IRQs. + +Note also the use of the `__attribute__((interrupt))` syntax in declaring the +IRQ handler. Some of the IRQ examples from the WCH HAL library have slightly +different syntax to make use of a fast IRQ mode but which is not compatible with +generic RISC-V GCC so that feature is not used here. + +# Use +Connect LEDs (with proper current limit resistors) to GPIO pins C0, D0, D4 and +a 3.3V logic level serial terminal to PD5. The LEDs will flash and an incrementing +count will be printed to the serial port at rates that are controlled by the +Systick IRQ. diff --git a/examples/tim1_pwm/.gdbinit b/examples/tim1_pwm/.gdbinit new file mode 100644 index 0000000000000000000000000000000000000000..427595ae0e4d2332e7e580facfd2c4327e8441c6 --- /dev/null +++ b/examples/tim1_pwm/.gdbinit @@ -0,0 +1,2 @@ +file tim1_pwm.elf +target extended-remote localhost:3333 diff --git a/examples/tim1_pwm/README.md b/examples/tim1_pwm/README.md new file mode 100644 index 0000000000000000000000000000000000000000..2cf153a2e142ec2d1a9a19b8a1be034954a307f5 --- /dev/null +++ b/examples/tim1_pwm/README.md @@ -0,0 +1,9 @@ +# Demonstration of PWM using Timer 1 +This example shows how to set up Timer 1 (the Advanced Control Timer) to generate +Pulse-Width Modulation (PWM) on two output pins. PWM is frequently used to do +variable brightness on LEDs or for digital-to-analog conversion when combined with +a suitable lowpass filter. + +## Use +Connect GPIO pins PD0 and PC4 to LEDs (with appropriate current limiting) and +observe that they fade with different cycles. diff --git a/examples/tim1_pwm/debug.sh b/examples/tim1_pwm/debug.sh new file mode 100755 index 0000000000000000000000000000000000000000..bb05a9465959cb153fad95bba4b2175667830528 --- /dev/null +++ b/examples/tim1_pwm/debug.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# before running this you should start OOCD server +#../../../MRS_Toolchain_Linux_x64_V1.70/OpenOCD/bin/openocd -f ../../../MRS_Toolchain_Linux_x64_V1.70/OpenOCD/bin/wch-riscv.cfg + +../../../MRS_Toolchain_Linux_x64_V1.70/RISC-V\ Embedded\ GCC/bin/riscv-none-embed-gdb