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;
}
@ -28,10 +28,10 @@ void ADC_Setup(void)
return;
}
void ADC_Init(uint8_t pin_num)
void ADC_Init(uint8_t adc_chan)
{
mock_c()->actualCall("ADC_Init")
->withUnsignedIntParameters("pin_num", pin_num);
->withUnsignedIntParameters("adc_chan", adc_chan);
}
void ADC_Enable(void)
@ -44,10 +44,10 @@ void 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")
->withUnsignedIntParameters("pin_num", pin_num);
->withUnsignedIntParameters("adc_chan", adc_chan);
if(fake_index == 0){
return 0;
@ -55,7 +55,7 @@ uint16_t ADC_ReadValue_Impl(uint8_t pin_num)
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){

View File

@ -15,12 +15,12 @@
void ADC_Setup(void);
void ADC_SetPin(uint8_t pin_num);
void ADC_Init(uint8_t pin_num);
void ADC_SetPin(uint8_t adc_chan);
void ADC_Init(uint8_t adc_chan);
void ADC_Enable(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_ZeroIndex(void);

View File

@ -13,10 +13,25 @@
#include "RegEdit.h"
#include "avr/io.h"
#define MAX_PIN_NUM 7
#define MAX_CHANNEL_NUM 3
static bool IsInvalidPin(uint8_t pin_num) {
if (pin_num > MAX_PIN_NUM) {
static uint8_t GetChannelPinNum(uint8_t adc_chan) {
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 false;
@ -36,22 +51,22 @@ void ADC_Setup(void) {
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;
}
// set the direction to input
// RegEdit_ClearBit((void *)&PORTA.DIR, pin_num);
RegEdit_ClearBit((void *)&DDRB, adc_chan);
// Disable the pull-up resistor
// RegEdit_ClearBit((void *)&PORTA.OUT, pin_num);
RegEdit_ClearBit((void *)&PORTB, adc_chan);
// 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) + pin_num,
// RegEdit_SetBit((void *)(&PORTA.PIN0CTRL) + adc_chan,
// PORT_ISC_INPUT_DISABLE_gc);
}
@ -65,15 +80,15 @@ void ADC_Disable() {
// RegEdit_ClearBit((void *)&ADC0.CTRLA, 0);
}
void ADC_SetPin(uint8_t pin_num) {
if (IsInvalidPin(pin_num)) {
void ADC_SetPin(uint8_t adc_chan) {
if (IsInvalidChannel(adc_chan)) {
return;
}
// 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);
/* 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.
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
* 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
* 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.
*
*/
void ADC_Init(uint8_t pin_num);
void ADC_Init(uint8_t adc_chan);
/**
* @brief Enables the ADC
@ -40,12 +40,12 @@ void ADC_Disable();
/**
* @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
* 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
@ -61,8 +61,8 @@ void ADC_Setup(void);
/**
* @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

View File

@ -3,7 +3,7 @@
#endif
#include "main.h"
//#include "ADC.h"
// #include "ADC.h"
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/delay.h>
@ -50,7 +50,7 @@ int main() {
while (1) {
UpdateButtonOutput(&btn1);
UpdateButtonInput(&btn1);
//MotorMoveTo(0);
// MotorMoveTo(0);
}
return 0;
@ -97,8 +97,8 @@ uint8_t ReadSpeed(void) {
uint8_t val = (uint8_t)(ADC >> 2);
//Map the speed value to the 100%-50% duty cycle range.
//val = (uint8_t)( (float)val / 255.0 ) * 100;
// Map the speed value to the 100%-50% duty cycle range.
// val = (uint8_t)( (float)val / 255.0 ) * 100;
return val;
}
@ -142,7 +142,7 @@ void MotorMoveTo(uint8_t target) {
uint8_t on_delay = ReadSpeed();
uint8_t pos = (uint8_t)(ReadFader() >> 2);
uint8_t idx =0;
uint8_t idx = 0;
while (diff(target, pos) > 8) {
on_delay = ReadSpeed();
@ -158,7 +158,7 @@ void MotorMoveTo(uint8_t target) {
_delay_us(MOTOR_PULSE);
}
MotorCoast();
for (;idx < 255; idx++) {
for (; idx < 255; idx++) {
_delay_us(MOTOR_PULSE);
}
}

View File

@ -73,12 +73,33 @@ TEST(test_ADC, ADC_SetupSetsRegisters)
ADC_Setup();
}
TEST(test_ADC, ADC_InitRejectsInvalidChannels)
{
for(uint8_t i = 4; i < 255; i++){
ADC_Init(i);
}
}
TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters)
{
//PB3/ADC3
//uint8_t adc_channel = 3;
uint8_t pin_num = 3;
//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
@ -102,7 +123,7 @@ TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters)
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
*/
ADC_Init(7);
ADC_Init(3);
}
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)