aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 2bbd0b33eb47cc3097a660f3dad5bd34ae13c142 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include "string.h"

#include "byte_math.h"
#include "ch573/gpio.h"
#include "ch573/pwr.h"
#include "ch573/uart.h"
#include "clock.h"
#include "panic.h"
#include "spi.h"
#include "string.h"
#include "sysled.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

#define AS_BYTE(n) ((n) * 256)

#define min(a, b) (a) < (b) ? (a) : (b)

uint8_t amp(uint8_t in, uint8_t n)
{
  uint32_t out = in;

  for (int i = 0; i < n; ++i) {
    out = (out * in) / 256;
  }

  return min(out, 255);
}

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()
{
  set_sysled(1);
  for (int i = 0; i < 30000; ++i) {
    asm volatile("");
  }
  set_sysled(0);
}

static void fast_delay()
{
  for (int i = 0; i < 1000; ++i) {
    asm volatile("");
  }
}

#define N_LEDS 150
#define N_DMA_XFER 2

extern int uart1_FILE_get(FILE* f);

static inline uint8_t byte_scale(uint8_t v, uint8_t scale)
{
  uint16_t acc = v;
  return (acc * scale) >> 8;
}

uint8_t clip(int x)
{
  if (x > 240) {
    return 240;
  }

  if (x < 0) {
    return 0;
  }

  return (uint8_t)x;
}

#define TIME_STEP 1

rgb_t get_rgb(uint32_t time, size_t x)
{
  int r = 0, g = 0, b = 0;

  uint8_t time8 = time & 0xff;
  int w = calc_w(time8 * TIME_STEP + x * 4);

  r = 0xff;
  g = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), 0x90);
  b = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), 0x20);
  // r = byte_scale(byte_sin(time8 * TIME_STEP + x * 2), AS_BYTE(0.25)) +
  //     AS_BYTE(0.75);
  // b = 0x40;
  // g = byte_scale(
  //         0xff - byte_sin(time8 * TIME_STEP + x * 7 + 0x80), AS_BYTE(0.5)) +
  //     AS_BYTE(0.5);

  rgb_t color;
  color.color = 0;
  color.r = clip(r + w);
  color.g = clip(byte_scale(g, AS_BYTE(0.75)) + w);
  color.b = clip(byte_scale(b, AS_BYTE(0.75)) + w);
  return color;
}

/*
 * Main routine. This is called on_reset once everything else has been set up.
 */
int main(void)
{
  char buf[N_LEDS * TOTAL_BYTES_PER_LED];

  set_system_clock_60Mhz();
  enable_sysled();
  enable_spi();

  size_t n = sizeof(buf);
  struct ws2812b_buf ws_buf;
  make_wsb2812b(&ws_buf, buf, n);
  ws_buf.byte_order = BYTE_ORDER_RGB;

  rgb_t color;
  uint32_t time = 0;

  GPIO_PORT.dir.set(GPIO_PORT_B, DIR_OUT, 7);
  while (1) {
    // Fill the buffer with calculated rgb colors.
    ws_buf.cur = 0;
    for (int i = 0; i < N_LEDS; ++i) {
      rgb_t rgb = get_rgb(time, i);
      write_rgb(&ws_buf, rgb);
    }

    time++;
    GPIO_PORT.out.set(GPIO_PORT_B, ON, 7);
    for (int i = 0; i < N_DMA_XFER; ++i) {
      wait_for_dma();
      start_dma(&ws_buf);
    }
    basic_delay();
  }

  return 0;
}