基质喷涂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.0 KiB

3 weeks ago
  1. #include "apphal.hpp"
  2. #include "bsp.h"
  3. #include "appcfg.h"
  4. using namespace iflytop;
  5. SPI_HandleTypeDef hspi1;
  6. #define MOTOR_SPI_SCK_PIN GPIO_PIN_5
  7. #define MOTOR_SPI_SDO_PIN GPIO_PIN_6
  8. #define MOTOR_SPI_SDI_PIN GPIO_PIN_5
  9. #define MOTOR_SPI_SCK_PORT GPIOA
  10. #define MOTOR_SPI_SDO_PORT GPIOA
  11. #define MOTOR_SPI_SDI_PORT GPIOB
  12. void AppHal::tmc_spi_init() {
  13. #if (MOTOR_SPI_INDEX == 1)
  14. __HAL_RCC_SPI1_CLK_ENABLE();
  15. // __HAL_RCC_GPIOA_CLK_ENABLE();
  16. // 使能相关 GPIO 端口时钟
  17. __HAL_RCC_GPIOA_CLK_ENABLE();
  18. __HAL_RCC_GPIOB_CLK_ENABLE();
  19. static_assert(&MOTOR_SPI_INS == &hspi1);
  20. #if 0
  21. hspi1.Instance = SPI1;
  22. hspi1.Init.Mode = SPI_MODE_MASTER;
  23. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  24. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  25. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  26. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  27. hspi1.Init.NSS = SPI_NSS_SOFT;
  28. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  29. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  30. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  31. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  32. hspi1.Init.CRCPolynomial = 10;
  33. #else
  34. hspi1.Instance = SPI1;
  35. hspi1.Init.Mode = SPI_MODE_MASTER;
  36. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  37. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  38. hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  39. hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  40. hspi1.Init.NSS = SPI_NSS_SOFT;
  41. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  42. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  43. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  44. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  45. hspi1.Init.CRCPolynomial = 10;
  46. #endif
  47. if (HAL_SPI_Init(&hspi1) != HAL_OK) {
  48. Error_Handler();
  49. }
  50. static_assert(MOTOR_SPI_SCK == PA5);
  51. static_assert(MOTOR_SPI_SDO == PA6);
  52. static_assert(MOTOR_SPI_SDI == PB5);
  53. #if 0
  54. GPIO_InitTypeDef GPIO_InitStruct = {0};
  55. GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
  56. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  57. GPIO_InitStruct.Pull = GPIO_NOPULL;
  58. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  59. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  60. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  61. #else
  62. GPIO_InitTypeDef GPIO_InitStruct = {0};
  63. // 使能相关 GPIO 端口时钟
  64. // __HAL_RCC_GPIOA_CLK_ENABLE();
  65. // __HAL_RCC_GPIOB_CLK_ENABLE();
  66. // 配置 SCK 引脚
  67. GPIO_InitStruct.Pin = MOTOR_SPI_SCK_PIN;
  68. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  69. GPIO_InitStruct.Pull = GPIO_NOPULL;
  70. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  71. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  72. HAL_GPIO_Init(MOTOR_SPI_SCK_PORT, &GPIO_InitStruct);
  73. // 配置 SDO 引脚
  74. GPIO_InitStruct.Pin = MOTOR_SPI_SDO_PIN;
  75. HAL_GPIO_Init(MOTOR_SPI_SDO_PORT, &GPIO_InitStruct);
  76. // 配置 SDI 引脚
  77. GPIO_InitStruct.Pin = MOTOR_SPI_SDI_PIN;
  78. HAL_GPIO_Init(MOTOR_SPI_SDI_PORT, &GPIO_InitStruct);
  79. #endif
  80. #else
  81. #error "MOTOR_SPI_INDEX not supported"
  82. #endif
  83. }