]> mj.ucw.cz Git - home-hw.git/blobdiff - test-opencm3/test.c
Merge branch 'master' of ssh://git.ucw.cz/home/mj/GIT/home-hw
[home-hw.git] / test-opencm3 / test.c
index e49bce74eeccaeec3a6875413ed42609e8f24c01..b4e9e707c53c2237fb82137681237a7438a115f0 100644 (file)
@@ -5,6 +5,7 @@
 #include <libopencm3/cm3/systick.h>
 #include <libopencm3/stm32/rcc.h>
 #include <libopencm3/stm32/gpio.h>
 #include <libopencm3/cm3/systick.h>
 #include <libopencm3/stm32/rcc.h>
 #include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/timer.h>
 #include <libopencm3/stm32/usart.h>
 
 static void clock_setup(void)
 #include <libopencm3/stm32/usart.h>
 
 static void clock_setup(void)
@@ -12,9 +13,11 @@ static void clock_setup(void)
        rcc_clock_setup_in_hse_8mhz_out_72mhz();
 
        rcc_periph_clock_enable(RCC_GPIOA);
        rcc_clock_setup_in_hse_8mhz_out_72mhz();
 
        rcc_periph_clock_enable(RCC_GPIOA);
+       rcc_periph_clock_enable(RCC_GPIOB);
        rcc_periph_clock_enable(RCC_GPIOC);
        rcc_periph_clock_enable(RCC_USART1);
        rcc_periph_clock_enable(RCC_TIM3);
        rcc_periph_clock_enable(RCC_GPIOC);
        rcc_periph_clock_enable(RCC_USART1);
        rcc_periph_clock_enable(RCC_TIM3);
+       rcc_periph_clock_enable(RCC_TIM4);
        rcc_periph_clock_enable(RCC_DMA1);
 }
 
        rcc_periph_clock_enable(RCC_DMA1);
 }
 
@@ -69,6 +72,7 @@ static void usart_setup(void)
 }
 
 static bool bypass_active;
 }
 
 static bool bypass_active;
+static byte pwm;
 
 static void show_temperature(void)
 {
 
 static void show_temperature(void)
 {
@@ -81,17 +85,46 @@ static void show_temperature(void)
                else
                        debug_printf("%3d.%03d", t / 1000, t % 1000);
        }
                else
                        debug_printf("%3d.%03d", t / 1000, t % 1000);
        }
-       debug_putc(' ');
-       debug_putc('0' + bypass_active);
+       debug_printf(" %d", bypass_active);
+       debug_printf(" %d", pwm);
        debug_puts("\r\n");
 }
 
        debug_puts("\r\n");
 }
 
+static void pwm_init(void)
+{
+       timer_set_prescaler(TIM4, 3);   // clock = 72 MHz / 2 = 36 MHz
+       timer_set_mode(TIM4, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
+       timer_disable_preload(TIM4);
+       timer_set_period(TIM4, 255);    // PWM frequency = 18 MHz / 256 = 70.3125 kHz   FIXME
+       timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_PWM1);
+       timer_set_oc_value(TIM4, TIM_OC1, 1);
+       pwm = 1;
+       /*
+        *      1       0.03
+        *      4       0.48
+        *      8       1.54
+        *      12      2.71
+        *      16      3.99
+        *      24      6.34
+        *      32      7.85
+        *      64      7.95
+        *      128     8.02
+        *      255     
+        */
+       timer_set_oc_polarity_high(TIM4, TIM_OC1);
+       timer_enable_counter(TIM4);
+       timer_enable_oc_output(TIM4, TIM_OC1);
+
+       gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO6);
+}
+
 int main(void)
 {
        clock_setup();
        gpio_setup();
        tick_setup();
        usart_setup();
 int main(void)
 {
        clock_setup();
        gpio_setup();
        tick_setup();
        usart_setup();
+       pwm_init();
 
        ds_init();
 
 
        ds_init();
 
@@ -110,6 +143,24 @@ int main(void)
                                bypass_active = 0;
                                gpio_clear(GPIOC, GPIO15);      // opto-coupler
                                gpio_set(GPIOC, GPIO14);        // LED
                                bypass_active = 0;
                                gpio_clear(GPIOC, GPIO15);      // opto-coupler
                                gpio_set(GPIOC, GPIO14);        // LED
+                       } else if (ch >= '0' && ch <= '9') {
+                               pwm = 3*(ch - '0') + 1;
+                               /*
+                                *      ch      pwm       %
+                                *      0         1       0
+                                *      1         4       0
+                                *      2         7      18
+                                *      3        10      31
+                                *      4        13      44
+                                *      5        16      57
+                                *      6        19      71
+                                *      7        22      84
+                                *      8        25      97
+                                *      9        28     100
+                                *
+                                *      % = pwm*4.389 - 12.723
+                                */
+                               timer_set_oc_value(TIM4, TIM_OC1, pwm);
                        }
                }
                if (cycles++ >= 50) {
                        }
                }
                if (cycles++ >= 50) {