summaryrefslogtreecommitdiff
path: root/bsp/apm32/libraries/APM32F10x_Library/APM32F10x_StdPeriphDriver/src/apm32f10x_dmc.c
blob: 619550794fe8ace407851ba9517d9275d48a6016 (plain)
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
/*!
 * @file        apm32f10x_dmc.c
 *
 * @brief       This file contains all the functions for the DMC controler peripheral
 *
 * @version     V1.0.1
 *
 * @date        2021-03-23
 *
 */
#include "apm32f10x_dmc.h"

/** @addtogroup Peripherals_Library Standard Peripheral Library
  @{
*/

/** @addtogroup DMC_Driver  DMC Driver
  @{
*/

/** @addtogroup  DMC_Fuctions Fuctions
  @{
*/

/*!
 * @brief       DMC controler configuration
 *
 * @param       dmcConfig: pointer to a DMC_Config_T structure
 *
 * @retval      None
 */
void DMC_Config(DMC_Config_T * dmcConfig)
{
    DMC->SW_B.MCSW = 1;
    while(!DMC->CTRL1_B.INIT);

    DMC->CFG_B.BAWCFG = dmcConfig->bankWidth;
    DMC->CFG_B.RAWCFG = dmcConfig->rowWidth;
    DMC->CFG_B.CAWCFG = dmcConfig->colWidth;

    DMC->MASK_B.MSIZESEL = dmcConfig->memorySize;
    DMC->CTRL2_B.CPHACFG = dmcConfig->clkPhase;

    DMC_ConfigTiming(&dmcConfig->timing);

    DMC->CTRL1_B.MODESET = 1;
    while(!DMC->CTRL1_B.MODESET);

    DMC->CTRL2_B.RDDEN = 1;
    DMC->CTRL2_B.RDDCFG = 7;
}

/*!
 * @brief       Fills each dmcConfig member with its default value
 *
 * @param       dmcConfig: pointer to a DMC_Config_T structure
 *
 * @retval      None
 */
void DMC_ConfigStructInit(DMC_Config_T * dmcConfig)
{
    dmcConfig->bankWidth   = DMC_BANK_WIDTH_2;
    dmcConfig->clkPhase    = DMC_CLK_PHASE_REVERSE;
    dmcConfig->colWidth    = DMC_COL_WIDTH_10;
    dmcConfig->rowWidth    = DMC_ROW_WIDTH_13;
    dmcConfig->memorySize  = DMC_MEMORY_SIZE_8MB;

    DMC_ConfigTimingStructInit(&dmcConfig->timing);
}

/*!
 * @brief       Timing configuration
 *
 * @param       timingConfig: pointer to a DMC_TimingConfig_T structure
 *
 * @retval      None
 */
void DMC_ConfigTiming(DMC_TimingConfig_T * timingConfig)
{
    DMC->TIM0_B.RASMINTSEL = timingConfig->tRAS;
    DMC->TIM0_B.DTIMSEL    = timingConfig->tRCD;
    DMC->TIM0_B.PCPSEL     = timingConfig->tRP;
    DMC->TIM0_B.WRTIMSEL   = timingConfig->tWR;
    DMC->TIM0_B.ARPSEL     = timingConfig->tARP;
    DMC->TIM0_B.ATACP      = timingConfig->tCMD;

    DMC->TIM0_B.CASLSEL0  =  timingConfig->latencyCAS & 0x03;
    DMC->TIM0_B.ECASLSEL1 = (timingConfig->latencyCAS >> 2) & 0x01;

    DMC->TIM0_B.XSR0  =  timingConfig->tXSR & 0X0F;
    DMC->TIM0_B.EXSR1 = (timingConfig->tXSR >> 4) & 0X1F;

    DMC->REF_B.RCYCCFG = timingConfig->tRFP;
}

/*!
 * @brief       Fills each config member with its default value
 *
 * @param       timingConfig: pointer to a DMC_TimingConfig_T structure
 *
 * @retval      None
 */
void DMC_ConfigTimingStructInit(DMC_TimingConfig_T * timingConfig)
{
    timingConfig->latencyCAS = DMC_CAS_LATENCY_3;
    timingConfig->tARP = DMC_AUTO_REFRESH_10;
    timingConfig->tRAS = DMC_RAS_MINIMUM_5;
    timingConfig->tCMD = DMC_ATA_CMD_7;
    timingConfig->tRCD = DMC_DELAY_TIME_2;
    timingConfig->tRP  = DMC_PRECHARGE_2;
    timingConfig->tWR  = DMC_NEXT_PRECHARGE_2;
    timingConfig->tXSR = 6;
    timingConfig->tRFP = 0xC3;
}

