board.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-11-07 RealThread first version
  9. */
  10. #include <rtthread.h>
  11. #include <board.h>
  12. #include <drv_common.h>
  13. RT_WEAK void rt_hw_board_init()
  14. {
  15. extern void hw_board_init(char *clock_src, int32_t clock_src_freq, int32_t clock_target_freq);
  16. /* Heap initialization */
  17. #if defined(RT_USING_HEAP)
  18. rt_system_heap_init((void *) HEAP_BEGIN, (void *) HEAP_END);
  19. #endif
  20. hw_board_init(BSP_CLOCK_SOURCE, BSP_CLOCK_SOURCE_FREQ_MHZ, BSP_CLOCK_SYSTEM_FREQ_MHZ);
  21. /* Set the shell console output device */
  22. #if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
  23. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  24. #endif
  25. /* Board underlying hardware initialization */
  26. #ifdef RT_USING_COMPONENTS_INIT
  27. rt_components_board_init();
  28. #endif
  29. }
  30. SPI_HandleTypeDef hspi1;
  31. SPI_HandleTypeDef hspi2;
  32. void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
  33. {
  34. GPIO_InitTypeDef GPIO_InitStruct = {0};
  35. if(spiHandle->Instance==SPI1)
  36. {
  37. /* USER CODE BEGIN SPI2_MspInit 0 */
  38. /* USER CODE END SPI2_MspInit 0 */
  39. /* SPI2 clock enable */
  40. __HAL_RCC_SPI1_CLK_ENABLE();
  41. __HAL_RCC_GPIOA_CLK_ENABLE();
  42. /**SPI1 GPIO Configuration
  43. PA5 ------> SPI1_SCK
  44. PA6 ------> SPI1_MISO
  45. PA7 ------> SPI1_MOSI
  46. */
  47. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  48. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  49. GPIO_InitStruct.Pull = GPIO_NOPULL;
  50. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  51. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  52. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  53. // GPIO_InitStruct.Pin = GPIO_PIN_4;
  54. // GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  55. // GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  56. // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  57. // HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  58. }
  59. if(spiHandle->Instance==SPI2)
  60. {
  61. /* USER CODE BEGIN SPI2_MspInit 0 */
  62. /* USER CODE END SPI2_MspInit 0 */
  63. /* SPI2 clock enable */
  64. __HAL_RCC_SPI2_CLK_ENABLE();
  65. __HAL_RCC_GPIOB_CLK_ENABLE();
  66. /**SPI2 GPIO Configuration
  67. PB13 ------> SPI2_SCK
  68. PB14 ------> SPI2_MISO
  69. PB15 ------> SPI2_MOSI
  70. */
  71. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  72. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  73. GPIO_InitStruct.Pull = GPIO_NOPULL;
  74. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  75. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  76. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  77. /* USER CODE BEGIN SPI2_MspInit 1 */
  78. /*Configure GPIO pin Output Level */
  79. // HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
  80. //
  81. // /*Configure GPIO pin : PtPin */
  82. // GPIO_InitStruct.Pin = GPIO_PIN_12;
  83. // GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  84. // GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  85. // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  86. // HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  87. /* USER CODE END SPI2_MspInit 1 */
  88. }
  89. }
  90. static int SPI2_INIT(void)
  91. {
  92. HAL_SPI_MspInit(&hspi2);
  93. return RT_EOK;
  94. }
  95. INIT_BOARD_EXPORT(SPI2_INIT);
  96. ///* 鬧꿍 SPI1 �구 */
  97. //int rt_hw_spi1_init(void)
  98. //{
  99. // //HAL_SPI_MspInit(&hspi1);
  100. // rt_hw_spi_device_attach("spi1", "spi30", GPIOA, GPIO_PIN_4); // CS 多신 PA4
  101. // return RT_EOK;
  102. //}
  103. //INIT_COMPONENT_EXPORT(rt_hw_spi1_init); // 菱땡놓迦뺏
  104. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  105. {
  106. GPIO_InitTypeDef GPIO_InitStruct = {0};
  107. if(adcHandle->Instance==ADC1)
  108. {
  109. /* USER CODE BEGIN ADC1_MspInit 0 */
  110. rt_kprintf("HAL_ADC_MspInit\n");
  111. /* USER CODE END ADC1_MspInit 0 */
  112. /* ADC1 clock enable */
  113. __HAL_RCC_ADC1_CLK_ENABLE();
  114. __HAL_RCC_GPIOC_CLK_ENABLE();
  115. /**ADC1 GPIO Configuration
  116. PC0 ------> ADC1_IN10
  117. */
  118. GPIO_InitStruct.Pin = GPIO_PIN_0;
  119. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  120. GPIO_InitStruct.Pull = GPIO_NOPULL;
  121. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  122. /* USER CODE BEGIN ADC1_MspInit 1 */
  123. /* USER CODE END ADC1_MspInit 1 */
  124. }
  125. }