spi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. ******************************************************************************
  3. * File Name : SPI.c
  4. * Description : This file provides code for the configuration
  5. * of the SPI instances.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under Ultimate Liberty license
  13. * SLA0044, the "License"; You may not use this file except in compliance with
  14. * the License. You may obtain a copy of the License at:
  15. * www.st.com/SLA0044
  16. *
  17. ******************************************************************************
  18. */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "spi.h"
  21. /* USER CODE BEGIN 0 */
  22. /* USER CODE END 0 */
  23. SPI_HandleTypeDef hspi1;
  24. /* SPI1 init function */
  25. void MX_SPI1_Init(void)
  26. {
  27. hspi1.Instance = SPI1;
  28. hspi1.Init.Mode = SPI_MODE_MASTER;
  29. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  30. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  31. hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  32. hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
  33. hspi1.Init.NSS = SPI_NSS_SOFT;
  34. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
  35. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  36. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  37. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  38. hspi1.Init.CRCPolynomial = 10;
  39. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  40. {
  41. Error_Handler();
  42. }
  43. }
  44. void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
  45. {
  46. GPIO_InitTypeDef GPIO_InitStruct = {0};
  47. if(spiHandle->Instance==SPI1)
  48. {
  49. /* USER CODE BEGIN SPI1_MspInit 0 */
  50. /* USER CODE END SPI1_MspInit 0 */
  51. /* SPI1 clock enable */
  52. __HAL_RCC_SPI1_CLK_ENABLE();
  53. __HAL_RCC_GPIOA_CLK_ENABLE();
  54. __HAL_RCC_GPIOB_CLK_ENABLE();
  55. /**SPI1 GPIO Configuration
  56. PA6 ------> SPI1_MISO
  57. PA7 ------> SPI1_MOSI
  58. PB3 ------> SPI1_SCK
  59. */
  60. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  61. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  62. GPIO_InitStruct.Pull = GPIO_NOPULL;
  63. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  64. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  65. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  66. GPIO_InitStruct.Pin = GPIO_PIN_3;
  67. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  68. GPIO_InitStruct.Pull = GPIO_NOPULL;
  69. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  70. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  71. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  72. /* USER CODE BEGIN SPI1_MspInit 1 */
  73. /* USER CODE END SPI1_MspInit 1 */
  74. }
  75. }
  76. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
  77. {
  78. if(spiHandle->Instance==SPI1)
  79. {
  80. /* USER CODE BEGIN SPI1_MspDeInit 0 */
  81. /* USER CODE END SPI1_MspDeInit 0 */
  82. /* Peripheral clock disable */
  83. __HAL_RCC_SPI1_CLK_DISABLE();
  84. /**SPI1 GPIO Configuration
  85. PA6 ------> SPI1_MISO
  86. PA7 ------> SPI1_MOSI
  87. PB3 ------> SPI1_SCK
  88. */
  89. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6|GPIO_PIN_7);
  90. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_3);
  91. /* USER CODE BEGIN SPI1_MspDeInit 1 */
  92. /* USER CODE END SPI1_MspDeInit 1 */
  93. }
  94. }
  95. /* USER CODE BEGIN 1 */
  96. /* USER CODE END 1 */
  97. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/