diff --git a/executables/referenceApp/platforms/s32k148evb/bspConfiguration/include/bsp/io/input/inputConfiguration.h b/executables/referenceApp/platforms/s32k148evb/bspConfiguration/include/bsp/io/input/inputConfiguration.h index 6526d4c8f5..b84fcf8a93 100644 --- a/executables/referenceApp/platforms/s32k148evb/bspConfiguration/include/bsp/io/input/inputConfiguration.h +++ b/executables/referenceApp/platforms/s32k148evb/bspConfiguration/include/bsp/io/input/inputConfiguration.h @@ -22,15 +22,14 @@ DigitalInput::InputConfiguration const* DigitalInput::getConfiguration(uint8_t h enum DigitalInputId { /* 0 */ EVAL_DI_1, - /* 1 */ EVAL_SW3, - - /* xx */ NUMBER_OF_INTERNAL_DIGITAL_INPUTS, - - // TODO: dynamic inputs here - /* yy */ // MyDynamicInput, - - NUMBER_OF_EXTERNAL_DIGITAL_INPUTS, - TOTAL_NUMBER_OF_DIGITAL_INPUTS = NUMBER_OF_EXTERNAL_DIGITAL_INPUTS, - PORT_UNAVAILABLE = TOTAL_NUMBER_OF_DIGITAL_INPUTS + /* 1 */ EVAL_SW3, + // TODO: other internal inputs go here + // update LAST_INTERNAL_DIGITAL_INPUT when adding a new internal input + LAST_INTERNAL_DIGITAL_INPUT = EVAL_SW3, + // TODO: dynamic inputs go here + // update LAST_DYNAMIC_DIGITAL_INPUT when adding a new external input + /* yy */ // MyFirstDynamicInput, + LAST_DYNAMIC_DIGITAL_INPUT = LAST_INTERNAL_DIGITAL_INPUT, + PORT_UNAVAILABLE }; #endif /* #if (BSP_INPUT_PIN_CONFIGURATION == 1) */ diff --git a/libs/bsp/bspInputManager/include/inputManager/DigitalInput.h b/libs/bsp/bspInputManager/include/inputManager/DigitalInput.h index a2a2bc032d..2ab0477978 100644 --- a/libs/bsp/bspInputManager/include/inputManager/DigitalInput.h +++ b/libs/bsp/bspInputManager/include/inputManager/DigitalInput.h @@ -3,6 +3,7 @@ #ifndef GUARD_CF7B03E0_39CD_48FA_AEF0_45E4BC276713 #define GUARD_CF7B03E0_39CD_48FA_AEF0_45E4BC276713 +#include "estd/static_assert.h" #include "estd/uncopyable.h" #include "io/DynamicClientCfg.h" #include "io/Io.h" @@ -20,6 +21,19 @@ class DigitalInput #endif #include "bsp/io/input/inputConfiguration.h" + static uint16_t const TOTAL_NUMBER_OF_DIGITAL_INPUTS + = static_cast(DigitalInputId::PORT_UNAVAILABLE); + static uint16_t const NUMBER_OF_EXTERNAL_DIGITAL_INPUTS = static_cast( + DigitalInputId::LAST_DYNAMIC_DIGITAL_INPUT - DigitalInputId::LAST_INTERNAL_DIGITAL_INPUT); + static uint16_t const NUMBER_OF_INTERNAL_DIGITAL_INPUTS + = TOTAL_NUMBER_OF_DIGITAL_INPUTS - NUMBER_OF_EXTERNAL_DIGITAL_INPUTS; + + // Make sure inputConfiguration has the correct structure + ESTD_STATIC_ASSERT( + DigitalInputId::LAST_DYNAMIC_DIGITAL_INPUT >= DigitalInputId::LAST_INTERNAL_DIGITAL_INPUT); + ESTD_STATIC_ASSERT( + DigitalInputId::PORT_UNAVAILABLE == DigitalInputId::LAST_DYNAMIC_DIGITAL_INPUT + 1); + // Api for all DynamicClients class IDynamicInputClient { @@ -90,12 +104,12 @@ class DigitalInput using dynamicClientType = uint16_t; - static uint8_t const InputAnzahlDynamic + static uint16_t const InputNumberDynamic = ((NUMBER_OF_EXTERNAL_DIGITAL_INPUTS > 0) - ? static_cast(NUMBER_OF_EXTERNAL_DIGITAL_INPUTS) + ? NUMBER_OF_EXTERNAL_DIGITAL_INPUTS : 1U); - static dynamicClient + static dynamicClient dynamicInputCfg; #if (INPUTDIGITAL_DEBOUNCE_ACTIVE == 1) static DebounceConfiguration debounced[NUMBER_OF_DIGITAL_INPUTS]; diff --git a/libs/bsp/bspInputManager/src/inputManager/DigitalInput.cpp b/libs/bsp/bspInputManager/src/inputManager/DigitalInput.cpp index 0ef6b75c3c..843aa9daa2 100644 --- a/libs/bsp/bspInputManager/src/inputManager/DigitalInput.cpp +++ b/libs/bsp/bspInputManager/src/inputManager/DigitalInput.cpp @@ -22,7 +22,7 @@ dynamicClient< DigitalInput::dynamicClientType, DigitalInput::IDynamicInputClient, 4, - DigitalInput::InputAnzahlDynamic> + DigitalInput::InputNumberDynamic> DigitalInput::dynamicInputCfg; void DigitalInput::init(uint8_t const hw, bool const doSetup) @@ -34,7 +34,7 @@ void DigitalInput::init(uint8_t const hw, bool const doSetup) } if (true == doSetup) { - for (int32_t i = 0; i < NUMBER_OF_INTERNAL_DIGITAL_INPUTS; i++) + for (uint16_t i = 0; i < NUMBER_OF_INTERNAL_DIGITAL_INPUTS; i++) { (void)Io::setDefaultConfiguration( static_cast(sfpDigitalInputConfiguration[i].ioNumber)); @@ -92,34 +92,35 @@ bsp::BspReturnCode DigitalInput::get(DigitalInputId const channel, bool& result) return bsp::BSP_ERROR; } - if (channel < NUMBER_OF_INTERNAL_DIGITAL_INPUTS) + const uint16_t tmpChannel = static_cast(channel); + + // Internal channel + if ((NUMBER_OF_INTERNAL_DIGITAL_INPUTS > 0) + && (channel <= DigitalInputId::LAST_INTERNAL_DIGITAL_INPUT)) { - if (Io::PORT_UNAVAILABLE == sfpDigitalInputConfiguration[channel].ioNumber) + if (Io::PORT_UNAVAILABLE == sfpDigitalInputConfiguration[tmpChannel].ioNumber) { return bsp::BSP_NOT_SUPPORTED; } #if (INPUTDIGITAL_DEBOUNCE_ACTIVE == 1) result = debounced[channel].vol != 0U; #else - result = Io::getPin(static_cast(sfpDigitalInputConfiguration[channel].ioNumber)); + result = Io::getPin(static_cast(sfpDigitalInputConfiguration[tmpChannel].ioNumber)); #endif - if (sfpDigitalInputConfiguration[channel].isInverted) + if (sfpDigitalInputConfiguration[tmpChannel].isInverted) { result = !result; } return bsp::BSP_OK; } - else if (channel == NUMBER_OF_INTERNAL_DIGITAL_INPUTS) - { - return bsp::BSP_ERROR; - } - else if (channel < TOTAL_NUMBER_OF_DIGITAL_INPUTS) + else if ((channel > DigitalInputId::LAST_INTERNAL_DIGITAL_INPUT) + && (channel <= DigitalInputId::LAST_DYNAMIC_DIGITAL_INPUT)) { // dynamic instance dynamicClientType const dynamicChannel - = static_cast(channel - NUMBER_OF_INTERNAL_DIGITAL_INPUTS - 1); - if (dynamicChannel < InputAnzahlDynamic) + = static_cast(tmpChannel - NUMBER_OF_INTERNAL_DIGITAL_INPUTS); + if (dynamicChannel < InputNumberDynamic) { if (dynamicInputCfg.getClientValid(dynamicChannel) == true) { @@ -145,12 +146,12 @@ bsp::BspReturnCode DigitalInput::get(DigitalInputId const channel, bool& result) bsp::BspReturnCode DigitalInput::setDynamicClient( uint16_t const inputNumber, uint16_t const clientInputNumber, IDynamicInputClient* const client) { - // range of outputNumber check - if ((inputNumber > static_cast(NUMBER_OF_INTERNAL_DIGITAL_INPUTS)) - && (inputNumber < static_cast(TOTAL_NUMBER_OF_DIGITAL_INPUTS))) + // range of input check + if ((inputNumber > static_cast(DigitalInputId::LAST_INTERNAL_DIGITAL_INPUT)) + && (inputNumber <= static_cast(DigitalInputId::LAST_DYNAMIC_DIGITAL_INPUT))) { dynamicClientType const dynamicChannel - = inputNumber - static_cast(NUMBER_OF_INTERNAL_DIGITAL_INPUTS) - 1U; + = static_cast(inputNumber - NUMBER_OF_INTERNAL_DIGITAL_INPUTS); interrupts::SuspendResumeAllInterruptsLock fLock; fLock.suspend(); if (dynamicInputCfg.setDynamicClient(dynamicChannel, clientInputNumber, client) == true) @@ -172,11 +173,12 @@ bsp::BspReturnCode DigitalInput::setDynamicClient( bsp::BspReturnCode DigitalInput::clrDynamicClient(uint16_t const inputNumber) { - if ((inputNumber > static_cast(NUMBER_OF_INTERNAL_DIGITAL_INPUTS)) - && (inputNumber < static_cast(TOTAL_NUMBER_OF_DIGITAL_INPUTS))) + // range of input check + if ((inputNumber > static_cast(DigitalInputId::LAST_INTERNAL_DIGITAL_INPUT)) + && (inputNumber <= static_cast(DigitalInputId::LAST_DYNAMIC_DIGITAL_INPUT))) { dynamicClientType const dynamicChannel - = inputNumber - static_cast(NUMBER_OF_INTERNAL_DIGITAL_INPUTS) - 1U; + = static_cast(inputNumber - NUMBER_OF_INTERNAL_DIGITAL_INPUTS); interrupts::SuspendResumeAllInterruptsLock fLock; fLock.suspend(); if (dynamicInputCfg.clearDynamicClient(dynamicChannel) == true) diff --git a/libs/bsp/bspInputManager/src/inputManager/DigitalInputTester.cpp b/libs/bsp/bspInputManager/src/inputManager/DigitalInputTester.cpp index 4e3904c228..abb085fb0b 100644 --- a/libs/bsp/bspInputManager/src/inputManager/DigitalInputTester.cpp +++ b/libs/bsp/bspInputManager/src/inputManager/DigitalInputTester.cpp @@ -22,9 +22,7 @@ void DigitalInputTester::executeCommand(::util::command::CommandContext& context ::util::format::SharedStringWriter out(context); (void)out.printf( "All digital inputs: %d \r\n", DigitalInput::TOTAL_NUMBER_OF_DIGITAL_INPUTS); - for (uint16_t i = 0U; - i < static_cast(DigitalInput::TOTAL_NUMBER_OF_DIGITAL_INPUTS); - ++i) + for (uint16_t i = 0U; i < DigitalInput::TOTAL_NUMBER_OF_DIGITAL_INPUTS; ++i) { bool temp; bsp::BspReturnCode const ret @@ -45,9 +43,9 @@ void DigitalInputTester::executeCommand(::util::command::CommandContext& context break; case 2: // "get" { - uint32_t const inputNo = context.scanIntToken(); + uint16_t const inputNo = context.scanIntToken(); (void)context.check( - inputNo < static_cast(DigitalInput::TOTAL_NUMBER_OF_DIGITAL_INPUTS)); + inputNo < DigitalInput::TOTAL_NUMBER_OF_DIGITAL_INPUTS); if (context.checkEol()) { ::util::format::SharedStringWriter out(context); @@ -70,9 +68,7 @@ void DigitalInputTester::executeCommand(::util::command::CommandContext& context { ::util::format::SharedStringWriter out(context); (void)out.printf("#"); - for (uint32_t i = 0U; - i < static_cast(DigitalInput::TOTAL_NUMBER_OF_DIGITAL_INPUTS); - ++i) + for (uint16_t i = 0U; i < DigitalInput::TOTAL_NUMBER_OF_DIGITAL_INPUTS; ++i) { bool temp; bsp::BspReturnCode const ret diff --git a/libs/bsp/bspInputManager/test/include/bsp/io/input/inputConfiguration.h b/libs/bsp/bspInputManager/test/include/bsp/io/input/inputConfiguration.h index ea286310b5..481792649f 100644 --- a/libs/bsp/bspInputManager/test/include/bsp/io/input/inputConfiguration.h +++ b/libs/bsp/bspInputManager/test/include/bsp/io/input/inputConfiguration.h @@ -3,10 +3,9 @@ enum DigitalInputId { InternalInput1, InternalInput2, - NUMBER_OF_INTERNAL_DIGITAL_INPUTS, + LAST_INTERNAL_DIGITAL_INPUT = InternalInput2, ExternalInput1, ExternalInput2, - NUMBER_OF_EXTERNAL_DIGITAL_INPUTS, - TOTAL_NUMBER_OF_DIGITAL_INPUTS = NUMBER_OF_EXTERNAL_DIGITAL_INPUTS, - PORT_UNAVAILABLE = TOTAL_NUMBER_OF_DIGITAL_INPUTS + LAST_DYNAMIC_DIGITAL_INPUT = ExternalInput2, + PORT_UNAVAILABLE };