#==============================================================
#
#  Abs:  Load YAML files and configure
#
#  Name: init_cpsw.cmd 
#
#  Side: Upon entry to this script, the following macros
#        are expected:
#     BOARD_IP         - IP address for carrier board
#     FPGA_IP          - FPGA IP Address and Slot number
#     YAML_CONFIG_FILE - Yam configuration filename
#     TOP_YAML_FILE    - Top Yaml filename, ex)<ioc>.yaml
#     DICT_FILE        - Dictionary file for manual,
#                        string empty of none.
#     IOC_DATA         - data dir path, wwrite access
#     IOC              - IOC name (from envpath)
#
#  Facility: EED Motion Control
#
#  Auth: dd-mmm-2018, Author's Name   (USERNAME)
#  Rev:  dd-mmm-yyyy, Reviewer's Name (USERNAME)
#--------------------------------------------------------------
#  Mod:
#        19-Jul-2023, Namrata Balakrishnan (namrata)
#           Added AMC Prefix environment variable to init file.
#        27-Apr-2021, N. Balakrishnan (namrata):
#          extract from iocBoot/common/init_atca.cmd
#          This is such that lcls and lcls2 timing and bsa initialization can be separate
#
#==============================================================
epicsEnvSet("AMCC_NAME"   ,"AMCC:${IOC_LOCA}:${IOC_UNIT}")

epicsEnvSet("TOP_YAML_FILE"   , "000TopLevel.yaml")

#epicsEnvSet("YAML_CONFIG_FILE", "defaultsFwsLcls1-7-23-19.yaml")
epicsEnvSet("YAML_CONFIG_FILE", "defaults.yaml")
epicsEnvSet("DICT_FILE"       , "fwsLcls2.dict")

# Yaml Downloader
DownloadYamlFile("${FPGA_IP}", "${IOC_DATA}/${IOC}/yaml")

# Load FPGA hierarchy from YAML
# cpswLoadYamlFile ( YAML hierarchy descrtiption file,
#                    Root Device Name (optional; default = 'root'),
#                    directory where YAML includes can be found (optional),
#                    IP address for carrier board (override the IP address in YAML) )
cpswLoadYamlFile("${IOC_DATA}/${IOC}/yaml/${TOP_YAML_FILE}", "NetIODev", "", "${FPGA_IP}")

# Load configuration to disable BSA/BLD/BSSS/BSAS on boot
cpswLoadConfigFile("${TOP}/firmware/yaml/disable_bld_bsss_bsas.yaml", "mmio/AmcCarrierCore/AmcCarrierBsa")

# Load FPGA configuration from YAML file
# cpswLoadConfigFile ( CONFIG YAML file (NOT hierarchy YAML!),
#                      Path prefix (if config YAML is not based of the root device; optional),
#                      directory where YAML includes can be found (optional) )
cpswLoadConfigFile("${IOC_DATA}/${IOC}/yaml/config/${YAML_CONFIG_FILE}", "mmio", "")

# Load configuration to set up registers for wirescanner MPS
cpswLoadConfigFile("${TOP}/firmware/yaml/mover_setup_mps.yaml", "mmio")
# Load configuration to set up AMC0
cpswLoadConfigFile("${TOP}/firmware/config/defaults_AMC0.yaml", "mmio")
# Load configuration to set up AMC1
cpswLoadConfigFile("${TOP}/firmware/config/defaults_AMC1.yaml", "mmio")

# Configure asyn port driver
# YCPSWASYNConfig(
#    Port Name,                 # the name given to this port driver
#    Root Path                  # OPTIONAL: Root path to start the generation. If empty, the Yaml root will be used
#    Record name Prefix,        # Record name prefix
#    Use DB Autogeneration,     # Set to 1 for autogeneration of records from the YAML definition. Set to 0 to disable it
#    Load dictionary,           # Dictionary file path with registers to load. An empty string will disable this function
# In Sector 0 L2KA00-05, the BCMs are in slots 6 and 7. Here, for testing purposes we are using slots 4 and 5.
YCPSWASYNConfig("cpsw", "", "", "0", "${TOP}/firmware/${DICT_FILE}")

# Command to access common platform FPGA registers
# cpswATCACommonAsynDriverConfigure(Asyn port name, yaml path, named root (optional))
# Leave this commented until we get a read access command to only read FPGA info
#cpswATCACommonAsynDriverConfigure("atca", "mmio", "")

# Load database for common CPSW registers
dbLoadRecords("db/cpsw_common.db","PORT=cpsw,P=${AMCC_NAME}")
# Leave this commented until we get a read access command to only read FPGA info
#dbLoadRecords("db/ATCACommon.db","PORT=atca,DEV=${AMCC_NAME}")

# End of script

