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