Compare commits

...

37 commits
v0.2.2 ... main

Author SHA1 Message Date
jakeg00dwin
ce747adb9a Updated version number 2025-02-24 18:54:19 -08:00
jakeg00dwin
6d73b3f8aa Added define for the saved position margin, loosened up it's value and increased the rampdown distance. 2025-02-24 18:53:55 -08:00
jakeg00dwin
d7049191bc Merge branch 'dev' 2025-02-24 18:06:13 -08:00
jakeg00dwin
b93a01f8c5 Added the rampdown distance as a define in the main.h file. 2025-02-24 18:05:29 -08:00
jakeg00dwin
33ddb0a4e9 Added a rampdown section to the speed. 2025-02-24 17:31:30 -08:00
jakeg00dwin
3fa844b719 Updated version number and added instructions. 2025-02-23 12:50:29 -08:00
jakeg00dwin
87b1cb9acd removed the duplicate bit shifting in favor of single operation in function. 2025-02-23 12:29:52 -08:00
b64c306250 Merge branch 'dev' 2025-02-23 11:54:34 -08:00
deaa59f5a2 Changed names for save locations. 2025-02-23 11:53:23 -08:00
91ee864d23 Removed the use of the tested ADC interface 2025-02-23 11:53:14 -08:00
78fdf54421 Adjusted value for more accurate input. 2025-02-23 10:58:42 -08:00
c2ce883adb Mocking tests now passing after fixing param name. 2025-02-23 10:58:07 -08:00
60b83865cd Added mock test runner function to menu. 2025-02-23 10:56:48 -08:00
82c16a7ddc Adjusted function signiture for ADC_ReadValue() 2025-02-23 10:56:38 -08:00
33b70d3283 Included the ADC module in main's linked libs 2025-02-23 10:56:13 -08:00
de463a3469 Cleaned up ADC module 2025-02-23 10:56:00 -08:00
2add13a745 Modified ADC mock module to have the new function signiture for the ADC_ReadValue() 2025-02-23 10:55:46 -08:00
6736e30dd1 Added extra testing/assert line for the returned error value. 2025-02-23 10:34:23 -08:00
c07fc415bf Added code to pass the ADC timeout feature 2025-02-23 10:32:22 -08:00
58ab3c12eb Added test for the ADC reading timeout feature 2025-02-23 10:32:13 -08:00
fc3d0d11a5 removed now uneeded functions. 2025-02-23 10:03:21 -08:00
90891545d1 Added code to pass the ADC_Disable() function. 2025-02-23 10:00:38 -08:00
352ee9e6a8 Added test for ADC_Disable() 2025-02-23 09:59:52 -08:00
969b852cb3 Added test for adc enable.
+ code for testing ADC enable.
+ code for passing ADC enable function test.
2025-02-23 09:58:30 -08:00
a00e176675 Added testing for the pin number and resolving it from a adc channel input. 2025-02-23 09:50:09 -08:00
6167089a28 Added onto init tests. 2025-02-22 22:00:44 -08:00
b103fe2b97 removed error line, causes issues with LSP. 2025-02-22 21:31:52 -08:00
jakeg00dwin
b377eab7c0 Updated with comments on prescaler usage for F_CPU 2025-02-20 16:05:23 -08:00
jakeg00dwin
43d15247ad Updated version number from changes. 2025-02-20 15:55:34 -08:00
jakeg00dwin
a29e16963f Merge branch 'dev' 2025-02-20 15:50:00 -08:00
jakeg00dwin
d3d69ca180 Added linux include dir for AVR. 2025-02-20 15:46:12 -08:00
jakeg00dwin
05b6de17d0 Added new F_CPU define. 2025-02-20 15:45:55 -08:00
jakeg00dwin
b2a3dcbed4 Added line for inclusion of AVR stuff on linux machines. 2025-02-20 15:45:44 -08:00
jakeg00dwin
49d8ca037b Adjusted file to use F_CPU after DIV8 and allow speed control during movement. 2025-02-20 15:45:29 -08:00
jakeg00dwin
c2ed2cead0 Added fuse flashing commands for default 9.6MHz 2025-02-20 15:44:55 -08:00
jakeg00dwin
4d229a973c updated version number 2025-02-18 19:30:01 -08:00
jakeg00dwin
cc57692a1d Updated the motor pulse define, too fast of a pulse doesn't wakeup the motor driver. 2025-02-18 19:29:30 -08:00
14 changed files with 239 additions and 203 deletions

