source: ctrl/firmware/Main/CubeMX/Core/Src/spi.c

Last change on this file was 51, checked in by Zed, 6 weeks ago

SDMMC low level driver was added.

File size: 5.2 KB
Line 
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
23/* USER CODE BEGIN 0 */
24
25/* USER CODE END 0 */
26
27SPI_HandleTypeDef hspi4;
28DMA_HandleTypeDef hdma_spi4_tx;
29
30/* SPI4 init function */
31void MX_SPI4_Init(void)
32{
33
34  /* USER CODE BEGIN SPI4_Init 0 */
35
36  /* USER CODE END SPI4_Init 0 */
37
38  /* USER CODE BEGIN SPI4_Init 1 */
39
40  /* USER CODE END SPI4_Init 1 */
41  hspi4.Instance = SPI4;
42  hspi4.Init.Mode = SPI_MODE_MASTER;
43  hspi4.Init.Direction = SPI_DIRECTION_1LINE;
44  hspi4.Init.DataSize = SPI_DATASIZE_9BIT;
45  hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;
46  hspi4.Init.CLKPhase = SPI_PHASE_1EDGE;
47  hspi4.Init.NSS = SPI_NSS_HARD_OUTPUT;
48  hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
49  hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
50  hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
51  hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
52  hspi4.Init.CRCPolynomial = 0x0;
53  hspi4.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
54  hspi4.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
55  hspi4.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
56  hspi4.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
57  hspi4.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
58  hspi4.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
59  hspi4.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
60  hspi4.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
61  hspi4.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
62  hspi4.Init.IOSwap = SPI_IO_SWAP_DISABLE;
63  if (HAL_SPI_Init(&hspi4) != HAL_OK)
64  {
65    Error_Handler();
66  }
67  /* USER CODE BEGIN SPI4_Init 2 */
68
69  /* USER CODE END SPI4_Init 2 */
70
71}
72
73void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
74{
75
76  GPIO_InitTypeDef GPIO_InitStruct = {0};
77  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
78  if(spiHandle->Instance==SPI4)
79  {
80  /* USER CODE BEGIN SPI4_MspInit 0 */
81
82  /* USER CODE END SPI4_MspInit 0 */
83
84  /** Initializes the peripherals clock
85  */
86    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SPI4;
87    PeriphClkInitStruct.Spi45ClockSelection = RCC_SPI45CLKSOURCE_HSE;
88    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
89    {
90      Error_Handler();
91    }
92
93    /* SPI4 clock enable */
94    __HAL_RCC_SPI4_CLK_ENABLE();
95
96    __HAL_RCC_GPIOE_CLK_ENABLE();
97    /**SPI4 GPIO Configuration
98    PE11     ------> SPI4_NSS
99    PE12     ------> SPI4_SCK
100    PE14     ------> SPI4_MOSI
101    */
102    GPIO_InitStruct.Pin = SPI4_NSS_DISPLAY_Pin|SPI4_SCK_DISPLAY_Pin|SPI4_MOSI_DISPLAY_Pin;
103    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
104    GPIO_InitStruct.Pull = GPIO_NOPULL;
105    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
106    GPIO_InitStruct.Alternate = GPIO_AF5_SPI4;
107    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
108
109    /* SPI4 DMA Init */
110    /* SPI4_TX Init */
111    hdma_spi4_tx.Instance = DMA1_Stream0;
112    hdma_spi4_tx.Init.Request = DMA_REQUEST_SPI4_TX;
113    hdma_spi4_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
114    hdma_spi4_tx.Init.PeriphInc = DMA_PINC_DISABLE;
115    hdma_spi4_tx.Init.MemInc = DMA_MINC_ENABLE;
116    hdma_spi4_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
117    hdma_spi4_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
118    hdma_spi4_tx.Init.Mode = DMA_NORMAL;
119    hdma_spi4_tx.Init.Priority = DMA_PRIORITY_LOW;
120    hdma_spi4_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
121    if (HAL_DMA_Init(&hdma_spi4_tx) != HAL_OK)
122    {
123      Error_Handler();
124    }
125
126    __HAL_LINKDMA(spiHandle,hdmatx,hdma_spi4_tx);
127
128    /* SPI4 interrupt Init */
129    HAL_NVIC_SetPriority(SPI4_IRQn, 0, 0);
130    HAL_NVIC_EnableIRQ(SPI4_IRQn);
131  /* USER CODE BEGIN SPI4_MspInit 1 */
132
133  /* USER CODE END SPI4_MspInit 1 */
134  }
135}
136
137void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
138{
139
140  if(spiHandle->Instance==SPI4)
141  {
142  /* USER CODE BEGIN SPI4_MspDeInit 0 */
143
144  /* USER CODE END SPI4_MspDeInit 0 */
145    /* Peripheral clock disable */
146    __HAL_RCC_SPI4_CLK_DISABLE();
147
148    /**SPI4 GPIO Configuration
149    PE11     ------> SPI4_NSS
150    PE12     ------> SPI4_SCK
151    PE14     ------> SPI4_MOSI
152    */
153    HAL_GPIO_DeInit(GPIOE, SPI4_NSS_DISPLAY_Pin|SPI4_SCK_DISPLAY_Pin|SPI4_MOSI_DISPLAY_Pin);
154
155    /* SPI4 DMA DeInit */
156    HAL_DMA_DeInit(spiHandle->hdmatx);
157
158    /* SPI4 interrupt Deinit */
159    HAL_NVIC_DisableIRQ(SPI4_IRQn);
160  /* USER CODE BEGIN SPI4_MspDeInit 1 */
161
162  /* USER CODE END SPI4_MspDeInit 1 */
163  }
164}
165
166/* USER CODE BEGIN 1 */
167
168/* USER CODE END 1 */
Note: See TracBrowser for help on using the repository browser.