language.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //language.c
  2. #include "language.h"
  3. #include <avr/pgmspace.h>
  4. #include <avr/eeprom.h>
  5. #include "bootapp.h"
  6. #include "Configuration.h"
  7. #include "pins.h"
  8. #ifdef W25X20CL
  9. #include "w25x20cl.h"
  10. #endif //W25X20CL
  11. // Currently active language selection.
  12. uint8_t lang_selected = 0;
  13. #if (LANG_MODE == 0) //primary language only
  14. uint8_t lang_select(__attribute__((unused)) uint8_t lang) { return 0; }
  15. uint8_t lang_get_count() { return 1; }
  16. uint16_t lang_get_code(__attribute__((unused)) uint8_t lang) { return LANG_CODE_EN; }
  17. const char* lang_get_name_by_code(__attribute__((unused)) uint16_t code) { return _n("English"); }
  18. void lang_reset(void) { }
  19. uint8_t lang_is_selected(void) { return 1; }
  20. #else //(LANG_MODE == 0) //secondary languages in progmem or xflash
  21. //reserved xx kbytes for secondary language table
  22. const char _SEC_LANG[LANG_SIZE_RESERVED] PROGMEM_I2 = "_SEC_LANG";
  23. //primary language signature
  24. const uint32_t _PRI_LANG_SIGNATURE[1] __attribute__((section(".progmem0"))) = {0xffffffff};
  25. //lang_table pointer
  26. lang_table_t* lang_table = 0;
  27. const char* lang_get_translation(const char* s)
  28. {
  29. if (lang_selected == 0) return s + 2; //primary language selected, return orig. str.
  30. if (lang_table == 0) return s + 2; //sec. lang table not found, return orig. str.
  31. uint16_t ui = pgm_read_word(((uint16_t*)s)); //read string id
  32. if (ui == 0xffff) return s + 2; //translation not found, return orig. str.
  33. ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16 + ui*2)))); //read relative offset
  34. if (pgm_read_byte(((uint8_t*)((char*)lang_table + ui))) == 0) //read first character
  35. return s + 2;//zero length string == not translated, return orig. str.
  36. return (const char*)((char*)lang_table + ui); //return calculated pointer
  37. }
  38. uint8_t lang_select(uint8_t lang)
  39. {
  40. if (lang == LANG_ID_PRI) //primary language
  41. {
  42. lang_table = 0;
  43. lang_selected = lang;
  44. }
  45. #ifdef W25X20CL
  46. if (lang_get_code(lang) == lang_get_code(LANG_ID_SEC)) lang = LANG_ID_SEC;
  47. if (lang == LANG_ID_SEC) //current secondary language
  48. {
  49. if (pgm_read_dword(((uint32_t*)_SEC_LANG_TABLE)) == LANG_MAGIC) //magic valid
  50. {
  51. if (lang_check(_SEC_LANG_TABLE))
  52. if (pgm_read_dword(((uint32_t*)(_SEC_LANG_TABLE + 12))) == pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE)))) //signature valid
  53. {
  54. lang_table = (lang_table_t*)(_SEC_LANG_TABLE); // set table pointer
  55. lang_selected = lang; // set language id
  56. }
  57. }
  58. }
  59. #else //W25X20CL
  60. if (lang == LANG_ID_SEC)
  61. {
  62. uint16_t table = _SEC_LANG_TABLE;
  63. if (pgm_read_dword(((uint32_t*)table)) == LANG_MAGIC) //magic valid
  64. {
  65. if (lang_check(table))
  66. if (pgm_read_dword(((uint32_t*)(table + 12))) == pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE)))) //signature valid
  67. {
  68. lang_table = (lang_table_t*)table; // set table pointer
  69. lang_selected = lang; // set language id
  70. }
  71. }
  72. }
  73. #endif //W25X20CL
  74. if (lang_selected == lang)
  75. {
  76. eeprom_update_byte((unsigned char*)EEPROM_LANG, lang_selected);
  77. return 1;
  78. }
  79. return 0;
  80. }
  81. uint8_t lang_check(uint16_t addr)
  82. {
  83. uint16_t sum = 0;
  84. uint16_t size = pgm_read_word((uint16_t*)(addr + 4));
  85. uint16_t lt_sum = pgm_read_word((uint16_t*)(addr + 8));
  86. uint16_t i; for (i = 0; i < size; i++)
  87. sum += (uint16_t)pgm_read_byte((uint8_t*)(addr + i)) << ((i & 1)?0:8);
  88. sum -= lt_sum; //subtract checksum
  89. sum = (sum >> 8) | ((sum & 0xff) << 8); //swap bytes
  90. return (sum == lt_sum);
  91. }
  92. uint8_t lang_get_count()
  93. {
  94. if (pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE))) == 0xffffffff)
  95. return 1; //signature not set - only primary language will be available
  96. #ifdef W25X20CL
  97. W25X20CL_SPI_ENTER();
  98. uint8_t count = 2; //count = 1+n (primary + secondary + all in xflash)
  99. uint32_t addr = 0x00000; //start of xflash
  100. lang_table_header_t header; //table header structure
  101. while (1)
  102. {
  103. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  104. if (header.magic != LANG_MAGIC) break; //break if magic not valid
  105. addr += header.size; //calc address of next table
  106. count++; //inc counter
  107. }
  108. #else //W25X20CL
  109. uint16_t table = _SEC_LANG_TABLE;
  110. uint8_t count = 1; //count = 1 (primary)
  111. while (pgm_read_dword(((uint32_t*)table)) == LANG_MAGIC) //magic valid
  112. {
  113. table += pgm_read_word((uint16_t*)(table + 4));
  114. count++;
  115. }
  116. #endif //W25X20CL
  117. return count;
  118. }
  119. uint8_t lang_get_header(uint8_t lang, lang_table_header_t* header, uint32_t* offset)
  120. {
  121. if (lang == LANG_ID_PRI) return 0; //primary lang not supported for this function
  122. #ifdef W25X20CL
  123. if (lang == LANG_ID_SEC)
  124. {
  125. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  126. memcpy_P(header, (lang_table_t*)(_SEC_LANG_TABLE), sizeof(lang_table_header_t)); //read table header from progmem
  127. if (offset) *offset = ui;
  128. return (header->magic == LANG_MAGIC)?1:0; //return 1 if magic valid
  129. }
  130. W25X20CL_SPI_ENTER();
  131. uint32_t addr = 0x00000; //start of xflash
  132. lang--;
  133. while (1)
  134. {
  135. w25x20cl_rd_data(addr, (uint8_t*)(header), sizeof(lang_table_header_t)); //read table header from xflash
  136. if (header->magic != LANG_MAGIC) break; //break if not valid
  137. if (offset) *offset = addr;
  138. if (--lang == 0) return 1;
  139. addr += header->size; //calc address of next table
  140. }
  141. #else //W25X20CL
  142. if (lang == LANG_ID_SEC)
  143. {
  144. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  145. memcpy_P(header, (lang_table_header_t*)ui, sizeof(lang_table_header_t)); //read table header from progmem
  146. if (offset) *offset = ui;
  147. return (header->magic == LANG_MAGIC)?1:0; //return 1 if magic valid
  148. }
  149. #endif //W25X20CL
  150. return 0;
  151. }
  152. uint16_t lang_get_code(uint8_t lang)
  153. {
  154. if (lang == LANG_ID_PRI) return LANG_CODE_EN; //primary lang = EN
  155. #ifdef W25X20CL
  156. if (lang == LANG_ID_SEC)
  157. {
  158. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  159. if (pgm_read_dword(((uint32_t*)(ui + 0))) != LANG_MAGIC) return LANG_CODE_XX; //magic not valid
  160. return pgm_read_word(((uint32_t*)(ui + 10))); //return lang code from progmem
  161. }
  162. W25X20CL_SPI_ENTER();
  163. uint32_t addr = 0x00000; //start of xflash
  164. lang_table_header_t header; //table header structure
  165. lang--;
  166. while (1)
  167. {
  168. w25x20cl_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
  169. if (header.magic != LANG_MAGIC) break; //break if not valid
  170. if (--lang == 0) return header.code;
  171. addr += header.size; //calc address of next table
  172. }
  173. #else //W25X20CL
  174. uint16_t table = _SEC_LANG_TABLE;
  175. uint8_t count = 1; //count = 1 (primary)
  176. while (pgm_read_dword((uint32_t*)table) == LANG_MAGIC) //magic valid
  177. {
  178. if (count == lang) return pgm_read_word(((uint16_t*)(table + 10))); //read language code
  179. table += pgm_read_word((uint16_t*)(table + 4));
  180. count++;
  181. }
  182. #endif //W25X20CL
  183. return LANG_CODE_XX;
  184. }
  185. const char* lang_get_name_by_code(uint16_t code)
  186. {
  187. switch (code)
  188. {
  189. case LANG_CODE_EN: return _n("English");
  190. case LANG_CODE_CZ: return _n("Cestina");
  191. case LANG_CODE_DE: return _n("Deutsch");
  192. case LANG_CODE_ES: return _n("Espanol");
  193. case LANG_CODE_FR: return _n("Francais");
  194. case LANG_CODE_IT: return _n("Italiano");
  195. case LANG_CODE_NL: return _n("Nederlands");
  196. case LANG_CODE_PL: return _n("Polski");
  197. }
  198. return _n("??");
  199. }
  200. void lang_reset(void)
  201. {
  202. lang_selected = 0;
  203. eeprom_update_byte((unsigned char*)EEPROM_LANG, LANG_ID_FORCE_SELECTION);
  204. }
  205. uint8_t lang_is_selected(void)
  206. {
  207. uint8_t lang_eeprom = eeprom_read_byte((unsigned char*)EEPROM_LANG);
  208. return (lang_eeprom != LANG_ID_FORCE_SELECTION) && (lang_eeprom == lang_selected);
  209. }
  210. #ifdef DEBUG_SEC_LANG
  211. #include <stdio.h>
  212. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  213. {
  214. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  215. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  216. }
  217. uint16_t lang_print_sec_lang(FILE* out)
  218. {
  219. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  220. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  221. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  222. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  223. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  224. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  225. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  226. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  227. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  228. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  229. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  230. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  231. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  232. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  233. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  234. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  235. if (_lt_magic != LANG_MAGIC) return 0;
  236. puts_P(_n(" strings:\n"));
  237. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  238. for (ui = 0; ui < _lt_count; ui++)
  239. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  240. return _lt_count;
  241. }
  242. #endif //DEBUG_SEC_LANG
  243. #endif //(LANG_MODE == 0)
  244. void lang_boot_update_start(uint8_t lang)
  245. {
  246. uint8_t cnt = lang_get_count();
  247. if ((lang < 2) || (lang > cnt)) return; //only languages from xflash can be selected
  248. bootapp_reboot_user0(lang << 4);
  249. }