Created new test for ADC_Setup() function and wrote code to pass the test.

This commit is contained in:
jakeg00dwin 2024-07-28 13:21:27 -07:00
parent 3af31e045d
commit 609529b3ef
5 changed files with 82 additions and 8 deletions

View File

@ -13,6 +13,12 @@
uint16_t fake_data[FAKESIZE]; uint16_t fake_data[FAKESIZE];
int fake_index = 0; int fake_index = 0;
void ADC_Setup(void)
{
}
void ADC_Init(uint8_t pin_num) void ADC_Init(uint8_t pin_num)
{ {
mock_c()->actualCall("ADC_Init") mock_c()->actualCall("ADC_Init")

View File

@ -13,10 +13,8 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
/**
* A function that adds two to a number void ADC_Setup(void);
* @param a The first argument
*/
void ADC_Init(uint8_t pin_num); void ADC_Init(uint8_t pin_num);
void ADC_Enable(uint8_t pin_num); void ADC_Enable(uint8_t pin_num);
void ADC_Disable(); void ADC_Disable();

View File

@ -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) void ADC_Init(uint8_t pin_num)
{ {
@ -54,6 +75,8 @@ void ADC_Init(uint8_t pin_num)
//Use VDD as our voltage refernce. //Use VDD as our voltage refernce.
//RegEdit_SetBit((void *) &ADC0.CTRLC, 4); //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); RegEdit_SetNum((void *) &ADC0.COMMAND, ADC_STCONV_bm);
/* Wait until ADC conversion done */ /* 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) ) 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: */ /* Clear the interrupt flag by writing 1: */
ADC0.INTFLAGS = ADC_RESRDY_bm; 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;
} }

View File

@ -53,5 +53,16 @@ void ADC_Disable();
*/ */
extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); 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 #endif //ADC_H

View File

@ -47,6 +47,41 @@ static void ADCStoresPortState(void){
.andReturnValue(0xFF); .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) TEST(test_ADC, ADC_InitPortAPin7UsesCorrectRegisters)
{ {
ADCStoresPortState(); ADCStoresPortState();
@ -92,6 +127,7 @@ TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)
.withPointerParameter("reg", (void *) &PORTA.PIN0CTRL) .withPointerParameter("reg", (void *) &PORTA.PIN0CTRL)
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc); .withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
ADC_Init(0); ADC_Init(0);
} }