14#include <libopencm3/cm3/cortex.h>
15#include <libopencm3/cm3/nvic.h>
16#include <libopencm3/stm32/adc.h>
17#include <libopencm3/stm32/dac.h>
18#include <libopencm3/stm32/gpio.h>
19#include <libopencm3/stm32/rcc.h>
20#include <libopencm3/stm32/timer.h>
21#include <libopencm3/stm32/usart.h>
35#define ASC_USART USART2
39#define RCC_GPIO_LED RCC_GPIOA
41#define TIM_OC_LED TIM_OC1
42#define RCC_TIM_LED RCC_TIM2
43#define AF_TIM_LED GPIO_AF2
46#define pulser_tick_freq 1000
47#define pulser_period 1000
48#define pulser_width 500
53#define IC_RCC_GPIO RCC_GPIOA
55#define IC_RCC_TIM RCC_TIM1
56#define IC_TI TIM_IC_IN_TI1
58uint32_t ic_pulse_length;
71uint32_t optionFlags = 0;
141 rcc_periph_clock_enable(RCC_GPIOA);
142 rcc_periph_clock_enable(RCC_USART2);
143 setup_usart(ASC_USART, 9600, GPIOA, GPIO2, GPIO_AF1, GPIOA, GPIO3,
145 nvic_enable_irq(NVIC_USART2_IRQ);
146 usart_enable_rx_interrupt(ASC_USART);
147 usart_enable(ASC_USART);
149 rcc_periph_clock_enable(RCC_GPIO_LED);
150 rcc_periph_clock_enable(RCC_TIM_LED);
152 TIM_LED, rcc_get_timer_clk_freq(TIM_LED) / pulser_tick_freq,
153 pulser_period, pulser_width, TIM_OC_LED, PORT_LED, PIN_LED, AF_TIM_LED);
154 timer_enable_counter(TIM_LED);
156 rcc_periph_clock_enable(IC_RCC_GPIO);
157 rcc_periph_clock_enable(IC_RCC_TIM);
159 IC_TIM, rcc_get_timer_clk_freq(IC_TIM) / pulser_tick_freq, 0xFFFF,
160 IC_TI, IC_PORT, IC_PIN, IC_AF);
161 timer_enable_counter(IC_TIM);
163 Catch(e) {
return e; }
169 if (timer_get_flag(IC_TIM, TIM_SR_CC1IF)) {
170 ic_pulse_length = TIM_CCR2(IC_TIM);
171 ic_period = TIM_CCR1(IC_TIM);
173 if (timer_get_flag(IC_TIM, TIM_SR_CC1OF)) {
175 timer_clear_flag(IC_TIM, TIM_SR_CC1OF);
177 if (timer_get_flag(IC_TIM, TIM_SR_CC2OF)) {
178 ic_overrun |= 1 << 4;
179 timer_clear_flag(IC_TIM, TIM_SR_CC2OF);
183 Catch(e) { nExceptions++; }
#define HANDLE_ASC_COMM_IN_POLLING_LOOP(uart_no)
Polling for ascii_serial_com_device and ascii_serial_com_register_pointers.
#define DECLARE_ASC_DEVICE_W_REGISTER_POINTERS()
Declarations for ascii_serial_com_device and ascii_serial_com_register_pointers.
#define SETUP_ASC_DEVICE_W_REGISTER_POINTERS(register_map, register_write_masks, nRegs)
Setup for ascii_serial_com_device and ascii_serial_com_register_pointers.
ASCII Serial Com Register Pointers.
void millisec_timer_systick_setup(uint32_t ahb_frequency)
Setup the systick timer.
Portable millisecond timer.
static uint32_t MILLISEC_TIMER_NOW
A counter that increments every millisecond.
#define MILLISEC_TIMER_SYSTICK_IT
Millisecond timer SysTick interrupt.
REGTYPE register_write_masks[nRegs]
Write masks for register_map.
volatile REGTYPE * register_map[nRegs]
Register Map.
To be used with the timer peripherals on STM32 microcontrollers.
#define setup_timer_capture_pwm_input(timer, prescale, max_timer_counts, input, gpio_port, gpio_pin, gpio_af)
Initialize a timer to capture periodic pulses on an input pin.
#define setup_timer_periodic_output_pulse(timer, prescale, period, pulse_length, output, gpio_port, gpio_pin, gpio_af)
Initialize a timer to output a periodic pulse on an output pin.
To be used with the USART peripherals on STM32 microcontrollers.
#define setup_usart(usart, baud, tx_port, tx_pin, tx_af, rx_port, rx_pin, rx_af)
Setup a USART.
#define def_usart_isr_push_rx_to_circ_buf(isr_name, usart, circular_buffer)
Define the ISR for a USART to push rx bytes to a circular buffer.