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>
12 static void clock_setup(void)
14 rcc_clock_setup_in_hse_8mhz_out_72mhz();
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_USART2);
20 rcc_periph_clock_enable(RCC_TIM2);
21 rcc_periph_clock_enable(RCC_TIM4);
23 rcc_periph_reset_pulse(RST_GPIOA);
24 rcc_periph_reset_pulse(RST_GPIOB);
25 rcc_periph_reset_pulse(RST_GPIOC);
26 rcc_periph_reset_pulse(RST_USART2);
27 rcc_periph_reset_pulse(RST_TIM2);
28 rcc_periph_reset_pulse(RST_TIM4);
31 static void gpio_setup(void)
33 // PC13 = BluePill LED
34 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
35 gpio_clear(GPIOC, GPIO13);
37 // Pins for MODBUS USART
38 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART2_RX);
39 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
40 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
43 static volatile u32 ms_ticks;
45 void sys_tick_handler(void)
50 static void tick_setup(void)
52 systick_set_frequency(1000, 72000000);
53 systick_counter_enable();
54 systick_interrupt_enable();
57 static void delay_ms(uint ms)
59 u32 start_ticks = ms_ticks;
60 while (ms_ticks - start_ticks < ms)
64 static void usart_setup(void)
67 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART2_TX);
69 usart_set_baudrate(USART2, 9600);
70 usart_set_databits(USART2, 8);
71 usart_set_stopbits(USART2, USART_STOPBITS_1);
72 usart_set_mode(USART2, USART_MODE_TX_RX);
73 usart_set_parity(USART2, USART_PARITY_NONE);
74 usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
88 cm_enable_interrupts(); // FIXME: Needed?
91 timer_set_prescaler(TIM4, 7); // clock = 72 MHz / 8 = 18 MHz FIXME!
92 timer_set_mode(TIM4, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
93 timer_disable_preload(TIM4);
94 timer_set_period(TIM4, 255); // PWM frequency = 18 MHz / 256 = 70.3125 kHz
95 timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_PWM1);
96 timer_set_oc_value(TIM4, TIM_OC1, 128);
97 timer_set_oc_polarity_high(TIM4, TIM_OC1);
98 timer_enable_counter(TIM4);
99 timer_enable_oc_output(TIM4, TIM_OC1);
102 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO6);
103 // gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8);
106 gpio_toggle(GPIOC, GPIO13);
108 // gpio_toggle(GPIOA, GPIO8);
109 //timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_FORCE_LOW);
111 //timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_FORCE_HIGH);
118 /*** Modbus callbacks ***/
120 bool modbus_check_discrete_input(u16 addr UNUSED)
125 bool modbus_get_discrete_input(u16 addr UNUSED)
130 bool modbus_check_coil(u16 addr UNUSED)
135 bool modbus_get_coil(u16 addr UNUSED)
140 void modbus_set_coil(u16 addr UNUSED, bool value UNUSED)
144 bool modbus_check_input_register(u16 addr UNUSED)
149 u16 modbus_get_input_register(u16 addr UNUSED)
154 bool modbus_check_holding_register(u16 addr UNUSED)
159 u16 modbus_get_holding_register(u16 addr UNUSED)
164 void modbus_set_holding_register(u16 addr UNUSED, u16 value UNUSED)
168 const char * const modbus_id_strings[MODBUS_ID_MAX] = {
169 [MODBUS_ID_VENDOR_NAME] = "United Computer Wizards",
170 [MODBUS_ID_PRODUCT_CODE] = "42",
171 [MODBUS_ID_MAJOR_MINOR_REVISION] = "1.0",
172 [MODBUS_ID_VENDOR_URL] = "http://www.ucw.cz/",
173 [MODBUS_ID_PRODUCT_NAME] = "Magic Gadget",
174 [MODBUS_ID_USER_APP_NAME] = NULL,