View file

@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)
# Use the fancy version substitution
project(main
VERSION 0.2.2
VERSION 0.3.1
DESCRIPTION "template for cmake + cpputest"
LANGUAGES C CXX
)
@ -71,6 +71,7 @@ include_directories(
./inc
/usr/local/avr/include/avr
/usr/local/avr/include
/usr/local/avr/avr/include #for Linux
)
add_subdirectory(src)

View file

@ -36,6 +36,12 @@ The speed can be adjusted by changing the input value of the speed pot. The
speed adjustment affects the on/off ratio of the motor and has a programmed
minimum speed value in the function to prevent endless looping.
### Move to secondary Position
When the motor has already preformed a move to position from a quick
button press, you can tap the button once more and it will go back to it's
previous position from your initial button press.
## Building Project(Windows)

View file

@ -15,9 +15,11 @@ set(AVR_MCU attiny13a)
#set(AVR_MCU avr64dd28) # Newer DX series, avrxmega2
#set(F_CPU 16000000UL)
#set(F_CPU 8000000)
set(F_CPU 9600000)
#set(F_CPU 4800000)
#set(F_CPU 8000000UL)
#set(F_CPU 9600000UL)#AVR without prescaler
set(F_CPU 1200000UL) #AVR (9.6MHz) with prescaler 8DIV
#set(F_CPU 4800000UL) #AVR without prescaler
#set(F_CPU 600000UL) #AVR (4.8MHz) with prescaler 8DIV
add_compile_definitions(F_CPU=${F_CPU})
# add_compile_definitions(MCU=atmega328p)

View file

