Ctrl_status at25dfx_df_2_ram(U32 addr, void *ram) { if (addr + 1 > AT25DFX_NSECT) { return CTRL_FAIL; } at25dfx_read(ram, SECTOR_SIZE, addr * SECTOR_SIZE); return CTRL_GOOD; }
Ctrl_status at25dfx_usb_read_10(U32 addr, U16 nb_sector) { if (addr + nb_sector > AT25DFX_NSECT) return CTRL_FAIL; for (; nb_sector; --nb_sector, addr++) { uint8_t *blkptr; if (addr < FIRST_BLK) blkptr = layfs_256_img + addr * SECTOR_SIZE; else { // Read the next sector. if (at25dfx_read(sector_buf, SECTOR_SIZE, (addr - FIRST_BLK) * SECTOR_SIZE) != AT25_SUCCESS) return CTRL_FAIL; blkptr = sector_buf; } if (!udi_msc_trans_block(true, blkptr, SECTOR_SIZE, 0)) return CTRL_FAIL; } return CTRL_GOOD; }
/** * \brief Application entry point for AT25DFx example. * * \return Unused (ANSI-C compatibility). */ int main(void) { uint16_t i; sysclk_init(); board_init(); /* Initialize the SerialFlash */ at25dfx_initialize(); /* Set the SerialFlash active */ at25dfx_set_mem_active(AT25DFX_MEM_ID); /* Unprotect the chip */ if (at25dfx_protect_chip(AT25_TYPE_UNPROTECT) == AT25_SUCCESS) { LED_On(DATA_FLASH_LED_EXAMPLE_0); } else { test_ko(); } /* Check if the SerialFlash is valid */ if (at25dfx_mem_check() == AT25_SUCCESS) { LED_On(DATA_FLASH_LED_EXAMPLE_0); } else { test_ko(); } /* Prepare half of the SerialFlash sector as 0xAA */ for (i = 0; i < AT25DFX_TEST_DATA_SIZE / 2; i++) { ram_buff[i] = 0xAA; } /* And the remaining half as 0x55 */ for (; i < AT25DFX_TEST_DATA_SIZE; i++) { ram_buff[i] = 0x55; } /* Erase the block before write */ at25dfx_erase_block(AT25DFX_TEST_BLOCK_ADDR); /* Write the data to the SerialFlash */ at25dfx_write(ram_buff, AT25DFX_TEST_DATA_SIZE, AT25DFX_TEST_BLOCK_ADDR); /* Read back this sector and compare them with the expected values */ at25dfx_read(ram_buff, AT25DFX_TEST_DATA_SIZE, AT25DFX_TEST_BLOCK_ADDR); for (i = 0; i < AT25DFX_TEST_DATA_SIZE / 2; i++) { if (ram_buff[i] != 0xAA) { test_ko(); } } for (; i < AT25DFX_TEST_DATA_SIZE; i++) { if (ram_buff[i] != 0x55) { test_ko(); } } /* Write one SerialFlash sector as 0x00, 0x01 .... */ for (i = 0; i < AT25DFX_TEST_DATA_SIZE; i++) { ram_buff[i] = i; } /* Erase the block before write */ at25dfx_erase_block(AT25DFX_TEST_BLOCK_ADDR); /* Write the data to the SerialFlash */ at25dfx_write(ram_buff, AT25DFX_TEST_DATA_SIZE, AT25DFX_TEST_BLOCK_ADDR); /* Read back this sector and compare them with the expected values */ at25dfx_read(ram_buff, AT25DFX_TEST_DATA_SIZE, AT25DFX_TEST_BLOCK_ADDR); for (i = 0; i < AT25DFX_TEST_DATA_SIZE; i++) { if (ram_buff[i] != (i % 0x100)) { test_ko(); } } LED_On(DATA_FLASH_LED_EXAMPLE_1); while (1); }
/** * \brief Test data read, write and erase API functions. * * This test calls the data chip erase, read, write and block erase API * functions. * * \param test Current test case. */ static void run_test_at25dfx_data_access(const struct test_case *test) { at25_status_t status; uint32_t i; uint32_t page_num = 0; /* Unprotect the SerialFlash first */ at25dfx_protect_chip(AT25_TYPE_UNPROTECT); /* Test block erase */ i = 0; page_num = 0; status = at25dfx_erase_block(AT25DFX_UNIT_TEST_BLOCK_ADDR); while (page_num < AT25DFX_UNIT_TEST_BLOCK_SIZE / AT25DFX_UNIT_TEST_PAGE_SIZE) { memset(data_buff, 0x0, sizeof(data_buff)); at25dfx_read(data_buff, AT25DFX_UNIT_TEST_PAGE_SIZE, page_num * AT25DFX_UNIT_TEST_PAGE_SIZE); for (i = 0; i < AT25DFX_UNIT_TEST_PAGE_SIZE; i++) { if (data_buff[i] != 0xff) { status = AT25_ERROR; } } page_num++; } /* Validate the SerialFlash block erase function */ test_assert_true(test, status == AT25_SUCCESS, "Block erase failed!"); /* Test read/write function */ i = 0; page_num = 0; while (page_num < AT25DFX_UNIT_TEST_BLOCK_SIZE / AT25DFX_UNIT_TEST_PAGE_SIZE) { for (i = 0; i < AT25DFX_UNIT_TEST_PAGE_SIZE; i++) { data_buff[i] = i & 0xff; } at25dfx_write(data_buff, AT25DFX_UNIT_TEST_PAGE_SIZE, page_num * AT25DFX_UNIT_TEST_PAGE_SIZE); /* Reset the data buffer */ memset(data_buff, 0x0, sizeof(data_buff)); status = at25dfx_read(data_buff, AT25DFX_UNIT_TEST_PAGE_SIZE, page_num * AT25DFX_UNIT_TEST_PAGE_SIZE); for (i = 0; i < AT25DFX_UNIT_TEST_PAGE_SIZE; i++) { if (data_buff[i] != (i & 0xff)) { status = AT25_ERROR; break; } } page_num++; } /* Validate the SerialFlash write function */ test_assert_true(test, status == AT25_SUCCESS, "Write operation failed!"); }