Nios® II Software Developer Handbook

ID 683525
Date 8/28/2023
Public
Document Table of Contents

5.6.1.2. Tcl Script to Examine Hardware and Choose Settings

Note: The Nios® II SBT uses slave descriptors to refer to components connected to the Nios® II processor. A slave descriptor is the unique name of a hardware component’s slave port.

If a component has only one slave port connected to the Nios® II processor, the slave descriptor is the same as the name of the component (for example, onchip_mem_0). If a component has multiple slave ports connecting the Nios® II to multiple resources in the component, the slave descriptor is the name of the component followed by an underscore and the slave port name (for example, onchip_mem_0_s1).

# Select a device connected to the processor as the default STDIO device.
# It returns the slave descriptor of the selected device.
# It gives first preference to devices with stdio in the name.
# It gives second preference to JTAG UARTs.
# If no JTAG UARTs are found, it uses the last character device.
# If no character devices are found, it returns "none".
# Procedure that does all the work of determining the stdio device
proc choose_default_stdio {} {
   set last_stdio "none"
   set first_jtag_uart "none"
# Get all slaves attached to the processor.
   set slave_descs [get_slave_descs]
   foreach slave_desc $slave_descs {
# Lookup module class name for slave descriptor.
      set module_name [get_module_name $slave_desc]
      set module_class_name [get_module_class_name $module_name]
# If the module_name contains "stdio", we choose it
# and return immediately.
      if { [regexp .*stdio.* $module_name] } {
         return $slave_desc
}
# Assume it is a JTAG UART if the module class name contains
# the string "jtag_uart". In that case, return the first one
# found.
   if { [regexp .*jtag_uart.* $module_class_name] } {
   if {$first_jtag_uart == "none"} {
      set first_jtag_uart $slave_desc
}
}
# Track last character device in case no JTAG UARTs found.
   if { [is_char_device $slave_desc] } {
      set last_stdio $slave_desc
}
}
   if {$first_jtag_uart != "none"} {
      return $first_jtag_uart
}
   return $last_stdio
}
# Call routine to determine stdio
   set default_stdio [choose_default_stdio]
# Set stdio settings to use results of above call.
   set_setting hal.stdin $default_stdio
   set_setting hal.stdout $default_stdio
   set_setting hal.stderr $default_stdio
# Select a device connected to the processor as the default STDIO device. 
# It returns the slave descriptor of the selected device. 
# It gives first preference to devices with stdio in the name. 
# It gives second preference to JTAG UARTs. 
# If no JTAG UARTs are found, it uses the last character device. 
# If no character devices are found, it returns "none". 
# Procedure that does all the work of determining the stdio device proc choose_default_stdio {} 
{  
   set last_stdio "none"  set first_jtag_uart "none"  
# Get all slaves attached to the processor.  
   set slave_descs [get_slave_descs]  foreach slave_desc $slave_descs 
{  
# Lookup module class name for slave descriptor.  
   set module_name [get_module_name $slave_desc]  
   set module_class_name [get_module_class_name $module_name]  
# If the module_name contains "stdio", we choose it  
# and return immediately.  
   if { [regexp .*stdio.* $module_name] } 
{  
      return $slave_desc  
}  
# Assume it is a JTAG UART if the module class name contains  
# the string "jtag_uart". In that case, return the first one  
# found.  
   if { [regexp .*jtag_uart.* $module_class_name] } 
{  
      if {$first_jtag_uart == "none"
} 
{  
         set first_jtag_uart $slave_desc  
}  
}  
# Track last character device in case no JTAG UARTs found.  
   if { [is_char_device $slave_desc] } 
{  
      set last_stdio $slave_desc  
}  
}  
   if {$first_jtag_uart != "none"} 
{  
      return $first_jtag_uart  
}  
   return $last_stdio 
} 
# Call routine to determine stdio set default_stdio [choose_default_stdio] 
# Set stdio settings to use results of above call. 
   set_setting hal.stdin $default_stdio set_setting hal.stdout 
   $default_stdio set_setting hal.stderr $default_stdio