comparison FontPack/base_upperRegion.c @ 24:4de447ee5d1b

ADD font check code that runs at FontPack installation.
author JeanDo
date Mon, 01 Jan 2018 20:12:34 +0100
parents 1df134a2706e
children 97eafbcb81a9
comparison
equal deleted inserted replaced
23:89f429767017 24:4de447ee5d1b
34 ****************************************************************************** 34 ******************************************************************************
35 */ 35 */
36 36
37 /* Includes ------------------------------------------------------------------*/ 37 /* Includes ------------------------------------------------------------------*/
38 38
39 // From Common/Inc:
39 #include "FirmwareData.h" 40 #include "FirmwareData.h"
41
42 // From Discovery/Inc
40 #include "gfx_fonts.h" 43 #include "gfx_fonts.h"
41 44
42 #include "stm32f4xx_hal.h" 45 // From AC6 support:
46 #include <stdio.h>
47
48 //////////////////////////////////////////////////////////////////////////////
43 49
44 const SFirmwareData font_FirmwareData __attribute__(( section(".upper_firmware_data") )) = 50 const SFirmwareData font_FirmwareData __attribute__(( section(".upper_firmware_data") )) =
45 { 51 {
46 .versionFirst = 0, 52 .versionFirst = 0,
47 .versionSecond = 9, 53 .versionSecond = 9,
63 .magic[0] = FIRMWARE_MAGIC_FIRST, 69 .magic[0] = FIRMWARE_MAGIC_FIRST,
64 .magic[1] = FIRMWARE_MAGIC_SECOND, 70 .magic[1] = FIRMWARE_MAGIC_SECOND,
65 .magic[2] = FIRMWARE_MAGIC_FONT, /* the magic byte for fonts*/ 71 .magic[2] = FIRMWARE_MAGIC_FONT, /* the magic byte for fonts*/
66 .magic[3] = FIRMWARE_MAGIC_END 72 .magic[3] = FIRMWARE_MAGIC_END
67 }; 73 };
74
75 //////////////////////////////////////////////////////////////////////////////
68 76
69 /* Fonts fixed in upper region */ 77 /* Fonts fixed in upper region */
70 #include "Fonts/font_awe48.h" 78 #include "Fonts/font_awe48.h"
71 #include "Fonts/font_T24.h" 79 #include "Fonts/font_T24.h"
72 #include "Fonts/font_T42.h" 80 #include "Fonts/font_T42.h"
79 /* Images fixed in upper region */ 87 /* Images fixed in upper region */
80 #include "Fonts/image_battery.h" 88 #include "Fonts/image_battery.h"
81 #include "Fonts/image_heinrichs_weikamp.h" 89 #include "Fonts/image_heinrichs_weikamp.h"
82 #include "Fonts/image_ostc.h" 90 #include "Fonts/image_ostc.h"
83 91
92 //////////////////////////////////////////////////////////////////////////////
93
94 static int errors = 0;
95 #define ASSERT(e) \
96 do { if( ! (e) ) {++errors; printf("FAIL at %3d: %s", __LINE__, #e);}} while(0)
97
98 #define ASSERT_RANGE(e, min, max) \
99 ASSERT(min <= e); ASSERT( e <= max)
100
84 int main(void) 101 int main(void)
85 { 102 {
103 //---- Check the linker puts the directory at the requested address ------
104 ASSERT( & Awe48 == (tFont*)0x8100000 );
105 ASSERT( & FontT24 == (tFont*)0x810000c );
106 ASSERT( & FontT42 == (tFont*)0x8100018 );
107 ASSERT( & FontT48 == (tFont*)0x8100024 );
108 ASSERT( & FontT54 == (tFont*)0x8100030 );
109 ASSERT( & FontT84 == (tFont*)0x810003c );
110 ASSERT( & FontT105 == (tFont*)0x8100048 );
111 ASSERT( & FontT144 == (tFont*)0x8100052 );
112
113 //---- Check the linker puts the font data in the requested section ------
114 extern tChar __upper_font_data;
115 extern tChar __upper_font_data_end;
116 ASSERT( &__upper_font_data == (tChar*)0x08132040 );
117 ASSERT_RANGE( (int)&__upper_font_data_end, 0x08132040, 0x081E0000);
118
119 //---- Walk through the directory data -----------------------------------
120 extern const tFont __font_directory;
121 extern const tFont __font_directory_end;
122 for(const tFont* font = & __font_directory; font < &__font_directory_end; ++font)
123 {
124 // Check END-OF-DIRECTORY magic marker
125 if( font->length == (uint32_t)-1 )
126 {
127 ASSERT( font == &FontT144 + 1 );
128 break;
129 }
130
131 // Check font descriptors are inside a safe range.
132 ASSERT_RANGE( font->length, 10, 103 );
133 ASSERT_RANGE( font->spacesize, 0, 18 );
134 ASSERT_RANGE( font->spacesize2Monospaced, 13, 72 );
135 ASSERT_RANGE( font->height, 28, 108 );
136
137 //---- Walk through each char ----------------------------------------
138 for(int i = 0; i < font->length; ++i)
139 {
140 const tChar* c = &font->chars[i];
141
142 // Check char data is indeed stored in the actual data section
143 ASSERT_RANGE( c, &__upper_font_data, &__upper_font_data_end);
144
145 // Check char data sanity
146 ASSERT_RANGE( c->code, 0x0000, 0xF143);
147
148 // Check image sanity
149 const tImage* image = c->image;
150 ASSERT_RANGE(image, (tImage*)&__upper_font_data, (tImage*)&__upper_font_data_end);
151 ASSERT_RANGE(image->width, font->spacesize, font->spacesize2Monospaced);
152 ASSERT(image->height == font->height);
153
154 // Uncompress image bytes
155 const uint8_t* byte = image->data;
156 ASSERT_RANGE(byte, (uint8_t*)&__upper_font_data, (uint8_t*)&__upper_font_data_end);
157
158 for(int w=0; w <image->width; ++w)
159 {
160 // Compression: special 0x01 byte at start of column means just skip it.
161 if( *byte++ == 0x01 )
162 continue;
163
164 int zeros = (byte[-1] == 0x00) ? 1 : 0;
165 for(int h = 1; h < image->height; ++h)
166 {
167 if( *byte == 0x00 )
168 ++zeros;
169
170 // Other bytes cannot have the 0x01 value...
171 ASSERT( *byte++ != 0x01 );
172 }
173
174 if( zeros == image->height )
175 printf("Font[%d] char[%d]: could skip column %d",
176 &__font_directory - font, i, w);
177 }
178
179 // Check the byte stream do not collide with the next char,
180 // or with the first tImage struct of the font.
181 if( (i+1) < font->length )
182 ASSERT( byte < font->chars[i+1].image->data );
183 else
184 ASSERT( byte < (uint8_t*)font->chars[0].image );
185
186 // TODO: check image bytes are contiguous between chars.
187 }
188 }
189
190 if( errors )
191 {
192 printf("Font Check: %d errors.", errors);
193 return -1;
194 }
195
196 printf("Font Check: no errors.");
86 return 0; 197 return 0;
87 } 198 }
88 199
89 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ 200 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/