-
Notifications
You must be signed in to change notification settings - Fork 2
/
stm32h743i_eval_sdram.c
477 lines (419 loc) · 14.3 KB
/
stm32h743i_eval_sdram.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
******************************************************************************
* @file stm32h743i_eval_sdram.c
* @author MCD Application Team
* @brief This file includes the SDRAM driver for the IS42S32800G memory
* device mounted on STM32H743I-EVAL evaluation boards.
@verbatim
How To use this driver:
-----------------------
- This driver is used to drive the IS42S32800G SDRAM external memory mounted
on STM32H743I-EVAL evaluation board.
- This driver does not need a specific component driver for the SDRAM device
to be included with.
Driver description:
------------------
+ Initialization steps:
o Initialize the SDRAM external memory using the BSP_SDRAM_Init() function. This
function includes the MSP layer hardware resources initialization and the
FMC controller configuration to interface with the external SDRAM memory.
o It contains the SDRAM initialization sequence to program the SDRAM external
device using the function BSP_SDRAM_Initialization_sequence(). Note that this
sequence is standard for all SDRAM devices, but can include some differences
from a device to another. If it is the case, the right sequence should be
implemented separately.
+ SDRAM read/write operations
o SDRAM external memory can be accessed with read/write operations once it is
initialized.
Read/write operation can be performed with AHB access using the functions
BSP_SDRAM_ReadData()/BSP_SDRAM_WriteData(), or by MDMA transfer using the functions
BSP_SDRAM_ReadData_DMA()/BSP_SDRAM_WriteData_DMA().
o The AHB access is performed with 32-bit width transaction, the MDMA transfer
configuration is fixed at single (no burst) word transfer (see the
SDRAM_MspInit() static function).
o User can implement his own functions for read/write access with his desired
configurations.
o If interrupt mode is used for MDMA transfer, the function BSP_SDRAM_MDMA_IRQHandler()
is called in IRQ handler file, to serve the generated interrupt once the MDMA
transfer is complete.
o You can send a command to the SDRAM device in runtime using the function
BSP_SDRAM_Sendcmd(), and giving the desired command as parameter chosen between
the predefined commands of the "FMC_SDRAM_CommandTypeDef" structure.
@endverbatim
******************************************************************************
* @attention
*
* Copyright (c) 2019 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h743i_eval_sdram.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32H743I_EVAL
* @{
*/
/** @addtogroup STM32H743I_EVAL_SDRAM
* @{
*/
/** @defgroup STM32H743I_EVAL_SDRAM_Exported_Variables Exported Variables
* @{
*/
SDRAM_HandleTypeDef hsdram[SDRAM_INSTANCES_NBR];
/**
* @}
*/
/** @defgroup STM32H743I_EVAL_SDRAM_Private_Variables Private Variables
* @{
*/
#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
static uint32_t IsMspCallbacksValid = 0;
#endif
/**
* @}
*/
/** @defgroup STM32H743I_EVAL_SDRAM_Private_Functions_Prototypes Private Functions Prototypes
* @{
*/
static void SDRAM_MspInit(SDRAM_HandleTypeDef *hSdram);
static void SDRAM_MspDeInit(SDRAM_HandleTypeDef *hSdram);
/**
* @}
*/
/** @defgroup STM32H743I_EVAL_SDRAM_Exported_Functions Exported Functions
* @{
*/
/**
* @brief Initializes the SDRAM device.
* @param Instance SDRAM Instance
* @retval BSP status
*/
int32_t BSP_SDRAM_Init(uint32_t Instance)
{
int32_t ret;
static IS42S32800G_Context_t pRegMode;
if(Instance >= SDRAM_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
ret = BSP_ERROR_NONE;
#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
/* Register the SDRAM MSP Callbacks */
if(IsMspCallbacksValid == 0)
{
if(BSP_SDRAM_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
#else
/* Msp SDRAM initialization */
SDRAM_MspInit(&hsdram[Instance]);
#endif /* USE_HAL_SDRAM_REGISTER_CALLBACKS */
if(ret == BSP_ERROR_NONE)
{
if(MX_SDRAM_BANK2_Init(&hsdram[Instance]) != HAL_OK)
{
ret = BSP_ERROR_NO_INIT;
}
else
{
/* External memory mode register configuration */
pRegMode.TargetBank = FMC_SDRAM_CMD_TARGET_BANK2;
pRegMode.RefreshMode = IS42S32800G_AUTOREFRESH_MODE_CMD;
pRegMode.RefreshRate = REFRESH_COUNT;
pRegMode.BurstLength = IS42S32800G_BURST_LENGTH_1;
pRegMode.BurstType = IS42S32800G_BURST_TYPE_SEQUENTIAL;
pRegMode.CASLatency = IS42S32800G_CAS_LATENCY_3;
pRegMode.OperationMode = IS42S32800G_OPERATING_MODE_STANDARD;
pRegMode.WriteBurstMode = IS42S32800G_WRITEBURST_MODE_SINGLE;
/* SDRAM initialization sequence */
if(IS42S32800G_Init(&hsdram[Instance], &pRegMode) != IS42S32800G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
}
}
return ret;
}
/**
* @brief DeInitializes the SDRAM device.
* @param Instance SDRAM Instance
* @retval BSP status
*/
int32_t BSP_SDRAM_DeInit(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;;
if(Instance >= SDRAM_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* SDRAM device de-initialization */
hsdram[Instance].Instance = FMC_SDRAM_DEVICE;
(void)HAL_SDRAM_DeInit(&hsdram[Instance]);
#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 0)
/* SDRAM controller de-initialization */
SDRAM_MspDeInit(&hsdram[Instance]);
#endif /* (USE_HAL_SDRAM_REGISTER_CALLBACKS == 0) */
}
return ret;
}
/**
* @brief Initializes the SDRAM periperal.
* @param hSdram SDRAM handle
* @retval HAL status
*/
__weak HAL_StatusTypeDef MX_SDRAM_BANK2_Init(SDRAM_HandleTypeDef *hSdram)
{
FMC_SDRAM_TimingTypeDef sdram_timing;
/* SDRAM device configuration */
hSdram->Instance = FMC_SDRAM_DEVICE;
/* SDRAM handle configuration */
hSdram->Init.SDBank = FMC_SDRAM_BANK2;
hSdram->Init.ColumnBitsNumber = FMC_SDRAM_COLUMN_BITS_NUM_9;
hSdram->Init.RowBitsNumber = FMC_SDRAM_ROW_BITS_NUM_12;
hSdram->Init.MemoryDataWidth = FMC_SDRAM_MEM_BUS_WIDTH_32;
hSdram->Init.InternalBankNumber = FMC_SDRAM_INTERN_BANKS_NUM_4;
hSdram->Init.CASLatency = FMC_SDRAM_CAS_LATENCY_3;
hSdram->Init.WriteProtection = FMC_SDRAM_WRITE_PROTECTION_DISABLE;
hSdram->Init.SDClockPeriod = FMC_SDRAM_CLOCK_PERIOD_2;
hSdram->Init.ReadBurst = FMC_SDRAM_RBURST_ENABLE;
hSdram->Init.ReadPipeDelay = FMC_SDRAM_RPIPE_DELAY_0;
/* Timing configuration for as SDRAM */
sdram_timing.LoadToActiveDelay = 2;
sdram_timing.ExitSelfRefreshDelay = 7;
sdram_timing.SelfRefreshTime = 4;
sdram_timing.RowCycleDelay = 7;
sdram_timing.WriteRecoveryTime = 2;
sdram_timing.RPDelay = 2;
sdram_timing.RCDDelay = 2;
/* SDRAM controller initialization */
if(HAL_SDRAM_Init(hSdram, &sdram_timing) != HAL_OK)
{
return HAL_ERROR;
}
return HAL_OK;
}
#if (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1)
/**
* @brief Default BSP SDRAM Msp Callbacks
* @param Instance SDRAM Instance
* @retval BSP status
*/
int32_t BSP_SDRAM_RegisterDefaultMspCallbacks (uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= SDRAM_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if(HAL_SDRAM_RegisterCallback(&hsdram[Instance], HAL_SDRAM_MSP_INIT_CB_ID, SDRAM_MspInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
if(HAL_SDRAM_RegisterCallback(&hsdram[Instance], HAL_SDRAM_MSP_DEINIT_CB_ID, SDRAM_MspDeInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsMspCallbacksValid = 1;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP SDRAM Msp Callback registering
* @param Instance SDRAM Instance
* @param CallBacks pointer to MspInit/MspDeInit callbacks functions
* @retval BSP status
*/
int32_t BSP_SDRAM_RegisterMspCallbacks (uint32_t Instance, BSP_SDRAM_Cb_t *CallBacks)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= SDRAM_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if(HAL_SDRAM_RegisterCallback(&hsdram[Instance], HAL_SDRAM_MSP_INIT_CB_ID, CallBacks->pMspInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
if(HAL_SDRAM_RegisterCallback(&hsdram[Instance], HAL_SDRAM_MSP_DEINIT_CB_ID, CallBacks->pMspDeInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
IsMspCallbacksValid = 1;
}
}
/* Return BSP status */
return ret;
}
#endif /* (USE_HAL_SDRAM_REGISTER_CALLBACKS == 1) */
/**
* @brief Sends command to the SDRAM bank.
* @param Instance SDRAM Instance
* @param SdramCmd Pointer to SDRAM command structure
* @retval BSP status
*/
int32_t BSP_SDRAM_SendCmd(uint32_t Instance, FMC_SDRAM_CommandTypeDef *SdramCmd)
{
int32_t ret;
if(Instance >= SDRAM_INSTANCES_NBR)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if(IS42S32800G_Sendcmd(&hsdram[Instance], SdramCmd) != IS42S32800G_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
return ret;
}
/**
* @brief This function handles SDRAM MDMA interrupt request.
* @param Instance SDRAM instance
* @retval None
*/
void BSP_SDRAM_IRQHandler(uint32_t Instance)
{
HAL_MDMA_IRQHandler(hsdram[Instance].hmdma);
}
/**
* @}
*/
/** @defgroup STM32H743I_EVAL_SDRAM_Private_Functions Private Functions
* @{
*/
/**
* @brief Initializes SDRAM MSP.
* @param hSdram SDRAM handle
* @retval None
*/
static void SDRAM_MspInit(SDRAM_HandleTypeDef *hSdram)
{
static MDMA_HandleTypeDef mdma_handle;
GPIO_InitTypeDef gpio_init_structure;
/* Enable FMC clock */
__HAL_RCC_FMC_CLK_ENABLE();
/* Enable chosen MDMAx clock */
SDRAM_MDMAx_CLK_ENABLE();
/* Enable GPIOs clock */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
/* Common GPIO configuration */
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init_structure.Alternate = GPIO_AF12_FMC;
/* GPIOD configuration */
gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_8| GPIO_PIN_9 | GPIO_PIN_10 |\
GPIO_PIN_14 | GPIO_PIN_15;
HAL_GPIO_Init(GPIOD, &gpio_init_structure);
/* GPIOE configuration */
gpio_init_structure.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;
HAL_GPIO_Init(GPIOE, &gpio_init_structure);
/* GPIOF configuration */
gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2| GPIO_PIN_3 | GPIO_PIN_4 |\
GPIO_PIN_5 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 |\
GPIO_PIN_15;
HAL_GPIO_Init(GPIOF, &gpio_init_structure);
/* GPIOG configuration */
gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 |\
GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_8 | GPIO_PIN_15;
HAL_GPIO_Init(GPIOG, &gpio_init_structure);
/* GPIOH configuration */
gpio_init_structure.Pin = GPIO_PIN_5 | GPIO_PIN_6 | 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;
HAL_GPIO_Init(GPIOH, &gpio_init_structure);
/* GPIOI configuration */
gpio_init_structure.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 |\
GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_9 | GPIO_PIN_10;
HAL_GPIO_Init(GPIOI, &gpio_init_structure);
/* Configure common MDMA parameters */
mdma_handle.Init.Request = MDMA_REQUEST_SW;
mdma_handle.Init.TransferTriggerMode = MDMA_BLOCK_TRANSFER;
mdma_handle.Init.Priority = MDMA_PRIORITY_HIGH;
mdma_handle.Init.Endianness = MDMA_LITTLE_ENDIANNESS_PRESERVE;
mdma_handle.Init.SourceInc = MDMA_SRC_INC_WORD;
mdma_handle.Init.DestinationInc = MDMA_DEST_INC_WORD;
mdma_handle.Init.SourceDataSize = MDMA_SRC_DATASIZE_WORD;
mdma_handle.Init.DestDataSize = MDMA_DEST_DATASIZE_WORD;
mdma_handle.Init.DataAlignment = MDMA_DATAALIGN_PACKENABLE;
mdma_handle.Init.SourceBurst = MDMA_SOURCE_BURST_SINGLE;
mdma_handle.Init.DestBurst = MDMA_DEST_BURST_SINGLE;
mdma_handle.Init.BufferTransferLength = 128;
mdma_handle.Init.SourceBlockAddressOffset = 0;
mdma_handle.Init.DestBlockAddressOffset = 0;
mdma_handle.Instance = SDRAM_MDMAx_CHANNEL;
/* Associate the MDMA handle */
__HAL_LINKDMA(hSdram, hmdma, mdma_handle);
/* De-initialize the Stream for new transfer */
HAL_MDMA_DeInit(&mdma_handle);
/* Configure the MDMA stream */
(void)HAL_MDMA_Init(&mdma_handle);
/* NVIC configuration for DMA transfer complete interrupt */
HAL_NVIC_SetPriority(SDRAM_MDMAx_IRQn, BSP_SDRAM_IT_PRIORITY, 0);
HAL_NVIC_EnableIRQ(SDRAM_MDMAx_IRQn);
}
/**
* @brief DeInitializes SDRAM MSP.
* @param hSdram SDRAM handle
* @retval None
*/
static void SDRAM_MspDeInit(SDRAM_HandleTypeDef *hSdram)
{
static MDMA_HandleTypeDef mdma_handle;
/* Prevent unused argument(s) compilation warning */
UNUSED(hSdram);
/* Disable NVIC configuration for MDMA interrupt */
HAL_NVIC_DisableIRQ(SDRAM_MDMAx_IRQn);
/* Deinitialize the stream for new transfer */
mdma_handle.Instance = SDRAM_MDMAx_CHANNEL;
(void)HAL_MDMA_DeInit(&mdma_handle);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/