2018-05-22 01:20:03 +00:00
|
|
|
//language.c
|
|
|
|
#include "language.h"
|
|
|
|
#include <avr/pgmspace.h>
|
2022-02-05 23:25:51 +00:00
|
|
|
#include <avr/io.h>
|
2018-06-10 14:04:32 +00:00
|
|
|
#include <avr/eeprom.h>
|
2018-05-29 19:03:47 +00:00
|
|
|
#include "bootapp.h"
|
2018-05-22 01:20:03 +00:00
|
|
|
|
2018-06-10 14:04:32 +00:00
|
|
|
#include "Configuration.h"
|
2018-06-21 17:58:13 +00:00
|
|
|
#include "pins.h"
|
2018-05-22 01:20:03 +00:00
|
|
|
|
2021-03-05 07:51:23 +00:00
|
|
|
#ifdef XFLASH
|
|
|
|
#include "xflash.h"
|
2021-06-08 13:20:23 +00:00
|
|
|
#include "xflash_layout.h"
|
2021-03-05 07:51:23 +00:00
|
|
|
#endif //XFLASH
|
2018-06-07 22:20:28 +00:00
|
|
|
|
|
|
|
// Currently active language selection.
|
2018-05-30 17:29:52 +00:00
|
|
|
uint8_t lang_selected = 0;
|
2018-05-22 01:20:03 +00:00
|
|
|
|
2018-06-07 22:20:28 +00:00
|
|
|
|
2018-05-22 01:20:03 +00:00
|
|
|
#if (LANG_MODE == 0) //primary language only
|
2018-06-07 22:20:28 +00:00
|
|
|
|
2020-09-11 13:12:49 +00:00
|
|
|
uint8_t lang_select(_UNUSED uint8_t lang) { return 0; }
|
2018-06-07 22:20:28 +00:00
|
|
|
uint8_t lang_get_count() { return 1; }
|
2020-09-11 13:12:49 +00:00
|
|
|
uint16_t lang_get_code(_UNUSED uint8_t lang) { return LANG_CODE_EN; }
|
|
|
|
const char* lang_get_name_by_code(_UNUSED uint16_t code) { return _n("English"); }
|
2018-06-10 14:28:15 +00:00
|
|
|
void lang_reset(void) { }
|
|
|
|
uint8_t lang_is_selected(void) { return 1; }
|
2018-06-07 22:20:28 +00:00
|
|
|
|
|
|
|
#else //(LANG_MODE == 0) //secondary languages in progmem or xflash
|
|
|
|
|
2018-05-22 01:20:03 +00:00
|
|
|
//reserved xx kbytes for secondary language table
|
2022-02-05 23:25:51 +00:00
|
|
|
const char _SEC_LANG[LANG_SIZE_RESERVED] __attribute__((aligned(SPM_PAGESIZE))) PROGMEM_I2 = "_SEC_LANG";
|
2018-05-22 01:20:03 +00:00
|
|
|
|
2018-06-15 18:37:33 +00:00
|
|
|
//primary language signature
|
|
|
|
const uint32_t _PRI_LANG_SIGNATURE[1] __attribute__((section(".progmem0"))) = {0xffffffff};
|
|
|
|
|
2018-05-22 01:20:03 +00:00
|
|
|
//lang_table pointer
|
|
|
|
lang_table_t* lang_table = 0;
|
|
|
|
|
|
|
|
const char* lang_get_translation(const char* s)
|
|
|
|
{
|
2018-06-07 22:20:28 +00:00
|
|
|
if (lang_selected == 0) return s + 2; //primary language selected, return orig. str.
|
|
|
|
if (lang_table == 0) return s + 2; //sec. lang table not found, return orig. str.
|
2018-05-22 01:20:03 +00:00
|
|
|
uint16_t ui = pgm_read_word(((uint16_t*)s)); //read string id
|
2018-06-07 22:20:28 +00:00
|
|
|
if (ui == 0xffff) return s + 2; //translation not found, return orig. str.
|
2018-05-22 01:20:03 +00:00
|
|
|
ui = pgm_read_word(((uint16_t*)(((char*)lang_table + 16 + ui*2)))); //read relative offset
|
2018-06-07 22:20:28 +00:00
|
|
|
if (pgm_read_byte(((uint8_t*)((char*)lang_table + ui))) == 0) //read first character
|
|
|
|
return s + 2;//zero length string == not translated, return orig. str.
|
2018-05-27 13:14:04 +00:00
|
|
|
return (const char*)((char*)lang_table + ui); //return calculated pointer
|
|
|
|
}
|
|
|
|
|
2018-06-07 22:20:28 +00:00
|
|
|
uint8_t lang_select(uint8_t lang)
|
2018-05-22 01:20:03 +00:00
|
|
|
{
|
2018-06-07 22:20:28 +00:00
|
|
|
if (lang == LANG_ID_PRI) //primary language
|
2018-05-22 01:20:03 +00:00
|
|
|
{
|
|
|
|
lang_table = 0;
|
2018-06-10 14:04:32 +00:00
|
|
|
lang_selected = lang;
|
2018-06-07 22:20:28 +00:00
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#ifdef XFLASH
|
2018-06-10 14:04:32 +00:00
|
|
|
if (lang_get_code(lang) == lang_get_code(LANG_ID_SEC)) lang = LANG_ID_SEC;
|
2018-06-07 22:20:28 +00:00
|
|
|
if (lang == LANG_ID_SEC) //current secondary language
|
|
|
|
{
|
2018-06-10 14:04:32 +00:00
|
|
|
if (pgm_read_dword(((uint32_t*)_SEC_LANG_TABLE)) == LANG_MAGIC) //magic valid
|
|
|
|
{
|
2018-06-15 18:37:33 +00:00
|
|
|
if (lang_check(_SEC_LANG_TABLE))
|
|
|
|
if (pgm_read_dword(((uint32_t*)(_SEC_LANG_TABLE + 12))) == pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE)))) //signature valid
|
|
|
|
{
|
2018-07-23 11:35:38 +00:00
|
|
|
lang_table = (lang_table_t*)(_SEC_LANG_TABLE); // set table pointer
|
2018-06-15 18:37:33 +00:00
|
|
|
lang_selected = lang; // set language id
|
|
|
|
}
|
2018-06-10 14:04:32 +00:00
|
|
|
}
|
2018-05-22 01:20:03 +00:00
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#else //XFLASH
|
2018-06-21 18:57:22 +00:00
|
|
|
if (lang == LANG_ID_SEC)
|
|
|
|
{
|
|
|
|
uint16_t table = _SEC_LANG_TABLE;
|
|
|
|
if (pgm_read_dword(((uint32_t*)table)) == LANG_MAGIC) //magic valid
|
|
|
|
{
|
|
|
|
if (lang_check(table))
|
|
|
|
if (pgm_read_dword(((uint32_t*)(table + 12))) == pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE)))) //signature valid
|
|
|
|
{
|
2019-03-05 15:27:47 +00:00
|
|
|
lang_table = (lang_table_t*)table; // set table pointer
|
2018-06-21 18:57:22 +00:00
|
|
|
lang_selected = lang; // set language id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#endif //XFLASH
|
2018-06-10 14:04:32 +00:00
|
|
|
if (lang_selected == lang)
|
|
|
|
{
|
|
|
|
eeprom_update_byte((unsigned char*)EEPROM_LANG, lang_selected);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-06-08 00:03:25 +00:00
|
|
|
return 0;
|
2018-05-28 15:14:55 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 18:37:33 +00:00
|
|
|
uint8_t lang_check(uint16_t addr)
|
|
|
|
{
|
|
|
|
uint16_t sum = 0;
|
|
|
|
uint16_t size = pgm_read_word((uint16_t*)(addr + 4));
|
|
|
|
uint16_t lt_sum = pgm_read_word((uint16_t*)(addr + 8));
|
|
|
|
uint16_t i; for (i = 0; i < size; i++)
|
|
|
|
sum += (uint16_t)pgm_read_byte((uint8_t*)(addr + i)) << ((i & 1)?0:8);
|
|
|
|
sum -= lt_sum; //subtract checksum
|
|
|
|
sum = (sum >> 8) | ((sum & 0xff) << 8); //swap bytes
|
|
|
|
return (sum == lt_sum);
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:29:52 +00:00
|
|
|
uint8_t lang_get_count()
|
2018-05-28 17:41:16 +00:00
|
|
|
{
|
2018-06-18 17:37:20 +00:00
|
|
|
if (pgm_read_dword(((uint32_t*)(_PRI_LANG_SIGNATURE))) == 0xffffffff)
|
|
|
|
return 1; //signature not set - only primary language will be available
|
2021-03-05 07:51:23 +00:00
|
|
|
#ifdef XFLASH
|
|
|
|
XFLASH_SPI_ENTER();
|
2018-06-07 23:19:49 +00:00
|
|
|
uint8_t count = 2; //count = 1+n (primary + secondary + all in xflash)
|
2021-06-08 13:20:23 +00:00
|
|
|
uint32_t addr = LANG_OFFSET;
|
2018-06-07 22:20:28 +00:00
|
|
|
lang_table_header_t header; //table header structure
|
|
|
|
while (1)
|
|
|
|
{
|
2021-03-05 07:51:23 +00:00
|
|
|
xflash_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
|
2018-06-07 22:20:28 +00:00
|
|
|
if (header.magic != LANG_MAGIC) break; //break if magic not valid
|
|
|
|
addr += header.size; //calc address of next table
|
|
|
|
count++; //inc counter
|
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#else //XFLASH
|
2018-06-21 18:57:22 +00:00
|
|
|
uint16_t table = _SEC_LANG_TABLE;
|
|
|
|
uint8_t count = 1; //count = 1 (primary)
|
|
|
|
while (pgm_read_dword(((uint32_t*)table)) == LANG_MAGIC) //magic valid
|
|
|
|
{
|
|
|
|
table += pgm_read_word((uint16_t*)(table + 4));
|
|
|
|
count++;
|
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#endif //XFLASH
|
2018-06-21 18:57:22 +00:00
|
|
|
return count;
|
2018-05-28 17:41:16 +00:00
|
|
|
}
|
|
|
|
|
2018-06-08 23:23:04 +00:00
|
|
|
uint8_t lang_get_header(uint8_t lang, lang_table_header_t* header, uint32_t* offset)
|
2018-05-28 15:14:55 +00:00
|
|
|
{
|
2018-06-08 23:23:04 +00:00
|
|
|
if (lang == LANG_ID_PRI) return 0; //primary lang not supported for this function
|
2021-03-05 07:51:23 +00:00
|
|
|
#ifdef XFLASH
|
2018-06-08 23:23:04 +00:00
|
|
|
if (lang == LANG_ID_SEC)
|
|
|
|
{
|
2018-06-09 17:16:36 +00:00
|
|
|
uint16_t ui = _SEC_LANG_TABLE; //table pointer
|
2018-07-23 11:35:38 +00:00
|
|
|
memcpy_P(header, (lang_table_t*)(_SEC_LANG_TABLE), sizeof(lang_table_header_t)); //read table header from progmem
|
2018-06-08 23:23:04 +00:00
|
|
|
if (offset) *offset = ui;
|
2018-06-15 18:37:33 +00:00
|
|
|
return (header->magic == LANG_MAGIC)?1:0; //return 1 if magic valid
|
2018-06-08 23:23:04 +00:00
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
XFLASH_SPI_ENTER();
|
2021-06-08 13:20:23 +00:00
|
|
|
uint32_t addr = LANG_OFFSET;
|
2018-06-08 23:23:04 +00:00
|
|
|
lang--;
|
|
|
|
while (1)
|
|
|
|
{
|
2021-03-05 07:51:23 +00:00
|
|
|
xflash_rd_data(addr, (uint8_t*)(header), sizeof(lang_table_header_t)); //read table header from xflash
|
2018-06-08 23:23:04 +00:00
|
|
|
if (header->magic != LANG_MAGIC) break; //break if not valid
|
|
|
|
if (offset) *offset = addr;
|
|
|
|
if (--lang == 0) return 1;
|
|
|
|
addr += header->size; //calc address of next table
|
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#else //XFLASH
|
2018-06-21 18:57:22 +00:00
|
|
|
if (lang == LANG_ID_SEC)
|
|
|
|
{
|
|
|
|
uint16_t ui = _SEC_LANG_TABLE; //table pointer
|
2019-03-05 15:27:47 +00:00
|
|
|
memcpy_P(header, (lang_table_header_t*)ui, sizeof(lang_table_header_t)); //read table header from progmem
|
2018-06-21 18:57:22 +00:00
|
|
|
if (offset) *offset = ui;
|
|
|
|
return (header->magic == LANG_MAGIC)?1:0; //return 1 if magic valid
|
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#endif //XFLASH
|
2018-06-22 23:31:38 +00:00
|
|
|
return 0;
|
2018-06-08 23:23:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t lang_get_code(uint8_t lang)
|
|
|
|
{
|
2018-06-07 22:20:28 +00:00
|
|
|
if (lang == LANG_ID_PRI) return LANG_CODE_EN; //primary lang = EN
|
2021-03-05 07:51:23 +00:00
|
|
|
#ifdef XFLASH
|
2018-06-01 08:47:47 +00:00
|
|
|
if (lang == LANG_ID_SEC)
|
|
|
|
{
|
2018-06-09 17:16:36 +00:00
|
|
|
uint16_t ui = _SEC_LANG_TABLE; //table pointer
|
2018-06-07 22:20:28 +00:00
|
|
|
if (pgm_read_dword(((uint32_t*)(ui + 0))) != LANG_MAGIC) return LANG_CODE_XX; //magic not valid
|
|
|
|
return pgm_read_word(((uint32_t*)(ui + 10))); //return lang code from progmem
|
2018-06-01 08:47:47 +00:00
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
XFLASH_SPI_ENTER();
|
2021-06-08 13:20:23 +00:00
|
|
|
uint32_t addr = LANG_OFFSET;
|
2018-06-07 22:20:28 +00:00
|
|
|
lang_table_header_t header; //table header structure
|
|
|
|
lang--;
|
|
|
|
while (1)
|
|
|
|
{
|
2021-03-05 07:51:23 +00:00
|
|
|
xflash_rd_data(addr, (uint8_t*)&header, sizeof(lang_table_header_t)); //read table header from xflash
|
2018-06-07 22:20:28 +00:00
|
|
|
if (header.magic != LANG_MAGIC) break; //break if not valid
|
|
|
|
if (--lang == 0) return header.code;
|
|
|
|
addr += header.size; //calc address of next table
|
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#else //XFLASH
|
2018-06-22 23:31:38 +00:00
|
|
|
uint16_t table = _SEC_LANG_TABLE;
|
|
|
|
uint8_t count = 1; //count = 1 (primary)
|
|
|
|
while (pgm_read_dword((uint32_t*)table) == LANG_MAGIC) //magic valid
|
2018-06-21 18:57:22 +00:00
|
|
|
{
|
2018-06-22 23:31:38 +00:00
|
|
|
if (count == lang) return pgm_read_word(((uint16_t*)(table + 10))); //read language code
|
|
|
|
table += pgm_read_word((uint16_t*)(table + 4));
|
|
|
|
count++;
|
2018-06-21 18:57:22 +00:00
|
|
|
}
|
2021-03-05 07:51:23 +00:00
|
|
|
#endif //XFLASH
|
2018-06-07 22:20:28 +00:00
|
|
|
return LANG_CODE_XX;
|
2018-06-01 08:47:47 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 22:20:28 +00:00
|
|
|
const char* lang_get_name_by_code(uint16_t code)
|
2018-06-01 08:47:47 +00:00
|
|
|
{
|
2018-06-07 22:20:28 +00:00
|
|
|
switch (code)
|
2018-06-01 08:47:47 +00:00
|
|
|
{
|
2018-06-07 22:20:28 +00:00
|
|
|
case LANG_CODE_EN: return _n("English");
|
|
|
|
case LANG_CODE_CZ: return _n("Cestina");
|
|
|
|
case LANG_CODE_DE: return _n("Deutsch");
|
|
|
|
case LANG_CODE_ES: return _n("Espanol");
|
2018-09-11 01:43:05 +00:00
|
|
|
case LANG_CODE_FR: return _n("Francais");
|
2018-06-07 22:20:28 +00:00
|
|
|
case LANG_CODE_IT: return _n("Italiano");
|
|
|
|
case LANG_CODE_PL: return _n("Polski");
|
2021-03-01 07:22:43 +00:00
|
|
|
#ifdef COMMUNITY_LANG_SUPPORT //Community language support
|
|
|
|
#ifdef COMMUNITY_LANG_NL
|
|
|
|
case LANG_CODE_NL: return _n("Nederlands"); //community contribution
|
|
|
|
#endif // COMMUNITY_LANG_NL
|
|
|
|
|
|
|
|
//Use the 3 lines below as a template and replace 'QR' and 'New language'
|
|
|
|
//#ifdef COMMUNITY_LANG_QR
|
|
|
|
// case LANG_CODE_QR: return _n("New language"); //community contribution
|
|
|
|
//#endif // COMMUNITY_LANG_QR
|
|
|
|
#endif // COMMUNITY_LANG_SUPPORT
|
2018-06-01 08:47:47 +00:00
|
|
|
}
|
2018-06-07 22:20:28 +00:00
|
|
|
return _n("??");
|
2018-05-28 15:14:55 +00:00
|
|
|
}
|
|
|
|
|
2018-06-10 14:04:32 +00:00
|
|
|
void lang_reset(void)
|
|
|
|
{
|
|
|
|
lang_selected = 0;
|
|
|
|
eeprom_update_byte((unsigned char*)EEPROM_LANG, LANG_ID_FORCE_SELECTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t lang_is_selected(void)
|
|
|
|
{
|
|
|
|
uint8_t lang_eeprom = eeprom_read_byte((unsigned char*)EEPROM_LANG);
|
|
|
|
return (lang_eeprom != LANG_ID_FORCE_SELECTION) && (lang_eeprom == lang_selected);
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:29:52 +00:00
|
|
|
#ifdef DEBUG_SEC_LANG
|
2018-10-18 13:39:08 +00:00
|
|
|
#include <stdio.h>
|
2018-06-07 22:20:28 +00:00
|
|
|
const char* lang_get_sec_lang_str_by_id(uint16_t id)
|
|
|
|
{
|
2018-06-09 17:16:36 +00:00
|
|
|
uint16_t ui = _SEC_LANG_TABLE; //table pointer
|
2018-06-07 22:20:28 +00:00
|
|
|
return ui + pgm_read_word(((uint16_t*)(ui + 16 + id * 2))); //read relative offset and return calculated pointer
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:29:52 +00:00
|
|
|
uint16_t lang_print_sec_lang(FILE* out)
|
|
|
|
{
|
|
|
|
printf_P(_n("&_SEC_LANG = 0x%04x\n"), &_SEC_LANG);
|
|
|
|
printf_P(_n("sizeof(_SEC_LANG) = 0x%04x\n"), sizeof(_SEC_LANG));
|
|
|
|
uint16_t ptr_lang_table0 = ((uint16_t)(&_SEC_LANG) + 0xff) & 0xff00;
|
|
|
|
printf_P(_n("&_lang_table0 = 0x%04x\n"), ptr_lang_table0);
|
|
|
|
uint32_t _lt_magic = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 0)));
|
|
|
|
uint16_t _lt_size = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 4)));
|
|
|
|
uint16_t _lt_count = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 6)));
|
|
|
|
uint16_t _lt_chsum = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 8)));
|
|
|
|
uint16_t _lt_resv0 = pgm_read_word(((uint16_t*)(ptr_lang_table0 + 10)));
|
|
|
|
uint32_t _lt_resv1 = pgm_read_dword(((uint32_t*)(ptr_lang_table0 + 12)));
|
2018-06-01 08:47:47 +00:00
|
|
|
printf_P(_n(" _lt_magic = 0x%08lx %S\n"), _lt_magic, (_lt_magic==LANG_MAGIC)?_n("OK"):_n("NA"));
|
2018-05-30 17:29:52 +00:00
|
|
|
printf_P(_n(" _lt_size = 0x%04x (%d)\n"), _lt_size, _lt_size);
|
|
|
|
printf_P(_n(" _lt_count = 0x%04x (%d)\n"), _lt_count, _lt_count);
|
|
|
|
printf_P(_n(" _lt_chsum = 0x%04x\n"), _lt_chsum);
|
|
|
|
printf_P(_n(" _lt_resv0 = 0x%04x\n"), _lt_resv0);
|
|
|
|
printf_P(_n(" _lt_resv1 = 0x%08lx\n"), _lt_resv1);
|
2018-06-01 08:47:47 +00:00
|
|
|
if (_lt_magic != LANG_MAGIC) return 0;
|
2022-02-05 23:23:40 +00:00
|
|
|
puts_P(_n(" strings:"));
|
2018-06-09 17:16:36 +00:00
|
|
|
uint16_t ui = _SEC_LANG_TABLE; //table pointer
|
2018-05-30 17:29:52 +00:00
|
|
|
for (ui = 0; ui < _lt_count; ui++)
|
|
|
|
fprintf_P(out, _n(" %3d %S\n"), ui, lang_get_sec_lang_str_by_id(ui));
|
|
|
|
return _lt_count;
|
|
|
|
}
|
|
|
|
#endif //DEBUG_SEC_LANG
|
|
|
|
|
2018-06-07 22:20:28 +00:00
|
|
|
#endif //(LANG_MODE == 0)
|
2018-05-30 17:29:52 +00:00
|
|
|
|
2018-05-29 19:03:47 +00:00
|
|
|
|
2018-06-09 22:22:28 +00:00
|
|
|
void lang_boot_update_start(uint8_t lang)
|
2018-06-09 17:16:36 +00:00
|
|
|
{
|
|
|
|
uint8_t cnt = lang_get_count();
|
|
|
|
if ((lang < 2) || (lang > cnt)) return; //only languages from xflash can be selected
|
|
|
|
bootapp_reboot_user0(lang << 4);
|
|
|
|
}
|