mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-13 11:29:58 +03:00
Optimize DMA option index conversion and validation
This commit is contained in:
parent
f3b5d47b4f
commit
80fdbc17fc
6 changed files with 78 additions and 36 deletions
|
@ -5415,18 +5415,38 @@ static void optToString(int optval, char *buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getDmaOptUiIndex(dmaoptEntry_t *entry, int index)
|
static int getDmaOptDisplayNumber(dmaoptEntry_t *entry, int index)
|
||||||
{
|
{
|
||||||
if (entry->presenceMask) {
|
if (entry->peripheral == DMA_PERIPH_TIMUP) {
|
||||||
return timerGetNumberByIndex(index);
|
int uiIndex = timerGetNumberByIndex(index);
|
||||||
|
if (!(TIM_N(uiIndex) & entry->presenceMask)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return uiIndex;
|
||||||
}
|
}
|
||||||
return DMA_OPT_UI_INDEX(index);
|
return DMA_OPT_UI_INDEX(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int transDmaoptIndex(dmaoptEntry_t *entry, int index)
|
static int displayNumberToDmaOptIndex(dmaoptEntry_t *entry, int index)
|
||||||
{
|
{
|
||||||
if (entry->presenceMask) {
|
if (index <= 0) {
|
||||||
return timerGetNumberByIndex(index) - 1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entry->peripheral == DMA_PERIPH_TIMUP) {
|
||||||
|
if (!(entry->presenceMask & TIM_N(index))) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return timerGetIndexByNumber(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
return index > entry->maxIndex ? -1 : index - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int dmaOptIndexToPeripheralMappingIndex(dmaoptEntry_t *entry, int index)
|
||||||
|
{
|
||||||
|
if (entry->peripheral == DMA_PERIPH_TIMUP) {
|
||||||
|
return timerGetNumberByIndex(index) - 1; // TIM1 = 0, TIM20 = 19
|
||||||
}
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
|
@ -5438,23 +5458,17 @@ static void printPeripheralDmaoptDetails(dmaoptEntry_t *entry, int index, const
|
||||||
// Note that using timerGetNumberByIndex is not a generic solution,
|
// Note that using timerGetNumberByIndex is not a generic solution,
|
||||||
// but we are lucky that TIMUP is the only peripheral with non-contiguous numbering.
|
// but we are lucky that TIMUP is the only peripheral with non-contiguous numbering.
|
||||||
|
|
||||||
int uiIndex;
|
int uiIndex = getDmaOptDisplayNumber(entry, index);
|
||||||
|
if (uiIndex == -1) {
|
||||||
if (entry->presenceMask) {
|
|
||||||
uiIndex = timerGetNumberByIndex(index);
|
|
||||||
if (!(TIM_N(uiIndex) & entry->presenceMask)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
uiIndex = DMA_OPT_UI_INDEX(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (dmaopt != DMA_OPT_UNUSED) {
|
if (dmaopt != DMA_OPT_UNUSED) {
|
||||||
printValue(dumpMask, equalsDefault,
|
printValue(dumpMask, equalsDefault,
|
||||||
"dma %s %d %d",
|
"dma %s %d %d",
|
||||||
entry->device, uiIndex, dmaopt);
|
entry->device, uiIndex, dmaopt);
|
||||||
|
|
||||||
const dmaChannelSpec_t *dmaChannelSpec = dmaGetChannelSpecByPeripheral(entry->peripheral, transDmaoptIndex(entry, index), dmaopt);
|
const dmaChannelSpec_t *dmaChannelSpec = dmaGetChannelSpecByPeripheral(entry->peripheral, dmaOptIndexToPeripheralMappingIndex(entry, index), dmaopt);
|
||||||
dmaCode_t dmaCode = 0;
|
dmaCode_t dmaCode = 0;
|
||||||
if (dmaChannelSpec) {
|
if (dmaChannelSpec) {
|
||||||
dmaCode = dmaChannelSpec->code;
|
dmaCode = dmaChannelSpec->code;
|
||||||
|
@ -5656,21 +5670,8 @@ static void cliDmaopt(const char *cmdName, char *cmdline)
|
||||||
const timerHardware_t *timer = NULL;
|
const timerHardware_t *timer = NULL;
|
||||||
pch = strtok_r(NULL, " ", &saveptr);
|
pch = strtok_r(NULL, " ", &saveptr);
|
||||||
if (entry) {
|
if (entry) {
|
||||||
bool isIndexValid = true;
|
index = displayNumberToDmaOptIndex(entry, pch ? atoi(pch) : -1);
|
||||||
index = pch ? (atoi(pch) - 1) : -1;
|
if (index == -1) {
|
||||||
if (entry->presenceMask) {
|
|
||||||
if (!(entry->presenceMask & TIM_N(index + 1))) {
|
|
||||||
isIndexValid = false;
|
|
||||||
} else {
|
|
||||||
index = TIMER_INDEX(index + 1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (index < 0 || index >= entry->maxIndex) {
|
|
||||||
isIndexValid = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isIndexValid) {
|
|
||||||
cliPrintErrorLinef(cmdName, "BAD INDEX: '%s'", pch ? pch : "");
|
cliPrintErrorLinef(cmdName, "BAD INDEX: '%s'", pch ? pch : "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -5716,7 +5717,7 @@ static void cliDmaopt(const char *cmdName, char *cmdline)
|
||||||
// Show possible opts
|
// Show possible opts
|
||||||
const dmaChannelSpec_t *dmaChannelSpec;
|
const dmaChannelSpec_t *dmaChannelSpec;
|
||||||
if (entry) {
|
if (entry) {
|
||||||
for (int opt = 0; (dmaChannelSpec = dmaGetChannelSpecByPeripheral(entry->peripheral, transDmaoptIndex(entry, index), opt)); opt++) {
|
for (int opt = 0; (dmaChannelSpec = dmaGetChannelSpecByPeripheral(entry->peripheral, dmaOptIndexToPeripheralMappingIndex(entry, index), opt)); opt++) {
|
||||||
cliPrintLinef("# %d: " DMASPEC_FORMAT_STRING, opt, DMA_CODE_CONTROLLER(dmaChannelSpec->code), DMA_CODE_STREAM(dmaChannelSpec->code), DMA_CODE_CHANNEL(dmaChannelSpec->code));
|
cliPrintLinef("# %d: " DMASPEC_FORMAT_STRING, opt, DMA_CODE_CONTROLLER(dmaChannelSpec->code), DMA_CODE_STREAM(dmaChannelSpec->code), DMA_CODE_CHANNEL(dmaChannelSpec->code));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -5734,8 +5735,8 @@ static void cliDmaopt(const char *cmdName, char *cmdline)
|
||||||
optval = atoi(pch);
|
optval = atoi(pch);
|
||||||
|
|
||||||
if (entry) {
|
if (entry) {
|
||||||
if (!dmaGetChannelSpecByPeripheral(entry->peripheral, transDmaoptIndex(entry, index), optval)) {
|
if (!dmaGetChannelSpecByPeripheral(entry->peripheral, dmaOptIndexToPeripheralMappingIndex(entry, index), optval)) {
|
||||||
cliPrintErrorLinef(cmdName, "INVALID DMA OPTION FOR %s %d: '%s'", entry->device, getDmaOptUiIndex(entry, index), pch);
|
cliPrintErrorLinef(cmdName, "INVALID DMA OPTION FOR %s %d: '%s'", entry->device, getDmaOptDisplayNumber(entry, index), pch);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -5758,7 +5759,7 @@ static void cliDmaopt(const char *cmdName, char *cmdline)
|
||||||
if (entry) {
|
if (entry) {
|
||||||
*optaddr = optval;
|
*optaddr = optval;
|
||||||
|
|
||||||
cliPrintLinef("# dma %s %d: changed from %s to %s", entry->device, getDmaOptUiIndex(entry, index), orgvalString, optvalString);
|
cliPrintLinef("# dma %s %d: changed from %s to %s", entry->device, getDmaOptDisplayNumber(entry, index), orgvalString, optvalString);
|
||||||
} else {
|
} else {
|
||||||
#if defined(USE_TIMER_MGMT)
|
#if defined(USE_TIMER_MGMT)
|
||||||
timerIoConfig->dmaopt = optval;
|
timerIoConfig->dmaopt = optval;
|
||||||
|
@ -5768,7 +5769,7 @@ static void cliDmaopt(const char *cmdName, char *cmdline)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (entry) {
|
if (entry) {
|
||||||
cliPrintLinef("# dma %s %d: no change: %s", entry->device, getDmaOptUiIndex(entry, index), orgvalString);
|
cliPrintLinef("# dma %s %d: no change: %s", entry->device, getDmaOptDisplayNumber(entry, index), orgvalString);
|
||||||
} else {
|
} else {
|
||||||
cliPrintLinef("# dma %c%02d: no change: %s", IO_GPIOPortIdxByTag(ioTag) + 'A', IO_GPIOPinIdxByTag(ioTag),orgvalString);
|
cliPrintLinef("# dma %c%02d: no change: %s", IO_GPIOPortIdxByTag(ioTag) + 'A', IO_GPIOPinIdxByTag(ioTag),orgvalString);
|
||||||
}
|
}
|
||||||
|
|
|
@ -224,6 +224,7 @@ uint16_t timerGetPrescalerByDesiredMhz(TIM_TypeDef *tim, uint16_t mhz);
|
||||||
uint16_t timerGetPeriodByPrescaler(TIM_TypeDef *tim, uint16_t prescaler, uint32_t hz);
|
uint16_t timerGetPeriodByPrescaler(TIM_TypeDef *tim, uint16_t prescaler, uint32_t hz);
|
||||||
|
|
||||||
int8_t timerGetNumberByIndex(uint8_t index);
|
int8_t timerGetNumberByIndex(uint8_t index);
|
||||||
|
int8_t timerGetIndexByNumber(uint8_t number);
|
||||||
int8_t timerGetTIMNumber(const TIM_TypeDef *tim);
|
int8_t timerGetTIMNumber(const TIM_TypeDef *tim);
|
||||||
uint8_t timerLookupChannelIndex(const uint16_t channel);
|
uint8_t timerLookupChannelIndex(const uint16_t channel);
|
||||||
|
|
||||||
|
|
|
@ -284,6 +284,16 @@ int8_t timerGetNumberByIndex(uint8_t index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t timerGetIndexByNumber(uint8_t number)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < USED_TIMER_COUNT; i++) {
|
||||||
|
if (timerNumbers[i] == number) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int8_t timerGetTIMNumber(const TMR_TypeDef *tim)
|
int8_t timerGetTIMNumber(const TMR_TypeDef *tim)
|
||||||
{
|
{
|
||||||
uint8_t index = lookupTimerIndex(tim);
|
uint8_t index = lookupTimerIndex(tim);
|
||||||
|
|
|
@ -283,6 +283,16 @@ int8_t timerGetNumberByIndex(uint8_t index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t timerGetIndexByNumber(uint8_t number)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < USED_TIMER_COUNT; i++) {
|
||||||
|
if (timerNumbers[i] == number) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int8_t timerGetTIMNumber(const tmr_type *tim)
|
int8_t timerGetTIMNumber(const tmr_type *tim)
|
||||||
{
|
{
|
||||||
const uint8_t index = lookupTimerIndex(tim);
|
const uint8_t index = lookupTimerIndex(tim);
|
||||||
|
|
|
@ -292,6 +292,16 @@ int8_t timerGetNumberByIndex(uint8_t index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t timerGetIndexByNumber(uint8_t number)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < USED_TIMER_COUNT; i++) {
|
||||||
|
if (timerNumbers[i] == number) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
|
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
|
||||||
{
|
{
|
||||||
uint8_t index = lookupTimerIndex(tim);
|
uint8_t index = lookupTimerIndex(tim);
|
||||||
|
|
|
@ -274,6 +274,16 @@ int8_t timerGetNumberByIndex(uint8_t index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int8_t timerGetIndexByNumber(uint8_t number)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < USED_TIMER_COUNT; i++) {
|
||||||
|
if (timerNumbers[i] == number) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
|
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
|
||||||
{
|
{
|
||||||
const uint8_t index = lookupTimerIndex(tim);
|
const uint8_t index = lookupTimerIndex(tim);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue