67 lines
1.8 KiB
C
67 lines
1.8 KiB
C
#include <libopencm3/stm32/rcc.h>
|
|
#include <libopencm3/stm32/gpio.h>
|
|
#include <libopencm3/stm32/usart.h>
|
|
|
|
#include <libopencm3/cm3/systick.h>
|
|
|
|
int main(void) {
|
|
rcc_periph_clock_enable(RCC_GPIOA);
|
|
rcc_periph_clock_enable(RCC_GPIOB);
|
|
|
|
rcc_periph_clock_enable(RCC_USART2);
|
|
|
|
// setup PA5-7 and PB1 for the stepper motor gates
|
|
gpio_clear(GPIOA, GPIO5|GPIO6|GPIO7);
|
|
gpio_clear(GPIOB, GPIO1);
|
|
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO5);
|
|
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO6);
|
|
gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO7);
|
|
gpio_mode_setup(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO1);
|
|
|
|
// setup PA2/3 for USART2_RX and USART2_TX
|
|
gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2|GPIO3);
|
|
gpio_set_af(GPIOA, GPIO_AF4, GPIO2|GPIO3);
|
|
|
|
// setup USART2 stuff
|
|
usart_set_baudrate(USART2, 115200);
|
|
usart_set_databits(USART2, 8);
|
|
usart_set_stopbits(USART2, USART_STOPBITS_1);
|
|
usart_set_mode(USART2, USART_MODE_TX_RX);
|
|
usart_set_parity(USART2, USART_PARITY_NONE);
|
|
usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
|
|
usart_enable(USART2);
|
|
|
|
#if 0
|
|
// if I need more speed than the internal 2 MHz
|
|
// setup PA0 as a clock input
|
|
rcc_bypass_enable(RCC_HSE);
|
|
rcc_osc_on(RCC_HSE);
|
|
|
|
rcc_wait_for_osc_ready(RCC_HSE);
|
|
rcc_set_sysclk_source(RCC_HSE);
|
|
#endif
|
|
|
|
// setup systick interrupt
|
|
systick_interrupt_disable();
|
|
systick_counter_disable();
|
|
|
|
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
|
|
systick_set_reload(rcc_ahb_frequency/100000-1);
|
|
systick_clear();
|
|
|
|
systick_interrupt_enable();
|
|
systick_counter_enable();
|
|
|
|
while (1) {
|
|
if ((USART_ISR(USART2) & USART_ISR_RXNE) != 0) {
|
|
// TODO process byte
|
|
uint8_t rx = usart_recv(USART2);
|
|
usart_send_blocking(USART2, rx);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SysTick_Handler(void);
|
|
void SysTick_Handler(void) {
|
|
}
|