本次实验学习QSPI外设。
首先进行外设初始化:
/* Initialize the Serial flash */
result = cy_serial_flash_qspi_init(smifMemConfigs[MEM_SLOT_NUM],
CYBSP_QSPI_D0, CYBSP_QSPI_D1, CYBSP_QSPI_D2, CYBSP_QSPI_D3, NC, NC,
NC, NC, CYBSP_QSPI_SCK, CYBSP_QSPI_SS, QSPI_BUS_FREQUENCY_HZ);
check_status("Serial Flash initialization failed", result);
写入之前要先进行擦除操作:
/* Use last sector to erase for flash operation */
ext_mem_address = (smifMemConfigs[0]->deviceCfg->memSize/2 -
smifMemConfigs[0]->deviceCfg->eraseSize *2);
sectorSize = cy_serial_flash_qspi_get_erase_size(ext_mem_address);
printf("\r\nTotal Flash Size:%u bytes\r\n",cy_serial_flash_qspi_get_size());
/* Erase before write */
printf("\r\n1. Erasing %u bytes of memory\r\n", sectorSize);
result = cy_serial_flash_qspi_erase(ext_mem_address, sectorSize);
check_status("Erasing memory failed", result);
/* Read after Erase to confirm that all data is 0xFF */
printf("\r\n2.Reading after Erase & verifying that each byte is 0xFF\r\n");
result = cy_serial_flash_qspi_read(ext_mem_address, PACKET_SIZE, rx_buf);
check_status("Reading memory failed", result);
print_array("Received Data", rx_buf, PACKET_SIZE);
memset(tx_buf, FLASH_DATA_AFTER_ERASE, PACKET_SIZE);
check_status("Flash contains data other than 0xFF after erase",
memcmp(tx_buf, rx_buf, PACKET_SIZE));
实验测试结果:
****************** Serial Flash Read and Write ******************
Total Flash Size:1048576 bytes
1. Erasing 4096 bytes of memory
2.Reading after Erase & verifying that each byte is 0xFF
Received Data (64 bytes):
-------------------------
0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF 0xFF
3. Writing data to memory
Written Data (64 bytes):
-------------------------
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2A 0x2B 0x2C 0x2D 0x2E 0x2F
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3A 0x3B 0x3C 0x3D 0x3E 0x3F
4. Reading back for verification
Received Data (64 bytes):
-------------------------
0x00 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0A 0x0B 0x0C 0x0D 0x0E 0x0F
0x10 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x1E 0x1F
0x20 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2A 0x2B 0x2C 0x2D 0x2E 0x2F
0x30 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3A 0x3B 0x3C 0x3D 0x3E 0x3F
=========================================================
SUCCESS: Read data matches with written data!
=========================================================
|