spi(1875).c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file spi.c
  5. * @brief This file provides code for the configuration
  6. * of the SPI instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "spi.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. SPI_HandleTypeDef hspi1;
  25. /* SPI1 init function */
  26. void MX_SPI1_Init(void)
  27. {
  28. /* USER CODE BEGIN SPI1_Init 0 */
  29. /* USER CODE END SPI1_Init 0 */
  30. /* USER CODE BEGIN SPI1_Init 1 */
  31. /* USER CODE END SPI1_Init 1 */
  32. hspi1.Instance = SPI1;
  33. hspi1.Init.Mode = SPI_MODE_MASTER;
  34. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  35. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  36. hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  37. hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  38. hspi1.Init.NSS = SPI_NSS_SOFT;
  39. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  40. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  41. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  42. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  43. hspi1.Init.CRCPolynomial = 10;
  44. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  45. {
  46. Error_Handler();
  47. }
  48. /* USER CODE BEGIN SPI1_Init 2 */
  49. /* USER CODE END SPI1_Init 2 */
  50. }
  51. void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
  52. {
  53. GPIO_InitTypeDef GPIO_InitStruct = {0};
  54. if(spiHandle->Instance==SPI1)
  55. {
  56. /* USER CODE BEGIN SPI1_MspInit 0 */
  57. /* USER CODE END SPI1_MspInit 0 */
  58. /* SPI1 clock enable */
  59. __HAL_RCC_SPI1_CLK_ENABLE();
  60. __HAL_RCC_GPIOA_CLK_ENABLE();
  61. /**SPI1 GPIO Configuration
  62. PA5 ------> SPI1_SCK
  63. PA6 ------> SPI1_MISO
  64. PA7 ------> SPI1_MOSI
  65. */
  66. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_7;
  67. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  68. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  69. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  70. GPIO_InitStruct.Pin = GPIO_PIN_6;
  71. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  72. GPIO_InitStruct.Pull = GPIO_NOPULL;
  73. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  74. /* USER CODE BEGIN SPI1_MspInit 1 */
  75. /* USER CODE END SPI1_MspInit 1 */
  76. }
  77. }
  78. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
  79. {
  80. if(spiHandle->Instance==SPI1)
  81. {
  82. /* USER CODE BEGIN SPI1_MspDeInit 0 */
  83. /* USER CODE END SPI1_MspDeInit 0 */
  84. /* Peripheral clock disable */
  85. __HAL_RCC_SPI1_CLK_DISABLE();
  86. /**SPI1 GPIO Configuration
  87. PA5 ------> SPI1_SCK
  88. PA6 ------> SPI1_MISO
  89. PA7 ------> SPI1_MOSI
  90. */
  91. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
  92. /* USER CODE BEGIN SPI1_MspDeInit 1 */
  93. /* USER CODE END SPI1_MspDeInit 1 */
  94. }
  95. }
  96. /* USER CODE BEGIN 1 */
  97. /* USER CODE END 1 */