language.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(uint8_t lang) { return 0; }
  15. uint8_t lang_get_count() { return 1; }
  16. uint16_t lang_get_code(uint8_t lang) { return LANG_CODE_EN; }
  17. const char* lang_get_name_by_code(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 = _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 = 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, ui, 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, 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. return 0;
  142. #else //W25X20CL
  143. if (lang == LANG_ID_SEC)
  144. {
  145. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  146. memcpy_P(header, ui, sizeof(lang_table_header_t)); //read table header from progmem
  147. if (offset) *offset = ui;
  148. return (header->magic == LANG_MAGIC)?1:0; //return 1 if magic valid
  149. }
  150. #endif //W25X20CL
  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. if (lang == LANG_ID_SEC)
  175. {
  176. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  177. if (pgm_read_dword(((uint32_t*)(ui + 0))) == LANG_MAGIC) //magic num is OK
  178. return pgm_read_word(((uint16_t*)(ui + 10))); //read language code
  179. }
  180. #endif //W25X20CL
  181. return LANG_CODE_XX;
  182. }
  183. const char* lang_get_name_by_code(uint16_t code)
  184. {
  185. switch (code)
  186. {
  187. case LANG_CODE_EN: return _n("English");
  188. case LANG_CODE_CZ: return _n("Cestina");
  189. case LANG_CODE_DE: return _n("Deutsch");
  190. case LANG_CODE_ES: return _n("Espanol");
  191. case LANG_CODE_IT: return _n("Italiano");
  192. case LANG_CODE_PL: return _n("Polski");
  193. }
  194. return _n("??");
  195. }
  196. void lang_reset(void)
  197. {
  198. lang_selected = 0;
  199. eeprom_update_byte((unsigned char*)EEPROM_LANG, LANG_ID_FORCE_SELECTION);
  200. }
  201. uint8_t lang_is_selected(void)
  202. {
  203. uint8_t lang_eeprom = eeprom_read_byte((unsigned char*)EEPROM_LANG);
  204. return (lang_eeprom != LANG_ID_FORCE_SELECTION) && (lang_eeprom == lang_selected);
  205. }
  206. #ifdef DEBUG_SEC_LANG
  207. const char* lang_get_sec_lang_str_by_id(uint16_t id)
  208. {
  209. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  210. return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
  211. }
  212. uint16_t lang_print_sec_lang(FILE* out)
  213. {
  214. printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
  215. printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
  216. uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
  217. printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
  218. uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
  219. uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
  220. uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
  221. uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
  222. uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
  223. uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
  224. printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
  225. printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
  226. printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
  227. printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
  228. printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
  229. printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
  230. if (_lt_magic != LANG_MAGIC) return 0;
  231. puts_P(_n(" strings:\n"));
  232. uint16_t ui = _SEC_LANG_TABLE; //table pointer
  233. for (ui = 0; ui < _lt_count; ui++)
  234. fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
  235. return _lt_count;
  236. }
  237. #endif //DEBUG_SEC_LANG
  238. #endif //(LANG_MODE == 0)
  239. void lang_boot_update_start(uint8_t lang)
  240. {
  241. uint8_t cnt = lang_get_count();
  242. if ((lang < 2) || (lang > cnt)) return; //only languages from xflash can be selected
  243. bootapp_reboot_user0(lang << 4);
  244. }