/*!
 * @brief       Set number of bank bits
 *
 * @param       bankWidth: Specifies the bank bits number
 *                         This parameter can be one of the following values:
 *                         @arg DMC_BANK_WIDTH_1
 *                         @arg DMC_BANK_WIDTH_2
 * @retval      None
 */
void DMC_ConfigBankWidth(DMC_BANK_WIDTH_T bankWidth)
{
    DMC->CFG_B.BAWCFG = bankWidth;
}

/*!
 * @brief       Set address bus width
 *
 * @param       rowWidth: Specifies the row address bits number
 *                        This parameter can be one of the following values:
 *                        @arg DMC_ROW_WIDTH_11
 *                        @arg DMC_ROW_WIDTH_12
 *                        @arg DMC_ROW_WIDTH_13
 *                        @arg DMC_ROW_WIDTH_14
 *                        @arg DMC_ROW_WIDTH_15
 *                        @arg DMC_ROW_WIDTH_16
 * @param       colWidth: Specifies the column address bits number
 *                        This parameter can be one of the following values:
 *                        @arg DMC_COL_WIDTH_8
 *                        @arg DMC_COL_WIDTH_9
 *                        @arg DMC_COL_WIDTH_10
 *                        @arg DMC_COL_WIDTH_11
 *                        @arg DMC_COL_WIDTH_12
 *                        @arg DMC_COL_WIDTH_13
 *                        @arg DMC_COL_WIDTH_14
 *                        @arg DMC_COL_WIDTH_15
 * @retval      None
 */
void DMC_ConfigAddrWidth(DMC_ROW_WIDTH_T rowWidth, DMC_COL_WIDTH_T colWidth)
{
    DMC->CFG_B.RAWCFG = rowWidth;
    DMC->CFG_B.CAWCFG = colWidth;
}

/*!
 * @brief       Set stable time after power up
 *
 * @param       stableTime: Numper of the clock, can be 0x0000 to 0xFFFF
 *
 * @retval      None
 */
void DMC_ConfigStableTimePowerup(uint16_t stableTime)
{
    DMC->TIM1_B.STBTIM = stableTime;
}

/*!
 * @brief       Number of auto-refreshes during initialization
 *
 * @param       num: Number of auto-refreshes can 1 to 16
 *                   This parameter can be one of the following values:
 *                        @arg DMC_AUTO_REFRESH_1
 *                        @arg DMC_AUTO_REFRESH_2
 *                             ......
 *                        @arg DMC_AUTO_REFRESH_15
 *                        @arg DMC_AUTO_REFRESH_16
 *
 * @retval      None
 */
void DMC_ConfigAutoRefreshNumDuringInit(DMC_AUTO_REFRESH_T num)
{
    DMC->TIM1_B.ARNUMCFG = num;
}

/*!
 * @brief       Number of DMC internal banks to be open at any time;
 *
 * @param       num:  Number of banks can 1 to 16
 *                   This parameter can be one of the following values:
 *                        @arg DMC_BANK_NUMBER_1
 *                        @arg DMC_BANK_NUMBER_2
 *                             ......
 *                        @arg DMC_BANK_NUMBER_15
 *                        @arg DMC_BANK_NUMBER_16
 * @retval      None
 */
void DMC_ConfigOpenBank(DMC_BANK_NUMBER_T num)
{
    DMC->CTRL1_B.BANKNUMCFG = num;
}

/*!
 * @brief       Read self-refresh status
 *
 * @param       None
 *
 * @retval      The status of self-refresh (SET or RESET)
 */
uint8_t DMC_ReadSelfRefreshStatus(void)
{
    uint8_t ret;

    ret = DMC->CTRL1_B.SRMFLG ? SET : RESET;

    return ret;
}

/*!
 * @brief       Set update mode bit
 *
 * @param       None
 *
 * @retval      None
 */
void DMC_EnableUpdateMode(void)
{
    DMC->CTRL1_B.MODESET = 1;
}

/*!
 * @brief       Enter power down mode
 *
 * @param       None
 *
 * @retval      None
 */
void DMC_EnterPowerdownMode(void)
{
    DMC->CTRL1_B.PDMEN = 1;
}

/*!
 * @brief       Exit self-refresh mode
 *
 * @param       None
 *
 * @retval      None
 */
void DMC_EixtSlefRefreshMode(void)
{
    DMC->CTRL1_B.SRMEN = 0;
}

/*!
 * @brief       Enter self-refresh mode
 *
 * @param       None
 *
 * @retval      None
 */
void DMC_EnterSlefRefreshMode(void)
{
    DMC->CTRL1_B.SRMEN = 1;
}

