Guest User

Untitled

a guest
Sep 24th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. extern "C" {
  2. #include <libopencm3/stm32/rcc.h>
  3. #include <libopencm3/stm32/gpio.h>
  4. #include <libopencm3/cm3/nvic.h>
  5. #include <libopencm3/stm32/usart.h>
  6. }
  7.  
  8. #define __IO volatile
  9.  
  10. static inline void usart_enable_idle_interrupt(uint32_t usart)
  11. {
  12. USART_CR1(usart) |= USART_CR1_IDLEIE;
  13. }
  14.  
  15. static inline void usart_disable_idle_interrupt(uint32_t usart)
  16. {
  17. USART_CR1(usart) &= ~USART_CR1_IDLEIE;
  18. }
  19.  
  20. static inline void usart_clear_idle_flag([[maybe_unused]]uint32_t usart)
  21. {
  22. __IO uint32_t tmpreg;
  23. tmpreg = USART2_SR;
  24. (void) tmpreg;
  25. tmpreg = USART2_DR;
  26. (void) tmpreg;
  27.  
  28. }
  29.  
  30. //PA2 -> TX2
  31. //PA3 -> RX2
  32.  
  33. void clock_setup()
  34. {
  35. //Led
  36. rcc_periph_clock_enable(RCC_GPIOC);
  37.  
  38. //USART
  39. rcc_periph_clock_enable(RCC_GPIOA);
  40. rcc_periph_clock_enable(RCC_USART2);
  41.  
  42. }
  43.  
  44. int main()
  45. {
  46. clock_setup();
  47.  
  48. //Led
  49. gpio_mode_setup(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO13);
  50. gpio_set(GPIOC, GPIO13);
  51.  
  52. // -> GPIO
  53. //
  54. /* Setup GPIO pins for USART2 transmit. */
  55. gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO2);
  56.  
  57. /* Setup GPIO pins for USART2 receive. */
  58. gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO3);
  59. // gpio_set_output_options(GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_25MHZ, GPIO3);
  60.  
  61. /* Setup USART2 TX and RX pin as alternate function. */
  62. gpio_set_af(GPIOA, GPIO_AF7, GPIO2);
  63. gpio_set_af(GPIOA, GPIO_AF7, GPIO3);
  64.  
  65. /* Setup USART2 parameters. */
  66. usart_set_baudrate(USART2, 9600);
  67. usart_set_databits(USART2, 8);
  68. usart_set_stopbits(USART2, USART_STOPBITS_1);
  69. usart_set_mode(USART2, USART_MODE_TX_RX);
  70. usart_set_parity(USART2, USART_PARITY_NONE);
  71. usart_set_flow_control(USART2, USART_FLOWCONTROL_NONE);
  72.  
  73. /* Enable USART2 Receive interrupt. */
  74. // usart_enable_rx_interrupt(USART2);
  75. usart_enable_idle_interrupt(USART2);
  76.  
  77. /* Enable NVIC irq */
  78. nvic_enable_irq(NVIC_USART2_IRQ);
  79.  
  80. /* Finally enable the USART. */
  81. usart_enable(USART2);
  82.  
  83. while(true)
  84. {
  85. __asm("nop");
  86. }
  87. }
  88.  
  89. void usart2_isr()
  90. {
  91. if (((USART_CR1(USART2) & USART_CR1_IDLEIE) != 0) &&
  92. ((USART_SR(USART2) & USART_FLAG_IDLE) != 0)
  93. )
  94. {
  95. usart_clear_idle_flag(USART2);
  96.  
  97. [[maybe_unused]] int x = 10;
  98. }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment