]> mj.ucw.cz Git - home-hw.git/blob - test-pwm/test.c
bsb-monitor: Fix comment
[home-hw.git] / test-pwm / test.c
1 #include "util.h"
2
3 #include <libopencm3/cm3/cortex.h>
4 #include <libopencm3/cm3/nvic.h>
5 #include <libopencm3/cm3/systick.h>
6 #include <libopencm3/stm32/rcc.h>
7 #include <libopencm3/stm32/gpio.h>
8 #include <libopencm3/stm32/usart.h>
9 #include <libopencm3/stm32/timer.h>
10
11 static void clock_setup(void)
12 {
13         rcc_clock_setup_in_hse_8mhz_out_72mhz();
14
15         rcc_periph_clock_enable(RCC_GPIOA);
16         rcc_periph_clock_enable(RCC_GPIOB);
17         rcc_periph_clock_enable(RCC_GPIOC);
18         rcc_periph_clock_enable(RCC_USART1);
19         rcc_periph_clock_enable(RCC_TIM4);
20
21         rcc_periph_reset_pulse(RST_GPIOA);
22         rcc_periph_reset_pulse(RST_GPIOB);
23         rcc_periph_reset_pulse(RST_GPIOC);
24         rcc_periph_reset_pulse(RST_USART1);
25         rcc_periph_reset_pulse(RST_TIM2);
26         rcc_periph_reset_pulse(RST_TIM4);
27 }
28
29 static void gpio_setup(void)
30 {
31         // PC13 = BluePill LED
32         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
33         gpio_clear(GPIOC, GPIO13);
34
35         // Pins for MODBUS USART
36         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
37         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
38         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
39 }
40
41 static volatile u32 ms_ticks;
42
43 void sys_tick_handler(void)
44 {
45         ms_ticks++;
46 }
47
48 static void tick_setup(void)
49 {
50         systick_set_frequency(1000, 72000000);
51         systick_counter_enable();
52         systick_interrupt_enable();
53 }
54
55 static void delay_ms(uint ms)
56 {
57         u32 start_ticks = ms_ticks;
58         while (ms_ticks - start_ticks < ms)
59                 ;
60 }
61
62 static void usart_setup(void)
63 {
64         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
65
66         usart_set_baudrate(USART1, 9600);
67         usart_set_databits(USART1, 8);
68         usart_set_stopbits(USART1, USART_STOPBITS_1);
69         usart_set_mode(USART1, USART_MODE_TX_RX);
70         usart_set_parity(USART1, USART_PARITY_NONE);
71         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
72
73         usart_enable(USART1);
74 }
75
76 int main(void)
77 {
78         clock_setup();
79         gpio_setup();
80         tick_setup();
81         usart_setup();
82
83         // cm_enable_interrupts();
84
85         timer_set_prescaler(TIM4, 3);   // clock = 72 MHz / 2 = 36 MHz
86         timer_set_mode(TIM4, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
87         timer_disable_preload(TIM4);
88         timer_set_period(TIM4, 255);    // PWM frequency = 18 MHz / 256 = 70.3125 kHz   FIXME
89         timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_PWM1);
90         timer_set_oc_value(TIM4, TIM_OC1, 24);
91         /*
92          *      1       0
93          *      4       0.6
94          *      8       1.6
95          *      12      2.64
96          *      16      3.52
97          *      24      6.16
98          *      32      7.2
99          *      64      7.8
100          *      128     7.8
101          *      255     8
102          */
103         timer_set_oc_polarity_high(TIM4, TIM_OC1);
104         timer_enable_counter(TIM4);
105         timer_enable_oc_output(TIM4, TIM_OC1);
106
107         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO6);
108         // gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
109
110         for (;;) {
111                 gpio_toggle(GPIOC, GPIO13);
112                 delay_ms(50);
113                 // gpio_toggle(GPIOA, GPIO8);
114                 //timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_FORCE_LOW);
115                 //delay_ms(50);
116                 //timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_FORCE_HIGH);
117         }
118
119         return 0;
120 }