Added 'timer show' command to CLI.

This commit is contained in:
mikeller 2019-08-05 19:39:26 +12:00
parent 025ff572b6
commit 6a0c5836de
16 changed files with 217 additions and 125 deletions

View File

@ -4892,7 +4892,7 @@ static bool strToPin(char *pch, ioTag_t *tag)
}
#ifdef USE_DMA
static void printDma(void)
static void showDma(void)
{
cliPrintLinefeed();
@ -5304,7 +5304,7 @@ static void cliDma(char* cmdline)
{
int len = strlen(cmdline);
if (len && strncasecmp(cmdline, "show", len) == 0) {
printDma();
showDma();
return;
}
@ -5316,103 +5316,6 @@ static void cliDma(char* cmdline)
#endif
}
#endif
static void cliResource(char *cmdline)
{
char *pch = NULL;
char *saveptr;
pch = strtok_r(cmdline, " ", &saveptr);
if (!pch) {
printResource(DUMP_MASTER | HIDE_UNUSED, NULL);
return;
} else if (strcasecmp(pch, "show") == 0) {
#ifdef MINIMAL_CLI
cliPrintLine("IO");
#else
cliPrintLine("Currently active IO resource assignments:\r\n(reboot to update)");
cliRepeat('-', 20);
#endif
for (int i = 0; i < DEFIO_IO_USED_COUNT; i++) {
const char* owner;
owner = ownerNames[ioRecs[i].owner];
cliPrintf("%c%02d: %s", IO_GPIOPortIdx(ioRecs + i) + 'A', IO_GPIOPinIdx(ioRecs + i), owner);
if (ioRecs[i].index > 0) {
cliPrintf(" %d", ioRecs[i].index);
}
cliPrintLinefeed();
}
#if defined(USE_DMA)
pch = strtok_r(NULL, " ", &saveptr);
if (strcasecmp(pch, "all") == 0) {
cliDma("show");
}
#endif
return;
}
uint8_t resourceIndex = 0;
int index = 0;
for (resourceIndex = 0; ; resourceIndex++) {
if (resourceIndex >= ARRAYLEN(resourceTable)) {
cliPrintErrorLinef("INVALID RESOURCE NAME: '%s'", pch);
return;
}
const char * resourceName = ownerNames[resourceTable[resourceIndex].owner];
if (strncasecmp(pch, resourceName, strlen(resourceName)) == 0) {
break;
}
}
pch = strtok_r(NULL, " ", &saveptr);
index = atoi(pch);
if (resourceTable[resourceIndex].maxIndex > 0 || index > 0) {
if (index <= 0 || index > MAX_RESOURCE_INDEX(resourceTable[resourceIndex].maxIndex)) {
cliShowArgumentRangeError("INDEX", 1, MAX_RESOURCE_INDEX(resourceTable[resourceIndex].maxIndex));
return;
}
index -= 1;
pch = strtok_r(NULL, " ", &saveptr);
}
ioTag_t *tag = getIoTag(resourceTable[resourceIndex], index);
if (strlen(pch) > 0) {
if (strToPin(pch, tag)) {
if (*tag == IO_TAG_NONE) {
#ifdef MINIMAL_CLI
cliPrintLine("Freed");
#else
cliPrintLine("Resource is freed");
#endif
return;
} else {
ioRec_t *rec = IO_Rec(IOGetByTag(*tag));
if (rec) {
resourceCheck(resourceIndex, index, *tag);
#ifdef MINIMAL_CLI
cliPrintLinef(" %c%02d set", IO_GPIOPortIdx(rec) + 'A', IO_GPIOPinIdx(rec));
#else
cliPrintLinef("\r\nResource is set to %c%02d", IO_GPIOPortIdx(rec) + 'A', IO_GPIOPinIdx(rec));
#endif
} else {
cliShowParseError();
}
return;
}
}
}
cliShowParseError();
}
#endif // USE_RESOURCE_MGMT
#ifdef USE_TIMER_MGMT
@ -5517,6 +5420,40 @@ static void alternateFunctionToString(const ioTag_t ioTag, const int index, char
}
}
static void showTimers(void)
{
cliPrintLinefeed();
#ifdef MINIMAL_CLI
cliPrintLine("Timers:");
#else
cliPrintLine("Currently active Timers:");
cliRepeat('-', 23);
#endif
int8_t timerNumber;
for (int i = 0; (timerNumber = timerGetNumberByIndex(i)); i++) {
cliPrintf("TIM%d:", timerNumber);
bool timerUsed = false;
for (unsigned timerIndex = 0; timerIndex < CC_CHANNELS_PER_TIMER; timerIndex++) {
const resourceOwner_e owner = timerGetOwner(timerNumber, CC_CHANNEL_FROM_INDEX(timerIndex));
if (owner) {
if (!timerUsed) {
timerUsed = true;
cliPrintLinefeed();
}
cliPrintLinef(" CH%d: %s", timerIndex + 1, ownerNames[owner]);
}
}
if (!timerUsed) {
cliPrintLine(" FREE");
}
}
}
static void cliTimer(char *cmdline)
{
int len = strlen(cmdline);
@ -5530,7 +5467,7 @@ static void cliTimer(char *cmdline)
return;
} else if (strncasecmp(cmdline, "show", len) == 0) {
cliPrintErrorLinef("NOT IMPLEMENTED YET");
showTimers();
return;
}
@ -5646,6 +5583,107 @@ static void cliTimer(char *cmdline)
}
#endif
#if defined(USE_RESOURCE_MGMT)
static void cliResource(char *cmdline)
{
char *pch = NULL;
char *saveptr;
pch = strtok_r(cmdline, " ", &saveptr);
if (!pch) {
printResource(DUMP_MASTER | HIDE_UNUSED, NULL);
return;
} else if (strcasecmp(pch, "show") == 0) {
#ifdef MINIMAL_CLI
cliPrintLine("IO");
#else
cliPrintLine("Currently active IO resource assignments:\r\n(reboot to update)");
cliRepeat('-', 20);
#endif
for (int i = 0; i < DEFIO_IO_USED_COUNT; i++) {
const char* owner;
owner = ownerNames[ioRecs[i].owner];
cliPrintf("%c%02d: %s", IO_GPIOPortIdx(ioRecs + i) + 'A', IO_GPIOPinIdx(ioRecs + i), owner);
if (ioRecs[i].index > 0) {
cliPrintf(" %d", ioRecs[i].index);
}
cliPrintLinefeed();
}
pch = strtok_r(NULL, " ", &saveptr);
if (strcasecmp(pch, "all") == 0) {
#if defined(USE_TIMER_MGMT)
cliTimer("show");
#endif
#if defined(USE_DMA)
cliDma("show");
#endif
}
return;
}
uint8_t resourceIndex = 0;
int index = 0;
for (resourceIndex = 0; ; resourceIndex++) {
if (resourceIndex >= ARRAYLEN(resourceTable)) {
cliPrintErrorLinef("INVALID RESOURCE NAME: '%s'", pch);
return;
}
const char * resourceName = ownerNames[resourceTable[resourceIndex].owner];
if (strncasecmp(pch, resourceName, strlen(resourceName)) == 0) {
break;
}
}
pch = strtok_r(NULL, " ", &saveptr);
index = atoi(pch);
if (resourceTable[resourceIndex].maxIndex > 0 || index > 0) {
if (index <= 0 || index > MAX_RESOURCE_INDEX(resourceTable[resourceIndex].maxIndex)) {
cliShowArgumentRangeError("INDEX", 1, MAX_RESOURCE_INDEX(resourceTable[resourceIndex].maxIndex));
return;
}
index -= 1;
pch = strtok_r(NULL, " ", &saveptr);
}
ioTag_t *tag = getIoTag(resourceTable[resourceIndex], index);
if (strlen(pch) > 0) {
if (strToPin(pch, tag)) {
if (*tag == IO_TAG_NONE) {
#ifdef MINIMAL_CLI
cliPrintLine("Freed");
#else
cliPrintLine("Resource is freed");
#endif
return;
} else {
ioRec_t *rec = IO_Rec(IOGetByTag(*tag));
if (rec) {
resourceCheck(resourceIndex, index, *tag);
#ifdef MINIMAL_CLI
cliPrintLinef(" %c%02d set", IO_GPIOPortIdx(rec) + 'A', IO_GPIOPinIdx(rec));
#else
cliPrintLinef("\r\nResource is set to %c%02d", IO_GPIOPortIdx(rec) + 'A', IO_GPIOPinIdx(rec));
#endif
} else {
cliShowParseError();
}
return;
}
}
}
cliShowParseError();
}
#endif
#ifdef USE_DSHOT_TELEMETRY
static void cliDshotTelemetryInfo(char *cmdline)
{

View File

@ -131,7 +131,7 @@ void cameraControlInit(void)
if (CAMERA_CONTROL_MODE_HARDWARE_PWM == cameraControlConfig()->mode) {
#ifdef CAMERA_CONTROL_HARDWARE_PWM_AVAILABLE
const timerHardware_t *timerHardware = timerGetByTag(cameraControlConfig()->ioTag);
const timerHardware_t *timerHardware = timerAllocate(cameraControlConfig()->ioTag, OWNER_CAMERA_CONTROL);
if (!timerHardware) {
return;

View File

@ -177,7 +177,7 @@ motorDevice_t *dshotPwmDevInit(const motorDevConfig_t *motorConfig, uint16_t idl
for (int motorIndex = 0; motorIndex < MAX_SUPPORTED_MOTORS && motorIndex < motorCount; motorIndex++) {
const ioTag_t tag = motorConfig->ioTags[motorIndex];
const timerHardware_t *timerHardware = timerGetByTag(tag);
const timerHardware_t *timerHardware = timerAllocate(tag, OWNER_MOTOR);
if (timerHardware == NULL) {
/* not enough motors initialised for the mixer or a break in the motors */

View File

@ -55,7 +55,7 @@ bool ws2811LedStripHardwareInit(ioTag_t ioTag)
return false;
}
const timerHardware_t *timerHardware = timerGetByTag(ioTag);
const timerHardware_t *timerHardware = timerAllocate(ioTag, OWNER_LED_STRIP);
if (timerHardware == NULL) {
return false;

View File

@ -83,7 +83,7 @@ bool ws2811LedStripHardwareInit(ioTag_t ioTag)
TIM_OCInitTypeDef TIM_OCInitStructure;
DMA_InitTypeDef DMA_InitStructure;
const timerHardware_t *timerHardware = timerGetByTag(ioTag);
const timerHardware_t *timerHardware = timerAllocate(ioTag, OWNER_LED_STRIP);
if (timerHardware == NULL) {
return false;

View File

@ -213,7 +213,7 @@ motorDevice_t *motorPwmDevInit(const motorDevConfig_t *motorConfig, uint16_t idl
for (int motorIndex = 0; motorIndex < MAX_SUPPORTED_MOTORS && motorIndex < motorCount; motorIndex++) {
const ioTag_t tag = motorConfig->ioTags[motorIndex];
const timerHardware_t *timerHardware = timerGetByTag(tag);
const timerHardware_t *timerHardware = timerAllocate(tag, OWNER_MOTOR);
if (timerHardware == NULL) {
/* not enough motors initialised for the mixer or a break in the motors */
@ -294,7 +294,7 @@ void servoDevInit(const servoDevConfig_t *servoConfig)
IOInit(servos[servoIndex].io, OWNER_SERVO, RESOURCE_INDEX(servoIndex));
const timerHardware_t *timer = timerGetByTag(tag);
const timerHardware_t *timer = timerAllocate(tag, OWNER_SERVO);
if (timer == NULL) {
/* flag failure and disable ability to arm */

View File

@ -371,7 +371,7 @@ void pwmRxInit(const pwmConfig_t *pwmConfig)
pwmInputPort_t *port = &pwmInputPorts[channel];
const timerHardware_t *timer = timerGetByTag(pwmConfig->ioTags[channel]);
const timerHardware_t *timer = timerAllocate(pwmConfig->ioTags[channel], OWNER_PWMINPUT);
if (!timer) {
/* TODO: maybe fail here if not enough channels? */
@ -428,7 +428,7 @@ void ppmRxInit(const ppmConfig_t *ppmConfig)
pwmInputPort_t *port = &pwmInputPorts[FIRST_PWM_PORT];
const timerHardware_t *timer = timerGetByTag(ppmConfig->ioTag);
const timerHardware_t *timer = timerAllocate(ppmConfig->ioTag, OWNER_PPMINPUT);
if (!timer) {
/* TODO: fail here? */
return;

View File

@ -668,7 +668,7 @@ static serialPort_t *openEscSerial(const motorDevConfig_t *motorConfig, escSeria
}
if (mode != PROTOCOL_KISSALL) {
const ioTag_t tag = motorConfig->ioTags[output];
const timerHardware_t *timerHardware = timerGetByTag(tag);
const timerHardware_t *timerHardware = timerAllocate(tag, OWNER_MOTOR);
if (timerHardware == NULL) {
return NULL;
@ -686,7 +686,7 @@ static serialPort_t *openEscSerial(const motorDevConfig_t *motorConfig, escSeria
}
escSerial->mode = mode;
escSerial->txTimerHardware = timerGetByTag(escSerialConfig()->ioTag);
escSerial->txTimerHardware = timerAllocate(escSerialConfig()->ioTag, OWNER_MOTOR);
if (escSerial->txTimerHardware == NULL) {
return NULL;
}
@ -745,7 +745,7 @@ static serialPort_t *openEscSerial(const motorDevConfig_t *motorConfig, escSeria
if (pwmMotors[i].enabled && pwmMotors[i].io != IO_NONE) {
const ioTag_t tag = motorConfig->ioTags[i];
if (tag != IO_TAG_NONE) {
const timerHardware_t *timerHardware = timerGetByTag(tag);
const timerHardware_t *timerHardware = timerAllocate(tag, OWNER_MOTOR);
if (timerHardware) {
escSerialOutputPortConfig(timerHardware);
escOutputs[escSerial->outputCount].io = pwmMotors[i].io;

View File

@ -242,8 +242,8 @@ serialPort_t *openSoftSerial(softSerialPortIndex_e portIndex, serialReceiveCallb
ioTag_t tagRx = serialPinConfig()->ioTagRx[pinCfgIndex];
ioTag_t tagTx = serialPinConfig()->ioTagTx[pinCfgIndex];
const timerHardware_t *timerRx = timerGetByTag(tagRx);
const timerHardware_t *timerTx = timerGetByTag(tagTx);
const timerHardware_t *timerRx = timerAllocate(tagRx, OWNER_SERIAL_RX);
const timerHardware_t *timerTx = timerAllocate(tagTx, OWNER_SERIAL_TX);
IO_t rxIO = IOGetByTag(tagRx);
IO_t txIO = IOGetByTag(tagTx);

View File

@ -61,7 +61,7 @@ static void pwmToggleBeeper(void)
static void beeperPwmInit(const ioTag_t tag, uint16_t frequency)
{
const timerHardware_t *timer = timerGetByTag(tag);
const timerHardware_t *timer = timerAllocate(tag, OWNER_BEEPER);
IO_t beeperIO = IOGetByTag(tag);
if (beeperIO && timer) {

View File

@ -257,10 +257,8 @@ const int8_t timerNumbers[USED_TIMER_COUNT] = {
#undef _DEF
};
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
int8_t timerGetNumberByIndex(uint8_t index)
{
uint8_t index = lookupTimerIndex(tim);
if (index < USED_TIMER_COUNT) {
return timerNumbers[index];
} else {
@ -268,6 +266,13 @@ int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
}
}
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
{
const uint8_t index = lookupTimerIndex(tim);
return timerGetNumberByIndex(index);
}
static inline uint8_t lookupChannelIndex(const uint16_t channel)
{
return channel >> 2;

View File

@ -270,8 +270,10 @@ uint8_t timerInputIrq(TIM_TypeDef *tim);
#if defined(USE_TIMER_MGMT)
timerIOConfig_t *timerIoConfigByTag(ioTag_t ioTag);
resourceOwner_e timerGetOwner(int8_t timerNumber, uint16_t timerChannel);
#endif
const timerHardware_t *timerGetByTag(ioTag_t ioTag);
const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner);
const timerHardware_t *timerGetByTagAndIndex(ioTag_t ioTag, unsigned timerIndex);
ioTag_t timerioTagGetByUsage(timerUsageFlag_e usageFlag, uint8_t index);
@ -292,5 +294,6 @@ uint16_t timerGetPrescalerByDesiredHertz(TIM_TypeDef *tim, uint32_t hz);
uint16_t timerGetPrescalerByDesiredMhz(TIM_TypeDef *tim, uint16_t mhz);
uint16_t timerGetPeriodByPrescaler(TIM_TypeDef *tim, uint16_t prescaler, uint32_t hz);
int8_t timerGetNumberByIndex(uint8_t index);
int8_t timerGetTIMNumber(const TIM_TypeDef *tim);
uint8_t timerLookupChannelIndex(const uint16_t channel);

View File

@ -27,9 +27,9 @@
#ifdef USE_TIMER_MGMT
#include "pg/timerio.h"
#endif
#ifdef USE_TIMER_MGMT
static resourceOwner_e timerOwners[MAX_TIMER_PINMAP_COUNT];
timerIOConfig_t *timerIoConfigByTag(ioTag_t ioTag)
{
for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
@ -37,7 +37,7 @@ timerIOConfig_t *timerIoConfigByTag(ioTag_t ioTag)
return timerIOConfigMutable(i);
}
}
UNUSED(ioTag);
return NULL;
}
@ -78,6 +78,40 @@ const timerHardware_t *timerGetByTag(ioTag_t ioTag)
return timerGetByTagAndIndex(ioTag, timerIndex);
}
resourceOwner_e timerGetOwner(int8_t timerNumber, uint16_t timerChannel)
{
for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
const timerHardware_t *timer = timerGetByTagAndIndex(timerIOConfig(i)->ioTag, timerIOConfig(i)->index);
if (timer && timerGetTIMNumber(timer->tim) == timerNumber && timer->channel == timerChannel) {
return timerOwners[i];
}
}
return OWNER_FREE;
}
const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner)
{
if (!ioTag) {
return NULL;
}
for (unsigned i = 0; i < MAX_TIMER_PINMAP_COUNT; i++) {
if (timerIOConfig(i)->ioTag == ioTag) {
const timerHardware_t *timer = timerGetByTagAndIndex(ioTag, timerIOConfig(i)->index);
if (timerGetOwner(timerGetTIMNumber(timer->tim), timer->channel)) {
return NULL;
}
timerOwners[i] = owner;
return timer;
}
}
return NULL;
}
#else
const timerHardware_t *timerGetByTag(ioTag_t ioTag)
@ -93,6 +127,13 @@ const timerHardware_t *timerGetByTag(ioTag_t ioTag)
#endif
return NULL;
}
const timerHardware_t *timerAllocate(ioTag_t ioTag, resourceOwner_e owner)
{
UNUSED(owner);
return timerGetByTag(ioTag);
}
#endif
ioTag_t timerioTagGetByUsage(timerUsageFlag_e usageFlag, uint8_t index)

View File

@ -264,10 +264,8 @@ const int8_t timerNumbers[USED_TIMER_COUNT] = {
#undef _DEF
};
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
int8_t timerGetNumberByIndex(uint8_t index)
{
uint8_t index = lookupTimerIndex(tim);
if (index < USED_TIMER_COUNT) {
return timerNumbers[index];
} else {
@ -275,6 +273,13 @@ int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
}
}
int8_t timerGetTIMNumber(const TIM_TypeDef *tim)
{
uint8_t index = lookupTimerIndex(tim);
return timerGetNumberByIndex(index);
}
static inline uint8_t lookupChannelIndex(const uint16_t channel)
{
return channel >> 2;

View File

@ -72,7 +72,7 @@ void transponderIrHardwareInit(ioTag_t ioTag, transponder_t *transponder)
return;
}
const timerHardware_t *timerHardware = timerGetByTag(ioTag);
const timerHardware_t *timerHardware = timerAllocate(ioTag, OWNER_TRANSPONDER);
TIM_TypeDef *timer = timerHardware->tim;
timerChannel = timerHardware->channel;
output = timerHardware->output;

View File

@ -67,7 +67,7 @@ void transponderIrHardwareInit(ioTag_t ioTag, transponder_t *transponder)
TIM_OCInitTypeDef TIM_OCInitStructure;
DMA_InitTypeDef DMA_InitStructure;
const timerHardware_t *timerHardware = timerGetByTag(ioTag);
const timerHardware_t *timerHardware = timerAllocate(ioTag, OWNER_TRANSPONDER);
timer = timerHardware->tim;
alternateFunction = timerHardware->alternateFunction;