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>© Copyright (c) 2019 STMicroelectronics. 10 * All rights reserved.</center></h2> 11 * 12 * This software component is licensed by ST under BSD 3-Clause license, 13 * the "License"; You may not use this file except in compliance with the 14 * License. You may obtain a copy of the License at: 15 * opensource.org/licenses/BSD-3-Clause 16 * 17 ****************************************************************************** 18 */ 19 20 /* Includes ------------------------------------------------------------------*/ 21 #include "spi.h" 22 23 /* USER CODE BEGIN 0 */ 24 25 /* USER CODE END 0 */ 26 27 SPI_HandleTypeDef hspi1; 28 29 /* SPI1 init function */ 30 void MX_SPI1_Init(void) 31 { 32 33 hspi1.Instance = SPI1; 34 hspi1.Init.Mode = SPI_MODE_MASTER; 35 hspi1.Init.Direction = SPI_DIRECTION_2LINES; 36 hspi1.Init.DataSize = SPI_DATASIZE_8BIT; 37 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; 38 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; 39 hspi1.Init.NSS = SPI_NSS_SOFT; 40 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; 41 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; 42 hspi1.Init.TIMode = SPI_TIMODE_DISABLE; 43 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; 44 hspi1.Init.CRCPolynomial = 10; 45 if (HAL_SPI_Init(&hspi1) != HAL_OK) 46 { 47 Error_Handler(); 48 } 49 50 } 51 52 void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle) 53 { 54 55 GPIO_InitTypeDef GPIO_InitStruct = {0}; 56 if(spiHandle->Instance==SPI1) 57 { 58 /* USER CODE BEGIN SPI1_MspInit 0 */ 59 60 /* USER CODE END SPI1_MspInit 0 */ 61 /* SPI1 clock enable */ 62 __HAL_RCC_SPI1_CLK_ENABLE(); 63 64 __HAL_RCC_GPIOA_CLK_ENABLE(); 65 /**SPI1 GPIO Configuration 66 PA5 ------> SPI1_SCK 67 PA6 ------> SPI1_MISO 68 PA7 ------> SPI1_MOSI 69 */ 70 GPIO_InitStruct.Pin = SPI1_SCK_Pin|SPI1_MISO_Pin|SPI1_MOSI_Pin; 71 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 72 GPIO_InitStruct.Pull = GPIO_NOPULL; 73 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 74 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; 75 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 76 77 /* USER CODE BEGIN SPI1_MspInit 1 */ 78 79 /* USER CODE END SPI1_MspInit 1 */ 80 } 81 } 82 83 void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle) 84 { 85 86 if(spiHandle->Instance==SPI1) 87 { 88 /* USER CODE BEGIN SPI1_MspDeInit 0 */ 89 90 /* USER CODE END SPI1_MspDeInit 0 */ 91 /* Peripheral clock disable */ 92 __HAL_RCC_SPI1_CLK_DISABLE(); 93 94 /**SPI1 GPIO Configuration 95 PA5 ------> SPI1_SCK 96 PA6 ------> SPI1_MISO 97 PA7 ------> SPI1_MOSI 98 */ 99 HAL_GPIO_DeInit(GPIOA, SPI1_SCK_Pin|SPI1_MISO_Pin|SPI1_MOSI_Pin); 100 101 /* USER CODE BEGIN SPI1_MspDeInit 1 */ 102 103 /* USER CODE END SPI1_MspDeInit 1 */ 104 } 105 } 106 107 /* USER CODE BEGIN 1 */ 108 109 /* USER CODE END 1 */ 110 111 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 112