]> mj.ucw.cz Git - home-hw.git/commitdiff
Waiting room: Proper display timing
authorMartin Mares <mj@ucw.cz>
Fri, 18 Apr 2025 19:56:39 +0000 (21:56 +0200)
committerMartin Mares <mj@ucw.cz>
Fri, 18 Apr 2025 19:56:39 +0000 (21:56 +0200)
waiting/README
waiting/firmware/main.c

index 5295125feff6f8e725022ffe0360c698d59434fc..9c47db33051cbcdd8f3f706f3e52aba20e9b4b1a 100644 (file)
@@ -2,6 +2,7 @@ Assignment of peripherals and pins
 ==================================
 
 USART1 debugging
+TIM1   microsecond timing
 TIM4   Neopixel control
 DMA1   Neopixel control
 I2C2   display 1602: HD44780U controller via PCF8574 expander, needs pull-up to 5V
index ac9b97915d9493a00a1e7383d79dd659e682b8d5..d1f76815ddc3b8206165d2a293c5ded238726c2b 100644 (file)
@@ -33,6 +33,7 @@ static void clock_init(void)
        rcc_periph_clock_enable(RCC_GPIOB);
        rcc_periph_clock_enable(RCC_GPIOC);
        rcc_periph_clock_enable(RCC_USART1);
+       rcc_periph_clock_enable(RCC_TIM1);
        // rcc_periph_clock_enable(RCC_TIM4);
        // rcc_periph_clock_enable(RCC_DMA1);
        rcc_periph_clock_enable(RCC_I2C2);
@@ -41,6 +42,7 @@ static void clock_init(void)
        rcc_periph_reset_pulse(RST_GPIOB);
        rcc_periph_reset_pulse(RST_GPIOC);
        rcc_periph_reset_pulse(RST_USART1);
+       rcc_periph_reset_pulse(RST_TIM1);
        // rcc_periph_reset_pulse(RST_TIM4);
        rcc_periph_reset_pulse(RST_I2C2);
 }
@@ -98,6 +100,26 @@ static void delay_ms(uint ms)
                ;
 }
 
+/*** Microsecond delays via TIM1 ***/
+
+static void delay_init(void)
+{
+       timer_set_prescaler(TIM1, CPU_CLOCK_MHZ - 1);   // 1 MHz
+       timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
+       timer_update_on_overflow(TIM1);
+       timer_disable_preload(TIM1);
+       timer_one_shot_mode(TIM1);
+}
+
+static void delay_us(uint us)
+{
+       timer_set_period(TIM1, us - 1);
+       timer_generate_event(TIM1, TIM_EGR_UG);
+       timer_enable_counter(TIM1);
+       while (TIM_CR1(TIM1) & TIM_CR1_CEN)
+               ;
+}
+
 /*** Display ***/
 
 /*
@@ -174,9 +196,9 @@ static void display_write_nibble(byte value)
        // Sends a nibble of data with a combination of control signals
        display_pcf_write(value);
        display_pcf_write(value | LCD_BIT_E);
-       delay_ms(1);    // FIXME
+       delay_us(50);
        display_pcf_write(value);
-       delay_ms(1);    // FIXME
+       delay_us(50);
 }
 
 static void display_command(byte cmd)
@@ -542,6 +564,7 @@ int main(void)
        gpio_init();
        usart_init();
        tick_init();
+       delay_init();
        display_init();
        // neopixel_init();
        usb_init();