From 9caddf6c5c0dce43a531c3dc8cf2831195ea0876 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Sun, 29 Nov 2020 21:02:37 -0700 Subject: The lights really work this time. I also found out that the noise in the lighhts was coming from the USB debug interface. For whatever reason it doesn't appear that the spi pins are not well isolated from the debugger noise so unplugging and running off one power source works pretty well. --- src/drv/ws2812B/ws2812b.c | 37 +++++++++++---------- src/kern/main.c | 84 ++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 91 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/drv/ws2812B/ws2812b.c b/src/drv/ws2812B/ws2812b.c index 2a1ccfb..0fc519a 100644 --- a/src/drv/ws2812B/ws2812b.c +++ b/src/drv/ws2812B/ws2812b.c @@ -6,24 +6,25 @@ #include "kern/panic.h" uint8_t sintable[256] = { - 128, 148, 156, 162, 168, 172, 177, 181, 184, 187, 191, 194, 196, 199, 202, - 204, 207, 209, 211, 213, 215, 217, 219, 221, 223, 224, 226, 228, 229, 231, - 232, 233, 235, 236, 237, 239, 240, 241, 242, 243, 244, 245, 246, 246, 247, - 248, 249, 249, 250, 251, 251, 252, 252, 253, 253, 253, 254, 254, 254, 255, - 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, - 253, 252, 252, 251, 251, 250, 249, 249, 248, 247, 246, 246, 245, 244, 243, - 242, 241, 240, 239, 237, 236, 235, 233, 232, 231, 229, 228, 226, 224, 223, - 221, 219, 217, 215, 213, 211, 209, 207, 204, 202, 199, 196, 194, 191, 187, - 184, 181, 177, 172, 168, 162, 156, 148, 128, 108, 100, 94, 88, 84, 79, - 75, 72, 69, 65, 62, 60, 57, 54, 52, 49, 47, 45, 43, 41, 39, - 37, 35, 33, 32, 30, 28, 27, 25, 24, 23, 21, 20, 19, 17, 16, - 15, 14, 13, 12, 11, 10, 10, 9, 8, 7, 7, 6, 5, 5, 4, - 4, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, - 7, 8, 9, 10, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, - 23, 24, 25, 27, 28, 30, 32, 33, 35, 37, 39, 41, 43, 45, 47, - 49, 52, 54, 57, 60, 62, 65, 69, 72, 75, 79, 84, 88, 94, 100, - 108}; + 128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170, + 173, 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211, + 213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240, + 241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254, + 254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251, + 250, 250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232, + 230, 228, 226, 224, 222, 220, 218, 215, 213, 211, 208, 206, 203, 201, 198, + 196, 193, 190, 188, 185, 182, 179, 176, 173, 170, 167, 165, 162, 158, 155, + 152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109, + 106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65, + 62, 59, 57, 54, 52, 49, 47, 44, 42, 40, 37, 35, 33, 31, 29, + 27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7, 6, + 5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0, + 0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11, + 12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37, + 40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76, + 79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121, + 124, +}; uint8_t byte_sin(uint8_t n) { diff --git a/src/kern/main.c b/src/kern/main.c index 5af5fa7..4acdedb 100644 --- a/src/kern/main.c +++ b/src/kern/main.c @@ -85,6 +85,14 @@ static size_t compiled_len; extern uint8_t sintable[256]; +static uint32_t time; + +void on_systick() +{ + // klogf("Systick.\n"); + ++time; +} + static void spi_write(uint8_t byte) { while (!regget(SPI1.s_r, spi_txe)) @@ -97,6 +105,8 @@ static void write_rgb(uint8_t red, uint8_t green, uint8_t blue) { #undef BIT #define BIT(b, n) (!!((b) & (1 << (n)))) + spi_write(0); + spi_write( (1 << 7) | (BIT(green, 7) << 6) | (0 << 5) | (0 << 4) | (1 << 3) | (BIT(green, 6) << 2) | (0 << 1) | (0 << 0)); @@ -135,6 +145,8 @@ static void write_rgb(uint8_t red, uint8_t green, uint8_t blue) spi_write( (1 << 7) | (BIT(blue, 1) << 6) | (0 << 5) | (0 << 4) | (1 << 3) | (BIT(blue, 0) << 2) | (0 << 1) | (0 << 0)); + + spi_write(0); } void latch() @@ -144,42 +156,90 @@ void latch() } } +static uint8_t amp(uint8_t in) +{ + uint8_t out = in; + + for (int i = 0; i < 10; ++ i) { + out = (out * in) / 256; + } + + return out; +} + /* Main function. This gets executed from the interrupt vector defined above. */ int main() { gpio_reserved_pin_t sysled = get_sysled(); + klogf("Start\n"); klogf("sintable: %p\n", sintable); klogf("sintable[5]: %d\n", sintable[5]); + klogf("Flashed with OpenOCD!\n"); + + SCB.strv_r = 1000; + + /* Enable interrupts. */ + regset(SCB.stcs_r, scb_tickint, 1); -#define SIZE 16 + /* Start the systick. */ + regset(SCB.stcs_r, scb_enable, 1); + +#define SIZE 128 rgb_t rgb[SIZE]; for (int i = 0; i < SIZE; ++i) { - rgb[i].g = 0x18; - rgb[i].r = 0x80; - rgb[i].b = 0x18; + rgb[i].g = 0xff; + rgb[i].r = 0xff; + rgb[i].b = 0xff; } - uint8_t red = 0x30; - uint8_t green = 0x30; - uint8_t blue = 0x00; + uint8_t red = 0x40; + uint8_t green = 0x40; + uint8_t blue = 0x40; // compiled = ws2812b_compile_rgb(rgb, SIZE); // compiled_len = SIZE * 9; configure_spi1(); - set_gpio_pin_high(sysled); - for (int i = 0; i < 100; ++ i) { + for (int i = 0; i < 100; ++i) { write_rgb(0, 0, 0); } latch(); - for (int i = 0; i < 256; ++i) { - write_rgb(red, byte_sin((i * 16) & 0xff) / 2, blue); + for (;;) { + set_gpio_pin_high(sysled); + + latch(); + + int i; + for (i = 0; i < SIZE; ++i) { + red = byte_sin((i * 4 * 2 + time / 1000) & 0xff) / 2; + green = byte_sin((i * 4 * 3 + time / 1000) & 0xff) / 2; + blue = amp(byte_sin((i * 2 * 3 + time / 1000) & 0xff)) / 2; + + // uint8_t red = byte_sin((i * 8 + 64 + time / 100) & 0xff); + // uint8_t green = byte_sin((i * 8 + 0 + time / 100) & 0xff); + // uint32_t blue = (((red - 128) * (green - 128)) / 256) + 128; + + write_rgb(red, green, blue); + } + + // write_rgb(0,0,0); + // write_rgb(0,0,0); + // write_rgb(0,0,0); + // write_rgb(0,0,0); + // write_rgb(0,0,0); + // write_rgb(0,0,0); + // write_rgb(0,0,0); + // write_rgb(0,0,0); + // write_rgb(red, green, blue); + + set_gpio_pin_low(sysled); + + latch(); } - set_gpio_pin_low(sysled); for (;;) { spi_write(0); -- cgit