Compare commits

..

No commits in common. "3194ec6ed35bcd5982fa8d6229c779a95329584e" and "e09f3c88b08bc08a75d05354548ac67c9f2b230c" have entirely different histories.

25 changed files with 16238 additions and 611 deletions

View File

@ -36,9 +36,8 @@ typedef union {
uint8_t y_offset:3;
uint8_t x_size:2;
uint8_t y_size:2;
uint8_t border_color:3;
uint8_t highlight_color:3;
uint32_t osd_rsv:11;
uint8_t border_color:2;
uint32_t osd_rsv:15;
} __attribute__((packed, __may_alias__));
uint32_t data;
} osd_config_reg;

View File

@ -156,4 +156,4 @@ add_interface_port osd_if vclk vclk Input 1
add_interface_port osd_if xpos xpos Input 11
add_interface_port osd_if ypos ypos Input 11
add_interface_port osd_if osd_enable osd_enable Output 1
add_interface_port osd_if osd_color osd_color Output 3
add_interface_port osd_if osd_color osd_color Output 2

View File

@ -35,7 +35,7 @@ module osd_generator_top (
input [10:0] xpos,
input [10:0] ypos,
output reg osd_enable,
output reg [2:0] osd_color
output reg [1:0] osd_color
);
localparam CHAR_ROWS = 30;
@ -43,10 +43,10 @@ localparam CHAR_COLS = 16;
localparam CHAR_SECTIONS = 2;
localparam CHAR_SEC_SEPARATOR = 2;
localparam BG_BLACK = 3'h0;
localparam BG_BLUE = 3'h1;
localparam BG_YELLOW = 3'h6;
localparam BG_WHITE = 3'h7;
localparam BG_BLACK = 2'h0;
localparam BG_BLUE = 2'h1;
localparam BG_YELLOW = 2'h2;
localparam BG_WHITE = 2'h3;
localparam OSD_CONFIG_REGNUM = 8'hf0;
localparam OSD_ROW_LSEC_ENABLE_REGNUM = 8'hf1;
@ -59,9 +59,7 @@ reg [31:0] config_reg[OSD_ROW_LSEC_ENABLE_REGNUM:OSD_ROW_COLOR_REGNUM] /* synthe
reg [10:0] xpos_osd_area_scaled, xpos_text_scaled;
reg [10:0] ypos_osd_area_scaled, ypos_text_scaled;
reg [7:0] x_ptr[2:5], y_ptr[2:5] /* synthesis ramstyle = "logic" */;
reg osd_text_act_lsec_x_hit, osd_text_act_lsec_en, osd_text_act_rsec_x_hit, osd_text_act_rsec_en, osd_text_act_y_hit;
reg osd_act_lsec_x_hit, osd_act_lsec_en, osd_act_rsec_x_hit, osd_act_rsec_en, osd_act_y_hit;
reg osd_text_act_pp[3:6], osd_act_pp[4:6];
reg osd_text_act_pp[2:6], osd_act_pp[3:6];
reg [14:0] to_ctr, to_ctr_ms;
reg char_px;
@ -73,8 +71,7 @@ wire [2:0] x_offset = osd_config[7:5];
wire [2:0] y_offset = osd_config[10:8];
wire [1:0] x_size = osd_config[12:11];
wire [1:0] y_size = osd_config[14:13];
wire [2:0] border_color = osd_config[17:15];
wire [2:0] highlight_color = osd_config[20:18];
wire [1:0] border_color = osd_config[16:15];
wire [10:0] xpos_scaled_w = (xpos >> x_size)-({3'h0, x_offset} << 3);
wire [10:0] ypos_scaled_w = (ypos >> y_size)-({3'h0, y_offset} << 3);
@ -126,27 +123,21 @@ always @(posedge vclk) begin
y_ptr[pp_idx] <= y_ptr[pp_idx-1];
end
osd_text_act_lsec_x_hit <= (xpos_text_scaled < 8*CHAR_COLS);
osd_text_act_lsec_en <= config_reg[OSD_ROW_LSEC_ENABLE_REGNUM][ypos_text_scaled/8];
osd_text_act_rsec_x_hit <= (xpos_text_scaled >= 8*(CHAR_COLS+CHAR_SEC_SEPARATOR)) & (xpos_text_scaled < 8*(2*CHAR_COLS+CHAR_SEC_SEPARATOR));
osd_text_act_rsec_en <= config_reg[OSD_ROW_RSEC_ENABLE_REGNUM][ypos_text_scaled/8];
osd_text_act_y_hit <= (ypos_text_scaled < 8*CHAR_ROWS);
osd_text_act_pp[3] <= render_enable &
osd_text_act_pp[2] <= render_enable &
(menu_active || (to_ctr_ms > 0)) &
((osd_text_act_lsec_x_hit & osd_text_act_lsec_en) | (osd_text_act_rsec_x_hit & osd_text_act_rsec_en)) & osd_text_act_y_hit;
for(pp_idx = 4; pp_idx <= 6; pp_idx = pp_idx+1) begin
(((xpos_text_scaled < 8*CHAR_COLS) & config_reg[OSD_ROW_LSEC_ENABLE_REGNUM][ypos_text_scaled/8]) |
((xpos_text_scaled >= 8*(CHAR_COLS+CHAR_SEC_SEPARATOR)) & (xpos_text_scaled < 8*(2*CHAR_COLS+CHAR_SEC_SEPARATOR)) & config_reg[OSD_ROW_RSEC_ENABLE_REGNUM][ypos_text_scaled/8])) &
(ypos_text_scaled < 8*CHAR_ROWS);
for(pp_idx = 3; pp_idx <= 6; pp_idx = pp_idx+1) begin
osd_text_act_pp[pp_idx] <= osd_text_act_pp[pp_idx-1];
end
osd_act_lsec_x_hit <= (xpos_osd_area_scaled/8 < (CHAR_COLS+1));
osd_act_lsec_en <= config_reg[OSD_ROW_LSEC_ENABLE_REGNUM][(ypos_osd_area_scaled/8) ? ((ypos_osd_area_scaled/8)-1) : 0];
osd_act_rsec_x_hit <= (xpos_osd_area_scaled/8 >= (CHAR_COLS+1)) & (xpos_osd_area_scaled/8 < (2*CHAR_COLS+CHAR_SEC_SEPARATOR+1));
osd_act_rsec_en <= (config_reg[OSD_ROW_RSEC_ENABLE_REGNUM][(ypos_osd_area_scaled/8)-1] | config_reg[OSD_ROW_RSEC_ENABLE_REGNUM][ypos_osd_area_scaled/8]);
osd_act_y_hit <= (ypos_osd_area_scaled < 8*(CHAR_ROWS+1));
osd_act_pp[4] <= render_enable &
osd_act_pp[3] <= render_enable &
(menu_active || (to_ctr_ms > 0)) &
((osd_act_lsec_x_hit & osd_act_lsec_en) | (osd_act_rsec_x_hit & osd_act_rsec_en)) & osd_act_y_hit;
for(pp_idx = 5; pp_idx <= 6; pp_idx = pp_idx+1) begin
(((xpos_osd_area_scaled/8 < (CHAR_COLS+1)) & config_reg[OSD_ROW_LSEC_ENABLE_REGNUM][(ypos_osd_area_scaled/8) ? ((ypos_osd_area_scaled/8)-1) : 0]) |
((xpos_osd_area_scaled/8 >= (CHAR_COLS+1)) & (xpos_osd_area_scaled/8 < (2*CHAR_COLS+CHAR_SEC_SEPARATOR+1)) & (config_reg[OSD_ROW_RSEC_ENABLE_REGNUM][(ypos_osd_area_scaled/8)-1] | config_reg[OSD_ROW_RSEC_ENABLE_REGNUM][ypos_osd_area_scaled/8]))) &
(ypos_osd_area_scaled < 8*(CHAR_ROWS+1));
for(pp_idx = 4; pp_idx <= 6; pp_idx = pp_idx+1) begin
osd_act_pp[pp_idx] <= osd_act_pp[pp_idx-1];
end
@ -156,7 +147,7 @@ always @(posedge vclk) begin
if (osd_text_act_pp[6]) begin
if (char_px) begin
osd_color <= config_reg[OSD_ROW_COLOR_REGNUM][char_row] ? highlight_color : BG_WHITE;
osd_color <= config_reg[OSD_ROW_COLOR_REGNUM][char_row] ? BG_YELLOW : BG_WHITE;
end else begin
osd_color <= BG_BLUE;
end

View File

@ -16,7 +16,7 @@
<hex_block>
<hex_filename>software/sys_controller/mem_init/flash.hex</hex_filename>
<hex_addressing>relative</hex_addressing>
<hex_offset>327680</hex_offset>
<hex_offset>524288</hex_offset>
<hex_little_endian>0</hex_little_endian>
</hex_block>
<version>10</version>

View File

@ -2,12 +2,10 @@
create_clock -period 27MHz -name clk27 [get_ports clk27]
set_input_delay -clock clk27 0 [get_ports {sda scl SD_CMD SD_DAT*}]
set_input_delay -clock clk27 0 [get_ports {sda scl SD_CMD SD_DAT* *ALTERA_DATA0}]
set_false_path -from [get_ports {btn* cfg* ir_rx HDMI_TX_INT_N LED_R}]
set_false_path -to {sys:sys_inst|sys_pio_1:pio_1|readdata*}
create_generated_clock -name flash_clk -divide_by 2 -source clk27 [get_pins sys:sys_inst|sys_intel_generic_serial_flash_interface_top_0:intel_generic_serial_flash_interface_top_0|sys_intel_generic_serial_flash_interface_top_0_qspi_inf_inst:qspi_inf_inst|flash_clk_reg|q]
create_generated_clock -name flash_clk_out -master_clock flash_clk -source [get_pins sys:sys_inst|sys_intel_generic_serial_flash_interface_top_0:intel_generic_serial_flash_interface_top_0|sys_intel_generic_serial_flash_interface_top_0_qspi_inf_inst:qspi_inf_inst|flash_clk_reg|q] -multiply_by 1 [get_ports *ALTERA_DCLK]
### Scanconverter clock constraints ###
@ -57,11 +55,6 @@ set_false_path -to [remove_from_collection [all_outputs] $critoutputs_hdmi]
# Lumacode (constrained to max. 60MHz sampling)
set_max_delay 16.6 -from [get_registers sys:sys_inst|sc_config_top:sc_config_0|altsyncram:lumacode_pal_ram|*]
# Flash controller (delays from N25Q128A datasheet)
set_input_delay -clock flash_clk_out -clock_fall 5 [get_ports *ALTERA_DATA0]
set_output_delay -clock flash_clk_out 4 [get_ports *ALTERA_SCE]
set_output_delay -clock flash_clk_out 2 [get_ports *ALTERA_SDO]
### CPU/scanconverter clock relations ###

View File

@ -137,7 +137,7 @@ reg remote_event_prev;
reg [14:0] to_ctr, to_ctr_ms;
wire lcd_bl_timeout;
wire [2:0] osd_color;
wire [1:0] osd_color;
wire osd_enable_pre;
wire osd_enable = osd_enable_pre & ~lt_active;
wire [10:0] xpos_sc;
@ -325,7 +325,15 @@ end
// Output registers
always @(posedge pclk_out) begin
if (osd_enable) begin
{HDMI_TX_RD, HDMI_TX_GD, HDMI_TX_BD} <= {{8{osd_color[2]}}, {8{osd_color[1]}}, {8{osd_color[0]}}};
if (osd_color == 2'h0) begin
{HDMI_TX_RD, HDMI_TX_GD, HDMI_TX_BD} <= 24'h000000;
end else if (osd_color == 2'h1) begin
{HDMI_TX_RD, HDMI_TX_GD, HDMI_TX_BD} <= 24'h0000ff;
end else if (osd_color == 2'h2) begin
{HDMI_TX_RD, HDMI_TX_GD, HDMI_TX_BD} <= 24'hffff00;
end else begin
{HDMI_TX_RD, HDMI_TX_GD, HDMI_TX_BD} <= 24'hffffff;
end
end else begin
{HDMI_TX_RD, HDMI_TX_GD, HDMI_TX_BD} <= {R_sc, G_sc, B_sc};
end
@ -377,7 +385,7 @@ sys sys_inst(
.reset_po_reset_n (po_reset_n),
.ibex_0_ndm_ndmreset_o (ndmreset_req),
.ibex_0_ndm_ndmreset_ack_i (ndmreset_ack),
.ibex_0_config_boot_addr_i (32'h02050000),
.ibex_0_config_boot_addr_i (32'h02080000),
.ibex_0_config_core_sleep_o (),
.master_0_master_reset_reset (jtagm_reset_req),
.i2c_opencores_0_export_scl_pad_io (scl),
@ -424,9 +432,9 @@ sys sys_inst(
.pll_reconfig_0_pll_reconfig_if_scandone (pll_scandone)
);
// These do not work in current Quartus version (24.1) and a patch file (scripts/qsys.patch) must be used after Qsys generation instead
// These do not work in current Quartus version (23.1) and a patch file (scripts/qsys.patch) must be used after Qsys generation instead
defparam
sys_inst.master_0.fifo.FIFO_DEPTH = 1024;
sys_inst.master_0.fifo.USE_MEMORY_BLOCKS = 0;
scanconverter #(
.EMIF_ENABLE(0),

View File

@ -1,13 +0,0 @@
--- sys/synthesis/submodules/sys_master_0.v 2025-07-25 10:15:46.708247405 +0300
+++ sys/synthesis/submodules/sys_master_0.v 2025-07-25 10:15:54.937804924 +0300
@@ -156,9 +156,9 @@
altera_avalon_sc_fifo #(
.SYMBOLS_PER_BEAT (1),
.BITS_PER_SYMBOL (8),
- .FIFO_DEPTH (64),
+ .FIFO_DEPTH (1024),
.CHANNEL_WIDTH (0),
.ERROR_WIDTH (0),
.USE_PACKETS (0),
.USE_FILL_LEVEL (0),

View File

@ -1,6 +1,6 @@
# flash details
set flash_base 0x02000000
set flash_imem_offset 0x00050000
set flash_imem_offset 0x00080000
set flash_imem_base [format 0x%.8x [expr $flash_base + $flash_imem_offset]]
set flash_secsize 65536
@ -64,24 +64,21 @@ for {set i 0} {$i<$num_sectors} {incr i} {
}
puts "Writing flash"
# JTAG to Avalon master does not support sink backpressure
# writes garbage and occasionally hangs (bug in generic serial flash IF?)
#master_write_from_file $claim_path mem_init/flash.bin $flash_imem_base
# work around lack of backpressure support by writing chunks of master FIFO size
# work around the issue by writing into small chunks so that FIFO does not fill up
set chunks [llength [glob mem_init/chunks/*]]
puts "Programming $chunks chunks"
set addr $flash_imem_base
for {set i 0} {$i<$chunks} {incr i} {
set file [format "flash.%04d" $i]
master_write_from_file $claim_path mem_init/chunks/$file $addr
set addr [expr $addr + 1024]
set addr [expr $addr + 64]
}
#master_read_to_file $claim_path mem_init/flash_readback.bin $flash_imem_base $bin_size
#master_read_to_file $claim_path mem_init/ram_readback.bin 0x010000 65536
# flush flashctrl cmd fifo to ensure writes have finished
master_read_32 $claim_path $flash_base 1
puts "Resetting system"
master_write_32 $claim_path 0x40 0x00000003
after 1

View File

@ -208,7 +208,7 @@ APP_CFLAGS_UNDEFINED_SYMBOLS :=
APP_CFLAGS_OPTIMIZATION := -Os
APP_CFLAGS_DEBUG_LEVEL :=
APP_CFLAGS_WARNINGS := -Wall -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-function -Wno-packed-bitfield-compat
APP_CFLAGS_USER_FLAGS := -fdata-sections -ffunction-sections -fshort-enums -fgnu89-inline -flto -msmall-data-limit=0
APP_CFLAGS_USER_FLAGS := -fdata-sections -ffunction-sections -fshort-enums -fgnu89-inline -flto
APP_ASFLAGS_USER :=
APP_LDFLAGS_USER := -Wl,--gc-sections
@ -1150,11 +1150,11 @@ src/userdata_sjis.c: src/userdata.c
iconv -f UTF-8 -t SHIFT-JIS src/userdata.c > src/userdata_sjis.c
mem_init/flash.hex: sys_controller.elf
$(RV_OBJCOPY) --change-addresses -0x02050000 -O binary --gap-fill 0 $< mem_init/flash.bin
$(RV_OBJCOPY) --change-addresses -0x02050000 -O ihex --gap-fill 0 $< mem_init/flash.hex
$(RV_OBJCOPY) --change-addresses -0x02080000 -O binary --gap-fill 0 $< mem_init/flash.bin
$(RV_OBJCOPY) --change-addresses -0x02080000 -O ihex --gap-fill 0 $< mem_init/flash.hex
mkdir -p mem_init/chunks
rm -f mem_init/chunks/*
split -d -b 1024 -a 4 mem_init/flash.bin mem_init/chunks/flash.
split -d -b 64 -a 4 mem_init/flash.bin mem_init/chunks/flash.
.PHONY: mem_init_generate_new
mem_init_generate_new: mem_init/flash.hex

View File

@ -65,24 +65,18 @@ extern alt_u16 rc_keymap_default[REMOTE_MAX_KEYS];
extern alt_u32 remote_code;
extern alt_u32 btn_code, btn_code_prev;
extern alt_u8 remote_rpt, remote_rpt_prev;
extern avconfig_t tc;
extern avconfig_t tc, tc_default;
extern alt_u8 vm_sel;
extern char target_profile_name[USERDATA_NAME_LEN+1];
extern char target_profile_name[PROFILE_NAME_LEN+1];
tvp_input_t target_tvp;
tvp_sync_input_t target_tvp_sync;
alt_u8 target_type;
alt_u8 update_cur_vm;
// Default settings
const settings_t ts_default = {
.osd_enable = 1,
.osd_status_timeout = 1,
.osd_highlight_color = 4,
.auto_av2_ypbpr = 1,
};
alt_u8 profile_sel, profile_sel_menu, sd_profile_sel_menu, input_profiles[AV_LAST], lt_sel;
alt_u8 profile_sel, profile_sel_menu, input_profiles[AV_LAST], lt_sel, def_input, profile_link, lcd_bl_timeout;
alt_u8 osd_enable=1, osd_status_timeout=1;
alt_u8 auto_input, auto_av1_ypbpr, auto_av2_ypbpr = 1, auto_av3_ypbpr;
char row1[LCD_ROW_LEN+1], row2[LCD_ROW_LEN+1], menu_row1[LCD_ROW_LEN+1], menu_row2[LCD_ROW_LEN+1];
@ -103,8 +97,6 @@ alt_u32 read_it2(alt_u32 regaddr);
mode_data_t vmode_in, vmode_out;
vm_proc_config_t vm_conf;
settings_t cs, ts;
// Manually (see cyiv-51005.pdf) or automatically (MIF/HEX from PLL megafunction) generated config may not
// provide fully correct scan chain data (e.g. mismatches in C3) and lead to incorrect PLL configuration.
// To get correct scan chain data, do the following:
@ -148,9 +140,9 @@ int loaded_lc_palette = -1;
void ui_disp_menu(alt_u8 osd_mode)
{
uint8_t menu_page;
alt_u8 menu_page;
if ((osd_mode == 1) || (ts.osd_enable == 2)) {
if ((osd_mode == 1) || (osd_enable == 2)) {
strncpy((char*)osd->osd_array.data[0][0], menu_row1, OSD_CHAR_COLS);
strncpy((char*)osd->osd_array.data[1][0], menu_row2, OSD_CHAR_COLS);
osd->osd_row_color.mask = 0;
@ -771,7 +763,7 @@ int load_profile() {
target_input = tc.link_av;
// Update profile link (also prevents the change of input from inducing a profile load).
input_profiles[ts.profile_link ? target_input : AV_TESTPAT] = profile_sel;
input_profiles[profile_link ? target_input : AV_TESTPAT] = profile_sel;
write_userdata(INIT_CONFIG_SLOT);
}
@ -785,18 +777,13 @@ int save_profile() {
if (retval == 0) {
profile_sel = profile_sel_menu;
input_profiles[ts.profile_link ? cm.avinput : AV_TESTPAT] = profile_sel;
input_profiles[profile_link ? cm.avinput : AV_TESTPAT] = profile_sel;
write_userdata(INIT_CONFIG_SLOT);
}
return retval;
}
void set_default_settings() {
memcpy(&ts, &ts_default, sizeof(settings_t));
set_default_keymap();
}
void set_default_c_shmask() {
memset(&c_shmask, 0, sizeof(c_shmask));
strncpy(c_shmask.name, "Custom: <none>", 20);
@ -870,19 +857,17 @@ int init_hw()
}*/
// Set defaults
set_default_profile(1);
set_default_avconfig();
memcpy(&cm.cc, &tc_default, sizeof(avconfig_t));
set_default_c_shmask();
set_default_settings();
memcpy(rc_keymap, rc_keymap_default, sizeof(rc_keymap));
// Init menu
init_menu();
// Load initconfig and profile
read_userdata(INIT_CONFIG_SLOT, 0);
profile_sel = input_profiles[AV_TESTPAT]; // Global profile
read_userdata(profile_sel, 0);
update_settings(1);
//read_userdata(INIT_CONFIG_SLOT, 0);
//read_userdata(profile_sel, 0);
// Setup test pattern
get_vmode(VMODE_480p, &vmode_in, &vmode_out, &vm_conf);
@ -992,19 +977,6 @@ int latency_test() {
return 0;
}
void update_settings(int init_setup) {
if (init_setup || (ts.osd_enable != cs.osd_enable) || (ts.osd_status_timeout != cs.osd_status_timeout) || (ts.osd_highlight_color != cs.osd_highlight_color)) {
osd->osd_config.enable = !!ts.osd_enable;
osd->osd_config.status_timeout = ts.osd_status_timeout;
osd->osd_config.highlight_color = 2+ts.osd_highlight_color;
refresh_osd();
}
if (init_setup)
target_input = ts.def_input;
memcpy(&cs, &ts, sizeof(settings_t));
}
int main()
{
ths_input_t target_ths = 0;
@ -1072,19 +1044,19 @@ int main()
}
// Auto input switching
if ((cs.auto_input != AUTO_OFF) && (cm.avinput != AV_TESTPAT) && !cm.sync_active && !menu_active
if ((auto_input != AUTO_OFF) && (cm.avinput != AV_TESTPAT) && !cm.sync_active && !menu_active
&& (alt_timestamp() >= auto_input_timestamp + 300 * (alt_timestamp_freq() >> 10)) && (auto_input_ctr < AUTO_MAX_COUNT)) {
// Keep switching on the same physical input when set to Current input or a short time after losing sync.
auto_input_keep_current = (cs.auto_input == AUTO_CURRENT_INPUT || auto_input_current_ctr < AUTO_CURRENT_MAX_COUNT);
auto_input_keep_current = (auto_input == AUTO_CURRENT_INPUT || auto_input_current_ctr < AUTO_CURRENT_MAX_COUNT);
switch(cm.avinput) {
case AV1_RGBs:
target_input = cs.auto_av1_ypbpr ? AV1_YPBPR : AV1_RGsB;
target_input = auto_av1_ypbpr ? AV1_YPBPR : AV1_RGsB;
break;
case AV1_RGsB:
case AV1_YPBPR:
target_input = auto_input_keep_current ? AV1_RGBs : (cs.auto_av2_ypbpr ? AV2_YPBPR : AV2_RGsB);
target_input = auto_input_keep_current ? AV1_RGBs : (auto_av2_ypbpr ? AV2_YPBPR : AV2_RGsB);
break;
case AV2_YPBPR:
case AV2_RGsB:
@ -1094,7 +1066,7 @@ int main()
target_input = AV3_RGBs;
break;
case AV3_RGBs:
target_input = cs.auto_av3_ypbpr ? AV3_YPBPR : AV3_RGsB;
target_input = auto_av3_ypbpr ? AV3_YPBPR : AV3_RGsB;
break;
case AV3_RGsB:
case AV3_YPBPR:
@ -1125,7 +1097,7 @@ int main()
if ((target_input != cm.avinput && man_input_change) || (auto_input_changed && cm.sync_active)) {
// The input changed, so load the appropriate profile if
// input->profile link is enabled
if (cs.profile_link && (profile_sel != input_profiles[target_input])) {
if (profile_link && (profile_sel != input_profiles[target_input])) {
profile_sel = input_profiles[target_input];
read_userdata(profile_sel, 0);
}
@ -1210,7 +1182,7 @@ int main()
ui_disp_status(1);
if (man_input_change) {
// record last input if it was selected manually
if (cs.def_input == AV_LAST)
if (def_input == AV_LAST)
write_userdata(INIT_CONFIG_SLOT);
// Set auto_input_timestamp when input is manually changed
auto_input_ctr = 0;
@ -1250,8 +1222,15 @@ int main()
printf("Changing AV3 RGB source\n");
cm.cc.av3_alt_rgb = tc.av3_alt_rgb;
}
update_settings(0);
if ((!!osd_enable != osd->osd_config.enable) || (osd_status_timeout != osd->osd_config.status_timeout)) {
osd->osd_config.enable = !!osd_enable;
osd->osd_config.status_timeout = osd_status_timeout;
if (menu_active) {
remote_code = 0;
render_osd_page();
display_menu(1);
}
}
if (cm.avinput != AV_TESTPAT) {
status = get_status(target_tvp_sync);

View File

@ -89,20 +89,6 @@ typedef struct {
avconfig_t cc;
} avmode_t;
typedef struct {
uint8_t profile_link;
avinput_t def_input;
uint8_t auto_input;
uint8_t auto_av1_ypbpr;
uint8_t auto_av2_ypbpr;
uint8_t auto_av3_ypbpr;
uint8_t lcd_bl_timeout;
uint8_t osd_enable;
uint8_t osd_status_timeout;
uint8_t osd_highlight_color;
uint8_t phase_hotkey_enable;
} settings_t;
typedef struct {
uint8_t iv_x;
uint8_t iv_y;
@ -131,9 +117,10 @@ void ui_disp_status(alt_u8 refresh_osd_timer);
void set_sampler_phase(uint8_t sampler_phase, uint8_t update_sc);
int load_profile();
int save_profile();
void print_vm_stats();
int latency_test();
void update_settings(int init_setup);
#endif

View File

@ -157,11 +157,6 @@ typedef struct {
avinput_t link_av;
} __attribute__((packed)) avconfig_t;
int set_default_profile(int update_cc);
int reset_profile();
int load_profile();
int save_profile();
int load_profile_sd();
int save_profile_sd();
int set_default_avconfig();
#endif

View File

@ -60,7 +60,6 @@ typedef enum {
#define REMOTE_MAX_KEYS (RC_PROF_HOTKEY-RC_BTN1+1)
void setup_rc();
void set_default_keymap();
int parse_control();
#endif

View File

@ -21,17 +21,26 @@
#define FIRMWARE_H_
#include <stdint.h>
#include "alt_types.h"
#include "sysconfig.h"
#define FW_VER_MAJOR 1
#define FW_VER_MINOR 20
#define PROFILE_VER_MAJOR 1
#define PROFILE_VER_MINOR 12
#define INITCFG_VER_MAJOR 1
#define INITCFG_VER_MINOR 0
#ifdef OSDLANG_JP
#define FW_SUFFIX "j"
#else
#define FW_SUFFIX ""
#endif
#define FW_UPDATE_RETRIES 3
typedef struct {
char fw_key[4];
uint8_t version_major;

View File

@ -128,7 +128,6 @@ void render_osd_page();
void display_menu(alt_u8 forcedisp);
void sampler_phase_disp(alt_u8 v);
void update_osd_size(mode_data_t *vm_out);
void refresh_osd();
static void vm_select();
static void vm_tweak(alt_u16 *v);

View File

@ -1,5 +1,5 @@
//
// Copyright (C) 2015-2025 Markus Hiienkari <mhiienka@niksula.hut.fi>
// Copyright (C) 2015-2023 Markus Hiienkari <mhiienka@niksula.hut.fi>
//
// This file is part of Open Source Scan Converter project.
//
@ -20,17 +20,29 @@
#ifndef USERDATA_H_
#define USERDATA_H_
#include <stdint.h>
#include "alt_types.h"
#include "sysconfig.h"
#include "controls.h"
#include "av_controller.h"
#include "avconfig.h"
#include "video_modes.h"
#include "flash.h"
#define USERDATA_NAME_LEN 13
#define MAX_USERDATA_ENTRY 15
#define MAX_SD_USERDATA_ENTRY 100
#define PROFILE_NAME_LEN 12
// EPCS16 pagesize is 256 bytes
// Flash is split 50-50 to FW and userdata, 1MB each
#define PAGESIZE 256
#define PAGES_PER_SECTOR 256 //EPCS "sector" corresponds to "block" on Spansion flash
#define SECTORSIZE (PAGESIZE*PAGES_PER_SECTOR)
#define USERDATA_OFFSET 0x100000
#define MAX_USERDATA_ENTRY 15 // 16 sectors for userdata
#define MAX_PROFILE (MAX_USERDATA_ENTRY-1)
#define MAX_SD_PROFILE (MAX_SD_USERDATA_ENTRY-1)
#define INIT_CONFIG_SLOT MAX_USERDATA_ENTRY
#define SD_INIT_CONFIG_SLOT MAX_SD_USERDATA_ENTRY
#define UDATA_IMPT_CANCELLED 104
#define UDATA_EXPT_CANCELLED 105
typedef enum {
UDE_INITCFG = 0,
@ -39,26 +51,40 @@ typedef enum {
typedef struct {
char userdata_key[8];
char name[USERDATA_NAME_LEN+1];
alt_u8 version_major;
alt_u8 version_minor;
ude_type type;
uint8_t num_items;
} __attribute__((packed, __may_alias__)) ude_hdr;
typedef struct {
uint16_t id;
uint16_t version;
uint16_t data_size;
} __attribute__((packed, __may_alias__)) ude_item_hdr;
ude_hdr hdr;
alt_u16 data_len;
alt_u8 last_profile[AV_LAST];
alt_u8 profile_link;
avinput_t last_input;
avinput_t def_input;
alt_u8 lcd_bl_timeout;
alt_u8 auto_input;
alt_u8 auto_av1_ypbpr;
alt_u8 auto_av2_ypbpr;
alt_u8 auto_av3_ypbpr;
alt_u8 osd_enable;
alt_u8 osd_status_timeout;
alt_u8 phase_hotkey_enable;
alt_u16 keys[REMOTE_MAX_KEYS];
} __attribute__((packed, __may_alias__)) ude_initcfg;
typedef struct {
ude_item_hdr hdr;
void *data;
} ude_item_map;
ude_hdr hdr;
char name[PROFILE_NAME_LEN+1];
alt_u16 avc_data_len;
alt_u16 vm_data_len;
avconfig_t avc;
//mode_data_t vm[VIDEO_MODES_CNT];
} __attribute__((packed, __may_alias__)) ude_profile;
int write_userdata(uint8_t entry);
int read_userdata(uint8_t entry, int dry_run);
int write_userdata_sd(uint8_t entry);
int read_userdata_sd(uint8_t entry, int dry_run);
int write_userdata(alt_u8 entry);
int read_userdata(alt_u8 entry, int dry_run);
int import_userdata();
int export_userdata();

View File

@ -1,14 +1,14 @@
SEARCH_DIR(.)
__DYNAMIC = 0;
/* First 16 flash sectors reserved for 2 firmware images (2x 0.5MB).
/* First 16 flash sectors reserved for firmware image (1MB).
In typical configuration a firmware image consists of
* compressed bitstream (5 sectors / 0.3MB)
* flash_imem (3 sectors / 0.2MB)
* compressed bitstream (8 sectors / 0.5MB)
* flash_imem (8 sectors / 0.5MB)
Last 16 flash sectors reserved for userdata (16x 64KB). */
MEMORY
{
flash_imem : ORIGIN = 0x02050000, LENGTH = 196608
flash_imem : ORIGIN = 0x02080000, LENGTH = 524288
dataram : ORIGIN = 0x00010000, LENGTH = 16384
}

View File

@ -21,15 +21,12 @@
#include "system.h"
#include "avconfig.h"
#include "av_controller.h"
#include "userdata.h"
#include "altera_avalon_pio_regs.h"
#include "tvp7002.h"
#define DEFAULT_ON 1
extern avmode_t cm;
extern alt_u8 update_cur_vm;
extern uint8_t sd_profile_sel_menu;
// Target configuration
avconfig_t tc;
@ -68,37 +65,14 @@ const avconfig_t tc_default = {
.link_av = AV_LAST,
};
int set_default_profile(int update_cc)
int set_default_avconfig()
{
memcpy(&tc, &tc_default, sizeof(avconfig_t));
tc.tx_mode = (IORD_ALTERA_AVALON_PIO_DATA(PIO_1_BASE) & HDMITX_MODE_MASK) ? TX_DVI : TX_HDMI_RGB;
if (update_cc)
memcpy(&cm.cc, &tc, sizeof(avconfig_t));
set_default_vm_table();
update_cur_vm = 1;
return 0;
}
int reset_profile() {
set_default_profile(0);
return 0;
}
int load_profile_sd() {
return read_userdata_sd(sd_profile_sel_menu, 0);
}
int save_profile_sd() {
int retval;
retval = write_userdata_sd(sd_profile_sel_menu);
if (retval == 0)
write_userdata_sd(SD_INIT_CONFIG_SLOT);
return retval;
}

View File

@ -43,12 +43,12 @@ extern char menu_row1[LCD_ROW_LEN+1], menu_row2[LCD_ROW_LEN+1];
extern mode_data_t video_modes_plm[];
extern avmode_t cm;
extern avconfig_t tc;
extern settings_t ts;
extern avinput_t target_input;
extern alt_u8 menu_active;
extern alt_u32 sys_ctrl;
extern alt_u16 tc_sampler_phase;
extern alt_u8 profile_sel, profile_sel_menu;
extern alt_u8 lcd_bl_timeout;
extern alt_u8 vm_edit;
extern volatile osd_regs *osd;
@ -94,7 +94,7 @@ void setup_rc()
if ((btn_code_prev == 0) && (btn_code == PB0_BIT)) {
if (i == 0) {
set_default_keymap();
memcpy(rc_keymap, rc_keymap_default, sizeof(rc_keymap));
i=REMOTE_MAX_KEYS;
} else {
i-=2;
@ -116,10 +116,6 @@ void setup_rc()
osd->osd_config.menu_active = 0;
}
void set_default_keymap() {
memcpy(rc_keymap, rc_keymap_default, sizeof(rc_keymap));
}
int parse_control()
{
int i, prof_x10=0, ret=0, retval;
@ -345,7 +341,7 @@ Button_Check:
sys_ctrl &= ~(3<<LCD_BL_TIMEOUT_OFFS);
if (!menu_active)
sys_ctrl |= (ts.lcd_bl_timeout << LCD_BL_TIMEOUT_OFFS);
sys_ctrl |= (lcd_bl_timeout << LCD_BL_TIMEOUT_OFFS);
IOWR_ALTERA_AVALON_PIO_DATA(PIO_0_BASE, sys_ctrl);

View File

@ -54,12 +54,10 @@ int fw_update() {
uint8_t databuf[SD_BLK_SIZE]; // temp buffer for data
uint16_t fs_csize, fs_startsec, cl_iter, cl_soffs;
if (!sdcard_dev.mount) {
retval = file_mount();
if (retval != 0) {
printf("SD card not detected %d\n", retval);
return -1;
}
retval = file_mount();
if (retval != 0) {
printf("SD card not detected %d\n", retval);
return -1;
}
//sniprintf(dirname_root, sizeof(dirname_root), "/%s", dirname);

View File

@ -1,5 +1,5 @@
//
// Copyright (C) 2015-2025 Markus Hiienkari <mhiienka@niksula.hut.fi>
// Copyright (C) 2015-2016 Markus Hiienkari <mhiienka@niksula.hut.fi>
//
// This file is part of Open Source Scan Converter project.
//
@ -20,16 +20,7 @@
#include <unistd.h>
#include "flash.h"
#define DEMUX_FINISH_DELAY 10000
void __attribute__((noinline, flatten, __section__(".text_bram"))) flash_write_protect(flash_ctrl_dev *dev, int enable) {
int i;
// add short delay to avoid mem/csr demux conflict (freeze) in intel_generic_serial_flash_interface
for(i = 0; i < DEMUX_FINISH_DELAY; i++){
asm volatile ("nop");
}
// Write enable
dev->regs->flash_cmd_cfg = 0x00000006;
dev->regs->flash_cmd_ctrl = 1;
@ -50,21 +41,9 @@ void __attribute__((noinline, flatten, __section__(".text_bram"))) flash_write_p
// Write disable
dev->regs->flash_cmd_cfg = 0x00000004;
dev->regs->flash_cmd_ctrl = 1;
// add short delay to avoid mem/csr demux conflict (freeze) in intel_generic_serial_flash_interface
for(i = 0; i < DEMUX_FINISH_DELAY; i++){
asm volatile ("nop");
}
}
void __attribute__((noinline, flatten, __section__(".text_bram"))) flash_sector_erase(flash_ctrl_dev *dev, uint32_t addr) {
int i;
// add short delay to avoid mem/csr demux conflict (freeze) in intel_generic_serial_flash_interface
for(i = 0; i < DEMUX_FINISH_DELAY; i++){
asm volatile ("nop");
}
// Write enable
dev->regs->flash_cmd_cfg = 0x00000006;
dev->regs->flash_cmd_ctrl = 1;
@ -81,9 +60,4 @@ void __attribute__((noinline, flatten, __section__(".text_bram"))) flash_sector_
if (!(dev->regs->flash_cmd_rddata[0] & (1<<0)))
break;
}
// add short delay to avoid mem/csr demux conflict (freeze) in intel_generic_serial_flash_interface
for(i = 0; i < DEMUX_FINISH_DELAY; i++){
asm volatile ("nop");
}
}

View File

@ -33,14 +33,15 @@
extern char row1[LCD_ROW_LEN+1], row2[LCD_ROW_LEN+1], menu_row1[LCD_ROW_LEN+1], menu_row2[LCD_ROW_LEN+1];
extern avmode_t cm;
extern avconfig_t tc;
extern settings_t ts;
extern mode_data_t video_modes_plm[];
extern alt_u32 remote_code;
extern alt_u16 rc_keymap[REMOTE_MAX_KEYS];
extern alt_u8 vm_sel, profile_sel_menu, sd_profile_sel_menu, lt_sel;
extern alt_u8 vm_sel, profile_sel_menu, lt_sel, def_input, profile_link, lcd_bl_timeout;
extern alt_u8 auto_input, auto_av1_ypbpr, auto_av2_ypbpr, auto_av3_ypbpr;
extern alt_u8 update_cur_vm;
extern alt_u8 osd_enable, osd_status_timeout, phase_hotkey_enable;
extern uint8_t sl_def_iv_x, sl_def_iv_y;
extern char target_profile_name[USERDATA_NAME_LEN+1];
extern char target_profile_name[PROFILE_NAME_LEN+1];
extern volatile osd_regs *osd;
extern const int num_video_modes_plm;
extern c_shmask_t c_shmask;
@ -76,7 +77,6 @@ static const char* const lt_desc[] = { "Top-left", "Center", "Bottom-right" };
static const char* const lcd_bl_timeout_desc[] = { "Off", "3s", "10s", "30s" };
static const char* const osd_enable_desc[] = { "Off", "Full", "Simple" };
static const char* const osd_status_desc[] = { "2s", "5s", "10s", "Off" };
static const char* const osd_color_desc[] = { "Green", "Cyan", "Red", "Magenta", "Yellow" };
static const char* const rgsb_ypbpr_desc[] = { "RGsB", "YPbPr" };
static const char* const auto_input_desc[] = { "Off", "Current input", "All inputs" };
static const char* const mask_color_desc[] = { "Black", "Blue", "Green", "Cyan", "Red", "Magenta", "Yellow", "White" };
@ -103,8 +103,7 @@ static void lt_disp(alt_u8 v) { strncpy(menu_row2, lt_desc[v], LCD_ROW_LEN+1); }
static void aud_db_disp(alt_u8 v) { sniprintf(menu_row2, LCD_ROW_LEN+1, "%d dB", ((alt_8)v-AUDIO_GAIN_0DB)); }
static void vm_display_name (alt_u8 v) { strncpy(menu_row2, video_modes_plm[v].name, LCD_ROW_LEN+1); }
static void link_av_desc (avinput_t v) { strncpy(menu_row2, v == AV_LAST ? "No link" : avinput_str[v], LCD_ROW_LEN+1); }
static void profile_disp(uint8_t v) { sniprintf(menu_row2, LCD_ROW_LEN+1, "%u: %s", v, (read_userdata(v, 1) != 0) ? "<empty>" : target_profile_name); }
static void sd_profile_disp(uint8_t v) { sniprintf(menu_row2, LCD_ROW_LEN+1, "%u: %s", v, (read_userdata_sd(v, 1) != 0) ? "<empty>" : target_profile_name); }
static void profile_disp(alt_u8 v) { read_userdata(v, 1); sniprintf(menu_row2, LCD_ROW_LEN+1, "%u: %s", v, (target_profile_name[0] == 0) ? "<empty>" : target_profile_name); }
static void alc_v_filter_disp(alt_u8 v) { sniprintf(menu_row2, LCD_ROW_LEN+1, LNG("%u lines","%u ライン"), (1<<v)); }
static void alc_h_filter_disp(alt_u8 v) { sniprintf(menu_row2, LCD_ROW_LEN+1, LNG("%u pixels","%u ドット"), (1<<(v+1))); }
void sampler_phase_disp(alt_u8 v) { sniprintf(menu_row2, LCD_ROW_LEN+1, "%d deg", (v*11250)/1000); }
@ -112,7 +111,6 @@ void sampler_phase_disp(alt_u8 v) { sniprintf(menu_row2, LCD_ROW_LEN+1, "%d deg"
static arg_info_t vm_arg_info = {&vm_sel, 0, vm_display_name};
static const arg_info_t profile_arg_info = {&profile_sel_menu, MAX_PROFILE, profile_disp};
static const arg_info_t sd_profile_arg_info = {&sd_profile_sel_menu, MAX_SD_PROFILE, sd_profile_disp};
static const arg_info_t lt_arg_info = {&lt_sel, (sizeof(lt_desc)/sizeof(char*))-1, lt_disp};
@ -243,24 +241,21 @@ MENU(menu_audio, P99_PROTECT({ \
MENU(menu_settings, P99_PROTECT({ \
{ LNG("Link prof->input","Link prof->input"), OPT_AVCONFIG_NUMVALUE, { .num = { &tc.link_av, OPT_WRAP, AV1_RGBs, AV_LAST, link_av_desc } } },
{ LNG("Link input->prof","Link input->prof"), OPT_AVCONFIG_SELECTION, { .sel = { &ts.profile_link, OPT_WRAP, SETTING_ITEM(off_on_desc) } } },
{ LNG("Initial input","ショキニュウリョク"), OPT_AVCONFIG_SELECTION, { .sel = { &ts.def_input, OPT_WRAP, SETTING_ITEM(avinput_str) } } },
{ "Autodetect input", OPT_AVCONFIG_SELECTION, { .sel = { &ts.auto_input, OPT_WRAP, SETTING_ITEM(auto_input_desc) } } },
{ "Auto AV1 Y/Gs", OPT_AVCONFIG_SELECTION, { .sel = { &ts.auto_av1_ypbpr, OPT_WRAP, SETTING_ITEM(rgsb_ypbpr_desc) } } },
{ "Auto AV2 Y/Gs", OPT_AVCONFIG_SELECTION, { .sel = { &ts.auto_av2_ypbpr, OPT_WRAP, SETTING_ITEM(rgsb_ypbpr_desc) } } },
{ "Auto AV3 Y/Gs", OPT_AVCONFIG_SELECTION, { .sel = { &ts.auto_av3_ypbpr, OPT_WRAP, SETTING_ITEM(rgsb_ypbpr_desc) } } },
{ "LCD BL timeout", OPT_AVCONFIG_SELECTION, { .sel = { &ts.lcd_bl_timeout, OPT_WRAP, SETTING_ITEM(lcd_bl_timeout_desc) } } },
{ "OSD", OPT_AVCONFIG_SELECTION, { .sel = { &ts.osd_enable, OPT_WRAP, SETTING_ITEM(osd_enable_desc) } } },
{ "OSD status disp.", OPT_AVCONFIG_SELECTION, { .sel = { &ts.osd_status_timeout, OPT_WRAP, SETTING_ITEM(osd_status_desc) } } },
{ "OSD cursor color", OPT_AVCONFIG_SELECTION, { .sel = { &ts.osd_highlight_color, OPT_WRAP, SETTING_ITEM(osd_color_desc) } } },
{ "Phase hotkey", OPT_AVCONFIG_SELECTION, { .sel = { &ts.phase_hotkey_enable, OPT_WRAP, SETTING_ITEM(off_on_desc) } } },
{ LNG("Link input->prof","Link input->prof"), OPT_AVCONFIG_SELECTION, { .sel = { &profile_link, OPT_WRAP, SETTING_ITEM(off_on_desc) } } },
{ LNG("Initial input","ショキニュウリョク"), OPT_AVCONFIG_SELECTION, { .sel = { &def_input, OPT_WRAP, SETTING_ITEM(avinput_str) } } },
{ "Autodetect input", OPT_AVCONFIG_SELECTION, { .sel = { &auto_input, OPT_WRAP, SETTING_ITEM(auto_input_desc) } } },
{ "Auto AV1 Y/Gs", OPT_AVCONFIG_SELECTION, { .sel = { &auto_av1_ypbpr, OPT_WRAP, SETTING_ITEM(rgsb_ypbpr_desc) } } },
{ "Auto AV2 Y/Gs", OPT_AVCONFIG_SELECTION, { .sel = { &auto_av2_ypbpr, OPT_WRAP, SETTING_ITEM(rgsb_ypbpr_desc) } } },
{ "Auto AV3 Y/Gs", OPT_AVCONFIG_SELECTION, { .sel = { &auto_av3_ypbpr, OPT_WRAP, SETTING_ITEM(rgsb_ypbpr_desc) } } },
{ "LCD BL timeout", OPT_AVCONFIG_SELECTION, { .sel = { &lcd_bl_timeout, OPT_WRAP, SETTING_ITEM(lcd_bl_timeout_desc) } } },
{ "OSD", OPT_AVCONFIG_SELECTION, { .sel = { &osd_enable, OPT_WRAP, SETTING_ITEM(osd_enable_desc) } } },
{ "OSD status disp.", OPT_AVCONFIG_SELECTION, { .sel = { &osd_status_timeout, OPT_WRAP, SETTING_ITEM(osd_status_desc) } } },
{ "Phase hotkey", OPT_AVCONFIG_SELECTION, { .sel = { &phase_hotkey_enable, OPT_WRAP, SETTING_ITEM(off_on_desc) } } },
{ LNG("<Load profile >","<プロファイルロード >"), OPT_FUNC_CALL, { .fun = { load_profile, &profile_arg_info } } },
{ LNG("<Save profile >","<プロファイルセーブ >"), OPT_FUNC_CALL, { .fun = { save_profile, &profile_arg_info } } },
{ "<SD Load profile>" , OPT_FUNC_CALL, { .fun = { load_profile_sd, &sd_profile_arg_info } } },
{ "<SD Save profile>" , OPT_FUNC_CALL, { .fun = { save_profile_sd, &sd_profile_arg_info } } },
{ LNG("<Reset profile>","<セッテイヲショキカ >"), OPT_FUNC_CALL, { .fun = { reset_profile, NULL } } },
//{ LNG("<Import sett. >","<セッテイヨミコミ >"), OPT_FUNC_CALL, { .fun = { import_userdata, NULL } } },
//{ LNG("<Export sett. >","<セッテイカキコミ >"), OPT_FUNC_CALL, { .fun = { export_userdata, NULL } } },
{ LNG("<Reset settings>","<セッテイヲショキカ >"), OPT_FUNC_CALL, { .fun = { set_default_avconfig, NULL } } },
{ LNG("<Import sett. >","<セッテイヨミコミ >"), OPT_FUNC_CALL, { .fun = { import_userdata, NULL } } },
{ LNG("<Export sett. >","<セッテイカキコミ >"), OPT_FUNC_CALL, { .fun = { export_userdata, NULL } } },
{ LNG("<Fw. update >","<ファームウェアアップデート>"), OPT_FUNC_CALL, { .fun = { fw_update, NULL } } },
}))
@ -341,7 +336,7 @@ void render_osd_page() {
const menuitem_t *item;
uint32_t row_mask[2] = {0, 0};
if (!menu_active || (ts.osd_enable != 1))
if (!menu_active || (osd_enable != 1))
return;
for (i=0; i < navi[navlvl].m->num_items; i++) {
@ -505,14 +500,6 @@ void update_osd_size(mode_data_t *vm_out) {
osd->osd_config.y_size = osd_size;
}
void refresh_osd() {
if (menu_active) {
remote_code = 0;
render_osd_page();
display_menu(1);
}
}
static void vm_select() {
vm_edit = vm_sel;
tc_h_samplerate = video_modes_plm[vm_edit].timings.h_total;

View File

@ -23,6 +23,7 @@
#include <stdio.h>
#include "system.h"
#include "userdata.h"
#include "fat16_export.h"
#include "flash.h"
#include "sdcard.h"
#include "firmware.h"
@ -30,417 +31,508 @@
#include "controls.h"
#include "av_controller.h"
#include "menu.h"
#include "ff.h"
#include "file.h"
#define UDE_ITEM(ID, VER, ITEM) {{ID, VER, sizeof(ITEM)}, &ITEM}
#include "utils.h"
#include "altera_avalon_pio_regs.h"
// include mode array definitions so that sizeof() can be used
#define VM_STATIC_INCLUDE
#include "video_modes_list.c"
#undef VM_STATIC_INCLUDE
extern flash_ctrl_dev flashctrl_dev;
extern uint16_t rc_keymap[REMOTE_MAX_KEYS];
extern uint8_t input_profiles[AV_LAST];
extern alt_u16 rc_keymap[REMOTE_MAX_KEYS];
extern avmode_t cm;
extern avconfig_t tc;
extern settings_t ts;
extern mode_data_t video_modes_plm[];
extern uint8_t update_cur_vm;
extern avinput_t target_input;
extern alt_u8 update_cur_vm;
extern alt_u8 input_profiles[AV_LAST];
extern alt_u8 profile_sel;
extern alt_u8 def_input, profile_link;
extern alt_u8 lcd_bl_timeout;
extern alt_u8 auto_input, auto_av1_ypbpr, auto_av2_ypbpr, auto_av3_ypbpr;
extern alt_u8 osd_enable, osd_status_timeout, phase_hotkey_enable;
extern SD_DEV sdcard_dev;
extern c_shmask_t c_shmask;
extern char menu_row1[LCD_ROW_LEN+1], menu_row2[LCD_ROW_LEN+1];
char target_profile_name[USERDATA_NAME_LEN+1], cur_profile_name[USERDATA_NAME_LEN+1];
char target_profile_name[PROFILE_NAME_LEN+1];
const ude_item_map ude_initcfg_items[] = {
UDE_ITEM(0, 120, rc_keymap),
UDE_ITEM(1, 120, input_profiles),
UDE_ITEM(2, 120, ts.profile_link),
UDE_ITEM(3, 120, ts.def_input),
UDE_ITEM(4, 120, ts.auto_input),
UDE_ITEM(5, 120, ts.auto_av1_ypbpr),
UDE_ITEM(6, 120, ts.auto_av2_ypbpr),
UDE_ITEM(7, 120, ts.auto_av3_ypbpr),
UDE_ITEM(8, 120, ts.lcd_bl_timeout),
UDE_ITEM(9, 120, ts.osd_enable),
UDE_ITEM(10, 120, ts.osd_status_timeout),
UDE_ITEM(11, 120, ts.osd_highlight_color),
UDE_ITEM(12, 120, ts.phase_hotkey_enable),
};
const ude_item_map ude_profile_items[] = {
{{0, 120, sizeof(video_modes_plm_default)}, video_modes_plm},
UDE_ITEM(1, 120, c_shmask),
// avconfig_t
UDE_ITEM(2, 120, tc.pm_240p),
UDE_ITEM(3, 120, tc.pm_384p),
UDE_ITEM(4, 120, tc.pm_480i),
UDE_ITEM(5, 120, tc.pm_480p),
UDE_ITEM(6, 120, tc.pm_1080i),
UDE_ITEM(7, 120, tc.pt_mode),
UDE_ITEM(8, 120, tc.l2_mode),
UDE_ITEM(9, 120, tc.l3_mode),
UDE_ITEM(10, 120, tc.l4_mode),
UDE_ITEM(11, 120, tc.l5_mode),
UDE_ITEM(12, 120, tc.l6_mode),
UDE_ITEM(13, 120, tc.l5_fmt),
UDE_ITEM(14, 120, tc.s480p_mode),
UDE_ITEM(15, 120, tc.s400p_mode),
UDE_ITEM(16, 120, tc.upsample2x),
UDE_ITEM(17, 120, tc.ar_256col),
UDE_ITEM(18, 120, tc.default_vic),
UDE_ITEM(19, 120, tc.clamp_offset),
UDE_ITEM(20, 120, tc.tvp_hpll2x),
UDE_ITEM(21, 120, tc.adc_pll_bw),
UDE_ITEM(22, 120, tc.fpga_pll_bw),
UDE_ITEM(23, 120, tc.sl_mode),
UDE_ITEM(24, 120, tc.sl_type),
UDE_ITEM(25, 120, tc.sl_hybr_str),
UDE_ITEM(26, 120, tc.sl_method),
UDE_ITEM(27, 120, tc.sl_altern),
UDE_ITEM(28, 120, tc.sl_str),
UDE_ITEM(29, 120, tc.sl_id),
UDE_ITEM(30, 120, tc.sl_cust_l_str),
UDE_ITEM(31, 120, tc.sl_cust_c_str),
UDE_ITEM(32, 120, tc.sl_cust_iv_x),
UDE_ITEM(33, 120, tc.sl_cust_iv_y),
UDE_ITEM(34, 120, tc.mask_br),
UDE_ITEM(35, 120, tc.mask_color),
UDE_ITEM(36, 120, tc.reverse_lpf),
UDE_ITEM(37, 120, tc.shmask_mode),
UDE_ITEM(38, 120, tc.shmask_str),
UDE_ITEM(39, 120, tc.lumacode_mode),
UDE_ITEM(40, 120, tc.lumacode_pal),
UDE_ITEM(41, 120, tc.sync_vth),
UDE_ITEM(42, 120, tc.linelen_tol),
UDE_ITEM(43, 120, tc.vsync_thold),
UDE_ITEM(44, 120, tc.pre_coast),
UDE_ITEM(45, 120, tc.post_coast),
UDE_ITEM(46, 120, tc.ypbpr_cs),
UDE_ITEM(47, 120, tc.video_lpf),
UDE_ITEM(48, 120, tc.sync_lpf),
UDE_ITEM(49, 120, tc.stc_lpf),
UDE_ITEM(50, 120, tc.alc_h_filter),
UDE_ITEM(51, 120, tc.alc_v_filter),
UDE_ITEM(52, 120, tc.col),
UDE_ITEM(53, 120, tc.full_vs_bypass),
UDE_ITEM(54, 120, tc.audio_dw_sampl),
UDE_ITEM(55, 120, tc.audio_swap_lr),
UDE_ITEM(56, 120, tc.audio_gain),
UDE_ITEM(57, 120, tc.audio_mono),
UDE_ITEM(58, 120, tc.tx_mode),
UDE_ITEM(59, 120, tc.hdmi_itc),
UDE_ITEM(60, 120, tc.hdmi_hdr),
UDE_ITEM(61, 120, tc.hdmi_vrr),
UDE_ITEM(62, 120, tc.full_tx_setup),
UDE_ITEM(63, 120, tc.av3_alt_rgb),
UDE_ITEM(64, 120, tc.link_av),
};
int write_userdata(uint8_t entry) {
ude_hdr hdr;
FIL name_file;
char p_filename[14];
const ude_item_map *target_map;
uint32_t flash_addr, bytes_written;
int i=0;
int write_userdata(alt_u8 entry)
{
alt_u8 databuf[PAGESIZE];
alt_u16 vm_to_write;
alt_u16 pageoffset, srcoffset;
alt_u8 pageno;
alt_u32 bytes_to_w;
int retval, i;
if (entry > MAX_USERDATA_ENTRY) {
printf("invalid entry\n");
return -1;
}
memset(&hdr, 0x00, sizeof(ude_hdr));
strlcpy(hdr.userdata_key, "USRDATA", 8);
hdr.type = (entry > MAX_PROFILE) ? UDE_INITCFG : UDE_PROFILE;
strncpy(((ude_hdr*)databuf)->userdata_key, "USRDATA", 8);
((ude_hdr*)databuf)->type = (entry > MAX_PROFILE) ? UDE_INITCFG : UDE_PROFILE;
if (hdr.type == UDE_INITCFG) {
target_map = ude_initcfg_items;
hdr.num_items = sizeof(ude_initcfg_items)/sizeof(ude_item_map);
switch (((ude_hdr*)databuf)->type) {
case UDE_INITCFG:
((ude_hdr*)databuf)->version_major = INITCFG_VER_MAJOR;
((ude_hdr*)databuf)->version_minor = INITCFG_VER_MINOR;
((ude_initcfg*)databuf)->data_len = sizeof(ude_initcfg) - offsetof(ude_initcfg, last_profile);
memcpy(((ude_initcfg*)databuf)->last_profile, input_profiles, sizeof(input_profiles));
((ude_initcfg*)databuf)->last_input = target_input;
((ude_initcfg*)databuf)->def_input = def_input;
((ude_initcfg*)databuf)->profile_link = profile_link;
((ude_initcfg*)databuf)->lcd_bl_timeout = lcd_bl_timeout;
((ude_initcfg*)databuf)->auto_input = auto_input;
((ude_initcfg*)databuf)->auto_av1_ypbpr = auto_av1_ypbpr;
((ude_initcfg*)databuf)->auto_av2_ypbpr = auto_av2_ypbpr;
((ude_initcfg*)databuf)->auto_av3_ypbpr = auto_av3_ypbpr;
((ude_initcfg*)databuf)->osd_enable = osd_enable;
((ude_initcfg*)databuf)->osd_status_timeout = osd_status_timeout;
((ude_initcfg*)databuf)->phase_hotkey_enable = phase_hotkey_enable;
memcpy(((ude_initcfg*)databuf)->keys, rc_keymap, sizeof(rc_keymap));
for (i=0; i<sizeof(ude_initcfg); i++)
databuf[i] = bitswap8(databuf[i]);
/*retval = alt_epcq_controller2_write(epcq_dev, (USERDATA_OFFSET+entry*SECTORSIZE), databuf, sizeof(ude_initcfg));
if (retval != 0)
return retval;*/
sniprintf(hdr.name, USERDATA_NAME_LEN+1, "INITCFG");
} else if (hdr.type == UDE_PROFILE) {
target_map = ude_profile_items;
hdr.num_items = sizeof(ude_profile_items)/sizeof(ude_item_map);
printf("Initconfig data written (%u bytes)\n", sizeof(ude_initcfg) - offsetof(ude_initcfg, last_profile));
break;
case UDE_PROFILE:
((ude_hdr*)databuf)->version_major = PROFILE_VER_MAJOR;
((ude_hdr*)databuf)->version_minor = PROFILE_VER_MINOR;
vm_to_write = sizeof(video_modes_plm_default);
((ude_profile*)databuf)->avc_data_len = sizeof(avconfig_t);
((ude_profile*)databuf)->vm_data_len = vm_to_write;
// Check if name override file exists
sniprintf(p_filename, sizeof(p_filename), "prof_n_i.txt");
if (!file_open(&name_file, p_filename)) {
if (target_profile_name[0] == 0)
sniprintf(target_profile_name, PROFILE_NAME_LEN+1, "<used>");
for (i=0; i<=entry; i++) {
if (file_get_string(&name_file, target_profile_name, sizeof(target_profile_name)) == NULL)
break;
}
strncpy(((ude_profile*)databuf)->name, target_profile_name, PROFILE_NAME_LEN+1);
file_close(&name_file);
pageoffset = offsetof(ude_profile, avc);
// assume that sizeof(avconfig_t) << PAGESIZE
memcpy(databuf+pageoffset, &tc, sizeof(avconfig_t));
pageoffset += sizeof(avconfig_t);
// erase sector and write a full page first, assume sizeof(video_modes_plm) >> PAGESIZE
memcpy(databuf+pageoffset, (char*)video_modes_plm, PAGESIZE-pageoffset);
srcoffset = PAGESIZE-pageoffset;
vm_to_write -= PAGESIZE-pageoffset;
for (i=0; i<PAGESIZE; i++)
databuf[i] = bitswap8(databuf[i]);
/*retval = alt_epcq_controller2_write(epcq_dev, (USERDATA_OFFSET+entry*SECTORSIZE), databuf, PAGESIZE);
if (retval != 0)
return retval;*/
// then write the rest page by page
pageno = 1;
while (vm_to_write > 0) {
memcpy(databuf, (char*)video_modes_plm+srcoffset, (vm_to_write > PAGESIZE) ? PAGESIZE : vm_to_write);
for (i=0; i<PAGESIZE; i++)
databuf[i] = bitswap8(databuf[i]);
/*retval = alt_epcq_controller2_write_block(epcq_dev, (USERDATA_OFFSET+entry*SECTORSIZE), (USERDATA_OFFSET+entry*SECTORSIZE+pageno*PAGESIZE), databuf, (vm_to_write > PAGESIZE) ? PAGESIZE : vm_to_write);
if (retval != 0)
return retval;*/
srcoffset += PAGESIZE;
vm_to_write = (vm_to_write < PAGESIZE) ? 0 : (vm_to_write - PAGESIZE);
pageno++;
}
if (i == entry+1) {
// strip CR / CRLF
target_profile_name[strcspn(target_profile_name, "\r\n")] = 0;
strlcpy(hdr.name, target_profile_name, USERDATA_NAME_LEN+1);
} else if (cur_profile_name[0] == 0) {
sniprintf(hdr.name, USERDATA_NAME_LEN+1, "<used>");
} else {
strlcpy(hdr.name, cur_profile_name, USERDATA_NAME_LEN+1);
}
printf("Profile %u data written (%u bytes)\n", entry, sizeof(avconfig_t)+sizeof(video_modes_plm_default));
break;
default:
break;
}
flash_addr = flashctrl_dev.flash_size - (16-entry)*FLASH_SECTOR_SIZE;
// Disable flash write protect and erase sector
flash_write_protect(&flashctrl_dev, 0);
flash_sector_erase(&flashctrl_dev, flash_addr);
// Write data into erased sector
memcpy((uint32_t*)(INTEL_GENERIC_SERIAL_FLASH_INTERFACE_TOP_0_AVL_MEM_BASE + flash_addr), &hdr, sizeof(ude_hdr));
bytes_written = sizeof(ude_hdr);
for (i=0; i<hdr.num_items; i++) {
memcpy((uint32_t*)(INTEL_GENERIC_SERIAL_FLASH_INTERFACE_TOP_0_AVL_MEM_BASE + flash_addr + bytes_written), &target_map[i].hdr, sizeof(ude_item_hdr));
bytes_written += sizeof(ude_item_hdr);
memcpy((uint32_t*)(INTEL_GENERIC_SERIAL_FLASH_INTERFACE_TOP_0_AVL_MEM_BASE + flash_addr + bytes_written), target_map[i].data, target_map[i].hdr.data_size);
bytes_written += target_map[i].hdr.data_size;
}
// Re-enable write protection
flash_write_protect(&flashctrl_dev, 1);
printf("%lu bytes written into userdata entry %u\n", bytes_written, entry);
return 0;
}
int read_userdata(uint8_t entry, int dry_run) {
ude_hdr hdr;
ude_item_hdr item_hdr;
const ude_item_map *target_map;
uint32_t flash_addr, bytes_read;
int i, j, target_map_items;
int read_userdata(alt_u8 entry, int dry_run)
{
int retval, i;
alt_u8 databuf[PAGESIZE];
alt_u16 vm_to_read;
alt_u16 pageoffset, dstoffset;
alt_u8 pageno;
target_profile_name[0] = 0;
if (entry > MAX_USERDATA_ENTRY) {
printf("invalid entry\n");
return -1;
}
flash_addr = flashctrl_dev.flash_size - (16-entry)*FLASH_SECTOR_SIZE;
memcpy(&hdr, (uint32_t*)(INTEL_GENERIC_SERIAL_FLASH_INTERFACE_TOP_0_AVL_MEM_BASE + flash_addr), sizeof(ude_hdr));
bytes_read = sizeof(ude_hdr);
//retval = alt_epcq_controller2_read(epcq_dev, (USERDATA_OFFSET+entry*SECTORSIZE), databuf, PAGESIZE);
for (i=0; i<PAGESIZE; i++)
databuf[i] = bitswap8(databuf[i]);
if (retval != 0)
return retval;
if (strncmp(hdr.userdata_key, "USRDATA", 8)) {
if (strncmp(((ude_hdr*)databuf)->userdata_key, "USRDATA", 8)) {
printf("No userdata found on entry %u\n", entry);
return 1;
}
strlcpy(target_profile_name, hdr.name, USERDATA_NAME_LEN+1);
if (dry_run)
return 0;
switch (((ude_hdr*)databuf)->type) {
case UDE_INITCFG:
if ((((ude_hdr*)databuf)->version_major != INITCFG_VER_MAJOR) || (((ude_hdr*)databuf)->version_minor != INITCFG_VER_MINOR)) {
printf("Initconfig version %u.%.2u does not match current one\n", ((ude_hdr*)databuf)->version_major, ((ude_hdr*)databuf)->version_minor);
return 2;
}
if (((ude_initcfg*)databuf)->data_len == sizeof(ude_initcfg) - offsetof(ude_initcfg, last_profile)) {
if (dry_run)
return 0;
target_map = (hdr.type == UDE_INITCFG) ? ude_initcfg_items : ude_profile_items;
target_map_items = (hdr.type == UDE_INITCFG) ? sizeof(ude_initcfg_items)/sizeof(ude_item_map) : sizeof(ude_profile_items)/sizeof(ude_item_map);
for (i = 0; i < sizeof(input_profiles)/sizeof(*input_profiles); ++i)
if (((ude_initcfg*)databuf)->last_profile[i] <= MAX_PROFILE)
input_profiles[i] = ((ude_initcfg*)databuf)->last_profile[i];
def_input = ((ude_initcfg*)databuf)->def_input;
if (def_input < AV_LAST)
target_input = def_input;
else if (((ude_initcfg*)databuf)->last_input < AV_LAST)
target_input = ((ude_initcfg*)databuf)->last_input;
auto_input = ((ude_initcfg*)databuf)->auto_input;
auto_av1_ypbpr = ((ude_initcfg*)databuf)->auto_av1_ypbpr;
auto_av2_ypbpr = ((ude_initcfg*)databuf)->auto_av2_ypbpr;
auto_av3_ypbpr = ((ude_initcfg*)databuf)->auto_av3_ypbpr;
osd_enable = ((ude_initcfg*)databuf)->osd_enable;
osd_status_timeout = ((ude_initcfg*)databuf)->osd_status_timeout;
profile_link = ((ude_initcfg*)databuf)->profile_link;
profile_sel = input_profiles[AV_TESTPAT]; // Global profile
lcd_bl_timeout = ((ude_initcfg*)databuf)->lcd_bl_timeout;
phase_hotkey_enable = ((ude_initcfg*)databuf)->phase_hotkey_enable;
memcpy(rc_keymap, ((ude_initcfg*)databuf)->keys, sizeof(rc_keymap));
printf("RC data read (%u bytes)\n", sizeof(rc_keymap));
}
break;
case UDE_PROFILE:
if ((((ude_hdr*)databuf)->version_major != PROFILE_VER_MAJOR) || (((ude_hdr*)databuf)->version_minor != PROFILE_VER_MINOR)) {
printf("Profile version %u.%.2u does not match current one\n", ((ude_hdr*)databuf)->version_major, ((ude_hdr*)databuf)->version_minor);
return 2;
}
if ((((ude_profile*)databuf)->avc_data_len == sizeof(avconfig_t)) && (((ude_profile*)databuf)->vm_data_len == sizeof(video_modes_plm_default))) {
strncpy(target_profile_name, ((ude_profile*)databuf)->name, PROFILE_NAME_LEN+1);
if (dry_run)
return 0;
for (i=0; i<hdr.num_items; i++) {
memcpy(&item_hdr, (uint32_t*)(INTEL_GENERIC_SERIAL_FLASH_INTERFACE_TOP_0_AVL_MEM_BASE + flash_addr + bytes_read), sizeof(ude_item_hdr));
bytes_read += sizeof(ude_item_hdr);
for (j=0; j<target_map_items; j++) {
if (!memcmp(&item_hdr, &target_map[j].hdr, sizeof(ude_item_hdr))) {
memcpy(target_map[j].data, (uint32_t*)(INTEL_GENERIC_SERIAL_FLASH_INTERFACE_TOP_0_AVL_MEM_BASE + flash_addr + bytes_read), item_hdr.data_size);
break;
vm_to_read = ((ude_profile*)databuf)->vm_data_len;
pageno = 0;
pageoffset = offsetof(ude_profile, avc);
// assume that sizeof(avconfig_t) << PAGESIZE
memcpy(&tc, databuf+pageoffset, sizeof(avconfig_t));
pageoffset += sizeof(avconfig_t);
dstoffset = 0;
while (vm_to_read > 0) {
if (vm_to_read >= PAGESIZE-pageoffset) {
memcpy((char*)video_modes_plm+dstoffset, databuf+pageoffset, PAGESIZE-pageoffset);
dstoffset += PAGESIZE-pageoffset;
vm_to_read -= PAGESIZE-pageoffset;
pageoffset = 0;
pageno++;
// check
//retval = alt_epcq_controller2_read(epcq_dev, (USERDATA_OFFSET+entry*SECTORSIZE+pageno*PAGESIZE), databuf, PAGESIZE);
for (i=0; i<PAGESIZE; i++)
databuf[i] = bitswap8(databuf[i]);
if (retval != 0)
return retval;
} else {
memcpy((char*)video_modes_plm+dstoffset, databuf+pageoffset, vm_to_read);
pageoffset += vm_to_read;
vm_to_read = 0;
}
}
}
bytes_read += item_hdr.data_size;
update_cur_vm = 1;
if (bytes_read >= FLASH_SECTOR_SIZE) {
printf("userdata entry %u corrupted\n", entry);
return -1;
printf("Profile %u data read (%u bytes)\n", entry, sizeof(avconfig_t)+sizeof(video_modes_plm_default));
}
break;
default:
printf("Unknown userdata entry\n");
break;
}
if (hdr.type == UDE_PROFILE)
update_cur_vm = 1;
strlcpy(cur_profile_name, target_profile_name, USERDATA_NAME_LEN+1);
printf("%lu bytes read from userdata entry %u\n", bytes_read, entry);
return 0;
}
int write_userdata_sd(uint8_t entry) {
FIL p_file, name_file;
ude_hdr hdr;
const ude_item_map *target_map;
unsigned int bytes_written, bytes_written_tot;
char p_filename[14];
int i=0, retval=0;
int import_userdata()
{
SDRESULTS res;
int retval;
int n, entries_imported=0;
char *errmsg;
alt_u8 databuf[SD_BLK_SIZE];
ude_hdr header;
alt_u32 btn_vec;
if (entry == SD_INIT_CONFIG_SLOT)
sniprintf(p_filename, sizeof(p_filename), "settings.bin");
else
sniprintf(p_filename, sizeof(p_filename), "prof%.2u.bin", entry);
retval = check_sdcard(databuf);
SPI_CS_High();
if (retval != 0)
goto sd_disable;
if (entry > MAX_SD_USERDATA_ENTRY) {
printf("invalid entry\n");
return -1;
strncpy(menu_row2, "Import? 1=Y, 2=N", LCD_ROW_LEN+1);
ui_disp_menu(2);
while (1) {
btn_vec = IORD_ALTERA_AVALON_PIO_DATA(PIO_1_BASE) & RC_MASK;
if (btn_vec == rc_keymap[RC_BTN1]) {
break;
} else if (btn_vec == rc_keymap[RC_BTN2]) {
retval = UDATA_IMPT_CANCELLED;
strncpy(menu_row2, "Cancelled", LCD_ROW_LEN+1);
goto sd_disable;
}
usleep(WAITLOOP_SLEEP_US);
}
if (!sdcard_dev.mount) {
retval = file_mount();
strncpy(menu_row2, "Loading...", LCD_ROW_LEN+1);
ui_disp_menu(2);
// Import the userdata
for (n=0; n<=MAX_USERDATA_ENTRY; ++n) {
res = SD_Read(&sdcard_dev, &header, (512+n*SECTORSIZE)/SD_BLK_SIZE, 0, sizeof(header));
if (res != SD_OK) {
printf("Failed to read SD card\n");
retval = -res;
goto sd_disable;
}
if (strncmp(header.userdata_key, "USRDATA", 8)) {
printf("Not an userdata entry at 0x%x\n", 512+n*SECTORSIZE);
continue;
}
if ((header.type == UDE_PROFILE) && ((header.version_major != PROFILE_VER_MAJOR) || (header.version_minor != PROFILE_VER_MINOR))) {
printf("Profile version %u.%.2u does not match current one\n", header.version_major, header.version_minor);
continue;
} else if ((header.type == UDE_INITCFG) && ((header.version_major != INITCFG_VER_MAJOR) || (header.version_minor != INITCFG_VER_MINOR))) {
printf("Initconfig version %u.%.2u does not match current one\n", header.version_major, header.version_minor);
continue;
} else if (header.type > UDE_PROFILE) {
printf("Unknown userdata entry type %u\n", header.type);
continue;
}
// Just blindly write the entry to flash
retval = copy_sd_to_flash((512+n*SECTORSIZE)/SD_BLK_SIZE, (n*PAGES_PER_SECTOR)+(USERDATA_OFFSET/PAGESIZE),
(header.type == UDE_PROFILE) ? (sizeof(ude_profile)+sizeof(video_modes_plm_default)) : sizeof(ude_initcfg), databuf);
if (retval != 0) {
printf("SD card not detected %d\n", retval);
return -2;
}
}
if (f_open(&p_file, p_filename, FA_WRITE|FA_CREATE_ALWAYS) != F_OK) {
return -3;
}
memset(&hdr, 0x00, sizeof(ude_hdr));
strlcpy(hdr.userdata_key, "USRDATA", 8);
hdr.type = (entry > MAX_SD_PROFILE) ? UDE_INITCFG : UDE_PROFILE;
if (hdr.type == UDE_INITCFG) {
target_map = ude_initcfg_items;
hdr.num_items = sizeof(ude_initcfg_items)/sizeof(ude_item_map);
sniprintf(hdr.name, USERDATA_NAME_LEN+1, "INITCFG");
} else if (hdr.type == UDE_PROFILE) {
target_map = ude_profile_items;
hdr.num_items = sizeof(ude_profile_items)/sizeof(ude_item_map);
// Check if name override file exists
sniprintf(p_filename, sizeof(p_filename), "prof_n.txt");
if (!file_open(&name_file, p_filename)) {
for (i=0; i<=entry; i++) {
if (file_get_string(&name_file, target_profile_name, sizeof(target_profile_name)) == NULL)
break;
}
file_close(&name_file);
printf("Copy from SD to flash failed (error %d)\n", retval);
goto sd_disable;
}
if (i == entry+1) {
// strip CR / CRLF
target_profile_name[strcspn(target_profile_name, "\r\n")] = 0;
strlcpy(hdr.name, target_profile_name, USERDATA_NAME_LEN+1);
} else if (cur_profile_name[0] == 0) {
sniprintf(hdr.name, USERDATA_NAME_LEN+1, "<used>");
} else {
strlcpy(hdr.name, cur_profile_name, USERDATA_NAME_LEN+1);
}
entries_imported++;
}
// Write header
if ((f_write(&p_file, &hdr, sizeof(ude_hdr), &bytes_written) != F_OK) || (bytes_written != sizeof(ude_hdr))) {
retval = -4;
goto close_file;
}
bytes_written_tot = bytes_written;
// flash read immediately after write might fail, add some delay
usleep(1000);
// Write data
for (i=0; i<hdr.num_items; i++) {
if ((f_write(&p_file, &target_map[i].hdr, sizeof(ude_item_hdr), &bytes_written) != F_OK) || (bytes_written != sizeof(ude_item_hdr))) {
retval = -5;
goto close_file;
}
bytes_written_tot += bytes_written;
read_userdata(INIT_CONFIG_SLOT, 0);
profile_sel = input_profiles[target_input];
read_userdata(profile_sel, 0);
if ((f_write(&p_file, target_map[i].data, target_map[i].hdr.data_size, &bytes_written) != F_OK) || (bytes_written != target_map[i].hdr.data_size)) {
retval = -6;
goto close_file;
}
bytes_written_tot += bytes_written;
}
sniprintf(menu_row2, LCD_ROW_LEN+1, "%d slots loaded", entries_imported);
retval = 1;
printf("%u bytes written into userdata entry %u\n", bytes_written_tot, entry);
sd_disable:
SPI_CS_High();
close_file:
file_close(&p_file);
return retval;
}
int read_userdata_sd(uint8_t entry, int dry_run) {
FIL p_file;
ude_hdr hdr;
ude_item_hdr item_hdr;
const ude_item_map *target_map;
unsigned int bytes_read, bytes_read_tot;
char p_filename[14];
int i, j, target_map_items, retval=0;
static alt_u8 poll_yesno(const useconds_t useconds, alt_u32 *const btn_vec_out)
{
alt_u32 btn_vec;
alt_u8 ret = 0U;
if (entry == SD_INIT_CONFIG_SLOT)
sniprintf(p_filename, 14, "settings.bin");
else
sniprintf(p_filename, 14, "prof%.2u.bin", entry);
for (alt_u32 i = 0; i < (useconds/WAITLOOP_SLEEP_US); ++i) {
btn_vec = IORD_ALTERA_AVALON_PIO_DATA(PIO_1_BASE) & RC_MASK;
if (entry > MAX_SD_USERDATA_ENTRY) {
printf("invalid entry\n");
return -1;
}
if (!sdcard_dev.mount) {
retval = file_mount();
if (retval != 0) {
printf("SD card not detected %d\n", retval);
return -2;
}
}
if (file_open(&p_file, p_filename) != F_OK) {
return -3;
}
if ((f_read(&p_file, &hdr, sizeof(ude_hdr), &bytes_read) != F_OK) || (bytes_read != sizeof(ude_hdr))) {
printf("Hdr read error\n");
retval = -4;
goto close_file;
}
bytes_read_tot = bytes_read;
if (strncmp(hdr.userdata_key, "USRDATA", 8)) {
printf("No userdata found on file\n");
retval = -5;
goto close_file;
}
strlcpy(target_profile_name, hdr.name, USERDATA_NAME_LEN+1);
if (dry_run)
goto close_file;
target_map = (hdr.type == UDE_INITCFG) ? ude_initcfg_items : ude_profile_items;
target_map_items = (hdr.type == UDE_INITCFG) ? sizeof(ude_initcfg_items)/sizeof(ude_item_map) : sizeof(ude_profile_items)/sizeof(ude_item_map);
for (i=0; i<hdr.num_items; i++) {
if ((f_read(&p_file, &item_hdr, sizeof(ude_item_hdr), &bytes_read) != F_OK) || (bytes_read != sizeof(ude_item_hdr))) {
printf("Item header read fail\n");
retval = -6;
goto close_file;
}
bytes_read_tot += sizeof(ude_item_hdr);
for (j=0; j<target_map_items; j++) {
if (!memcmp(&item_hdr, &target_map[j].hdr, sizeof(ude_item_hdr))) {
if ((f_read(&p_file, target_map[j].data, item_hdr.data_size, &bytes_read) != F_OK) || (bytes_read != item_hdr.data_size)) {
printf("Item data read fail\n");
retval = -7;
goto close_file;
}
for (alt_u32 j = RC_BTN1; j < (REMOTE_MAX_KEYS - 1); ++j) {
if (btn_vec == rc_keymap[j]) {
ret = 1U;
break;
}
}
bytes_read_tot += item_hdr.data_size;
if (j == target_map_items)
f_lseek(&p_file, bytes_read_tot);
if (ret)
break;
usleep(WAITLOOP_SLEEP_US);
}
if (hdr.type == UDE_PROFILE)
update_cur_vm = 1;
if (ret)
*btn_vec_out = btn_vec;
strlcpy(cur_profile_name, target_profile_name, USERDATA_NAME_LEN+1);
printf("%u bytes read from userdata entry %u\n", bytes_read_tot, entry);
close_file:
file_close(&p_file);
return retval;
return ret;
}
int export_userdata()
{
int retval;
const char *msg;
alt_u8 databuf[SD_BLK_SIZE];
alt_u8 prompt_state = 0;
useconds_t prompt_delay;
const alt_u8 prompt_transitions[] = { 1, 2, 0, 0, };
const alt_u8 prompt_ofs[] = { 0, 16, 31, LNG(48, 47), };
const char *prompt_msgs =
LNG(
"SD CARD WILL BE" "\0" // [ 0..15]
"OVERWRITTEN!!!" "\0" // [16..30]
"Export? 1=Y, 2=N""\0" // [31..47]
"Press 1 or 2", // [48..60]
"SDカードヲウワガキシマス" "\0" // [ 0..15]
"ゴチュウイクダサイ!!!" "\0" // [16..30]
"1=ジッコウスル 2=ヤメル" "\0" // [31..46]
"ドチラカエランデクダサイ" // [47..60]
);
alt_u32 btn_vec, sd_block_offset;
_Static_assert(SD_BLK_SIZE == FAT16_SECTOR_SIZE, "Sector size mismatch");
retval = check_sdcard(databuf);
SPI_CS_High();
if (retval != 0) {
retval = -retval;
goto out;
}
usleep(100000U);
while (1) {
msg = &prompt_msgs[prompt_ofs[prompt_state]];
prompt_delay = (prompt_state == 2) ? 2000000U
: ((prompt_state == 3) ? 300000U : 1000000U);
prompt_state = prompt_transitions[prompt_state];
strncpy(menu_row2, msg, LCD_ROW_LEN+1);
ui_disp_menu(2);
if (poll_yesno(prompt_delay, &btn_vec))
goto eval_button;
continue;
eval_button:
if (btn_vec == rc_keymap[RC_BTN1]) {
break;
} else if (btn_vec == rc_keymap[RC_BTN2] ||
btn_vec == rc_keymap[RC_BACK])
{
retval = UDATA_EXPT_CANCELLED;
goto out;
}
prompt_state = 3;
}
usleep(100000U);
strncpy(menu_row1,"SD Format", LCD_ROW_LEN+1);
strncpy(menu_row2,"1=FAT16, 2=RAW", LCD_ROW_LEN+1);
ui_disp_menu(2);
if ((!poll_yesno(5000000U, &btn_vec)) || ((btn_vec != rc_keymap[RC_BTN1]) && (btn_vec != rc_keymap[RC_BTN2]))) {
retval = UDATA_EXPT_CANCELLED;
goto out;
}
sd_block_offset = (btn_vec == rc_keymap[RC_BTN1]) ? (PROF_16_DATA_OFS/SD_BLK_SIZE) : 0;
strncpy(menu_row2, LNG("Exporting...", "オマチクダサイ"), LCD_ROW_LEN+1);
ui_disp_menu(2);
// RAW copy
if (btn_vec == rc_keymap[RC_BTN2])
goto copy_start;
/* Zero out the boot sector, FATs and root directory. */
memset(databuf, 0, SD_BLK_SIZE);
for (alt_u32 sector = 0;
sector < (FAT16_ROOT_DIR_FIRST_SECTOR + FAT16_ROOT_DIR_SECTORS);
++sector)
{
retval = SD_Write(&sdcard_dev, databuf, sector);
if (retval)
goto out;
}
/* Generate and write the boot sector. */
generate_boot_sector_16(databuf);
retval = SD_Write(&sdcard_dev, databuf, 0);
if (retval)
goto out;
/* Generate and write the file allocation tables. */
for (alt_u16 clusters_written = 0, sd_blk_idx = 0;
clusters_written < (PROF_16_DATA_SIZE/FAT16_CLUSTER_SIZE);)
{
memset(databuf, 0, SD_BLK_SIZE);
clusters_written = generate_fat16(databuf, clusters_written);
retval = SD_Write(&sdcard_dev, databuf,
(FAT16_1_OFS/SD_BLK_SIZE) + sd_blk_idx);
if (retval)
goto out;
retval = SD_Write(&sdcard_dev, databuf,
(FAT16_2_OFS/SD_BLK_SIZE) + sd_blk_idx);
if (retval)
goto out;
++sd_blk_idx;
}
/* Write the directory entry of the settings file. */
memset(databuf, 0, SD_BLK_SIZE);
memcpy(databuf, prof_dirent_16, PROF_DIRENT_16_SIZE);
retval = SD_Write(&sdcard_dev, databuf, PROF_DIRENT_16_OFS/SD_BLK_SIZE);
if (retval)
goto out;
copy_start:
// Zero out first 512 bytes (1 SD block) of the file
memset(databuf, 0, SD_BLK_SIZE);
retval = SD_Write(&sdcard_dev, databuf, sd_block_offset++);
if (retval)
goto out;
/* This may wear the SD card a bit more than necessary... */
retval = copy_flash_to_sd(USERDATA_OFFSET/PAGESIZE,
sd_block_offset,
(MAX_USERDATA_ENTRY + 1) * SECTORSIZE,
databuf);
out:
SPI_CS_High();
switch (retval) {
case 0:
msg = LNG("Success", "カンリョウシマシタ"); // Alternative: "カンリョウイタシマシタ"
break;
case SD_NOINIT:
msg = LNG("No SD card det.", "SDカードガミツカリマセン");
break;
case -EINVAL:
msg = LNG("Invalid params.", "パラメータガムコウデス");
break;
case UDATA_EXPT_CANCELLED:
msg = LNG("Cancelled", "キャンセルサレマシタ"); // Alternative: "キャンセルサセテイタダキマス"
break;
default:
msg = LNG("SD/Flash error", "SDカFLASHノエラー"); // フラッシュ would be NG.
break;
}
strncpy(menu_row2, msg, LCD_ROW_LEN+1);
if (!retval) {
return 1;
} else {
/*
* We want the message above to remain on screen, so return a
* positive value which nevertheless stands out when debugging.
*/
return 0x0dead;
}
}

View File

@ -136,10 +136,7 @@ BSP_CFLAGS_OPTIMIZATION = -Os
# C/C++ compiler warning level. "-Wall" is commonly used.This setting defines
# the value of BSP_CFLAGS_WARNINGS in Makefile.
BSP_CFLAGS_WARNINGS = -Wall -Wno-unused-but-set-variable -Wno-unused-variable -Wno-unused-function
# Match APP flags
BSP_CFLAGS_USER_FLAGS := -fdata-sections -ffunction-sections -fshort-enums -fgnu89-inline -flto -msmall-data-limit=0
BSP_CFLAGS_WARNINGS = -Wall
# C compiler command.
CC = riscv64-unknown-elf-gcc -xc

15640
sys.sopcinfo Normal file

File diff suppressed because one or more lines are too long