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/timer.h>
9 #include <libopencm3/stm32/usart.h>
11 static void clock_setup(void)
13 rcc_clock_setup_in_hse_8mhz_out_72mhz();
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_TIM3);
20 rcc_periph_clock_enable(RCC_TIM4);
21 rcc_periph_clock_enable(RCC_DMA1);
24 static void gpio_setup(void)
26 // PC13 = BluePill LED
27 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
28 gpio_clear(GPIOC, GPIO13);
31 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO14);
32 gpio_set(GPIOC, GPIO14);
34 // PC15 = bypass opto-coupler
35 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
36 gpio_clear(GPIOC, GPIO15);
39 static volatile u32 ms_ticks;
41 void sys_tick_handler(void)
46 static void tick_setup(void)
48 systick_set_frequency(1000, 72000000);
49 systick_counter_enable();
50 systick_interrupt_enable();
53 static void delay_ms(uint ms)
55 u32 start_ticks = ms_ticks;
56 while (ms_ticks - start_ticks < ms)
60 static void usart_setup(void)
62 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
64 usart_set_baudrate(USART1, 115200);
65 usart_set_databits(USART1, 8);
66 usart_set_stopbits(USART1, USART_STOPBITS_1);
67 usart_set_mode(USART1, USART_MODE_TX_RX);
68 usart_set_parity(USART1, USART_PARITY_NONE);
69 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
74 static bool bypass_active;
77 static void show_temperature(void)
80 for (uint i=0; ds_sensors[i].address[0]; i++) {
82 int t = ds_sensors[i].current_temp;
83 if (t == DS_TEMP_UNKNOWN)
84 debug_puts("---.---");
86 debug_printf("%3d.%03d", t / 1000, t % 1000);
88 debug_printf(" %d", bypass_active);
89 debug_printf(" %d", pwm);
93 static void pwm_init(void)
95 timer_set_prescaler(TIM4, 3); // clock = 72 MHz / 2 = 36 MHz
96 timer_set_mode(TIM4, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
97 timer_disable_preload(TIM4);
98 timer_set_period(TIM4, 255); // PWM frequency = 18 MHz / 256 = 70.3125 kHz FIXME
99 timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_PWM1);
100 timer_set_oc_value(TIM4, TIM_OC1, 1);
114 timer_set_oc_polarity_high(TIM4, TIM_OC1);
115 timer_enable_counter(TIM4);
116 timer_enable_oc_output(TIM4, TIM_OC1);
118 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO6);
133 gpio_toggle(GPIOC, GPIO13);
136 if (usart_get_flag(USART1, USART_SR_RXNE)) {
137 uint ch = usart_recv(USART1);
140 gpio_set(GPIOC, GPIO15); // opto-coupler
141 gpio_clear(GPIOC, GPIO14); // LED
142 } else if (ch == 'b') {
144 gpio_clear(GPIOC, GPIO15); // opto-coupler
145 gpio_set(GPIOC, GPIO14); // LED
146 } else if (ch >= '0' && ch <= '9') {
147 pwm = 3*(ch - '0') + 1;
161 * % = pwm*4.389 - 12.723
163 timer_set_oc_value(TIM4, TIM_OC1, pwm);
166 if (cycles++ >= 50) {