mm32_msp.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (c) 2022-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-01-22 chasel first version
  9. */
  10. #include <rtthread.h>
  11. #include "mm32_device.h"
  12. #include "mm32_msp.h"
  13. void mm32_msp_uart_init(void *instance)
  14. {
  15. GPIO_InitTypeDef GPIO_InitStructure;
  16. UART_TypeDef *uart_x = (UART_TypeDef *)instance;
  17. #ifdef BSP_USING_UART1
  18. if(UART1 == uart_x)
  19. {
  20. /* Enable UART clock */
  21. RCC_APB2PeriphClockCmd(RCC_APB2Periph_UART1, ENABLE);
  22. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  23. /* Configure USART Rx/tx PIN */
  24. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  25. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
  26. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  27. GPIO_Init(GPIOA, &GPIO_InitStructure);
  28. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  29. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  30. GPIO_Init(GPIOA, &GPIO_InitStructure);
  31. GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7);
  32. GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7);
  33. }
  34. #endif
  35. #ifdef BSP_USING_UART2
  36. if(UART2 == uart_x)
  37. {
  38. /* Enable UART clock */
  39. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2, ENABLE);
  40. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  41. /* Configure USART Rx/tx PIN */
  42. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  43. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  45. GPIO_Init(GPIOA, &GPIO_InitStructure);
  46. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  47. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  48. GPIO_Init(GPIOA, &GPIO_InitStructure);
  49. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7);
  50. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7);
  51. }
  52. #endif
  53. #ifdef BSP_USING_UART3
  54. if(UART3 == uart_x)
  55. {
  56. /* Enable UART clock */
  57. RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART3, ENABLE);
  58. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  59. GPIO_InitTypeDef GPIO_InitStructure;
  60. /* Configure USART Rx/tx PIN */
  61. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  62. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_High;
  63. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  64. GPIO_Init(GPIOC, &GPIO_InitStructure);
  65. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  66. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  67. GPIO_Init(GPIOC, &GPIO_InitStructure);
  68. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_7);
  69. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_7);
  70. }
  71. #endif
  72. /* add others */
  73. }
  74. #ifdef BSP_USING_ADC
  75. void mm32_msp_adc_init(void *instance)
  76. {
  77. GPIO_InitTypeDef GPIO_InitStruct;
  78. ADC_TypeDef *adc_x = (ADC_TypeDef *)instance;
  79. #ifdef BSP_USING_ADC1
  80. if(adc_x == ADC1)
  81. {
  82. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //Enable ADC1 clock
  83. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  84. /* configure adc channel as analog input */
  85. GPIO_StructInit(&GPIO_InitStruct);
  86. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  87. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  88. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
  89. GPIO_Init(GPIOA, &GPIO_InitStruct);
  90. }
  91. #endif
  92. #ifdef BSP_USING_ADC2
  93. if(adc_x == ADC2)
  94. {
  95. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); //Enable ADC2 clock
  96. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  97. /* configure adc channel as analog input */
  98. GPIO_StructInit(&GPIO_InitStruct);
  99. GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  100. GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  101. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
  102. GPIO_Init(GPIOA, &GPIO_InitStruct);
  103. }
  104. #endif
  105. }
  106. #endif /* BSP_USING_ADC */