]> mj.ucw.cz Git - home-hw.git/blob - ir-send/test.c
db52d8cc87b70fca8fbb1f7dc5ae6d4418132dfa
[home-hw.git] / ir-send / 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_TIM2);
20         rcc_periph_clock_enable(RCC_AFIO);
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 }
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         // Remap TIM2
36         // gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, AFIO_MAPR_TIM2_REMAP_FULL_REMAP);
37         AFIO_MAPR = 0x04000300;
38 }
39
40 static volatile u32 ms_ticks;
41
42 void sys_tick_handler(void)
43 {
44         ms_ticks++;
45 }
46
47 static void tick_setup(void)
48 {
49         systick_set_frequency(1000, 72000000);
50         systick_counter_enable();
51         systick_interrupt_enable();
52 }
53
54 static void delay_ms(uint ms)
55 {
56         u32 start_ticks = ms_ticks;
57         while (ms_ticks - start_ticks < ms)
58                 ;
59 }
60
61 static void usart_setup(void)
62 {
63         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
64
65         usart_set_baudrate(USART1, 115200);
66         usart_set_databits(USART1, 8);
67         usart_set_stopbits(USART1, USART_STOPBITS_1);
68         usart_set_mode(USART1, USART_MODE_TX_RX);
69         usart_set_parity(USART1, USART_PARITY_NONE);
70         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
71
72         usart_enable(USART1);
73 }
74
75 int main(void)
76 {
77         clock_setup();
78         gpio_setup();
79         tick_setup();
80         usart_setup();
81
82         // cm_enable_interrupts();
83         debug_puts("Hello!\n");
84
85 #if 1
86         timer_set_prescaler(TIM2, 3);
87         timer_set_mode(TIM2, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
88         timer_disable_preload(TIM2);
89         timer_set_period(TIM2, 65535);
90         timer_set_oc_mode(TIM2, TIM_OC3, TIM_OCM_PWM1);
91         timer_set_oc_value(TIM2, TIM_OC3, 32768);
92         timer_set_oc_polarity_high(TIM2, TIM_OC3);
93         timer_enable_counter(TIM2);
94         timer_enable_oc_output(TIM2, TIM_OC3);
95 #endif
96
97         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO10);
98         // gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO10);
99         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
100
101         for (;;) {
102                 gpio_toggle(GPIOC, GPIO13);
103                 delay_ms(20);
104                 gpio_toggle(GPIOA, GPIO15);
105                 debug_putc('.');
106                 timer_set_oc_mode(TIM2, TIM_OC3, TIM_OCM_FORCE_LOW);
107                 delay_ms(20);
108                 timer_set_oc_mode(TIM2, TIM_OC3, TIM_OCM_FORCE_HIGH);
109         }
110
111         return 0;
112 }