Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern "C" {
- #include <libopencm3/stm32/rcc.h>
- #include <libopencm3/stm32/gpio.h>
- #include <libopencm3/cm3/nvic.h>
- #include <libopencm3/stm32/usart.h>
- }
- #define __IO volatile
- static inline void usart_enable_idle_interrupt(uint32_t usart)
- {
- USART_CR1(usart) |= USART_CR1_IDLEIE;
- }
- static inline void usart_disable_idle_interrupt(uint32_t usart)
- {
- USART_CR1(usart) &= ~USART_CR1_IDLEIE;
- }
- static inline void usart_clear_idle_flag([[maybe_unused]]uint32_t usart)
- {
- __IO uint32_t tmpreg;
- tmpreg = USART2_SR;
- (void) tmpreg;
- tmpreg = USART2_DR;
- (void) tmpreg;
- }
- //PA2 -> TX2
- //PA3 -> RX2
- void clock_setup()
- {
- //Led
- rcc_periph_clock_enable(RCC_GPIOC);
- //USART
- rcc_periph_clock_enable(RCC_GPIOA);
- rcc_periph_clock_enable(RCC_USART2);
- }
- int main()
- {
- clock_setup();
- //Led
- gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO13);
- gpio_set(GPIOC, GPIO13);
- // -> GPIO
- //
- /* Setup GPIO pins for USART2 transmit. */
- gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
- /* Setup GPIO pins for USART2 receive. */
- gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3);
- // gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO3);
- /* Setup USART2 TX and RX pin as alternate function. */
- gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
- gpio_set_af(GPIOA, GPIO_AF7, GPIO3);
- /* Setup USART2 parameters. */
- usart_set_baudrate(USART2, 9600);
- 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);
- /* Enable USART2 Receive interrupt. */
- // usart_enable_rx_interrupt(USART2);
- usart_enable_idle_interrupt(USART2);
- /* Enable NVIC irq */
- nvic_enable_irq(NVIC_USART2_IRQ);
- /* Finally enable the USART. */
- usart_enable(USART2);
- while(true)
- {
- __asm("nop");
- }
- }
- void usart2_isr()
- {
- if (((USART_CR1(USART2) & USART_CR1_IDLEIE) != 0) &&
- ((USART_SR(USART2) & USART_FLAG_IDLE) != 0)
- )
- {
- usart_clear_idle_flag(USART2);
- [[maybe_unused]] int x = 10;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment