source: trunk/firmware/Src/spi.c @ 6

Last change on this file since 6 was 6, checked in by f.jahn, 3 months ago
File size: 3.7 KB
Line 
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) 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
27SPI_HandleTypeDef hspi1;
28
29/* SPI1 init function */
30void 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_2EDGE;
39  hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
40  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
41  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
42  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;                     // TI mode is not compartible with this ADC
43  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
44  hspi1.Init.CRCPolynomial = 7;
45  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
46  hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
47
48  if (HAL_SPI_Init(&hspi1) != HAL_OK)
49  {
50    Error_Handler();
51  }
52
53}
54
55void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
56{
57
58  GPIO_InitTypeDef GPIO_InitStruct = {0};
59  if(spiHandle->Instance==SPI1)
60  {
61  /* USER CODE BEGIN SPI1_MspInit 0 */
62
63  /* USER CODE END SPI1_MspInit 0 */
64    /* SPI1 clock enable */
65    __HAL_RCC_SPI1_CLK_ENABLE();
66 
67    __HAL_RCC_GPIOA_CLK_ENABLE();
68    /**SPI1 GPIO Configuration   
69    PA4     ------> SPI1_NSS
70    PA5     ------> SPI1_SCK
71    PA6     ------> SPI1_MISO
72    PA7     ------> SPI1_MOSI
73    */
74    GPIO_InitStruct.Pin = /*ADC_SPI1_NSS_Pin|*/ADC_SPI1_SCK_Pin|ADC_SPI1_MISO_Pin|ADC_SPI1_MOSI_Pin;
75    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
76    GPIO_InitStruct.Pull = GPIO_NOPULL;
77    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
78    GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
79    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
80
81  /* USER CODE BEGIN SPI1_MspInit 1 */
82
83    GPIO_InitTypeDef GPIOInitStruct = {0};
84
85    GPIOInitStruct.Pin = ADC_SPI1_NSS_Pin;
86    GPIOInitStruct.Mode = GPIO_MODE_OUTPUT_PP;
87    GPIOInitStruct.Pull = GPIO_NOPULL;
88    HAL_GPIO_Init(ADC_SPI1_NSS_GPIO_Port, &GPIOInitStruct);
89    HAL_GPIO_WritePin(ADC_SPI1_NSS_GPIO_Port, ADC_SPI1_SCK_Pin, GPIO_PIN_SET);
90
91  /* USER CODE END SPI1_MspInit 1 */
92  }
93}
94
95void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
96{
97
98  if(spiHandle->Instance==SPI1)
99  {
100  /* USER CODE BEGIN SPI1_MspDeInit 0 */
101
102  /* USER CODE END SPI1_MspDeInit 0 */
103    /* Peripheral clock disable */
104    __HAL_RCC_SPI1_CLK_DISABLE();
105 
106    /**SPI1 GPIO Configuration   
107    PA4     ------> SPI1_NSS
108    PA5     ------> SPI1_SCK
109    PA6     ------> SPI1_MISO
110    PA7     ------> SPI1_MOSI
111    */
112    HAL_GPIO_DeInit(GPIOA, ADC_SPI1_NSS_Pin|ADC_SPI1_SCK_Pin|ADC_SPI1_MISO_Pin|ADC_SPI1_MOSI_Pin);
113
114  /* USER CODE BEGIN SPI1_MspDeInit 1 */
115
116  /* USER CODE END SPI1_MspDeInit 1 */
117  }
118} 
119
120/* USER CODE BEGIN 1 */
121
122/* USER CODE END 1 */
123
124/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.