2018-01-05 02:19:51 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import sys, fnmatch
|
|
|
|
import importlib
|
|
|
|
|
|
|
|
# peripheral types that can be shared, wildcard patterns
|
2020-10-12 16:57:29 -03:00
|
|
|
SHARED_MAP = ["I2C*", "USART*_TX", "UART*_TX", "SPI*", "TIM*_UP", "TIM*_CH*"]
|
2018-01-05 02:19:51 -04:00
|
|
|
|
|
|
|
ignore_list = []
|
|
|
|
dma_map = None
|
|
|
|
|
|
|
|
debug = False
|
|
|
|
|
2021-03-01 17:52:11 -04:00
|
|
|
def check_possibility(periph, dma_stream, curr_dict, dma_map, check_list, cannot_use_stream, forbidden_map):
|
2019-02-14 06:18:13 -04:00
|
|
|
global ignore_list
|
2021-01-17 12:10:59 -04:00
|
|
|
if debug:
|
|
|
|
print('............ Checking ', periph, dma_stream, 'without', cannot_use_stream)
|
2020-10-07 18:46:48 -03:00
|
|
|
for other_periph in sorted(curr_dict.keys()):
|
2018-01-30 01:24:13 -04:00
|
|
|
if other_periph != periph:
|
|
|
|
if curr_dict[other_periph] == dma_stream:
|
2021-03-01 17:52:11 -04:00
|
|
|
if other_periph in forbidden_map[periph]:
|
|
|
|
if debug:
|
|
|
|
print('.................... Forbidden', periph, other_periph)
|
|
|
|
return False
|
2021-01-17 12:10:59 -04:00
|
|
|
if debug:
|
|
|
|
print('.................... Collision', other_periph, dma_stream)
|
2018-01-30 01:24:13 -04:00
|
|
|
ignore_list.append(periph)
|
|
|
|
check_str = "%s(%d,%d) %s(%d,%d)" % (
|
|
|
|
other_periph, curr_dict[other_periph][0],
|
|
|
|
curr_dict[other_periph][1], periph, dma_stream[0],
|
|
|
|
dma_stream[1])
|
|
|
|
#check if we did this before
|
|
|
|
if check_str in check_list:
|
|
|
|
return False
|
|
|
|
check_list.append(check_str)
|
|
|
|
if debug:
|
|
|
|
print("Trying to Resolve Conflict: ", check_str)
|
|
|
|
#check if we can resolve by swapping with other periphs
|
2018-03-13 21:28:14 -03:00
|
|
|
for streamchan in dma_map[other_periph]:
|
|
|
|
stream = (streamchan[0], streamchan[1])
|
2021-01-17 12:10:59 -04:00
|
|
|
|
|
|
|
if stream != curr_dict[other_periph] and \
|
|
|
|
stream not in cannot_use_stream and \
|
2021-03-01 17:52:11 -04:00
|
|
|
check_possibility(other_periph, stream, curr_dict, dma_map, check_list,
|
|
|
|
cannot_use_stream+[(dma_stream)], forbidden_map):
|
2021-01-17 12:10:59 -04:00
|
|
|
curr_dict[other_periph] = stream
|
|
|
|
if debug:
|
|
|
|
print ('....................... Resolving', other_periph, stream)
|
2018-01-30 01:24:13 -04:00
|
|
|
return True
|
2021-01-17 12:10:59 -04:00
|
|
|
if debug:
|
|
|
|
print ('....................... UnSolved !!!!!!!!', periph, dma_stream)
|
2018-01-30 01:24:13 -04:00
|
|
|
return False
|
2021-01-17 12:10:59 -04:00
|
|
|
if debug:
|
|
|
|
print ('....................... Solved ..........', periph, dma_stream)
|
2018-01-30 01:24:13 -04:00
|
|
|
return True
|
|
|
|
|
2018-02-08 07:13:00 -04:00
|
|
|
def can_share(periph, noshare_list):
|
2018-01-30 01:24:13 -04:00
|
|
|
'''check if a peripheral is in the SHARED_MAP list'''
|
2018-02-08 07:13:00 -04:00
|
|
|
for noshare in noshare_list:
|
|
|
|
if fnmatch.fnmatch(periph, noshare):
|
|
|
|
return False
|
2018-01-30 01:24:13 -04:00
|
|
|
for f in SHARED_MAP:
|
|
|
|
if fnmatch.fnmatch(periph, f):
|
|
|
|
return True
|
|
|
|
if debug:
|
|
|
|
print("%s can't share" % periph)
|
|
|
|
return False
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2019-02-19 22:55:18 -04:00
|
|
|
# list of peripherals on H7 that are on DMAMUX2 and BDMA
|
|
|
|
have_DMAMUX = False
|
|
|
|
DMAMUX2_peripherals = [ 'I2C4', 'SPI6', 'ADC3' ]
|
|
|
|
|
|
|
|
def dmamux_channel(key):
|
|
|
|
'''return DMAMUX channel for H7'''
|
|
|
|
for p in DMAMUX2_peripherals:
|
|
|
|
if key.find(p) != -1:
|
|
|
|
return 'STM32_DMAMUX2_' + key
|
|
|
|
# default to DMAMUX1
|
|
|
|
return 'STM32_DMAMUX1_' + key
|
|
|
|
|
|
|
|
def dma_name(key):
|
|
|
|
'''return 'DMA' or 'BDMA' based on peripheral name'''
|
|
|
|
if not have_DMAMUX:
|
|
|
|
return "DMA"
|
|
|
|
for p in DMAMUX2_peripherals:
|
|
|
|
if key.find(p) != -1:
|
|
|
|
return 'BDMA'
|
|
|
|
return 'DMA'
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
def chibios_dma_define_name(key):
|
2018-01-30 01:24:13 -04:00
|
|
|
'''return define name needed for board.h for ChibiOS'''
|
2019-02-19 22:55:18 -04:00
|
|
|
dma_key = key + '_' + dma_name(key)
|
2018-01-30 01:24:13 -04:00
|
|
|
if key.startswith('ADC'):
|
2019-02-19 22:55:18 -04:00
|
|
|
return 'STM32_ADC_%s_' % dma_key
|
2018-01-30 01:24:13 -04:00
|
|
|
elif key.startswith('SPI'):
|
2019-02-19 22:55:18 -04:00
|
|
|
return 'STM32_SPI_%s_' % dma_key
|
2018-01-30 01:24:13 -04:00
|
|
|
elif key.startswith('I2C'):
|
2019-02-19 22:55:18 -04:00
|
|
|
return 'STM32_I2C_%s_' % dma_key
|
2018-01-30 01:24:13 -04:00
|
|
|
elif key.startswith('USART'):
|
2019-02-19 22:55:18 -04:00
|
|
|
return 'STM32_UART_%s_' % dma_key
|
2018-01-30 01:24:13 -04:00
|
|
|
elif key.startswith('UART'):
|
2019-02-19 22:55:18 -04:00
|
|
|
return 'STM32_UART_%s_' % dma_key
|
2018-03-02 22:57:33 -04:00
|
|
|
elif key.startswith('SDIO') or key.startswith('SDMMC'):
|
2019-02-19 22:55:18 -04:00
|
|
|
return 'STM32_SDC_%s_' % dma_key
|
2018-01-30 01:24:13 -04:00
|
|
|
elif key.startswith('TIM'):
|
2019-02-19 22:55:18 -04:00
|
|
|
return 'STM32_TIM_%s_' % dma_key
|
2018-01-30 01:24:13 -04:00
|
|
|
else:
|
|
|
|
print("Error: Unknown key type %s" % key)
|
|
|
|
sys.exit(1)
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-02-08 07:13:00 -04:00
|
|
|
def get_list_index(peripheral, priority_list):
|
|
|
|
'''return index into priority_list for a peripheral'''
|
|
|
|
for i in range(len(priority_list)):
|
|
|
|
str = priority_list[i]
|
|
|
|
if fnmatch.fnmatch(peripheral, str):
|
|
|
|
return i
|
|
|
|
# default to max priority
|
|
|
|
return len(priority_list)
|
|
|
|
|
|
|
|
def get_sharing_priority(periph_list, priority_list):
|
|
|
|
'''get priority of a list of peripherals we could share with'''
|
|
|
|
highest = len(priority_list)
|
|
|
|
for p in periph_list:
|
|
|
|
prio = get_list_index(p, priority_list)
|
|
|
|
if prio < highest:
|
|
|
|
highest = prio
|
|
|
|
return highest
|
|
|
|
|
2021-03-07 23:24:38 -04:00
|
|
|
def generate_DMAMUX_map_mask(peripheral_list, channel_mask, noshare_list, dma_exclude, stream_ofs):
|
2019-02-14 06:18:13 -04:00
|
|
|
'''
|
|
|
|
generate a dma map suitable for a board with a DMAMUX
|
|
|
|
|
|
|
|
In principle any peripheral can use any stream, but we need to
|
|
|
|
ensure that a peripheral doesn't try to use the same stream as its
|
|
|
|
partner (eg. a RX/TX pair)
|
|
|
|
'''
|
|
|
|
dma_map = {}
|
|
|
|
idsets = {}
|
|
|
|
|
|
|
|
# first unshareable peripherals
|
2019-02-19 22:55:18 -04:00
|
|
|
available = channel_mask
|
2019-02-14 06:18:13 -04:00
|
|
|
for p in peripheral_list:
|
|
|
|
dma_map[p] = []
|
|
|
|
idsets[p] = set()
|
|
|
|
|
|
|
|
for p in peripheral_list:
|
2019-02-18 18:50:04 -04:00
|
|
|
if can_share(p, noshare_list) or p in dma_exclude:
|
2019-02-14 06:18:13 -04:00
|
|
|
continue
|
|
|
|
for i in range(16):
|
|
|
|
mask = (1<<i)
|
|
|
|
if available & mask != 0:
|
2019-02-18 18:50:04 -04:00
|
|
|
available &= ~mask
|
2019-02-14 06:18:13 -04:00
|
|
|
dma = (i // 8) + 1
|
|
|
|
stream = i % 8
|
2021-03-07 23:24:38 -04:00
|
|
|
dma_map[p].append((dma,stream))
|
2019-02-14 06:18:13 -04:00
|
|
|
idsets[p].add(i)
|
|
|
|
break
|
|
|
|
|
2019-02-18 18:50:04 -04:00
|
|
|
if debug:
|
|
|
|
print('dma_map1: ', dma_map)
|
2019-03-02 05:21:36 -04:00
|
|
|
print('available: 0x%04x' % available)
|
2019-02-18 18:50:04 -04:00
|
|
|
|
2019-02-14 06:18:13 -04:00
|
|
|
# now shareable
|
|
|
|
idx = 0
|
|
|
|
for p in peripheral_list:
|
2019-02-18 18:50:04 -04:00
|
|
|
if not can_share(p, noshare_list) or p in dma_exclude:
|
2019-02-14 06:18:13 -04:00
|
|
|
continue
|
|
|
|
base = idx % 16
|
|
|
|
for i in range(16):
|
|
|
|
found = None
|
2019-04-19 22:22:06 -03:00
|
|
|
for ii in list(range(base,16)) + list(range(0,base)):
|
2019-02-14 06:18:13 -04:00
|
|
|
if (1<<ii) & available == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
dma = (ii // 8) + 1
|
|
|
|
stream = ii % 8
|
|
|
|
|
|
|
|
if (dma,stream) in dma_map[p]:
|
2021-03-01 17:52:11 -04:00
|
|
|
# this peripheral is already using the stream
|
2019-02-14 06:18:13 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
# prevent attempts to share with other half of same peripheral
|
|
|
|
if p.endswith('RX'):
|
2020-12-30 08:08:16 -04:00
|
|
|
other = p[:-2] + 'TX'
|
2019-02-14 06:18:13 -04:00
|
|
|
elif p.endswith('TX'):
|
2020-12-30 08:08:16 -04:00
|
|
|
other = p[:-2] + 'RX'
|
|
|
|
else:
|
|
|
|
other = None
|
2019-02-14 06:18:13 -04:00
|
|
|
|
2021-03-11 19:30:17 -04:00
|
|
|
if other is not None and other in idsets and ii in idsets[other]:
|
2020-12-30 08:08:16 -04:00
|
|
|
if len(idsets[p]) >= len(idsets[other]) and len(idsets[other]) > 0:
|
2020-10-12 16:57:29 -03:00
|
|
|
continue
|
2020-12-30 08:08:16 -04:00
|
|
|
idsets[other].remove(ii)
|
|
|
|
dma_map[other].remove((dma,stream))
|
2019-02-14 06:18:13 -04:00
|
|
|
found = ii
|
|
|
|
break
|
|
|
|
if found is None:
|
|
|
|
continue
|
|
|
|
base = (found+1) % 16
|
|
|
|
dma = (found // 8) + 1
|
|
|
|
stream = found % 8
|
|
|
|
dma_map[p].append((dma,stream))
|
|
|
|
idsets[p].add(found)
|
|
|
|
idx = (idx+1) % 16
|
2021-03-07 23:24:38 -04:00
|
|
|
|
|
|
|
if stream_ofs != 0:
|
|
|
|
# add in stream_ofs to cope with STM32G4
|
|
|
|
for p in dma_map.keys():
|
|
|
|
for (dma,stream) in dma_map[p]:
|
|
|
|
map2 = []
|
|
|
|
map2.append((dma,stream+stream_ofs))
|
|
|
|
dma_map[p] = map2
|
|
|
|
|
2019-02-14 06:18:13 -04:00
|
|
|
if debug:
|
|
|
|
print('dma_map: ', dma_map)
|
|
|
|
print('idsets: ', idsets)
|
2019-03-02 05:21:36 -04:00
|
|
|
print('available: 0x%04x' % available)
|
2019-02-14 06:18:13 -04:00
|
|
|
return dma_map
|
|
|
|
|
2021-03-07 23:24:38 -04:00
|
|
|
def generate_DMAMUX_map(peripheral_list, noshare_list, dma_exclude, stream_ofs):
|
2019-02-19 22:55:18 -04:00
|
|
|
'''
|
|
|
|
generate a dma map suitable for a board with a DMAMUX1 and DMAMUX2
|
|
|
|
'''
|
|
|
|
# first split peripheral_list into those for DMAMUX1 and those for DMAMUX2
|
|
|
|
dmamux1_peripherals = []
|
|
|
|
dmamux2_peripherals = []
|
|
|
|
for p in peripheral_list:
|
|
|
|
if dma_name(p) == 'BDMA':
|
|
|
|
dmamux2_peripherals.append(p)
|
|
|
|
else:
|
|
|
|
dmamux1_peripherals.append(p)
|
2021-03-07 23:24:38 -04:00
|
|
|
map1 = generate_DMAMUX_map_mask(dmamux1_peripherals, 0xFFFF, noshare_list, dma_exclude, stream_ofs)
|
2021-08-24 02:18:31 -03:00
|
|
|
# there are 8 BDMA streams, but an issue has been found where if I2C4 and
|
|
|
|
# SPI6 use neighboring streams then we sometimes lose a BDMA completion
|
|
|
|
# interrupt. We also found that both ADC3 and SPI6_RX can't use the first
|
|
|
|
# stream. To avoid more complications we now statically allocate the BDMA
|
|
|
|
# streams for the 3 possible peripherals. To keep this code simpler we
|
|
|
|
# still have the mapping code here, but it ends not not being used and the
|
|
|
|
# static allocation is in stm32h7_mcuconf.h
|
|
|
|
map2 = generate_DMAMUX_map_mask(dmamux2_peripherals, 0xff, noshare_list, dma_exclude, stream_ofs)
|
2019-03-02 05:21:36 -04:00
|
|
|
# translate entries from map2 to "DMA controller 3", which is used for BDMA
|
2020-10-07 18:46:48 -03:00
|
|
|
for p in sorted(map2.keys()):
|
2019-03-02 05:21:36 -04:00
|
|
|
streams = []
|
|
|
|
for (controller,stream) in map2[p]:
|
|
|
|
streams.append((3,stream))
|
|
|
|
map2[p] = streams
|
2019-02-19 22:55:18 -04:00
|
|
|
both = map1
|
|
|
|
both.update(map2)
|
|
|
|
if debug:
|
|
|
|
print('dma_map_both: ', both)
|
|
|
|
return both
|
|
|
|
|
2021-03-01 17:52:11 -04:00
|
|
|
def sharing_allowed(p1, p2):
|
|
|
|
'''return true if sharing is allowed between p1 and p2'''
|
|
|
|
if p1 == p2:
|
|
|
|
return True
|
|
|
|
# don't allow RX and TX of same peripheral to share
|
|
|
|
if p1.endswith('_RX') and p2.endswith('_TX') and p1[:-2] == p2[:-2]:
|
|
|
|
return False
|
|
|
|
# don't allow sharing of two TIMn_UP channels as DShot code can't cope
|
|
|
|
if p1.endswith("_UP") and p2.endswith("_UP") and p1.startswith("TIM") and p2.startswith("TIM"):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def check_sharing(shared):
|
|
|
|
'''check if DMA channel sharing is OK'''
|
|
|
|
for p in shared:
|
|
|
|
# don't share UART RX with anything
|
|
|
|
if (p.startswith("UART") or p.startswith("USART")) and p.endswith("_RX"):
|
|
|
|
print("Illegal sharing of %s" % p)
|
|
|
|
return False
|
|
|
|
|
|
|
|
# don't share ADC with anything
|
|
|
|
if p.startswith("ADC"):
|
|
|
|
print("Illegal sharing of %s" % p)
|
|
|
|
return False
|
|
|
|
|
|
|
|
for p2 in shared:
|
|
|
|
if not sharing_allowed(p, p2):
|
|
|
|
print("Illegal sharing of %s and %s" % (p, p2))
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def forbidden_list(p, peripheral_list):
|
|
|
|
'''given a peripheral, form a list of other peripherals we may not share with'''
|
|
|
|
ret = []
|
|
|
|
for p2 in peripheral_list:
|
|
|
|
if not sharing_allowed(p, p2):
|
|
|
|
ret.append(p2)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2018-02-08 07:13:00 -04:00
|
|
|
def write_dma_header(f, peripheral_list, mcu_type, dma_exclude=[],
|
2023-05-24 22:27:45 -03:00
|
|
|
dma_priority='', dma_noshare=[], quiet=False):
|
2018-01-30 01:24:13 -04:00
|
|
|
'''write out a DMA resolver header file'''
|
2020-12-30 08:08:16 -04:00
|
|
|
global dma_map, have_DMAMUX, has_bdshot
|
|
|
|
timer_ch_periph = []
|
|
|
|
|
|
|
|
has_bdshot = False
|
2018-02-08 07:13:00 -04:00
|
|
|
|
|
|
|
# form a list of DMA priorities
|
|
|
|
priority_list = dma_priority.split()
|
|
|
|
|
|
|
|
# sort by priority
|
|
|
|
peripheral_list = sorted(peripheral_list, key=lambda x: get_list_index(x, priority_list))
|
|
|
|
|
|
|
|
# form a list of peripherals that can't share
|
2021-03-16 04:28:25 -03:00
|
|
|
noshare_list = dma_noshare[:]
|
2018-02-08 07:13:00 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
try:
|
|
|
|
lib = importlib.import_module(mcu_type)
|
2018-08-29 10:18:55 -03:00
|
|
|
if hasattr(lib, "DMA_Map"):
|
|
|
|
dma_map = lib.DMA_Map
|
|
|
|
else:
|
2021-02-28 12:08:27 -04:00
|
|
|
return [], []
|
2018-01-30 01:24:13 -04:00
|
|
|
except ImportError:
|
|
|
|
print("Unable to find module for MCU %s" % mcu_type)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-02-14 06:18:13 -04:00
|
|
|
if dma_map is None:
|
|
|
|
have_DMAMUX = True
|
2020-12-30 08:08:16 -04:00
|
|
|
# ensure we don't assign dma for TIMx_CH as we share that with TIMx_UP
|
|
|
|
timer_ch_periph = [periph for periph in peripheral_list if "_CH" in periph]
|
|
|
|
dma_exclude += timer_ch_periph
|
2021-03-07 23:24:38 -04:00
|
|
|
if mcu_type.startswith("STM32G4"):
|
|
|
|
stream_ofs = 1
|
|
|
|
else:
|
|
|
|
stream_ofs = 0
|
|
|
|
|
|
|
|
dma_map = generate_DMAMUX_map(peripheral_list, noshare_list, dma_exclude, stream_ofs)
|
2019-02-14 06:18:13 -04:00
|
|
|
|
2023-05-24 22:27:45 -03:00
|
|
|
if not quiet:
|
|
|
|
print("Writing DMA map")
|
2018-01-30 01:24:13 -04:00
|
|
|
unassigned = []
|
|
|
|
curr_dict = {}
|
|
|
|
|
2021-03-01 17:52:11 -04:00
|
|
|
# build a map from peripheral name to a list of peripherals that it cannot share with
|
|
|
|
forbidden_map = {}
|
|
|
|
for p in peripheral_list:
|
|
|
|
forbidden_map[p] = forbidden_list(p, peripheral_list)
|
|
|
|
|
2023-09-01 13:51:37 -03:00
|
|
|
# force sharing of TIMx_UP and TIMx_CHy if possible
|
|
|
|
periphs = peripheral_list.copy()
|
|
|
|
forbidden_streams = []
|
2018-01-30 01:24:13 -04:00
|
|
|
for periph in peripheral_list:
|
2023-09-01 13:51:37 -03:00
|
|
|
if "_UP" in periph:
|
|
|
|
for periph2 in peripheral_list:
|
|
|
|
if "_CH" in periph2 and periph[:4] == periph2[:4]:
|
|
|
|
shared_channels = [value for value in dma_map[periph] if value in dma_map[periph2]]
|
|
|
|
if len(shared_channels) > 0:
|
|
|
|
stream = (shared_channels[0][0], shared_channels[0][1])
|
|
|
|
curr_dict[periph] = stream
|
|
|
|
curr_dict[periph2] = stream
|
|
|
|
forbidden_streams.append(stream)
|
|
|
|
periphs.remove(periph)
|
|
|
|
periphs.remove(periph2)
|
|
|
|
print("Sharing channel %s for %s %s" % (stream, periph, periph2))
|
|
|
|
|
|
|
|
for periph in periphs:
|
2020-12-30 08:08:16 -04:00
|
|
|
if "_CH" in periph:
|
|
|
|
has_bdshot = True # the list contains a CH port
|
2018-02-06 03:20:09 -04:00
|
|
|
if periph in dma_exclude:
|
|
|
|
continue
|
2018-01-30 01:24:13 -04:00
|
|
|
assigned = False
|
|
|
|
check_list = []
|
|
|
|
if not periph in dma_map:
|
|
|
|
print("Unknown peripheral function %s in DMA map for %s" %
|
|
|
|
(periph, mcu_type))
|
|
|
|
sys.exit(1)
|
2021-01-17 12:10:59 -04:00
|
|
|
|
|
|
|
if debug:
|
|
|
|
print('\n\n.....Starting lookup for', periph)
|
2018-03-13 21:28:14 -03:00
|
|
|
for streamchan in dma_map[periph]:
|
2021-01-17 12:10:59 -04:00
|
|
|
if debug:
|
|
|
|
print('........Possibility for', periph, streamchan)
|
2018-03-13 21:28:14 -03:00
|
|
|
stream = (streamchan[0], streamchan[1])
|
2018-01-30 01:24:13 -04:00
|
|
|
if check_possibility(periph, stream, curr_dict, dma_map,
|
2023-09-01 13:51:37 -03:00
|
|
|
check_list, forbidden_streams, forbidden_map):
|
2018-01-30 01:24:13 -04:00
|
|
|
curr_dict[periph] = stream
|
2021-01-17 12:10:59 -04:00
|
|
|
if debug:
|
|
|
|
print ('....................... Setting', periph, stream)
|
2018-01-30 01:24:13 -04:00
|
|
|
assigned = True
|
|
|
|
break
|
|
|
|
if assigned == False:
|
|
|
|
unassigned.append(periph)
|
|
|
|
|
2019-02-14 06:18:13 -04:00
|
|
|
if debug:
|
|
|
|
print('curr_dict: ', curr_dict)
|
|
|
|
print('unassigned: ', unassigned)
|
|
|
|
|
2018-02-08 07:13:00 -04:00
|
|
|
# now look for shared DMA possibilities
|
2018-01-30 01:24:13 -04:00
|
|
|
stream_assign = {}
|
2020-10-07 18:46:48 -03:00
|
|
|
for k in sorted(curr_dict.keys()):
|
2019-02-14 06:18:13 -04:00
|
|
|
p = curr_dict[k]
|
|
|
|
if not p in stream_assign:
|
|
|
|
stream_assign[p] = [k]
|
|
|
|
else:
|
|
|
|
stream_assign[p].append(k)
|
2018-01-30 01:24:13 -04:00
|
|
|
|
|
|
|
unassigned_new = unassigned[:]
|
|
|
|
for periph in unassigned:
|
2018-02-08 07:13:00 -04:00
|
|
|
share_possibility = []
|
2018-03-13 21:28:14 -03:00
|
|
|
for streamchan in dma_map[periph]:
|
|
|
|
stream = (streamchan[0], streamchan[1])
|
2018-01-30 01:24:13 -04:00
|
|
|
share_ok = True
|
|
|
|
for periph2 in stream_assign[stream]:
|
2021-03-01 17:52:11 -04:00
|
|
|
if not can_share(periph, noshare_list) or not can_share(periph2, noshare_list) or periph2 in forbidden_map[periph]:
|
2018-01-30 01:24:13 -04:00
|
|
|
share_ok = False
|
|
|
|
if share_ok:
|
2018-02-08 07:13:00 -04:00
|
|
|
share_possibility.append(stream)
|
|
|
|
if share_possibility:
|
|
|
|
# sort the possible sharings so minimise impact on high priority streams
|
|
|
|
share_possibility = sorted(share_possibility, key=lambda x: get_sharing_priority(stream_assign[x], priority_list))
|
|
|
|
# and take the one with the least impact (lowest value for highest priority stream share)
|
|
|
|
stream = share_possibility[-1]
|
|
|
|
if debug:
|
|
|
|
print("Sharing %s on %s with %s" % (periph, stream,
|
|
|
|
stream_assign[stream]))
|
|
|
|
curr_dict[periph] = stream
|
|
|
|
stream_assign[stream].append(periph)
|
|
|
|
unassigned_new.remove(periph)
|
2018-01-30 01:24:13 -04:00
|
|
|
unassigned = unassigned_new
|
|
|
|
|
2021-03-01 17:52:11 -04:00
|
|
|
for key in sorted(curr_dict.keys()):
|
|
|
|
stream = curr_dict[key]
|
|
|
|
if len(stream_assign[stream]) > 1:
|
|
|
|
if not check_sharing(stream_assign[stream]):
|
|
|
|
sys.exit(1)
|
|
|
|
|
2019-02-14 06:18:13 -04:00
|
|
|
if debug:
|
|
|
|
print(stream_assign)
|
|
|
|
|
2018-03-13 21:28:14 -03:00
|
|
|
f.write("\n\n// auto-generated DMA mapping from dma_resolver.py\n")
|
2018-01-30 01:24:13 -04:00
|
|
|
|
|
|
|
if unassigned:
|
|
|
|
f.write(
|
|
|
|
"\n// Note: The following peripherals can't be resolved for DMA: %s\n\n"
|
|
|
|
% unassigned)
|
|
|
|
|
2021-02-28 12:01:01 -04:00
|
|
|
ordered_up_channels = []
|
|
|
|
|
|
|
|
# produce a list of timers ordered by the DMA streamid of the UP channel
|
|
|
|
# this is so that the dshot code can take out the UP DMA locks in the same order as I2C and SPI
|
|
|
|
for key in curr_dict.keys():
|
|
|
|
if "_UP" in key:
|
|
|
|
ordered_up_channels.append(key)
|
|
|
|
|
|
|
|
def order_by_streamid(key):
|
|
|
|
stream = curr_dict[key]
|
|
|
|
return (stream[0] * 8 + stream[1]) * 20 + int(key[3:-3])
|
|
|
|
|
|
|
|
ordered_up_channels = sorted(ordered_up_channels, key=order_by_streamid)
|
|
|
|
ordered_timers = []
|
|
|
|
for key in ordered_up_channels:
|
|
|
|
ordered_timers.append(key[0:-3])
|
|
|
|
|
2021-08-27 21:58:18 -03:00
|
|
|
shared_set = set()
|
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
for key in sorted(curr_dict.keys()):
|
|
|
|
stream = curr_dict[key]
|
|
|
|
shared = ''
|
|
|
|
if len(stream_assign[stream]) > 1:
|
|
|
|
shared = ' // shared %s' % ','.join(stream_assign[stream])
|
2021-08-27 21:58:18 -03:00
|
|
|
if stream[0] in [1,2]:
|
|
|
|
shared_set.add("(1U<<STM32_DMA_STREAM_ID(%u,%u))" % (stream[0],stream[1]))
|
2019-02-06 17:10:40 -04:00
|
|
|
if curr_dict[key] == "STM32_DMA_STREAM_ID_ANY":
|
|
|
|
f.write("#define %-30s STM32_DMA_STREAM_ID_ANY\n" % (chibios_dma_define_name(key)+'STREAM'))
|
2019-02-19 22:55:18 -04:00
|
|
|
f.write("#define %-30s %s\n" % (chibios_dma_define_name(key)+'CHAN', dmamux_channel(key)))
|
2019-02-06 17:10:40 -04:00
|
|
|
continue
|
|
|
|
else:
|
2019-03-02 05:21:36 -04:00
|
|
|
dma_controller = curr_dict[key][0]
|
|
|
|
if dma_controller == 3:
|
2021-08-24 02:18:31 -03:00
|
|
|
# BDMA resources turn out to be very strange on H743. For now
|
|
|
|
# we will skip trying to allocate them automatically and
|
|
|
|
# instead rely on allocation in stm32h7_mcuconf.h.
|
2021-08-20 21:19:16 -03:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
f.write("#define %-30s STM32_DMA_STREAM_ID(%u, %u)%s\n" %
|
|
|
|
(chibios_dma_define_name(key)+'STREAM', dma_controller,
|
|
|
|
curr_dict[key][1], shared))
|
2020-12-30 08:08:16 -04:00
|
|
|
if have_DMAMUX and "_UP" in key:
|
|
|
|
# share the dma with rest of the _CH ports
|
|
|
|
for ch in range(1,5):
|
|
|
|
chkey = key.replace('_UP', '_CH{}'.format(ch))
|
|
|
|
if chkey not in timer_ch_periph:
|
|
|
|
continue
|
|
|
|
f.write("#define %-30s STM32_DMA_STREAM_ID(%u, %u)%s\n" %
|
|
|
|
(chibios_dma_define_name(chkey)+'STREAM', dma_controller,
|
|
|
|
curr_dict[key][1], shared))
|
2020-10-07 18:46:48 -03:00
|
|
|
for streamchan in sorted(dma_map[key]):
|
2018-03-13 21:28:14 -03:00
|
|
|
if stream == (streamchan[0], streamchan[1]):
|
2019-02-14 06:18:13 -04:00
|
|
|
if have_DMAMUX:
|
2019-02-19 22:55:18 -04:00
|
|
|
chan = dmamux_channel(key)
|
2019-02-14 06:18:13 -04:00
|
|
|
else:
|
|
|
|
chan = streamchan[2]
|
|
|
|
f.write("#define %-30s %s\n" %
|
|
|
|
(chibios_dma_define_name(key)+'CHAN', chan))
|
2020-12-30 08:08:16 -04:00
|
|
|
if have_DMAMUX and "_UP" in key:
|
|
|
|
# share the devid with rest of the _CH ports
|
|
|
|
for ch in range(1,5):
|
|
|
|
chkey = key.replace('_UP', '_CH{}'.format(ch))
|
|
|
|
if chkey not in timer_ch_periph:
|
|
|
|
continue
|
|
|
|
f.write("#define %-30s %s\n" %
|
|
|
|
(chibios_dma_define_name(chkey)+'CHAN',
|
|
|
|
chan.replace('_UP', '_CH{}'.format(ch))))
|
2018-03-13 21:28:14 -03:00
|
|
|
break
|
2018-01-30 01:24:13 -04:00
|
|
|
|
2021-08-27 21:58:18 -03:00
|
|
|
f.write("\n// Mask of DMA streams which are shared\n")
|
|
|
|
if len(shared_set) == 0:
|
|
|
|
f.write("#define SHARED_DMA_MASK 0\n")
|
|
|
|
else:
|
2021-12-12 19:16:34 -04:00
|
|
|
f.write("#define SHARED_DMA_MASK (%s)\n" % '|'.join(sorted(list(shared_set))))
|
2021-08-27 21:58:18 -03:00
|
|
|
|
2019-02-20 02:17:38 -04:00
|
|
|
# now generate UARTDriver.cpp DMA config lines
|
2018-01-30 01:24:13 -04:00
|
|
|
f.write("\n\n// generated UART DMA configuration lines\n")
|
|
|
|
for u in range(1, 9):
|
|
|
|
key = None
|
|
|
|
if 'USART%u_TX' % u in peripheral_list:
|
|
|
|
key = 'USART%u' % u
|
|
|
|
if 'UART%u_TX' % u in peripheral_list:
|
|
|
|
key = 'UART%u' % u
|
|
|
|
if 'USART%u_RX' % u in peripheral_list:
|
|
|
|
key = 'USART%u' % u
|
|
|
|
if 'UART%u_RX' % u in peripheral_list:
|
|
|
|
key = 'UART%u' % u
|
|
|
|
if key is None:
|
|
|
|
continue
|
2019-02-14 06:18:13 -04:00
|
|
|
if have_DMAMUX:
|
2019-02-13 19:13:00 -04:00
|
|
|
# use DMAMUX ID as channel number
|
2019-02-19 22:55:18 -04:00
|
|
|
dma_rx_chn = dmamux_channel(key + "_RX")
|
|
|
|
dma_tx_chn = dmamux_channel(key + "_TX")
|
2019-02-06 17:10:40 -04:00
|
|
|
else:
|
2019-02-19 22:55:18 -04:00
|
|
|
dma_rx_chn = "STM32_UART_%s_RX_%s_CHAN" % (key, dma_name(key))
|
|
|
|
dma_tx_chn = "STM32_UART_%s_TX_%s_CHAN" % (key, dma_name(key))
|
2019-02-06 17:10:40 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
f.write("#define STM32_%s_RX_DMA_CONFIG " % key)
|
|
|
|
if key + "_RX" in curr_dict:
|
|
|
|
f.write(
|
2019-02-19 22:55:18 -04:00
|
|
|
"true, STM32_UART_%s_RX_%s_STREAM, %s\n" % (key, dma_name(key), dma_rx_chn))
|
2018-01-30 01:24:13 -04:00
|
|
|
else:
|
|
|
|
f.write("false, 0, 0\n")
|
|
|
|
f.write("#define STM32_%s_TX_DMA_CONFIG " % key)
|
|
|
|
if key + "_TX" in curr_dict:
|
|
|
|
f.write(
|
2019-02-19 22:55:18 -04:00
|
|
|
"true, STM32_UART_%s_TX_%s_STREAM, %s\n" % (key, dma_name(key), dma_tx_chn))
|
2018-01-30 01:24:13 -04:00
|
|
|
else:
|
|
|
|
f.write("false, 0, 0\n")
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2019-02-20 02:17:38 -04:00
|
|
|
# now generate SPI DMA streams lines
|
|
|
|
f.write("\n\n// generated SPI DMA configuration lines\n")
|
|
|
|
for u in range(1, 9):
|
|
|
|
if 'SPI%u_TX' % u in peripheral_list and 'SPI%u_RX' % u in peripheral_list:
|
|
|
|
key = 'SPI%u' % u
|
|
|
|
else:
|
|
|
|
continue
|
2021-08-24 02:18:31 -03:00
|
|
|
if dma_name(key) == 'BDMA':
|
|
|
|
# we use SHARED_DMA_NONE for SPI6 on H7 as we don't need to lock the stream
|
|
|
|
# as it is never shared
|
|
|
|
f.write('#define STM32_SPI_%s_DMA_STREAMS SHARED_DMA_NONE, SHARED_DMA_NONE\n' % key)
|
|
|
|
else:
|
|
|
|
f.write('#define STM32_SPI_%s_DMA_STREAMS STM32_SPI_%s_TX_%s_STREAM, STM32_SPI_%s_RX_%s_STREAM\n' % (
|
|
|
|
key, key, dma_name(key), key, dma_name(key)))
|
2021-02-28 12:01:01 -04:00
|
|
|
return unassigned, ordered_timers
|
2019-02-20 02:17:38 -04:00
|
|
|
|
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
if __name__ == '__main__':
|
2018-01-30 01:24:13 -04:00
|
|
|
import optparse
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
parser = optparse.OptionParser("dma_resolver.py")
|
|
|
|
parser.add_option("-M", "--mcu", default=None, help='MCU type')
|
|
|
|
parser.add_option(
|
|
|
|
"-D", "--debug", action='store_true', help='enable debug')
|
|
|
|
parser.add_option(
|
|
|
|
"-P",
|
|
|
|
"--peripherals",
|
|
|
|
default=None,
|
|
|
|
help='peripheral list (comma separated)')
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
opts, args = parser.parse_args()
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
if opts.peripherals is None:
|
|
|
|
print("Please provide a peripheral list with -P")
|
|
|
|
sys.exit(1)
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
if opts.mcu is None:
|
|
|
|
print("Please provide a MCU type with -<")
|
|
|
|
sys.exit(1)
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
debug = opts.debug
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
plist = opts.peripherals.split(',')
|
|
|
|
mcu_type = opts.mcu
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-01-30 01:24:13 -04:00
|
|
|
f = open("dma.h", "w")
|
|
|
|
write_dma_header(f, plist, mcu_type)
|
2018-02-06 03:20:09 -04:00
|
|
|
|