board.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2006-2026, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2026-02-03 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 hspi2;
  31. void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
  32. {
  33. GPIO_InitTypeDef GPIO_InitStruct = {0};
  34. if(spiHandle->Instance==SPI1)
  35. {
  36. /* USER CODE BEGIN SPI2_MspInit 0 */
  37. /* USER CODE END SPI2_MspInit 0 */
  38. /* SPI2 clock enable */
  39. __HAL_RCC_SPI1_CLK_ENABLE();
  40. __HAL_RCC_GPIOA_CLK_ENABLE();
  41. /**SPI1 GPIO Configuration
  42. PA5 ------> SPI1_SCK
  43. PA6 ------> SPI1_MISO
  44. PA7 ------> SPI1_MOSI
  45. */
  46. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  47. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  48. GPIO_InitStruct.Pull = GPIO_NOPULL;
  49. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  50. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  51. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  52. // GPIO_InitStruct.Pin = GPIO_PIN_4;
  53. // GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  54. // GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  55. // GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  56. // HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  57. }
  58. if(spiHandle->Instance==SPI2)
  59. {
  60. /* USER CODE BEGIN SPI2_MspInit 0 */
  61. /* USER CODE END SPI2_MspInit 0 */
  62. /* SPI2 clock enable */
  63. __HAL_RCC_SPI2_CLK_ENABLE();
  64. __HAL_RCC_GPIOB_CLK_ENABLE();
  65. /**SPI2 GPIO Configuration
  66. PB13 ------> SPI2_SCK
  67. PB14 ------> SPI2_MISO
  68. PB15 ------> SPI2_MOSI
  69. */
  70. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  71. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  72. GPIO_InitStruct.Pull = GPIO_NOPULL;
  73. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  74. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  75. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  76. /* USER CODE BEGIN SPI2_MspInit 1 */
  77. /*Configure GPIO pin Output Level */
  78. /*Configure GPIO pin : PtPin */
  79. GPIO_InitStruct.Pin = GPIO_PIN_12;
  80. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  81. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  82. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  83. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  84. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
  85. /* USER CODE END SPI2_MspInit 1 */
  86. }
  87. }
  88. static int SPI2_INIT(void)
  89. {
  90. HAL_SPI_MspInit(&hspi2);
  91. return RT_EOK;
  92. }
  93. INIT_BOARD_EXPORT(SPI2_INIT);