-
Notifications
You must be signed in to change notification settings - Fork 1
/
mt48lc4m32b2.c
345 lines (310 loc) · 8.99 KB
/
mt48lc4m32b2.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
/**
******************************************************************************
* @file mt48lc4m32b2.c
* @author MCD Application Team
* @brief mt48lc4m32b2 sdram 128Mb driver file
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2019 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "mt48lc4m32b2.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup Components
* @{
*/
/** @defgroup MT48LC4M32B2 MT48LC4M32B2
* @brief This file provides a set of functions needed to drive the
* MT48LC4M32B2 SDRAM memory.
* @{
*/
/** @defgroup MT48LC4M32B2_Private_Variables MT48LC4M32B2 Private Variables
* @{
*/
static FMC_SDRAM_CommandTypeDef Command;
/**
* @}
*/
/** @defgroup MT48LC4M32B2_Function_Prototypes MT48LC4M32B2 Function Prototypes
* @{
*/
static int32_t MT48LC4M32B2_Delay(uint32_t Delay);
/**
* @}
*/
/** @defgroup MT48LC4M32B2_Exported_Functions MT48LC4M32B2 Exported Functions
* @{
*/
/**
* @brief Initializes the MT48LC4M32B2 SDRAM memory
* @param Ctx : Component object pointer
* @param pRegMode : Pointer to Register Mode stucture
* @retval error status
*/
int32_t MT48LC4M32B2_Init(SDRAM_HandleTypeDef *Ctx, MT48LC4M32B2_Context_t *pRegMode)
{
int32_t ret = MT48LC4M32B2_ERROR;
/* Step 1: Configure a clock configuration enable command */
if(MT48LC4M32B2_ClockEnable(Ctx, pRegMode->TargetBank) == MT48LC4M32B2_OK)
{
/* Step 2: Insert 100 us minimum delay */
/* Inserted delay is equal to 1 ms due to systick time base unit (ms) */
(void)MT48LC4M32B2_Delay(1);
/* Step 3: Configure a PALL (precharge all) command */
if(MT48LC4M32B2_Precharge(Ctx, pRegMode->TargetBank) == MT48LC4M32B2_OK)
{
/* Step 4: Configure a Refresh command */
if(MT48LC4M32B2_RefreshMode(Ctx, pRegMode->TargetBank, pRegMode->RefreshMode) == MT48LC4M32B2_OK)
{
/* Step 5: Program the external memory mode register */
if(MT48LC4M32B2_ModeRegConfig(Ctx, pRegMode) == MT48LC4M32B2_OK)
{
/* Step 6: Set the refresh rate counter */
if(MT48LC4M32B2_RefreshRate(Ctx, pRegMode->RefreshRate) == MT48LC4M32B2_OK)
{
ret = MT48LC4M32B2_OK;
}
}
}
}
}
return ret;
}
/**
* @brief Enable SDRAM clock
* @param Ctx : Component object pointer
* @param Interface : Could be FMC_SDRAM_CMD_TARGET_BANK1 or FMC_SDRAM_CMD_TARGET_BANK2
* @retval error status
*/
int32_t MT48LC4M32B2_ClockEnable(SDRAM_HandleTypeDef *Ctx, uint32_t Interface)
{
Command.CommandMode = MT48LC4M32B2_CLK_ENABLE_CMD;
Command.CommandTarget = Interface;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = 0;
/* Send the command */
if(HAL_SDRAM_SendCommand(Ctx, &Command, MT48LC4M32B2_TIMEOUT) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Precharge all sdram banks
* @param Ctx : Component object pointer
* @param Interface : Could be FMC_SDRAM_CMD_TARGET_BANK1 or FMC_SDRAM_CMD_TARGET_BANK2
* @retval error status
*/
int32_t MT48LC4M32B2_Precharge(SDRAM_HandleTypeDef *Ctx, uint32_t Interface)
{
Command.CommandMode = MT48LC4M32B2_PALL_CMD;
Command.CommandTarget = Interface;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = 0;
/* Send the command */
if(HAL_SDRAM_SendCommand(Ctx, &Command, MT48LC4M32B2_TIMEOUT) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Program the external memory mode register
* @param Ctx : Component object pointer
* @param pRegMode : Pointer to Register Mode stucture
* @retval error status
*/
int32_t MT48LC4M32B2_ModeRegConfig(SDRAM_HandleTypeDef *Ctx, MT48LC4M32B2_Context_t *pRegMode)
{
uint32_t tmpmrd;
/* Program the external memory mode register */
tmpmrd = (uint32_t)pRegMode->BurstLength |\
pRegMode->BurstType |\
pRegMode->CASLatency |\
pRegMode->OperationMode |\
pRegMode->WriteBurstMode;
Command.CommandMode = MT48LC4M32B2_LOAD_MODE_CMD;
Command.CommandTarget = pRegMode->TargetBank;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = tmpmrd;
/* Send the command */
if(HAL_SDRAM_SendCommand(Ctx, &Command, MT48LC4M32B2_TIMEOUT) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Program the SDRAM timing
* @param Ctx : Component object pointer
* @param pTiming : Pointer to SDRAM timing configuration stucture
* @retval error status
*/
int32_t MT48LC4M32B2_TimingConfig(SDRAM_HandleTypeDef *Ctx, FMC_SDRAM_TimingTypeDef *pTiming)
{
/* Program the SDRAM timing */
if(HAL_SDRAM_Init(Ctx, pTiming) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Configure Refresh mode
* @param Ctx : Component object pointer
* @param Interface : Could be FMC_SDRAM_CMD_TARGET_BANK1 or FMC_SDRAM_CMD_TARGET_BANK2
* @param RefreshMode : Could be MT48LC4M32B2_CMD_AUTOREFRESH_MODE or
* MT48LC4M32B2_CMD_SELFREFRESH_MODE
* @retval error status
*/
int32_t MT48LC4M32B2_RefreshMode(SDRAM_HandleTypeDef *Ctx, uint32_t Interface, uint32_t RefreshMode)
{
Command.CommandMode = RefreshMode;
Command.CommandTarget = Interface;
Command.AutoRefreshNumber = 8;
Command.ModeRegisterDefinition = 0;
/* Send the command */
if(HAL_SDRAM_SendCommand(Ctx, &Command, MT48LC4M32B2_TIMEOUT) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Set the device refresh rate
* @param Ctx : Component object pointer
* @param RefreshCount : The refresh rate to be programmed
* @retval error status
*/
int32_t MT48LC4M32B2_RefreshRate(SDRAM_HandleTypeDef *Ctx, uint32_t RefreshCount)
{
/* Set the device refresh rate */
if(HAL_SDRAM_ProgramRefreshRate(Ctx, RefreshCount) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Enter Power mode
* @param Ctx : Component object pointer
* @param Interface : Could be FMC_SDRAM_CMD_TARGET_BANK1 or FMC_SDRAM_CMD_TARGET_BANK2
* @retval error status
*/
int32_t MT48LC4M32B2_EnterPowerMode(SDRAM_HandleTypeDef *Ctx, uint32_t Interface)
{
Command.CommandMode = MT48LC4M32B2_POWERDOWN_MODE_CMD;
Command.CommandTarget = Interface;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = 0;
/* Send the command */
if(HAL_SDRAM_SendCommand(Ctx, &Command, MT48LC4M32B2_TIMEOUT) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Exit Power mode
* @param Ctx : Component object pointer
* @param Interface : Could be FMC_SDRAM_CMD_TARGET_BANK1 or FMC_SDRAM_CMD_TARGET_BANK2
* @retval error status
*/
int32_t MT48LC4M32B2_ExitPowerMode(SDRAM_HandleTypeDef *Ctx, uint32_t Interface)
{
Command.CommandMode = MT48LC4M32B2_NORMAL_MODE_CMD;
Command.CommandTarget = Interface;
Command.AutoRefreshNumber = 1;
Command.ModeRegisterDefinition = 0;
/* Send the command */
if(HAL_SDRAM_SendCommand(Ctx, &Command, MT48LC4M32B2_TIMEOUT) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @brief Sends command to the SDRAM bank.
* @param Ctx : Component object pointer
* @param SdramCmd : Pointer to SDRAM command structure
* @retval SDRAM status
*/
int32_t MT48LC4M32B2_Sendcmd(SDRAM_HandleTypeDef *Ctx, FMC_SDRAM_CommandTypeDef *SdramCmd)
{
if(HAL_SDRAM_SendCommand(Ctx, SdramCmd, MT48LC4M32B2_TIMEOUT) != HAL_OK)
{
return MT48LC4M32B2_ERROR;
}
else
{
return MT48LC4M32B2_OK;
}
}
/**
* @}
*/
/** @defgroup MT48LC4M32B2_Private_Functions MT48LC4M32B2 Private Functions
* @{
*/
/**
* @brief This function provides accurate delay (in milliseconds)
* @param Delay: specifies the delay time length, in milliseconds
* @retval MT48LC4M32B2_OK
*/
static int32_t MT48LC4M32B2_Delay(uint32_t Delay)
{
uint32_t tickstart;
tickstart = HAL_GetTick();
while((HAL_GetTick() - tickstart) < Delay)
{
}
return MT48LC4M32B2_OK;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/