11#include < cstdint>
2+ #include < array>
23
34#include " flash_os.hpp"
45
6+ /* *
7+ * @brief Helper function to get the minimum of two values
8+ *
9+ * @tparam T
10+ * @param a
11+ * @param b
12+ * @return constexpr auto
13+ */
14+ template <typename T>
15+ constexpr auto min (const T a, const T b) {
16+ return (a > b) ? b : a;
17+ }
18+
519int main () {
620 // flag to mark if we want to run the test code
721 constexpr static bool debug = false ;
@@ -12,7 +26,75 @@ int main() {
1226 return 0 ;
1327 }
1428
15- // TODO: add a example here to test the flash algorithm
29+ // test data buffer we use to write to the flash
30+ static const std::array<uint8_t , 512 > testdata = {
31+ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 ,
32+ 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F ,
33+ 0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
34+ 0x18 , 0x19 , 0x1A , 0x1B , 0x1C , 0x1D , 0x1E , 0x1F ,
35+ 0x20 , 0x21 , 0x22 , 0x23 , 0x24 , 0x25 , 0x26 , 0x27 ,
36+ 0x28 , 0x29 , 0x2A , 0x2B , 0x2C , 0x2D , 0x2E , 0x2F ,
37+ 0x30 , 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 ,
38+ 0x38 , 0x39 , 0x3A , 0x3B , 0x3C , 0x3D , 0x3E , 0x3F ,
39+
40+ // the rest is just zeroes
41+ };
42+
43+ // buffer we store the data we read back into
44+ static std::array<uint8_t , 512 > buffer = {};
45+
46+ // call the init function we want to erase
47+ Init (FlashDevice.base_address , 0 , 1 );
48+
49+ // erase a sector
50+ const auto erase_result = EraseSector (FlashDevice.base_address );
51+
52+ // check for errors
53+ if (erase_result != 0 ) {
54+ // we have an error, lock up here so the user can debug
55+ while (true ) {}
56+ }
57+
58+ // uninit the flash with parameter erase
59+ UnInit (1 );
60+
61+ // call the init function we want to program
62+ Init (FlashDevice.base_address , 0 , 2 );
63+
64+ // program a page
65+ const auto program_result = ProgramPage (
66+ FlashDevice.base_address ,
67+ min (testdata.size (), static_cast <std::size_t >(FlashDevice.page_size )),
68+ testdata.data ()
69+ );
70+
71+ // check for errors
72+ if (program_result != 0 ) {
73+ // we have an error, lock up here so the user can debug
74+ while (true ) {}
75+ }
76+
77+ // uninit the flash with parameter program
78+ UnInit (2 );
79+
80+ // call the init function we want to read
81+ Init (FlashDevice.base_address , 0 , 0 );
82+
83+ // read back the data
84+ const auto read_result = SEGGER_OPEN_Read (
85+ FlashDevice.base_address ,
86+ min (buffer.size (), static_cast <std::size_t >(FlashDevice.size )),
87+ buffer.data ()
88+ );
89+
90+ // check for errors
91+ if (read_result < 0 ) {
92+ // we have an error, lock up here so the user can debug
93+ while (true ) {}
94+ }
95+
96+ // uninit the flash with parameter read
97+ UnInit (0 );
1698
1799 return 0 ;
18100};
0 commit comments