Advertisement
Guest User

main.c

a guest
May 3rd, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include "main.h"
  2. #include "stm32f10x_conf.h"
  3. #include "stm32f10x.h"
  4. #include "comm.h"
  5.  
  6. SPI_InitTypeDef SPI2_InitStructure;
  7. GPIO_InitTypeDef GPIO_InitStructure;
  8. USART_InitTypeDef USART_InitStructure;
  9.  
  10. const char welcome[]="Hello test serial communication !!\n\n";
  11.  
  12.  
  13. int main(void)
  14. {
  15.  
  16. // clock
  17. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB, ENABLE);
  18. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB1Periph_USART2, ENABLE);
  19.  
  20. //Configuration GPIO
  21. //GPIO LED STAT
  22. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_12;
  23. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  24. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
  25. GPIO_Init(GPIOC, &GPIO_InitStructure);
  26.  
  27. // GPIO USART
  28. /* Configure USART2 TX as alternate function push-pull */
  29. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  30. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  31. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  32. GPIO_Init(GPIOA, &GPIO_InitStructure);
  33.  
  34. /* Configure USART2 RX as input floating */
  35. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  36. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  37. GPIO_Init(GPIOA, &GPIO_InitStructure);
  38.  
  39. //GPIO SPI :
  40. //PC6 WP
  41. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_6;
  42. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING;
  43. GPIO_Init(GPIOC, &GPIO_InitStructure);
  44.  
  45. //PC7 CP
  46. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_7;
  47. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING;
  48. GPIO_Init(GPIOC, &GPIO_InitStructure);
  49.  
  50. /* Configure NSS select */
  51. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  52. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  53. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  54. GPIO_Init(GPIOB, &GPIO_InitStructure);
  55.  
  56. GPIO_SetBits(GPIOB, GPIO_Pin_12);
  57.  
  58. //PB13 SPI2 SCK
  59. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_13;
  60. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  61. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
  62. GPIO_Init(GPIOB, &GPIO_InitStructure);
  63.  
  64. //PB14 SPI2 MISO
  65. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_14;
  66. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  67. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IPU;
  68. GPIO_Init(GPIOB, &GPIO_InitStructure);
  69.  
  70. //PB15 SPI2 MOSI
  71. GPIO_InitStructure.GPIO_Pin= GPIO_Pin_15;
  72. GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz;
  73. GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
  74. GPIO_Init(GPIOB, &GPIO_InitStructure);
  75.  
  76. //Configuration USART2
  77.  
  78. USART_InitStructure.USART_BaudRate = 115200;
  79. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  80. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  81. USART_InitStructure.USART_Parity = USART_Parity_No;
  82. USART_InitStructure.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
  83. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  84.  
  85. /* Configure USART2 */
  86. USART_Init(USART2, &USART_InitStructure);
  87.  
  88. /* Enable the USART2 */
  89. USART_Cmd(USART2, ENABLE);
  90.  
  91. // Configuration SPI2
  92.  
  93. SPI2_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  94. SPI2_InitStructure.SPI_Mode = SPI_Mode_Master;
  95. SPI2_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  96. SPI2_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  97. SPI2_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  98. SPI2_InitStructure.SPI_NSS = SPI_NSS_Soft;
  99. SPI2_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  100. SPI2_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
  101. SPI2_InitStructure.SPI_CRCPolynomial = 7;
  102. SPI_Init(SPI2, &SPI2_InitStructure);
  103.  
  104. // CRC DISABLE
  105. SPI_CalculateCRC(SPI2, DISABLE);
  106.  
  107. // Activation SPI
  108. SPI_Cmd(SPI2, ENABLE);
  109.  
  110. // SET / RESET LED STAT
  111.  
  112. GPIOC->BSRR = 1 << 12;
  113.  
  114. //send on USART2
  115. comm_puts(welcome);
  116.  
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement