spi.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. ******************************************************************************
  3. * File Name : SPI.c
  4. * Description : This file provides code for the configuration
  5. * of the SPI instances.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "spi.h"
  51. /* USER CODE BEGIN 0 */
  52. /* USER CODE END 0 */
  53. SPI_HandleTypeDef hspi1;
  54. /* SPI1 init function */
  55. void MX_SPI1_Init(void) {
  56. hspi1.Instance = SPI1;
  57. hspi1.Init.Mode = SPI_MODE_MASTER;
  58. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  59. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  60. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  61. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  62. hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
  63. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
  64. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  65. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  66. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  67. hspi1.Init.CRCPolynomial = 10;
  68. if (HAL_SPI_Init(&hspi1) != HAL_OK) {
  69. Error_Handler();
  70. }
  71. }
  72. void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) {
  73. GPIO_InitTypeDef GPIO_InitStruct = { 0 };
  74. if (spiHandle->Instance == SPI1) {
  75. /* USER CODE BEGIN SPI1_MspInit 0 */
  76. /* USER CODE END SPI1_MspInit 0 */
  77. /* SPI1 clock enable */
  78. __HAL_RCC_SPI1_CLK_ENABLE()
  79. ;
  80. __HAL_RCC_GPIOA_CLK_ENABLE()
  81. ;
  82. /**SPI1 GPIO Configuration
  83. PA4 ------> SPI1_NSS
  84. PA5 ------> SPI1_SCK
  85. PA6 ------> SPI1_MISO
  86. PA7 ------> SPI1_MOSI
  87. */
  88. GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
  89. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  90. GPIO_InitStruct.Pull = GPIO_NOPULL;
  91. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  92. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  93. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  94. /* USER CODE BEGIN SPI1_MspInit 1 */
  95. /* USER CODE END SPI1_MspInit 1 */
  96. }
  97. }
  98. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) {
  99. if (spiHandle->Instance == SPI1) {
  100. /* USER CODE BEGIN SPI1_MspDeInit 0 */
  101. /* USER CODE END SPI1_MspDeInit 0 */
  102. /* Peripheral clock disable */
  103. __HAL_RCC_SPI1_CLK_DISABLE();
  104. /**SPI1 GPIO Configuration
  105. PA4 ------> SPI1_NSS
  106. PA5 ------> SPI1_SCK
  107. PA6 ------> SPI1_MISO
  108. PA7 ------> SPI1_MOSI
  109. */
  110. HAL_GPIO_DeInit(GPIOA,
  111. GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
  112. /* USER CODE BEGIN SPI1_MspDeInit 1 */
  113. /* USER CODE END SPI1_MspDeInit 1 */
  114. }
  115. }
  116. /* USER CODE BEGIN 1 */
  117. /* USER CODE END 1 */
  118. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/