Added onto init tests.

This commit is contained in:
Jake Goodwin 2025-02-22 22:00:44 -08:00
parent b103fe2b97
commit 6167089a28
6 changed files with 72 additions and 36 deletions

View File

@ -17,7 +17,7 @@ static bool is_setup = false;
void ADC_SetPin(uint8_t pin_num) void ADC_SetPin(uint8_t adc_chan)
{ {
return; return;
} }
@ -28,10 +28,10 @@ void ADC_Setup(void)
return; return;
} }
void ADC_Init(uint8_t pin_num) void ADC_Init(uint8_t adc_chan)
{ {
mock_c()->actualCall("ADC_Init") mock_c()->actualCall("ADC_Init")
->withUnsignedIntParameters("pin_num", pin_num); ->withUnsignedIntParameters("adc_chan", adc_chan);
} }
void ADC_Enable(void) void ADC_Enable(void)
@ -44,10 +44,10 @@ void ADC_Disable()
mock_c()->actualCall("ADC_Disable"); mock_c()->actualCall("ADC_Disable");
} }
uint16_t ADC_ReadValue_Impl(uint8_t pin_num) uint16_t ADC_ReadValue_Impl(uint8_t adc_chan)
{ {
mock_c()->actualCall("ADC_ReadValue_Impl") mock_c()->actualCall("ADC_ReadValue_Impl")
->withUnsignedIntParameters("pin_num", pin_num); ->withUnsignedIntParameters("adc_chan", adc_chan);
if(fake_index == 0){ if(fake_index == 0){
return 0; return 0;
@ -55,7 +55,7 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num)
return fake_data[--fake_index]; return fake_data[--fake_index];
} }
uint16_t (*ADC_ReadValue)(uint8_t pin_num) = ADC_ReadValue_Impl; uint16_t (*ADC_ReadValue)(uint8_t adc_chan) = ADC_ReadValue_Impl;
void MockADC_PushValue(uint16_t value){ void MockADC_PushValue(uint16_t value){

View File

@ -15,12 +15,12 @@
void ADC_Setup(void); void ADC_Setup(void);
void ADC_SetPin(uint8_t pin_num); void ADC_SetPin(uint8_t adc_chan);
void ADC_Init(uint8_t pin_num); void ADC_Init(uint8_t adc_chan);
void ADC_Enable(void); void ADC_Enable(void);
void ADC_Disable(void); void ADC_Disable(void);
extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); extern uint16_t (*ADC_ReadValue)(uint8_t adc_chan);
void MockADC_PushValue(uint16_t value); void MockADC_PushValue(uint16_t value);
void MockADC_ZeroIndex(void); void MockADC_ZeroIndex(void);

View File

@ -13,10 +13,25 @@
#include "RegEdit.h" #include "RegEdit.h"
#include "avr/io.h" #include "avr/io.h"
#define MAX_PIN_NUM 7 #define MAX_CHANNEL_NUM 3
static bool IsInvalidPin(uint8_t pin_num) { static uint8_t GetChannelPinNum(uint8_t adc_chan) {
if (pin_num > MAX_PIN_NUM) { switch (adc_chan) {
case 0:
return PB5;
case 1:
return PB2;
case 2:
return PB4;
case 3:
return PB3;
default:
return 255; /*return invalid pin num.*/
}
}
static bool IsInvalidChannel(uint8_t adc_chan) {
if (adc_chan > MAX_CHANNEL_NUM) {
return true; return true;
} }
return false; return false;
@ -36,22 +51,22 @@ void ADC_Setup(void) {
RegEdit_AND_Num((void *)&ADCSRB, (1 << ADTS2) | (0 << ADTS1) | (1 << ADTS0)); RegEdit_AND_Num((void *)&ADCSRB, (1 << ADTS2) | (0 << ADTS1) | (1 << ADTS0));
} }
void ADC_Init(uint8_t pin_num) { void ADC_Init(uint8_t adc_chan) {
if (IsInvalidPin(pin_num)) { if (IsInvalidChannel(adc_chan)) {
return; return;
} }
// set the direction to input // set the direction to input
// RegEdit_ClearBit((void *)&PORTA.DIR, pin_num); RegEdit_ClearBit((void *)&DDRB, adc_chan);
// Disable the pull-up resistor // Disable the pull-up resistor
// RegEdit_ClearBit((void *)&PORTA.OUT, pin_num); RegEdit_ClearBit((void *)&PORTB, adc_chan);
// 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
// memory and means we don't need a switch statment. // memory and means we don't need a switch statment.
// RegEdit_SetBit((void *)(&PORTA.PIN0CTRL) + pin_num, // RegEdit_SetBit((void *)(&PORTA.PIN0CTRL) + adc_chan,
// PORT_ISC_INPUT_DISABLE_gc); // PORT_ISC_INPUT_DISABLE_gc);
} }
@ -65,15 +80,15 @@ void ADC_Disable() {
// RegEdit_ClearBit((void *)&ADC0.CTRLA, 0); // RegEdit_ClearBit((void *)&ADC0.CTRLA, 0);
} }
void ADC_SetPin(uint8_t pin_num) { void ADC_SetPin(uint8_t adc_chan) {
if (IsInvalidPin(pin_num)) { if (IsInvalidChannel(adc_chan)) {
return; return;
} }
// RegEdit_ClearRegister((void *)&ADC0.MUXPOS); // RegEdit_ClearRegister((void *)&ADC0.MUXPOS);
// RegEdit_SetNum((void *)&ADC0.MUXPOS, pin_num); // RegEdit_SetNum((void *)&ADC0.MUXPOS, adc_chan);
} }
uint16_t ADC_ReadValue_Impl(uint8_t pin_num) { uint16_t ADC_ReadValue_Impl(uint8_t adc_chan) {
// 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 */
@ -94,4 +109,4 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num) {
} }
// Set the default for the function pointer. // Set the default for the function pointer.
uint16_t (*ADC_ReadValue)(uint8_t pin_num) = ADC_ReadValue_Impl; uint16_t (*ADC_ReadValue)(uint8_t adc_chan) = ADC_ReadValue_Impl;

View File

@ -16,7 +16,7 @@
/** /**
* @brief Initializes the AVR hardware in order to accept * @brief Initializes the AVR hardware in order to accept
* Input for ADC usage. * Input for ADC usage.
* @param pin_num The number of the pin 0-7 you are initializing. * @param adc_chan The channel of the pin 0-7 you are initializing.
* *
* This function only makes use of PORTA by default. It sets the direction * This function only makes use of PORTA by default. It sets the direction
* register to input, disables the pull-up resistor and also diables interrupts * register to input, disables the pull-up resistor and also diables interrupts
@ -25,7 +25,7 @@
* This in turn helps reduce noise when using the ADC. * This in turn helps reduce noise when using the ADC.
* *
*/ */
void ADC_Init(uint8_t pin_num); void ADC_Init(uint8_t adc_chan);
/** /**
* @brief Enables the ADC * @brief Enables the ADC
@ -40,12 +40,12 @@ void ADC_Disable();
/** /**
* @brief Reads ADC value into variable * @brief Reads ADC value into variable
* *
* @param pin_num The bin number of the ADC pin being read. * @param adc_chan The bin number of the ADC pin being read.
* *
* This function depends on the ADC already being initialized and enabled * This function depends on the ADC already being initialized and enabled
* before being called. * before being called.
*/ */
extern uint16_t (*ADC_ReadValue)(uint8_t pin_num); extern uint16_t (*ADC_ReadValue)(uint8_t adc_chan);
/** /**
* @brief Sets up the ADC * @brief Sets up the ADC
@ -61,8 +61,8 @@ void ADC_Setup(void);
/** /**
* @brief Sets the pin used in the MUX for ADC0. * @brief Sets the pin used in the MUX for ADC0.
* *
* @param pin_num The number of the pin in Port A. * @param adc_chan The number of the pin in Port A.
*/ */
void ADC_SetPin(uint8_t pin_num); void ADC_SetPin(uint8_t adc_chan);
#endif // ADC_H #endif // ADC_H

View File

@ -73,12 +73,33 @@ TEST(test_ADC, ADC_SetupSetsRegisters)
ADC_Setup(); ADC_Setup();
} }
TEST(test_ADC, ADC_InitRejectsInvalidChannels)
{
for(uint8_t i = 4; i < 255; i++){
ADC_Init(i);
}
}
TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters) TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters)
{ {
//PB3/ADC3 //PB3/ADC3
//uint8_t adc_channel = 3;
uint8_t pin_num = 3;
//Check it disables the pin's digital circuitry //Check it disables the pin's digital circuitry
//Check it sets the pin for input
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &DDRB)
.withUnsignedIntParameter("bit_num", pin_num);
//Check that the pull-up resistor is disabled.
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTB)
.withUnsignedIntParameter("bit_num", pin_num);
//Check that the ADC pin is selected in ADMUX //Check that the ADC pin is selected in ADMUX
@ -102,7 +123,7 @@ TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters)
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc); .withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
*/ */
ADC_Init(7); ADC_Init(3);
} }
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters) TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)