22#include " page_crc.h"
33#include " pages.h"
44
5- // Abstract the FastCrC32 functions
6- // - they have have very slight differences in signatures, which causes the Arduino
7- // compiler to fail for some boards (the Platform IO compiler works fine though)
8- static inline uint32_t initializeCrc (FastCRC32 &crc, const uint8_t *buffer, uint16_t len) {
9- return crc.crc32 (buffer, len);
10- }
11- static inline uint32_t updateCrc (FastCRC32 &crc, const uint8_t *buffer, uint16_t len) {
12- return crc.crc32_upd (buffer, len);
13- }
14-
15- using pCrcCalc = uint32_t (*)(FastCRC32 &, const uint8_t *, uint16_t );
16-
17- static inline uint32_t compute_raw_crc (const page_iterator_t &entity, pCrcCalc calcFunc, FastCRC32 &crcCalc)
18- {
19- return calcFunc (crcCalc, (uint8_t *)entity.pData , entity.address .size );
20- }
21-
22- static inline uint32_t compute_row_crc (const table_row_iterator &row, pCrcCalc calcFunc, FastCRC32 &crcCalc)
5+ uint32_t __attribute__ ((optimize(" Os" ))) calculatePageCRC32(byte pageNum)
236{
24- return calcFunc (crcCalc, &*row, row.size ());
25- }
7+ FastCRC32 crcCalc;
8+
9+ byte buffer = getPageValue (pageNum, 0 );
10+ uint32_t crc = crcCalc.crc32 (&buffer, 1U );
2611
27- static inline uint32_t compute_tablevalues_crc (table_value_iterator it, pCrcCalc calcFunc, FastCRC32 &crcCalc)
28- {
29- uint32_t crc = compute_row_crc (*it, calcFunc, crcCalc);
30- ++it;
31-
32- while (!it.at_end ())
12+ for (uint16_t offset=1 ; offset<getPageSize (pageNum); ++offset)
3313 {
34- crc = compute_row_crc (*it, &updateCrc, crcCalc );
35- ++it ;
14+ buffer = getPageValue (pageNum, offset );
15+ crc = crcCalc. crc32_upd (&buffer, 1U ) ;
3616 }
37- return crc;
38- }
39-
40- static inline uint32_t compute_tableaxis_crc (table_axis_iterator it, uint32_t crc, FastCRC32 &crcCalc)
41- {
42- byte values[32 ]; // Fingers crossed we don't have a table bigger than 32x32
43- byte *pValue = values;
44- while (!it.at_end ())
45- {
46- *pValue++ = (byte)*it;
47- ++it;
48- }
49- return pValue-values==0 ? crc : crcCalc.crc32_upd (values, pValue-values);
50- }
51-
52- static inline uint32_t compute_table_crc (const page_iterator_t &entity, pCrcCalc calcFunc, FastCRC32 &crcCalc)
53- {
54- return compute_tableaxis_crc (y_begin (entity),
55- compute_tableaxis_crc (x_begin (entity),
56- compute_tablevalues_crc (rows_begin (entity), calcFunc, crcCalc),
57- crcCalc),
58- crcCalc);
59- }
6017
61- static inline uint32_t pad_crc (uint16_t padding, uint32_t crc, FastCRC32 &crcCalc)
62- {
63- const uint8_t raw_value = 0u ;
64- while (padding>0 )
65- {
66- crc = crcCalc.crc32_upd (&raw_value, 1 );
67- --padding;
68- }
6918 return crc;
70- }
71-
72- static inline uint32_t compute_crc (const page_iterator_t &entity, pCrcCalc calcFunc, FastCRC32 &crcCalc)
73- {
74- switch (entity.type )
75- {
76- case Raw:
77- return compute_raw_crc (entity, calcFunc, crcCalc);
78- break ;
79-
80- case Table:
81- return compute_table_crc (entity, calcFunc, crcCalc);
82- break ;
83-
84- default :
85- case NoEntity:
86- return pad_crc (entity.address .size , 0U , crcCalc);
87- break ;
88- }
89- }
90-
91- uint32_t calculatePageCRC32 (byte pageNum)
92- {
93- FastCRC32 crcCalc;
94- page_iterator_t entity = page_begin (pageNum);
95- // Initial CRC calc
96- uint32_t crc = compute_crc (entity, &initializeCrc, crcCalc);
97-
98- entity = advance (entity);
99- while (entity.type !=End)
100- {
101- crc = compute_crc (entity, &updateCrc /* Note that we are *updating* */ , crcCalc);
102- entity = advance (entity);
103- }
104- return pad_crc (getPageSize (pageNum) - (entity.address .start +entity.address .size ), crc, crcCalc);
10519}
0 commit comments