From 609529b3ef08b6c3ea15d63c299ad589dd66ec46 Mon Sep 17 00:00:00 2001 From: jakeg00dwin Date: Sun, 28 Jul 2024 13:21:27 -0700 Subject: [PATCH] Created new test for ADC_Setup() function and wrote code to pass the test. --- mocks/MockADC/MockADC.c | 6 ++++++ mocks/MockADC/MockADC.h | 6 ++---- src/ADC/ADC.c | 31 +++++++++++++++++++++++++++---- src/ADC/ADC.h | 11 +++++++++++ tests/ADC/test_ADC.cpp | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 8 deletions(-) diff --git a/mocks/MockADC/MockADC.c b/mocks/MockADC/MockADC.c index ced85b1..8c55769 100644 --- a/mocks/MockADC/MockADC.c +++ b/mocks/MockADC/MockADC.c @@ -13,6 +13,12 @@ uint16_t fake_data[FAKESIZE]; int fake_index = 0; + +void ADC_Setup(void) +{ + +} + void ADC_Init(uint8_t pin_num) { mock_c()->actualCall("ADC_Init") diff --git a/mocks/MockADC/MockADC.h b/mocks/MockADC/MockADC.h index d16be3f..c69e6ad 100644 --- a/mocks/MockADC/MockADC.h +++ b/mocks/MockADC/MockADC.h @@ -13,10 +13,8 @@ #include #include -/** - * A function that adds two to a number - * @param a The first argument - */ + +void ADC_Setup(void); void ADC_Init(uint8_t pin_num); void ADC_Enable(uint8_t pin_num); void ADC_Disable(); diff --git a/src/ADC/ADC.c b/src/ADC/ADC.c index 44e41e4..f468fea 100644 --- a/src/ADC/ADC.c +++ b/src/ADC/ADC.c @@ -24,6 +24,27 @@ static bool IsInvalidPin(uint8_t pin_num){ } +void ADC_Setup(void) +{ + //Clears control register A for ADC0 + RegEdit_SetNum((void *) &ADC0.CTRLA, 0x00); + + //Sets The sample accumulation number to 32. + RegEdit_SetNum((void *) &ADC0.CTRLB, 0x5); + + //Sets the voltage reference to VDD or VCC. + RegEdit_SetBit((void *) &ADC0.CTRLC, 4); + + //Sets the pre-scalar for the adc sample rate. + RegEdit_SetBit((void *) &ADC0.CTRLC, 2); + + //Setup an Initalization delay. + RegEdit_OR_Num((void *) &ADC0.CTRLD, (2<<5)); + + //Set the bit for ADC variation during readings. + RegEdit_SetBit((void *) &ADC0.CTRLD, 4); +} + void ADC_Init(uint8_t pin_num) { @@ -54,6 +75,8 @@ void ADC_Init(uint8_t pin_num) //Use VDD as our voltage refernce. //RegEdit_SetBit((void *) &ADC0.CTRLC, 4); + //We setup the ADC to take 32 samples. + //RegEdit_OR_Num((void *) &ADC0.CTRLB, 0x5); } @@ -88,8 +111,6 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num) RegEdit_SetNum((void *) &ADC0.COMMAND, ADC_STCONV_bm); /* Wait until ADC conversion done */ - //while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) - //while(!(RegEdit_IsBitSet((void *) &ADC0.INTFLAGS, ADC_RESSEL_bm))) while ( !(ADC0.INTFLAGS & ADC_RESRDY_bm) ) { ; @@ -97,8 +118,10 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num) /* Clear the interrupt flag by writing 1: */ ADC0.INTFLAGS = ADC_RESRDY_bm; - - return (uint16_t) ADC0.RES; + + uint16_t adc_val = (uint16_t) ADC0.RES; + adc_val = adc_val >> 5; + return adc_val; } diff --git a/src/ADC/ADC.h b/src/ADC/ADC.h index 9de277c..b3a7118 100644 --- a/src/ADC/ADC.h +++ b/src/ADC/ADC.h @@ -53,5 +53,16 @@ void ADC_Disable(); */ extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); +/** + * @brief Sets up the ADC + * + * This function sets up the ADC to take and accumulate 32 samples. It also + * sets the inital delay to 32 ADC clock cycles, and sets the VREF to VDD or + * VCC. + * + * This function should only need to be called once. + */ +void ADC_Setup(void); + #endif //ADC_H diff --git a/tests/ADC/test_ADC.cpp b/tests/ADC/test_ADC.cpp index c077306..e6316bf 100644 --- a/tests/ADC/test_ADC.cpp +++ b/tests/ADC/test_ADC.cpp @@ -47,6 +47,41 @@ static void ADCStoresPortState(void){ .andReturnValue(0xFF); } +TEST(test_ADC, ADC_SetupSetsRegisters) +{ + //Clears control register A for ADC0 + mock().expectOneCall("RegEdit_SetNum") + .withPointerParameter("reg", (void *) &ADC0.CTRLA) + .withUnsignedIntParameter("num", 0x00); + + //Sets The sample accumulation number to 32. + mock().expectOneCall("RegEdit_SetNum") + .withPointerParameter("reg", (void *) &ADC0.CTRLB) + .withUnsignedIntParameter("num", 0x5); + + //Sets the voltage reference to VDD or VCC. + mock().expectOneCall("RegEdit_SetBit") + .withPointerParameter("reg", (void *) &ADC0.CTRLC) + .withUnsignedIntParameter("bit_num", 4); + + //Sets the pre-scalar for the adc sample rate. + mock().expectOneCall("RegEdit_SetBit") + .withPointerParameter("reg", (void *) &ADC0.CTRLC) + .withUnsignedIntParameter("bit_num", 2); + + //Setup an Initalization delay. + mock().expectOneCall("RegEdit_OR_Num") + .withPointerParameter("reg", (void *) &ADC0.CTRLD) + .withUnsignedIntParameter("num", (2<<5)); + + //Set the bit for ADC variation during readings. + mock().expectOneCall("RegEdit_SetBit") + .withPointerParameter("reg", (void *) &ADC0.CTRLD) + .withUnsignedIntParameter("bit_num", 4); + + ADC_Setup(); +} + TEST(test_ADC, ADC_InitPortAPin7UsesCorrectRegisters) { ADCStoresPortState(); @@ -92,6 +127,7 @@ TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters) .withPointerParameter("reg", (void *) &PORTA.PIN0CTRL) .withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc); + ADC_Init(0); }