/*!
 * @brief       Init DMC
 *
 * @param       None
 *
 * @retval      None
 */
void DMC_EnableInit(void)
{
    DMC->CTRL1_B.INIT = 1;
}

/*!
 * @brief       Set refresh type before enter self-refresh
 *
 * @param       refresh: Specifies the refresh type
 *                The parameter can be one of following values:
 *                @arg DMC_REFRESH_ROW_ONE: Refresh one row
 *                @arg DMC_REFRESH_ROW_ALL: Refresh all row
 *
 * @retval      None
 */
void DMC_ConfigFullRefreshBeforeSR(DMC_REFRESH_T refresh)
{
    DMC->CTRL1_B.FRBSREN = refresh;
}

/*!
 * @brief       Set refresh type after exit self-refresh
 *
 * @param       refresh: Specifies the refresh type
 *                The parameter can be one of following values:
 *                @arg DMC_REFRESH_ROW_ONE: Refresh one row
 *                @arg DMC_REFRESH_ROW_ALL: Refresh all row
 *
 * @retval      None
 */
void DMC_ConfigFullRefreshAfterSR(DMC_REFRESH_T refresh)
{
    DMC->CTRL1_B.FRASREN = refresh;
}

/*!
 * @brief       Config precharge type
 *
 * @param       precharge: Specifies the precharge type
 *                The parameter can be one of following values:
 *                @arg DMC_PRECHARGE_IM:    Immediate precharge
 *                @arg DMC_PRECHARGE_DELAY: Delayed precharge
 *
 * @retval      None
 */
void DMC_ConfigPrechargeType(DMC_PRECHARE_T precharge)
{
    DMC->CTRL1_B.PCACFG = precharge;
}

/*!
 * @brief       Config refresh period
 *
 * @param       period:   Specifies the refresh period, can be 0x0000 to 0xFFFF
 *
 * @retval      None
 */
void DMC_ConfigRefreshPeriod(uint16_t period)
{
    DMC->REF_B.RCYCCFG = period;
}

/*!
 * @brief       Config memory size
 *
 * @param       memorySize: Specifies memory size
 *                The parameter can be one of following values:
 *                @arg DMC_MEMORY_SIZE_0:     Memory size is no link
 *                @arg DMC_MEMORY_SIZE_64KB:  Memory size is 64KB
 *                @arg DMC_MEMORY_SIZE_128KB: Memory size is 128KB
 *                @arg DMC_MEMORY_SIZE_256KB: Memory size is 256KB
 *                @arg DMC_MEMORY_SIZE_512KB: Memory size is 512KB
 *                @arg DMC_MEMORY_SIZE_1MB:   Memory size is 1MB
 *                @arg DMC_MEMORY_SIZE_2MB:   Memory size is 2MB
 *                @arg DMC_MEMORY_SIZE_4MB:   Memory size is 4MB
 *                @arg DMC_MEMORY_SIZE_8MB:   Memory size is 8MB
 *                @arg DMC_MEMORY_SIZE_16MB:  Memory size is 16MB
 *                @arg DMC_MEMORY_SIZE_32MB:  Memory size is 32MB
 *                @arg DMC_MEMORY_SIZE_64MB:  Memory size is 64MB
 *                @arg DMC_MEMORY_SIZE_128MB: Memory size is 128MB
 *                @arg DMC_MEMORY_SIZE_256MB: Memory size is 256MB
 *
 * @retval      None
 */
void DMC_ConfigMemorySize(DMC_MEMORY_SIZE_T memorySize)
{
    DMC->MASK_B.MSIZESEL = memorySize;
}

/*!
 * @brief       Enable DMC controler
 *
 * @param       None
 *
 * @retval      None
 */
void DMC_Enable(void)
{
    DMC->SW_B.MCSW = 1;
}

/*!
 * @brief       Disable DMC controler
 *
 * @param       None
 *
 * @retval      None
 */
void DMC_Disable(void)
{
    DMC->SW_B.MCSW = 0;
}

/*!
 * @brief       Set DMC clock phase
 *
 * @param       clkPhase: Specifies clock phase
 *                The parameter can be one of following values:
 *                @arg DMC_CLK_PHASE_NORMAL: Clock phase is normal
 *                @arg DMC_CLK_PHASE_REVERSE: Clock phase is reverse
 *
 * @retval      None
 *
 */
void DMC_ConfigClockPhase(DMC_CLK_PHASE_T clkPhase)
{
    DMC->CTRL2_B.CPHACFG = clkPhase;
}

/**@} end of group DMC_Fuctions*/
/**@} end of group DMC_Driver */
/**@} end of group Peripherals_Library*/