嗨对话框,
我正在尝试使程序写入并读取自己的闪存一些传感器数据。它始终失败,自第二次写入它,我通过再次读出书面数据来验证书面数据。你有一些建议解决问题吗?
我的代码如下:
unsigned char rd_data [256];
unsigned char wr_data [256];
无符号短期CRC16(Const Unsigned Char * NDATA,无符号短WLONGTH)
{
静态const unsigned短wcRctable [] = {
0x0000,0xc0c1,0xc181,0x0140,0xc301,0x03c0,0x0280,0xC241,
0xc601,0x06c0,0x0780,0xc741,0x0500,0xc5c1,0xc481,0x0440,
0xcc01,0x0cc0,0x0d80,0xcd41,0x0f00,0xcfc1,0xce81,0x0e40,
0x0a00,0xcac1,0xcb81,0x0b40,0xc901,0x09c0,0x0880,0xc841,
0xD801,0x18C0,0x1980,0xD941,0x1b00,0xdbc1,0xda81,0x1a40,
0x1e00,0xdec1,0xdf81,0x1f40,0xdd01,0x1dc0,0x1c80,0xdc41,
0x1400,0xD4C1,0xD581,0x1540,0xD701,0x17C0,0x1680,0xD641,
0xD201,0x12C0,0x1380,0xD341,0x1100,0xD1C1,0xD081,0x1040,
0xF001,0x30C0,0x3180,0xF141,0x3300,0xF3C1,0xF281,0x3240,
0x3600,0xF6C1,0xF781,0x3740,0xF501,0x35C0,0x3480,0xF441,
0x3c00,0xfcc1,0xfd81,0x3d40,0xff01,0x3fc0,0x3e80,0xfe41,
0xFA01,0x3AC0,0x3b80,0xfb41,0x3900,0xf9c1,0xf881,0x3840,
0x2800,0xe8c1,0xe981,0x2940,0xeb01,0x2bc0,0x2a80,0xea41,
0xEE01,0x2EC0,0x2F80,0xEF41,0x2D00,0xEDC1,0xEC81,0x2C40,
0xe401,0x24c0,0x2580,0xe541,0x2700,0xe7c1,0xe681,0x2640,
0x2200,0xe2c1,0xe381,0x2340,0xe101,0x21c0,0x2080,0xe041,
0x001,0x60c0,0x6180,0xa141,0x6300,0xa3c1,0xa281,0x6240,
0x6600,0xa6c1,0x781,0x6740,0x0501,0x65c0,0x6480,0xA441,
0x6c00,0xacc1,0xad81,0x6d40,0xAF01,0x6fc0,0x6e80,0xae41,
0xAA01,0x6AC0,0x6B80,0xAB41,0x6900,0xA9C1,0xA881,0x6840,
0x7800,0xb8c1,0xb981,0x7940,0xbb01,0x7bc0,0x7a80,0xba41,
0xbe01,0x7ec0,0x7f80,0xbf41,0x7d00,0xbdc1,0xbc81,0x7c40,
0xb401,0x74c0,0x7580,0xb541,0x7700,0xb7c1,0xb681,0x7640,
0x7200,0xb2c1,0xb381,0x7340,0xb101,0x71c0,0x7080,0xb041,
0x5000,0x90c1,0x9181,0x5140,0x9301,0x53c0,0x5280,0x9241,
0x9601,0x56c0,0x5780,0x9741,0x5500,0x95c1,0x9481,0x5440,
0x9c01,0x5cc0,0x5d80,0x9d41,0x5f00,0x9fc1,0x9e81,0x5e40,
0x5a00,0x9ac1,0x9b81,0x5b40,0x9901,0x59c0,0x59c0,0x5880,0x9841,0x9841,
0x8801,0x48c0,0x4980,0x8941,0x4b00,0x8bc1,0x8a81,0x4a40,
0x4e00,0x8ec1,0x8f81,0x4f40,0x8d01,0x4dc0,0x4c80,0x8c41,
0x4400,0x84c1,0x8581,0x4540,0x8701,0x47c0,0x4680,0x8641,
0x8201,0x42c0,0x4380,0x8341,0x4100,0x81c1,0x8081,0x4040};
未签名的char ntemp;
unsigned short wcrcword = 0xffff;
而(Wlength--)
{
ntemp = * ndata ++ ^ wcrcword;
wcrcword >> = 8;
wcrcword ^ = wccctable [ntemp];
}
返回wcrcword;
}
静态void da14583_spi_flash_init(void)
{
spi_pad_t cs_pad_param;
//配置内部闪存GPIO
gpio_configurepin(da14583_spi_flash_en_gpio_port,da14583_spi_flash_en_gpio_pin,输出,pid_spi_en,true);
gpio_configurepin(da14583_spi_flash_clk_gpio_port,da14583_spi_flash_clk_gpio_pin,输出,pid_spi_clk,false);
gpio_configurepin(da14583_spi_flash_do_gpio_port,da14583_spi_flash_do_gpio_pin,输出,pid_spi_do,false);
gpio_configurepin(da14583_spi_flash_di_gpio_port,da14583_spi_flash_di_gpio_pin,输入,pid_spi_di,false);
cs_pad_param.port = da14583_spi_flash_en_gpio_port;
cs_pad_param.pin = da14583_spi_flash_en_gpio_pin;
//初始化SPI驱动程序
spi_init(&cs_pad_param,spi_mode_8bit,spi_rool_master,spi_clk_idle_pol_low,spi_pha_mode_0,spi_mint_disable,spi_xtal_div_8);
//初始化SPI闪存驱动程序
spi_flash_init(da14583_spi_flash_size,da14583_spi_flash_page_size);
//上电闪存
SPI_FLASH_RELEASE_FROM_POWER_DOWN();
}
静态void da14583_spi_flash_release(void)
{
//电源闪烁
spi_flash_power_down();
//发布SPI控制器
spi_release();
//发布内部闪存GPIO
gpio_configurepin(da14583_spi_flash_en_gpio_port,da14583_spi_flash_en_gpio_pin,输出,pid_gpio,true);
gpio_configurepin(da14583_spi_flash_clk_gpio_port,da14583_spi_flash_clk_gpio_pin,input_pulldown,pid_gpio,false);
gpio_configurepin(da14583_spi_flash_do_gpio_port,da14583_spi_flash_do_gpio_pin,input_pulldown,pid_gpio,false);
gpio_configurepin(da14583_spi_flash_di_gpio_port,da14583_spi_flash_di_gpio_pin,input_pulldown,pid_gpio,false);
}
void UpdateWhitelistold(void)//写入闪存并将白名单加载到内存中以进行应用
{
// system_init();
periph_init();
无符号短wcrc = 0xffff;
unsigned char crch;
无符号char crcl;
INT16_T BTRD;
unsigned char i;
// uint16_t read_size = 256;
unsigned char write_size = 256;//将此变量设置为所需的读取大小
Memset(RD_DATA,0x00,256);
Memset(WR_DATA,0x00,256);
wr_data [0] = 0x01;
wr_data [1] = 0x12;
WR_DATA [2] = 0x34;
// memcpy(wr_data + 3,white_addr2,6);
for(i = 0; i <6; i ++)
{
wr_data [3 + i] = white_addr2 [i];
}
WCRC = CRC16(WR_DATA,10);
WR_DATA [10] = WCRC / 256;
WR_DATA [11] = WCRC%256;
// white_addr.
#if debug_log.
printf_string(“\ n \ r \ n \ r whitelist_setup =”);
printf_byte(whitelist_setup);
UART2_INIT(UART_BAUDRATE_115K2,3);
#万一
da14583_spi_flash_init();
//上电外围设备的电源域
setBits16(PMU_CTRL_REG,PERIPH_SLEEP,0);
而(!(getword16(sys_stat_reg)&per_is_up));
//启用焊盘
SetBits16(Sys_ctrl_reg,pad_latch_en,1);
//初始化DA14583内部闪存
#if debug_log.
printf_string(“\ n \ r写flash:\ n”);
UART2_INIT(UART_BAUDRATE_115K2,3);
for(i = 0; i
printf_byte(wr_data [i]);
printf_string(“”);
UART2_INIT(UART_BAUDRATE_115K2,3);
}
printf_string(“\ n”);
#万一
spi_flash_page_program(wr_data,0x1f800,write_size);
da14583_spi_flash_release();
da14583_spi_flash_init();
setBits16(PMU_CTRL_REG,PERIPH_SLEEP,0);
而(!(getword16(sys_stat_reg)&per_is_up));
//启用焊盘
SetBits16(Sys_ctrl_reg,pad_latch_en,1);
#if debug_log.
printf_string(“\ n \ r \ n \ r double check ......”);
UART2_INIT(UART_BAUDRATE_115K2,3);
#万一
btrd = spi_flash_read_data(rd_data,0x1f800,write_size);
//显示结果
#if debug_log.
printf_string(“\ n \ r \ n \ rl218再次读取闪光灯\ n”);
UART2_INIT(UART_BAUDRATE_115K2,3);
for(i = 0; i
printf_byte(rd_data [i]);
printf_string(“”);
}
printf_string(“\ n \ r \ n \ r l 493字节数:0x”);
printf_byte((btrd >> 8)&0xff);
printf_byte((btrd)&0xff);
printf_string(“\ n \ r”);
UART2_INIT(UART_BAUDRATE_115K2,3);
#万一
WCRC = CRC16(RD_DATA,10);
CRCH = WCRC / 256;
crcl = wcrc%256;
if((crch == wr_data [10])&&(crcl == wr_data [11]))
{
#if debug_log.
printf_string(“\ ncrc正确\ n \ r”);
UART2_INIT(UART_BAUDRATE_115K2,3);
#万一
#if debug_log.
printf_string(“\ n \ r \ n \ r whitelist_setup =”);
printf_byte(whitelist_setup);
UART2_INIT(UART_BAUDRATE_115K2,3);
#万一
whitelist_setup = 1;
// memcpy(addr_target,white_addr,6);
}
别的
{
#if debug_log.
printf_string(“\ ncrc错误\ n \ r”);
UART2_INIT(UART_BAUDRATE_115K2,3);
#万一
}
//释放DA14583内部闪光灯
da14583_spi_flash_release();
flagbct2 = 0;
flagbct1 = 0;
BCT1 = 0;
//返回状态;
// system_init();
// periph_init();
}

嗨Liuluan002,
描述是非常通用的,问题可以是许多事情的副作用,它可能是一个错误,或者在读取数据时可能是一个错误,我可以建议的是看看peripheral_examples和该项目的SPI项目执行写入并读取闪光灯,因此您可以将其视为示例。
谢谢mt_dialog.