Removed the usage of the saving of PORTA, also removed params from Enable func.

This commit is contained in:
jakeg00dwin 2024-07-28 16:45:07 -07:00
parent 9e750af5bf
commit ac6b93b49e
3 changed files with 22 additions and 71 deletions

View File

@ -15,9 +15,6 @@
#define MAX_PIN_NUM 7 #define MAX_PIN_NUM 7
static uint8_t porta_out;
static uint8_t porta_dir;
static bool IsInvalidPin(uint8_t pin_num){ static bool IsInvalidPin(uint8_t pin_num){
if(pin_num > MAX_PIN_NUM){ if(pin_num > MAX_PIN_NUM){
return true; return true;
@ -52,15 +49,12 @@ void ADC_Init(uint8_t pin_num)
if(IsInvalidPin(pin_num)){return;} if(IsInvalidPin(pin_num)){return;}
//Save the porta status.
porta_out = RegEdit_ReadReg((void *) &PORTA.OUT);
porta_dir = RegEdit_ReadReg((void *) &PORTA.DIR);
//set the direction to input //set the direction to input
RegEdit_SetBit((void *) &PORTA.DIRCLR, pin_num); RegEdit_ClearBit((void *) &PORTA.DIR, pin_num);
//Disable the pull-up resistor //Disable the pull-up resistor
RegEdit_SetBit((void *) &PORTA.OUTCLR, pin_num); RegEdit_ClearBit((void *) &PORTA.OUT, pin_num);
//Disable input buffer //Disable input buffer
//We do some kinda nasty address addition but it saves //We do some kinda nasty address addition but it saves
@ -73,13 +67,8 @@ void ADC_Init(uint8_t pin_num)
} }
void ADC_Enable(uint8_t pin_num) void ADC_Enable(void)
{ {
if(IsInvalidPin(pin_num)){return;}
//Use muxpos to select pins for ADC to connect.
RegEdit_OR_Num((void *) &ADC0.MUXPOS, pin_num);
//Set the enable bit in the CTRLA register //Set the enable bit in the CTRLA register
RegEdit_SetBit((void *) &ADC0.CTRLA, 0); RegEdit_SetBit((void *) &ADC0.CTRLA, 0);
} }
@ -87,24 +76,19 @@ void ADC_Enable(uint8_t pin_num)
void ADC_Disable() void ADC_Disable()
{ {
//Set for no ADC connections.
RegEdit_ClearRegister((void *) &ADC0.MUXPOS);
//Clear the enable ADC flag //Clear the enable ADC flag
RegEdit_ClearBit((void *) &ADC0.CTRLA, 0); RegEdit_ClearBit((void *) &ADC0.CTRLA, 0);
//Restore the port registers
RegEdit_SetNum((void *) &PORTA.OUT, porta_out);
RegEdit_SetNum((void *) &PORTA.DIR, porta_dir);
} }
void ADC_SetPin(uint8_t pin_num) void ADC_SetPin(uint8_t pin_num)
{ {
if(IsInvalidPin(pin_num)){return;}
RegEdit_ClearRegister((void *) &ADC0.MUXPOS); RegEdit_ClearRegister((void *) &ADC0.MUXPOS);
RegEdit_SetNum((void *) &ADC0.MUXPOS, pin_num); RegEdit_SetNum((void *) &ADC0.MUXPOS, pin_num);
} }
uint16_t ADC_ReadValue_Impl(uint8_t pin_num) 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);

View File

@ -32,9 +32,8 @@ void ADC_Init(uint8_t pin_num);
/** /**
* @brief Enables the ADC * @brief Enables the ADC
* @param pin_num The number of the pin 0-7 you are enabling
*/ */
void ADC_Enable(uint8_t pin_num); void ADC_Enable(void);
/** /**

View File

@ -37,15 +37,6 @@ TEST(test_ADC, FirstTest)
CHECK(true); CHECK(true);
} }
static void ADCStoresPortState(void){
mock().expectOneCall("RegEdit_ReadReg")
.withPointerParameter("reg", (void *) &PORTA.OUT)
.andReturnValue(0xFF);
mock().expectOneCall("RegEdit_ReadReg")
.withPointerParameter("reg", (void *) &PORTA.DIR)
.andReturnValue(0xFF);
}
TEST(test_ADC, ADC_SetupSetsRegisters) TEST(test_ADC, ADC_SetupSetsRegisters)
{ {
@ -84,16 +75,14 @@ TEST(test_ADC, ADC_SetupSetsRegisters)
TEST(test_ADC, ADC_InitPortAPin7UsesCorrectRegisters) TEST(test_ADC, ADC_InitPortAPin7UsesCorrectRegisters)
{ {
ADCStoresPortState();
//Check for setting the direction to input. //Check for setting the direction to input.
mock().expectOneCall("RegEdit_SetBit") mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.DIRCLR) .withPointerParameter("reg", (void *) &PORTA.DIR)
.withUnsignedIntParameter("bit_num", 7); .withUnsignedIntParameter("bit_num", 7);
//Check that the pullup is off //Check that the pullup is off
mock().expectOneCall("RegEdit_SetBit") mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.OUTCLR) .withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", 7); .withUnsignedIntParameter("bit_num", 7);
//Set the ISC(input sense config) to disable digital input //Set the ISC(input sense config) to disable digital input
@ -109,16 +98,14 @@ TEST(test_ADC, ADC_InitPortAPin7UsesCorrectRegisters)
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters) TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)
{ {
ADCStoresPortState();
//Check for setting the direction to input. //Check for setting the direction to input.
mock().expectOneCall("RegEdit_SetBit") mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.DIRCLR) .withPointerParameter("reg", (void *) &PORTA.DIR)
.withUnsignedIntParameter("bit_num", 0); .withUnsignedIntParameter("bit_num", 0);
//Check that the pullup is off //Check that the pullup is off
mock().expectOneCall("RegEdit_SetBit") mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.OUTCLR) .withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", 0); .withUnsignedIntParameter("bit_num", 0);
//Set the ISC(input sense config) to disable digital input //Set the ISC(input sense config) to disable digital input
@ -137,48 +124,23 @@ TEST(test_ADC, ADC_InitDoesNothingOnHighPinNumbers)
ADC_Init(8); ADC_Init(8);
} }
TEST(test_ADC, ADC_EnableSuccessOnPin7) TEST(test_ADC, ADC_EnablePasses)
{ {
//Set the MUXPOS or the ADC pin stuff.
mock().expectOneCall("RegEdit_OR_Num")
.withPointerParameter("reg", (void *) &ADC0.MUXPOS)
.withUnsignedIntParameter("num", ADC_MUXPOS_AIN7_gc);
mock().expectOneCall("RegEdit_SetBit") mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &ADC0.CTRLA) .withPointerParameter("reg", (void *) &ADC0.CTRLA)
.withUnsignedIntParameter("bit_num", 0); .withUnsignedIntParameter("bit_num", 0);
ADC_Enable(7); ADC_Enable();
} }
TEST(test_ADC, ADC_EnableNothingOnHighPinNumbers)
{
mock().expectNoCall("RegEdit_SetBit");
ADC_Enable(8);
}
TEST(test_ADC, ADC_DisablePasses) TEST(test_ADC, ADC_DisablePasses)
{ {
//Clears the muxpos register
mock().expectOneCall("RegEdit_ClearRegister")
.withPointerParameter("reg", (void *) &ADC0.MUXPOS);
mock().expectOneCall("RegEdit_ClearBit") mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &ADC0.CTRLA) .withPointerParameter("reg", (void *) &ADC0.CTRLA)
.withUnsignedIntParameter("bit_num", 0); .withUnsignedIntParameter("bit_num", 0);
mock().expectOneCall("RegEdit_SetNum")
.withPointerParameter("reg", (void*) &PORTA.OUT)
.ignoreOtherParameters();
mock().expectOneCall("RegEdit_SetNum")
.withPointerParameter("reg", (void*) &PORTA.DIR)
.ignoreOtherParameters();
ADC_Disable(); ADC_Disable();
} }
@ -197,6 +159,12 @@ TEST(test_ADC, ADC_SetPinSetsRightRegisters)
} }
TEST(test_ADC, ADC_SetPinFailsOnInvalidPin)
{
ADC_SetPin(8);
}
static uint16_t ADC_ReadValueFake(uint8_t pin_num) static uint16_t ADC_ReadValueFake(uint8_t pin_num)
{ {
return 512; return 512;