2024-07-25 00:21:29 +00:00
|
|
|
/*
|
|
|
|
* Author: Jake G
|
|
|
|
* Date: 2024
|
|
|
|
* filename: test_ADC.c
|
|
|
|
* description: module_purpose
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CppUTest/CommandLineTestRunner.h"
|
|
|
|
#include "CppUTestExt/MockSupport.h"
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
//This define allows us to dircetly include the device header without error.
|
|
|
|
#define _AVR_IO_H_
|
|
|
|
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include <iotn404.h> //ATtiny404 header fille.
|
|
|
|
#include "ADC.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_GROUP(test_ADC)
|
|
|
|
{
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
void teardown()
|
|
|
|
{
|
|
|
|
mock().checkExpectations();
|
|
|
|
mock().clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(test_ADC, FirstTest)
|
|
|
|
{
|
|
|
|
CHECK(true);
|
|
|
|
}
|
|
|
|
|
2024-07-27 16:23:36 +00:00
|
|
|
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);
|
|
|
|
}
|
2024-07-25 00:21:29 +00:00
|
|
|
|
|
|
|
TEST(test_ADC, ADC_InitPortAPin7UsesCorrectRegisters)
|
|
|
|
{
|
2024-07-27 16:23:36 +00:00
|
|
|
ADCStoresPortState();
|
|
|
|
|
2024-07-25 00:21:29 +00:00
|
|
|
//Check for setting the direction to input.
|
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.DIRCLR)
|
|
|
|
.withUnsignedIntParameter("bit_num", 7);
|
|
|
|
|
|
|
|
//Check that the pullup is off
|
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.OUTCLR)
|
|
|
|
.withUnsignedIntParameter("bit_num", 7);
|
|
|
|
|
|
|
|
//Set the ISC(input sense config) to disable digital input
|
|
|
|
//buffering and reduce the noise on ADC usage.
|
2024-07-27 16:06:23 +00:00
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
2024-07-25 00:21:29 +00:00
|
|
|
.withPointerParameter("reg", (void *) &PORTA.PIN7CTRL)
|
2024-07-27 16:06:23 +00:00
|
|
|
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
|
2024-07-25 00:21:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
ADC_Init(7);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)
|
|
|
|
{
|
|
|
|
//Check for setting the direction to input.
|
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.DIRCLR)
|
|
|
|
.withUnsignedIntParameter("bit_num", 0);
|
|
|
|
|
|
|
|
//Check that the pullup is off
|
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.OUTCLR)
|
|
|
|
.withUnsignedIntParameter("bit_num", 0);
|
|
|
|
|
|
|
|
//Set the ISC(input sense config) to disable digital input
|
|
|
|
//buffering and reduce the noise on ADC usage.
|
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
2024-07-27 16:06:23 +00:00
|
|
|
.withPointerParameter("reg", (void *) &PORTA.PIN0CTRL)
|
|
|
|
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
|
2024-07-25 00:21:29 +00:00
|
|
|
|
|
|
|
ADC_Init(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_ADC, ADC_InitDoesNothingOnHighPinNumbers)
|
|
|
|
{
|
|
|
|
mock().expectNoCall("RegEdit_SetBit");
|
|
|
|
ADC_Init(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_ADC, ADC_EnableSuccessOnPin7)
|
|
|
|
{
|
|
|
|
|
|
|
|
//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")
|
|
|
|
.withPointerParameter("reg", (void *) &ADC0.CTRLA)
|
|
|
|
.withUnsignedIntParameter("bit_num", 0);
|
|
|
|
|
|
|
|
ADC_Enable(7);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(test_ADC, ADC_EnableNothingOnHighPinNumbers)
|
|
|
|
{
|
2024-07-27 01:19:05 +00:00
|
|
|
|
2024-07-25 00:21:29 +00:00
|
|
|
mock().expectNoCall("RegEdit_SetBit");
|
|
|
|
ADC_Enable(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_ADC, ADC_DisablePasses)
|
|
|
|
{
|
|
|
|
//Clears the muxpos register
|
|
|
|
mock().expectOneCall("RegEdit_ClearRegister")
|
|
|
|
.withPointerParameter("reg", (void *) &ADC0.MUXPOS);
|
|
|
|
|
|
|
|
mock().expectOneCall("RegEdit_ClearBit")
|
|
|
|
.withPointerParameter("reg", (void *) &ADC0.CTRLA)
|
|
|
|
.withUnsignedIntParameter("bit_num", 0);
|
2024-07-27 01:19:05 +00:00
|
|
|
|
|
|
|
mock().expectOneCall("RegEdit_SetNum")
|
|
|
|
.withPointerParameter("reg", (void*) &PORTA.OUT)
|
|
|
|
.ignoreOtherParameters();
|
|
|
|
|
|
|
|
mock().expectOneCall("RegEdit_SetNum")
|
|
|
|
.withPointerParameter("reg", (void*) &PORTA.DIR)
|
|
|
|
.ignoreOtherParameters();
|
2024-07-25 00:21:29 +00:00
|
|
|
|
|
|
|
ADC_Disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static uint16_t ADC_ReadValueFake(uint8_t pin_num)
|
|
|
|
{
|
|
|
|
return 512;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_GROUP(test_ADCRead)
|
|
|
|
{
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
UT_PTR_SET(ADC_ReadValue, ADC_ReadValueFake);
|
|
|
|
}
|
|
|
|
|
|
|
|
void teardown()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(test_ADCRead, FunctionPointerSwapWorks)
|
|
|
|
{
|
|
|
|
uint16_t value = ADC_ReadValue(0);
|
|
|
|
|
|
|
|
LONGS_EQUAL(512, value);
|
|
|
|
}
|
|
|
|
|