First test code for SK6812 LEDs

This commit is contained in:
Mario Hüttel 2021-03-12 22:28:00 +01:00
parent dbc25dcead
commit f2183c5dac
4 changed files with 95 additions and 11 deletions

View File

@ -12,7 +12,7 @@ endif
#Add Files and Folders below#########################################################
CFILES = main.c syscalls/syscalls.c setup/system_init.c startup/startup_stm32f0xx.c
ASFILES =
ASFILES = sk6812.S
INCLUDEPATH = -Iinclude -Iinclude/cmsis
OBJDIR=obj
@ -39,7 +39,7 @@ LFLAGS += -mfloat-abi=soft --disable-newlib-supplied-syscalls -nostartfiles
LFLAGS += -Tstartup/stm32f030.ld -Wl,-Map=$(mapfile).map -Wl,--gc-sections -g
CFLAGS = -c -fmessage-length=0 -mlittle-endian -mthumb -mcpu=cortex-m0 -mthumb-interwork
CFLAGS += -mfloat-abi=soft -nostartfiles -Wall -g
CFLAGS += -mfloat-abi=soft -nostartfiles -Wall -g -O3
####################################################################################

View File

@ -1,19 +1,32 @@
#include <stm32f0xx.h>
#include <cmsis/core_cm0.h>
unsigned int i = 0x12345678;
unsigned char c = 2;
int main(void) {
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
GPIOB->MODER |= (1<<1*2);
GPIOB->ODR |= (1<<1);
extern void sk6812_send_led(uint32_t rgbw);
volatile uint32_t wait_tick = 0;
static void wait_for_ticks(uint32_t ticks)
{
wait_tick = 0;
while (wait_tick < ticks);
}
int main(void)
{
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
GPIOA->MODER |= (1<<3*2);
SysTick_Config(800000);
while(1) {
i++;
__disable_irq();
sk6812_send_led(0x000000FF);
__enable_irq();
wait_for_ticks(30);
}
}
void SysTick_Handler(void) {
GPIOB->ODR ^= (1<<1);
wait_tick++;
}

View File

@ -61,10 +61,34 @@ static void __init_default_clocks(void)
RCC->CIR = 0x00000000;
}
void __setup_clocks(void)
{
uint32_t tmp;
/* TODO: Switch HSE on for better accuracy */
/* Switch PLL source to HSI OSC */
RCC->CFGR &= ~RCC_CFGR_PLLSRC;
/* Set PLL multiplication to 12 (4 MHz * 12 = 48 MHz SysClk) */
RCC->CFGR |= RCC_CFGR_PLLMUL_3 | RCC_CFGR_PLLMUL_1;
/* HSI Already running. Switch on PLL */
RCC->CR |= RCC_CR_PLLON;
/* Wait for PLL to be ready */
while (!(RCC->CR & RCC_CR_PLLRDY));
/* Switch System Clock to PLL */
tmp = RCC->CFGR;
tmp &= ~0x3;
tmp |= RCC_CFGR_SW_1;
RCC->CFGR = tmp;
}
void __system_init(void)
{
__init_default_clocks();
__setup_clocks();
}

47
firmware/sk6812.S Normal file
View File

@ -0,0 +1,47 @@
.global sk6812_send_led
.equ BSRR_REGISTER, 0x48000018
.equ PINNUM, 3
.syntax unified
sk6812_send_led:
push {lr}
push {r4, r5, r6}
ldr r1, =BSRR_REGISTER
ldr r2, =(1<<PINNUM)
ldr r3, =(1<<(PINNUM+16))
ldr r4, =32
ldr r5, =0x80000000
_bitloop:
tst r0, r5
beq sk6812_send_zero
bne sk6812_send_one
_bitloop_ret:
lsls r0, r0, #1
subs r4, r4, #1
bne _bitloop
pop {r4, r5, r6}
pop {pc}
sk6812_send_one:
str r2, [r1]
ldr r6, =0x5
bl wait_r6
str r3, [r1]
ldr r6, =0x2
bl wait_r6
b _bitloop_ret
sk6812_send_zero:
str r2, [r1]
ldr r6, =0x1
bl wait_r6
str r3, [r1]
ldr r6, =0x5
bl wait_r6
b _bitloop_ret
wait_r6:
subs r6, r6, #1
bne wait_r6
bx lr