summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2020-08-30 12:17:33 +0200
committerjaseg <git@jaseg.net>2020-08-30 12:17:33 +0200
commitbfd8db0098413474514eab4dfebeb0803ad94fc4 (patch)
tree34bea46c06def022c6197e1a3b760620e1517d84
parente51d35f6d6d15ee09dd368b5c47c547f8646bdf5 (diff)
downloadmoargb-bfd8db0098413474514eab4dfebeb0803ad94fc4.tar.gz
moargb-bfd8db0098413474514eab4dfebeb0803ad94fc4.tar.bz2
moargb-bfd8db0098413474514eab4dfebeb0803ad94fc4.zip
Dimming worksHEADmaster
-rw-r--r--Makefile9
-rw-r--r--color.c40
-rw-r--r--color.h14
-rw-r--r--main.c120
4 files changed, 157 insertions, 26 deletions
diff --git a/Makefile b/Makefile
index 4cf97a7..b56e124 100644
--- a/Makefile
+++ b/Makefile
@@ -33,8 +33,7 @@ LDFLAGS = -nostdlib
#LDFLAGS += -specs=rdimon.specs -DSEMIHOSTING
LDFLAGS += -Wl,-Map=main.map
#LDFLAGS += -Wl,--gc-sections
-LIBS =
-#LIBS = -lgcc
+LIBS = -lm -lgcc
#LIBS += -lrdimon
CFLAGS += -DSTM32F103xB -DHSE_VALUE=8000000 -DLSE_VALUE=32768
@@ -42,7 +41,7 @@ CFLAGS += -DCOMPILE_TIME=$(shell date +%s) -DTARGET_DATE=$(shell date -d 'Aug 17
LDFLAGS += -Tstm32_flash.ld
CFLAGS += -I$(CMSIS_DEV_PATH)/Include -I$(CMSIS_PATH)/Include -I$(HAL_PATH)/Inc -Iconfig -Wno-unused -I../common
-#LDFLAGS += -L$(CMSIS_PATH)/Lib/GCC -larm_cortexM3l_math
+LIBS += -L$(CMSIS_PATH)/Lib/GCC -larm_cortexM3l_math
###################################################
@@ -61,7 +60,7 @@ cmsis_exports.c: $(CMSIS_DEV_PATH)/Include/stm32f103xb.h $(CMSIS_PATH)/Include/c
$(CC) -c $(CFLAGS) -o $@ $^
# $(CC) -E $(CFLAGS) -o $(@:.o=.pp) $^
-sources.tar.xz: main.c Makefile
+sources.tar.xz: main.c color.c Makefile
tar -caf $@ $^
# don't ask...
@@ -71,7 +70,7 @@ sources.tar.xz.zip: sources.tar.xz
sources.c: sources.tar.xz.zip
xxd -i $< | head -n -1 | sed 's/=/__attribute__((section(".source_tarball"))) =/' > $@
-main.elf: main.c startup_stm32f103xb.s system_stm32f1xx.c $(HAL_PATH)/Src/stm32f1xx_ll_utils.c base.c cmsis_exports.c
+main.elf: main.c color.c startup_stm32f103xb.s system_stm32f1xx.c $(HAL_PATH)/Src/stm32f1xx_ll_utils.c base.c cmsis_exports.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
$(OBJCOPY) -O ihex $@ $(@:.elf=.hex)
$(OBJCOPY) -O binary $@ $(@:.elf=.bin)
diff --git a/color.c b/color.c
new file mode 100644
index 0000000..9522fd5
--- /dev/null
+++ b/color.c
@@ -0,0 +1,40 @@
+
+#include <math.h>
+
+#include "color.h"
+
+
+void hsv_to_rgb(struct hsvf *hsv, struct rgbf *rgb) {
+ float h = hsv->h, s = hsv->s, v = hsv->v;
+ float c = v * s; // Chroma
+ float hmod = fmodf(h * 6.0f, 6.0f);
+ float x = c * (1.0f - fabsf(fmodf(hmod, 2.0f) - 1.0f));
+ float m = v - c;
+
+ switch ((int)hmod) {
+ case 0:
+ rgb->r = c; rgb->g = x; rgb->b = 0;
+ break;
+ case 1:
+ rgb->r = x; rgb->g = c; rgb->b = 0;
+ break;
+ case 2:
+ rgb->r = 0; rgb->g = c; rgb->b = x;
+ break;
+ case 3:
+ rgb->r = 0; rgb->g = x; rgb->b = c;
+ break;
+ case 4:
+ rgb->r = x; rgb->g = 0; rgb->b = c;
+ break;
+ case 5:
+ rgb->r = c; rgb->g = 0; rgb->b = x;
+ break;
+ default:
+ rgb->r = 0; rgb->g = 0; rgb->b = 0;
+ }
+
+ rgb->r += m;
+ rgb->g += m;
+ rgb->b += m;
+}
diff --git a/color.h b/color.h
new file mode 100644
index 0000000..07f46d3
--- /dev/null
+++ b/color.h
@@ -0,0 +1,14 @@
+#ifndef __COLOR_H__
+#define __COLOR_H__
+
+struct hsvf {
+ float h, s, v;
+};
+
+struct rgbf {
+ float r, g, b;
+};
+
+void hsv_to_rgb(struct hsvf *hsv_in, struct rgbf *rgb_out);
+
+#endif /* __COLOR_H__ */
diff --git a/main.c b/main.c
index c1f2f41..313bb7c 100644
--- a/main.c
+++ b/main.c
@@ -16,6 +16,18 @@
*/
#include "global.h"
+#include "math.h"
+#include "color.h"
+
+static struct hsvf g_color_s = {0.0f, 0.0f, 0.0f};
+struct hsvf *g_color = &g_color_s;
+volatile uint64_t g_time_ms = 0;
+
+uint64_t wait_until(uint64_t timestamp);
+bool check_interval(uint64_t *timestamp, uint64_t interval);
+void blink_led(uint64_t *ts, int t_on, int t_off, GPIO_TypeDef *gpio, int pin);
+
+void update_timers(struct rgbf *rgb);
uint32_t pcg32_random_r() {
// *Really* minimal PCG32 code / (c) 2014 M.E. O'Neill / pcg-random.org
@@ -34,38 +46,27 @@ uint32_t pcg32_random_r() {
int main(void){
/* We're starting out from HSI@8MHz */
SystemCoreClockUpdate();
- SCB->SCR &= (~SCB_SCR_SLEEPONEXIT_Msk) & (~SCB_SCR_SLEEPDEEP_Msk); /* Disable for now */
- for (int i=0; i<50000; i++)
- asm volatile ("nop");
+ SysTick_Config(SystemCoreClock / 1000); /* 1ms tick */
/* Turn on lots of neat things */
- RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN;
+ RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_AFIOEN | RCC_APB2ENR_TIM1EN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN | RCC_APB1ENR_TIM4EN;
GPIOC->CRH |=
(0<<GPIO_CRH_CNF13_Pos) | (2<<GPIO_CRH_MODE13_Pos); /* PC13 - LED */
GPIOB->CRL |=
- (2<<GPIO_CRL_CNF5_Pos) | (2<<GPIO_CRL_MODE5_Pos); /* PB5 - TIM3_CH2 */
+ (2<<GPIO_CRL_CNF5_Pos) | (2<<GPIO_CRL_MODE5_Pos); /* PB5 - TIM3_CH2 (r fractional) */
AFIO->MAPR |= (2 << AFIO_MAPR_TIM3_REMAP_Pos); /* Map TIM3_CH2 to PB5 */
GPIOB->CRH |=
- (2<<GPIO_CRH_CNF8_Pos) | (2<<GPIO_CRH_MODE8_Pos) /* PB8 - TIM4_CH3 */
- | (2<<GPIO_CRH_CNF9_Pos) | (2<<GPIO_CRH_MODE9_Pos); /* PB9 - TIM4_CH4 */
+ (2<<GPIO_CRH_CNF8_Pos) | (2<<GPIO_CRH_MODE8_Pos) /* PB8 - TIM4_CH3 (g fractional) */
+ | (2<<GPIO_CRH_CNF9_Pos) | (2<<GPIO_CRH_MODE9_Pos); /* PB9 - TIM4_CH4 (b fractional) */
GPIOC->ODR |= 1<<13; /* LED */
- TIM3->SMCR = (3<<TIM_SMCR_TS_Pos) | (6 << TIM_SMCR_SMS_Pos);
- TIM4->CR2 = (4<<TIM_CR2_MMS_Pos);
-
- int period = 0xffff;
- int thr[3] = {20000, 30000};
+ GPIOA->CRH = (2<<GPIO_CRH_CNF8_Pos) | (2<<GPIO_CRH_MODE8_Pos); /* PA8 - TIM1_CH1 (global dimming) */
- int overlap = 1000;
- TIM4->CCR3 = thr[0];
- TIM4->CCR4 = thr[1];
- TIM4->CCR1 = thr[0];
- TIM3->CCR2 = thr[1] - thr[0];
- TIM3->ARR = 0xfffe;
- TIM4->ARR = 0xffff;
+ TIM3->SMCR = (3<<TIM_SMCR_TS_Pos) | (4 << TIM_SMCR_SMS_Pos);
+ TIM4->CR2 = (4<<TIM_CR2_MMS_Pos);
TIM3->CCER = TIM_CCER_CC2E;
TIM3->CCMR1 = (0<<TIM_CCMR1_CC2S_Pos) | TIM_CCMR1_OC2PE | (6<<TIM_CCMR1_OC2M_Pos);
@@ -74,13 +75,59 @@ int main(void){
TIM4->CCMR2 = (0<<TIM_CCMR2_CC4S_Pos) | TIM_CCMR2_OC4PE | (6<<TIM_CCMR2_OC4M_Pos) \
| (0<<TIM_CCMR2_CC3S_Pos) | TIM_CCMR2_OC3PE | (7<<TIM_CCMR2_OC3M_Pos);
- TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_OPM | TIM_CR1_OPM;
+ TIM3->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
TIM4->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
+ TIM4->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
+
+ TIM1->CCMR1 = (0<<TIM_CCMR1_CC1S_Pos) | TIM_CCMR1_OC1PE | (6<<TIM_CCMR1_OC1M_Pos);
+ TIM1->SMCR = (3 << TIM_SMCR_TS_Pos) | (4 << TIM_SMCR_SMS_Pos);
+ TIM1->CCER = TIM_CCER_CC1E;
+ TIM1->ARR = 0xffff;
+ TIM1->BDTR = TIM_BDTR_MOE;
+ TIM1->CR1 = TIM_CR1_ARPE | TIM_CR1_CEN;
+ uint64_t ts = g_time_ms;
+ uint64_t led_ts = 0;
for (;;) {
+ g_color->h = fmodf(g_color->h + 0.0005, 1.0f);
+ //g_color->h = 0.0;
+ g_color->s = 0.8;
+ g_color->v = 1.0;
+
+ struct rgbf rgb;
+ hsv_to_rgb(g_color, &rgb);
+ update_timers(&rgb);
+
+ ts = wait_until(ts + 5);
+
+ blink_led(&led_ts, 100, 200, GPIOC, 13);
}
}
+void update_timers(struct rgbf *rgb) {
+ float gamma = 2.2f;
+ rgb->r = powf(rgb->r, gamma);
+ rgb->g = powf(rgb->g, gamma);
+ rgb->b = powf(rgb->b, gamma);
+
+ float total = rgb->r + rgb->g + rgb->b;
+ rgb->r /= total;
+ rgb->g /= total;
+ rgb->b /= total;
+
+ float period = 0xffff;
+ int thr[2] = {roundf(period * rgb->r), roundf(period * (rgb->r + rgb->g))};
+
+ TIM4->CCR3 = thr[0];
+ TIM4->CCR4 = thr[1];
+ TIM4->CCR1 = thr[0];
+ TIM3->CCR2 = thr[1] - thr[0];
+ TIM3->ARR = period;
+ TIM4->ARR = period;
+
+ TIM1->CCR1 = roundf((period - 1) * total / 3.0f);
+}
+
void gdb_dump(void) {
/* debugger hook */
}
@@ -104,6 +151,37 @@ void PendSV_Handler(void) {
}
void SysTick_Handler(void) {
- asm volatile ("bkpt");
+ g_time_ms ++;
+}
+
+uint64_t wait_until(uint64_t timestamp) {
+ while (g_time_ms < timestamp)
+ ;
+ return g_time_ms;
+}
+
+bool check_interval(uint64_t *timestamp, uint64_t interval) {
+ if (*timestamp == 0) {
+ *timestamp = g_time_ms;
+ return false;
+ }
+
+ if (g_time_ms < *timestamp + interval)
+ return false;
+
+ *timestamp = g_time_ms;
+ return true;
+}
+
+void blink_led(uint64_t *ts, int t_on, int t_off, GPIO_TypeDef *gpio, int pin) {
+ int bm = 1<<pin;
+ if (gpio->ODR & bm) {
+ if (check_interval(ts, t_off))
+ gpio->BRR = bm;
+
+ } else {
+ if (check_interval(ts, t_on))
+ gpio->BSRR = bm;
+ }
}