diff --git a/init.c b/init.c index 52c35ac..26e765f 100644 --- a/init.c +++ b/init.c @@ -61,7 +61,10 @@ void init_usart(void) void init_systick(void) { - SysTick_CSR = (SysTick_CSR & ~SysTick_CSR_CLKSOURCE) | SysTick_CSR_CLKSOURCE_CORE; + //SysTick_CSR = (SysTick_CSR & ~SysTick_CSR_CLKSOURCE) | (1 << __CTZ(SysTick_CSR_CLKSOURCE)); + + patch_register(SysTick_CSR, SysTick_CSR_CLKSOURCE, 1); // 1 - core, 0 - div 8 + SysTick_RELOAD = 16000; // 1ms interrupt @ 16MHz core clock SysTick_CSR |= SysTick_CSR_TICKINT | SysTick_CSR_ENABLE; } @@ -103,6 +106,8 @@ void init_dac(void) gpio_set_mode(GPIOA, BIT4, MODER_ANALOG); // PA4 - DAC CH1 out DAC_CR |= DAC_CR_EN1; // enable first channel + + DAC_DHR12R1 = 0; // reset value } @@ -114,14 +119,14 @@ void init_pwm1(void) // using timer 3, channel 1 gpio_set_af(GPIOA, BIT6, AF2); - patch_register(&TIM3_CCMR1, TIM_CCMR1_OC1M, TIM_OCM_PWM1); // set PWM1 mode + patch_register(TIM3_CCMR1, TIM_CCMR1_OC1M, TIM_OCM_PWM1); // set PWM1 mode TIM3_CCMR1 |= TIM_CCMR1_OC1PE; // preload enable TIM3_CR1 |= TIM_CR1_ARPE; // auto reload is buffered TIM3_CCER |= TIM_CCER_CC1E; // enable output compare (PWM output) - patch_register(&TIM3_CR1, TIM_CR1_CMS, TIM_CMS_EDGE); // centering mode - patch_register(&TIM3_CR1, TIM_CR1_DIR, 0); // count upwards only + patch_register(TIM3_CR1, TIM_CR1_CMS, TIM_CMS_EDGE); // centering mode + patch_register(TIM3_CR1, TIM_CR1_DIR, 0); // count upwards only // frequency set to 16 kHz diff --git a/lib/common.c b/lib/common.c index 0dd7ad0..a5a42c6 100644 --- a/lib/common.c +++ b/lib/common.c @@ -10,9 +10,12 @@ inline uint32_t __RBIT(uint32_t value) } +// implemented as a macro for more efficient code + +/* __attribute__((always_inline)) inline void patch_register(io32_t reg, uint32_t mask, uint32_t replacement) { - *reg &= ~mask; - *reg |= replacement << __CTZ(mask); + *reg = (*reg & ~mask) | (replacement << __CTZ(mask)); } +*/ diff --git a/lib/common.h b/lib/common.h index d157e6a..1bf1f33 100644 --- a/lib/common.h +++ b/lib/common.h @@ -87,8 +87,15 @@ uint32_t __RBIT(uint32_t value); // defined in c file as inline asm #define BIT31 BIT(31) -void patch_register(io32_t reg, uint32_t mask, uint32_t replacement); +// reg - MMIO32 +// mask - uint32_t +// replacement - uint32_t (value that goes in place of mask, right-aligned) +// +// example: original value 0x12345678, patch_register(reg, 0xF00, 0xC) -> 0x12345C78 +#define patch_register(reg, mask, replacement) (reg) = ((reg) & ~(mask)) | ((replacement) << __CTZ(mask)) + +//void patch_register(io32_t reg, uint32_t mask, uint32_t replacement); #include "defs_base.h" diff --git a/main.c b/main.c index c020ac6..6cd7387 100644 --- a/main.c +++ b/main.c @@ -15,6 +15,8 @@ static bool gate_closed = false; static uint32_t gate_cnt = 0; +float exposure_out, exposure_accum; +uint32_t exposure_cnt; /** IRQ */ void USART2_IRQHandler(void) @@ -99,15 +101,31 @@ int main(void) char buf[200]; usart_tx_string(USART2, "DAQ system started.\n"); + delay_ms(100); + bool first = true; while (1) { - delay_ms(200); - float cels = measure_temp(); float angle = measure_angle(); float resis = measure_resistance(); float expos = measure_exposure(); + // --- exposure averaging + exposure_accum += expos; + exposure_cnt++; + + if (exposure_cnt >= 5) { + exposure_out = exposure_accum / exposure_cnt; + exposure_accum = 0; + exposure_cnt = 0; + } + + if (first) { + exposure_out = expos; // pretend it's averaged + } + // --- + + buf_reset(buf); buf_append_str(buf, "T "); buf_append_flt(buf, cels, 1); @@ -122,7 +140,7 @@ int main(void) buf_append_str(buf, " | "); buf_append_str(buf, "L "); - buf_append_flt(buf, expos, 1); + buf_append_flt(buf, exposure_out, 1); buf_append_str(buf, "% | "); buf_append_str(buf, "G "); @@ -141,5 +159,8 @@ int main(void) buf_append_str(buf, "\n"); usart_tx_string(USART2, buf); + + delay_ms(100); + first = false; } }