hk32_msp.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-14 Jonas first version
  9. */
  10. #include <hk32f0xx.h>
  11. #include <rtthread.h>
  12. #include "hk32_msp.h"
  13. #ifdef BSP_USING_UART
  14. void hk32_msp_usart_init(void *Instance)
  15. {
  16. GPIO_InitTypeDef GPIO_InitStruct;
  17. USART_TypeDef *USARTx = (USART_TypeDef *)Instance;
  18. GPIO_StructInit(&GPIO_InitStruct);
  19. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  20. #ifdef BSP_USING_UART1
  21. if (USART1 == USARTx)
  22. {
  23. #define USART1_REMAP
  24. #ifndef USART1_REMAP
  25. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  26. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  27. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  28. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  29. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
  30. GPIO_Init(GPIOA, &GPIO_InitStruct);
  31. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  32. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  33. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  34. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
  35. GPIO_Init(GPIOA, &GPIO_InitStruct);
  36. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
  37. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
  38. #else
  39. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  40. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  41. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  42. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  43. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
  44. GPIO_Init(GPIOB, &GPIO_InitStruct);
  45. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  46. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  47. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  48. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
  49. GPIO_Init(GPIOB, &GPIO_InitStruct);
  50. GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
  51. GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
  52. #endif
  53. }
  54. #endif
  55. #ifdef BSP_USING_UART2
  56. if (USART2 == USARTx)
  57. {
  58. RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  59. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  60. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  61. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  62. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
  63. GPIO_Init(GPIOA, &GPIO_InitStruct);
  64. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  65. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  66. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  67. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
  68. GPIO_Init(GPIOA, &GPIO_InitStruct);
  69. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
  70. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  71. }
  72. #endif
  73. /* Add others */
  74. }
  75. #endif /* BSP_USING_SERIAL */
  76. #ifdef BSP_USING_I2C
  77. void hk32_msp_i2c_init(void *Instance)
  78. {
  79. GPIO_InitTypeDef GPIO_InitStruct;
  80. I2C_TypeDef *I2Cx = (I2C_TypeDef *)Instance;
  81. GPIO_StructInit(&GPIO_InitStruct);
  82. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  83. #ifdef BSP_USING_I2C1
  84. if (I2C1 == I2Cx)
  85. {
  86. #ifndef I2C1_REMAP
  87. RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  88. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  89. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  90. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  91. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
  92. GPIO_Init(GPIOB, &GPIO_InitStruct);
  93. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  94. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  95. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  96. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
  97. GPIO_Init(GPIOB, &GPIO_InitStruct);
  98. GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
  99. GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
  100. #else
  101. RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  102. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  103. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  104. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  105. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  106. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
  107. GPIO_Init(GPIOB, &GPIO_InitStruct);
  108. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  109. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  110. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  111. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
  112. GPIO_Init(GPIOB, &GPIO_InitStruct);
  113. GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_1);
  114. GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1);
  115. #endif
  116. }
  117. #endif
  118. /* Add others */
  119. }
  120. #endif /* BSP_USING_I2C */
  121. #ifdef BSP_USING_SPI
  122. void hk32_msp_spi_init(void *Instance)
  123. {
  124. GPIO_InitTypeDef GPIO_InitStruct;
  125. SPI_TypeDef *SPIx = (SPI_TypeDef *)Instance;
  126. GPIO_StructInit(&GPIO_InitStruct);
  127. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  128. #ifdef BSP_USING_SPI1
  129. if (SPI1 == SPIx)
  130. {
  131. #ifndef SPI1_REMAP
  132. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  133. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  134. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  135. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  136. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
  137. GPIO_Init(GPIOA, &GPIO_InitStruct);
  138. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  139. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  140. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  141. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  142. GPIO_Init(GPIOA, &GPIO_InitStruct);
  143. GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
  144. GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
  145. GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
  146. #else
  147. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  148. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  149. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  150. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  151. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  152. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
  153. GPIO_Init(GPIOA, &GPIO_InitStruct);
  154. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  155. GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  156. GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  157. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
  158. GPIO_Init(GPIOB, &GPIO_InitStruct);
  159. GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_0);
  160. GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_0);
  161. GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_0);
  162. #endif
  163. }
  164. #endif
  165. /* Add others */
  166. }
  167. #endif /* BSP_USING_I2C */