4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
8 * \brief Flash memory programmar interface.
11 * \author Stefano Fedrigo <aleph@develer.com>
12 * \author Francesco Sacchi <batt@develer.com>
13 * \author Daniele Basile <asterix@develer.com>
22 #include <cfg/compiler.h>
24 #include <cfg/debug.h>
27 * Include platform-specific binding code if we're hosted.
28 * Try the CPU specific one for bare-metal environments.
31 #include OS_CSOURCE(prog)
33 #include CPU_CSOURCE(prog)
38 #define ONE_BYTE_TEST_ADDRESS 347
40 uint8_t test_buf[TEST_SIZE];
41 uint8_t save_buf[TEST_SIZE];
44 * Program memory read/write subtest.
45 * Try to write/read in the same \param f file location \param _size bytes.
46 * \return true if all is ok, false otherwise
47 * \note Restore file position at exit (if no error)
48 * \note Test buffer \param buf must be filled with
49 * the following statement:
54 static bool prog_rwTest(KFile *f, uint8_t *buf, size_t _size)
58 if (f->write(f, buf, size) != size)
65 // Read flash in test buffer
66 if (f->read(f, buf, size) != size)
71 for (size_t i = 0; i < size; i++)
72 if (buf[i] != (i & 0xff))
79 * Test for program memory read/write.
85 // Set up flash programming functions.
87 fd.close = prog_close;
89 fd.write = prog_write;
92 // Fill in test buffer
93 for (int i = 0; i < TEST_SIZE; i++)
94 test_buf[i] = (i & 0xff);
97 fd.open(&fd, NULL, 0);
98 // Save flash content (for later restore).
99 fd.read(&fd, save_buf, sizeof(save_buf));
100 fd.seek(&fd, -TEST_SIZE);
102 // Test flash read/write to address 0..TEST_SIZE
103 if (!prog_rwTest(&fd, test_buf, TEST_SIZE))
106 // One byte read/write test
107 fd.seek(&fd, ONE_BYTE_TEST_ADDRESS); // Random address
108 if (!prog_rwTest(&fd, test_buf, 1))
110 fd.seek(&fd, -(int32_t)ONE_BYTE_TEST_ADDRESS);
112 // Restore old flash data
113 if (fd.write(&fd, save_buf, sizeof(test_buf)) != TEST_SIZE)
115 fd.seek(&fd, -TEST_SIZE);
117 // Go to the Flash end
118 fd.seek(&fd, fd.size - TEST_SIZE);
119 // Save flash content (for later restore).
120 fd.read(&fd, save_buf, sizeof(save_buf));
121 fd.seek(&fd, -TEST_SIZE);
123 // Test flash read/write to address (FLASHEND - TEST_SIZE) ... FLASHEND
124 if (!prog_rwTest(&fd, test_buf, TEST_SIZE))
127 // Go to half test size.
128 fd.seek(&fd, (TEST_SIZE / 2));
130 // This test should FAIL, cause we try to write over file end.
131 if (prog_rwTest(&fd, test_buf, TEST_SIZE))
134 fd.seek(&fd, -TEST_SIZE);
135 // Restore old flash data
136 fd.write(&fd, save_buf, TEST_SIZE);