XMC — NAND Flash
固件库: AT32F403A_407_Firmware_Library V2.2.2
芯片: AT32
源文件: AT32/AT32F403A_407_Firmware_Library_V2.2.2/project/at_start_f403a/examples/xmc/nand/ecc/src/main.c
功能简介
本示例演示外部存储控制器。XMC/EXMC 可以连接并口 LCD、NAND Flash、SRAM 等外部总线设备,实现高速数据传输。
完整代码
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
|
#include "xmc_ecc.h"
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
int main(void)
{
/* configure the system clock */
system_clock_config();
/* led initalization */
at32_board_init();
/* usart initalization */
uart_print_init(115200);
/* get system clock */
crm_clocks_freq_get(&crm_clocks_freq_struct);
/* xmc initialization */
nand_init();
/* nand reset command */
nand_reset();
delay_us(10);
/* nand read id command */
nand_read_id(&nand_id_struct);
/* verify the nand id */
/* nand support:spansion:s34ml01g100tfi003 micron:mt29f16g08cbaca sandisk:sdtnqgama-008g toshiba:tc58teg6ddkta00 samsung:k9gag08u0e-scb0 hynix:h27u1g8f2ctr
id :0x01f1001d id :0x2c48044a id :0x45de9493 id :0x98de9493 id :0xecd58472 id :0xadf1801d
*/
if((nand_id_struct.maker_id == NAND_AT_MakerID) && (nand_id_struct.device_id == NAND_AT_DeviceID))
{
/* nand memory address to write to */
write_read_addr_struct.zone = 0x00;
write_read_addr_struct.block = 0x00;
write_read_addr_struct.page = 0x00;
write_read_addr_struct.byte = 0x00;
/* erase the nand first block */
status = nand_erase_block(write_read_addr_struct);
/* write data to xmc nand memory */
/* fill the buffer to send */
fill_buffer(tx_buffer, BUFFER_SIZE , 0x0);
/* change the regular to make ecc value isn't 0 */
tx_buffer[10]=0x03;
/* calculate ecc value while transmiting */
xmc_nand_ecc_enable(XMC_BANK2_NAND, TRUE);
status = nand_write_small_page(tx_buffer, write_read_addr_struct, page_number);
/* save the right ecc_value (in this case ,we suppose it as the true value) */
ecc_value_write_last = ecc_value_write;
/* change the 85 data like there is a 1 bit error happened */
status = nand_erase_block(write_read_addr_struct);
tx_buffer[10]=0x23;
status = nand_write_small_page(tx_buffer, write_read_addr_struct, page_number);
/* calculate ecc value while transmiting */
xmc_nand_ecc_enable(XMC_BANK2_NAND, TRUE);
/* read back the written data */
status = nand_read_small_page (rx_buffer, write_read_addr_struct, page_number);
/* ecc check and correct 1 bit error */
nand_ecc_correction(rx_buffer ,ecc_value_write_last ,ecc_value_read);
if(rx_buffer[10]==0x03)
{
printf("ecc is work\r\n");
}
else
{
printf("ecc is not work\r\n");
}
}
else
{
printf("the id is error\r\n");
}
while(1)
{
}
}
|
代码讲解
系统时钟初始化:配置 MCU 的主频和时钟树。不同芯片的时钟配置不同,一般由工具生成。
板级初始化:初始化开发板上的 LED、按键等基础外设。
串口初始化:配置串口波特率(这里是 115200),使能 printf 重定向。
printf 输出:通过串口打印调试信息。需要先调用 uart_print_init 完成重定向。