spi_bridge.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #include "spi_bridge.h"
  2. SPI_HandleTypeDef hspi2;
  3. //
  4. void spi_bridge_init(void)
  5. {
  6. }
  7. //
  8. #if 0
  9. I2C_HandleTypeDef *i2c_get_bridge(void)
  10. {
  11. return &hi2c2;
  12. }
  13. #endif
  14. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  15. {
  16. GPIO_InitTypeDef GPIO_InitStruct = {0};
  17. if(hspi->Instance==SPI2)
  18. {
  19. __HAL_RCC_SPI2_CLK_ENABLE();
  20. __HAL_RCC_GPIOA_CLK_ENABLE();
  21. __HAL_RCC_GPIOA_CLK_ENABLE();
  22. /**SPI1 GPIO Configuration
  23. PA4 ------> SPI1_NSS
  24. PA5 ------> SPI1_SCK
  25. PA7 ------> SPI1_MOSI
  26. */
  27. GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7;
  28. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  29. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  30. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  31. }
  32. #if 0
  33. /* SPI1 DMA Init */
  34. /* SPI1_TX Init */
  35. hdma_spi1_tx.Instance = DMA1_Channel3;
  36. hdma_spi1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  37. hdma_spi1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  38. hdma_spi1_tx.Init.MemInc = DMA_MINC_ENABLE;
  39. hdma_spi1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  40. hdma_spi1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  41. hdma_spi1_tx.Init.Mode = DMA_NORMAL;
  42. hdma_spi1_tx.Init.Priority = DMA_PRIORITY_LOW;
  43. if (HAL_DMA_Init(&hdma_spi1_tx) != HAL_OK)
  44. {
  45. Error_Handler();
  46. }
  47. __HAL_LINKDMA(hspi,hdmatx,hdma_spi1_tx);
  48. /* SPI1 interrupt Init */
  49. HAL_NVIC_SetPriority(SPI1_IRQn, 5, 0);
  50. HAL_NVIC_EnableIRQ(SPI1_IRQn);
  51. /* USER CODE BEGIN SPI1_MspInit 1 */
  52. /* USER CODE END SPI1_MspInit 1 */
  53. }
  54. else if(hspi->Instance==SPI2)
  55. {
  56. /* USER CODE BEGIN SPI2_MspInit 0 */
  57. /* USER CODE END SPI2_MspInit 0 */
  58. /* Peripheral clock enable */
  59. __HAL_RCC_SPI2_CLK_ENABLE();
  60. __HAL_RCC_GPIOB_CLK_ENABLE();
  61. /**SPI2 GPIO Configuration
  62. PB13 ------> SPI2_SCK
  63. PB14 ------> SPI2_MISO
  64. PB15 ------> SPI2_MOSI
  65. */
  66. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
  67. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  68. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  69. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  70. GPIO_InitStruct.Pin = GPIO_PIN_14;
  71. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  72. GPIO_InitStruct.Pull = GPIO_NOPULL;
  73. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  74. #endif
  75. }