@ -32,8 +32,8 @@
//Addresses in the eeprom for the two switch states
#define ROM_SP_ADR 0x0
#define ROM_SS1_ADR 0x1
#define ROM_SS2_ADR 0x2
#define POSITION1_ADR 0x1
#define POSITION2_ADR 0x2
#define ROM_EP_ADR 0x3
//Debounce check number.
@ -49,7 +49,10 @@
#define SPEED_PIN PB2 // Pin 7/ADC1
#define BUTTON_PIN PB4 // Pin 3 - Button input
#define MOTOR_PULSE 1 //uS motor base pulse
#define MOTOR_RAMPDOWN_DIST 32
#define MOTOR_PULSE 6 //uS motor base pulse
#define SAVED_POS_MARGIN 4
/*The timing of "ticks" is dependent on the AVR timer's counter register
@ -89,7 +92,6 @@ typedef struct {
uint8_t timer_enabled: 1;
uint8_t pressed_ticks;
uint8_t input_pin;
uint8_t output_pin;
}btn_state;
@ -106,7 +108,7 @@ void InitProg(void);
/**
* @brief Reads the ADC pin from the fader.
*/
uint16_t ReadFader(void);
uint8_t ReadFader(void);
/**

View file

@ -16,22 +16,16 @@ int fake_index = 0;
static bool is_setup = false;
void ADC_SetPin(uint8_t pin_num)
{
return;
}
void ADC_Setup(void)
{
is_setup = true;
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 +38,10 @@ void ADC_Disable()
mock_c()->actualCall("ADC_Disable");
}
uint16_t ADC_ReadValue_Impl(uint8_t pin_num)
uint16_t ADC_ReadValue_Impl(void)
{
mock_c()->actualCall("ADC_ReadValue_Impl")
->withUnsignedIntParameters("pin_num", pin_num);
mock_c()->actualCall("ADC_ReadValue_Impl");
if(fake_index == 0){
return 0;
@ -55,7 +49,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)(void) = ADC_ReadValue_Impl;
void MockADC_PushValue(uint16_t value){

View file

@ -15,12 +15,11 @@
void ADC_Setup(void);
void ADC_SetPin(uint8_t pin_num);
void ADC_Init(uint8_t pin_num);
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)(void);
void MockADC_PushValue(uint16_t value);
void MockADC_ZeroIndex(void);

11
otto.sh
View file

@ -224,12 +224,19 @@ run_c_tests () {
make AllTests && ./tests/AllTests -c -v
}
run_mock_tests () {
format_source_code
clear_cmake_cache
cmake -DUNIT_TESTING=ON -DCMAKE_VERBOSE_MAKEFILE=${CMAKE_VERBOSE} ../
make Mock_Tests && ./tests/Mock_Tests -c -v
}
print_menu () {
echo "BUILD MENU:"
echo "0. Add Mock Module"
echo "1. Run Tests"
echo "2. Build Project(hex)"
echo "3. User Option"
echo "3. Run MockTests"
echo "4. Flash to AVR"
echo "5. Add new module to project"
echo "6. Delete module from project"
@ -264,7 +271,7 @@ main() {
3)
echo "You selected Option 3"
valid_choice=true
build_hex_optimized
run_mock_tests
;;
4)
echo "You selected Option 4"

View file

@ -13,15 +13,30 @@
#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 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;
case 1:
return PB2;
case 2:
return PB4;
case 3:
return PB3;
default:
return 255; /*return invalid pin num.*/
}
}
void ADC_Setup(void) {
// Clear the register, set VCC as ref, ADC0 as channel and
// leave result right adjusted.
@ -36,62 +51,61 @@ 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;
}
// get the associated pin number.
uint8_t pin_num = ADC_GetChannelPinNum(adc_chan);
// set the direction to input
// RegEdit_ClearBit((void *)&PORTA.DIR, pin_num);
RegEdit_ClearBit((void *)&DDRB, pin_num);
// Disable the pull-up resistor
// RegEdit_ClearBit((void *)&PORTA.OUT, pin_num);
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) + pin_num,
// 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) {
// Set the enable bit in the CTRLA register
// RegEdit_SetBit((void *)&ADC0.CTRLA, 0);
RegEdit_SetBit((void *)&ADCSRA, ADEN);
}
void ADC_Disable() {
// Clear the enable ADC flag
// RegEdit_ClearBit((void *)&ADC0.CTRLA, 0);
RegEdit_ClearBit((void *)&ADCSRA, ADEN);
}
void ADC_SetPin(uint8_t pin_num) {
if (IsInvalidPin(pin_num)) {
return;
uint16_t ADC_ReadValue_Impl(void) {
// start conversion.
RegEdit_SetBit((void *)&ADCSRA, ADSC);
/* Wait until ADC conversion done or it times out. */
uint8_t cycles = 1;
while (1) {
if (!RegEdit_IsBitSet((void *)&ADCSRA, ADSC)) {
break;
}
// if It times out return an invalid value for a 12bit ADC.
else if (cycles >= ADC_WAIT_TIMEOUT) {
return UINT16_MAX;
}
cycles++;
}
// RegEdit_ClearRegister((void *)&ADC0.MUXPOS);
// RegEdit_SetNum((void *)&ADC0.MUXPOS, pin_num);
}
uint16_t ADC_ReadValue_Impl(uint8_t pin_num) {
// RegEdit_SetNum((void *)&ADC0.COMMAND, ADC_STCONV_bm);
/* Wait until ADC conversion done */
/*
while (!(ADC0.INTFLAGS & ADC_RESRDY_bm)) {
;
}
*/
/* Clear the interrupt flag by writing 1: */
// ADC0.INTFLAGS = ADC_RESRDY_bm;
// uint16_t adc_val = (uint16_t)ADC0.RES;
uint16_t adc_val = 0;
adc_val = adc_val >> 5;
return adc_val;
uint16_t val = ADC;
return val;
}
// Set the default for the function pointer.
uint16_t (*ADC_ReadValue)(uint8_t pin_num) = ADC_ReadValue_Impl;
uint16_t (*ADC_ReadValue)(void) = ADC_ReadValue_Impl;

View file

@ -13,10 +13,16 @@
#include <stdbool.h>
#include <stdint.h>
/**
* @brief The number of loop cycles it waits for ADC conversion before
* timing out.
*/
#define ADC_WAIT_TIMEOUT 16
/**
* @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 +31,13 @@
* 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 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
@ -40,12 +52,11 @@ void ADC_Disable();
/**
* @brief Reads ADC value into variable
*
* @param pin_num 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)();
/**
* @brief Sets up the ADC
@ -58,11 +69,4 @@ extern uint16_t (*ADC_ReadValue)(uint8_t pin_num);
*/
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.
*/
void ADC_SetPin(uint8_t pin_num);
#endif // ADC_H

View file

@ -4,6 +4,7 @@ add_executable(${PROJECT_NAME}
target_link_libraries(${PROJECT_NAME}
#RegEdit
#ADC
#timer
)
@ -36,10 +37,11 @@ add_custom_target(bin ALL
endif()
# Setup for default 9.6MHz
if(NOT TARGET upload)
# Upload command (adjust according to your programmer)
add_custom_target(upload ALL
COMMAND avrdude -c ${PROGRAMMER} -P ${PORT} -p ${AVR_MCU} -B 125kHz -U flash:w:${CMAKE_PROJECT_NAME}.hex
COMMAND avrdude -c ${PROGRAMMER} -P ${PORT} -p ${AVR_MCU} -B 125kHz -U lfuse:w:0x6a:m -U hfuse:w:0xFF:m -U lock:w:0xFF:m -U flash:w:${CMAKE_PROJECT_NAME}.hex
DEPENDS hex
)
endif()

View file

@ -1,8 +1,9 @@
#ifndef F_CPU
#define F_CPU 4800000UL
#define F_CPU 1200000UL
#endif
#include "main.h"
//#include "ADC.h"
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/delay.h>
@ -32,7 +33,7 @@ volatile uint16_t tick_count;
// #############################
static inline void InitTimer0(void);
static void InitBtn(btn_state *b, uint8_t input_pin, uint8_t output_pin);
static void InitBtn(btn_state *b, uint8_t input_pin);
static void ClearButtonTimer(btn_state *b);
static void StartButtonTimer(btn_state *b);
static void CheckButtonLongpress(btn_state *b);
@ -62,7 +63,7 @@ void InitProg(void) {
/*Set the debounced state to all high*/
debounced_state = 0xFF;
InitBtn(&btn1, BUTTON_PIN, PIN_ACTIVE1);
InitBtn(&btn1, BUTTON_PIN);
/*Wait 5ms for pull-up resistors voltage to become stable.*/
_delay_ms(5);
@ -73,11 +74,11 @@ void InitProg(void) {
// Checks against a bit pattern we defined to represent the start of data.
if (eeprom_read_byte((uint8_t *)ROM_SP_ADR) == START_PATTERN) {
MotorMoveTo(eeprom_read_byte((uint8_t *)ROM_SS1_ADR));
MotorMoveTo(eeprom_read_byte((uint8_t *)POSITION1_ADR));
} else {
// otherwise we write the init values for the start pattern and states.
eeprom_write_byte((uint8_t *)ROM_SP_ADR, START_PATTERN);
eeprom_write_byte((uint8_t *)ROM_SS1_ADR, 0x7F);
eeprom_write_byte((uint8_t *)POSITION1_ADR, 0x7F);
}
InitTimer0();
@ -85,6 +86,7 @@ void InitProg(void) {
uint8_t ReadSpeed(void) {
// Initialize ADC
ADMUX = (0 << MUX1) | (1 << MUX0); // Select ADC1 (PB2)
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
@ -92,26 +94,25 @@ uint8_t ReadSpeed(void) {
while (ADCSRA & (1 << ADSC)) {
} // Wait for conversion to finish
uint8_t val = (uint8_t)(ADC >> 2);
// We want to set a minimum acceptable speed.
if (val < 32) {
val = 32;
}
//Normally a bitshift of 2 is done for 8bit ADC values,
//however we want to divide by two after this as well so we
//do a third shift followed by an addition of 127 to yield a mapping of
//approximatly 50%-96.6% duty cycle range.
uint8_t val = (uint8_t)(ADC >> 3) + 127;
return val;
}
// change to ReadFader(void)
uint16_t ReadFader(void) {
// Initialize ADC
uint8_t ReadFader(void) {
// Initialize ADC
ADMUX = (1 << MUX1) | (1 << MUX0); // Select ADC3 (PB3)
ADCSRA = (1 << ADEN) | (1 << ADPS1) | (1 << ADPS0); // Enable ADC, prescaler 8
ADCSRA |= (1 << ADSC); // Start conversion
while (ADCSRA & (1 << ADSC)) {
} // Wait for conversion to finish
return ADC;
return (uint8_t)(ADC >> 2);
}
/*
@ -127,24 +128,37 @@ static inline uint8_t diff(uint8_t a, uint8_t b) {
return b - a;
}
void MotorSetSavePos() {
uint8_t pos = (uint8_t)(ReadFader() >> 2);
eeprom_write_byte((uint8_t *)ROM_SS1_ADR, pos);
void MotorSetSavePos(uint8_t *ADR) {
uint8_t pos = ReadFader();
eeprom_write_byte((uint8_t *)ADR, pos);
return;
}
uint8_t MotorGetSavedPos(void) {
return (uint8_t)eeprom_read_byte((uint8_t *)ROM_SS1_ADR);
uint8_t MotorGetSavedPos(uint8_t *ADR) {
return (uint8_t)eeprom_read_byte((uint8_t *)ADR);
}
void MotorMoveTo(uint8_t target) {
uint8_t on_delay = ReadFader();
uint8_t off_delay = 255 - on_delay;
uint8_t pos = (uint8_t)(ReadFader() >> 2);
uint8_t on_delay = ReadSpeed();
uint8_t pos = ReadFader();
uint8_t delta = diff(target, pos);
uint8_t idx = 0;
while (delta > 0) {
pos = ReadFader();
delta = diff(target, pos);
//Impliment a ramp down when near target position.
if(delta <= MOTOR_RAMPDOWN_DIST){
//Ignore speed value and set for 50% speed.
on_delay = 127;
}
//Otherwise use the speed input.
else{
on_delay = ReadSpeed();
}
while (diff(target, pos) > 8) {
pos = (uint8_t)(ReadFader() >> 2);
if (target > pos) {
MotorMove(1);
} else {
@ -152,19 +166,21 @@ void MotorMoveTo(uint8_t target) {
}
// The delay ratio controlls the PWM waveforms.
for(uint8_t i = 0; i < on_delay; i++){
_delay_us(MOTOR_PULSE);
for (idx = 0; idx < on_delay; idx++) {
_delay_us(MOTOR_PULSE);
}
MotorCoast();
for(uint8_t i = 0; i < off_delay; i++){
_delay_us(MOTOR_PULSE);
for (; idx < 255; idx++) {
_delay_us(MOTOR_PULSE);
}
}
return;
}
// Using the compatable bool type.
// The motor being used seems to stop working below a 50% duty cycle.
void MotorMove(uint8_t fwd) {
if (fwd) {
PORTB |= (1 << PWM_PIN1);
@ -187,21 +203,17 @@ void MotorCoast(void) {
*/
/*This is kinda like our button constructor*/
static void InitBtn(btn_state *b, uint8_t input_pin, uint8_t output_pin) {
static void InitBtn(btn_state *b, uint8_t input_pin) {
b->is_long_pressed = 0;
b->is_pressed = 0;
b->is_active = 0;
b->pressed_ticks = 0;
b->timer_enabled = 0;
b->input_pin = input_pin;
b->output_pin = output_pin;
/*Configure the buttons inputs and outputs*/
DDRB &= ~(1 << b->input_pin);
PORTB |= (1 << b->input_pin);
DDRB |= (1 << b->output_pin);
PORTB &= ~(1 << b->output_pin);
}
static void ClearButtonTimer(btn_state *b) {
@ -245,6 +257,7 @@ static void UpdateButtonOutput(btn_state *b) {
/*Then start the timer and update the output*/
// ToggleOutput(b);
StartButtonTimer(b);
return;
}
@ -264,12 +277,21 @@ static void UpdateButtonOutput(btn_state *b) {
else if (!b->is_pressed) {
/*If the button was released on a long press.*/
if (b->is_long_pressed) {
MotorSetSavePos();
MotorSetSavePos((uint8_t *)POSITION1_ADR);
}
/*If the button pres was a short one.*/
else if (!b->is_long_pressed) {
if (b->is_active) {
MotorMoveTo(MotorGetSavedPos());
/*If already in saved position then go back to original pos.*/
if(diff(MotorGetSavedPos((uint8_t *)POSITION1_ADR), ReadFader()) <= SAVED_POS_MARGIN){
MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION2_ADR));
}
else{
/*Save the current position into position 2 EEPROM*/
MotorSetSavePos((uint8_t *)POSITION2_ADR);
MotorMoveTo(MotorGetSavedPos((uint8_t *)POSITION1_ADR));
}
} else {
MotorCoast();
}

View file

@ -13,5 +13,6 @@ target_link_libraries(test_ADC
#Needed for the tests to function
include_directories(
/usr/local/avr/include/avr
#/usr/local/avr/avr/include #for Linux
#/usr/lib/avr/include/avr
)

View file

@ -73,116 +73,100 @@ TEST(test_ADC, ADC_SetupSetsRegisters)
ADC_Setup();
}
TEST(test_ADC, ADC_InitPortbADC3UsesCorrectRegisters)
TEST(test_ADC, ADC_InitRejectsInvalidChannels)
{
//PB3/ADC3
//Check it disables the pin's digital circuitry
//Check that the ADC pin is selected in ADMUX
//
/*
//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(7);
for(uint8_t i = 4; i < 255; i++){
ADC_Init(i);
}
}
TEST(test_ADC, ADC_InitPortAPin0UsesCorrectRegisters)
TEST(test_ADC, ADC_InitPortbWorksWithValidChannels)
{
/*
//Check for setting the direction to input.
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.DIR)
.withUnsignedIntParameter("bit_num", 0);
//Check that the pullup is off
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &PORTA.OUT)
.withUnsignedIntParameter("bit_num", 0);
uint8_t pin_num;
//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.PIN0CTRL)
.withUnsignedIntParameter("bit_num", PORT_ISC_INPUT_DISABLE_gc);
for(uint8_t adc_channel = 0; adc_channel <= 3; adc_channel++)
{
pin_num = ADC_GetChannelPinNum(adc_channel);
*/
//ADC_Init(0);
//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
//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);
}
}
TEST(test_ADC, ADC_InitDoesNothingOnHighPinNumbers)
{
//mock().expectNoCall("RegEdit_SetBit");
//ADC_Init(8);
}
TEST(test_ADC, ADC_EnablePasses)
{
/*
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &ADC0.CTRLA)
.withUnsignedIntParameter("bit_num", 0);
*/
//ADC_Enable();
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &ADCSRA)
.withUnsignedIntParameter("bit_num", ADEN);
ADC_Enable();
}
TEST(test_ADC, ADC_DisablePasses)
{
/*
mock().expectOneCall("RegEdit_ClearBit")
.withPointerParameter("reg", (void *) &ADC0.CTRLA)
.withUnsignedIntParameter("bit_num", 0);
*/
//ADC_Disable();
.withPointerParameter("reg", (void *) &ADCSRA)
.withUnsignedIntParameter("bit_num", ADEN);
ADC_Disable();
}
TEST(test_ADC, ADC_SetPinSetsRightRegisters)
TEST(test_ADC, ADC_ReadValueImplimentationTimesOut)
{
/*
//It clears existing MUXPOS register values.
mock().expectOneCall("RegEdit_ClearRegister")
.withPointerParameter("reg", (void *) &ADC0.MUXPOS);
//Expect the start conversion bit to be set.
mock().expectOneCall("RegEdit_SetBit")
.withPointerParameter("reg", (void *) &ADCSRA)
.withUnsignedIntParameter("bit_num", ADSC);
//It Correctly sets the pin number.
mock().expectOneCall("RegEdit_SetNum")
.withPointerParameter("reg", (void *) &ADC0.MUXPOS)
.withUnsignedIntParameter("num", 4);
*/
//ADC_SetPin(4);
//Check that it can time out.
//Expect it to read the start bit
mock().expectNCalls(ADC_WAIT_TIMEOUT, "RegEdit_IsBitSet")
.withPointerParameter("reg", (void *) &ADCSRA)
.withUnsignedIntParameter("bit_num", ADSC);
//Check that it return the "error" value
uint16_t value = ADC_ReadValue();
CHECK_EQUAL_TEXT(UINT16_MAX, value, "ERROR: Should return UINT16_MAX!");
}
TEST(test_ADC, ADC_SetPinFailsOnInvalidPin)
{
//ADC_SetPin(8);
}
static uint16_t ADC_ReadValueFake(uint8_t pin_num)
static uint16_t ADC_ReadValueFake()
{
return 512;
}
TEST_GROUP(test_ADCRead)
TEST_GROUP(test_ADCReadPtrSwap)
{
void setup()
{
@ -195,9 +179,9 @@ TEST_GROUP(test_ADCRead)
}
};
TEST(test_ADCRead, FunctionPointerSwapWorks)
TEST(test_ADCReadPtrSwap, FunctionPointerSwapWorks)
{
uint16_t value = ADC_ReadValue(0);
uint16_t value = ADC_ReadValue();
LONGS_EQUAL(512, value);
}

View file

@ -30,9 +30,9 @@ TEST_GROUP(test_MockADC)
TEST(test_MockADC, ADC_InitExpects)
{
mock().expectOneCall("ADC_Init")
.withUnsignedIntParameter("pin_num", 7);
.withUnsignedIntParameter("adc_chan", 0);
ADC_Init(7);
ADC_Init(0);
}
TEST(test_MockADC, ADC_EnableExpects)
@ -55,10 +55,9 @@ TEST(test_MockADC, ADC_ReadValue)
MockADC_ZeroIndex();
MockADC_PushValue(512);
mock().expectOneCall("ADC_ReadValue_Impl")
.withUnsignedIntParameter("pin_num", 0x2);
mock().expectOneCall("ADC_ReadValue_Impl");
uint16_t val = ADC_ReadValue(0x2);
uint16_t val = ADC_ReadValue();
LONGS_EQUAL(512, val);
}
@ -68,10 +67,9 @@ TEST(test_MockADC, ADC_ReadValueReturnsZeroOnEmptyBuffer)
MockADC_ZeroIndex();
mock().expectOneCall("ADC_ReadValue_Impl")
.withUnsignedIntParameter("pin_num", 0x2)
.andReturnValue(0x0000);
uint16_t val = ADC_ReadValue(0x2);
uint16_t val = ADC_ReadValue();
LONGS_EQUAL(0, val);
}