4
4
*/
5
5
6
6
#include "fsmc_nand.h"
7
+ #include "log.h"
7
8
#include <stm32f10x.h>
8
9
9
10
#define CMD_AREA (uint32_t)(1<<16) /* A16 = CLE high */
33
34
34
35
#define UNDEFINED_CMD 0xFF
35
36
36
- typedef struct
37
+ typedef struct __attribute__(( __packed__ ))
37
38
{
39
+ uint8_t setup_time ;
40
+ uint8_t wait_setup_time ;
41
+ uint8_t hold_setup_time ;
42
+ uint8_t hi_z_setup_time ;
43
+ uint8_t clr_setup_time ;
44
+ uint8_t ar_setup_time ;
38
45
uint8_t row_cycles ;
39
46
uint8_t col_cycles ;
40
47
uint8_t read1_cmd ;
@@ -47,9 +54,9 @@ typedef struct
47
54
uint8_t erase1_cmd ;
48
55
uint8_t erase2_cmd ;
49
56
uint8_t status_cmd ;
50
- } fsmc_cmd_t ;
57
+ } fsmc_conf_t ;
51
58
52
- static fsmc_cmd_t fsmc_cmd ;
59
+ static fsmc_conf_t fsmc_conf ;
53
60
54
61
static void nand_gpio_init (void )
55
62
{
@@ -83,53 +90,66 @@ static void nand_gpio_init(void)
83
90
84
91
}
85
92
86
- static void nand_fsmc_init (chip_info_t * chip_info )
93
+ static void nand_fsmc_init ()
87
94
{
88
95
FSMC_NANDInitTypeDef fsmc_init ;
89
96
FSMC_NAND_PCCARDTimingInitTypeDef timing_init ;
90
97
91
98
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_FSMC , ENABLE );
92
99
93
- timing_init .FSMC_SetupTime = chip_info -> setup_time ;
94
- timing_init .FSMC_WaitSetupTime = chip_info -> wait_setup_time ;
95
- timing_init .FSMC_HoldSetupTime = chip_info -> hold_setup_time ;
96
- timing_init .FSMC_HiZSetupTime = chip_info -> hi_z_setup_time ;
100
+ timing_init .FSMC_SetupTime = fsmc_conf . setup_time ;
101
+ timing_init .FSMC_WaitSetupTime = fsmc_conf . wait_setup_time ;
102
+ timing_init .FSMC_HoldSetupTime = fsmc_conf . hold_setup_time ;
103
+ timing_init .FSMC_HiZSetupTime = fsmc_conf . hi_z_setup_time ;
97
104
98
105
fsmc_init .FSMC_Bank = FSMC_Bank2_NAND ;
99
106
fsmc_init .FSMC_Waitfeature = FSMC_Waitfeature_Enable ;
100
107
fsmc_init .FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b ;
101
108
fsmc_init .FSMC_ECC = FSMC_ECC_Enable ;
102
109
fsmc_init .FSMC_ECCPageSize = FSMC_ECCPageSize_2048Bytes ;
103
- fsmc_init .FSMC_TCLRSetupTime = chip_info -> clr_setup_time ;
104
- fsmc_init .FSMC_TARSetupTime = chip_info -> ar_setup_time ;
110
+ fsmc_init .FSMC_TCLRSetupTime = fsmc_conf . clr_setup_time ;
111
+ fsmc_init .FSMC_TARSetupTime = fsmc_conf . ar_setup_time ;
105
112
fsmc_init .FSMC_CommonSpaceTimingStruct = & timing_init ;
106
113
fsmc_init .FSMC_AttributeSpaceTimingStruct = & timing_init ;
107
114
FSMC_NANDInit (& fsmc_init );
108
115
109
116
FSMC_NANDCmd (FSMC_Bank2_NAND , ENABLE );
110
117
}
111
118
112
- static void nand_cmd_init ( chip_info_t * chip_info )
119
+ static void nand_print_fsmc_info ( )
113
120
{
114
- fsmc_cmd .row_cycles = chip_info -> row_cycles ;
115
- fsmc_cmd .col_cycles = chip_info -> col_cycles ;
116
- fsmc_cmd .read1_cmd = chip_info -> read1_cmd ;
117
- fsmc_cmd .read2_cmd = chip_info -> read2_cmd ;
118
- fsmc_cmd .read_spare_cmd = chip_info -> read_spare_cmd ;
119
- fsmc_cmd .read_id_cmd = chip_info -> read_id_cmd ;
120
- fsmc_cmd .reset_cmd = chip_info -> reset_cmd ;
121
- fsmc_cmd .write1_cmd = chip_info -> write1_cmd ;
122
- fsmc_cmd .write2_cmd = chip_info -> write2_cmd ;
123
- fsmc_cmd .erase1_cmd = chip_info -> erase1_cmd ;
124
- fsmc_cmd .erase2_cmd = chip_info -> erase2_cmd ;
125
- fsmc_cmd .status_cmd = chip_info -> status_cmd ;
121
+ DEBUG_PRINT ("Setup time: %d\r\n" , fsmc_conf .setup_time );
122
+ DEBUG_PRINT ("Wait setup time: %d\r\n" , fsmc_conf .wait_setup_time );
123
+ DEBUG_PRINT ("Hold setup time: %d\r\n" , fsmc_conf .hold_setup_time );
124
+ DEBUG_PRINT ("HiZ setup time: %d\r\n" , fsmc_conf .hi_z_setup_time );
125
+ DEBUG_PRINT ("CLR setip time: %d\r\n" , fsmc_conf .clr_setup_time );
126
+ DEBUG_PRINT ("AR setip time: %d\r\n" , fsmc_conf .ar_setup_time );
127
+ DEBUG_PRINT ("Row cycles: %d\r\n" , fsmc_conf .row_cycles );
128
+ DEBUG_PRINT ("Col. cycles: %d\r\n" , fsmc_conf .col_cycles );
129
+ DEBUG_PRINT ("Read command 1: %d\r\n" , fsmc_conf .read1_cmd );
130
+ DEBUG_PRINT ("Read command 2: %d\r\n" , fsmc_conf .read2_cmd );
131
+ DEBUG_PRINT ("Read spare command: %d\r\n" , fsmc_conf .read_spare_cmd );
132
+ DEBUG_PRINT ("Read ID command: %d\r\n" , fsmc_conf .read_id_cmd );
133
+ DEBUG_PRINT ("Reset command: %d\r\n" , fsmc_conf .reset_cmd );
134
+ DEBUG_PRINT ("Write 1 command: %d\r\n" , fsmc_conf .write1_cmd );
135
+ DEBUG_PRINT ("Write 2 command: %d\r\n" , fsmc_conf .write2_cmd );
136
+ DEBUG_PRINT ("Erase 1 command: %d\r\n" , fsmc_conf .erase1_cmd );
137
+ DEBUG_PRINT ("Erase 2 command: %d\r\n" , fsmc_conf .erase2_cmd );
138
+ DEBUG_PRINT ("Status command: %d\r\n" , fsmc_conf .status_cmd );
126
139
}
127
140
128
- static void nand_init (chip_info_t * chip_info )
141
+ static int nand_init (void * conf , uint32_t conf_size )
129
142
{
143
+ if (conf_size < sizeof (fsmc_conf_t ))
144
+ return -1 ;
145
+
146
+ fsmc_conf = * (fsmc_conf_t * )conf ;
147
+
130
148
nand_gpio_init ();
131
- nand_fsmc_init (chip_info );
132
- nand_cmd_init (chip_info );
149
+ nand_fsmc_init (fsmc_conf );
150
+ nand_print_fsmc_info ();
151
+
152
+ return 0 ;
133
153
}
134
154
135
155
static void nand_uninit ()
@@ -141,7 +161,7 @@ static uint32_t nand_read_status()
141
161
{
142
162
uint32_t data , status ;
143
163
144
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .status_cmd ;
164
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .status_cmd ;
145
165
data = * (__IO uint8_t * )(Bank_NAND_ADDR );
146
166
147
167
if ((data & NAND_ERROR ) == NAND_ERROR )
@@ -177,7 +197,7 @@ static void nand_read_id(chip_id_t *nand_id)
177
197
{
178
198
uint32_t data = 0 ;
179
199
180
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .read_id_cmd ;
200
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .read_id_cmd ;
181
201
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) = 0x00 ;
182
202
183
203
/* Sequence to read ID from NAND flash */
@@ -196,9 +216,9 @@ static void nand_write_page_async(uint8_t *buf, uint32_t page,
196
216
{
197
217
uint32_t i ;
198
218
199
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .write1_cmd ;
219
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .write1_cmd ;
200
220
201
- switch (fsmc_cmd .col_cycles )
221
+ switch (fsmc_conf .col_cycles )
202
222
{
203
223
case 1 :
204
224
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) = 0x00 ;
@@ -222,7 +242,7 @@ static void nand_write_page_async(uint8_t *buf, uint32_t page,
222
242
break ;
223
243
}
224
244
225
- switch (fsmc_cmd .row_cycles )
245
+ switch (fsmc_conf .row_cycles )
226
246
{
227
247
case 1 :
228
248
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) = ADDR_1st_CYCLE (page );
@@ -249,18 +269,18 @@ static void nand_write_page_async(uint8_t *buf, uint32_t page,
249
269
for (i = 0 ; i < page_size ; i ++ )
250
270
* (__IO uint8_t * )(Bank_NAND_ADDR | DATA_AREA ) = buf [i ];
251
271
252
- if (fsmc_cmd .write2_cmd != UNDEFINED_CMD )
253
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .write2_cmd ;
272
+ if (fsmc_conf .write2_cmd != UNDEFINED_CMD )
273
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .write2_cmd ;
254
274
}
255
275
256
276
static uint32_t nand_read_data (uint8_t * buf , uint32_t page ,
257
277
uint32_t page_offset , uint32_t data_size )
258
278
{
259
279
uint32_t i ;
260
280
261
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .read1_cmd ;
281
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .read1_cmd ;
262
282
263
- switch (fsmc_cmd .col_cycles )
283
+ switch (fsmc_conf .col_cycles )
264
284
{
265
285
case 1 :
266
286
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) =
@@ -293,7 +313,7 @@ static uint32_t nand_read_data(uint8_t *buf, uint32_t page,
293
313
break ;
294
314
}
295
315
296
- switch (fsmc_cmd .row_cycles )
316
+ switch (fsmc_conf .row_cycles )
297
317
{
298
318
case 1 :
299
319
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) = ADDR_1st_CYCLE (page );
@@ -317,8 +337,8 @@ static uint32_t nand_read_data(uint8_t *buf, uint32_t page,
317
337
break ;
318
338
}
319
339
320
- if (fsmc_cmd .read2_cmd != UNDEFINED_CMD )
321
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .read2_cmd ;
340
+ if (fsmc_conf .read2_cmd != UNDEFINED_CMD )
341
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .read2_cmd ;
322
342
323
343
for (i = 0 ; i < data_size ; i ++ )
324
344
buf [i ] = * (__IO uint8_t * )(Bank_NAND_ADDR | DATA_AREA );
@@ -336,12 +356,12 @@ static uint32_t nand_read_spare_data(uint8_t *buf, uint32_t page,
336
356
{
337
357
uint32_t i ;
338
358
339
- if (fsmc_cmd .read_spare_cmd == UNDEFINED_CMD )
359
+ if (fsmc_conf .read_spare_cmd == UNDEFINED_CMD )
340
360
return FLASH_STATUS_INVALID_CMD ;
341
361
342
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .read_spare_cmd ;
362
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .read_spare_cmd ;
343
363
344
- switch (fsmc_cmd .col_cycles )
364
+ switch (fsmc_conf .col_cycles )
345
365
{
346
366
case 1 :
347
367
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) =
@@ -374,7 +394,7 @@ static uint32_t nand_read_spare_data(uint8_t *buf, uint32_t page,
374
394
break ;
375
395
}
376
396
377
- switch (fsmc_cmd .row_cycles )
397
+ switch (fsmc_conf .row_cycles )
378
398
{
379
399
case 1 :
380
400
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) = ADDR_1st_CYCLE (page );
@@ -406,9 +426,9 @@ static uint32_t nand_read_spare_data(uint8_t *buf, uint32_t page,
406
426
407
427
static uint32_t nand_erase_block (uint32_t page )
408
428
{
409
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .erase1_cmd ;
429
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .erase1_cmd ;
410
430
411
- switch (fsmc_cmd .row_cycles )
431
+ switch (fsmc_conf .row_cycles )
412
432
{
413
433
case 1 :
414
434
* (__IO uint8_t * )(Bank_NAND_ADDR | ADDR_AREA ) = ADDR_1st_CYCLE (page );
@@ -432,8 +452,8 @@ static uint32_t nand_erase_block(uint32_t page)
432
452
break ;
433
453
}
434
454
435
- if (fsmc_cmd .erase2_cmd != UNDEFINED_CMD )
436
- * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_cmd .erase2_cmd ;
455
+ if (fsmc_conf .erase2_cmd != UNDEFINED_CMD )
456
+ * (__IO uint8_t * )(Bank_NAND_ADDR | CMD_AREA ) = fsmc_conf .erase2_cmd ;
437
457
438
458
return nand_get_status ();
439
459
}
0 commit comments