#include #include #include "ch573/gpio.h" #include "ch573/pwr.h" #include "ch573/uart.h" #include "clock.h" #include "spi.h" #include "string.h" #include "system.h" #include "ws2812b.h" #define GPIO_PORT_A ch573_gpio__gpio_port_a #define GPIO_PORT_B ch573_gpio__gpio_port_b #define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF #define UART1 ch573_uart__uart1 #define UART CH573_UART__UART_T_INTF #define PWR1 ch573_pwr__pwr_mgmt #define PWR CH573_PWR__PWR_MGMT_T_INTF uint32_t collatz(uint32_t n) { uint32_t c = 0; while (n > 1) { if (n % 2 == 0) { n /= 2; } else { n = n * 3 + 1; } c++; } return c; } static void print_reg(uint32_t reg) { // 0x40001008 printf("register @0x%08x = 0x%08x\n", reg, *(uint32_t*)reg); } static void set_system_clock_6Mhz(void) { print_reg(0x40001008); printf("Setting system clock to 6Mhz...\n"); clock_cfg_t clk_cfg = {0}; clk_cfg.sel = CLOCK_SELECTION_HSE; clk_cfg.pll_clock_divisor = 5; if (set_system_clock(&clk_cfg)) { printf("Failed to set system clock.\n"); } print_reg(0x40001008); printf("Done\n"); } static void set_system_clock_3Mhz(void) { print_reg(0x40001008); printf("Setting system clock to 3Mhz...\n"); clock_cfg_t clk_cfg = {0}; clk_cfg.sel = CLOCK_SELECTION_HSE; clk_cfg.pll_clock_divisor = 10; if (set_system_clock(&clk_cfg)) { printf("Failed to set system clock.\n"); } print_reg(0x40001008); printf("Done\n"); } static void set_system_clock_60Mhz(void) { print_reg(0x40001008); printf("Setting system clock to 60Mhz...\n"); clock_cfg_t clk_cfg = {0}; clk_cfg.sel = CLOCK_SELECTION_PLL; clk_cfg.pll_clock_divisor = 8; if (set_system_clock(&clk_cfg)) { printf("Failed to set system clock.\n"); } print_reg(0x40001008); printf("Done\n"); } static void set_system_clock_30Mhz(void) { print_reg(0x40001008); printf("Setting system clock to 30Mhz...\n"); clock_cfg_t clk_cfg = {0}; clk_cfg.sel = CLOCK_SELECTION_PLL; clk_cfg.pll_clock_divisor = 16; if (set_system_clock(&clk_cfg)) { printf("Failed to set system clock.\n"); } print_reg(0x40001008); printf("Done\n"); } static void set_system_clock_32kHz() { print_reg(0x40001008); printf("Setting system clock to 32kHz...\n"); clock_cfg_t clk_cfg = {0}; clk_cfg.sel = CLOCK_SELECTION_LSE; if (set_system_clock(&clk_cfg)) { printf("Failed to set system clock.\n"); } } static void basic_delay() { for (int i = 0; i < 100000; ++i) { asm volatile(""); } } static void fast_delay() { for (int i = 0; i < 1000; ++i) { asm volatile(""); } } #define N_LEDS 100 extern int uart1_FILE_get(FILE* f); /* * Main routine. This is called on_reset once everything else has been set up. */ int main(void) { printf("Running SPI.\n"); set_system_clock_60Mhz(); printf("What is your name? "); char buf[1024]; enable_spi(); // char buf[1024]; // memset(buf, 0xf0, sizeof(buf)); // for (int i = 0; i < 50; ++ i) { // buf[i] = 0xaa; // buf[sizeof(buf) - i] = 0xaa; // } size_t n = sizeof(buf); struct ws2812b_buf ws_buf; make_wsb2812b(&ws_buf, buf, n); rgb_t color; color.color = 0; color.r = 0xff; color.g = 0x00; color.b = 0x00; uint8_t time = 0xff; // for (size_t i = 0; i < N_LEDS; ++i) { // char* loc = buf + i * TOTAL_BYTES_PER_LED; // printf( // "Bytes: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", // loc[0], // loc[1], // loc[2], // loc[3], // loc[4], // loc[5], // loc[6], // loc[7], // loc[8], // loc[9], // loc[10]); // } GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 7); while (1) { ws_buf.cur = 0; for (int i = 0; i < N_LEDS; ++i) { color.r = time; color.g = 0; color.b = 0xff - time; write_rgb(&ws_buf, color); } time --; GPIO_PORT.out.set(GPIO_PORT_B, ON, 7); // printf("WS_BUF: %p, %zu\n", ws_buf.buf, ws_buf.cur); start_dma(&ws_buf); wait_for_dma(); basic_delay(); } return 0; }