Added testing for the pin number and resolving it from a adc channel input.

This commit is contained in:
Jake Goodwin 2025-02-23 09:50:09 -08:00
parent 6167089a28
commit a00e176675
3 changed files with 53 additions and 49 deletions

View File

@ -15,7 +15,14 @@
#define MAX_CHANNEL_NUM 3 #define MAX_CHANNEL_NUM 3
static uint8_t GetChannelPinNum(uint8_t adc_chan) { static bool IsInvalidChannel(uint8_t adc_chan) {
if (adc_chan > MAX_CHANNEL_NUM) {
return true;
}
return false;
}
uint8_t ADC_GetChannelPinNum(uint8_t adc_chan) {
switch (adc_chan) { switch (adc_chan) {
case 0: case 0:
return PB5; return PB5;
@ -30,13 +37,6 @@ static uint8_t GetChannelPinNum(uint8_t adc_chan) {
} }
} }
static bool IsInvalidChannel(uint8_t adc_chan) {
if (adc_chan > MAX_CHANNEL_NUM) {
return true;
}
return false;
}
void ADC_Setup(void) { void ADC_Setup(void) {
// Clear the register, set VCC as ref, ADC0 as channel and // Clear the register, set VCC as ref, ADC0 as channel and
// leave result right adjusted. // leave result right adjusted.
@ -57,17 +57,22 @@ void ADC_Init(uint8_t adc_chan) {
return; return;
} }
// get the associated pin number.
uint8_t pin_num = ADC_GetChannelPinNum(adc_chan);
// set the direction to input // set the direction to input
RegEdit_ClearBit((void *)&DDRB, adc_chan); RegEdit_ClearBit((void *)&DDRB, pin_num);
// Disable the pull-up resistor // Disable the pull-up resistor
RegEdit_ClearBit((void *)&PORTB, adc_chan); RegEdit_ClearBit((void *)&PORTB, pin_num);
// Disable input buffer // Set the adc mux reg for our channel.
// We do some kinda nasty address addition but it saves
// memory and means we don't need a switch statment. // Clear the existing mux bits.
// RegEdit_SetBit((void *)(&PORTA.PIN0CTRL) + adc_chan, RegEdit_AND_Num((void *)&ADMUX, 0xFC);
// PORT_ISC_INPUT_DISABLE_gc);
// Set the correct bits
RegEdit_OR_Num((void *)&ADMUX, adc_chan);
} }
void ADC_Enable(void) { void ADC_Enable(void) {

View File

@ -27,6 +27,12 @@
*/ */
void ADC_Init(uint8_t adc_chan); void ADC_Init(uint8_t adc_chan);
/**
* @brief Returns the pin number for the adc channel or 255 on an error.
* @param The ADC channel.
*/
uint8_t ADC_GetChannelPinNum(uint8_t adc_chan);
/** /**
* @brief Enables the ADC * @brief Enables the ADC
*/ */

View File

@ -80,50 +80,43 @@ TEST(test_ADC, ADC_InitRejectsInvalidChannels)
} }
} }
TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters)
TEST(test_ADC, ADC_InitPortbWorksWithValidChannels)
{ {
//PB3/ADC3 uint8_t pin_num;
//uint8_t adc_channel = 3;
uint8_t pin_num = 3;
//Check it disables the pin's digital circuitry for(uint8_t adc_channel = 0; adc_channel <= 3; adc_channel++)
{
pin_num = ADC_GetChannelPinNum(adc_channel);
//Check it sets the pin for input //Check it disables the pin's digital circuitry
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &DDRB) //Check it sets the pin for input
.withUnsignedIntParameter("bit_num", pin_num); mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &DDRB)
.withUnsignedIntParameter("bit_num", pin_num);
//Check that the pull-up resistor is disabled. //Check that the pull-up resistor is disabled.
mock().expectOneCall("RegEdit_ClearBit") mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTB) .withPointerParameter("reg", (void *) &PORTB)
.withUnsignedIntParameter("bit_num", pin_num); .withUnsignedIntParameter("bit_num", pin_num);
//Check that the ADC pin is selected in ADMUX //Check that the ADC pin is selected in ADMUX
//Clears the last two bits first.
mock().expectOneCall("RegEdit_AND_Num")
.withPointerParameter("reg", (void *) &ADMUX)
.withUnsignedIntParameter("num", 0xFC);
// //Selects the ADC pin/channel
mock().expectOneCall("RegEdit_OR_Num")
.withPointerParameter("reg", (void *) &ADMUX)
.withUnsignedIntParameter("num", adc_channel);
/* ADC_Init(adc_channel);
//Check for setting the direction to input. }
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.DIR)
.withUnsignedIntParameter("bit_num", 7);
//Check that the pullup is off
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", 7);
//Set the ISC(input sense config) to disable digital input
//buffering and reduce the noise on ADC usage.
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &PORTA.PIN7CTRL)
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
*/
ADC_Init(3);
} }
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters) TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)