From a00e17667563fd2fd6877ff5eb02348361797ec3 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 23 Feb 2025 09:50:09 -0800 Subject: [PATCH] Added testing for the pin number and resolving it from a adc channel input. --- src/ADC/ADC.c | 35 +++++++++++++----------- src/ADC/ADC.h | 6 +++++ tests/ADC/test_ADC.cpp | 61 +++++++++++++++++++----------------------- 3 files changed, 53 insertions(+), 49 deletions(-) diff --git a/src/ADC/ADC.c b/src/ADC/ADC.c index 21c9532..e0f558c 100644 --- a/src/ADC/ADC.c +++ b/src/ADC/ADC.c @@ -15,7 +15,14 @@ #define MAX_CHANNEL_NUM 3 -static uint8_t GetChannelPinNum(uint8_t adc_chan) { +static bool IsInvalidChannel(uint8_t adc_chan) { + if (adc_chan > MAX_CHANNEL_NUM) { + return true; + } + return false; +} + +uint8_t ADC_GetChannelPinNum(uint8_t adc_chan) { switch (adc_chan) { case 0: return PB5; @@ -30,13 +37,6 @@ static uint8_t GetChannelPinNum(uint8_t adc_chan) { } } -static bool IsInvalidChannel(uint8_t adc_chan) { - if (adc_chan > MAX_CHANNEL_NUM) { - return true; - } - return false; -} - void ADC_Setup(void) { // Clear the register, set VCC as ref, ADC0 as channel and // leave result right adjusted. @@ -57,17 +57,22 @@ void ADC_Init(uint8_t adc_chan) { return; } + // get the associated pin number. + uint8_t pin_num = ADC_GetChannelPinNum(adc_chan); + // set the direction to input - RegEdit_ClearBit((void *)&DDRB, adc_chan); + RegEdit_ClearBit((void *)&DDRB, pin_num); // Disable the pull-up resistor - RegEdit_ClearBit((void *)&PORTB, adc_chan); + RegEdit_ClearBit((void *)&PORTB, pin_num); - // Disable input buffer - // We do some kinda nasty address addition but it saves - // memory and means we don't need a switch statment. - // RegEdit_SetBit((void *)(&PORTA.PIN0CTRL) + adc_chan, - // PORT_ISC_INPUT_DISABLE_gc); + // Set the adc mux reg for our channel. + + // Clear the existing mux bits. + RegEdit_AND_Num((void *)&ADMUX, 0xFC); + + // Set the correct bits + RegEdit_OR_Num((void *)&ADMUX, adc_chan); } void ADC_Enable(void) { diff --git a/src/ADC/ADC.h b/src/ADC/ADC.h index c369fa6..33c6026 100644 --- a/src/ADC/ADC.h +++ b/src/ADC/ADC.h @@ -27,6 +27,12 @@ */ void ADC_Init(uint8_t adc_chan); +/** + * @brief Returns the pin number for the adc channel or 255 on an error. + * @param The ADC channel. + */ +uint8_t ADC_GetChannelPinNum(uint8_t adc_chan); + /** * @brief Enables the ADC */ diff --git a/tests/ADC/test_ADC.cpp b/tests/ADC/test_ADC.cpp index 292d30c..69ddd19 100644 --- a/tests/ADC/test_ADC.cpp +++ b/tests/ADC/test_ADC.cpp @@ -80,50 +80,43 @@ TEST(test_ADC, ADC_InitRejectsInvalidChannels) } } -TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters) + +TEST(test_ADC, ADC_InitPortbWorksWithValidChannels) { - //PB3/ADC3 - //uint8_t adc_channel = 3; - uint8_t pin_num = 3; + uint8_t pin_num; - //Check it disables the pin's digital circuitry + for(uint8_t adc_channel = 0; adc_channel <= 3; adc_channel++) + { + pin_num = ADC_GetChannelPinNum(adc_channel); - //Check it sets the pin for input - mock().expectOneCall("RegEdit_ClearBit") - .withPointerParameter("reg", (void *) &DDRB) - .withUnsignedIntParameter("bit_num", pin_num); + //Check it disables the pin's digital circuitry + + //Check it sets the pin for input + mock().expectOneCall("RegEdit_ClearBit") + .withPointerParameter("reg", (void *) &DDRB) + .withUnsignedIntParameter("bit_num", pin_num); - //Check that the pull-up resistor is disabled. - mock().expectOneCall("RegEdit_ClearBit") - .withPointerParameter("reg", (void *) &PORTB) - .withUnsignedIntParameter("bit_num", pin_num); + //Check that the pull-up resistor is disabled. + mock().expectOneCall("RegEdit_ClearBit") + .withPointerParameter("reg", (void *) &PORTB) + .withUnsignedIntParameter("bit_num", pin_num); - //Check that the ADC pin is selected in ADMUX - + //Check that the ADC pin is selected in ADMUX - // + //Clears the last two bits first. + mock().expectOneCall("RegEdit_AND_Num") + .withPointerParameter("reg", (void *) &ADMUX) + .withUnsignedIntParameter("num", 0xFC); - /* - //Check for setting the direction to input. - mock().expectOneCall("RegEdit_ClearBit") - .withPointerParameter("reg", (void *) &PORTA.DIR) - .withUnsignedIntParameter("bit_num", 7); - - //Check that the pullup is off - mock().expectOneCall("RegEdit_ClearBit") - .withPointerParameter("reg", (void *) &PORTA.OUT) - .withUnsignedIntParameter("bit_num", 7); + //Selects the ADC pin/channel + mock().expectOneCall("RegEdit_OR_Num") + .withPointerParameter("reg", (void *) &ADMUX) + .withUnsignedIntParameter("num", adc_channel); - //Set the ISC(input sense config) to disable digital input - //buffering and reduce the noise on ADC usage. - mock().expectOneCall("RegEdit_SetBit") - .withPointerParameter("reg", (void *) &PORTA.PIN7CTRL) - .withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc); - - */ - ADC_Init(3); + ADC_Init(adc_channel); + } } TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)