2024-09-18 19:48:01 +00:00
|
|
|
/*
|
|
|
|
* Author: Jake G
|
|
|
|
* Date: 2024
|
|
|
|
* filename: test_load.cpp
|
|
|
|
* description:
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 "load.h"
|
|
|
|
#include "MockADC.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_GROUP(test_load)
|
|
|
|
{
|
2024-09-23 19:43:24 +00:00
|
|
|
uint8_t adc_pin;
|
|
|
|
uint8_t load_pin;
|
2024-09-18 19:48:01 +00:00
|
|
|
void setup()
|
|
|
|
{
|
2024-09-23 19:43:24 +00:00
|
|
|
adc_pin = 4;
|
|
|
|
load_pin = 7;
|
2024-09-23 20:12:53 +00:00
|
|
|
MockADC_ZeroIndex();
|
|
|
|
Load_SoftResetDisabledLoads();
|
2024-09-18 19:48:01 +00:00
|
|
|
}
|
|
|
|
void teardown()
|
|
|
|
{
|
|
|
|
mock().checkExpectations();
|
|
|
|
mock().clear();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(test_load, LoadPass)
|
|
|
|
{
|
|
|
|
CHECK_TRUE(true);
|
|
|
|
}
|
|
|
|
|
2024-09-23 19:43:24 +00:00
|
|
|
void setup_adc_expectations(uint8_t adc_pin)
|
2024-09-18 19:48:01 +00:00
|
|
|
{
|
|
|
|
mock().expectOneCall("ADC_Init")
|
2024-09-23 19:43:24 +00:00
|
|
|
.withUnsignedIntParameter("pin_num", adc_pin);
|
2024-09-18 19:48:01 +00:00
|
|
|
|
|
|
|
mock().expectOneCall("ADC_Enable");
|
|
|
|
mock().expectOneCall("ADC_ReadValue_Impl")
|
2024-09-23 19:43:24 +00:00
|
|
|
.withUnsignedIntParameter("pin_num", adc_pin);
|
2024-09-18 19:48:01 +00:00
|
|
|
mock().expectOneCall("ADC_Disable");
|
2024-09-23 19:43:24 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 04:44:42 +00:00
|
|
|
void expect_porta_disabled(uint8_t load_pin, bool output_level)
|
2024-09-23 19:43:24 +00:00
|
|
|
{
|
2024-10-19 04:44:42 +00:00
|
|
|
mock().expectOneCall("RegEdit_IsBitSet")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.OUT)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin)
|
|
|
|
.andReturnValue(output_level);
|
|
|
|
|
2024-09-18 19:48:01 +00:00
|
|
|
mock().expectOneCall("RegEdit_ClearBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.OUT)
|
2024-09-23 19:43:24 +00:00
|
|
|
.withUnsignedIntParameter("bit_num", load_pin);
|
2024-09-18 19:48:01 +00:00
|
|
|
}
|
2024-09-21 01:42:35 +00:00
|
|
|
|
2024-10-19 04:44:42 +00:00
|
|
|
void expect_porta_enabled(uint8_t load_pin, bool output_level)
|
2024-09-23 19:08:54 +00:00
|
|
|
{
|
2024-10-19 04:44:42 +00:00
|
|
|
mock().expectOneCall("RegEdit_IsBitSet")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.OUT)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin)
|
|
|
|
.andReturnValue(output_level);
|
|
|
|
|
2024-09-23 19:43:24 +00:00
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.DIR)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin);
|
2024-09-23 19:08:54 +00:00
|
|
|
|
2024-09-23 19:43:24 +00:00
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTA.OUT)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin);
|
|
|
|
}
|
|
|
|
|
2024-10-19 04:52:40 +00:00
|
|
|
void expect_portb_disabled(uint8_t load_pin, bool output_level)
|
2024-09-23 19:43:24 +00:00
|
|
|
{
|
2024-10-19 04:52:40 +00:00
|
|
|
mock().expectOneCall("RegEdit_IsBitSet")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTB.OUT)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin)
|
|
|
|
.andReturnValue(output_level);
|
|
|
|
|
2024-09-23 19:08:54 +00:00
|
|
|
mock().expectOneCall("RegEdit_ClearBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTB.OUT)
|
2024-09-23 19:43:24 +00:00
|
|
|
.withUnsignedIntParameter("bit_num", load_pin);
|
|
|
|
}
|
|
|
|
|
2024-09-23 19:53:35 +00:00
|
|
|
|
2024-10-19 04:52:40 +00:00
|
|
|
void expect_portb_enabled(uint8_t load_pin, bool output_level)
|
2024-09-23 19:53:35 +00:00
|
|
|
{
|
2024-10-19 04:52:40 +00:00
|
|
|
mock().expectOneCall("RegEdit_IsBitSet")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTB.OUT)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin)
|
|
|
|
.andReturnValue(output_level);
|
|
|
|
|
2024-09-23 19:53:35 +00:00
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTB.DIR)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin);
|
|
|
|
|
|
|
|
mock().expectOneCall("RegEdit_SetBit")
|
|
|
|
.withPointerParameter("reg", (void *) &PORTB.OUT)
|
|
|
|
.withUnsignedIntParameter("bit_num", load_pin);
|
|
|
|
}
|
|
|
|
|
2024-09-23 19:43:24 +00:00
|
|
|
TEST(test_load, PortAHandlerDisabledHigh)
|
|
|
|
{
|
|
|
|
MockADC_PushValue(HIGHTHRESH);
|
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:44:42 +00:00
|
|
|
expect_porta_disabled(load_pin, true);
|
2024-09-23 19:43:24 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortA(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_load, PortAHandlerDisabledLow)
|
|
|
|
{
|
|
|
|
MockADC_PushValue(LOWTHRESH);
|
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:44:42 +00:00
|
|
|
expect_porta_disabled(load_pin, false);
|
2024-09-23 19:43:24 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortA(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
2024-10-19 04:44:42 +00:00
|
|
|
TEST(test_load, PortAHandlerEnabledRisingEdgeLO)
|
|
|
|
{
|
|
|
|
//Start from the rising edge.
|
|
|
|
MockADC_PushValue(HYSTERESIS_LO - 1);
|
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
|
|
|
expect_porta_enabled(load_pin, false);
|
|
|
|
|
|
|
|
Load_HandleLoadPortA(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_load, PortAHandlerEnabledRisingEdgeHI)
|
2024-09-23 19:43:24 +00:00
|
|
|
{
|
2024-10-19 04:44:42 +00:00
|
|
|
//Start from the rising edge above lo.
|
|
|
|
MockADC_PushValue(HYSTERESIS_HI - 1);
|
2024-09-23 19:43:24 +00:00
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:44:42 +00:00
|
|
|
expect_porta_enabled(load_pin, true);
|
2024-09-23 19:43:24 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortA(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_load, PortAHandlerDisabledAboveTarget)
|
|
|
|
{
|
2024-10-19 04:44:42 +00:00
|
|
|
MockADC_PushValue(HYSTERESIS_HI + 1);
|
2024-09-23 19:43:24 +00:00
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:44:42 +00:00
|
|
|
expect_porta_disabled(load_pin, true);
|
2024-09-23 19:43:24 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortA(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
2024-09-23 20:12:53 +00:00
|
|
|
TEST(test_load, PortAHandlerDisablesUntilPoweReset)
|
|
|
|
{
|
2024-10-19 04:44:42 +00:00
|
|
|
MockADC_PushValue(HYSTERESIS_HI - 1);
|
2024-09-23 20:12:53 +00:00
|
|
|
MockADC_PushValue(HIGHTHRESH);
|
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:44:42 +00:00
|
|
|
expect_porta_disabled(load_pin, true);
|
2024-09-23 20:12:53 +00:00
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:44:42 +00:00
|
|
|
expect_porta_disabled(load_pin, false);
|
2024-09-23 20:12:53 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortA(adc_pin, load_pin);
|
|
|
|
Load_HandleLoadPortA(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
2024-09-23 19:53:35 +00:00
|
|
|
|
|
|
|
TEST(test_load, PortBHandlerDisabledHigh)
|
|
|
|
{
|
|
|
|
MockADC_PushValue(HIGHTHRESH);
|
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:52:40 +00:00
|
|
|
expect_portb_disabled(load_pin, true);
|
2024-09-23 19:53:35 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortB(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_load, PortBHandlerDisabledLow)
|
2024-09-23 19:43:24 +00:00
|
|
|
{
|
2024-09-23 19:53:35 +00:00
|
|
|
MockADC_PushValue(LOWTHRESH);
|
2024-09-23 19:43:24 +00:00
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:52:40 +00:00
|
|
|
expect_portb_disabled(load_pin, false);
|
2024-09-23 19:08:54 +00:00
|
|
|
|
2024-09-23 19:43:24 +00:00
|
|
|
Load_HandleLoadPortB(adc_pin, load_pin);
|
2024-09-23 19:08:54 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 19:53:35 +00:00
|
|
|
|
2024-10-19 04:52:40 +00:00
|
|
|
TEST(test_load, PortBHandlerEnabledRisingEdgeLO)
|
2024-09-23 19:53:35 +00:00
|
|
|
{
|
2024-10-19 04:52:40 +00:00
|
|
|
//Start from the rising edge.
|
|
|
|
MockADC_PushValue(HYSTERESIS_LO - 1);
|
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
|
|
|
expect_portb_enabled(load_pin, false);
|
|
|
|
|
|
|
|
Load_HandleLoadPortB(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(test_load, PortBHandlerEnabledRisingEdgeHI)
|
|
|
|
{
|
|
|
|
//Start from the rising edge.
|
2024-10-19 04:44:42 +00:00
|
|
|
MockADC_PushValue(HYSTERESIS_HI - 1);
|
2024-09-23 19:53:35 +00:00
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:52:40 +00:00
|
|
|
expect_portb_enabled(load_pin, true);
|
2024-09-23 19:53:35 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortB(adc_pin, load_pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(test_load, PortBHandlerDisabledAboveTarget)
|
|
|
|
{
|
2024-10-19 04:44:42 +00:00
|
|
|
MockADC_PushValue(HYSTERESIS_HI);
|
2024-09-23 19:53:35 +00:00
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:52:40 +00:00
|
|
|
expect_portb_disabled(load_pin, true);
|
2024-09-23 19:53:35 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortB(adc_pin, load_pin);
|
|
|
|
}
|
2024-09-23 20:12:53 +00:00
|
|
|
|
|
|
|
TEST(test_load, PortBHandlerDisablesUntilPoweReset)
|
|
|
|
{
|
2024-10-19 04:44:42 +00:00
|
|
|
MockADC_PushValue(HYSTERESIS_HI - 1);
|
2024-09-23 20:12:53 +00:00
|
|
|
MockADC_PushValue(HIGHTHRESH);
|
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:52:40 +00:00
|
|
|
expect_portb_disabled(load_pin, true);
|
2024-09-23 20:12:53 +00:00
|
|
|
|
|
|
|
setup_adc_expectations(adc_pin);
|
2024-10-19 04:52:40 +00:00
|
|
|
expect_portb_disabled(load_pin, false);
|
2024-09-23 20:12:53 +00:00
|
|
|
|
|
|
|
Load_HandleLoadPortB(adc_pin, load_pin);
|
|
|
|
Load_HandleLoadPortB(adc_pin, load_pin);
|
|
|
|
}
|