2018-11-16 01:51:50 +08:00
|
|
|
#
|
|
|
|
# Copyright (c) 2018-2019 Linaro
|
2019-01-31 00:54:21 +08:00
|
|
|
# Copyright (c) 2019 Nordic Semiconductor ASA
|
2018-11-16 01:51:50 +08:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
# Types we support
|
|
|
|
# 'string', 'int', 'hex', 'bool'
|
|
|
|
|
2019-01-31 00:54:21 +08:00
|
|
|
doc_mode = os.environ.get('KCONFIG_DOC_MODE') == "1"
|
2019-02-11 22:57:34 +08:00
|
|
|
|
2018-11-16 01:51:50 +08:00
|
|
|
dt_defines = {}
|
2019-03-21 02:30:29 +08:00
|
|
|
if not doc_mode:
|
2019-02-20 02:48:35 +08:00
|
|
|
# The env var 'GENERATED_DTS_BOARD_CONF' must be set unless we are in
|
|
|
|
# doc mode
|
|
|
|
GENERATED_DTS_BOARD_CONF = os.environ['GENERATED_DTS_BOARD_CONF']
|
|
|
|
if os.path.isfile(GENERATED_DTS_BOARD_CONF):
|
|
|
|
with open(GENERATED_DTS_BOARD_CONF, 'r', encoding='utf-8') as fd:
|
|
|
|
for line in fd:
|
|
|
|
if '=' in line:
|
2019-04-08 20:13:39 +08:00
|
|
|
define, val = line.split('=', 1)
|
2019-02-20 02:48:35 +08:00
|
|
|
dt_defines[define] = val.strip()
|
2018-11-16 01:51:50 +08:00
|
|
|
|
|
|
|
def _dt_units_to_scale(unit):
|
|
|
|
if not unit:
|
|
|
|
return 0
|
|
|
|
if unit in {'k', 'K'}:
|
|
|
|
return 10
|
|
|
|
if unit in {'m', 'M'}:
|
|
|
|
return 20
|
|
|
|
if unit in {'g', 'G'}:
|
|
|
|
return 30
|
|
|
|
|
|
|
|
def dt_int_val(kconf, _, name, unit=None):
|
|
|
|
"""
|
|
|
|
This function looks up 'name' in the DTS generated "conf" style database
|
2019-02-12 16:44:14 +08:00
|
|
|
(generated_dts_board.conf in <build_dir>/zephyr/include/generated/)
|
2019-02-09 05:20:21 +08:00
|
|
|
and if it's found it will return the value as an decimal integer. The
|
2018-11-16 01:51:50 +08:00
|
|
|
function will divide the value based on 'unit':
|
|
|
|
None No division
|
|
|
|
'k' or 'K' divide by 1024 (1 << 10)
|
|
|
|
'm' or 'M' divide by 1,048,576 (1 << 20)
|
|
|
|
'g' or 'G' divide by 1,073,741,824 (1 << 30)
|
|
|
|
"""
|
2019-01-31 00:54:21 +08:00
|
|
|
if doc_mode or name not in dt_defines:
|
2018-11-16 01:51:50 +08:00
|
|
|
return "0"
|
|
|
|
|
|
|
|
d = dt_defines[name]
|
|
|
|
if d.startswith(('0x', '0X')):
|
|
|
|
d = int(d, 16)
|
|
|
|
else:
|
|
|
|
d = int(d)
|
|
|
|
d >>= _dt_units_to_scale(unit)
|
|
|
|
|
|
|
|
return str(d)
|
|
|
|
|
|
|
|
def dt_hex_val(kconf, _, name, unit=None):
|
|
|
|
"""
|
|
|
|
This function looks up 'name' in the DTS generated "conf" style database
|
2019-02-12 16:44:14 +08:00
|
|
|
(generated_dts_board.conf in <build_dir>/zephyr/include/generated/)
|
2019-02-09 05:20:21 +08:00
|
|
|
and if it's found it will return the value as an hex integer. The
|
2018-11-16 01:51:50 +08:00
|
|
|
function will divide the value based on 'unit':
|
|
|
|
None No division
|
|
|
|
'k' or 'K' divide by 1024 (1 << 10)
|
|
|
|
'm' or 'M' divide by 1,048,576 (1 << 20)
|
|
|
|
'g' or 'G' divide by 1,073,741,824 (1 << 30)
|
|
|
|
"""
|
2019-01-31 00:54:21 +08:00
|
|
|
if doc_mode or name not in dt_defines:
|
2018-11-16 01:51:50 +08:00
|
|
|
return "0x0"
|
|
|
|
|
|
|
|
d = dt_defines[name]
|
|
|
|
if d.startswith(('0x', '0X')):
|
|
|
|
d = int(d, 16)
|
|
|
|
else:
|
|
|
|
d = int(d)
|
|
|
|
d >>= _dt_units_to_scale(unit)
|
|
|
|
|
|
|
|
return hex(d)
|
|
|
|
|
2019-02-08 22:25:21 +08:00
|
|
|
def dt_str_val(kconf, _, name):
|
|
|
|
"""
|
|
|
|
This function looks up 'name' in the DTS generated "conf" style database
|
2019-02-12 16:44:14 +08:00
|
|
|
(generated_dts_board.conf in <build_dir>/zephyr/include/generated/)
|
2019-02-09 05:20:21 +08:00
|
|
|
and if it's found it will return the value as string. If it's not found we
|
2019-02-09 00:59:49 +08:00
|
|
|
return an empty string.
|
2019-02-08 22:25:21 +08:00
|
|
|
"""
|
|
|
|
if doc_mode or name not in dt_defines:
|
|
|
|
return ""
|
|
|
|
|
2019-02-09 00:59:49 +08:00
|
|
|
return dt_defines[name].strip('"')
|
2018-11-16 01:51:50 +08:00
|
|
|
|
|
|
|
functions = {
|
|
|
|
"dt_int_val": (dt_int_val, 1, 2),
|
|
|
|
"dt_hex_val": (dt_hex_val, 1, 2),
|
2019-02-08 22:25:21 +08:00
|
|
|
"dt_str_val": (dt_str_val, 1, 1),
|
2018-11-16 01:51:50 +08:00
|
|
|
}
|