Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_it.h"
  3.  
  4. #define USARTy USART2
  5. #define USARTy_GPIO GPIOD
  6. #define USARTy_CLK RCC_APB1Periph_USART2
  7. #define USARTy_GPIO_CLK RCC_APB2Periph_GPIOD
  8. #define USARTy_RxPin GPIO_Pin_6
  9. #define USARTy_TxPin GPIO_Pin_5
  10.  
  11. typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
  12.  
  13. #define TxBufferSize (countof(TxBuffer))
  14.  
  15. /* Private macro -------------------------------------------------------------*/
  16. #define countof(a) (sizeof(a) / sizeof(*(a)))
  17.  
  18. /* Private variables ---------------------------------------------------------*/
  19. USART_InitTypeDef USART_InitStructure;
  20. uint8_t TxBuffer[] = "Buffer Send from USARTy to USARTz using Flags";
  21. uint8_t RxBuffer[TxBufferSize];
  22. __IO uint8_t TxCounter = 0, RxCounter = 0;
  23.  
  24. void RCC_Configuration(void);
  25. void GPIO_Configuration(void);
  26. void USART_Configuration(void);
  27.  
  28. volatile TestStatus TransferStatus = FAILED;
  29.  
  30. TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  31. //__IO uint8_t index = 0;
  32.  
  33. static __IO uint32_t TimingDelay;
  34.  
  35. /* Private function prototypes -----------------------------------------------*/
  36. void Delay(__IO uint32_t nTime)
  37. {
  38. TimingDelay = nTime;
  39. while(TimingDelay != 0);
  40. }
  41.  
  42. /**
  43. * @brief Decrements the TimingDelay variable.
  44. * @param None
  45. * @retval None
  46. */
  47. void RCC_Configuration(void)
  48. {
  49. /* Enable GPIO clock */
  50. RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
  51. RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE);
  52.  
  53. }
  54. void GPIO_Configuration(void)
  55. {
  56. GPIO_InitTypeDef GPIO_InitStructure;
  57.  
  58.  
  59.  
  60. /* Enable the USART2 Pins Software Remapping */
  61. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  62.  
  63.  
  64.  
  65. /* Configure USARTy Rx as input floating */
  66. GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
  67. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  68. GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
  69.  
  70.  
  71. /* Configure USARTy Tx as alternate function push-pull */
  72. GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
  73. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  74. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  75. GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
  76.  
  77. }
  78. void USART_Configuration(void)
  79. {
  80.  
  81. USART_InitStructure.USART_BaudRate = 9600;
  82. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  83. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  84. USART_InitStructure.USART_Parity = USART_Parity_No;
  85. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  86. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  87.  
  88. /* Configure USARTy */
  89. USART_Init(USARTy, &USART_InitStructure);
  90.  
  91.  
  92. /* Enable the USARTy */
  93. USART_Cmd(USARTy, ENABLE);
  94.  
  95. }
  96.  
  97. int main(void)
  98. {
  99. RCC_Configuration();
  100. GPIO_Configuration();
  101. USART_Configuration();
  102.  
  103. while (1)
  104. {
  105. while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET)
  106. {
  107. }
  108. RxBuffer [1] = (USART_ReceiveData(USARTy));
  109. while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
  110. {
  111. }
  112. USART_SendData(USARTy, RxBuffer[1]);
  113.  
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement