Fixed fsmc timing some more so it's stable
[SCSI2SD-V6.git] / STM32CubeMX / SCSI2SD-V6 / Src / fsmc.c
1 /**
2 ******************************************************************************
3 * File Name : FSMC.c
4 * Description : This file provides code for the configuration
5 * of the FSMC peripheral.
6 ******************************************************************************
7 *
8 * COPYRIGHT(c) 2016 STMicroelectronics
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3. Neither the name of STMicroelectronics nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 ******************************************************************************
33 */
34 /* Includes ------------------------------------------------------------------*/
35 #include "fsmc.h"
36
37 #include "gpio.h"
38
39 /* USER CODE BEGIN 0 */
40
41 /* USER CODE END 0 */
42
43 SRAM_HandleTypeDef hsram1;
44
45 /* FSMC initialization function */
46 void MX_FSMC_Init(void)
47 {
48 FSMC_NORSRAM_TimingTypeDef Timing;
49
50 /** Perform the SRAM1 memory initialization sequence
51 */
52 hsram1.Instance = FSMC_NORSRAM_DEVICE;
53 hsram1.Extended = FSMC_NORSRAM_EXTENDED_DEVICE;
54 /* hsram1.Init */
55 hsram1.Init.NSBank = FSMC_NORSRAM_BANK1;
56 hsram1.Init.DataAddressMux = FSMC_DATA_ADDRESS_MUX_ENABLE;
57 hsram1.Init.MemoryType = FSMC_MEMORY_TYPE_PSRAM;
58 hsram1.Init.MemoryDataWidth = FSMC_NORSRAM_MEM_BUS_WIDTH_16;
59 hsram1.Init.BurstAccessMode = FSMC_BURST_ACCESS_MODE_DISABLE;
60 hsram1.Init.WaitSignalPolarity = FSMC_WAIT_SIGNAL_POLARITY_LOW;
61 hsram1.Init.WrapMode = FSMC_WRAP_MODE_DISABLE;
62 hsram1.Init.WaitSignalActive = FSMC_WAIT_TIMING_BEFORE_WS;
63 hsram1.Init.WriteOperation = FSMC_WRITE_OPERATION_ENABLE;
64 hsram1.Init.WaitSignal = FSMC_WAIT_SIGNAL_DISABLE;
65 hsram1.Init.ExtendedMode = FSMC_EXTENDED_MODE_DISABLE;
66 hsram1.Init.AsynchronousWait = FSMC_ASYNCHRONOUS_WAIT_DISABLE;
67 hsram1.Init.WriteBurst = FSMC_WRITE_BURST_DISABLE;
68 /* Timing */
69
70 // 1 clock to read the address, + 3 for the synchroniser
71 Timing.AddressSetupTime = 4;
72 Timing.AddressHoldTime = 1;
73
74 // 3 for synchroniser, 1 to skip hold time, 1 to process read, 1 to output
75 Timing.DataSetupTime = 6;
76
77 // Allow a clock for us to release signals, plus 3 for the synchroniser to
78 // realise the cycle has ended. Need to avoid both devices acting as outputs
79 // on the multiplexed lines at the same time.
80 Timing.BusTurnAroundDuration = 4;
81
82 Timing.CLKDivision = 16; // Ignored for async
83 Timing.DataLatency = 17; // Ignored for async
84 Timing.AccessMode = FSMC_ACCESS_MODE_A;
85 /* ExtTiming */
86
87 HAL_SRAM_Init(&hsram1, &Timing, NULL);
88
89 }
90
91 static int FSMC_Initialized = 0;
92
93 static void HAL_FSMC_MspInit(void){
94 /* USER CODE BEGIN FSMC_MspInit 0 */
95
96 /* USER CODE END FSMC_MspInit 0 */
97 GPIO_InitTypeDef GPIO_InitStruct;
98 if (FSMC_Initialized) {
99 return;
100 }
101 FSMC_Initialized = 1;
102 /* Peripheral clock enable */
103 __FSMC_CLK_ENABLE();
104
105 /** FSMC GPIO Configuration
106 PE7 ------> FSMC_DA4
107 PE8 ------> FSMC_DA5
108 PE9 ------> FSMC_DA6
109 PE10 ------> FSMC_DA7
110 PE11 ------> FSMC_DA8
111 PE12 ------> FSMC_DA9
112 PE13 ------> FSMC_DA10
113 PE14 ------> FSMC_DA11
114 PE15 ------> FSMC_DA12
115 PD14 ------> FSMC_DA0
116 PD15 ------> FSMC_DA1
117 PD0 ------> FSMC_DA2
118 PD1 ------> FSMC_DA3
119 PD4 ------> FSMC_NOE
120 PD5 ------> FSMC_NWE
121 PD7 ------> FSMC_NE1
122 PD8 ------> FSMC_DA13
123 PD9 ------> FSMC_DA14
124 PD10 ------> FSMC_DA15
125 PB7 ------> FSMC_NL
126 PE0 ------> FSMC_NBL0
127 PE1 ------> FSMC_NBL1
128 */
129 /* GPIO_InitStruct */
130 GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
131 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
132 GPIO_InitStruct.Pull = GPIO_NOPULL;
133 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
134 GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
135
136 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
137
138 /* GPIO_InitStruct */
139 GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1
140 |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10;
141 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
142 GPIO_InitStruct.Pull = GPIO_NOPULL;
143 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
144 GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
145
146 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
147
148 /* GPIO_InitStruct */
149 GPIO_InitStruct.Pin = GPIO_PIN_7;
150 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
151 GPIO_InitStruct.Pull = GPIO_NOPULL;
152 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
153 GPIO_InitStruct.Alternate = GPIO_AF12_FSMC;
154
155 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
156
157 /* USER CODE BEGIN FSMC_MspInit 1 */
158
159 /* USER CODE END FSMC_MspInit 1 */
160 }
161
162 void HAL_SRAM_MspInit(SRAM_HandleTypeDef* hsram){
163 /* USER CODE BEGIN SRAM_MspInit 0 */
164
165 /* USER CODE END SRAM_MspInit 0 */
166 HAL_FSMC_MspInit();
167 /* USER CODE BEGIN SRAM_MspInit 1 */
168
169 /* USER CODE END SRAM_MspInit 1 */
170 }
171
172 static int FSMC_DeInitialized = 0;
173
174 static void HAL_FSMC_MspDeInit(void){
175 /* USER CODE BEGIN FSMC_MspDeInit 0 */
176
177 /* USER CODE END FSMC_MspDeInit 0 */
178 if (FSMC_DeInitialized) {
179 return;
180 }
181 FSMC_DeInitialized = 1;
182 /* Peripheral clock enable */
183 __FSMC_CLK_DISABLE();
184
185 /** FSMC GPIO Configuration
186 PE7 ------> FSMC_DA4
187 PE8 ------> FSMC_DA5
188 PE9 ------> FSMC_DA6
189 PE10 ------> FSMC_DA7
190 PE11 ------> FSMC_DA8
191 PE12 ------> FSMC_DA9
192 PE13 ------> FSMC_DA10
193 PE14 ------> FSMC_DA11
194 PE15 ------> FSMC_DA12
195 PD14 ------> FSMC_DA0
196 PD15 ------> FSMC_DA1
197 PD0 ------> FSMC_DA2
198 PD1 ------> FSMC_DA3
199 PD4 ------> FSMC_NOE
200 PD5 ------> FSMC_NWE
201 PD7 ------> FSMC_NE1
202 PD8 ------> FSMC_DA13
203 PD9 ------> FSMC_DA14
204 PD10 ------> FSMC_DA15
205 PB7 ------> FSMC_NL
206 PE0 ------> FSMC_NBL0
207 PE1 ------> FSMC_NBL1
208 */
209
210 HAL_GPIO_DeInit(GPIOE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15);
211
212 HAL_GPIO_DeInit(GPIOD, GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1
213 |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
214
215 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
216
217 /* USER CODE BEGIN FSMC_MspDeInit 1 */
218
219 /* USER CODE END FSMC_MspDeInit 1 */
220 }
221
222 void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef* hsram){
223 /* USER CODE BEGIN SRAM_MspDeInit 0 */
224
225 /* USER CODE END SRAM_MspDeInit 0 */
226 HAL_FSMC_MspDeInit();
227 /* USER CODE BEGIN SRAM_MspDeInit 1 */
228
229 /* USER CODE END SRAM_MspDeInit 1 */
230 }
231 /**
232 * @}
233 */
234
235 /**
236 * @}
237 */
238
239 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/