status_code_t nvm_write(mem_type_t mem, uint32_t address, void *buffer, uint32_t len) { switch (mem) { case INT_FLASH: case INT_USERPAGE: flash_api_memcpy((volatile void *)address, (const void *)buffer, len, true); break; #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX) case AT45DBX: { uint32_t sector = address / AT45DBX_SECTOR_SIZE; if (!at45dbx_write_sector_open(sector)) { return ERR_BAD_ADDRESS; } at45dbx_write_sector_from_ram((const void *)buffer); at45dbx_write_close(); } break; #endif default: return ERR_INVALID_ARG; } return STATUS_OK; }
Ctrl_status at45dbx_ram_2_df(U32 addr, const void *ram) { if (addr + 1 > AT45DBX_MEM_CNT << (AT45DBX_MEM_SIZE - AT45DBX_SECTOR_BITS)) { return CTRL_FAIL; } at45dbx_write_sector_open(addr); at45dbx_write_sector_from_ram(ram); at45dbx_write_close(); return CTRL_GOOD; }
status_code_t nvm_write(mem_type_t mem, uint32_t address, void *buffer, uint32_t len) { switch (mem) { case INT_FLASH: #if SAM4S /*! This erases 8 pages of flash before writing */ if (flash_erase_page(address, IFLASH_ERASE_PAGES_8)) { return ERR_INVALID_ARG; } if (flash_write(address, (const void *)buffer, len, false)) { return ERR_INVALID_ARG; } #elif SAM4L flashcalw_memcpy((volatile void *)address, (const void *)buffer, len, true); #else if (flash_write(address, (const void *)buffer, len, true)) { return ERR_INVALID_ARG; } #endif break; #if SAM4S case INT_USERPAGE: if (flash_write_user_signature((const void *)buffer, len)) { return ERR_INVALID_ARG; } break; #endif #if defined(USE_EXTMEM) && defined(CONF_BOARD_AT45DBX) case AT45DBX: { uint32_t sector = address / AT45DBX_SECTOR_SIZE; if (!at45dbx_write_sector_open(sector)) { return ERR_BAD_ADDRESS; } at45dbx_write_sector_from_ram((const void *)buffer); at45dbx_write_close(); } break; #endif default: return ERR_INVALID_ARG; } return STATUS_OK; }
/** * \brief Test the read and write multiple sector operations on the DataFlash * * This function will test the read and write functionalities of the DataFlash * using multiple sector access. It will first fill the 10 first DataFlash * sectors with a known pattern and read it back to test each value.\n * * \param test Current test case. */ static void run_multiple_sector_access_test(const struct test_case *test) { bool status; uint32_t i; uint8_t value, cur_sector; uint8_t expected; /* Write the ram sector to the DataFlash */ status = at45dbx_write_sector_open(0); test_assert_true(test, status == true, "Cannot open the DataFlash memory for write access"); cur_sector = 0; while (cur_sector++ < 10) { /* Fills a ram sector with a known pattern */ for (i=0; i<AT45DBX_SECTOR_SIZE; i++) { value = BYTE_PATTERN3(cur_sector * AT45DBX_SECTOR_SIZE + i); ((uint8_t *) sector_buf)[i] = value; } /* Write sector on DataFlash */ status = at45dbx_write_sector_from_ram(sector_buf); test_assert_true(test, status == true, "Error while writing sector # %i", cur_sector); } at45dbx_write_close(); /* Read back the sector of the DataFlash */ status = at45dbx_read_sector_open(0); test_assert_true(test, status == true, "Cannot open the DataFlash memory for read access"); cur_sector = 0; while (cur_sector++ < 10) { // Read the next sector. status = at45dbx_read_sector_to_ram(sector_buf); test_assert_true(test, status == true, "Error while reading sector # %i", cur_sector); /* Test each values of the current sector */ for (i=0; i<AT45DBX_SECTOR_SIZE; i++) { expected = BYTE_PATTERN3(cur_sector * AT45DBX_SECTOR_SIZE + i); test_assert_true(test, ((uint8_t *) sector_buf)[i] == expected, "Value not expected @ 0x%08x in sector # %i" " (read: 0x%02x, expected: 0x%02x)", i, cur_sector, ((uint8_t *) sector_buf)[i], expected); } } at45dbx_read_close(); }
Ctrl_status at45dbx_usb_write_10(U32 addr, U16 nb_sector) { if (addr + nb_sector > AT45DBX_MEM_CNT << (AT45DBX_MEM_SIZE - AT45DBX_SECTOR_BITS)){ return CTRL_FAIL; } at45dbx_write_sector_open(addr); while (nb_sector--) { // Write the next sector. udi_msc_trans_block( false, sector_buf, AT45DBX_SECTOR_SIZE, NULL); at45dbx_write_sector_from_ram(sector_buf); } at45dbx_write_close(); return CTRL_GOOD; }
/** * \brief Test the read and write sector operations on the DataFlash * * This function will test the read and write functionalities of the DataFlash * using sector access. It will first fill a sector with a known pattern and * read it back by testing each value read.\n * * \param test Current test case. */ static void run_sector_access_test(const struct test_case *test) { uint32_t i; bool status; /* Fills a ram sector with a known pattern */ for (i=0; i<AT45DBX_SECTOR_SIZE; i++) { sector_buf[i] = BYTE_PATTERN2(i); } /* Write the ram sector to the DataFlash */ status = at45dbx_write_sector_open(0); test_assert_true(test, status == true, "Cannot open the DataFlash memory for write access"); status = at45dbx_write_sector_from_ram(sector_buf); test_assert_true(test, status == true, "Write sector operation error"); at45dbx_write_close(); /* Clear the ram sector buffer */ for (i=0; i<AT45DBX_SECTOR_SIZE; i++) { sector_buf[i] = 0; } /* Read back the sector of the DataFlash */ status = at45dbx_read_sector_open(0); test_assert_true(test, status == true, "Cannot open the DataFlash memory for read access"); status = at45dbx_read_sector_to_ram(sector_buf); test_assert_true(test, status == true, "Read sector operation error"); at45dbx_read_close(); /* Compare the values read from the DataFlash with the expected values */ for (i=0; i<AT45DBX_SECTOR_SIZE; i++) { test_assert_true(test, sector_buf[i] == BYTE_PATTERN2(i), "Value not expected @ 0x%08x (read: 0x%02x," " expected: 0x%02x)", i, sector_buf[i], BYTE_PATTERN2(i)); } }
/*! \brief Main function. */ int main(void) { uint16_t i; // Initialize the system - clock and board. system_init(); at45dbx_init(); if(at45dbx_mem_check()==true) { port_pin_set_output_level(DATA_FLASH_LED_EXAMPLE_0, false); } else { test_ko(); } // Prepare half a data flash sector to 0xAA for(i=0;i<AT45DBX_SECTOR_SIZE/2;i++) { ram_buf[i]=0xAA; } // And the remaining half to 0x55 for(;i<AT45DBX_SECTOR_SIZE;i++) { ram_buf[i]=0x55; } at45dbx_write_sector_open(TARGET_SECTOR); at45dbx_write_sector_from_ram(ram_buf); at45dbx_write_close(); // Read back this sector and compare to expected values at45dbx_read_sector_open(TARGET_SECTOR); at45dbx_read_sector_to_ram(ram_buf); at45dbx_read_close(); for(i=0;i<AT45DBX_SECTOR_SIZE/2;i++) { if (ram_buf[i]!=0xAA) { test_ko(); } } for(;i<AT45DBX_SECTOR_SIZE;i++) { if (ram_buf[i]!=0x55) { test_ko(); } } // Write one data flash sector to 0x00, 0x01 .... for(i=0;i<AT45DBX_SECTOR_SIZE;i++) { ram_buf[i]=i; } at45dbx_write_sector_open(TARGET_SECTOR); at45dbx_write_sector_from_ram(ram_buf); at45dbx_write_close(); // Read one data flash sector to ram at45dbx_read_sector_open(TARGET_SECTOR); at45dbx_read_sector_to_ram(ram_buf); at45dbx_read_close(); for(i=0;i<AT45DBX_SECTOR_SIZE;i++) { if ( ram_buf[i]!=(i%0x100) ) { test_ko(); } } while (1); }
/*! \brief Main function. */ int main(void) { uint16_t i; sysclk_init(); // Initialize the board. // The board-specific conf_board.h file contains the configuration of the board // initialization. board_init(); at45dbx_init(); if(at45dbx_mem_check()==true) { gpio_set_pin_low(DATA_FLASH_LED_EXAMPLE_0); } else { test_ko(); } // Prepare half a data flash sector to 0xAA for(i=0;i<AT45DBX_SECTOR_SIZE/2;i++) { ram_buf[i]=0xAA; } // And the remaining half to 0x55 for(;i<AT45DBX_SECTOR_SIZE;i++) { ram_buf[i]=0x55; } at45dbx_write_sector_open(TARGET_SECTOR); at45dbx_write_sector_from_ram(ram_buf); at45dbx_write_close(); // Read back this sector and compare to expected values at45dbx_read_sector_open(TARGET_SECTOR); at45dbx_read_sector_to_ram(ram_buf); at45dbx_read_close(); for(i=0;i<AT45DBX_SECTOR_SIZE/2;i++) { if (ram_buf[i]!=0xAA) { test_ko(); } } for(;i<AT45DBX_SECTOR_SIZE;i++) { if (ram_buf[i]!=0x55) { test_ko(); } } // Write one data flash sector to 0x00, 0x01 .... for(i=0;i<AT45DBX_SECTOR_SIZE;i++) { ram_buf[i]=i; } at45dbx_write_sector_open(TARGET_SECTOR); at45dbx_write_sector_from_ram(ram_buf); at45dbx_write_close(); // Read one data flash sector to ram at45dbx_read_sector_open(TARGET_SECTOR); at45dbx_read_sector_to_ram(ram_buf); at45dbx_read_close(); for(i=0;i<AT45DBX_SECTOR_SIZE;i++) { if ( ram_buf[i]!=(i%0x100) ) { test_ko(); } } gpio_set_pin_low(DATA_FLASH_LED_EXAMPLE_1); while (1); }