-uint8_t test_buf[TEST_SIZE];
-uint8_t save_buf[TEST_SIZE];
-
-/**
- * Program memory read/write subtest.
- * Try to write/read in the same \a f file location \a _size bytes.
- * \return true if all is ok, false otherwise
- * \note Restore file position at exit (if no error)
- * \note Test buffer \a buf must be filled with
- * the following statement:
- * <pre>
- * buf[i] = i & 0xff
- * </pre>
- */
-static bool flash_avr_rwTest(KFile *f, uint8_t *buf, size_t _size)
-{
- int32_t size = _size;
- // Write test buffer
- if (f->write(f, buf, size) != size)
- return false;
- f->seek(f, -size, SEEK_CUR);
-
- // Reset test buffer
- memset(buf, 0, size);
-
- // Read flash in test buffer
- if (f->read(f, buf, size) != size)
- return false;
- f->seek(f, -size, SEEK_CUR);
-
- // Check test result
- for (size_t i = 0; i < size; i++)
- if (buf[i] != (i & 0xff))
- return false;
-
- return true;
-}
-
-/**
- * Test for program memory read/write.
- */
-bool flash_avr_test(void)
-{
- KFile fd;
-
- // Set up flash programming functions.
- flash_avr_init(&fd);
-
- // Fill in test buffer
- for (int i = 0; i < TEST_SIZE; i++)
- test_buf[i] = (i & 0xff);
-
- // Open flash
- fd.open(&fd, NULL, 0);
- // Save flash content (for later restore).
- fd.read(&fd, save_buf, sizeof(save_buf));
- // Seek to addr 0
- if (fd.seek(&fd, 0, SEEK_SET) != 0)
- goto flash_avr_test_end;
-
- // Test flash read/write to address 0..TEST_SIZE
- if (!flash_avr_rwTest(&fd, test_buf, TEST_SIZE))
- goto flash_avr_test_end;
-
- // One byte read/write test
- fd.seek(&fd, ONE_BYTE_TEST_ADDRESS, SEEK_CUR); // Random address
- if (!flash_avr_rwTest(&fd, test_buf, 1))
- goto flash_avr_test_end;
- fd.seek(&fd, -(int32_t)ONE_BYTE_TEST_ADDRESS, SEEK_CUR);
-
- // Restore old flash data
- if (fd.write(&fd, save_buf, sizeof(test_buf)) != TEST_SIZE)
- goto flash_avr_test_end;
- fd.seek(&fd, -TEST_SIZE, SEEK_CUR);
-
- // Go to the Flash end
- fd.seek(&fd, -TEST_SIZE, SEEK_END);
- // Save flash content (for later restore).
- fd.read(&fd, save_buf, sizeof(save_buf));
- fd.seek(&fd, -TEST_SIZE, SEEK_CUR);
-
- // Test flash read/write to address (FLASHEND - TEST_SIZE) ... FLASHEND
- if (!flash_avr_rwTest(&fd, test_buf, TEST_SIZE))
- goto flash_avr_test_end;
-
- // Go to half test size.
- fd.seek(&fd, (TEST_SIZE / 2), SEEK_CUR);
-
- // This test should FAIL, cause we try to write over file end.
- kprintf("This test should fail.\n");
- if (flash_avr_rwTest(&fd, test_buf, TEST_SIZE))
- goto flash_avr_test_end;
-
- fd.seek(&fd, -TEST_SIZE, SEEK_CUR);
- // Restore old flash data
- fd.write(&fd, save_buf, TEST_SIZE);
-
- fd.close(&fd);
- return true;
-
-flash_avr_test_end:
- fd.close(&fd);
- return false;