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

View File

@ -27,6 +27,12 @@
*/
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
*/

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 adc_channel = 3;
uint8_t pin_num = 3;
uint8_t pin_num;
//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
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &DDRB)
.withUnsignedIntParameter("bit_num", pin_num);
//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 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
//
//Clears the last two bits first.
mock().expectOneCall("RegEdit_AND_Num")
.withPointerParameter("reg", (void *) &ADMUX)
.withUnsignedIntParameter("num", 0xFC);
/*
//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);
//Selects the ADC pin/channel
mock().expectOneCall("RegEdit_OR_Num")
.withPointerParameter("reg", (void *) &ADMUX)
.withUnsignedIntParameter("num", adc_channel);
//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);
ADC_Init(adc_channel);
}
}
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)