diff --git a/README.md b/README.md index 9a93192..bcf6dc3 100644 --- a/README.md +++ b/README.md @@ -105,11 +105,37 @@ Older hardware (InspireMatrix): * This folder contains a bunch of test programs that are subject to change, and are not guaranteed to work for your specific hardware. +> [!NOTE] +> Both `savepaint` and `save-rvasm` implements an filesystem structure. + * `savepaint` - * Todo + * Add saving and loading features. + * Combines `paint`, `movingcar`, `snake-game` and `tic-tac-toe` + * How to play: + * Before first icon appearing, hold Y to clear all the saved paints. + * After first icon appearing, use `UP` and `DOWN` to select apps. + * Use `LEFT` to use the selected app. + * Integrated `paint`: + * Press 64 (or `NUM_LEDS` buttons) to toggle between foreground color and background color. + * Press X or Y to change the foreground or background color. + * Press `UP` button to load a saved paint. + * Press `DOWN` button to save the current paint. + * Integrated `movingcar`: + * Currently 64 buttons are divided into left and right section. + * For each row (8 buttons), left four buttons correspond to `Left Wheel Forward`, `Left Wheel Backward`, `Right Wheel Forward`, `Right Wheel Backward`. + * For each row (8 buttons), right four buttons correspond to `Red`, `Green`, `Blue` and `Special Command` (Currently not used). The LEDs will display the colors when the motor is running. + * E.g. if buttons (counting from left) 1, 3, 5 are pressed, the car will go forward and displays red on the entire screen. + * Special: If all 8 buttons are pressed, the program will go back the first instruction to execute. + * Integrated `snake-game`: + * Press Y to start. + * Use `up / down / left / right` to move the snake. + * Integrated `tic-tac-toe`: + * Press any of the empty spot to play. + * When any of the app ends, press `Y` to go back to the main menu. (`NVIC_SystemReset()` is called) * `save-rvasm` - * Todo + * Add saving and loading features + * Uses `rv-asm`. * `snake-game` * Porting the classic snake game to `InspireMatrix`, which has `8x8`=`64` LEDs and `up / down / left / right` controls. @@ -120,6 +146,36 @@ Older hardware (InspireMatrix): * Green is player while Red is the bot * Try to win it (very easy) +## `funconfig.h` Explanation + +```c +#ifndef _FUNCONFIG_H // Guard +#define _FUNCONFIG_H + +#define CH32V003 1 // Required +#define FUNCONF_USE_DEBUGPRINTF 0 +#define FUNCONF_USE_UARTPRINTF 1 // For printf() over UART (Use Hercules to view) +#define FUNCONF_UART_PRINTF_BAUD 115200 // Baud rate +// Connect UTX pin to RX pin in WCH-LinkE + +// Newly created definitions +#define horizontalButtons 8 +#define verticalButtons 8 +#define NUM_LEDS (horizontalButtons * verticalButtons) + +// Not required for the new InspireComputer board +// For GameConsole, need to define the following +// and connect buzzer pin to RX. +// Affects ch32v003fun.h SystemInit() UART printf initialization. +// #define CH32V003J4M6_USE_PD6_AS_UART_TX + +// Required for WS2812B LEDs `ws2812b_simple.h` +#define FUNCONF_SYSTICK_USE_HCLK 1 + +#endif + +``` + ## Compilation Two options are available for compilation: diff --git a/ch32v003_stt/training/ch32v003/i2c_dump_audio/Makefile b/ch32v003_stt/training/ch32v003/i2c_dump_audio/Makefile deleted file mode 100644 index 6a3f24e..0000000 --- a/ch32v003_stt/training/ch32v003/i2c_dump_audio/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -TARGET:=i2c_dump - -CH32V003FUN?=../../../../ch32v003fun -include ../../../../ch32v003fun/ch32v003fun.mk diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/Makefile b/ch32v003_stt/training/ch32v003/i2c_receive_audio/Makefile deleted file mode 100644 index 54372bb..0000000 --- a/ch32v003_stt/training/ch32v003/i2c_receive_audio/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -TARGET:=i2c_receive - -CH32V003FUN?=../../../../ch32v003fun -include ../../../../ch32v003fun/ch32v003fun.mk diff --git a/ch32v003_stt/training/ch32v003/not-working/i2c_dump_audio/Makefile b/ch32v003_stt/training/ch32v003/not-working/i2c_dump_audio/Makefile new file mode 100644 index 0000000..94d9504 --- /dev/null +++ b/ch32v003_stt/training/ch32v003/not-working/i2c_dump_audio/Makefile @@ -0,0 +1,4 @@ +TARGET:=i2c_dump + +CH32V003FUN?=../../../../../ch32v003fun +include ../../../../../ch32v003fun/ch32v003fun.mk diff --git a/ch32v003_stt/training/ch32v003/i2c_dump_audio/funconfig.h b/ch32v003_stt/training/ch32v003/not-working/i2c_dump_audio/funconfig.h similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_dump_audio/funconfig.h rename to ch32v003_stt/training/ch32v003/not-working/i2c_dump_audio/funconfig.h diff --git a/ch32v003_stt/training/ch32v003/i2c_dump_audio/i2c_dump.c b/ch32v003_stt/training/ch32v003/not-working/i2c_dump_audio/i2c_dump.c similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_dump_audio/i2c_dump.c rename to ch32v003_stt/training/ch32v003/not-working/i2c_dump_audio/i2c_dump.c diff --git a/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/Makefile b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/Makefile new file mode 100644 index 0000000..3bea07e --- /dev/null +++ b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/Makefile @@ -0,0 +1,4 @@ +TARGET:=i2c_receive + +CH32V003FUN?=../../../../../ch32v003fun +include ../../../../../ch32v003fun/ch32v003fun.mk diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/a.c b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/a.c similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_receive_audio/a.c rename to ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/a.c diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/codebook.h b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/codebook.h similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_receive_audio/codebook.h rename to ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/codebook.h diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/dctm_20x8_8bit.h b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/dctm_20x8_8bit.h similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_receive_audio/dctm_20x8_8bit.h rename to ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/dctm_20x8_8bit.h diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/funconfig.h b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/funconfig.h similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_receive_audio/funconfig.h rename to ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/funconfig.h diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/i2c_receive.c b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/i2c_receive.c similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_receive_audio/i2c_receive.c rename to ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/i2c_receive.c diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/mel_mx.h b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/mel_mx.h similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_receive_audio/mel_mx.h rename to ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/mel_mx.h diff --git a/ch32v003_stt/training/ch32v003/i2c_receive_audio/twiddles_RES13.h b/ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/twiddles_RES13.h similarity index 100% rename from ch32v003_stt/training/ch32v003/i2c_receive_audio/twiddles_RES13.h rename to ch32v003_stt/training/ch32v003/not-working/i2c_receive_audio/twiddles_RES13.h diff --git a/ch32v003_stt/training/ch32v003/ws2812bvisualize/Makefile b/ch32v003_stt/training/ch32v003/ws2812bvisualize/Makefile deleted file mode 100644 index 4c5b6f6..0000000 --- a/ch32v003_stt/training/ch32v003/ws2812bvisualize/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -TARGET:=ws2812bvisualize - -CH32V003FUN?=../../../../ch32v003fun -include ../../../../ch32v003fun/ch32v003fun.mk diff --git a/ch32v003_stt/training/ch32v003/ws2812bvisualize/funconfig.h b/ch32v003_stt/training/ch32v003/ws2812bvisualize/funconfig.h deleted file mode 100644 index f4cc236..0000000 --- a/ch32v003_stt/training/ch32v003/ws2812bvisualize/funconfig.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef _FUNCONFIG_H -#define _FUNCONFIG_H - -#define CH32V003 1 -#define FUNCONF_USE_HSE 1 -#define FUNCONF_USE_PLL 1 // Do not use built-in 2x PLL -#define FUNCONF_USE_UARTPRINTF 1 -#define FUNCONF_SYSTICK_USE_HCLK 1 -#define FUNCONF_UART_PRINTF_BAUD 115200 -#define horizontalButtons 8 -#define verticalButtons 8 -#define NUM_LEDS (horizontalButtons * verticalButtons) - -#endif - diff --git a/ch32v003_stt/training/ch32v003/ws2812bvisualize/ws2812bvisualize.c b/ch32v003_stt/training/ch32v003/ws2812bvisualize/ws2812bvisualize.c deleted file mode 100644 index 0f01e37..0000000 --- a/ch32v003_stt/training/ch32v003/ws2812bvisualize/ws2812bvisualize.c +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Visualize audio input from a microphone on a WS2812B LED strip. - * Analog input from a microphone is sampled at 6400 samples/sec. - * Reading is proportional to the brightness and number of LEDs turned on. - */ - -#define WS2812BSIMPLE_IMPLEMENTATION -#include "ch32v003fun.h" -#include "colors.h" -#include "ws2812b_simple.h" - -#include - -static int result = 0, total = 0, count = 0, c = 0; -#define range_low -385 -#define range_high 638 -#define range_diff (range_high - range_low) -static const uint8_t _range_ratio(int val) { - return (val - range_low) * UINT8_MAX / range_diff; -} -// initialize timer for 6400 samples/sec -void init_timer(void); - -// initialize ADC for analog input from PD4 (a microphone) -void init_adc(void); - -// timer interrupt handler -void TIM1_UP_IRQHandler(void) __attribute__((interrupt)); - -int main() { - SystemInit(); - - init_adc(); - init_timer(); - - while (1) { - clear(); - c = _range_ratio(c); - for (uint8_t i = 0; i < (NUM_LEDS * (c / UINT8_MAX)); i++) { - set_color(i, (color_t){c, c, c}); - } - WS2812BSimpleSend(GPIOC, 5, (uint8_t *)led_array, NUM_LEDS * 3); - } -} - -void init_timer(void) { - // TIMER - printf("Initializing timer...\r\n"); - RCC->APB2PCENR |= RCC_APB2Periph_TIM1; - TIM1->CTLR1 |= TIM_CounterMode_Up | TIM_CKD_DIV1; - TIM1->CTLR2 = TIM_MMS_1; - TIM1->ATRLR = 80; // lower = higher sample rate. 800 for 6400sam/sec - TIM1->PSC = 10 - 1; - TIM1->RPTCR = 0; - TIM1->SWEVGR = TIM_PSCReloadMode_Immediate; - - NVIC_EnableIRQ(TIM1_UP_IRQn); - TIM1->INTFR = ~TIM_FLAG_Update; - TIM1->DMAINTENR |= TIM_IT_Update; - TIM1->CTLR1 |= TIM_CEN; -} - -void init_adc(void) { - printf("Initializing ADC... (bjs pin PD4 I think...)\r\n"); - - // ADCCLK = 24 MHz => RCC_ADCPRE divide by 2 - RCC->CFGR0 &= ~(0x1F << 11); - - // Enable GPIOD and ADC - RCC->APB2PCENR |= RCC_APB2Periph_GPIOD | RCC_APB2Periph_ADC1; - - // PD4 is analog input chl 7 - GPIOD->CFGLR &= ~(0xf << (4 * 4)); // pin D4 analog-in now - - // Reset the ADC to init all regs - RCC->APB2PRSTR |= RCC_APB2Periph_ADC1; - RCC->APB2PRSTR &= ~RCC_APB2Periph_ADC1; - - // Set sequencer to channel 7 only - ADC1->RSQR1 = 0; - ADC1->RSQR2 = 0; - ADC1->RSQR3 = 7; - - // set sampling time for chl 7 - ADC1->SAMPTR2 &= ~(ADC_SMP0 << (3 * 7)); - ADC1->SAMPTR2 |= 7 << (3 * 7); // 0:7 => 3/9/15/30/43/57/73/241 cycles - - // Keep CALVOL register with initial value - ADC1->CTLR2 = ADC_ADON | ADC_EXTSEL; - - // Reset calibrate - printf("Calibrating ADC...\r\n"); - ADC1->CTLR2 |= ADC_RSTCAL; - while (ADC1->CTLR2 & ADC_RSTCAL) - ; - // Calibrate - ADC1->CTLR2 |= ADC_CAL; - while (ADC1->CTLR2 & ADC_CAL) - ; - - printf("Calibrating done...\r\n"); -} -void TIM1_UP_IRQHandler(void) { - if (TIM1->INTFR & TIM_FLAG_Update) { - TIM1->INTFR = ~TIM_FLAG_Update; - result = ADC1->RDATAR; - result -= 512; - total += result; - count++; - if (count == 8) { - total >>= 3; - total += 127; - c = total; - total = 0; - count = 0; - } - ADC1->CTLR2 |= ADC_SWSTART; // start next ADC conversion - } -}