Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <string.h>
  2. #include <errno.h>
  3. #include <stm32.h>
  4. #include <sys/stat.h>
  5. #include <gpio.h>
  6. #include <stdio.h>
  7.  
  8. #define STDOUT_FILENO 1
  9. #define STDIN_FILENO 0
  10. #define STDERR_FILENO 2
  11.  
  12. #define USART_Mode_Rx_Tx (USART_CR1_RE | \
  13. USART_CR1_TE)
  14. #define USART_Enable USART_CR1_UE
  15.  
  16. #define USART_WordLength_8b 0x0000
  17. #define USART_WordLength_9b USART_CR1_M
  18.  
  19. #define USART_Parity_No 0x0000
  20. #define USART_Parity_Even USART_CR1_PCE
  21. #define USART_Parity_Odd (USART_CR1_PCE | \
  22. USART_CR1_PS)
  23.  
  24. #define USART_StopBits_1 0x0000
  25. #define USART_StopBits_0_5 0x1000
  26. #define USART_StopBits_2 0x2000
  27. #define USART_StopBits_1_5 0x3000
  28.  
  29. #define USART_FlowControl_None 0x0000
  30. #define USART_FlowControl_RTS USART_CR3_RTSE
  31. #define USART_FlowControl_CTS USART_CR3_CTSE
  32.  
  33. #define HSI_HZ 16000000U
  34. #define PCLK1_HZ HSI_HZ
  35.  
  36. uint32_t const baudrate = 9600U;
  37.  
  38. void setUp() {
  39. RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_DMA1EN;
  40. RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
  41. RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
  42. GPIOafConfigure(GPIOA,
  43. 2,
  44. GPIO_OType_PP,
  45. GPIO_Fast_Speed,
  46. GPIO_PuPd_NOPULL,
  47. GPIO_AF_USART2);
  48. GPIOafConfigure(GPIOA,
  49. 3,
  50. GPIO_OType_PP,
  51. GPIO_Fast_Speed,
  52. GPIO_PuPd_UP,
  53. GPIO_AF_USART2);
  54. USART2->CR1 = USART_CR1_RE | USART_CR1_TE;
  55. USART2->CR2 = 0;
  56. USART2->BRR = (PCLK1_HZ + (baudrate / 2U)) / baudrate;
  57. USART2->CR3 = USART_CR3_DMAT | USART_CR3_DMAR;
  58. DMA1_Stream6->CR = 4U << 25 |
  59. DMA_SxCR_PL_1 |
  60. DMA_SxCR_MINC |
  61. DMA_SxCR_DIR_0 |
  62. DMA_SxCR_TCIE;
  63. DMA1_Stream6->PAR = (uint32_t)&USART2->DR;
  64. DMA1_Stream5->CR = 4U << 25 |
  65. DMA_SxCR_PL_1 |
  66. DMA_SxCR_MINC |
  67. DMA_SxCR_TCIE;
  68. DMA1_Stream5->PAR = (uint32_t)&USART2->DR;
  69. DMA1->HIFCR = DMA_HIFCR_CTCIF6 |
  70. DMA_HIFCR_CTCIF5;
  71. NVIC_EnableIRQ(DMA1_Stream6_IRQn);
  72. NVIC_EnableIRQ(DMA1_Stream5_IRQn);
  73. USART2->CR1 |= USART_CR1_UE;
  74.  
  75. __NOP();
  76. }
  77.  
  78. void sendMessage(char* message) {
  79. DMA1_Stream6->M0AR = (uint32_t) message;
  80. DMA1_Stream6->NDTR = strlen(message);
  81. DMA1_Stream6->CR |= DMA_SxCR_EN;
  82. }
  83.  
  84. int main() {
  85. setUp();
  86. sendMessage("Initialized\n\0");
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement